You are here

public function AcquiaLiftWebTestFundamentals::testAcquiaLiftQueue in Acquia Lift Connector 7

File

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

Class

AcquiaLiftWebTestFundamentals

Code

public function testAcquiaLiftQueue() {
  $marketer = $this
    ->drupalCreateUser(array(
    'access administration pages',
    'manage personalized content',
  ));
  $this
    ->drupalLogin($marketer);

  // Create a new agent via the UI.
  $agent = $this
    ->createTestAgent(array(), FALSE);
  $agent_name = $agent
    ->getTitle();
  $machine_name = $agent
    ->getMachineName();

  // There should now be a js setting for triggering queue processing.
  $settings = $this
    ->drupalGetSettings();
  $this
    ->assertEqual(1, $settings['acquia_lift']['sync_queue']);
  $expected_queue_items = array();
  $queued_agent_item = array(
    'method' => 'saveAgent',
    'args' => array(
      $machine_name,
      $agent_name,
      'adaptive',
      PERSONALIZE_STATUS_NOT_STARTED,
      0.1,
      0.2,
      1,
    ),
    'agent' => $machine_name,
  );
  $expected_queue_items[] = $queued_agent_item;
  $this
    ->assertQueueItems($expected_queue_items);

  // Now cause the queue to be processed, which would normally happen
  // via an ajax request.
  $this
    ->drupalGet('acquia_lift/queue');

  // Confirm that the queue is now empty.
  $this
    ->assertQueueItems(array());
  $this
    ->drupalGet('admin/structure/personalize');
  $this
    ->assertNoText(t('At least one of your campaigns has configuration that has not been fully synchronized with Acquia Lift. This should resolve itself on the next cron run.'));

  // Now save it again but don't process the queue.
  $this
    ->drupalPost("admin/structure/personalize/manage/{$machine_name}/edit", array(), $this
    ->getButton('agent'));

  // Save a goal for the agent.
  $goal_name = 'form_submit';
  personalize_goal_save($machine_name, $goal_name, 2);
  $expected_queue_items = array();
  $expected_queue_items[] = $queued_agent_item;
  $expected_queue_items[] = array(
    'method' => 'saveGoal',
    'args' => array(
      $machine_name,
      $goal_name,
    ),
    'agent' => $machine_name,
  );
  $expected_queue_items[] = $queued_agent_item;
  $this
    ->assertQueueItems($expected_queue_items);

  // Since we can't simulate the queued requests timing out during a web
  // test, we simply unset the queue trigger session without processing
  // the queue.
  $this
    ->assertTrue(isset($_SESSION['acquia_lift_queue_trigger']));

  // Log the user out and back in again to get rid of the session variable.
  $this
    ->drupalLogout();
  $this
    ->drupalLogin($marketer);

  // Now they should get a message warning them that there are items that
  // need to get sync'd to Acquia Lift.
  $this
    ->drupalGet('admin/structure/personalize');
  $this
    ->assertText(t('At least one of your campaigns has configuration that has not been fully synchronized with Acquia Lift. This should resolve itself on the next cron run.'));
  $this
    ->drupalLogout();
  $admin_user = $this
    ->drupalCreateUser(array(
    'administer site configuration',
    'access administration pages',
    'manage personalized content',
  ));
  $this
    ->drupalLogin($admin_user);
  $this
    ->drupalGet('admin/structure/personalize');
  $this
    ->assertRaw(t('At least one of your campaigns has configuration that has not been fully synchronized with Acquia Lift. This should resolve itself on the next cron run.') . t(' Click here to <a href="@cron">run cron manually</a>.', array(
    '@cron' => url('admin/reports/status/run-cron'),
  )));

  // Now run cron
  $this
    ->drupalGet('admin/reports/status/run-cron');
  $this
    ->assertQueueItems(array());

  // Now save the agent again.
  $this
    ->drupalPost("admin/structure/personalize/manage/{$machine_name}/edit", array(), $this
    ->getButton('agent'));
  $expected_queue_items = array(
    $queued_agent_item,
  );
  $this
    ->assertQueueItems($expected_queue_items);

  // Cause the queue to fail.
  AcquiaLiftAPI::setTestInstance(TRUE);

  // Run the queue.
  $_SESSION['acquia_lift_queue_trigger'] = true;
  acquia_lift_process_queue(FALSE);
  $expected_logs = array();
  for ($i = 0; $i <= AcquiaLiftQueue::MAX_RETRIES; $i++) {
    $expected_logs[] = array(
      'level' => 'error',
      'message' => "The campaign {$machine_name} could not be pushed to Acquia Lift",
    );
  }
  $test_logger = new AcquiaLiftTestLogger(FALSE);
  $logs = $test_logger
    ->getLogs();
  $this
    ->assertEqual($expected_logs, $logs);

  // Item should be removed after final attempt.
  $this
    ->assertQueueItems(array());
  $test_logger
    ->clearLogs();

  // Edit the agent again.
  $edit = array(
    'cache_decisions' => FALSE,
  );
  $this
    ->drupalPost("admin/structure/personalize/manage/{$machine_name}/edit", $edit, $this
    ->getButton('agent'));
  personalize_agent_set_status($machine_name, PERSONALIZE_STATUS_PAUSED);
  $this
    ->resetAll();
  $queued_agent_item['args'][6] = FALSE;
  $expected_queue_items = array(
    $queued_agent_item,
  );
  $expected_queue_items[] = array(
    'method' => 'updateAgentStatus',
    'args' => array(
      $machine_name,
      PERSONALIZE_STATUS_PAUSED,
    ),
    'agent' => $machine_name,
  );
  $this
    ->assertQueueItems($expected_queue_items);
  $this
    ->resetAll();

  // Cause the queue to fail based on a bad request. There should not be any retries,
  // it should just be aborted.
  AcquiaLiftAPI::setTestInstance(TRUE, TRUE);

  // 2nd param specifies simulating client-side error, i.e. 400 error.
  // Run the queue.
  $_SESSION['acquia_lift_queue_trigger'] = true;
  acquia_lift_process_queue(FALSE);
  $expected_logs = array();
  $expected_logs[] = array(
    'level' => 'error',
    'message' => "The campaign {$machine_name} could not be pushed to Acquia Lift",
  );
  $logs = $test_logger
    ->getLogs();
  $this
    ->assertEqual($expected_logs, $logs);

  // Item should be removed after final attempt.
  $this
    ->assertQueueItems(array());
}