You are here

function AcquiaLiftWebTestTarget::testCreateTargetAudiences in Acquia Lift Connector 7.2

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

File

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

Class

AcquiaLiftWebTestTarget

Code

function testCreateTargetAudiences() {
  module_load_include('inc', 'acquia_lift', 'acquia_lift.admin');
  $agent = $this
    ->createTargetingAgent();
  $this
    ->resetAll();
  $label = $this
    ->randomName();
  $contexts = array(
    array(
      'context' => implode(PERSONALIZE_TARGETING_ADMIN_SEPARATOR, array(
        'some_plugin',
        'some_context',
      )),
      'operator' => 'contains',
      'match' => 'ohai',
    ),
    array(
      'context' => implode(PERSONALIZE_TARGETING_ADMIN_SEPARATOR, array(
        'some_plugin',
        'some_other_context',
      )),
      'operator' => 'starts',
      'match' => 'stuff',
    ),
    array(
      'context' => implode(PERSONALIZE_TARGETING_ADMIN_SEPARATOR, array(
        'some_other_plugin',
        'some_context',
      )),
      'operator' => 'equals',
      'match' => 'kthxbai',
    ),
  );

  // Try saving a target audience without having created an option set.
  $saved = acquia_lift_target_audience_save($label, $agent->machine_name, $contexts, 'AND');
  $this
    ->assertFalse($saved);

  // Now create an option set for the agent.
  $this
    ->createOptionSet(0, array(
    'plugin' => 'blocks',
    'agent' => $agent->machine_name,
    'option_ids' => array(
      'first-choice',
      'second-choice',
      'third-choice',
    ),
  ));
  $this
    ->resetAll();

  // The "everyone else" target audience should have been created.
  $option_set = acquia_lift_get_option_set_for_targeting($agent->machine_name);
  $this
    ->assertTrue(isset($option_set->targeting[ACQUIA_LIFT_TARGETING_EVERYONE_ELSE]));

  // There should not be any targeting set to the agent property yet.
  $agent = personalize_agent_load($agent->machine_name);
  $this
    ->assertFalse(isset($agent->data['lift_targeting']));

  // Add the option set options to the everyone audience.
  $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($targeting, $agent->data['lift_targeting']);
  $saved = acquia_lift_target_audience_save($label, $agent->machine_name, $contexts, 'AND');
  $this
    ->assertTrue($saved);

  // Check that the expected targeting rules were saved on the option set.
  $option_set = personalize_option_set_load($option_set->osid, TRUE);
  $this
    ->assertNotNull($option_set->targeting);
  $audience_name = personalize_generate_machine_name($label, NULL, '-');
  $expected_targeting = array(
    ACQUIA_LIFT_TARGETING_EVERYONE_ELSE => array(
      'label' => t('Everyone else'),
      'weight' => 1,
      'targeting_features' => array(),
      'targeting_rules' => array(),
      'targeting_strategy' => 'OR',
    ),
    $audience_name => array(
      'label' => $label,
      'weight' => 50,
      'targeting_features' => array(
        'some_context::sc-ohai',
        'some_other_context::ss-stuff',
        'some_context::kthxbai',
      ),
      'targeting_rules' => array(
        'some_context::sc-ohai' => array(
          'context' => 'some_context',
          'operator' => 'contains',
          'match' => 'ohai',
          'plugin' => 'some_plugin',
        ),
        'some_other_context::ss-stuff' => array(
          'context' => 'some_other_context',
          'operator' => 'starts',
          'match' => 'stuff',
          'plugin' => 'some_plugin',
        ),
        'some_context::kthxbai' => array(
          'context' => 'some_context',
          'operator' => 'equals',
          'match' => 'kthxbai',
          'plugin' => 'some_other_plugin',
        ),
      ),
      'targeting_strategy' => 'AND',
    ),
  );
  $this
    ->assertEqual($expected_targeting, $option_set->targeting);

  // Add another audience.
  $label = $this
    ->randomName();
  $contexts = array(
    array(
      'context' => implode(PERSONALIZE_TARGETING_ADMIN_SEPARATOR, array(
        'boolean_plugin',
        'some_context',
      )),
      'operator' => 'equals',
      'match' => 1,
    ),
    array(
      'context' => implode(PERSONALIZE_TARGETING_ADMIN_SEPARATOR, array(
        'boolean_plugin',
        'some_other_context',
      )),
      'operator' => 'equals',
      'match' => '0',
    ),
  );
  $this
    ->resetAll();
  $saved = acquia_lift_target_audience_save($label, $agent->machine_name, $contexts, 'OR', 60);
  $this
    ->assertTrue($saved);
  $option_set = personalize_option_set_load($option_set->osid, TRUE);
  $audience_names = array_keys($option_set->targeting);
  $expected_targeting[$audience_names[2]] = array(
    'label' => $label,
    'weight' => 60,
    'targeting_features' => array(
      'some_context::1',
      'some_other_context::0',
    ),
    'targeting_rules' => array(
      'some_context::1' => array(
        'context' => 'some_context',
        'operator' => 'equals',
        'match' => '1',
        'plugin' => 'boolean_plugin',
      ),
      'some_other_context::0' => array(
        'context' => 'some_other_context',
        'operator' => 'equals',
        'match' => '0',
        'plugin' => 'boolean_plugin',
      ),
    ),
    'targeting_strategy' => 'OR',
  );
  $this
    ->assertEqual($expected_targeting, $option_set->targeting);
}