From 46d2e0369aac89548b4bc02d7480d5975e733c25 Mon Sep 17 00:00:00 2001 From: Riny van Tiggelen Date: Tue, 15 Oct 2024 21:42:45 +0200 Subject: [PATCH] [FEATURE] Added functional and unit test for VisibilityChecker, fixed other tests to have working functional and unit tests --- .github/workflows/tests.yml | 8 +- Build/phpunit/UnitTests.xml | 4 + .../PageLayoutHeader/VisibilityChecker.php | 2 +- .../VisibilityCheckerTest.php | 64 +++++++++ Tests/Functional/Utility/YoastUtilityTest.php | 111 ++-------------- .../Unit/Controller/CrawlerControllerTest.php | 22 ++-- .../Controller/DashboardControllerTest.php | 14 +- .../VisibilityCheckerTest.php | 122 ++++++++++++++++++ 8 files changed, 226 insertions(+), 121 deletions(-) create mode 100644 Tests/Functional/Service/PageLayoutHeader/VisibilityCheckerTest.php create mode 100644 Tests/Unit/Service/PageLayoutHeader/VisibilityCheckerTest.php diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 490d60b8..421f9cdb 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -51,4 +51,10 @@ jobs: run: Build/Scripts/runTests.sh -n -t ${{ matrix.typo3 }} -p ${{ matrix.php }} -s cgl -n - name: PHPStan - run: Build/Scripts/runTests.sh -t ${{ matrix.typo3 }} -p ${{ matrix.php }} -s phpstan -e "--error-format=github" \ No newline at end of file + run: Build/Scripts/runTests.sh -t ${{ matrix.typo3 }} -p ${{ matrix.php }} -s phpstan -e "--error-format=github" + + - name: Functional tests + run: Build/Scripts/runTests.sh -t ${{ matrix.typo3 }} -p ${{ matrix.php }} -s functional + + - name: Unit tests + run: Build/Scripts/runTests.sh -t ${{ matrix.typo3 }} -p ${{ matrix.php }} -s unit \ No newline at end of file diff --git a/Build/phpunit/UnitTests.xml b/Build/phpunit/UnitTests.xml index 33a62ed9..9c891bcb 100644 --- a/Build/phpunit/UnitTests.xml +++ b/Build/phpunit/UnitTests.xml @@ -6,6 +6,10 @@ bootstrap="UnitTestsBootstrap.php" cacheResult="false" displayDetailsOnTestsThatTriggerWarnings="true" + displayDetailsOnSkippedTests="true" + displayDetailsOnTestsThatTriggerDeprecations="true" + displayDetailsOnTestsThatTriggerErrors="true" + displayDetailsOnPhpunitDeprecations="true" colors="true" failOnRisky="true" failOnWarning="true" diff --git a/Classes/Service/PageLayoutHeader/VisibilityChecker.php b/Classes/Service/PageLayoutHeader/VisibilityChecker.php index d5d109a4..7ffe3096 100644 --- a/Classes/Service/PageLayoutHeader/VisibilityChecker.php +++ b/Classes/Service/PageLayoutHeader/VisibilityChecker.php @@ -32,7 +32,7 @@ protected function isSnippetPreviewEnabled(int $pageId, array $pageRecord): bool { $backendUser = $this->getBackendUser(); - if (!$GLOBALS['BE_USER']->check('non_exclude_fields', 'pages:tx_yoastseo_snippetpreview')) { + if (!$backendUser->check('non_exclude_fields', 'pages:tx_yoastseo_snippetpreview')) { return false; } diff --git a/Tests/Functional/Service/PageLayoutHeader/VisibilityCheckerTest.php b/Tests/Functional/Service/PageLayoutHeader/VisibilityCheckerTest.php new file mode 100644 index 00000000..f8a8e17b --- /dev/null +++ b/Tests/Functional/Service/PageLayoutHeader/VisibilityCheckerTest.php @@ -0,0 +1,64 @@ + [ + 'yoast_seo' => [ + 'allowedDoktypes' => [1, 6], + ], + ], + ]; + + public function setUp(): void + { + parent::setUp(); + + $this->importCSVDataSet(__DIR__ . '/../../Fixtures/be_users.csv'); + $this->setUpBackendUser(1); + } + + #[Test] + #[DataProvider('isSnippetPreviewEnabledCorrectlyBasedOnPageDoktypeDataProvider')] + public function isSnippetPreviewEnabledCorrectlyBasedOnPageDoktype(int $doktype, bool $expected): void + { + $visibilityChecker = new VisibilityChecker(); + self::assertEquals($expected, $visibilityChecker->shouldShowPreview(1, ['doktype' => $doktype])); + } + + public static function isSnippetPreviewEnabledCorrectlyBasedOnPageDoktypeDataProvider(): array + { + return [ + 'doktype 1 should return true' => [ + 1, + true, + ], + 'doktype 6 should return true' => [ + 6, + true, + ], + 'doktype 2 should return false' => [ + 2, + false, + ], + ]; + } +} diff --git a/Tests/Functional/Utility/YoastUtilityTest.php b/Tests/Functional/Utility/YoastUtilityTest.php index 7c214377..8d12e18a 100644 --- a/Tests/Functional/Utility/YoastUtilityTest.php +++ b/Tests/Functional/Utility/YoastUtilityTest.php @@ -4,23 +4,21 @@ namespace YoastSeoForTypo3\YoastSeo\Tests\Functional\Utility; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; -use TYPO3\CMS\Core\Core\Bootstrap; use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase; use YoastSeoForTypo3\YoastSeo\Utility\YoastUtility; -/** - * @covers \YoastSeoForTypo3\YoastSeo\Utility\YoastUtility - */ +#[CoversClass(YoastUtility::class)] class YoastUtilityTest extends FunctionalTestCase { - protected const DOKTYPES_FROM_CONFIGURATION = [1, 2, 3]; + protected const EXTCONF_DOKTYPES = [1, 2, 3]; protected array $configurationToUseInTestInstance = [ 'EXTCONF' => [ 'yoast_seo' => [ - 'allowedDoktypes' => self::DOKTYPES_FROM_CONFIGURATION, + 'allowedDoktypes' => self::EXTCONF_DOKTYPES, ], ], ]; @@ -31,7 +29,6 @@ public function setUp(): void $this->importCSVDataSet(__DIR__ . '/../Fixtures/be_users.csv'); $this->setUpBackendUser(1); - Bootstrap::initializeLanguageObject(); } #[Test] @@ -39,7 +36,7 @@ public function getAllowedDoktypesReturnsAllowedDoktypes(): void { $allowedDoktypes = YoastUtility::getAllowedDoktypes(); - self::assertSame(self::DOKTYPES_FROM_CONFIGURATION, $allowedDoktypes); + self::assertSame(self::EXTCONF_DOKTYPES, $allowedDoktypes); } #[DataProvider('areTheRightDoktypesExtractedFromConfigurationDataProvider')] @@ -51,120 +48,38 @@ public function areTheRightDoktypesExtractedFromConfiguration(array $inputArray, self::assertEquals($expected, $actual); } - #[DataProvider('isSnippetPreviewEnabledCorrectlyBasedOnPageTsConfigurationDataProvider')] - #[Test] - public function isSnippetPreviewEnabledCorrectlyBasedOnPageTsConfiguration(int $pageId, array $config, bool $expected): void - { - $actual = YoastUtility::snippetPreviewEnabled($pageId, ['tx_yoastseo_hide_snippet_preview' => false], $config); - - self::assertEquals($expected, $actual); - } - - #[DataProvider('isSnippetPreviewEnabledCorrectlyBasedOnPageRecordDataProvider')] - #[Test] - public function isSnippetPreviewEnabledCorrectlyBasedOnPageRecord(int $pageId, array $pageRecord, bool $expected): void - { - $actual = YoastUtility::snippetPreviewEnabled($pageId, $pageRecord, []); - - self::assertEquals($expected, $actual); - } - public static function areTheRightDoktypesExtractedFromConfigurationDataProvider(): array { return [ - [ + 'empty configuration array should return EXTCONF allowedDoktypes' => [ [], - self::DOKTYPES_FROM_CONFIGURATION, + self::EXTCONF_DOKTYPES, ], - [ + 'configuration array with doktypes 1 and 6 should not return doktype 1 more than once' => [ [ 'allowedDoktypes' => [ 'page' => 1, 'backend_user_section' => 6, ], ], - array_merge(self::DOKTYPES_FROM_CONFIGURATION, [6]), + array_merge(self::EXTCONF_DOKTYPES, [6]), ], - [ + 'configuration array with doktype 6 should return EXTCONF allowedDoktypes plus doktype 6' => [ [ 'allowedDoktypes' => [ 'backend_user_section' => 6, ], ], - array_merge(self::DOKTYPES_FROM_CONFIGURATION, [6]), + array_merge(self::EXTCONF_DOKTYPES, [6]), ], - [ + 'configuration array with doktypes 1 (different key) and 6 should not return doktype 1 more than once' => [ [ 'allowedDoktypes' => [ 'duplicateDoktype' => 1, 'backend_user_section' => 6, ], ], - array_merge(self::DOKTYPES_FROM_CONFIGURATION, [6]), - ], - ]; - } - - public static function isSnippetPreviewEnabledCorrectlyBasedOnPageTsConfigurationDataProvider(): array - { - return [ - [ - 1, - [], - true, - ], - [ - 1, - [ - 'mod.' => [ - 'web_SeoPlugin.' => [ - 'disableSnippetPreview' => 0, - ], - ], - ], - true, - ], - [ - 1, - [ - 'mod.' => [ - 'web_SeoPlugin.' => [ - 'disableSnippetPreview' => 1, - ], - ], - ], - false, - ], - ]; - } - - public static function isSnippetPreviewEnabledCorrectlyBasedOnPageRecordDataProvider(): array - { - return [ - [ - 1, - [], - true, - ], - [ - 1, - ['tx_yoastseo_hide_snippet_preview' => '0'], - true, - ], - [ - 1, - ['tx_yoastseo_hide_snippet_preview' => false], - true, - ], - [ - 1, - ['tx_yoastseo_hide_snippet_preview' => '1'], - false, - ], - [ - 1, - ['tx_yoastseo_hide_snippet_preview' => true], - false, + array_merge(self::EXTCONF_DOKTYPES, [6]), ], ]; } diff --git a/Tests/Unit/Controller/CrawlerControllerTest.php b/Tests/Unit/Controller/CrawlerControllerTest.php index 9ce5ee74..4055fc6a 100644 --- a/Tests/Unit/Controller/CrawlerControllerTest.php +++ b/Tests/Unit/Controller/CrawlerControllerTest.php @@ -4,19 +4,20 @@ namespace YoastSeoForTypo3\YoastSeo\Tests\Unit\Controller; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\Test; use TYPO3\CMS\Core\Http\HtmlResponse; use TYPO3\CMS\Core\Http\RedirectResponse; use TYPO3\CMS\Core\Site\SiteFinder; use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; use TYPO3\CMS\Extbase\Mvc\Request; +use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder; use TYPO3\TestingFramework\Core\Unit\UnitTestCase; use YoastSeoForTypo3\YoastSeo\Controller\CrawlerController; use YoastSeoForTypo3\YoastSeo\Service\Crawler\CrawlerJavascriptConfigService; use YoastSeoForTypo3\YoastSeo\Service\Crawler\CrawlerService; -/** - * @covers \YoastSeoForTypo3\YoastSeo\Controller\CrawlerController - */ +#[CoversClass(CrawlerController::class)] final class CrawlerControllerTest extends UnitTestCase { private CrawlerController $subject; @@ -45,21 +46,20 @@ protected function setUp(): void $request = $this->createMock(Request::class); $this->subject->_set('request', $request); + $uriBuilder = $this->createMock(UriBuilder::class); + $this->subject->_set('uriBuilder', $uriBuilder); + $responseStub = $this->createStub(HtmlResponse::class); $this->subject->method('returnResponse')->willReturn($responseStub); } - /** - * @test - */ + #[Test] public function isActionController(): void { self::assertInstanceOf(ActionController::class, $this->subject); } - /** - * @test - */ + #[Test] public function indexActionReturnsHtmlResponse(): void { $result = $this->subject->indexAction(); @@ -67,9 +67,7 @@ public function indexActionReturnsHtmlResponse(): void self::assertInstanceOf(HtmlResponse::class, $result); } - /** - * @test - */ + #[Test] public function resetProgressActionReturnsRedirectResponse(): void { $result = $this->subject->resetProgressAction(1, 1); diff --git a/Tests/Unit/Controller/DashboardControllerTest.php b/Tests/Unit/Controller/DashboardControllerTest.php index 94dea6d4..c313fb9d 100644 --- a/Tests/Unit/Controller/DashboardControllerTest.php +++ b/Tests/Unit/Controller/DashboardControllerTest.php @@ -4,14 +4,14 @@ namespace YoastSeoForTypo3\YoastSeo\Tests\Unit\Controller; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\Test; use TYPO3\CMS\Core\Http\HtmlResponse; use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; use TYPO3\TestingFramework\Core\Unit\UnitTestCase; use YoastSeoForTypo3\YoastSeo\Controller\DashboardController; -/** - * @covers \YoastSeoForTypo3\YoastSeo\Controller\DashboardController - */ +#[CoversClass(DashboardController::class)] final class DashboardControllerTest extends UnitTestCase { private DashboardController $subject; @@ -32,17 +32,13 @@ protected function setUp(): void $this->subject->method('returnResponse')->willReturn($responseStub); } - /** - * @test - */ + #[Test] public function isActionController(): void { self::assertInstanceOf(ActionController::class, $this->subject); } - /** - * @test - */ + #[Test] public function indexActionReturnsHtmlResponse(): void { $result = $this->subject->indexAction(); diff --git a/Tests/Unit/Service/PageLayoutHeader/VisibilityCheckerTest.php b/Tests/Unit/Service/PageLayoutHeader/VisibilityCheckerTest.php new file mode 100644 index 00000000..775157db --- /dev/null +++ b/Tests/Unit/Service/PageLayoutHeader/VisibilityCheckerTest.php @@ -0,0 +1,122 @@ +getAccessibleMock(BackendUserAuthentication::class, ['check']); + $backendUser->method('check')->willReturn(true); + + $this->subject = $this->getAccessibleMock( + VisibilityChecker::class, + ['getBackendUser', 'getPageTsConfig'], + ); + $this->subject->method('getBackendUser') + ->willReturn($backendUser); + } + + #[DataProvider('isSnippetPreviewEnabledCorrectlyBasedOnPageTsConfigurationDataProvider')] + #[Test] + public function isSnippetPreviewEnabledCorrectlyBasedOnPageTsConfiguration(int $pageId, array $config, bool $expected): void + { + $this->subject->method('getPageTsConfig')->willReturn($config); + $actual = $this->subject->_call('isSnippetPreviewEnabled', $pageId, []); + + self::assertEquals($expected, $actual); + } + + #[DataProvider('isSnippetPreviewEnabledCorrectlyBasedOnPageRecordDataProvider')] + #[Test] + public function isSnippetPreviewEnabledCorrectlyBasedOnPageRecord(int $pageId, array $pageRecord, bool $expected): void + { + $this->subject->method('getPageTsConfig')->willReturn([]); + $actual = $this->subject->_call('isSnippetPreviewEnabled', $pageId, $pageRecord); + + self::assertEquals($expected, $actual); + } + + public static function isSnippetPreviewEnabledCorrectlyBasedOnPageTsConfigurationDataProvider(): array + { + return [ + 'empty tsconfig should show snippet preview' => [ + 1, + [], + true, + ], + 'disableSnippetPreview set to 0 should show snippet preview' => [ + 1, + [ + 'mod.' => [ + 'web_SeoPlugin.' => [ + 'disableSnippetPreview' => 0, + ], + ], + ], + true, + ], + 'disableSnippetPreview set to 1 should not show snippet preview' => [ + 1, + [ + 'mod.' => [ + 'web_SeoPlugin.' => [ + 'disableSnippetPreview' => 1, + ], + ], + ], + false, + ], + ]; + } + + public static function isSnippetPreviewEnabledCorrectlyBasedOnPageRecordDataProvider(): array + { + return [ + 'page record without tx_yoastseo_hide_snippet_preview should show snippet preview' => [ + 1, + [], + true, + ], + 'tx_yoastseo_hide_snippet_preview set to 0 should show snippet preview' => [ + 1, + ['tx_yoastseo_hide_snippet_preview' => '0'], + true, + ], + 'tx_yoastseo_hide_snippet_preview set to false should show snippet preview' => [ + 1, + ['tx_yoastseo_hide_snippet_preview' => false], + true, + ], + 'tx_yoastseo_hide_snippet_preview set to 1 should not show snippet preview' => [ + 1, + ['tx_yoastseo_hide_snippet_preview' => '1'], + false, + ], + 'tx_yoastseo_hide_snippet_preview set to true should not show snippet preview' => [ + 1, + ['tx_yoastseo_hide_snippet_preview' => true], + false, + ], + ]; + } +}