1
0

VersionCheckTest.php 11 KB

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