VersionCheckTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /**
  3. * @author Lukas Reschke <lukas@owncloud.com>
  4. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  5. *
  6. * @copyright Copyright (c) 2015, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace Test\Updater;
  23. use OC\Updater\VersionCheck;
  24. use OCP\IConfig;
  25. use OCP\Util;
  26. class VersionCheckTest extends \Test\TestCase {
  27. /** @var IConfig| \PHPUnit_Framework_MockObject_MockObject */
  28. private $config;
  29. /** @var VersionCheck | \PHPUnit_Framework_MockObject_MockObject*/
  30. private $updater;
  31. public function setUp() {
  32. parent::setUp();
  33. $this->config = $this->getMockBuilder('\OCP\IConfig')
  34. ->disableOriginalConstructor()
  35. ->getMock();
  36. $clientService = $this->getMockBuilder('\OCP\Http\Client\IClientService')
  37. ->disableOriginalConstructor()
  38. ->getMock();
  39. $this->updater = $this->getMockBuilder(VersionCheck::class)
  40. ->setMethods(['getUrlContent'])
  41. ->setConstructorArgs([$clientService, $this->config])
  42. ->getMock();
  43. }
  44. /**
  45. * @param string $baseUrl
  46. * @return string
  47. */
  48. private function buildUpdateUrl($baseUrl) {
  49. return $baseUrl . '?version='.implode('x', Util::getVersion()).'xinstalledatxlastupdatedatx'.\OC_Util::getChannel().'xxx'.PHP_MAJOR_VERSION.'x'.PHP_MINOR_VERSION.'x'.PHP_RELEASE_VERSION;
  50. }
  51. public function testCheckInCache() {
  52. $expectedResult = [
  53. 'version' => '8.0.4.2',
  54. 'versionstring' => 'ownCloud 8.0.4',
  55. 'url' => 'https://download.owncloud.org/community/owncloud-8.0.4.zip',
  56. 'web' => 'http://doc.owncloud.org/server/8.0/admin_manual/maintenance/upgrade.html',
  57. ];
  58. $this->config
  59. ->expects($this->at(0))
  60. ->method('getAppValue')
  61. ->with('core', 'lastupdatedat')
  62. ->will($this->returnValue(time()));
  63. $this->config
  64. ->expects($this->at(1))
  65. ->method('getAppValue')
  66. ->with('core', 'lastupdateResult')
  67. ->will($this->returnValue(json_encode($expectedResult)));
  68. $this->assertSame($expectedResult, $this->updater->check());
  69. }
  70. public function testCheckWithoutUpdateUrl() {
  71. $expectedResult = [
  72. 'version' => '8.0.4.2',
  73. 'versionstring' => 'ownCloud 8.0.4',
  74. 'url' => 'https://download.owncloud.org/community/owncloud-8.0.4.zip',
  75. 'web' => 'http://doc.owncloud.org/server/8.0/admin_manual/maintenance/upgrade.html',
  76. 'autoupdater' => '0',
  77. ];
  78. $this->config
  79. ->expects($this->at(0))
  80. ->method('getAppValue')
  81. ->with('core', 'lastupdatedat')
  82. ->will($this->returnValue(0));
  83. $this->config
  84. ->expects($this->at(1))
  85. ->method('getSystemValue')
  86. ->with('updater.server.url', 'https://updates.nextcloud.com/updater_server/')
  87. ->willReturnArgument(1);
  88. $this->config
  89. ->expects($this->at(2))
  90. ->method('setAppValue')
  91. ->with('core', 'lastupdatedat', $this->isType('integer'));
  92. $this->config
  93. ->expects($this->at(4))
  94. ->method('getAppValue')
  95. ->with('core', 'installedat')
  96. ->will($this->returnValue('installedat'));
  97. $this->config
  98. ->expects($this->at(5))
  99. ->method('getAppValue')
  100. ->with('core', 'lastupdatedat')
  101. ->will($this->returnValue('lastupdatedat'));
  102. $this->config
  103. ->expects($this->at(6))
  104. ->method('setAppValue')
  105. ->with('core', 'lastupdateResult', json_encode($expectedResult));
  106. $updateXml = '<?xml version="1.0"?>
  107. <owncloud>
  108. <version>8.0.4.2</version>
  109. <versionstring>ownCloud 8.0.4</versionstring>
  110. <url>https://download.owncloud.org/community/owncloud-8.0.4.zip</url>
  111. <web>http://doc.owncloud.org/server/8.0/admin_manual/maintenance/upgrade.html</web>
  112. <autoupdater>0</autoupdater>
  113. </owncloud>';
  114. $this->updater
  115. ->expects($this->once())
  116. ->method('getUrlContent')
  117. ->with($this->buildUpdateUrl('https://updates.nextcloud.com/updater_server/'))
  118. ->will($this->returnValue($updateXml));
  119. $this->assertSame($expectedResult, $this->updater->check());
  120. }
  121. public function testCheckWithInvalidXml() {
  122. $this->config
  123. ->expects($this->at(0))
  124. ->method('getAppValue')
  125. ->with('core', 'lastupdatedat')
  126. ->will($this->returnValue(0));
  127. $this->config
  128. ->expects($this->at(1))
  129. ->method('getSystemValue')
  130. ->with('updater.server.url', 'https://updates.nextcloud.com/updater_server/')
  131. ->willReturnArgument(1);
  132. $this->config
  133. ->expects($this->at(2))
  134. ->method('setAppValue')
  135. ->with('core', 'lastupdatedat', $this->isType('integer'));
  136. $this->config
  137. ->expects($this->at(4))
  138. ->method('getAppValue')
  139. ->with('core', 'installedat')
  140. ->will($this->returnValue('installedat'));
  141. $this->config
  142. ->expects($this->at(5))
  143. ->method('getAppValue')
  144. ->with('core', 'lastupdatedat')
  145. ->will($this->returnValue('lastupdatedat'));
  146. $this->config
  147. ->expects($this->at(6))
  148. ->method('setAppValue')
  149. ->with('core', 'lastupdateResult', 'false');
  150. $updateXml = 'Invalid XML Response!';
  151. $this->updater
  152. ->expects($this->once())
  153. ->method('getUrlContent')
  154. ->with($this->buildUpdateUrl('https://updates.nextcloud.com/updater_server/'))
  155. ->will($this->returnValue($updateXml));
  156. $this->assertSame([], $this->updater->check());
  157. }
  158. public function testCheckWithEmptyValidXmlResponse() {
  159. $expectedResult = [
  160. 'version' => '',
  161. 'versionstring' => '',
  162. 'url' => '',
  163. 'web' => '',
  164. 'autoupdater' => '',
  165. ];
  166. $this->config
  167. ->expects($this->at(0))
  168. ->method('getAppValue')
  169. ->with('core', 'lastupdatedat')
  170. ->will($this->returnValue(0));
  171. $this->config
  172. ->expects($this->at(1))
  173. ->method('getSystemValue')
  174. ->with('updater.server.url', 'https://updates.nextcloud.com/updater_server/')
  175. ->willReturnArgument(1);
  176. $this->config
  177. ->expects($this->at(2))
  178. ->method('setAppValue')
  179. ->with('core', 'lastupdatedat', $this->isType('integer'));
  180. $this->config
  181. ->expects($this->at(4))
  182. ->method('getAppValue')
  183. ->with('core', 'installedat')
  184. ->will($this->returnValue('installedat'));
  185. $this->config
  186. ->expects($this->at(5))
  187. ->method('getAppValue')
  188. ->with('core', 'lastupdatedat')
  189. ->will($this->returnValue('lastupdatedat'));
  190. $updateXml = '<?xml version="1.0"?>
  191. <owncloud>
  192. <version></version>
  193. <versionstring></versionstring>
  194. <url></url>
  195. <web></web>
  196. <autoupdater></autoupdater>
  197. </owncloud>';
  198. $this->updater
  199. ->expects($this->once())
  200. ->method('getUrlContent')
  201. ->with($this->buildUpdateUrl('https://updates.nextcloud.com/updater_server/'))
  202. ->will($this->returnValue($updateXml));
  203. $this->assertSame($expectedResult, $this->updater->check());
  204. }
  205. public function testCheckWithEmptyInvalidXmlResponse() {
  206. $expectedResult = [];
  207. $this->config
  208. ->expects($this->at(0))
  209. ->method('getAppValue')
  210. ->with('core', 'lastupdatedat')
  211. ->will($this->returnValue(0));
  212. $this->config
  213. ->expects($this->at(1))
  214. ->method('getSystemValue')
  215. ->with('updater.server.url', 'https://updates.nextcloud.com/updater_server/')
  216. ->willReturnArgument(1);
  217. $this->config
  218. ->expects($this->at(2))
  219. ->method('setAppValue')
  220. ->with('core', 'lastupdatedat', $this->isType('integer'));
  221. $this->config
  222. ->expects($this->at(4))
  223. ->method('getAppValue')
  224. ->with('core', 'installedat')
  225. ->will($this->returnValue('installedat'));
  226. $this->config
  227. ->expects($this->at(5))
  228. ->method('getAppValue')
  229. ->with('core', 'lastupdatedat')
  230. ->will($this->returnValue('lastupdatedat'));
  231. $this->config
  232. ->expects($this->at(6))
  233. ->method('setAppValue')
  234. ->with('core', 'lastupdateResult', json_encode($expectedResult));
  235. $updateXml = '';
  236. $this->updater
  237. ->expects($this->once())
  238. ->method('getUrlContent')
  239. ->with($this->buildUpdateUrl('https://updates.nextcloud.com/updater_server/'))
  240. ->will($this->returnValue($updateXml));
  241. $this->assertSame($expectedResult, $this->updater->check());
  242. }
  243. }