Skip to content

Commit 2edec5a

Browse files
Merged Pull Request '#41 feature/cookies->main : FEAT: The enable cookies option for JavaScriptBuilderElement is now configurable per-request.'
FEAT: The enable cookies option for JavaScriptBuilderElement is now configurable per-request.
2 parents 460acca + 8ec9ef7 commit 2edec5a

File tree

8 files changed

+117
-634
lines changed

8 files changed

+117
-634
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "javascript-templates"]
2+
path = javascript-templates
3+
url = https://github.com/51degrees/javascript-templates

javascript-templates

Submodule javascript-templates added at 1230271

javascript-templates/JavaScriptResource.mustache

Lines changed: 0 additions & 617 deletions
This file was deleted.

javascript-templates/README.md

Lines changed: 0 additions & 8 deletions
This file was deleted.

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<file>tests/CoreTests.php</file>
55
<file>tests/ExampleTests.php</file>
66
<file>tests/JavaScriptBundlerTests.php</file>
7+
<file>tests/EnableCookiesTests.php</file>
78
<file>tests/FlowDataTests.php</file>
89
<file>tests/SetHeaderTests.php</file>
910
</testsuite>

src/JavascriptBuilderElement.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ public function processInternal(FlowData $flowData): void
168168
$vars['_sessionId'] = $flowData->evidence->get('query.session-id');
169169
$vars['_sequence'] = $flowData->evidence->get('query.sequence');
170170

171+
$enableCookies = $flowData->evidence->get('query.fod-js-enable-cookies');
172+
if ($enableCookies !== null) {
173+
$vars['_enableCookies'] = strtolower($enableCookies) === 'true';
174+
}
175+
171176
$jsParams = [];
172177
foreach ($params as $param => $paramValue) {
173178
$paramKey = explode('.', $param)[1];

tests/EnableCookiesTests.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
/* *********************************************************************
3+
* This Original Work is copyright of 51 Degrees Mobile Experts Limited.
4+
* Copyright 2023 51 Degrees Mobile Experts Limited, Davidson House,
5+
* Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
6+
*
7+
* This Original Work is licensed under the European Union Public Licence
8+
* (EUPL) v.1.2 and is subject to its terms as set out below.
9+
*
10+
* If a copy of the EUPL was not distributed with this file, You can obtain
11+
* one at https://opensource.org/licenses/EUPL-1.2.
12+
*
13+
* The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
14+
* amended by the European Commission) shall be deemed incompatible for
15+
* the purposes of the Work and the provisions of the compatibility
16+
* clause in Article 5 of the EUPL shall not apply.
17+
*
18+
* If using the Work as, or as part of, a network application, by
19+
* including the attribution notice(s) required under Article 5 of the EUPL
20+
* in the end user terms of the application under an appropriate heading,
21+
* such notice(s) shall fulfill the requirements of that article.
22+
* ********************************************************************* */
23+
24+
namespace fiftyone\pipeline\core\tests;
25+
26+
use fiftyone\pipeline\core\AspectPropertyValue;
27+
use fiftyone\pipeline\core\ElementDataDictionary;
28+
use fiftyone\pipeline\core\FlowElement;
29+
use fiftyone\pipeline\core\PipelineBuilder;
30+
use fiftyone\pipeline\core\JavascriptBuilderElement;
31+
use fiftyone\pipeline\core\SequenceElement;
32+
use fiftyone\pipeline\core\JsonBundlerElement;
33+
use PHPUnit\Framework\TestCase;
34+
use PHPUnit\Framework\Attributes\DataProvider;
35+
36+
class CookieElement extends FlowElement
37+
{
38+
public string $dataKey = 'cookie';
39+
40+
public array $properties = [
41+
'javascript' => [
42+
'type' => 'javascript'
43+
]
44+
];
45+
46+
public function processInternal($flowData): void
47+
{
48+
$contents = [];
49+
50+
$contents['javascript'] = "document.cookie = 'some cookie value'";
51+
$contents['normal'] = true;
52+
53+
$data = new ElementDataDictionary($this, $contents);
54+
55+
$flowData->setElementData($data);
56+
}
57+
}
58+
59+
class EnableCookiesTests extends TestCase
60+
{
61+
public static function provider_testJavaScriptCookies()
62+
{
63+
return [
64+
[false, false, false],
65+
[true, false, false],
66+
[false, true, true],
67+
[true, true, true]
68+
];
69+
}
70+
71+
/**
72+
* Test that the cookie settings are respected correctly.
73+
* @dataProvider provider_testJavaScriptCookies
74+
*/
75+
#[DataProvider("provider_testJavaScriptCookies")]
76+
public function testJavaScriptCookies($enableInConfig, $enableInEvidence, $expectCookie)
77+
{
78+
$jsElement = new JavascriptBuilderElement([
79+
'enableCookies' => $enableInConfig
80+
]);
81+
82+
$pipeline = (new PipelineBuilder())
83+
->add(new CookieElement())
84+
->add(new SequenceElement())
85+
->add(new JsonBundlerElement())
86+
->add($jsElement)
87+
->build();
88+
89+
$flowData = $pipeline->createFlowData();
90+
$flowData->evidence->set('query.fod-js-enable-cookies', $enableInEvidence ? 'true' : 'false');
91+
$flowData->process();
92+
93+
$js = $flowData->javascriptbuilder->javascript;
94+
$matches = substr_count($js, 'document.cookie');
95+
if ($expectCookie === true) {
96+
$this->assertSame(2, $matches);
97+
}
98+
else {
99+
$this->assertSame(1, $matches);
100+
}
101+
}
102+
}

tests/SetHeaderTests.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use fiftyone\pipeline\core\tests\classes\TestPipeline;
3232
use fiftyone\pipeline\core\Utils;
3333
use PHPUnit\Framework\TestCase;
34+
use PHPUnit\Framework\Attributes\DataProvider;
3435

3536
class SetHeaderTests extends TestCase
3637
{
@@ -80,11 +81,10 @@ public static function provider_testGetResponseHeaderValue()
8081

8182
/**
8283
* Test response header value to be set for UACH.
83-
*
84+
*
8485
* @dataProvider provider_testGetResponseHeaderValue
85-
* @param mixed $device
86-
* @param mixed $expectedValue
8786
*/
87+
#[DataProvider("provider_testGetResponseHeaderValue")]
8888
public function testGetResponseHeaderValue($device, $expectedValue)
8989
{
9090
$setHeaderPropertiesDict = [
@@ -142,11 +142,9 @@ public static function provider_testGetResponseHeaderName_Valid()
142142

143143
/**
144144
* Test get response header function for valid formats.
145-
*
146145
* @dataProvider provider_testGetResponseHeaderName_Valid
147-
* @param mixed $data
148-
* @param mixed $expectedValue
149146
*/
147+
#[DataProvider("provider_testGetResponseHeaderName_Valid")]
150148
public function testGetResponseHeaderNameValid($data, $expectedValue)
151149
{
152150
$setHeaderElement = new SetHeaderElement();
@@ -166,11 +164,9 @@ public static function provider_testGetResponseHeaderName_InValid()
166164

167165
/**
168166
* Test get response header function for valid formats.
169-
*
170167
* @dataProvider provider_testGetResponseHeaderName_InValid
171-
* @param mixed $data
172-
* @param mixed $expectedValue
173168
*/
169+
#[DataProvider("provider_testGetResponseHeaderName_InValid")]
174170
public function testGetResponseHeaderNameInValid($data, $expectedValue)
175171
{
176172
$setHeaderElement = new SetHeaderElement();

0 commit comments

Comments
 (0)