1
0

UpdateCheckerTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 OCP\AppFramework\Services\IInitialState;
  12. use PHPUnit\Framework\MockObject\MockObject;
  13. use Test\TestCase;
  14. class UpdateCheckerTest extends TestCase {
  15. private ChangesCheck&MockObject $changesChecker;
  16. private VersionCheck&MockObject $updater;
  17. private IInitialState&MockObject $initialState;
  18. private UpdateChecker $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->initialState = $this->createMock(IInitialState::class);
  24. $this->updateChecker = new UpdateChecker(
  25. $this->updater,
  26. $this->changesChecker,
  27. $this->initialState,
  28. );
  29. }
  30. public function testGetUpdateStateWithUpdateAndInvalidLink(): void {
  31. $this->updater
  32. ->expects($this->once())
  33. ->method('check')
  34. ->willReturn([
  35. 'version' => '1.2.3',
  36. 'versionstring' => 'Nextcloud 1.2.3',
  37. 'web' => 'javascript:alert(1)',
  38. 'url' => 'javascript:alert(2)',
  39. 'changes' => 'javascript:alert(3)',
  40. 'autoupdater' => '0',
  41. 'eol' => '1',
  42. ]);
  43. $expected = [
  44. 'updateAvailable' => true,
  45. 'updateVersion' => '1.2.3',
  46. 'updateVersionString' => 'Nextcloud 1.2.3',
  47. 'updaterEnabled' => false,
  48. 'versionIsEol' => true,
  49. ];
  50. $this->assertSame($expected, $this->updateChecker->getUpdateState());
  51. }
  52. public function testGetUpdateStateWithUpdateAndValidLink(): void {
  53. $changes = [
  54. 'changelog' => 'https://nextcloud.com/changelog/#123-0-0',
  55. 'whatsNew' => [
  56. 'en' => [
  57. 'regular' => [
  58. 'Yardarm heave to brig spyglass smartly pillage',
  59. 'Bounty gangway bilge skysail rope\'s end',
  60. 'Maroon cutlass spirits nipperkin Plate Fleet',
  61. ],
  62. 'admin' => [
  63. 'Scourge of the seven seas coffer doubloon',
  64. 'Brig me splice the main brace',
  65. ]
  66. ]
  67. ]
  68. ];
  69. $this->updater
  70. ->expects($this->once())
  71. ->method('check')
  72. ->willReturn([
  73. 'version' => '1.2.3',
  74. 'versionstring' => 'Nextcloud 1.2.3',
  75. 'web' => 'https://docs.nextcloud.com/myUrl',
  76. 'url' => 'https://downloads.nextcloud.org/server',
  77. 'changes' => 'https://updates.nextcloud.com/changelog_server/?version=123.0.0',
  78. 'autoupdater' => '1',
  79. 'eol' => '0',
  80. ]);
  81. $this->changesChecker->expects($this->once())
  82. ->method('check')
  83. ->willReturn($changes);
  84. $expected = [
  85. 'updateAvailable' => true,
  86. 'updateVersion' => '1.2.3',
  87. 'updateVersionString' => 'Nextcloud 1.2.3',
  88. 'updaterEnabled' => true,
  89. 'versionIsEol' => false,
  90. 'updateLink' => 'https://docs.nextcloud.com/myUrl',
  91. 'downloadLink' => 'https://downloads.nextcloud.org/server',
  92. 'changes' => $changes,
  93. ];
  94. $this->assertSame($expected, $this->updateChecker->getUpdateState());
  95. }
  96. public function testGetUpdateStateWithoutUpdate(): void {
  97. $this->updater
  98. ->expects($this->once())
  99. ->method('check')
  100. ->willReturn([]);
  101. $expected = [];
  102. $this->assertSame($expected, $this->updateChecker->getUpdateState());
  103. }
  104. public function testSetInitialState(): void {
  105. $this->updater
  106. ->expects($this->once())
  107. ->method('check')
  108. ->willReturn([
  109. 'version' => '1.2.3',
  110. 'versionstring' => 'Nextcloud 1.2.3',
  111. 'web' => 'https://docs.nextcloud.com/myUrl',
  112. 'url' => 'https://downloads.nextcloud.org/server',
  113. 'changes' => 'https://updates.nextcloud.com/changelog_server/?version=123.0.0',
  114. 'autoupdater' => '1',
  115. 'eol' => '0',
  116. ]);
  117. $this->initialState->expects(self::once())
  118. ->method('provideInitialState')
  119. ->with('updateState', [
  120. 'updateVersion' => 'Nextcloud 1.2.3',
  121. 'updateLink' => 'https://docs.nextcloud.com/myUrl',
  122. ]);
  123. $this->updateChecker->setInitialState();
  124. }
  125. }