You are here

public function AcquiaLiftWebTestWorkflow::testPersonalizeFieldsAutoCreateGoal in Acquia Lift Connector 7

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

Tests automatic creation of a goal for personalized fields and auto- starting of the campaign.

File

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

Class

AcquiaLiftWebTestWorkflow

Code

public function testPersonalizeFieldsAutoCreateGoal() {
  $this
    ->resetAll();
  module_enable(array(
    'personalize_fields',
  ));
  $this
    ->resetAll();
  $admin_user = $this
    ->drupalCreateUser(array(
    'access administration pages',
    'administer site configuration',
    'access content',
    'administer content types',
    'administer nodes',
    'bypass node access',
    'manage personalized content',
  ));
  $this
    ->drupalLogin($admin_user);

  // Add personalizable field to the article node type.
  $field = array(
    'type' => 'text',
    'field_name' => 'article_headline',
    'cardinality' => -1,
    'settings' => array(
      'personalize' => array(
        'enabled' => 1,
        'agent_type' => 'acquia_lift',
        'options' => array(
          'acquia_lift' => array(
            'control_rate' => 10,
            'decision_style' => 'adaptive',
            'explore_rate' => 25,
          ),
        ),
        'create_goal' => 1,
        'auto_start' => 1,
      ),
    ),
  );
  field_create_field($field);
  $instance = array(
    'field_name' => 'article_headline',
    'entity_type' => 'node',
    'label' => 'Personalizable Headline',
    'bundle' => 'article',
    'required' => FALSE,
  );
  field_create_instance($instance);
  list($node1, $os1, $agent_name) = $this
    ->createPersonalizedField();
  $first_osid = 'osid-' . $os1->osid;
  $goals = personalize_goal_load_by_conditions(array(
    'agent' => $agent_name,
  ));
  $this
    ->assertEqual(1, count($goals));
  $goal = reset($goals);
  $action = visitor_actions_custom_load($goal->action);
  $this
    ->assertEqual('click', $action['event']);
  $this
    ->assertEqual('[data-personalize=osid-1]', $action['identifier']);
  $action_name = personalize_generate_machine_name(t('Clicks @option_set', array(
    '@option_set' => $os1->label,
  )), NULL, '_');
  $this
    ->assertEqual($action_name, $action['machine_name']);
  $this
    ->assertTrue($action['limited_use']);
  $expected_queue_items = array(
    array(
      'method' => 'saveAgent',
      'args' => array(
        $agent_name,
        'Article: Personalizable Headline 1',
        'adaptive',
        PERSONALIZE_STATUS_NOT_STARTED,
        10 / 100,
        25 / 100,
        true,
      ),
      'agent' => $agent_name,
    ),
    array(
      'method' => 'savePoint',
      'args' => array(
        $agent_name,
        $first_osid,
      ),
      'agent' => $agent_name,
    ),
    array(
      'method' => 'saveDecision',
      'args' => array(
        $agent_name,
        $first_osid,
        $first_osid,
      ),
      'agent' => $agent_name,
    ),
    array(
      'method' => 'saveChoice',
      'args' => array(
        $agent_name,
        $first_osid,
        $first_osid,
        'first-value',
      ),
      'agent' => $agent_name,
    ),
    array(
      'method' => 'saveChoice',
      'args' => array(
        $agent_name,
        $first_osid,
        $first_osid,
        'second-value',
      ),
      'agent' => $agent_name,
    ),
    array(
      'method' => 'saveGoal',
      'args' => array(
        $agent_name,
        $action_name,
      ),
      'agent' => $agent_name,
    ),
    array(
      'callback' => 'personalize_agent_set_status',
      'args' => array(
        $agent_name,
        PERSONALIZE_STATUS_RUNNING,
      ),
    ),
  );
  $this
    ->assertQueueItems($expected_queue_items);

  // Manually set the agent's status to running, bypassing the verification check.
  variable_set(_personalize_agent_get_status_variable($agent_name), PERSONALIZE_STATUS_RUNNING);
  $this
    ->resetAll();
  $expected_action_listeners = array(
    $action_name => array(
      array(
        'agent' => $agent_name,
        'value' => 1,
      ),
    ),
  );

  // Confirm that the goal is in the js settings on the /user page.
  $this
    ->drupalGet('user');
  $settings = $this
    ->drupalGetSettings();
  $this
    ->assertEqual($expected_action_listeners, $settings['personalize']['actionListeners']);

  // Change the field settings to specify particular pages for the goal.
  $edit = array(
    'field[settings][personalize][goal_pages]' => "node",
  );
  $this
    ->drupalPost('admin/structure/types/manage/article/fields/article_headline', $edit, t('Save settings'));
  $this
    ->resetAll();

  // Create another personalized field on a new node.
  list($node2, $os2, $second_agent) = $this
    ->createPersonalizedField();

  // Manually set the agent's status to running, bypassing the verification check.
  variable_set(_personalize_agent_get_status_variable($second_agent), PERSONALIZE_STATUS_RUNNING);
  $this
    ->resetAll();
  $goals = personalize_goal_load_by_conditions(array(
    'agent' => $second_agent,
  ));
  $this
    ->assertEqual(1, count($goals));
  $goal = reset($goals);

  // Confirm that the goal is *not* in the js settings on the user page but is
  // on the node page.
  $this
    ->drupalGet('user');
  $settings = $this
    ->drupalGetSettings();
  $this
    ->assertFalse(isset($settings['personalize']['actionListeners'][$goal->action]));
  $this
    ->drupalGet('node');
  $settings = $this
    ->drupalGetSettings();
  $expected_action_listeners[$goal->action] = array(
    array(
      'agent' => $second_agent,
      'value' => 1,
    ),
  );
  $this
    ->assertEqual($expected_action_listeners, $settings['personalize']['actionListeners']);
  $this->personalizedQueue
    ->deleteQueue();

  // Now change the field settings to not auto-create a goal.
  $edit = array(
    'field[settings][personalize][create_goal]' => FALSE,
  );
  $this
    ->drupalPost('admin/structure/types/manage/article/fields/article_headline', $edit, t('Save settings'));
  list($node3, $os3, $third_agent) = $this
    ->createPersonalizedField();
  $third_osid = 'osid-' . $os3->osid;
  $this
    ->resetAll();

  // Confirm that our agent and option set were created.
  $option_set = personalize_option_set_load($os3->osid);
  $this
    ->assertEqual($third_agent, $option_set->agent);

  // There should be no goal for this agent.
  $goals = personalize_goal_load_by_conditions(array(
    'agent' => $third_agent,
  ));
  $this
    ->assertTrue(empty($goals));

  // We should only have items in the queue for saving the agent and the
  // option set.
  $expected_queue_items = array(
    array(
      'method' => 'saveAgent',
      'args' => array(
        $third_agent,
        'Article: Personalizable Headline 3',
        'adaptive',
        PERSONALIZE_STATUS_NOT_STARTED,
        10 / 100,
        25 / 100,
        true,
      ),
      'agent' => $third_agent,
    ),
    array(
      'method' => 'savePoint',
      'args' => array(
        $third_agent,
        $third_osid,
      ),
      'agent' => $third_agent,
    ),
    array(
      'method' => 'saveDecision',
      'args' => array(
        $third_agent,
        $third_osid,
        $third_osid,
      ),
      'agent' => $third_agent,
    ),
    array(
      'method' => 'saveChoice',
      'args' => array(
        $third_agent,
        $third_osid,
        $third_osid,
        'first-value',
      ),
      'agent' => $third_agent,
    ),
    array(
      'method' => 'saveChoice',
      'args' => array(
        $third_agent,
        $third_osid,
        $third_osid,
        'second-value',
      ),
      'agent' => $third_agent,
    ),
  );
  $this
    ->assertQueueItems($expected_queue_items);
}