1
0

VersionCheckTest.php 9.5 KB

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