UpdateCheckerTest.php 3.9 KB

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