Skip to content

Commit

Permalink
Merge pull request #8 from digifactory/fix/cookies-error-handling
Browse files Browse the repository at this point in the history
Fix/cookies error handling
  • Loading branch information
r-pennings authored Apr 24, 2021
2 parents f24d710 + 1acca72 commit 81e9eab
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 18 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
# Changelog

All notable changes to `laravel-cookie-consent` will be documented in this file.
## 1.0.4 - 2021-04-24
- Updated error handling
- Added tests for error handling

## 1.0.3 - 2021-02-10
- Removed TravisCi
- Added Github workflow actions
- Support for PHP 8.0
- Dropped support for PHP 7.3

## 1.0.2 - 2020-10-05
Laravel 8 support.
- Laravel 8 support.

## 1.0.1 - 2020-02-27
- Renamed all occurences of `CookieBot` to `Cookiebot`
Expand Down
10 changes: 10 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
"name": "Mark Jansen",
"email": "[email protected]",
"role": "Developer"
},
{
"name": "Maurice Bosch",
"email": "[email protected]",
"role": "Developer"
},
{
"name": "Roel Pennings",
"email": "[email protected]",
"role": "Developer"
}
],
"require": {
Expand Down
41 changes: 24 additions & 17 deletions src/Providers/Cookiebot.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,36 @@ private function consentGiven(): Collection
$phpJson = preg_replace('/\s*:\s*([a-zA-Z0-9_]+?)([}\[,])/', ':"$1"$2', preg_replace('/([{\[,])\s*([a-zA-Z0-9_]+?):/', '$1"$2":', str_replace("'", '"', stripslashes($_COOKIE['CookieConsent']))));
$cookieConsent = json_decode($phpJson);

$consentFor = collect();
return $this->getConsentFor($cookieConsent);
}
} else {
//The user has not accepted cookies - set strictly necessary cookies only
return collect(CookieConsent::CONSENT_NECESSARY);
}
}

if (filter_var($cookieConsent->necessary, FILTER_VALIDATE_BOOLEAN)) {
$consentFor->push(CookieConsent::CONSENT_NECESSARY);
}
private function getConsentFor($cookieConsent): Collection
{
$consentFor = collect();

if (filter_var($cookieConsent->preferences, FILTER_VALIDATE_BOOLEAN)) {
$consentFor->push(CookieConsent::CONSENT_PREFERENCES);
}
if (is_object($cookieConsent)) {
if (filter_var($cookieConsent->necessary, FILTER_VALIDATE_BOOLEAN)) {
$consentFor->push(CookieConsent::CONSENT_NECESSARY);
}

if (filter_var($cookieConsent->statistics, FILTER_VALIDATE_BOOLEAN)) {
$consentFor->push(CookieConsent::CONSENT_STATISTICS);
}
if (filter_var($cookieConsent->preferences, FILTER_VALIDATE_BOOLEAN)) {
$consentFor->push(CookieConsent::CONSENT_PREFERENCES);
}

if (filter_var($cookieConsent->marketing, FILTER_VALIDATE_BOOLEAN)) {
$consentFor->push(CookieConsent::CONSENT_MARKETING);
}
if (filter_var($cookieConsent->statistics, FILTER_VALIDATE_BOOLEAN)) {
$consentFor->push(CookieConsent::CONSENT_STATISTICS);
}

return $consentFor;
if (filter_var($cookieConsent->marketing, FILTER_VALIDATE_BOOLEAN)) {
$consentFor->push(CookieConsent::CONSENT_MARKETING);
}
} else {
//The user has not accepted cookies - set strictly necessary cookies only
return collect(CookieConsent::CONSENT_NECESSARY);
}

return $consentFor;
}
}
51 changes: 51 additions & 0 deletions tests/CookiebotProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,40 @@ public function it_allows_necessary_cookies_without_cookie()
$this->assertFalse($cookieConsent->forMarketing());
}

/** @test */
public function it_does_not_allow_empty_cookies()
{
$_COOKIE['CookieConsent'] = '';

$view = view('consent-test')->render();

$this->assertMatchesSnapshot($view);

$cookieConsent = app('cookie-consent');

$this->assertFalse($cookieConsent->forNecessary());
$this->assertFalse($cookieConsent->forPreferences());
$this->assertFalse($cookieConsent->forStatistics());
$this->assertFalse($cookieConsent->forMarketing());
}

/** @test */
public function it_does_not_allow_non_object_cookies()
{
$_COOKIE['CookieConsent'] = '0';

$view = view('consent-test')->render();

$this->assertMatchesSnapshot($view);

$cookieConsent = app('cookie-consent');

$this->assertFalse($cookieConsent->forNecessary());
$this->assertFalse($cookieConsent->forPreferences());
$this->assertFalse($cookieConsent->forStatistics());
$this->assertFalse($cookieConsent->forMarketing());
}

/** @test */
public function it_does_not_allow_cookies()
{
Expand All @@ -55,6 +89,23 @@ public function it_does_not_allow_cookies()
$this->assertFalse($cookieConsent->forMarketing());
}

/** @test */
public function it_allows_necessary_cookies()
{
$_COOKIE['CookieConsent'] = "{stamp:'digifactory/laravel-cookie-consent',necessary:true,preferences:false,statistics:false,marketing:false,ver:3,utc:1582284206975}";

$view = view('consent-test')->render();

$this->assertMatchesSnapshot($view);

$cookieConsent = app('cookie-consent');

$this->assertTrue($cookieConsent->forNecessary());
$this->assertFalse($cookieConsent->forPreferences());
$this->assertFalse($cookieConsent->forStatistics());
$this->assertFalse($cookieConsent->forMarketing());
}

/** @test */
public function it_allows_preference_cookies()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Necessary cookies allowed


Preferences cookies not allowed

Preferences cookies not allowed

Statistics cookies not allowed

Statistics cookies not allowed

Marketing cookies not allowed

Marketing cookies not allowed
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Necessary cookies not allowed

Necessary cookies not allowed

Preferences cookies not allowed

Preferences cookies not allowed

Statistics cookies not allowed

Statistics cookies not allowed

Marketing cookies not allowed

Marketing cookies not allowed
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Necessary cookies not allowed

Necessary cookies not allowed

Preferences cookies not allowed

Preferences cookies not allowed

Statistics cookies not allowed

Statistics cookies not allowed

Marketing cookies not allowed

Marketing cookies not allowed

0 comments on commit 81e9eab

Please sign in to comment.