From 811c2d92f08d4dbf1cd22521679a6dc43fac1f5d Mon Sep 17 00:00:00 2001 From: Owais Date: Fri, 4 Jan 2019 17:22:48 +0500 Subject: [PATCH 1/2] fix(audience evaluation): evaluate with unknown audience Ids --- src/Optimizely/ProjectConfig.php | 7 ++++--- src/Optimizely/Utils/Validator.php | 7 ++++++- tests/ProjectConfigTest.php | 4 ++-- tests/UtilsTests/ValidatorTest.php | 20 +++++++++++++++++++- 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/Optimizely/ProjectConfig.php b/src/Optimizely/ProjectConfig.php index fd172299..4ae69616 100644 --- a/src/Optimizely/ProjectConfig.php +++ b/src/Optimizely/ProjectConfig.php @@ -1,6 +1,6 @@ _logger->log(Logger::ERROR, sprintf('Audience ID "%s" is not in datafile.', $audienceId)); $this->_errorHandler->handleError(new InvalidAudienceException('Provided audience is not in datafile.')); - return new Audience(); + + return null; } /** diff --git a/src/Optimizely/Utils/Validator.php b/src/Optimizely/Utils/Validator.php index 8800a07c..dd6f8eeb 100644 --- a/src/Optimizely/Utils/Validator.php +++ b/src/Optimizely/Utils/Validator.php @@ -1,6 +1,6 @@ getAudience($audienceId); + if ($audience === null) { + return null; + } + return $conditionTreeEvaluator->evaluate($audience->getConditionsList(), $evaluateCustomAttr); }; diff --git a/tests/ProjectConfigTest.php b/tests/ProjectConfigTest.php index b19c7526..82de415f 100644 --- a/tests/ProjectConfigTest.php +++ b/tests/ProjectConfigTest.php @@ -1,6 +1,6 @@ method('handleError') ->with(new InvalidAudienceException('Provided audience is not in datafile.')); - $this->assertEquals(new Audience(), $this->config->getAudience('invalid_id')); + $this->assertNull($this->config->getAudience('invalid_id')); } public function testGetAttributeValidKey() diff --git a/tests/UtilsTests/ValidatorTest.php b/tests/UtilsTests/ValidatorTest.php index b0909a14..459b886a 100644 --- a/tests/UtilsTests/ValidatorTest.php +++ b/tests/UtilsTests/ValidatorTest.php @@ -1,6 +1,6 @@ loggerMock, new NoOpErrorHandler()); + $experiment = $config->getExperimentFromKey('test_experiment'); + + // Both audience Ids and audience conditions exist. Audience Ids is ignored. + $experiment->setAudienceIds([]); + $experiment->setAudienceConditions(["or", "unknown_audience_id", "7718080042"]); + + $this->assertTrue( + Validator::isUserInExperiment( + $config, + $experiment, + ['device_type' => 'iPhone', 'location' => 'San Francisco'] + ) + ); + } + // test that isUserInExperiment evaluates simple audience. public function testIsUserInExperimentWithSimpleAudience() { From 9dc0544e108410305506e22233cbefa3a6f3fb95 Mon Sep 17 00:00:00 2001 From: Owais Date: Wed, 9 Jan 2019 17:21:20 +0500 Subject: [PATCH 2/2] add comment --- tests/UtilsTests/ValidatorTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/UtilsTests/ValidatorTest.php b/tests/UtilsTests/ValidatorTest.php index 459b886a..be1e9769 100644 --- a/tests/UtilsTests/ValidatorTest.php +++ b/tests/UtilsTests/ValidatorTest.php @@ -355,6 +355,7 @@ public function testIsUserInExperimentWithUnknownAudienceId() $experiment->setAudienceIds([]); $experiment->setAudienceConditions(["or", "unknown_audience_id", "7718080042"]); + // User qualifies for audience with ID "7718080042". $this->assertTrue( Validator::isUserInExperiment( $config,