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