You are here

function AcquiaLiftWebTestTarget::testImplementTargetingStructure in Acquia Lift Connector 7

Same name and namespace in other branches
  1. 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');

  // First, set up our agent, option set, audience and desired targeting
  // structure.
  $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;
  }

  // Keep the option ids in an array.
  $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.');
  }

  // Now implement the targeting structure that is currently just stored in
  // the 'lift_targeting' property.
  AcquiaLiftAPI::setTestInstance();
  acquia_lift_update_targeting($agent);

  // We should have a new nested acquia_lift agent and 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);

  // Confirm the correct options have been added.
  $expected_options = array(
    array(
      'option_id' => $option_ids[1],
    ),
    array(
      'option_id' => $option_ids[2],
    ),
  );
  $this
    ->assertEqual($expected_options, $nested_option_set->options);

  // Confirm this osid is on the original option set's targeting rule for that
  // audience.
  $option_set = personalize_option_set_load($parent_option_set->osid, TRUE);
  $this
    ->assertEqual($nested_osid, $option_set->targeting[$audience_name]['osid']);

  // Create a new target audience and change the strucutre of our campaign.
  $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();

  // When we implement the new structure it should delete the existing nested
  // option set and create a new one.
  AcquiaLiftAPI::setTestInstance();
  acquia_lift_update_targeting($agent);
  $nested_option_set = personalize_option_set_load($nested_osid, TRUE);
  $this
    ->assertFalse($nested_option_set);

  // Check the new agent and 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);

  // Confirm the correct options have been added.
  $expected_options = array(
    array(
      'option_id' => $option_ids[0],
    ),
    array(
      'option_id' => $option_ids[1],
    ),
  );
  $this
    ->assertEqual($expected_options, $nested_option_set->options);

  // Confirm this osid is on the original option set's targeting rule for that
  // audience.
  $parent_option_set = personalize_option_set_load($parent_option_set->osid, TRUE);
  $this
    ->assertEqual($nested_osid, $parent_option_set->targeting[$second_audience]['osid']);

  // This audience should not have an option id property.
  $this
    ->assertFalse(isset($parent_option_set->targeting[$second_audience]['option_id']));

  // Confirm the option_id property is now on the original audience.
  $this
    ->assertEqual($option_ids[2], $parent_option_set->targeting[$audience_name]['option_id']);

  // It should no longer have an osid property.
  $this
    ->assertFalse(isset($parent_option_set->targeting[$audience_name]['osid']));

  // Now let's place our personalized block so we can test how it gets rendered.
  $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();

  // THe parent agent and option set should be in the personalize js settings.
  $this
    ->assertTrue(isset($settings['personalize']['agent_map'][$agent->machine_name]));
  $this
    ->assertTrue(isset($settings['personalize']['option_sets']['osid-' . $parent_option_set->osid]));

  // The nested agent and option set should *not* be in the personlaize js settings.
  $this
    ->assertFalse(isset($settings['personalize']['agent_map'][$nested_agent->machine_name]));
  $this
    ->assertFalse(isset($settings['personalize']['option_sets']['osid-' . $nested_osid]));

  // The nested agent and option set should be in the acquia_lift_target js settings.
  $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]));

  // The parent agent and option set should *not* be in the acquia_lift_target js settings.
  $this
    ->assertFalse(isset($settings['acquia_lift_target']['agent_map'][$agent->machine_name]));
  $this
    ->assertEqual(1, count($settings['acquia_lift_target']['option_sets']));

  // Change the targeting structure again to have two nested tests.
  $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();

  // When we implement the new structure it should not delete the existing nested
  // option set but it should change the audience it belongs to.
  AcquiaLiftAPI::setTestInstance();
  acquia_lift_update_targeting($agent);
  $nested_option_set = personalize_option_set_load($nested_osid, TRUE);
  $this
    ->assertTrue($nested_option_set);

  // We should have two nested agents each with one option set.
  $agents = personalize_agent_load_by_type('acquia_lift');
  $this
    ->assertEqual(2, count($agents));

  // Find the osid of the new option set so we can check that it has been set
  // on the correct target audience.
  foreach ($agents as $nested) {

    // Find the *new* nested agent.
    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)) {

    // Confirm that the correct osid is assigend to each of the two target
    // audiences.
    $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');
  }
}