Skip to content

Commit

Permalink
refactor(sdk): update logs and added test cases for feature rollout w…
Browse files Browse the repository at this point in the history
…hitelisting
  • Loading branch information
Abbas-khaliq authored and softvar committed Oct 21, 2021
1 parent b1ef80f commit 1140b3f
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 11 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.23.2] - 2020-10-21

### Changed

- Updated whitelisting logs for Feature Rollout campaign
- Test cases added to verify whitelisting cases in Feature Rollout campaign

## [1.23.1] - 2021-10-07

### Changed
Expand Down
2 changes: 1 addition & 1 deletion src/Constants/LogMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class LogMessages
'FEATURE_ENABLED_FOR_USER' => '({file}): Feature having feature-key:{featureKey} for user ID:{userId} is {status}',
'USER_IN_FEATURE_ROLLOUT' => '({file}): User ID:{userId} is in feature rollout:{featureKey}',
'USER_NOT_IN_FEATURE_ROLLOUT' => '({file}): User ID:{userId} is NOT in feature rollout:{featureKey}',
'WHITELISTING_ELIGIBILITY_STATUS' => '({file}): User ID:{userId} of campaign:{campaign_key} with variation_targeting_variables:{variation_targeting_variables} {status} whitelisting and hence {variation} variation is assigned',
'WHITELISTING_ELIGIBILITY_STATUS' => '({file}): User ID:{userId} of campaign:{campaign_key} with variation_targeting_variables:{variation_targeting_variables} {status} whitelisting {variation}',
'VARIABLE_FOUND' => '({file}): Value for variable:{variableKey} of campaign:{campaignKey} is:{variableValue} for user:{userId}',
'VARIABLE_NOT_FOUND' => '({file}): Value for variable:{variableKey} of campaign:{campaignKey} is not found for user:{userId}',
'WHITELISTING_SKIPPED' => '({file}): For userId:{userId} of campaign:{campaignKey},{reason} whitelisting was skipped {variation}',
Expand Down
6 changes: 1 addition & 5 deletions src/Core/VariationDecider.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,7 @@ public function fetchVariationData($userStorageObj, $campaign, $userId, $options
);
}
} else {
if ($campaign['type'] === CampaignTypes::AB) {
$decision['isUserWhitelisted'] = !!$bucketInfo['name'];
} elseif ($campaign['type'] === CampaignTypes::FEATURE_TEST) {
$decision['isUserWhitelisted'] = $bucketInfo['isFeatureEnabled'];
}
$decision['isUserWhitelisted'] = true;
}

