You are here

function AcquiaLiftWebTestTarget::testCreateTargetAudiences in Acquia Lift Connector 7

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

File

tests/acquia_lift.test, line 3232
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 hte agent.
  list($option_set, ) = $this
    ->createOptionSet(0, array(
    'plugin' => 'blocks',
    'agent' => $agent->machine_name,
    'option_ids' => array(
      'first-choice',
      'second-choice',
      'third-choice',
    ),
  ), FALSE);
  $this
    ->resetAll();
  $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 = key($option_set->targeting);
  $expected_targeting = array(
    $audience_name => array(
      'label' => $label,
      'weight' => 0,
      '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');
  $this
    ->assertTrue($saved);
  $option_set = personalize_option_set_load($option_set->osid, TRUE);
  $audience_names = array_keys($option_set->targeting);
  $expected_targeting[$audience_names[1]] = array(
    'label' => $label,
    'weight' => 1,
    '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);
}