VersionCheckTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. public function setUp() {
  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('getAppValue')
  63. ->with('core', 'lastupdatedat')
  64. ->will($this->returnValue(time()));
  65. $this->config
  66. ->expects($this->at(1))
  67. ->method('getAppValue')
  68. ->with('core', 'lastupdateResult')
  69. ->will($this->returnValue(json_encode($expectedResult)));
  70. $this->assertSame($expectedResult, $this->updater->check());
  71. }
  72. public function testCheckWithoutUpdateUrl() {
  73. $expectedResult = [
  74. 'version' => '8.0.4.2',
  75. 'versionstring' => 'ownCloud 8.0.4',
  76. 'url' => 'https://download.example.org/community/owncloud-8.0.4.zip',
  77. 'web' => 'http://doc.example.org/server/8.0/admin_manual/maintenance/upgrade.html',
  78. 'changes' => '',
  79. 'autoupdater' => '0',
  80. 'eol' => '1',
  81. ];
  82. $this->config
  83. ->expects($this->at(0))
  84. ->method('getAppValue')
  85. ->with('core', 'lastupdatedat')
  86. ->will($this->returnValue(0));
  87. $this->config
  88. ->expects($this->at(1))
  89. ->method('getSystemValue')
  90. ->with('updater.server.url', 'https://updates.nextcloud.com/updater_server/')
  91. ->willReturnArgument(1);
  92. $this->config
  93. ->expects($this->at(2))
  94. ->method('setAppValue')
  95. ->with('core', 'lastupdatedat', $this->isType('integer'));
  96. $this->config
  97. ->expects($this->at(4))
  98. ->method('getAppValue')
  99. ->with('core', 'installedat')
  100. ->will($this->returnValue('installedat'));
  101. $this->config
  102. ->expects($this->at(5))
  103. ->method('getAppValue')
  104. ->with('core', 'lastupdatedat')
  105. ->will($this->returnValue('lastupdatedat'));
  106. $this->config
  107. ->expects($this->at(6))
  108. ->method('setAppValue')
  109. ->with('core', 'lastupdateResult', json_encode($expectedResult));
  110. $updateXml = '<?xml version="1.0"?>
  111. <owncloud>
  112. <version>8.0.4.2</version>
  113. <versionstring>ownCloud 8.0.4</versionstring>
  114. <url>https://download.example.org/community/owncloud-8.0.4.zip</url>
  115. <web>http://doc.example.org/server/8.0/admin_manual/maintenance/upgrade.html</web>
  116. <autoupdater>0</autoupdater>
  117. <eol>1</eol>
  118. </owncloud>';
  119. $this->updater
  120. ->expects($this->once())
  121. ->method('getUrlContent')
  122. ->with($this->buildUpdateUrl('https://updates.nextcloud.com/updater_server/'))
  123. ->will($this->returnValue($updateXml));
  124. $this->assertSame($expectedResult, $this->updater->check());
  125. }
  126. public function testCheckWithInvalidXml() {
  127. $this->config
  128. ->expects($this->at(0))
  129. ->method('getAppValue')
  130. ->with('core', 'lastupdatedat')
  131. ->will($this->returnValue(0));
  132. $this->config
  133. ->expects($this->at(1))
  134. ->method('getSystemValue')
  135. ->with('updater.server.url', 'https://updates.nextcloud.com/updater_server/')
  136. ->willReturnArgument(1);
  137. $this->config
  138. ->expects($this->at(2))
  139. ->method('setAppValue')
  140. ->with('core', 'lastupdatedat', $this->isType('integer'));
  141. $this->config
  142. ->expects($this->at(4))
  143. ->method('getAppValue')
  144. ->with('core', 'installedat')
  145. ->will($this->returnValue('installedat'));
  146. $this->config
  147. ->expects($this->at(5))
  148. ->method('getAppValue')
  149. ->with('core', 'lastupdatedat')
  150. ->will($this->returnValue('lastupdatedat'));
  151. $this->config
  152. ->expects($this->at(6))
  153. ->method('setAppValue')
  154. ->with('core', 'lastupdateResult', '[]');
  155. $updateXml = 'Invalid XML Response!';
  156. $this->updater
  157. ->expects($this->once())
  158. ->method('getUrlContent')
  159. ->with($this->buildUpdateUrl('https://updates.nextcloud.com/updater_server/'))
  160. ->will($this->returnValue($updateXml));
  161. $this->assertSame([], $this->updater->check());
  162. }
  163. public function testCheckWithEmptyValidXmlResponse() {
  164. $expectedResult = [
  165. 'version' => '',
  166. 'versionstring' => '',
  167. 'url' => '',
  168. 'web' => '',
  169. 'changes' => '',
  170. 'autoupdater' => '',
  171. 'eol' => '0',
  172. ];
  173. $this->config
  174. ->expects($this->at(0))
  175. ->method('getAppValue')
  176. ->with('core', 'lastupdatedat')
  177. ->will($this->returnValue(0));
  178. $this->config
  179. ->expects($this->at(1))
  180. ->method('getSystemValue')
  181. ->with('updater.server.url', 'https://updates.nextcloud.com/updater_server/')
  182. ->willReturnArgument(1);
  183. $this->config
  184. ->expects($this->at(2))
  185. ->method('setAppValue')
  186. ->with('core', 'lastupdatedat', $this->isType('integer'));
  187. $this->config
  188. ->expects($this->at(4))
  189. ->method('getAppValue')
  190. ->with('core', 'installedat')
  191. ->will($this->returnValue('installedat'));
  192. $this->config
  193. ->expects($this->at(5))
  194. ->method('getAppValue')
  195. ->with('core', 'lastupdatedat')
  196. ->will($this->returnValue('lastupdatedat'));
  197. $updateXml = '<?xml version="1.0"?>
  198. <owncloud>
  199. <version></version>
  200. <versionstring></versionstring>
  201. <url></url>
  202. <web></web>
  203. <autoupdater></autoupdater>
  204. </owncloud>';
  205. $this->updater
  206. ->expects($this->once())
  207. ->method('getUrlContent')
  208. ->with($this->buildUpdateUrl('https://updates.nextcloud.com/updater_server/'))
  209. ->will($this->returnValue($updateXml));
  210. $this->assertSame($expectedResult, $this->updater->check());
  211. }
  212. public function testCheckWithEmptyInvalidXmlResponse() {
  213. $expectedResult = [];
  214. $this->config
  215. ->expects($this->at(0))
  216. ->method('getAppValue')
  217. ->with('core', 'lastupdatedat')
  218. ->will($this->returnValue(0));
  219. $this->config
  220. ->expects($this->at(1))
  221. ->method('getSystemValue')
  222. ->with('updater.server.url', 'https://updates.nextcloud.com/updater_server/')
  223. ->willReturnArgument(1);
  224. $this->config
  225. ->expects($this->at(2))
  226. ->method('setAppValue')
  227. ->with('core', 'lastupdatedat', $this->isType('integer'));
  228. $this->config
  229. ->expects($this->at(4))
  230. ->method('getAppValue')
  231. ->with('core', 'installedat')
  232. ->will($this->returnValue('installedat'));
  233. $this->config
  234. ->expects($this->at(5))
  235. ->method('getAppValue')
  236. ->with('core', 'lastupdatedat')
  237. ->will($this->returnValue('lastupdatedat'));
  238. $this->config
  239. ->expects($this->at(6))
  240. ->method('setAppValue')
  241. ->with('core', 'lastupdateResult', json_encode($expectedResult));
  242. $updateXml = '';
  243. $this->updater
  244. ->expects($this->once())
  245. ->method('getUrlContent')
  246. ->with($this->buildUpdateUrl('https://updates.nextcloud.com/updater_server/'))
  247. ->will($this->returnValue($updateXml));
  248. $this->assertSame($expectedResult, $this->updater->check());
  249. }
  250. public function testCheckWithMissingAttributeXmlResponse() {
  251. $expectedResult = [
  252. 'version' => '',
  253. 'versionstring' => '',
  254. 'url' => '',
  255. 'web' => '',
  256. 'changes' => '',
  257. 'autoupdater' => '',
  258. 'eol' => '0',
  259. ];
  260. $this->config
  261. ->expects($this->at(0))
  262. ->method('getAppValue')
  263. ->with('core', 'lastupdatedat')
  264. ->will($this->returnValue(0));
  265. $this->config
  266. ->expects($this->at(1))
  267. ->method('getSystemValue')
  268. ->with('updater.server.url', 'https://updates.nextcloud.com/updater_server/')
  269. ->willReturnArgument(1);
  270. $this->config
  271. ->expects($this->at(2))
  272. ->method('setAppValue')
  273. ->with('core', 'lastupdatedat', $this->isType('integer'));
  274. $this->config
  275. ->expects($this->at(4))
  276. ->method('getAppValue')
  277. ->with('core', 'installedat')
  278. ->will($this->returnValue('installedat'));
  279. $this->config
  280. ->expects($this->at(5))
  281. ->method('getAppValue')
  282. ->with('core', 'lastupdatedat')
  283. ->will($this->returnValue('lastupdatedat'));
  284. // missing autoupdater element should still not fail
  285. $updateXml = '<?xml version="1.0"?>
  286. <owncloud>
  287. <version></version>
  288. <versionstring></versionstring>
  289. <url></url>
  290. <web></web>
  291. </owncloud>';
  292. $this->updater
  293. ->expects($this->once())
  294. ->method('getUrlContent')
  295. ->with($this->buildUpdateUrl('https://updates.nextcloud.com/updater_server/'))
  296. ->will($this->returnValue($updateXml));
  297. $this->assertSame($expectedResult, $this->updater->check());
  298. }
  299. }