function AcquiaLiftWebTestTarget::testImplementTargetingStructure in Acquia Lift Connector 7        
                          
                  
                        Same name and namespace in other branches
- 7.2 tests/acquia_lift.test \AcquiaLiftWebTestTarget::testImplementTargetingStructure()
File
 
   - tests/acquia_lift.test, line 3386
- Integration tests for Acquia Lift module.
Class
  
  - AcquiaLiftWebTestTarget 
Code
function testImplementTargetingStructure() {
  module_load_include('inc', 'acquia_lift', 'acquia_lift.admin');
  
  $agent = $this
    ->createTargetingAgent();
  $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_update_targeting($agent);
  
  $agents = personalize_agent_load_by_type('acquia_lift');
  $this
    ->assertEqual(1, count($agents));
  $nested_agent = reset($agents);
  $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);
  
  $option_set = personalize_option_set_load($parent_option_set->osid, TRUE);
  $this
    ->assertEqual($nested_osid, $option_set->targeting[$audience_name]['osid']);
  
  $second_audience = personalize_generate_machine_name($this
    ->randomName(), NULL, '-');
  $this
    ->createTargetAudience($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],
    ),
  );
  try {
    acquia_lift_save_targeting_structure($agent, $targeting);
  } catch (AcquiaLiftException $e) {
    $this
      ->fail('Exception thrown when none expected.');
  }
  $this
    ->resetAll();
  
  AcquiaLiftAPI::setTestInstance();
  acquia_lift_update_targeting($agent);
  $nested_option_set = personalize_option_set_load($nested_osid, TRUE);
  $this
    ->assertFalse($nested_option_set);
  
  $agents = personalize_agent_load_by_type('acquia_lift');
  $this
    ->assertEqual(1, count($agents));
  $nested_agent = reset($agents);
  $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']));
  
  $this
    ->assertEqual($option_ids[2], $parent_option_set->targeting[$audience_name]['option_id']);
  
  $this
    ->assertFalse(isset($parent_option_set->targeting[$audience_name]['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(
    $audience_name => array(
      $option_ids[0],
      $option_ids[1],
    ),
    $second_audience => array(
      $option_ids[0],
      $option_ids[2],
    ),
  );
  try {
    acquia_lift_save_targeting_structure($agent, $targeting);
  } catch (AcquiaLiftException $e) {
    $this
      ->fail('Exception thrown when none expected.');
  }
  $this
    ->resetAll();
  
  AcquiaLiftAPI::setTestInstance();
  acquia_lift_update_targeting($agent);
  $nested_option_set = personalize_option_set_load($nested_osid, TRUE);
  $this
    ->assertTrue($nested_option_set);
  
  $agents = personalize_agent_load_by_type('acquia_lift');
  $this
    ->assertEqual(2, count($agents));
  
  foreach ($agents as $nested) {
    
    if ($nested->machine_name != $nested_agent->machine_name) {
      $option_sets = personalize_option_set_load_by_agent($nested->machine_name);
      $nested_os = reset($option_sets);
      $nested_osid2 = $nested_os->osid;
    }
  }
  if (isset($nested_osid2)) {
    
    $parent_option_set = personalize_option_set_load($parent_option_set->osid, TRUE);
    $this
      ->assertEqual(2, count($parent_option_set->targeting));
    foreach (array(
      $audience_name => $nested_osid,
      $second_audience => $nested_osid2,
    ) as $audience => $osid) {
      $this
        ->assertEqual($osid, $parent_option_set->targeting[$audience]['osid']);
      $this
        ->assertFalse(isset($parent_option_set->targeting[$audience]['option_id']));
    }
  }
  else {
    $this
      ->fail('Could not find the second option set');
  }
}