From a95cd7d20ea8776f8e3e832a0f7f591503d4f8b2 Mon Sep 17 00:00:00 2001 From: upskaling Date: Tue, 29 Aug 2023 07:41:20 +0000 Subject: [PATCH 1/4] fix use RequestStack instead of Request - https://github.com/Harborn-digital/cookie-consent-bundle/issues/65 --- Cookie/CookieChecker.php | 5 +++-- Resources/config/services.yaml | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Cookie/CookieChecker.php b/Cookie/CookieChecker.php index 8d5d6e5..976867d 100644 --- a/Cookie/CookieChecker.php +++ b/Cookie/CookieChecker.php @@ -12,6 +12,7 @@ use ConnectHolland\CookieConsentBundle\Enum\CookieNameEnum; use Symfony\Component\HttpFoundation\Cookie; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\RequestStack; class CookieChecker { @@ -20,9 +21,9 @@ class CookieChecker */ private $request; - public function __construct(Request $request) + public function __construct(RequestStack $request) { - $this->request = $request; + $this->request = $request->getCurrentRequest(); } /** diff --git a/Resources/config/services.yaml b/Resources/config/services.yaml index 3ad5654..c6c3452 100644 --- a/Resources/config/services.yaml +++ b/Resources/config/services.yaml @@ -24,7 +24,8 @@ services: public: true ConnectHolland\CookieConsentBundle\Cookie\CookieChecker: - arguments: ["@=service('request_stack').getCurrentRequest()"] + arguments: + $request: '@request_stack' ConnectHolland\CookieConsentBundle\Cookie\CookieLogger: arguments: From e181ea65dc4c45bf1671753bcd695aa75dd51c80 Mon Sep 17 00:00:00 2001 From: upskaling Date: Wed, 30 Aug 2023 10:14:45 +0000 Subject: [PATCH 2/4] fixup! fix use RequestStack instead of Request --- Resources/config/services.yaml | 4 +++ Tests/Cookie/CookieCheckerTest.php | 17 ++++++++-- .../Twig/CHCookieConsentTwigExtensionTest.php | 33 ++++++------------- Twig/CHCookieConsentTwigExtension.php | 31 ++++++++--------- 4 files changed, 42 insertions(+), 43 deletions(-) diff --git a/Resources/config/services.yaml b/Resources/config/services.yaml index c6c3452..4ca2825 100644 --- a/Resources/config/services.yaml +++ b/Resources/config/services.yaml @@ -30,3 +30,7 @@ services: ConnectHolland\CookieConsentBundle\Cookie\CookieLogger: arguments: $request: "@=service('request_stack').getCurrentRequest()" + + ConnectHolland\CookieConsentBundle\Twig\CHCookieConsentTwigExtension: + arguments: + $cookieChecker: '@ConnectHolland\CookieConsentBundle\Cookie\CookieChecker' \ No newline at end of file diff --git a/Tests/Cookie/CookieCheckerTest.php b/Tests/Cookie/CookieCheckerTest.php index 898062d..f2c3395 100644 --- a/Tests/Cookie/CookieCheckerTest.php +++ b/Tests/Cookie/CookieCheckerTest.php @@ -14,6 +14,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\ParameterBag; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\RequestStack; class CookieCheckerTest extends TestCase { @@ -22,6 +23,11 @@ class CookieCheckerTest extends TestCase */ private $request; + /** + * @var MockObject + */ + private $requestStack; + /** * @var CookieChecker */ @@ -29,8 +35,15 @@ class CookieCheckerTest extends TestCase public function setUp(): void { - $this->request = $this->createMock(Request::class); - $this->cookieChecker = new CookieChecker($this->request); + $this->requestStack = $this->createMock(RequestStack::class); + $this->request = $this->createMock(Request::class); + + $this->requestStack + ->expects($this->any()) + ->method('getCurrentRequest') + ->willReturn($this->request); + + $this->cookieChecker = new CookieChecker($this->requestStack); } /** diff --git a/Tests/Twig/CHCookieConsentTwigExtensionTest.php b/Tests/Twig/CHCookieConsentTwigExtensionTest.php index 5bcf0f8..1bb8db7 100644 --- a/Tests/Twig/CHCookieConsentTwigExtensionTest.php +++ b/Tests/Twig/CHCookieConsentTwigExtensionTest.php @@ -9,10 +9,9 @@ namespace ConnectHolland\CookieConsentBundle\Tests\Twig; +use ConnectHolland\CookieConsentBundle\Cookie\CookieChecker; use ConnectHolland\CookieConsentBundle\Twig\CHCookieConsentTwigExtension; use PHPUnit\Framework\TestCase; -use Symfony\Bridge\Twig\AppVariable; -use Symfony\Component\HttpFoundation\Request; class CHCookieConsentTwigExtensionTest extends TestCase { @@ -21,9 +20,15 @@ class CHCookieConsentTwigExtensionTest extends TestCase */ private $chCookieConsentTwigExtension; + /** + * @var MockObject + */ + private $cookieChecker; + public function setUp(): void { - $this->chCookieConsentTwigExtension = new CHCookieConsentTwigExtension(); + $this->cookieChecker = $this->createMock(CookieChecker::class); + $this->chCookieConsentTwigExtension = new CHCookieConsentTwigExtension($this->cookieChecker); } public function testGetFunctions(): void @@ -37,32 +42,14 @@ public function testGetFunctions(): void public function testIsCookieConsentSavedByUser(): void { - $request = new Request(); - - $appVariable = $this->createMock(AppVariable::class); - $appVariable - ->expects($this->once()) - ->method('getRequest') - ->wilLReturn($request); - - $context = ['app' => $appVariable]; - $result = $this->chCookieConsentTwigExtension->isCookieConsentSavedByUser($context); + $result = $this->chCookieConsentTwigExtension->isCookieConsentSavedByUser(); $this->assertSame($result, false); } public function testIsCategoryAllowedByUser(): void { - $request = new Request(); - - $appVariable = $this->createMock(AppVariable::class); - $appVariable - ->expects($this->once()) - ->method('getRequest') - ->wilLReturn($request); - - $context = ['app' => $appVariable]; - $result = $this->chCookieConsentTwigExtension->isCategoryAllowedByUser($context, 'analytics'); + $result = $this->chCookieConsentTwigExtension->isCategoryAllowedByUser('analytics'); $this->assertSame($result, false); } diff --git a/Twig/CHCookieConsentTwigExtension.php b/Twig/CHCookieConsentTwigExtension.php index 8d9ad1b..875ef08 100644 --- a/Twig/CHCookieConsentTwigExtension.php +++ b/Twig/CHCookieConsentTwigExtension.php @@ -10,7 +10,6 @@ namespace ConnectHolland\CookieConsentBundle\Twig; use ConnectHolland\CookieConsentBundle\Cookie\CookieChecker; -use Symfony\Component\HttpFoundation\Request; use Twig\Extension\AbstractExtension; use Twig\TwigFunction; @@ -26,42 +25,38 @@ public function getFunctions() return [ new TwigFunction( 'chcookieconsent_isCookieConsentSavedByUser', - [$this, 'isCookieConsentSavedByUser'], - ['needs_context' => true] + [$this, 'isCookieConsentSavedByUser'] ), new TwigFunction( 'chcookieconsent_isCategoryAllowedByUser', - [$this, 'isCategoryAllowedByUser'], - ['needs_context' => true] + [$this, 'isCategoryAllowedByUser'] ), ]; } /** - * Checks if user has sent cookie consent form. + * @var CookieChecker */ - public function isCookieConsentSavedByUser(array $context): bool - { - $cookieChecker = $this->getCookieChecker($context['app']->getRequest()); + private $cookieChecker; - return $cookieChecker->isCookieConsentSavedByUser(); + public function __construct(CookieChecker $cookieChecker) + { + $this->cookieChecker = $cookieChecker; } /** - * Checks if user has given permission for cookie category. + * Checks if user has sent cookie consent form. */ - public function isCategoryAllowedByUser(array $context, string $category): bool + public function isCookieConsentSavedByUser(): bool { - $cookieChecker = $this->getCookieChecker($context['app']->getRequest()); - - return $cookieChecker->isCategoryAllowedByUser($category); + return $this->cookieChecker->isCookieConsentSavedByUser(); } /** - * Get instance of CookieChecker. + * Checks if user has given permission for cookie category. */ - private function getCookieChecker(Request $request): CookieChecker + public function isCategoryAllowedByUser(string $category): bool { - return new CookieChecker($request); + return $this->cookieChecker->isCategoryAllowedByUser($category); } } From 222d2a81c27cf81e60edd79204aeabd5108f9cac Mon Sep 17 00:00:00 2001 From: upskaling Date: Fri, 1 Sep 2023 11:43:09 +0000 Subject: [PATCH 3/4] fix replace request by requestStack https://github.com/Harborn-digital/cookie-consent-bundle/pull/118?notification_referrer_id=NT_kwDOAPVzerM3NTM3MzUzMjcxOjE2MDg1ODgy#discussion_r1312869547 --- Cookie/CookieChecker.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Cookie/CookieChecker.php b/Cookie/CookieChecker.php index 976867d..b6e0484 100644 --- a/Cookie/CookieChecker.php +++ b/Cookie/CookieChecker.php @@ -10,20 +10,18 @@ namespace ConnectHolland\CookieConsentBundle\Cookie; use ConnectHolland\CookieConsentBundle\Enum\CookieNameEnum; -use Symfony\Component\HttpFoundation\Cookie; -use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; class CookieChecker { /** - * @var Request + * @var RequestStack */ - private $request; + private $requestStack; - public function __construct(RequestStack $request) + public function __construct(RequestStack $requestStack) { - $this->request = $request->getCurrentRequest(); + $this->requestStack = $requestStack; } /** @@ -31,7 +29,7 @@ public function __construct(RequestStack $request) */ public function isCookieConsentSavedByUser(): bool { - return $this->request->cookies->has(CookieNameEnum::COOKIE_CONSENT_NAME); + return $this->requestStack->getCurrentRequest()->cookies->has(CookieNameEnum::COOKIE_CONSENT_NAME); } /** @@ -39,6 +37,6 @@ public function isCookieConsentSavedByUser(): bool */ public function isCategoryAllowedByUser(string $category): bool { - return $this->request->cookies->get(CookieNameEnum::getCookieCategoryName($category)) === 'true'; + return $this->requestStack->getCurrentRequest()->cookies->get(CookieNameEnum::getCookieCategoryName($category)) === 'true'; } } From 1240756dff7c3311f2bda5d1714d9d8699d99593 Mon Sep 17 00:00:00 2001 From: upskaling Date: Fri, 1 Sep 2023 12:43:12 +0000 Subject: [PATCH 4/4] fixup! fix replace request by requestStack --- Resources/config/services.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Resources/config/services.yaml b/Resources/config/services.yaml index 4ca2825..934a676 100644 --- a/Resources/config/services.yaml +++ b/Resources/config/services.yaml @@ -25,7 +25,7 @@ services: ConnectHolland\CookieConsentBundle\Cookie\CookieChecker: arguments: - $request: '@request_stack' + $requestStack: '@request_stack' ConnectHolland\CookieConsentBundle\Cookie\CookieLogger: arguments: