From c8ae43479cde4cd2f6eb7750d36439ed75b58d0c Mon Sep 17 00:00:00 2001 From: Daniel Gohlke Date: Mon, 26 Jan 2026 16:46:02 +0100 Subject: [PATCH] [BUGFIX] Get frontend user group from context to handle special prices Relates: #115 --- Classes/Domain/Model/EventDate.php | 17 +- Classes/Domain/Model/PriceCategory.php | 17 +- .../Domain/Model/AbstractSpecialPrice.php | 247 ++++++++++++++++++ .../Functional/Domain/Model/EventDateTest.php | 27 ++ .../Domain/Model/PriceCategoryTest.php | 27 ++ 5 files changed, 331 insertions(+), 4 deletions(-) create mode 100644 Tests/Functional/Domain/Model/AbstractSpecialPrice.php create mode 100644 Tests/Functional/Domain/Model/EventDateTest.php create mode 100644 Tests/Functional/Domain/Model/PriceCategoryTest.php diff --git a/Classes/Domain/Model/EventDate.php b/Classes/Domain/Model/EventDate.php index 1cb6bc36..c77983c5 100644 --- a/Classes/Domain/Model/EventDate.php +++ b/Classes/Domain/Model/EventDate.php @@ -10,6 +10,9 @@ * For the full copyright and license information, please read the * LICENSE file that was distributed with this source code. */ + +use TYPO3\CMS\Core\Context\Context; +use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Annotation\ORM\Cascade; use TYPO3\CMS\Extbase\Annotation\Validate; use TYPO3\CMS\Extbase\Domain\Model\FileReference; @@ -227,8 +230,13 @@ public function setPriceCategories(ObjectStorage $priceCategories): void $this->priceCategories = $priceCategories; } - public function getBestSpecialPrice(array $frontendUserGroupIds = []): ?SpecialPrice + public function getBestSpecialPrice(?array $frontendUserGroupIds = null): ?SpecialPrice { + if (is_null($frontendUserGroupIds)) { + $context = GeneralUtility::makeInstance(Context::class); + $frontendUserGroupIds = $context->getPropertyFromAspect('frontend.user', 'groupIds'); + } + $bestSpecialPrice = null; if ($this->specialPrices) { @@ -246,8 +254,13 @@ public function getBestSpecialPrice(array $frontendUserGroupIds = []): ?SpecialP return $bestSpecialPrice; } - public function getBestPrice(array $frontendUserGroupIds = []): float + public function getBestPrice(?array $frontendUserGroupIds = null): float { + if (is_null($frontendUserGroupIds)) { + $context = GeneralUtility::makeInstance(Context::class); + $frontendUserGroupIds = $context->getPropertyFromAspect('frontend.user', 'groupIds'); + } + $price = $this->getPrice(); $specialPrice = $this->getBestSpecialPrice($frontendUserGroupIds); diff --git a/Classes/Domain/Model/PriceCategory.php b/Classes/Domain/Model/PriceCategory.php index cd5085b1..268e9af8 100644 --- a/Classes/Domain/Model/PriceCategory.php +++ b/Classes/Domain/Model/PriceCategory.php @@ -10,6 +10,9 @@ * For the full copyright and license information, please read the * LICENSE file that was distributed with this source code. */ + +use TYPO3\CMS\Core\Context\Context; +use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Annotation\ORM\Cascade; use TYPO3\CMS\Extbase\Annotation\Validate; use TYPO3\CMS\Extbase\DomainObject\AbstractEntity; @@ -135,8 +138,13 @@ public function getSeatsAvailable(): int /** * Returns best Special Price */ - public function getBestSpecialPrice(array $frontendUserGroupIds = []): ?SpecialPrice + public function getBestSpecialPrice(?array $frontendUserGroupIds = null): ?SpecialPrice { + if (is_null($frontendUserGroupIds)) { + $context = GeneralUtility::makeInstance(Context::class); + $frontendUserGroupIds = $context->getPropertyFromAspect('frontend.user', 'groupIds'); + } + $bestSpecialPrice = null; if ($this->specialPrices) { @@ -157,8 +165,13 @@ public function getBestSpecialPrice(array $frontendUserGroupIds = []): ?SpecialP /** * Returns price of best Special Price */ - public function getBestPrice(array $frontendUserGroupIds = []): float + public function getBestPrice(?array $frontendUserGroupIds = null): float { + if (is_null($frontendUserGroupIds)) { + $context = GeneralUtility::makeInstance(Context::class); + $frontendUserGroupIds = $context->getPropertyFromAspect('frontend.user', 'groupIds'); + } + $price = $this->getPrice(); $specialPrice = $this->getBestSpecialPrice($frontendUserGroupIds); diff --git a/Tests/Functional/Domain/Model/AbstractSpecialPrice.php b/Tests/Functional/Domain/Model/AbstractSpecialPrice.php new file mode 100644 index 00000000..af9836dd --- /dev/null +++ b/Tests/Functional/Domain/Model/AbstractSpecialPrice.php @@ -0,0 +1,247 @@ +subject); + } + + #[Test] + public function getBestSpecialPriceReturnsNullIfEventHasNoSpecialPrice(): void + { + $objectStorage = new ObjectStorage(); + $this->subject->setSpecialPrices($objectStorage); + + self::assertNull( + $this->subject->getBestSpecialPrice() + ); + } + + #[Test] + public function getBestSpecialPriceReturnsNullIfEventHasSpecialPriceButUserHasNoGroup(): void + { + $this->setUpFrontendUserAspect(); + + $frontendUserGroup1 = self::createStub(FrontendUserGroup::class); + $frontendUserGroup1->method('getUid')->willReturn(42); + + $objectStorage = new ObjectStorage(); + $specialPrice1 = $this->createSpecialPriceForFrontendUserGroup(10.00, $frontendUserGroup1); + $objectStorage->attach($specialPrice1); + $this->subject->setSpecialPrices($objectStorage); + + self::assertNull( + $this->subject->getBestSpecialPrice() + ); + + self::assertSame( + $this->price, + $this->subject->getBestPrice() + ); + } + + #[Test] + public function getBestSpecialPriceReturnsSpecialPriceIfEventHasASpecialPriceWithNoUserGroup(): void + { + $this->setUpFrontendUserAspect([]); + + $frontendUserGroup1 = self::createStub(FrontendUserGroup::class); + $frontendUserGroup1->method('getUid')->willReturn(42); + + $objectStorage = new ObjectStorage(); + $specialPrice1 = $this->createSpecialPriceForFrontendUserGroup(10.00, $frontendUserGroup1); + $objectStorage->attach($specialPrice1); + $this->subject->setSpecialPrices($objectStorage); + + self::assertNull( + $this->subject->getBestSpecialPrice() + ); + + self::assertSame( + $this->price, + $this->subject->getBestPrice() + ); + } + + #[Test] + public function getBestSpecialPriceReturnsSpecialPriceIfEventHasASpecialPriceWithMatchingUserGroup(): void + { + $this->setUpFrontendUserAspect([42, 43]); + + $frontendUserGroup1 = self::createStub(FrontendUserGroup::class); + $frontendUserGroup1->method('getUid')->willReturn(42); + + $objectStorage = new ObjectStorage(); + $specialPrice1 = $this->createSpecialPriceForFrontendUserGroup(10.00, $frontendUserGroup1); + $objectStorage->attach($specialPrice1); + $this->subject->setSpecialPrices($objectStorage); + + self::assertSame( + $specialPrice1, + $this->subject->getBestSpecialPrice() + ); + + self::assertSame( + 10.00, + $this->subject->getBestPrice() + ); + } + + #[Test] + public function getBestSpecialPriceReturnsBestSpecialPriceIfEventHasSpecialPricesWithMatchingUserGroup(): void + { + $this->setUpFrontendUserAspect([42, 43]); + + $frontendUserGroup1 = self::createStub(FrontendUserGroup::class); + $frontendUserGroup1->method('getUid')->willReturn(42); + $frontendUserGroup2 = self::createStub(FrontendUserGroup::class); + $frontendUserGroup2->method('getUid')->willReturn(43); + + $objectStorage = new ObjectStorage(); + $specialPrice1 = $this->createSpecialPriceForFrontendUserGroup(10.00, $frontendUserGroup1); + $objectStorage->attach($specialPrice1); + $specialPrice2 = $this->createSpecialPriceForFrontendUserGroup(8.00, $frontendUserGroup2); + $objectStorage->attach($specialPrice2); + $this->subject->setSpecialPrices($objectStorage); + + self::assertSame( + $specialPrice2, + $this->subject->getBestSpecialPrice() + ); + + self::assertSame( + 8.00, + $this->subject->getBestPrice() + ); + } + + #[Test] + public function getBestSpecialPriceReturnsBestSpecialPriceForUserGroupIfEventHasSpecialPricesWithMatchingUserGroup(): void + { + $this->setUpFrontendUserAspect([42, 43]); + + $frontendUserGroup1 = self::createStub(FrontendUserGroup::class); + $frontendUserGroup1->method('getUid')->willReturn(42); + $frontendUserGroup2 = self::createStub(FrontendUserGroup::class); + $frontendUserGroup2->method('getUid')->willReturn(43); + $frontendUserGroup3 = self::createStub(FrontendUserGroup::class); + $frontendUserGroup3->method('getUid')->willReturn(44); + + $objectStorage = new ObjectStorage(); + $specialPrice1 = $this->createSpecialPriceForFrontendUserGroup(9.00, $frontendUserGroup1); + $objectStorage->attach($specialPrice1); + $specialPrice2 = $this->createSpecialPriceForFrontendUserGroup(11.00, $frontendUserGroup2); + $objectStorage->attach($specialPrice2); + $specialPrice3 = $this->createSpecialPriceForFrontendUserGroup(7.00, $frontendUserGroup3); + $objectStorage->attach($specialPrice3); + $this->subject->setSpecialPrices($objectStorage); + + self::assertSame( + $specialPrice1, + $this->subject->getBestSpecialPrice() + ); + + self::assertSame( + 9.00, + $this->subject->getBestPrice() + ); + } + + #[Test] + public function getBestSpecialPriceReturnsSpecialPriceIfEventHasASpecialPriceWithNotMatchingUserGroup(): void + { + $this->setUpFrontendUserAspect([41, 43]); + + $frontendUserGroup1 = self::createStub(FrontendUserGroup::class); + $frontendUserGroup1->method('getUid')->willReturn(42); + + $objectStorage = new ObjectStorage(); + $specialPrice1 = $this->createSpecialPriceForFrontendUserGroup(10.00, $frontendUserGroup1); + $objectStorage->attach($specialPrice1); + $this->subject->setSpecialPrices($objectStorage); + + self::assertNull( + $this->subject->getBestSpecialPrice() + ); + + self::assertSame( + $this->price, + $this->subject->getBestPrice() + ); + } + + #[Test] + public function getBestPriceReturnsPriceIfSpecialPriceIsGreater(): void + { + $this->setUpFrontendUserAspect([42]); + + $frontendUserGroup1 = self::createStub(FrontendUserGroup::class); + $frontendUserGroup1->method('getUid')->willReturn(42); + + $objectStorage = new ObjectStorage(); + $specialPrice1 = $this->createSpecialPriceForFrontendUserGroup(20.00, $frontendUserGroup1); + $objectStorage->attach($specialPrice1); + $this->subject->setSpecialPrices($objectStorage); + + self::assertSame( + $specialPrice1, + $this->subject->getBestSpecialPrice() + ); + + self::assertSame( + $this->price, + $this->subject->getBestPrice() + ); + } + + private function createSpecialPriceForFrontendUserGroup(float $price, FrontendUserGroup $frontendUserGroup): SpecialPrice + { + $specialPrice = new SpecialPrice(); + $specialPrice->setPrice($price); + $specialPrice->setFrontendUserGroup($frontendUserGroup); + + return $specialPrice; + } + + private function setUpFrontendUserAspect(?array $groupIds = null): void + { + $userAspect = GeneralUtility::makeInstance( + UserAspect::class, + null, + $groupIds + ); + + $context = GeneralUtility::makeInstance(Context::class); + $context->setAspect( + 'frontend.user', + $userAspect + ); + } +} diff --git a/Tests/Functional/Domain/Model/EventDateTest.php b/Tests/Functional/Domain/Model/EventDateTest.php new file mode 100644 index 00000000..8d186cde --- /dev/null +++ b/Tests/Functional/Domain/Model/EventDateTest.php @@ -0,0 +1,27 @@ +price = 17.49; + + $this->subject = new EventDate(); + $this->subject->setPrice($this->price); + } +} diff --git a/Tests/Functional/Domain/Model/PriceCategoryTest.php b/Tests/Functional/Domain/Model/PriceCategoryTest.php new file mode 100644 index 00000000..d2a5ab16 --- /dev/null +++ b/Tests/Functional/Domain/Model/PriceCategoryTest.php @@ -0,0 +1,27 @@ +price = 13.44; + + $this->subject = new PriceCategory(); + $this->subject->setPrice($this->price); + } +}