CodeIntegrityTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Settings\Tests;
  8. use OC\IntegrityCheck\Checker;
  9. use OCA\Settings\SetupChecks\CodeIntegrity;
  10. use OCP\IL10N;
  11. use OCP\IURLGenerator;
  12. use OCP\SetupCheck\SetupResult;
  13. use PHPUnit\Framework\MockObject\MockObject;
  14. use Test\TestCase;
  15. class CodeIntegrityTest extends TestCase {
  16. private IL10N&MockObject $l10n;
  17. private IURLGenerator&MockObject $urlGenerator;
  18. private Checker&MockObject $checker;
  19. protected function setUp(): void {
  20. parent::setUp();
  21. $this->l10n = $this->getMockBuilder(IL10N::class)
  22. ->disableOriginalConstructor()->getMock();
  23. $this->l10n->expects($this->any())
  24. ->method('t')
  25. ->willReturnCallback(function ($message, array $replace) {
  26. return vsprintf($message, $replace);
  27. });
  28. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  29. $this->checker = $this->createMock(Checker::class);
  30. }
  31. public function testSkipOnDisabled(): void {
  32. $this->checker->expects($this->atLeastOnce())
  33. ->method('isCodeCheckEnforced')
  34. ->willReturn(false);
  35. $check = new CodeIntegrity(
  36. $this->l10n,
  37. $this->urlGenerator,
  38. $this->checker,
  39. );
  40. $this->assertEquals(SetupResult::INFO, $check->run()->getSeverity());
  41. }
  42. public function testSuccessOnEmptyResults(): void {
  43. $this->checker->expects($this->atLeastOnce())
  44. ->method('isCodeCheckEnforced')
  45. ->willReturn(true);
  46. $this->checker->expects($this->atLeastOnce())
  47. ->method('getResults')
  48. ->willReturn([]);
  49. $this->checker->expects(($this->atLeastOnce()))
  50. ->method('hasPassedCheck')
  51. ->willReturn(true);
  52. $check = new CodeIntegrity(
  53. $this->l10n,
  54. $this->urlGenerator,
  55. $this->checker,
  56. );
  57. $this->assertEquals(SetupResult::SUCCESS, $check->run()->getSeverity());
  58. }
  59. public function testCheckerIsReRunWithoutResults(): void {
  60. $this->checker->expects($this->atLeastOnce())
  61. ->method('isCodeCheckEnforced')
  62. ->willReturn(true);
  63. $this->checker->expects($this->atLeastOnce())
  64. ->method('getResults')
  65. ->willReturn(null);
  66. $this->checker->expects(($this->atLeastOnce()))
  67. ->method('hasPassedCheck')
  68. ->willReturn(true);
  69. // This is important and must be called
  70. $this->checker->expects($this->once())
  71. ->method('runInstanceVerification');
  72. $check = new CodeIntegrity(
  73. $this->l10n,
  74. $this->urlGenerator,
  75. $this->checker,
  76. );
  77. $this->assertEquals(SetupResult::SUCCESS, $check->run()->getSeverity());
  78. }
  79. public function testCheckerIsNotReReInAdvance(): void {
  80. $this->checker->expects($this->atLeastOnce())
  81. ->method('isCodeCheckEnforced')
  82. ->willReturn(true);
  83. $this->checker->expects($this->atLeastOnce())
  84. ->method('getResults')
  85. ->willReturn(['mocked']);
  86. $this->checker->expects(($this->atLeastOnce()))
  87. ->method('hasPassedCheck')
  88. ->willReturn(true);
  89. // There are results thus this must never be called
  90. $this->checker->expects($this->never())
  91. ->method('runInstanceVerification');
  92. $check = new CodeIntegrity(
  93. $this->l10n,
  94. $this->urlGenerator,
  95. $this->checker,
  96. );
  97. $this->assertEquals(SetupResult::SUCCESS, $check->run()->getSeverity());
  98. }
  99. public function testErrorOnMissingIntegrity(): void {
  100. $this->checker->expects($this->atLeastOnce())
  101. ->method('isCodeCheckEnforced')
  102. ->willReturn(true);
  103. $this->checker->expects($this->atLeastOnce())
  104. ->method('getResults')
  105. ->willReturn(['mocked']);
  106. $this->checker->expects(($this->atLeastOnce()))
  107. ->method('hasPassedCheck')
  108. ->willReturn(false);
  109. $check = new CodeIntegrity(
  110. $this->l10n,
  111. $this->urlGenerator,
  112. $this->checker,
  113. );
  114. $this->assertEquals(SetupResult::ERROR, $check->run()->getSeverity());
  115. }
  116. }