if ($bucketInfo != null) {
Expand Down
43 changes: 40 additions & 3 deletions src/Utils/Campaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
namespace vwo\Utils;

use Monolog\Logger;
use vwo\Constants\CampaignTypes;
use vwo\Core\Bucketer;
use vwo\Services\LoggerService;
use vwo\Utils\Common as CommonUtil;
Expand Down Expand Up @@ -73,7 +74,19 @@ public static function findVariationFromWhiteListing($campaign, $userId, $option
$bucketInfo = self::getForcedBucket($campaign, $userId, $variationTargetingVariables, $disableLogs);
$status = $bucketInfo != null ? 'satisfy' : "didn't satisfy";

LoggerService::log(Logger::DEBUG, LogMessages::INFO_MESSAGES['WHITELISTING_ELIGIBILITY_STATUS'], ['{status}' => $status, '{userId}' => $userId, '{variation}' => $status == 'satisfy' ? $bucketInfo['name'] : 'no', '{campaign_key}' => $campaign['key'], '{variation_targeting_variables}' => json_encode($variationTargetingVariables)], self::$CLASSNAME, $disableLogs);
LoggerService::log(
Logger::DEBUG,
LogMessages::INFO_MESSAGES['WHITELISTING_ELIGIBILITY_STATUS'],
[
'{status}' => $status,
'{userId}' => $userId,
'{variation}' => $status == 'satisfy' ? ($campaign["type"] == CampaignTypes::FEATURE_ROLLOUT ? 'and hence becomes part of the rollout' : $bucketInfo['name'] . 'and hence variation is assigned') : '',
'{campaign_key}' => $campaign['key'],
'{variation_targeting_variables}' => json_encode($variationTargetingVariables)
],
self::$CLASSNAME,
$disableLogs
);
} else {
LoggerService::log(Logger::INFO, LogMessages::INFO_MESSAGES['WHITELISTING_SKIPPED'], [ '{reason}' => '','{userId}' => $userId, '{campaignKey}' => $campaign['key'],'{variation}' => ''], self::$CLASSNAME, $disableLogs);
}
Expand Down Expand Up @@ -103,9 +116,33 @@ private static function getForcedBucket($campaign, $userId, $variationTargetingV
$totalVariationTraffic += $variation['weight'];
$validVariations[] = $variation;
}
LoggerService::log(Logger::INFO, LogMessages::INFO_MESSAGES['SEGMENTATION_STATUS'], [ '{userId}' => $userId, '{campaignKey}' => $campaign['key'],'{segmentationType}' => 'whitelisting','{variation}' => 'for variation:' . $variation['name'],'{status}' => $result === true ? 'passed' : 'failed','{customVariables}' => json_encode($variationTargetingVariables)], self::$CLASSNAME, $disableLogs);
LoggerService::log(
Logger::INFO,
LogMessages::INFO_MESSAGES['SEGMENTATION_STATUS'],
[
'{userId}' => $userId,
'{campaignKey}' => $campaign['key'],
'{segmentationType}' => 'whitelisting',
'{variation}' => $campaign['type'] == CampaignTypes::FEATURE_ROLLOUT ? '' : 'for variation:' . $variation['name'],
'{status}' => $result === true ? 'passed' : 'failed',
'{customVariables}' => json_encode($variationTargetingVariables)
],
self::$CLASSNAME,
$disableLogs
);
} else {
LoggerService::log(Logger::INFO, LogMessages::INFO_MESSAGES['WHITELISTING_SKIPPED'], [ '{reason}' => 'segment was missing, hence','{userId}' => $userId, '{campaignKey}' => $campaign['key'],'{variation}' => 'for variation:' . $variation['name']], self::$CLASSNAME, $disableLogs);
LoggerService::log(
Logger::INFO,
LogMessages::INFO_MESSAGES['WHITELISTING_SKIPPED'],
[
'{reason}' => 'segment was missing, hence',
'{userId}' => $userId,
'{campaignKey}' => $campaign['key'],
'{variation}' => $campaign['type'] == CampaignTypes::FEATURE_ROLLOUT ? '' : 'for variation:' . $variation['name']
],
self::$CLASSNAME,
$disableLogs
);
}
}
$totalValidVariations = count($validVariations);
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/ImpressionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ImpressionBuilder
/**
* sdk version for api hit
*/
const SDK_VERSION = '1.23.1';
const SDK_VERSION = '1.23.2';
/**
* sdk langauge for api hit
*/
Expand Down
12 changes: 11 additions & 1 deletion src/Utils/Validations.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,17 @@ public static function checkPreSegmentation($campaign, $userId, $options, $disab
);
return $response;
} else {
LoggerService::log(Logger::INFO, LogMessages::INFO_MESSAGES['SEGMENTATION_SKIPPED'], ['{campaignKey}' => $campaign['key'],'{userId}' => $userId,'{variation}' => ''], self::$CLASSNAME, $disableLogs);
LoggerService::log(
Logger::INFO,
LogMessages::INFO_MESSAGES['SEGMENTATION_SKIPPED'],
[
'{campaignKey}' => $campaign['key'],
'{userId}' => $userId,
'{variation}' => ''
],
self::$CLASSNAME,
$disableLogs
);
return true;
}
}
Expand Down
78 changes: 78 additions & 0 deletions tests/VWOTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -694,4 +694,82 @@ public function testGetFeatureVariableValuePassAfterCampaignActivationForFeature
$value = $this->vwoInstance->getFeatureVariableValue($campaignKey, 'V1', $userId);
$this->assertEquals(10, $value);
}

public function testFRWhitelisting()
{
$campaignKey = 'FEATURE_ROLLOUT_KEY';
$options = ['variationTargetingVariables' => ['chrome' => false]];

$settings = FRWhitelistingSettings::setUp();

$vwoInstance = TestUtil::instantiateSdk($settings);
$vwoInstance->eventDispatcher = TestUtil::mockEventDispatcher($this);
foreach ($this->users as $userId) {
$isFeatureEnabled = $vwoInstance->isFeatureEnabled($campaignKey, $userId, $options);
$variableValue = $vwoInstance->getFeatureVariableValue($campaignKey, 'V1', $userId, $options);

$this->assertEquals(true, $isFeatureEnabled);
$this->assertEquals(10, $variableValue);
}
}

public function testFRWhitelistingPassWhenTrafficZero()
{
$campaignKey = 'FEATURE_ROLLOUT_KEY';
$options = ['variationTargetingVariables' => ['chrome' => false]];

$settings = FRWhitelistingSettings::setUp();
$settings["campaigns"][0]["percentTraffic"] = 0;

$vwoInstance = TestUtil::instantiateSdk($settings);
$vwoInstance->eventDispatcher = TestUtil::mockEventDispatcher($this);
foreach ($this->users as $userId) {
$isFeatureEnabled = $vwoInstance->isFeatureEnabled($campaignKey, $userId, $options);
$variableValue = $vwoInstance->getFeatureVariableValue($campaignKey, 'V1', $userId, $options);

$this->assertEquals(true, $isFeatureEnabled);
$this->assertEquals(10, $variableValue);
}
}

