VersionCheckTest.php 11 KB

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