Skip to content

Commit 823ebcb

Browse files
Fixed an issue with setting multiple forced variation values (#55)
* Added test for multiple setForcedVariation calls. * Fixed an issue with setting multiple forced variation values.
1 parent 9f5bb63 commit 823ebcb

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/Optimizely/ProjectConfig.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ public function setForcedVariation($experimentKey, $userId, $variationKey)
411411
}
412412

413413
if (empty($variationKey)) {
414-
unset($this->_forcedVariationMap[$userId]);
414+
unset($this->_forcedVariationMap[$userId][$experimentId]);
415415
$this->_logger->log(Logger::DEBUG, sprintf('Variation mapped to experiment "%s" has been removed for user "%s".', $experimentKey, $userId));
416416
return TRUE;
417417
}
@@ -422,7 +422,8 @@ public function setForcedVariation($experimentKey, $userId, $variationKey)
422422
return FALSE;
423423
}
424424

425-
$this->_forcedVariationMap[$userId] = array($experimentId => $variationId);
425+
$this->_forcedVariationMap[$userId][$experimentId] = $variationId;
426+
426427
$this->_logger->log(Logger::DEBUG, sprintf('Set variation "%s" for experiment "%s" and user "%s" in the forced variation map.', $variationId, $experimentId, $userId));
427428

428429
return TRUE;

tests/ProjectConfigTest.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,11 +630,13 @@ public function testSetGetForcedVariation()
630630
$userId = 'test_user';
631631
$invalidUserId = 'invalid_user';
632632
$experimentKey = 'test_experiment';
633+
$experimentKey2 = 'group_experiment_1';
633634
$invalidExperimentKey = 'invalid_experiment';
634635
$variationKey = 'control';
636+
$variationKey2 = 'group_exp_1_var_1';
635637
$invalidVariationKey = 'invalid_variation';
636-
637-
$optlyObject = new Optimizely(DATAFILE, new ValidEventDispatcher(), $this->loggerMock);
638+
639+
$optlyObject = new Optimizely(DATAFILE, new ValidEventDispatcher(), $this->loggerMock );
638640
$userAttributes = [
639641
'device_type' => 'iPhone',
640642
'location' => 'San Francisco'
@@ -661,6 +663,19 @@ public function testSetGetForcedVariation()
661663
$forcedVariation = $this->config->getForcedVariation($experimentKey, $userId);
662664
$this->assertEquals($variationKey, $forcedVariation->getKey());
663665

666+
// check multiple sets
667+
$this->assertTrue($this->config->setForcedVariation($experimentKey2, $userId, $variationKey2));
668+
$forcedVariation2 = $this->config->getForcedVariation($experimentKey2, $userId);
669+
$this->assertEquals($variationKey2, $forcedVariation2->getKey());
670+
// make sure the second set does not overwrite the first set
671+
$forcedVariation = $this->config->getForcedVariation($experimentKey, $userId);
672+
$this->assertEquals($variationKey, $forcedVariation->getKey());
673+
// make sure unsetting the second experiment-to-variation mapping does not unset the
674+
// first experiment-to-variation mapping
675+
$this->assertTrue($this->config->setForcedVariation($experimentKey2, $userId, null));
676+
$forcedVariation = $this->config->getForcedVariation($experimentKey, $userId);
677+
$this->assertEquals($variationKey, $forcedVariation->getKey());
678+
664679
// an invalid user ID should return a null variation
665680
$this->assertNull($this->config->getForcedVariation($experimentKey, $invalidUserId));
666681
}

0 commit comments

Comments
 (0)