|
| 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 | +} |
0 commit comments