UpdateCheckerTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\UpdateNotification\Tests;
  27. use OC\Updater\ChangesCheck;
  28. use OC\Updater\VersionCheck;
  29. use OCA\UpdateNotification\UpdateChecker;
  30. use Test\TestCase;
  31. class UpdateCheckerTest extends TestCase {
  32. /** @var ChangesCheck|\PHPUnit_Framework_MockObject_MockObject */
  33. protected $changesChecker;
  34. /** @var VersionCheck|\PHPUnit_Framework_MockObject_MockObject */
  35. private $updater;
  36. /** @var UpdateChecker */
  37. private $updateChecker;
  38. protected function setUp(): void {
  39. parent::setUp();
  40. $this->updater = $this->createMock(VersionCheck::class);
  41. $this->changesChecker = $this->createMock(ChangesCheck::class);
  42. $this->updateChecker = new UpdateChecker($this->updater, $this->changesChecker);
  43. }
  44. public function testGetUpdateStateWithUpdateAndInvalidLink() {
  45. $this->updater
  46. ->expects($this->once())
  47. ->method('check')
  48. ->willReturn([
  49. 'version' => '1.2.3',
  50. 'versionstring' => 'Nextcloud 1.2.3',
  51. 'web'=> 'javascript:alert(1)',
  52. 'url'=> 'javascript:alert(2)',
  53. 'changes' => 'javascript:alert(3)',
  54. 'autoupdater'=> '0',
  55. 'eol'=> '1',
  56. ]);
  57. $expected = [
  58. 'updateAvailable' => true,
  59. 'updateVersion' => '1.2.3',
  60. 'updateVersionString' => 'Nextcloud 1.2.3',
  61. 'updaterEnabled' => false,
  62. 'versionIsEol' => true,
  63. ];
  64. $this->assertSame($expected, $this->updateChecker->getUpdateState());
  65. }
  66. public function testGetUpdateStateWithUpdateAndValidLink() {
  67. $changes = [
  68. 'changelog' => 'https://nextcloud.com/changelog/#123-0-0',
  69. 'whatsNew' => [
  70. 'en' => [
  71. 'regular' => [
  72. 'Yardarm heave to brig spyglass smartly pillage',
  73. 'Bounty gangway bilge skysail rope\'s end',
  74. 'Maroon cutlass spirits nipperkin Plate Fleet',
  75. ],
  76. 'admin' => [
  77. 'Scourge of the seven seas coffer doubloon',
  78. 'Brig me splice the main brace',
  79. ]
  80. ]
  81. ]
  82. ];
  83. $this->updater
  84. ->expects($this->once())
  85. ->method('check')
  86. ->willReturn([
  87. 'version' => '1.2.3',
  88. 'versionstring' => 'Nextcloud 1.2.3',
  89. 'web'=> 'https://docs.nextcloud.com/myUrl',
  90. 'url'=> 'https://downloads.nextcloud.org/server',
  91. 'changes' => 'https://updates.nextcloud.com/changelog_server/?version=123.0.0',
  92. 'autoupdater'=> '1',
  93. 'eol'=> '0',
  94. ]);
  95. $this->changesChecker->expects($this->once())
  96. ->method('check')
  97. ->willReturn($changes);
  98. $expected = [
  99. 'updateAvailable' => true,
  100. 'updateVersion' => '1.2.3',
  101. 'updateVersionString' => 'Nextcloud 1.2.3',
  102. 'updaterEnabled' => true,
  103. 'versionIsEol' => false,
  104. 'updateLink' => 'https://docs.nextcloud.com/myUrl',
  105. 'downloadLink' => 'https://downloads.nextcloud.org/server',
  106. 'changes' => $changes,
  107. ];
  108. $this->assertSame($expected, $this->updateChecker->getUpdateState());
  109. }
  110. public function testGetUpdateStateWithoutUpdate() {
  111. $this->updater
  112. ->expects($this->once())
  113. ->method('check')
  114. ->willReturn([]);
  115. $expected = [];
  116. $this->assertSame($expected, $this->updateChecker->getUpdateState());
  117. }
  118. }