|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2019, Optimizely |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +namespace Optimizely\Tests; |
| 18 | + |
| 19 | +use Optimizely\Event\Builder\EventBuilder; |
| 20 | +use Optimizely\Event\Dispatcher\CurlEventDispatcher; |
| 21 | +use Optimizely\Event\LogEvent; |
| 22 | +use Optimizely\ErrorHandler\NoOpErrorHandler; |
| 23 | +use Optimizely\Logger\NoOpLogger; |
| 24 | +use Optimizely\ProjectConfig; |
| 25 | + |
| 26 | +class CurlEventDispatcherTest extends \PHPUnit_Framework_TestCase |
| 27 | +{ |
| 28 | + public function setUp() |
| 29 | + { |
| 30 | + $logger = new NoOpLogger(); |
| 31 | + $this->config = new ProjectConfig(DATAFILE, $logger, new NoOpErrorHandler()); |
| 32 | + $this->eventBuilder = new EventBuilder($logger); |
| 33 | + } |
| 34 | + |
| 35 | + // Test that user input values are escaped. This includes |
| 36 | + // 1. user ID. |
| 37 | + // 2. attribute values of type string. |
| 38 | + // 3. event tag keys. |
| 39 | + // 4. event tag values of type string. |
| 40 | + public function testSanitizeEventPayloadWithEventTags() |
| 41 | + { |
| 42 | + $eventDispatcher = new CurlEventDispatcher(); |
| 43 | + |
| 44 | + $userAttributes = [ |
| 45 | + 'device_type' => 'iPhone', |
| 46 | + 'company' => 'Optimizely', |
| 47 | + ]; |
| 48 | + |
| 49 | + $logEvent = $this->eventBuilder->createConversionEvent( |
| 50 | + $this->config, |
| 51 | + 'purchase', |
| 52 | + 'testUserId', |
| 53 | + $userAttributes, |
| 54 | + array( |
| 55 | + 'revenue' => 56, |
| 56 | + 'value'=> '13.37', |
| 57 | + 'boolean-tag' => false, |
| 58 | + 'float' => 5.5, |
| 59 | + 'integer' => 6 |
| 60 | + ) |
| 61 | + ); |
| 62 | + |
| 63 | + $expectedParams = $logEvent->getParams(); |
| 64 | + |
| 65 | + $expectedUserId = escapeshellarg('testUserId'); |
| 66 | + $expectedParams['visitors'][0]['visitor_id'] = $expectedUserId; |
| 67 | + |
| 68 | + $expectedAttributes = $expectedParams['visitors'][0]['attributes']; |
| 69 | + foreach ($expectedAttributes as &$attr) { |
| 70 | + if (in_array($attr['key'], ['device_type', 'company'])) { |
| 71 | + $attr['value'] = escapeshellarg($attr['value']); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + $expectedParams['visitors'][0]['attributes'] = $expectedAttributes; |
| 76 | + |
| 77 | + $expectedEventTags = []; |
| 78 | + $expectedEventTags[escapeshellarg('revenue')] = 56; |
| 79 | + $expectedEventTags[escapeshellarg('value')] = escapeshellarg('13.37'); |
| 80 | + $expectedEventTags[escapeshellarg('boolean-tag')] = false; |
| 81 | + $expectedEventTags[escapeshellarg('float')] = 5.5; |
| 82 | + $expectedEventTags[escapeshellarg('integer')] = 6; |
| 83 | + |
| 84 | + $expectedParams['visitors'][0]['snapshots'][0]['events'][0]['tags'] = $expectedEventTags; |
| 85 | + |
| 86 | + $this->assertEquals($expectedParams, $eventDispatcher->sanitizeEventPayload($logEvent->getParams())); |
| 87 | + } |
| 88 | +} |
0 commit comments