VersionCheckTest.php 10 KB

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