You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VersionCheckTest.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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\Http\Client\IClientService;
  25. use OCP\IAppConfig;
  26. use OCP\IConfig;
  27. use OCP\IUserManager;
  28. use OCP\Support\Subscription\IRegistry;
  29. use OCP\Util;
  30. use Psr\Log\LoggerInterface;
  31. class VersionCheckTest extends \Test\TestCase {
  32. /** @var IConfig| \PHPUnit\Framework\MockObject\MockObject */
  33. private $config;
  34. /** @var IAppConfig| \PHPUnit\Framework\MockObject\MockObject */
  35. private $appConfig;
  36. /** @var VersionCheck | \PHPUnit\Framework\MockObject\MockObject*/
  37. private $updater;
  38. /** @var IRegistry | \PHPUnit\Framework\Mo2ckObject\MockObject*/
  39. private $registry;
  40. /** @var LoggerInterface | \PHPUnit\Framework\Mo2ckObject\MockObject*/
  41. private $logger;
  42. protected function setUp(): void {
  43. parent::setUp();
  44. $this->config = $this->getMockBuilder(IConfig::class)
  45. ->disableOriginalConstructor()
  46. ->getMock();
  47. $this->appConfig = $this->getMockBuilder(IAppConfig::class)
  48. ->disableOriginalConstructor()
  49. ->getMock();
  50. $clientService = $this->getMockBuilder(IClientService::class)
  51. ->disableOriginalConstructor()
  52. ->getMock();
  53. $this->registry = $this->createMock(IRegistry::class);
  54. $this->registry
  55. ->method('delegateHasValidSubscription')
  56. ->willReturn(false);
  57. $this->logger = $this->createMock(LoggerInterface::class);
  58. $this->updater = $this->getMockBuilder(VersionCheck::class)
  59. ->setMethods(['getUrlContent'])
  60. ->setConstructorArgs([
  61. $clientService,
  62. $this->config,
  63. $this->appConfig,
  64. $this->createMock(IUserManager::class),
  65. $this->registry,
  66. $this->logger,
  67. ])
  68. ->getMock();
  69. }
  70. /**
  71. * @param string $baseUrl
  72. * @return string
  73. */
  74. private function buildUpdateUrl($baseUrl) {
  75. return $baseUrl . '?version='.implode('x', Util::getVersion()).'xinstalledatx' . time() . 'x'.\OC_Util::getChannel().'xxx'.PHP_MAJOR_VERSION.'x'.PHP_MINOR_VERSION.'x'.PHP_RELEASE_VERSION.'x0x0';
  76. }
  77. public function testCheckInCache() {
  78. $expectedResult = [
  79. 'version' => '8.0.4.2',
  80. 'versionstring' => 'ownCloud 8.0.4',
  81. 'url' => 'https://download.example.org/community/owncloud-8.0.4.zip',
  82. 'web' => 'http://doc.example.org/server/8.0/admin_manual/maintenance/upgrade.html',
  83. 'changes' => '',
  84. ];
  85. $this->config
  86. ->expects($this->once())
  87. ->method('getSystemValueBool')
  88. ->with('has_internet_connection', true)
  89. ->willReturn(true);
  90. $this->appConfig
  91. ->expects($this->once())
  92. ->method('getValueInt')
  93. ->with('core', 'lastupdatedat')
  94. ->willReturn(time());
  95. $this->config
  96. ->expects($this->once())
  97. ->method('getAppValue')
  98. ->with('core', 'lastupdateResult')
  99. ->willReturn(json_encode($expectedResult));
  100. $this->assertSame($expectedResult, $this->updater->check());
  101. }
  102. public function testCheckWithoutUpdateUrl() {
  103. $expectedResult = [
  104. 'version' => '8.0.4.2',
  105. 'versionstring' => 'ownCloud 8.0.4',
  106. 'url' => 'https://download.example.org/community/owncloud-8.0.4.zip',
  107. 'web' => 'http://doc.example.org/server/8.0/admin_manual/maintenance/upgrade.html',
  108. 'changes' => '',
  109. 'autoupdater' => '0',
  110. 'eol' => '1',
  111. ];
  112. $this->config
  113. ->expects($this->once())
  114. ->method('getSystemValueBool')
  115. ->with('has_internet_connection', true)
  116. ->willReturn(true);
  117. $this->appConfig
  118. ->expects($this->exactly(2))
  119. ->method('getValueInt')
  120. ->with('core', 'lastupdatedat')
  121. ->willReturnOnConsecutiveCalls(
  122. 0,
  123. time(),
  124. );
  125. $this->config
  126. ->expects($this->exactly(2))
  127. ->method('getAppValue')
  128. ->with('core', 'installedat')
  129. ->willReturn('installedat');
  130. $this->config
  131. ->expects($this->once())
  132. ->method('getSystemValueString')
  133. ->with('updater.server.url', 'https://updates.nextcloud.com/updater_server/')
  134. ->willReturnArgument(1);
  135. $this->appConfig
  136. ->expects($this->once())
  137. ->method('setValueInt')
  138. ->with('core', 'lastupdatedat', time());
  139. $this->config
  140. ->expects($this->once())
  141. ->method('setAppValue')
  142. ->with('core', 'lastupdateResult', json_encode($expectedResult));
  143. $updateXml = '<?xml version="1.0"?>
  144. <owncloud>
  145. <version>8.0.4.2</version>
  146. <versionstring>ownCloud 8.0.4</versionstring>
  147. <url>https://download.example.org/community/owncloud-8.0.4.zip</url>
  148. <web>http://doc.example.org/server/8.0/admin_manual/maintenance/upgrade.html</web>
  149. <autoupdater>0</autoupdater>
  150. <eol>1</eol>
  151. </owncloud>';
  152. $this->updater
  153. ->expects($this->once())
  154. ->method('getUrlContent')
  155. ->with($this->buildUpdateUrl('https://updates.nextcloud.com/updater_server/'))
  156. ->willReturn($updateXml);
  157. $this->assertSame($expectedResult, $this->updater->check());
  158. }
  159. public function testCheckWithInvalidXml() {
  160. $this->config
  161. ->expects($this->once())
  162. ->method('getSystemValueBool')
  163. ->with('has_internet_connection', true)
  164. ->willReturn(true);
  165. $this->appConfig
  166. ->expects($this->exactly(2))
  167. ->method('getValueInt')
  168. ->with('core', 'lastupdatedat')
  169. ->willReturnOnConsecutiveCalls(
  170. 0,
  171. time(),
  172. );
  173. $this->config
  174. ->expects($this->exactly(2))
  175. ->method('getAppValue')
  176. ->with('core', 'installedat')
  177. ->willReturn('installedat');
  178. $this->config
  179. ->expects($this->once())
  180. ->method('getSystemValueString')
  181. ->with('updater.server.url', 'https://updates.nextcloud.com/updater_server/')
  182. ->willReturnArgument(1);
  183. $this->appConfig
  184. ->expects($this->once())
  185. ->method('setValueInt')
  186. ->with('core', 'lastupdatedat', time());
  187. $this->config
  188. ->expects($this->once())
  189. ->method('setAppValue')
  190. ->with('core', 'lastupdateResult', $this->isType('string'));
  191. $updateXml = 'Invalid XML Response!';
  192. $this->updater
  193. ->expects($this->once())
  194. ->method('getUrlContent')
  195. ->with($this->buildUpdateUrl('https://updates.nextcloud.com/updater_server/'))
  196. ->willReturn($updateXml);
  197. $this->assertSame([], $this->updater->check());
  198. }
  199. public function testCheckWithEmptyValidXmlResponse() {
  200. $expectedResult = [
  201. 'version' => '',
  202. 'versionstring' => '',
  203. 'url' => '',
  204. 'web' => '',
  205. 'changes' => '',
  206. 'autoupdater' => '',
  207. 'eol' => '0',
  208. ];
  209. $this->config
  210. ->expects($this->once())
  211. ->method('getSystemValueBool')
  212. ->with('has_internet_connection', true)
  213. ->willReturn(true);
  214. $this->appConfig
  215. ->expects($this->exactly(2))
  216. ->method('getValueInt')
  217. ->with('core', 'lastupdatedat')
  218. ->willReturnOnConsecutiveCalls(
  219. 0,
  220. time(),
  221. );
  222. $this->config
  223. ->expects($this->exactly(2))
  224. ->method('getAppValue')
  225. ->with('core', 'installedat')
  226. ->willReturn('installedat');
  227. $this->config
  228. ->expects($this->once())
  229. ->method('getSystemValueString')
  230. ->with('updater.server.url', 'https://updates.nextcloud.com/updater_server/')
  231. ->willReturnArgument(1);
  232. $this->appConfig
  233. ->expects($this->once())
  234. ->method('setValueInt')
  235. ->with('core', 'lastupdatedat', time());
  236. $this->config
  237. ->expects($this->once())
  238. ->method('setAppValue')
  239. ->with('core', 'lastupdateResult', $this->isType('string'));
  240. $updateXml = '<?xml version="1.0"?>
  241. <owncloud>
  242. <version></version>
  243. <versionstring></versionstring>
  244. <url></url>
  245. <web></web>
  246. <autoupdater></autoupdater>
  247. </owncloud>';
  248. $this->updater
  249. ->expects($this->once())
  250. ->method('getUrlContent')
  251. ->with($this->buildUpdateUrl('https://updates.nextcloud.com/updater_server/'))
  252. ->willReturn($updateXml);
  253. $this->assertSame($expectedResult, $this->updater->check());
  254. }
  255. public function testCheckWithEmptyInvalidXmlResponse() {
  256. $expectedResult = [];
  257. $this->config
  258. ->expects($this->once())
  259. ->method('getSystemValueBool')
  260. ->with('has_internet_connection', true)
  261. ->willReturn(true);
  262. $this->appConfig
  263. ->expects($this->exactly(2))
  264. ->method('getValueInt')
  265. ->with('core', 'lastupdatedat')
  266. ->willReturnOnConsecutiveCalls(
  267. 0,
  268. time(),
  269. );
  270. $this->config
  271. ->expects($this->exactly(2))
  272. ->method('getAppValue')
  273. ->with('core', 'installedat')
  274. ->willReturn('installedat');
  275. $this->config
  276. ->expects($this->once())
  277. ->method('getSystemValueString')
  278. ->with('updater.server.url', 'https://updates.nextcloud.com/updater_server/')
  279. ->willReturnArgument(1);
  280. $this->appConfig
  281. ->expects($this->once())
  282. ->method('setValueInt')
  283. ->with('core', 'lastupdatedat', time());
  284. $this->config
  285. ->expects($this->once())
  286. ->method('setAppValue')
  287. ->with('core', 'lastupdateResult', $this->isType('string'));
  288. $updateXml = '';
  289. $this->updater
  290. ->expects($this->once())
  291. ->method('getUrlContent')
  292. ->with($this->buildUpdateUrl('https://updates.nextcloud.com/updater_server/'))
  293. ->willReturn($updateXml);
  294. $this->assertSame($expectedResult, $this->updater->check());
  295. }
  296. public function testCheckWithMissingAttributeXmlResponse() {
  297. $expectedResult = [
  298. 'version' => '',
  299. 'versionstring' => '',
  300. 'url' => '',
  301. 'web' => '',
  302. 'changes' => '',
  303. 'autoupdater' => '',
  304. 'eol' => '0',
  305. ];
  306. $this->config
  307. ->expects($this->once())
  308. ->method('getSystemValueBool')
  309. ->with('has_internet_connection', true)
  310. ->willReturn(true);
  311. $this->appConfig
  312. ->expects($this->exactly(2))
  313. ->method('getValueInt')
  314. ->with('core', 'lastupdatedat')
  315. ->willReturnOnConsecutiveCalls(
  316. 0,
  317. time(),
  318. );
  319. $this->config
  320. ->expects($this->exactly(2))
  321. ->method('getAppValue')
  322. ->with('core', 'installedat')
  323. ->willReturn('installedat');
  324. $this->config
  325. ->expects($this->once())
  326. ->method('getSystemValueString')
  327. ->with('updater.server.url', 'https://updates.nextcloud.com/updater_server/')
  328. ->willReturnArgument(1);
  329. $this->appConfig
  330. ->expects($this->once())
  331. ->method('setValueInt')
  332. ->with('core', 'lastupdatedat', time());
  333. $this->config
  334. ->expects($this->once())
  335. ->method('setAppValue')
  336. ->with('core', 'lastupdateResult', $this->isType('string'));
  337. // missing autoupdater element should still not fail
  338. $updateXml = '<?xml version="1.0"?>
  339. <owncloud>
  340. <version></version>
  341. <versionstring></versionstring>
  342. <url></url>
  343. <web></web>
  344. </owncloud>';
  345. $this->updater
  346. ->expects($this->once())
  347. ->method('getUrlContent')
  348. ->with($this->buildUpdateUrl('https://updates.nextcloud.com/updater_server/'))
  349. ->willReturn($updateXml);
  350. $this->assertSame($expectedResult, $this->updater->check());
  351. }
  352. public function testNoInternet() {
  353. $this->config
  354. ->expects($this->once())
  355. ->method('getSystemValueBool')
  356. ->with('has_internet_connection', true)
  357. ->willReturn(false);
  358. $this->assertFalse($this->updater->check());
  359. }
  360. }