You are here

function AcquiaLiftWebTestTarget::testAssignVariations in Acquia Lift Connector 7.2

Same name and namespace in other branches
  1. 7 tests/acquia_lift.test \AcquiaLiftWebTestTarget::testAssignVariations()

File

tests/acquia_lift.test, line 1847
Integration tests for Acquia Lift module.

Class

AcquiaLiftWebTestTarget

Code

function testAssignVariations() {
  module_load_include('inc', 'acquia_lift', 'acquia_lift.admin');
  $agent = $this
    ->createTargetingAgent();
  $this
    ->assertFalse(isset($agent->data['lift_targeting']));
  $this
    ->resetAll();
  $option_set = $this
    ->createOptionSet(0, array(
    'plugin' => 'blocks',
    'agent' => $agent->machine_name,
    'option_ids' => array(
      'first-choice',
      'second-choice',
      'third-choice',
    ),
  ));
  $this
    ->resetAll();

  // The everyone else audience should have been created automatically.
  // Now assign the initial variations to it.
  $targeting = array(
    ACQUIA_LIFT_TARGETING_EVERYONE_ELSE => array(
      'first-choice',
      'second-choice',
      'third-choice',
    ),
  );
  acquia_lift_save_targeting_structure($agent, $targeting);
  $agent = personalize_agent_load($agent->machine_name);
  $this
    ->assertEqual($agent->data['lift_targeting'], $targeting);

  // Try to set up targeting with a non-existent audience.
  $audience_name = personalize_generate_machine_name($this
    ->randomName(), NULL, '-');
  $targeting = array(
    $audience_name => array(
      'second-choice',
      'third-choice',
    ),
    ACQUIA_LIFT_TARGETING_EVERYONE_ELSE => array(
      'first-choice',
    ),
  );
  try {
    acquia_lift_save_targeting_structure($agent, $targeting);
    $this
      ->fail('Should not reach here');
  } catch (AcquiaLiftException $e) {
    $this
      ->assertEqual('Invalid audience', $e
      ->getMessage());
  }

  // Now create the audience and try again.
  $this
    ->createTargetAudience($option_set, $audience_name);
  $this
    ->resetAll();
  try {
    acquia_lift_save_targeting_structure($agent, $targeting);
  } catch (AcquiaLiftException $e) {
    $this
      ->fail('Exception thrown when none expected.');
  }
  $this
    ->assertEqual($targeting, $agent->data['lift_targeting']);

  // Now delete the option set and re-add to add the new audience but not
  // the targeting variations.
  personalize_option_set_delete($option_set->osid);
  $agent->data['lift_targeting'] = array();
  $agent = personalize_agent_save($agent);
  $this
    ->resetAll();
  $option_set = $this
    ->createOptionSet(1, array(
    'plugin' => 'blocks',
    'agent' => $agent->machine_name,
    'option_ids' => array(
      'first-choice',
      'second-choice',
      'third-choice',
    ),
  ));

  // Verify that there is no targeting on the campaign.
  $agent = personalize_agent_load($agent->machine_name, TRUE);
  $this
    ->assertEqual($agent->data['lift_targeting'], array());

  // Verify that the everyone else audience has been added.
  $option_set = personalize_option_set_load($option_set->osid);
  $expected = array(
    ACQUIA_LIFT_TARGETING_EVERYONE_ELSE => array(
      'label' => t('Everyone'),
      'weight' => 1,
      'targeting_features' => array(),
      'targeting_rules' => array(),
      'targeting_strategy' => 'OR',
    ),
  );
  $this
    ->assertEqual($option_set->targeting, $expected);
}