function AcquiaLiftWebTestTarget::testImplementTargetingStructure in Acquia Lift Connector 7.2
Same name and namespace in other branches
- 7 tests/acquia_lift.test \AcquiaLiftWebTestTarget::testImplementTargetingStructure()
File
- tests/acquia_lift.test, line 1924
- Integration tests for Acquia Lift module.
Class
- AcquiaLiftWebTestTarget
Code
function testImplementTargetingStructure() {
module_load_include('inc', 'acquia_lift', 'acquia_lift.admin');
$now = time();
$agent = $this
->createTargetingAgent();
$agent->data['explore_rate'] = 25;
$agent->data['control_rate'] = 25;
$agent->data['decision_style'] = 'adaptive';
$agent->data['cache_decisions'] = 1;
personalize_agent_save($agent);
$this
->resetAll();
$parent_option_set = $this
->createPersonalizedBlock(0, $agent, 3);
if (empty($parent_option_set)) {
$this
->fail('Could not create option set');
return;
}
$option_ids = array();
foreach ($parent_option_set->options as $option) {
$option_ids[] = $option['option_id'];
}
$this
->resetAll();
$audience_name = personalize_generate_machine_name($this
->randomName(), NULL, '-');
$this
->createTargetAudience($parent_option_set, $audience_name);
$targeting = array(
$audience_name => array(
$option_ids[1],
$option_ids[2],
),
);
try {
acquia_lift_save_targeting_structure($agent, $targeting);
} catch (AcquiaLiftException $e) {
$this
->fail('Exception thrown when none expected.');
}
AcquiaLiftAPI::setTestInstance();
acquia_lift_implement_targeting($agent);
$this
->resetAll();
$agents = personalize_agent_load_by_type(self::$agent_plugin);
$this
->assertEqual(1, count($agents));
$nested_agent = reset($agents);
$nested_test_name = $nested_agent->machine_name;
$this
->assertEqual($nested_agent->data['decision_style'], 'adaptive');
$this
->assertEqual($nested_agent->data['control_rate'], 25);
$this
->assertEqual($nested_agent->data['explore_rate'], 25);
$this
->assertEqual($nested_agent->data['cache_decisions'], 1);
$option_sets = personalize_option_set_load_by_agent($nested_agent->machine_name);
$this
->assertEqual(1, count($option_sets));
$nested_osid = key($option_sets);
$nested_option_set = reset($option_sets);
$this
->assertEqual('options', $nested_option_set->plugin);
$expected_options = array(
array(
'option_id' => $option_ids[1],
),
array(
'option_id' => $option_ids[2],
),
);
$this
->assertEqual($expected_options, $nested_option_set->options);
$parent_option_set = personalize_option_set_load($parent_option_set->osid, TRUE);
$this
->assertEqual($nested_osid, $parent_option_set->targeting[$audience_name]['osid']);
$second_audience = personalize_generate_machine_name($this
->randomName(), NULL, '-');
$this
->createTargetAudience($parent_option_set, $second_audience, array(
array(
'context' => implode(PERSONALIZE_TARGETING_ADMIN_SEPARATOR, array(
'some_plugin',
'some_context',
)),
'operator' => 'equals',
'match' => 'kthxbai',
),
));
$targeting = array(
$audience_name => array(
$option_ids[2],
),
$second_audience => array(
$option_ids[0],
$option_ids[1],
),
);
$agent->data['explore_rate'] = 35;
$agent->data['cache_decisions'] = 0;
personalize_agent_save($agent);
try {
acquia_lift_save_targeting_structure($agent, $targeting);
} catch (AcquiaLiftException $e) {
$this
->fail('Exception thrown when none expected.');
}
$this
->resetAll();
AcquiaLiftAPI::setTestInstance();
acquia_lift_implement_targeting($agent);
$this
->resetAll();
$parent_option_set = personalize_option_set_load($parent_option_set->osid, TRUE);
$this
->assertFalse(isset($parent_option_set->targeting[$audience_name]['osid']));
$this
->assertNotEqual($nested_osid, $parent_option_set->targeting[$second_audience]['osid']);
$retired = acquia_lift_get_retired_tests($agent->machine_name);
$this
->assertEqual(1, count($retired));
$retired_test = personalize_agent_load($nested_test_name, TRUE);
$this
->assertTrue($retired_test->data['lift_retired'] >= $now);
$this
->assertEqual($agent->machine_name, $retired_test->data['lift_parent']);
$this
->assertEqual($audience_name, $retired_test->data['lift_audience']);
$status = personalize_agent_get_status($nested_test_name);
$this
->assertEqual(PERSONALIZE_STATUS_COMPLETED, $status);
$nested_os = personalize_option_set_load($parent_option_set->targeting[$second_audience]['osid'], TRUE);
$nested_agent = personalize_agent_load($nested_os->agent);
$this
->assertEqual($nested_agent->data['decision_style'], 'adaptive');
$this
->assertEqual($nested_agent->data['control_rate'], 25);
$this
->assertEqual($nested_agent->data['explore_rate'], 35);
$this
->assertEqual($nested_agent->data['cache_decisions'], 1);
$option_sets = personalize_option_set_load_by_agent($nested_agent->machine_name);
$this
->assertEqual(1, count($option_sets));
$nested_osid = key($option_sets);
$nested_option_set = reset($option_sets);
$this
->assertEqual('options', $nested_option_set->plugin);
$expected_options = array(
array(
'option_id' => $option_ids[0],
),
array(
'option_id' => $option_ids[1],
),
);
$this
->assertEqual($expected_options, $nested_option_set->options);
$parent_option_set = personalize_option_set_load($parent_option_set->osid, TRUE);
$this
->assertEqual($nested_osid, $parent_option_set->targeting[$second_audience]['osid']);
$this
->assertFalse(isset($parent_option_set->targeting[$second_audience]['option_id']));
$first_audience = $audience_name . '-2';
$this
->assertEqual($option_ids[2], $parent_option_set->targeting[$first_audience]['option_id']);
$this
->assertFalse(isset($parent_option_set->targeting[$first_audience]['osid']));
$admin_user = $this
->drupalCreateUser(array(
'access administration pages',
'manage personalized content',
'administer blocks',
));
$this
->drupalLogin($admin_user);
$edit = array(
'blocks[personalize_blocks_' . $parent_option_set->osid . '][region]' => 'content',
);
$this
->drupalPost('admin/structure/block', $edit, t('Save blocks'));
$this
->drupalLogout();
$this
->drupalGet('');
$settings = $this
->drupalGetSettings();
$this
->assertTrue(isset($settings['personalize']['agent_map'][$agent->machine_name]));
$this
->assertTrue(isset($settings['personalize']['option_sets']['osid-' . $parent_option_set->osid]));
$this
->assertFalse(isset($settings['personalize']['agent_map'][$nested_agent->machine_name]));
$this
->assertFalse(isset($settings['personalize']['option_sets']['osid-' . $nested_osid]));
$this
->assertTrue(isset($settings['acquia_lift_target']['agent_map'][$nested_agent->machine_name]));
$this
->assertTrue(isset($settings['acquia_lift_target']['option_sets']['osid-' . $nested_osid]));
$this
->assertFalse(isset($settings['acquia_lift_target']['agent_map'][$agent->machine_name]));
$this
->assertEqual(1, count($settings['acquia_lift_target']['option_sets']));
$targeting = array(
$first_audience => array(
$option_ids[0],
$option_ids[1],
),
$second_audience => array(
$option_ids[0],
$option_ids[2],
),
);
$agent->data['control_rate'] = 50;
personalize_agent_save($agent);
try {
acquia_lift_save_targeting_structure($agent, $targeting);
} catch (AcquiaLiftException $e) {
$this
->fail('Exception thrown when none expected.');
}
$this
->resetAll();
AcquiaLiftAPI::setTestInstance();
acquia_lift_implement_targeting($agent);
$this
->resetAll();
$retired = acquia_lift_get_retired_tests($agent->machine_name);
$this
->assertEqual(2, count($retired));
$first_audience = $first_audience . '-2';
$second_audience = $second_audience . '-2';
$nested = acquia_lift_get_nested_tests($agent);
$this
->assertEqual(2, count($nested));
$nested_agents = personalize_agent_load_multiple($nested);
$new_osids = array();
foreach ($nested_agents as $test) {
$this
->assertEqual($test->data['decision_style'], 'adaptive');
$this
->assertEqual($test->data['control_rate'], 50);
$this
->assertEqual($test->data['explore_rate'], 35);
$this
->assertEqual($test->data['cache_decisions'], 1);
$os = personalize_option_set_load_by_agent($test->machine_name);
$o = reset($os);
$ids = array_map(function ($option) {
return $option['option_id'];
}, $o->options);
if (in_array('option-3', $ids)) {
$new_osids[$second_audience] = $o->osid;
}
else {
$new_osids[$first_audience] = $o->osid;
}
}
$parent_option_set = personalize_option_set_load($parent_option_set->osid, TRUE);
$this
->assertEqual(3, count($parent_option_set->targeting));
foreach ($new_osids as $audience => $osid) {
$this
->assertEqual($osid, $parent_option_set->targeting[$audience]['osid']);
$this
->assertFalse(isset($parent_option_set->targeting[$audience]['option_id']));
}
}