VersionCheckTest.php 10 KB

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