VersionCheckTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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->at(0))
  62. ->method('getSystemValueBool')
  63. ->with('has_internet_connection', true)
  64. ->willReturn(true);
  65. $this->config
  66. ->expects($this->at(1))
  67. ->method('getAppValue')
  68. ->with('core', 'lastupdatedat')
  69. ->willReturn(time());
  70. $this->config
  71. ->expects($this->at(2))
  72. ->method('getAppValue')
  73. ->with('core', 'lastupdateResult')
  74. ->willReturn(json_encode($expectedResult));
  75. $this->assertSame($expectedResult, $this->updater->check());
  76. }
  77. public function testCheckWithoutUpdateUrl() {
  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. 'autoupdater' => '0',
  85. 'eol' => '1',
  86. ];
  87. $this->config
  88. ->expects($this->at(0))
  89. ->method('getSystemValueBool')
  90. ->with('has_internet_connection', true)
  91. ->willReturn(true);
  92. $this->config
  93. ->expects($this->at(1))
  94. ->method('getAppValue')
  95. ->with('core', 'lastupdatedat')
  96. ->willReturn(0);
  97. $this->config
  98. ->expects($this->at(2))
  99. ->method('getSystemValue')
  100. ->with('updater.server.url', 'https://updates.nextcloud.com/updater_server/')
  101. ->willReturnArgument(1);
  102. $this->config
  103. ->expects($this->at(3))
  104. ->method('setAppValue')
  105. ->with('core', 'lastupdatedat', $this->isType('integer'));
  106. $this->config
  107. ->expects($this->at(5))
  108. ->method('getAppValue')
  109. ->with('core', 'installedat')
  110. ->willReturn('installedat');
  111. $this->config
  112. ->expects($this->at(6))
  113. ->method('getAppValue')
  114. ->with('core', 'lastupdatedat')
  115. ->willReturn('lastupdatedat');
  116. $this->config
  117. ->expects($this->at(7))
  118. ->method('setAppValue')
  119. ->with('core', 'lastupdateResult', json_encode($expectedResult));
  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->at(0))
  139. ->method('getSystemValueBool')
  140. ->with('has_internet_connection', true)
  141. ->willReturn(true);
  142. $this->config
  143. ->expects($this->at(1))
  144. ->method('getAppValue')
  145. ->with('core', 'lastupdatedat')
  146. ->willReturn(0);
  147. $this->config
  148. ->expects($this->at(2))
  149. ->method('getSystemValue')
  150. ->with('updater.server.url', 'https://updates.nextcloud.com/updater_server/')
  151. ->willReturnArgument(1);
  152. $this->config
  153. ->expects($this->at(3))
  154. ->method('setAppValue')
  155. ->with('core', 'lastupdatedat', $this->isType('integer'));
  156. $this->config
  157. ->expects($this->at(5))
  158. ->method('getAppValue')
  159. ->with('core', 'installedat')
  160. ->willReturn('installedat');
  161. $this->config
  162. ->expects($this->at(6))
  163. ->method('getAppValue')
  164. ->with('core', 'lastupdatedat')
  165. ->willReturn('lastupdatedat');
  166. $this->config
  167. ->expects($this->at(7))
  168. ->method('setAppValue')
  169. ->with('core', 'lastupdateResult', '[]');
  170. $updateXml = 'Invalid XML Response!';
  171. $this->updater
  172. ->expects($this->once())
  173. ->method('getUrlContent')
  174. ->with($this->buildUpdateUrl('https://updates.nextcloud.com/updater_server/'))
  175. ->willReturn($updateXml);
  176. $this->assertSame([], $this->updater->check());
  177. }
  178. public function testCheckWithEmptyValidXmlResponse() {
  179. $expectedResult = [
  180. 'version' => '',
  181. 'versionstring' => '',
  182. 'url' => '',
  183. 'web' => '',
  184. 'changes' => '',
  185. 'autoupdater' => '',
  186. 'eol' => '0',
  187. ];
  188. $this->config
  189. ->expects($this->at(0))
  190. ->method('getSystemValueBool')
  191. ->with('has_internet_connection', true)
  192. ->willReturn(true);
  193. $this->config
  194. ->expects($this->at(1))
  195. ->method('getAppValue')
  196. ->with('core', 'lastupdatedat')
  197. ->willReturn(0);
  198. $this->config
  199. ->expects($this->at(2))
  200. ->method('getSystemValue')
  201. ->with('updater.server.url', 'https://updates.nextcloud.com/updater_server/')
  202. ->willReturnArgument(1);
  203. $this->config
  204. ->expects($this->at(3))
  205. ->method('setAppValue')
  206. ->with('core', 'lastupdatedat', $this->isType('integer'));
  207. $this->config
  208. ->expects($this->at(5))
  209. ->method('getAppValue')
  210. ->with('core', 'installedat')
  211. ->willReturn('installedat');
  212. $this->config
  213. ->expects($this->at(6))
  214. ->method('getAppValue')
  215. ->with('core', 'lastupdatedat')
  216. ->willReturn('lastupdatedat');
  217. $updateXml = '<?xml version="1.0"?>
  218. <owncloud>
  219. <version></version>
  220. <versionstring></versionstring>
  221. <url></url>
  222. <web></web>
  223. <autoupdater></autoupdater>
  224. </owncloud>';
  225. $this->updater
  226. ->expects($this->once())
  227. ->method('getUrlContent')
  228. ->with($this->buildUpdateUrl('https://updates.nextcloud.com/updater_server/'))
  229. ->willReturn($updateXml);
  230. $this->assertSame($expectedResult, $this->updater->check());
  231. }
  232. public function testCheckWithEmptyInvalidXmlResponse() {
  233. $expectedResult = [];
  234. $this->config
  235. ->expects($this->at(0))
  236. ->method('getSystemValueBool')
  237. ->with('has_internet_connection', true)
  238. ->willReturn(true);
  239. $this->config
  240. ->expects($this->at(1))
  241. ->method('getAppValue')
  242. ->with('core', 'lastupdatedat')
  243. ->willReturn(0);
  244. $this->config
  245. ->expects($this->at(2))
  246. ->method('getSystemValue')
  247. ->with('updater.server.url', 'https://updates.nextcloud.com/updater_server/')
  248. ->willReturnArgument(1);
  249. $this->config
  250. ->expects($this->at(3))
  251. ->method('setAppValue')
  252. ->with('core', 'lastupdatedat', $this->isType('integer'));
  253. $this->config
  254. ->expects($this->at(5))
  255. ->method('getAppValue')
  256. ->with('core', 'installedat')
  257. ->willReturn('installedat');
  258. $this->config
  259. ->expects($this->at(6))
  260. ->method('getAppValue')
  261. ->with('core', 'lastupdatedat')
  262. ->willReturn('lastupdatedat');
  263. $this->config
  264. ->expects($this->at(7))
  265. ->method('setAppValue')
  266. ->with('core', 'lastupdateResult', json_encode($expectedResult));
  267. $updateXml = '';
  268. $this->updater
  269. ->expects($this->once())
  270. ->method('getUrlContent')
  271. ->with($this->buildUpdateUrl('https://updates.nextcloud.com/updater_server/'))
  272. ->willReturn($updateXml);
  273. $this->assertSame($expectedResult, $this->updater->check());
  274. }
  275. public function testCheckWithMissingAttributeXmlResponse() {
  276. $expectedResult = [
  277. 'version' => '',
  278. 'versionstring' => '',
  279. 'url' => '',
  280. 'web' => '',
  281. 'changes' => '',
  282. 'autoupdater' => '',
  283. 'eol' => '0',
  284. ];
  285. $this->config
  286. ->expects($this->at(0))
  287. ->method('getSystemValueBool')
  288. ->with('has_internet_connection', true)
  289. ->willReturn(true);
  290. $this->config
  291. ->expects($this->at(1))
  292. ->method('getAppValue')
  293. ->with('core', 'lastupdatedat')
  294. ->willReturn(0);
  295. $this->config
  296. ->expects($this->at(2))
  297. ->method('getSystemValue')
  298. ->with('updater.server.url', 'https://updates.nextcloud.com/updater_server/')
  299. ->willReturnArgument(1);
  300. $this->config
  301. ->expects($this->at(3))
  302. ->method('setAppValue')
  303. ->with('core', 'lastupdatedat', $this->isType('integer'));
  304. $this->config
  305. ->expects($this->at(5))
  306. ->method('getAppValue')
  307. ->with('core', 'installedat')
  308. ->willReturn('installedat');
  309. $this->config
  310. ->expects($this->at(6))
  311. ->method('getAppValue')
  312. ->with('core', 'lastupdatedat')
  313. ->willReturn('lastupdatedat');
  314. // missing autoupdater element should still not fail
  315. $updateXml = '<?xml version="1.0"?>
  316. <owncloud>
  317. <version></version>
  318. <versionstring></versionstring>
  319. <url></url>
  320. <web></web>
  321. </owncloud>';
  322. $this->updater
  323. ->expects($this->once())
  324. ->method('getUrlContent')
  325. ->with($this->buildUpdateUrl('https://updates.nextcloud.com/updater_server/'))
  326. ->willReturn($updateXml);
  327. $this->assertSame($expectedResult, $this->updater->check());
  328. }
  329. public function testNoInternet() {
  330. $this->config
  331. ->expects($this->at(0))
  332. ->method('getSystemValueBool')
  333. ->with('has_internet_connection', true)
  334. ->willReturn(false);
  335. $this->assertFalse($this->updater->check());
  336. }
  337. }