Skip to content

Commit

Permalink
Merged Pull Request '#41 feature/cookies->main : FEAT: The enable coo…
Browse files Browse the repository at this point in the history
…kies option for JavaScriptBuilderElement is now configurable per-request.'

FEAT: The enable cookies option for JavaScriptBuilderElement is now configurable per-request.
  • Loading branch information
Automation51D authored Jun 4, 2024
2 parents 460acca + 8ec9ef7 commit 2edec5a
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 634 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "javascript-templates"]
path = javascript-templates
url = https://github.com/51degrees/javascript-templates
1 change: 1 addition & 0 deletions javascript-templates
Submodule javascript-templates added at 123027
617 changes: 0 additions & 617 deletions javascript-templates/JavaScriptResource.mustache

This file was deleted.

8 changes: 0 additions & 8 deletions javascript-templates/README.md

This file was deleted.

1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<file>tests/CoreTests.php</file>
<file>tests/ExampleTests.php</file>
<file>tests/JavaScriptBundlerTests.php</file>
<file>tests/EnableCookiesTests.php</file>
<file>tests/FlowDataTests.php</file>
<file>tests/SetHeaderTests.php</file>
</testsuite>
Expand Down
5 changes: 5 additions & 0 deletions src/JavascriptBuilderElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ public function processInternal(FlowData $flowData): void
$vars['_sessionId'] = $flowData->evidence->get('query.session-id');
$vars['_sequence'] = $flowData->evidence->get('query.sequence');

$enableCookies = $flowData->evidence->get('query.fod-js-enable-cookies');
if ($enableCookies !== null) {
$vars['_enableCookies'] = strtolower($enableCookies) === 'true';
}

$jsParams = [];
foreach ($params as $param => $paramValue) {
$paramKey = explode('.', $param)[1];
Expand Down
102 changes: 102 additions & 0 deletions tests/EnableCookiesTests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/* *********************************************************************
* This Original Work is copyright of 51 Degrees Mobile Experts Limited.
* Copyright 2023 51 Degrees Mobile Experts Limited, Davidson House,
* Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
*
* This Original Work is licensed under the European Union Public Licence
* (EUPL) v.1.2 and is subject to its terms as set out below.
*
* If a copy of the EUPL was not distributed with this file, You can obtain
* one at https://opensource.org/licenses/EUPL-1.2.
*
* The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
* amended by the European Commission) shall be deemed incompatible for
* the purposes of the Work and the provisions of the compatibility
* clause in Article 5 of the EUPL shall not apply.
*
* If using the Work as, or as part of, a network application, by
* including the attribution notice(s) required under Article 5 of the EUPL
* in the end user terms of the application under an appropriate heading,
* such notice(s) shall fulfill the requirements of that article.
* ********************************************************************* */

namespace fiftyone\pipeline\core\tests;

use fiftyone\pipeline\core\AspectPropertyValue;
use fiftyone\pipeline\core\ElementDataDictionary;
use fiftyone\pipeline\core\FlowElement;
use fiftyone\pipeline\core\PipelineBuilder;
use fiftyone\pipeline\core\JavascriptBuilderElement;
use fiftyone\pipeline\core\SequenceElement;
use fiftyone\pipeline\core\JsonBundlerElement;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;

class CookieElement extends FlowElement
{
public string $dataKey = 'cookie';

public array $properties = [
'javascript' => [
'type' => 'javascript'
]
];

public function processInternal($flowData): void
{
$contents = [];

$contents['javascript'] = "document.cookie = 'some cookie value'";
$contents['normal'] = true;

$data = new ElementDataDictionary($this, $contents);

$flowData->setElementData($data);
}
}

class EnableCookiesTests extends TestCase
{
public static function provider_testJavaScriptCookies()
{
return [
[false, false, false],
[true, false, false],
[false, true, true],
[true, true, true]
];
}

/**
* Test that the cookie settings are respected correctly.
* @dataProvider provider_testJavaScriptCookies
*/
#[DataProvider("provider_testJavaScriptCookies")]
public function testJavaScriptCookies($enableInConfig, $enableInEvidence, $expectCookie)
{
$jsElement = new JavascriptBuilderElement([
'enableCookies' => $enableInConfig
]);

$pipeline = (new PipelineBuilder())
->add(new CookieElement())
->add(new SequenceElement())
->add(new JsonBundlerElement())
->add($jsElement)
->build();

$flowData = $pipeline->createFlowData();
$flowData->evidence->set('query.fod-js-enable-cookies', $enableInEvidence ? 'true' : 'false');
$flowData->process();

$js = $flowData->javascriptbuilder->javascript;
$matches = substr_count($js, 'document.cookie');
if ($expectCookie === true) {
$this->assertSame(2, $matches);
}
else {
$this->assertSame(1, $matches);
}
}
}
14 changes: 5 additions & 9 deletions tests/SetHeaderTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use fiftyone\pipeline\core\tests\classes\TestPipeline;
use fiftyone\pipeline\core\Utils;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;

class SetHeaderTests extends TestCase
{
Expand Down Expand Up @@ -80,11 +81,10 @@ public static function provider_testGetResponseHeaderValue()

/**
* Test response header value to be set for UACH.
*
*
* @dataProvider provider_testGetResponseHeaderValue
* @param mixed $device
* @param mixed $expectedValue
*/
#[DataProvider("provider_testGetResponseHeaderValue")]
public function testGetResponseHeaderValue($device, $expectedValue)
{
$setHeaderPropertiesDict = [
Expand Down Expand Up @@ -142,11 +142,9 @@ public static function provider_testGetResponseHeaderName_Valid()

/**
* Test get response header function for valid formats.
*
* @dataProvider provider_testGetResponseHeaderName_Valid
* @param mixed $data
* @param mixed $expectedValue
*/
#[DataProvider("provider_testGetResponseHeaderName_Valid")]
public function testGetResponseHeaderNameValid($data, $expectedValue)
{
$setHeaderElement = new SetHeaderElement();
Expand All @@ -166,11 +164,9 @@ public static function provider_testGetResponseHeaderName_InValid()

/**
* Test get response header function for valid formats.
*
* @dataProvider provider_testGetResponseHeaderName_InValid
* @param mixed $data
* @param mixed $expectedValue
*/
#[DataProvider("provider_testGetResponseHeaderName_InValid")]
public function testGetResponseHeaderNameInValid($data, $expectedValue)
{
$setHeaderElement = new SetHeaderElement();
Expand Down

0 comments on commit 2edec5a

Please sign in to comment.