You are here

function AcquiaLiftWebTestAgentAdmin::testPauseAgents in Acquia Lift Connector 7

Tests that agents are paused when they need to be paused.

File

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

Class

AcquiaLiftWebTestAgentAdmin

Code

function testPauseAgents() {

  // Create an agent with a couple of option sets and a couple of goals.
  $control_rate = 10;
  $explore_rate = 30;
  $agent_name = 'my-test-lift-agent';
  $this
    ->createTestAgent(array(
    'name' => $agent_name,
    'control_rate' => $control_rate,
    'explore_rate' => $explore_rate,
  ));
  $option_set_values = array(
    array(
      'agent' => $agent_name,
      'plugin' => 'type1',
      'option_ids' => array(
        'option-1',
        'option-2',
      ),
    ),
    array(
      'agent' => $agent_name,
      'plugin' => 'type2',
      'option_ids' => array(
        'option-a',
        'option-b',
        'option-c',
      ),
    ),
  );
  $option_sets = array();
  foreach ($option_set_values as $i => $values) {
    list($option_set, $new_queue_items) = $this
      ->createOptionSet($i, $values);
    $option_sets[] = $option_set;
  }
  $first_osid = $option_sets[0]->osid;
  $second_osid = $option_sets[1]->osid;

  // Add goals.
  personalize_goal_save($agent_name, 'first-goal', 1);
  personalize_goal_save($agent_name, 'second-goal', 1);
  $this
    ->resetAll();
  $goals = array();
  foreach (personalize_goal_load_by_conditions(array(
    'agent' => $agent_name,
  )) as $goal) {
    $goals[] = $goal;
  }

  // We need to bypass the personalize_agent_set_status() function because it
  // does the verification check, which would fail.
  variable_set(_personalize_agent_get_status_variable($agent_name), PERSONALIZE_STATUS_RUNNING);

  // Delete a goal - this should not pause the agent.
  personalize_goal_delete($goals[0]->id);
  $this
    ->resetAll();
  $status = personalize_agent_get_status($agent_name);
  $this
    ->assertEqual($status, PERSONALIZE_STATUS_RUNNING);

  // Now delete the remaining goal - this should pause the agent.
  personalize_goal_delete($goals[1]->id);
  $this
    ->resetAll();
  $status = personalize_agent_get_status($agent_name);
  $this
    ->assertEqual($status, PERSONALIZE_STATUS_PAUSED);

  // Add a goal back and set it to running again.
  personalize_goal_save($agent_name, 'third-goal', 1);
  variable_set(_personalize_agent_get_status_variable($agent_name), PERSONALIZE_STATUS_RUNNING);

  // Delete an option set - this should not pause the agent.
  personalize_option_set_delete($first_osid);
  $this
    ->resetAll();
  $status = personalize_agent_get_status($agent_name);
  $this
    ->assertEqual($status, PERSONALIZE_STATUS_RUNNING);

  // Delete an option from the remaining option set, it should remain running
  $option_set = personalize_option_set_load($second_osid);
  unset($option_set->options[2]);
  $option_set = personalize_option_set_save($option_set);
  $this
    ->resetAll();
  $status = personalize_agent_get_status($agent_name);
  $this
    ->assertEqual($status, PERSONALIZE_STATUS_RUNNING);

  // Delete another option, it should be paused.
  unset($option_set->options[1]);
  $option_set = personalize_option_set_save($option_set);
  $this
    ->resetAll();
  $status = personalize_agent_get_status($agent_name);
  $this
    ->assertEqual($status, PERSONALIZE_STATUS_PAUSED);

  // Clear the queue.
  $this->personalizedQueue
    ->deleteQueue();

  // Add the option back and start the campaign again.
  $option_set->options[1] = array(
    'option_id' => 'new-option',
    'option_label' => 'New Option',
  );
  $option_set = personalize_option_set_save($option_set);
  $this
    ->resetAll();
  variable_set(_personalize_agent_get_status_variable($agent_name), PERSONALIZE_STATUS_RUNNING);

  // Cause the queue to fail based on a bad request.
  AcquiaLiftAPI::setTestInstance(TRUE, TRUE);

  // Run the queue - the 400 error will abort the queue and the handleFailedItem() method
  // should pause the agent.
  $_SESSION['acquia_lift_queue_trigger'] = true;
  acquia_lift_process_queue(FALSE);
  $this
    ->resetAll();
  $status = personalize_agent_get_status($agent_name);
  $this
    ->assertEqual($status, PERSONALIZE_STATUS_PAUSED);

  // Set it to running again and delete that last remaining option set.
  variable_set(_personalize_agent_get_status_variable($agent_name), PERSONALIZE_STATUS_RUNNING);
  personalize_option_set_delete($second_osid);
  $this
    ->resetAll();
  $status = personalize_agent_get_status($agent_name);
  $this
    ->assertEqual($status, PERSONALIZE_STATUS_PAUSED);
}