updater.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * Copyright (c) 2014 Lukas Reschke <lukas@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC;
  9. class UpdaterTest extends \Test\TestCase {
  10. public function versionCompatibilityTestData() {
  11. return array(
  12. array('1.0.0.0', '2.2.0', true),
  13. array('1.1.1.1', '2.0.0', true),
  14. array('5.0.3', '4.0.3', false),
  15. array('12.0.3', '13.4.5', true),
  16. array('1', '2', true),
  17. array('2', '2', true),
  18. array('6.0.5', '6.0.6', true),
  19. array('5.0.6', '7.0.4', false)
  20. );
  21. }
  22. /**
  23. * @dataProvider versionCompatibilityTestData
  24. */
  25. public function testIsUpgradePossible($oldVersion, $newVersion, $result) {
  26. $updater = new Updater(\OC::$server->getHTTPHelper(), \OC::$server->getConfig());
  27. $this->assertSame($result, $updater->isUpgradePossible($oldVersion, $newVersion));
  28. }
  29. public function testBrokenXmlResponse(){
  30. $invalidUpdater = $this->getUpdaterMock('OMG!');
  31. $invalidResult = $invalidUpdater->check();
  32. $this->assertEmpty($invalidResult);
  33. }
  34. public function testEmptyResponse(){
  35. $emptyUpdater = $this->getUpdaterMock('');
  36. $emptyResult = $emptyUpdater->check();
  37. $this->assertEmpty($emptyResult);
  38. // Error while fetching new contents e.g. too many redirects
  39. $falseUpdater = $this->getUpdaterMock(false);
  40. $falseResult = $falseUpdater->check();
  41. $this->assertEmpty($falseResult);
  42. }
  43. public function testValidEmptyXmlResponse(){
  44. $updater = $this->getUpdaterMock(
  45. '<?xml version="1.0"?><owncloud><version></version><versionstring></versionstring><url></url><web></web></owncloud>'
  46. );
  47. $result = array_map('strval', $updater->check());
  48. $this->assertArrayHasKey('version', $result);
  49. $this->assertArrayHasKey('versionstring', $result);
  50. $this->assertArrayHasKey('url', $result);
  51. $this->assertArrayHasKey('web', $result);
  52. $this->assertEmpty($result['version']);
  53. $this->assertEmpty($result['versionstring']);
  54. $this->assertEmpty($result['url']);
  55. $this->assertEmpty($result['web']);
  56. }
  57. public function testValidUpdateResponse(){
  58. $newUpdater = $this->getUpdaterMock(
  59. '<?xml version="1.0"?>
  60. <owncloud>
  61. <version>7.0.3.4</version>
  62. <versionstring>ownCloud 7.0.3</versionstring>
  63. <url>http://download.owncloud.org/community/owncloud-7.0.3.zip</url>
  64. <web>http://owncloud.org/</web>
  65. </owncloud>'
  66. );
  67. $newResult = array_map('strval', $newUpdater->check());
  68. $this->assertArrayHasKey('version', $newResult);
  69. $this->assertArrayHasKey('versionstring', $newResult);
  70. $this->assertArrayHasKey('url', $newResult);
  71. $this->assertArrayHasKey('web', $newResult);
  72. $this->assertEquals('7.0.3.4', $newResult['version']);
  73. $this->assertEquals('ownCloud 7.0.3', $newResult['versionstring']);
  74. $this->assertEquals('http://download.owncloud.org/community/owncloud-7.0.3.zip', $newResult['url']);
  75. $this->assertEquals('http://owncloud.org/', $newResult['web']);
  76. }
  77. protected function getUpdaterMock($content){
  78. // Invalidate cache
  79. $mockedConfig = $this->getMockBuilder('\OCP\IConfig')
  80. ->disableOriginalConstructor()
  81. ->getMock()
  82. ;
  83. $clientService = $this->getMock('\OCP\Http\Client\IClientService');
  84. $mockedHTTPHelper = $this->getMockBuilder('\OC\HTTPHelper')
  85. ->setConstructorArgs([\OC::$server->getConfig(), $clientService])
  86. ->getMock()
  87. ;
  88. $mockedHTTPHelper->expects($this->once())->method('getUrlContent')->will($this->returnValue($content));
  89. return new Updater($mockedHTTPHelper, $mockedConfig);
  90. }
  91. }