Skip to content

Commit 513039d

Browse files
oakbanimikeproeng37
authored andcommitted
fix(audience evaluation): evaluate unknown audience id to null and continue (#153)
1 parent c83981b commit 513039d

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

src/Optimizely/ProjectConfig.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2016, 2018, Optimizely
3+
* Copyright 2016, 2018-2019 Optimizely
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -455,7 +455,7 @@ public function getEvent($eventKey)
455455
* @param $audienceId string ID of the audience.
456456
*
457457
* @return Audience Entity corresponding to the ID.
458-
* Dummy entity is returned if ID is invalid.
458+
* Null is returned if ID is invalid.
459459
*/
460460
public function getAudience($audienceId)
461461
{
@@ -465,7 +465,8 @@ public function getAudience($audienceId)
465465

466466
$this->_logger->log(Logger::ERROR, sprintf('Audience ID "%s" is not in datafile.', $audienceId));
467467
$this->_errorHandler->handleError(new InvalidAudienceException('Provided audience is not in datafile.'));
468-
return new Audience();
468+
469+
return null;
469470
}
470471

471472
/**

src/Optimizely/Utils/Validator.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2016-2018, Optimizely
3+
* Copyright 2016-2019, Optimizely
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -160,7 +160,12 @@ public static function isUserInExperiment($config, $experiment, $userAttributes)
160160

161161
$evaluateAudience = function($audienceId) use ($config, $evaluateCustomAttr) {
162162
$conditionTreeEvaluator = new ConditionTreeEvaluator();
163+
163164
$audience = $config->getAudience($audienceId);
165+
if ($audience === null) {
166+
return null;
167+
}
168+
164169
return $conditionTreeEvaluator->evaluate($audience->getConditionsList(), $evaluateCustomAttr);
165170
};
166171

tests/ProjectConfigTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2016-2018, Optimizely
3+
* Copyright 2016-2019, Optimizely
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -546,7 +546,7 @@ public function testGetAudienceInvalidKey()
546546
->method('handleError')
547547
->with(new InvalidAudienceException('Provided audience is not in datafile.'));
548548

549-
$this->assertEquals(new Audience(), $this->config->getAudience('invalid_id'));
549+
$this->assertNull($this->config->getAudience('invalid_id'));
550550
}
551551

552552
public function testGetAttributeValidKey()

tests/UtilsTests/ValidatorTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2016-2018, Optimizely
3+
* Copyright 2016-2019, Optimizely
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -346,6 +346,25 @@ public function testIsUserInExperimentWithAudienceConditionsSetToAudienceIdStrin
346346
);
347347
}
348348

349+
public function testIsUserInExperimentWithUnknownAudienceId()
350+
{
351+
$config = new ProjectConfig(DATAFILE, $this->loggerMock, new NoOpErrorHandler());
352+
$experiment = $config->getExperimentFromKey('test_experiment');
353+
354+
// Both audience Ids and audience conditions exist. Audience Ids is ignored.
355+
$experiment->setAudienceIds([]);
356+
$experiment->setAudienceConditions(["or", "unknown_audience_id", "7718080042"]);
357+
358+
// User qualifies for audience with ID "7718080042".
359+
$this->assertTrue(
360+
Validator::isUserInExperiment(
361+
$config,
362+
$experiment,
363+
['device_type' => 'iPhone', 'location' => 'San Francisco']
364+
)
365+
);
366+
}
367+
349368
// test that isUserInExperiment evaluates simple audience.
350369
public function testIsUserInExperimentWithSimpleAudience()
351370
{

0 commit comments

Comments
 (0)