Skip to content

Commit b4404a5

Browse files
committed
[BUGFIX] Get frontend user group from context to handle special prices
Relates: #115
1 parent 967a52b commit b4404a5

5 files changed

Lines changed: 331 additions & 4 deletions

File tree

Classes/Domain/Model/EventDate.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
* For the full copyright and license information, please read the
1111
* LICENSE file that was distributed with this source code.
1212
*/
13+
14+
use TYPO3\CMS\Core\Context\Context;
15+
use TYPO3\CMS\Core\Utility\GeneralUtility;
1316
use TYPO3\CMS\Extbase\Annotation\ORM\Cascade;
1417
use TYPO3\CMS\Extbase\Annotation\Validate;
1518
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
@@ -227,8 +230,13 @@ public function setPriceCategories(ObjectStorage $priceCategories): void
227230
$this->priceCategories = $priceCategories;
228231
}
229232

230-
public function getBestSpecialPrice(array $frontendUserGroupIds = []): ?SpecialPrice
233+
public function getBestSpecialPrice(?array $frontendUserGroupIds = null): ?SpecialPrice
231234
{
235+
if (is_null($frontendUserGroupIds)) {
236+
$context = GeneralUtility::makeInstance(Context::class);
237+
$frontendUserGroupIds = $context->getPropertyFromAspect('frontend.user', 'groupIds');
238+
}
239+
232240
$bestSpecialPrice = null;
233241

234242
if ($this->specialPrices) {
@@ -246,8 +254,13 @@ public function getBestSpecialPrice(array $frontendUserGroupIds = []): ?SpecialP
246254
return $bestSpecialPrice;
247255
}
248256

249-
public function getBestPrice(array $frontendUserGroupIds = []): float
257+
public function getBestPrice(?array $frontendUserGroupIds = null): float
250258
{
259+
if (is_null($frontendUserGroupIds)) {
260+
$context = GeneralUtility::makeInstance(Context::class);
261+
$frontendUserGroupIds = $context->getPropertyFromAspect('frontend.user', 'groupIds');
262+
}
263+
251264
$price = $this->getPrice();
252265

253266
$specialPrice = $this->getBestSpecialPrice($frontendUserGroupIds);

Classes/Domain/Model/PriceCategory.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
* For the full copyright and license information, please read the
1111
* LICENSE file that was distributed with this source code.
1212
*/
13+
14+
use TYPO3\CMS\Core\Context\Context;
15+
use TYPO3\CMS\Core\Utility\GeneralUtility;
1316
use TYPO3\CMS\Extbase\Annotation\ORM\Cascade;
1417
use TYPO3\CMS\Extbase\Annotation\Validate;
1518
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
@@ -135,8 +138,13 @@ public function getSeatsAvailable(): int
135138
/**
136139
* Returns best Special Price
137140
*/
138-
public function getBestSpecialPrice(array $frontendUserGroupIds = []): ?SpecialPrice
141+
public function getBestSpecialPrice(?array $frontendUserGroupIds = null): ?SpecialPrice
139142
{
143+
if (is_null($frontendUserGroupIds)) {
144+
$context = GeneralUtility::makeInstance(Context::class);
145+
$frontendUserGroupIds = $context->getPropertyFromAspect('frontend.user', 'groupIds');
146+
}
147+
140148
$bestSpecialPrice = null;
141149

142150
if ($this->specialPrices) {
@@ -157,8 +165,13 @@ public function getBestSpecialPrice(array $frontendUserGroupIds = []): ?SpecialP
157165
/**
158166
* Returns price of best Special Price
159167
*/
160-
public function getBestPrice(array $frontendUserGroupIds = []): float
168+
public function getBestPrice(?array $frontendUserGroupIds = null): float
161169
{
170+
if (is_null($frontendUserGroupIds)) {
171+
$context = GeneralUtility::makeInstance(Context::class);
172+
$frontendUserGroupIds = $context->getPropertyFromAspect('frontend.user', 'groupIds');
173+
}
174+
162175
$price = $this->getPrice();
163176

164177
$specialPrice = $this->getBestSpecialPrice($frontendUserGroupIds);
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Extcode\CartEvents\Tests\Functional\Domain\Model;
6+
7+
/*
8+
* This file is part of the package extcode/cart-events.
9+
*
10+
* For the full copyright and license information, please read the
11+
* LICENSE file that was distributed with this source code.
12+
*/
13+
14+
use Extcode\Cart\Domain\Model\FrontendUserGroup;
15+
use Extcode\CartEvents\Domain\Model\EventDate;
16+
use Extcode\CartEvents\Domain\Model\PriceCategory;
17+
use Extcode\CartEvents\Domain\Model\SpecialPrice;
18+
use PHPUnit\Framework\Attributes\Test;
19+
use TYPO3\CMS\Core\Context\Context;
20+
use TYPO3\CMS\Core\Context\UserAspect;
21+
use TYPO3\CMS\Core\Utility\GeneralUtility;
22+
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
23+
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
24+
25+
abstract class AbstractSpecialPriceTest extends FunctionalTestCase
26+
{
27+
protected float $price;
28+
29+
protected EventDate|PriceCategory $subject;
30+
31+
protected function tearDown(): void
32+
{
33+
unset($this->subject);
34+
}
35+
36+
#[Test]
37+
public function getBestSpecialPriceReturnsNullIfEventHasNoSpecialPrice(): void
38+
{
39+
$objectStorage = new ObjectStorage();
40+
$this->subject->setSpecialPrices($objectStorage);
41+
42+
self::assertNull(
43+
$this->subject->getBestSpecialPrice()
44+
);
45+
}
46+
47+
#[Test]
48+
public function getBestSpecialPriceReturnsNullIfEventHasSpecialPriceButUserHasNoGroup(): void
49+
{
50+
$this->setUpFrontendUserAspect();
51+
52+
$frontendUserGroup1 = self::createStub(FrontendUserGroup::class);
53+
$frontendUserGroup1->method('getUid')->willReturn(42);
54+
55+
$objectStorage = new ObjectStorage();
56+
$specialPrice1 = $this->createSpecialPriceForFrontendUserGroup(10.00, $frontendUserGroup1);
57+
$objectStorage->attach($specialPrice1);
58+
$this->subject->setSpecialPrices($objectStorage);
59+
60+
self::assertNull(
61+
$this->subject->getBestSpecialPrice()
62+
);
63+
64+
self::assertSame(
65+
$this->price,
66+
$this->subject->getBestPrice()
67+
);
68+
}
69+
70+
#[Test]
71+
public function getBestSpecialPriceReturnsSpecialPriceIfEventHasASpecialPriceWithNoUserGroup(): void
72+
{
73+
$this->setUpFrontendUserAspect([]);
74+
75+
$frontendUserGroup1 = self::createStub(FrontendUserGroup::class);
76+
$frontendUserGroup1->method('getUid')->willReturn(42);
77+
78+
$objectStorage = new ObjectStorage();
79+
$specialPrice1 = $this->createSpecialPriceForFrontendUserGroup(10.00, $frontendUserGroup1);
80+
$objectStorage->attach($specialPrice1);
81+
$this->subject->setSpecialPrices($objectStorage);
82+
83+
self::assertNull(
84+
$this->subject->getBestSpecialPrice()
85+
);
86+
87+
self::assertSame(
88+
$this->price,
89+
$this->subject->getBestPrice()
90+
);
91+
}
92+
93+
#[Test]
94+
public function getBestSpecialPriceReturnsSpecialPriceIfEventHasASpecialPriceWithMatchingUserGroup(): void
95+
{
96+
$this->setUpFrontendUserAspect([42, 43]);
97+
98+
$frontendUserGroup1 = self::createStub(FrontendUserGroup::class);
99+
$frontendUserGroup1->method('getUid')->willReturn(42);
100+
101+
$objectStorage = new ObjectStorage();
102+
$specialPrice1 = $this->createSpecialPriceForFrontendUserGroup(10.00, $frontendUserGroup1);
103+
$objectStorage->attach($specialPrice1);
104+
$this->subject->setSpecialPrices($objectStorage);
105+
106+
self::assertSame(
107+
$specialPrice1,
108+
$this->subject->getBestSpecialPrice()
109+
);
110+
111+
self::assertSame(
112+
10.00,
113+
$this->subject->getBestPrice()
114+
);
115+
}
116+
117+
#[Test]
118+
public function getBestSpecialPriceReturnsBestSpecialPriceIfEventHasSpecialPricesWithMatchingUserGroup(): void
119+
{
120+
$this->setUpFrontendUserAspect([42, 43]);
121+
122+
$frontendUserGroup1 = self::createStub(FrontendUserGroup::class);
123+
$frontendUserGroup1->method('getUid')->willReturn(42);
124+
$frontendUserGroup2 = self::createStub(FrontendUserGroup::class);
125+
$frontendUserGroup2->method('getUid')->willReturn(43);
126+
127+
$objectStorage = new ObjectStorage();
128+
$specialPrice1 = $this->createSpecialPriceForFrontendUserGroup(10.00, $frontendUserGroup1);
129+
$objectStorage->attach($specialPrice1);
130+
$specialPrice2 = $this->createSpecialPriceForFrontendUserGroup(8.00, $frontendUserGroup2);
131+
$objectStorage->attach($specialPrice2);
132+
$this->subject->setSpecialPrices($objectStorage);
133+
134+
self::assertSame(
135+
$specialPrice2,
136+
$this->subject->getBestSpecialPrice()
137+
);
138+
139+
self::assertSame(
140+
8.00,
141+
$this->subject->getBestPrice()
142+
);
143+
}
144+
145+
#[Test]
146+
public function getBestSpecialPriceReturnsBestSpecialPriceForUserGroupIfEventHasSpecialPricesWithMatchingUserGroup(): void
147+
{
148+
$this->setUpFrontendUserAspect([42, 43]);
149+
150+
$frontendUserGroup1 = self::createStub(FrontendUserGroup::class);
151+
$frontendUserGroup1->method('getUid')->willReturn(42);
152+
$frontendUserGroup2 = self::createStub(FrontendUserGroup::class);
153+
$frontendUserGroup2->method('getUid')->willReturn(43);
154+
$frontendUserGroup3 = self::createStub(FrontendUserGroup::class);
155+
$frontendUserGroup3->method('getUid')->willReturn(44);
156+
157+
$objectStorage = new ObjectStorage();
158+
$specialPrice1 = $this->createSpecialPriceForFrontendUserGroup(9.00, $frontendUserGroup1);
159+
$objectStorage->attach($specialPrice1);
160+
$specialPrice2 = $this->createSpecialPriceForFrontendUserGroup(11.00, $frontendUserGroup2);
161+
$objectStorage->attach($specialPrice2);
162+
$specialPrice3 = $this->createSpecialPriceForFrontendUserGroup(7.00, $frontendUserGroup3);
163+
$objectStorage->attach($specialPrice3);
164+
$this->subject->setSpecialPrices($objectStorage);
165+
166+
self::assertSame(
167+
$specialPrice1,
168+
$this->subject->getBestSpecialPrice()
169+
);
170+
171+
self::assertSame(
172+
9.00,
173+
$this->subject->getBestPrice()
174+
);
175+
}
176+
177+
#[Test]
178+
public function getBestSpecialPriceReturnsSpecialPriceIfEventHasASpecialPriceWithNotMatchingUserGroup(): void
179+
{
180+
$this->setUpFrontendUserAspect([41, 43]);
181+
182+
$frontendUserGroup1 = self::createStub(FrontendUserGroup::class);
183+
$frontendUserGroup1->method('getUid')->willReturn(42);
184+
185+
$objectStorage = new ObjectStorage();
186+
$specialPrice1 = $this->createSpecialPriceForFrontendUserGroup(10.00, $frontendUserGroup1);
187+
$objectStorage->attach($specialPrice1);
188+
$this->subject->setSpecialPrices($objectStorage);
189+
190+
self::assertNull(
191+
$this->subject->getBestSpecialPrice()
192+
);
193+
194+
self::assertSame(
195+
$this->price,
196+
$this->subject->getBestPrice()
197+
);
198+
}
199+
200+
#[Test]
201+
public function getBestPriceReturnsPriceIfSpecialPriceIsGreater(): void
202+
{
203+
$this->setUpFrontendUserAspect([42]);
204+
205+
$frontendUserGroup1 = self::createStub(FrontendUserGroup::class);
206+
$frontendUserGroup1->method('getUid')->willReturn(42);
207+
208+
$objectStorage = new ObjectStorage();
209+
$specialPrice1 = $this->createSpecialPriceForFrontendUserGroup(20.00, $frontendUserGroup1);
210+
$objectStorage->attach($specialPrice1);
211+
$this->subject->setSpecialPrices($objectStorage);
212+
213+
self::assertSame(
214+
$specialPrice1,
215+
$this->subject->getBestSpecialPrice()
216+
);
217+
218+
self::assertSame(
219+
$this->price,
220+
$this->subject->getBestPrice()
221+
);
222+
}
223+
224+
private function createSpecialPriceForFrontendUserGroup(float $price, FrontendUserGroup $frontendUserGroup): SpecialPrice
225+
{
226+
$specialPrice = new SpecialPrice();
227+
$specialPrice->setPrice($price);
228+
$specialPrice->setFrontendUserGroup($frontendUserGroup);
229+
230+
return $specialPrice;
231+
}
232+
233+
private function setUpFrontendUserAspect(?array $groupIds = null): void
234+
{
235+
$userAspect = GeneralUtility::makeInstance(
236+
UserAspect::class,
237+
null,
238+
$groupIds
239+
);
240+
241+
$context = GeneralUtility::makeInstance(Context::class);
242+
$context->setAspect(
243+
'frontend.user',
244+
$userAspect
245+
);
246+
}
247+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Extcode\CartEvents\Tests\Functional\Domain\Model;
6+
7+
/*
8+
* This file is part of the package extcode/cart-events.
9+
*
10+
* For the full copyright and license information, please read the
11+
* LICENSE file that was distributed with this source code.
12+
*/
13+
14+
use Extcode\CartEvents\Domain\Model\EventDate;
15+
use PHPUnit\Framework\Attributes\CoversClass;
16+
17+
#[CoversClass(EventDate::class)]
18+
class EventDateTest extends AbstractSpecialPriceTest
19+
{
20+
protected function setUp(): void
21+
{
22+
$this->price = 17.49;
23+
24+
$this->subject = new EventDate();
25+
$this->subject->setPrice($this->price);
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Extcode\CartEvents\Tests\Functional\Domain\Model;
6+
7+
/*
8+
* This file is part of the package extcode/cart-events.
9+
*
10+
* For the full copyright and license information, please read the
11+
* LICENSE file that was distributed with this source code.
12+
*/
13+
14+
use Extcode\CartEvents\Domain\Model\PriceCategory;
15+
use PHPUnit\Framework\Attributes\CoversClass;
16+
17+
#[CoversClass(PriceCategory::class)]
18+
class PriceCategoryTest extends AbstractSpecialPriceTest
19+
{
20+
protected function setUp(): void
21+
{
22+
$this->price = 13.44;
23+
24+
$this->subject = new PriceCategory();
25+
$this->subject->setPrice($this->price);
26+
}
27+
}

0 commit comments

Comments
 (0)