public function testFRWhitelistingNotPassed()
{
$campaignKey = 'FEATURE_ROLLOUT_KEY';
$options = ['variationTargetingVariables' => ['chrome' => true]];

$settings = FRWhitelistingSettings::setUp();

$vwoInstance = TestUtil::instantiateSdk($settings);
$vwoInstance->eventDispatcher = TestUtil::mockEventDispatcher($this);
foreach ($this->users as $userId) {
$isFeatureEnabled = $vwoInstance->isFeatureEnabled($campaignKey, $userId, $options);
$variableValue = $vwoInstance->getFeatureVariableValue($campaignKey, 'V1', $userId, $options);

$this->assertEquals(true, $isFeatureEnabled);
$this->assertEquals(10, $variableValue);
}
}

public function testFRWhitelistingNotPassedAndTraffic10()
{
$campaignKey = 'FEATURE_ROLLOUT_KEY';
$options = ['variationTargetingVariables' => ['chrome' => true]];

$settings = FRWhitelistingSettings::setUp();
$settings["campaigns"][0]["percentTraffic"] = 10;

$vwoInstance = TestUtil::instantiateSdk($settings);
$vwoInstance->eventDispatcher = TestUtil::mockEventDispatcher($this);
foreach ($this->users as $userId) {
$isFeatureEnabled = $vwoInstance->isFeatureEnabled($campaignKey, $userId, $options);
$variableValue = $vwoInstance->getFeatureVariableValue($campaignKey, 'V1', $userId, $options);

$expected = ucfirst($this->variationResults['FR_T_10_WHITELISTING_FAIL'][$userId]);
$this->assertEquals($expected, $isFeatureEnabled);
if($expected) {
$expected = 10;
}
$this->assertEquals($expected, $variableValue);
}
}
}
80 changes: 80 additions & 0 deletions tests/assets/FRWhitelistingSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/**
* Copyright 2019-2021 Wingify Software Pvt. Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace vwo;

class FRWhitelistingSettings
{
public static function setup()
{
return array(
'sdkKey' => 'loremipsum123456',
'campaigns' =>
array(
0 =>
[
'id' => 93,
"name" => "Campaign-93",
'status' => 'RUNNING',
'percentTraffic' => 100,
'type' => 'FEATURE_ROLLOUT',
'variables' => [
[
'value' => 10,
'type' => 'integer',
'key' => 'V1',
'id' => 1,
]
],
'key' => 'FEATURE_ROLLOUT_KEY',
'variations' => [
[
'id' => 1,
'name' => 'Feature-Rollout',
'changes' => [],
'weight' => 100,
'segments' =>
[
'or' =>
[
[
"custom_variable" =>
[
"chrome" => "false"
]
]
]
]
],
],
'goals' => [
[
'type' => 'ON_PAGE',
'id' => 1,
'identifier' => 'http://vwo_d_feature-rollout',
]
],
'isBucketingSeedEnabled' => true,
'isForcedVariationEnabled' => true,
],
),
'accountId' => 0,
'version' => 1,
);
}
}
29 changes: 29 additions & 0 deletions tests/assets/VariationResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -871,5 +871,34 @@ class VariationResults
'Zeba'
=> 'Variation-1'
],
'FR_T_10_WHITELISTING_FAIL' => [
'Ashley' => false,
'Bill' => false,
'Chris' => false,
'Dominic' => false,
'Emma' => false,
'Faizan' => true,
'Gimmy' => false,
'Harry' => false,
'Ian' => false,
'John' => false,
'King' => false,
'Lisa' => true,
'Mona' => true,
'Nina' => true,
'Olivia' => false,
'Pete' => false,
'Queen' => true,
'Robert' => false,
'Sarah' => false,
'Tierra' => false,
'Una' => false,
'Varun' => false,
'Will' => false,
'Xin' => false,
'You' => false,
'Zeba' => false
]

];
}
1 change: 1 addition & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@
require 'assets/SettingsWithPreSegmentation.php';
require 'assets/SegmentEvaluatorJson.php';
require 'assets/WhitelistingJson.php';
require 'assets/FRWhitelistingSettings.php';
require 'assets/VariationResults.php';
require 'TestUtil.php';

0 comments on commit 1140b3f

Please sign in to comment.