UpdateCheckerTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\UpdateNotification\Tests;
  8. use OC\Updater\ChangesCheck;
  9. use OC\Updater\VersionCheck;
  10. use OCA\UpdateNotification\UpdateChecker;
  11. use Test\TestCase;
  12. class UpdateCheckerTest extends TestCase {
  13. /** @var ChangesCheck|\PHPUnit\Framework\MockObject\MockObject */
  14. protected $changesChecker;
  15. /** @var VersionCheck|\PHPUnit\Framework\MockObject\MockObject */
  16. private $updater;
  17. /** @var UpdateChecker */
  18. private $updateChecker;
  19. protected function setUp(): void {
  20. parent::setUp();
  21. $this->updater = $this->createMock(VersionCheck::class);
  22. $this->changesChecker = $this->createMock(ChangesCheck::class);
  23. $this->updateChecker = new UpdateChecker($this->updater, $this->changesChecker);
  24. }
  25. public function testGetUpdateStateWithUpdateAndInvalidLink() {
  26. $this->updater
  27. ->expects($this->once())
  28. ->method('check')
  29. ->willReturn([
  30. 'version' => '1.2.3',
  31. 'versionstring' => 'Nextcloud 1.2.3',
  32. 'web' => 'javascript:alert(1)',
  33. 'url' => 'javascript:alert(2)',
  34. 'changes' => 'javascript:alert(3)',
  35. 'autoupdater' => '0',
  36. 'eol' => '1',
  37. ]);
  38. $expected = [
  39. 'updateAvailable' => true,
  40. 'updateVersion' => '1.2.3',
  41. 'updateVersionString' => 'Nextcloud 1.2.3',
  42. 'updaterEnabled' => false,
  43. 'versionIsEol' => true,
  44. ];
  45. $this->assertSame($expected, $this->updateChecker->getUpdateState());
  46. }
  47. public function testGetUpdateStateWithUpdateAndValidLink() {
  48. $changes = [
  49. 'changelog' => 'https://nextcloud.com/changelog/#123-0-0',
  50. 'whatsNew' => [
  51. 'en' => [
  52. 'regular' => [
  53. 'Yardarm heave to brig spyglass smartly pillage',
  54. 'Bounty gangway bilge skysail rope\'s end',
  55. 'Maroon cutlass spirits nipperkin Plate Fleet',
  56. ],
  57. 'admin' => [
  58. 'Scourge of the seven seas coffer doubloon',
  59. 'Brig me splice the main brace',
  60. ]
  61. ]
  62. ]
  63. ];
  64. $this->updater
  65. ->expects($this->once())
  66. ->method('check')
  67. ->willReturn([
  68. 'version' => '1.2.3',
  69. 'versionstring' => 'Nextcloud 1.2.3',
  70. 'web' => 'https://docs.nextcloud.com/myUrl',
  71. 'url' => 'https://downloads.nextcloud.org/server',
  72. 'changes' => 'https://updates.nextcloud.com/changelog_server/?version=123.0.0',
  73. 'autoupdater' => '1',
  74. 'eol' => '0',
  75. ]);
  76. $this->changesChecker->expects($this->once())
  77. ->method('check')
  78. ->willReturn($changes);
  79. $expected = [
  80. 'updateAvailable' => true,
  81. 'updateVersion' => '1.2.3',
  82. 'updateVersionString' => 'Nextcloud 1.2.3',
  83. 'updaterEnabled' => true,
  84. 'versionIsEol' => false,
  85. 'updateLink' => 'https://docs.nextcloud.com/myUrl',
  86. 'downloadLink' => 'https://downloads.nextcloud.org/server',
  87. 'changes' => $changes,
  88. ];
  89. $this->assertSame($expected, $this->updateChecker->getUpdateState());
  90. }
  91. public function testGetUpdateStateWithoutUpdate() {
  92. $this->updater
  93. ->expects($this->once())
  94. ->method('check')
  95. ->willReturn([]);
  96. $expected = [];
  97. $this->assertSame($expected, $this->updateChecker->getUpdateState());
  98. }
  99. }