From 2708c2e7a8ffafa35e7ad9b63c9a2b1913447095 Mon Sep 17 00:00:00 2001 From: Andrew Longosz Date: Tue, 7 Nov 2023 10:32:44 +0100 Subject: [PATCH] IBX-6875: Implemented IsPreview View Matcher (#288) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For more details see https://issues.ibexa.co/browse/IBX-6875 and https://github.com/ibexa/core/pull/288. `IsPreview` View Matcher allows to distinguish views depending on if template is used to render preview or view of a content item. Key changes: * Implemented IsPreview View Matcher * [Tests] Added unit test coverage for IsPreview View Matcher --------- Co-authored-by: Adam Wójs --- .../Matcher/ContentBased/IsPreview.php | 48 ++++++++++ .../Matcher/ContentBased/IsPreviewTest.php | 96 +++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 src/lib/MVC/Symfony/Matcher/ContentBased/IsPreview.php create mode 100644 tests/lib/MVC/Symfony/Matcher/ContentBased/IsPreviewTest.php diff --git a/src/lib/MVC/Symfony/Matcher/ContentBased/IsPreview.php b/src/lib/MVC/Symfony/Matcher/ContentBased/IsPreview.php new file mode 100644 index 0000000000..c9221cc466 --- /dev/null +++ b/src/lib/MVC/Symfony/Matcher/ContentBased/IsPreview.php @@ -0,0 +1,48 @@ +isPreview = $matchingConfig; + } + + public function match(View $view): bool + { + $isPreview = $view->hasParameter(PreviewController::PREVIEW_PARAMETER_NAME) + && $view->getParameter(PreviewController::PREVIEW_PARAMETER_NAME); + + return $this->isPreview === $isPreview; + } +} diff --git a/tests/lib/MVC/Symfony/Matcher/ContentBased/IsPreviewTest.php b/tests/lib/MVC/Symfony/Matcher/ContentBased/IsPreviewTest.php new file mode 100644 index 0000000000..114ce35369 --- /dev/null +++ b/tests/lib/MVC/Symfony/Matcher/ContentBased/IsPreviewTest.php @@ -0,0 +1,96 @@ +isPreviewMatcher = new IsPreview(); + } + + /** + * @return iterable + */ + public static function getDataForTestMatch(): iterable + { + $previewContentView = new ContentView(); + $previewContentView->setParameters(['isPreview' => true]); + + $notPreviewContentView = new ContentView(); + $notPreviewContentView->setParameters(['isPreview' => false]); + + $viewContentView = new ContentView(); + yield 'match for preview content view' => [ + $previewContentView, + true, // IsPreview: true + true, // matches the view + ]; + + yield 'do not match for preview content view' => [ + $previewContentView, + false, // IsPreview: false + false, // doesn't match the view + ]; + + yield 'match for view content view' => [ + $notPreviewContentView, + false, // IsPreview: false + true, // matches since it's not a preview content view + ]; + + yield 'do not match for view content view' => [ + $notPreviewContentView, + true, // IsPreview: true + false, // doesn't match since it's not a preview content view + ]; + + yield 'not match for not set isPreview parameter' => [ + $viewContentView, + true, // IsPreview: true + false, // by default, it's not a preview view if parameter is not set + ]; + + yield 'do not match for not set isPreview parameter' => [ + $viewContentView, + false, + true, // matches not a preview view, when parameter is not set + ]; + } + + /** + * @dataProvider getDataForTestMatch + * + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException + */ + public function testMatch(View $view, bool $matchConfig, bool $expectedIsPreview): void + { + $this->isPreviewMatcher->setMatchingConfig($matchConfig); + + self::assertSame($expectedIsPreview, $this->isPreviewMatcher->match($view)); + } + + public function testSetMatchConfigThrowsInvalidArgumentException(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('IsPreview matcher expects true or false value, got a value of integer type'); + $this->isPreviewMatcher->setMatchingConfig(123); + } +}