You are here

function AcquiaLiftWebTestTarget::testAgentEditable in Acquia Lift Connector 7.2

Tests which components of a campaign are editable when the depending on agent status.

File

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

Class

AcquiaLiftWebTestTarget

Code

function testAgentEditable() {
  $this
    ->drupalLogin($this->managerUser);
  $agent = $this
    ->createTargetingAgent();
  $option_set = $this
    ->createOptionSet(0, array(
    'plugin' => 'blocks',
    'agent' => $agent->machine_name,
    'option_ids' => array(
      'first-choice',
      'second-choice',
      'third-choice',
    ),
  ));
  $this
    ->resetAll();

  // Load the automatic goal for reference.
  $goals = personalize_goal_load_by_conditions(array(
    'agent' => $agent->machine_name,
  ));
  $this
    ->assertEqual(count($goals), 1);
  $goal = reset($goals);

  // Verify that the agent is shown for administration.
  $this
    ->drupalGet('');
  $settings = $this
    ->drupalGetSettings();
  $this
    ->assertTrue(!empty($settings['acquia_lift']['campaigns'][$agent->machine_name]));

  // Start the agent through the UI so that the targeting gets implemented.
  $this
    ->createTestFromOptionSet($agent, $option_set);
  $this
    ->drupalGet('admin/structure/personalize/manage/' . $agent->machine_name . '/review');
  $this
    ->drupalPost(NULL, array(), $this
    ->getButton('wizard_start'));

  // Verify that the agent is no longer shown for administration.
  $this
    ->drupalGet('');
  $settings = $this
    ->drupalGetSettings();
  $this
    ->assertFalse($settings['acquia_lift']['campaigns'][$agent->machine_name]['editable']);

  // Try to edit the goal.
  try {
    personalize_goal_save($agent->machine_name, 'clicks_option_set_1', 5, $goal->id);
    $this
      ->fail('Should not ever get here because a cannot be saved when agent is running.');
  } catch (Exception $e) {
    $this
      ->assertTrue($e instanceof PersonalizeException);
    $this
      ->assertEqual($e
      ->getMessage(), 'Goals cannot be modified until the personalization is paused.');
  }

  // Try to add a variation
  $option_set->options[] = array(
    'option_id' => 'fourth-choice',
    'option_label' => 'Fourth choice',
  );
  try {
    $option_set = personalize_option_set_save($option_set);
    $this
      ->pass('No errors should be thrown when adding variations to a running campaign.');
  } catch (Exception $e) {
    $this
      ->fail('No errors should be thrown when adding variations to a running campaign.');
  }

  // Try to delete the new variation.
  array_pop($option_set->options);
  try {
    $option_set = personalize_option_set_save($option_set);
    $this
      ->pass('No errors should be thrown when deleting non-targeted variations from a running campaign.');
  } catch (Exception $e) {
    $this
      ->fail('No errors should be thrown when deleting non-targeted variations from a running campaign.');
  }

  // Try to delete a targeted option.
  unset($option_set->options[0]);
  try {
    $option_set = personalize_option_set_save($option_set);
    $this
      ->fail('Should not get here because an error should be thrown while removing this variation.');
  } catch (Exception $e) {
    $this
      ->assertTrue($e instanceof PersonalizeException);
    $this
      ->assertEqual($e
      ->getMessage(), 'Variations used in targeting cannot be removed until the personalization is paused.');
  }

  // Pause the agent.
  personalize_agent_set_status($agent->machine_name, PERSONALIZE_STATUS_PAUSED);

  // Edit the goal.
  personalize_goal_save($agent->machine_name, 'clicks_option_set_1', 5, $goal->id);

  // Verify the change.
  $goal = personalize_goal_load($goal->id);
  $this
    ->assertEqual($goal->value, 5);

  // Remove the variation.
  $option_set = personalize_option_set_save($option_set);
  $this
    ->assertEqual(count($option_set->options), 2);

  // Verify the settings for administration.
  $this
    ->drupalGet('');
  $settings = $this
    ->drupalGetSettings();
  $this
    ->assertTrue($settings['acquia_lift']['campaigns'][$agent->machine_name]['editable']);

  // Schedule the agent.
  $start_date = strtotime('+1 month midnight');
  personalize_agent_set_start_date($agent->machine_name, $start_date);
  personalize_agent_set_status($agent->machine_name, PERSONALIZE_STATUS_SCHEDULED);

  // Try to edit the goal.
  try {
    personalize_goal_save($agent->machine_name, 'clicks_option_set_1', 3, $goal->id);
    $this
      ->fail('Should not ever get here because a cannot be saved when agent is running.');
  } catch (Exception $e) {
    $this
      ->assertTrue($e instanceof PersonalizeException);
    $this
      ->assertEqual($e
      ->getMessage(), 'Goals cannot be modified until the personalization is paused.');
  }

  // Try to delete a targeted option.
  unset($option_set->options[1]);
  try {
    $option_set = personalize_option_set_save($option_set);
    $this
      ->fail('Should not get here because an error should be thrown while removing this variation.');
  } catch (Exception $e) {
    $this
      ->assertTrue($e instanceof PersonalizeException);
    $this
      ->assertEqual($e
      ->getMessage(), 'Variations used in targeting cannot be removed until the personalization is paused.');
  }

  // Verify the agent can't be edited in JS settings.
  $this
    ->drupalGet('');
  $settings = $this
    ->drupalGetSettings();
  $this
    ->assertFalse($settings['acquia_lift']['campaigns'][$agent->machine_name]['editable']);

  // Complete the agent.
  personalize_agent_set_status($agent->machine_name, PERSONALIZE_STATUS_COMPLETED);

  // Try to edit the goal.
  try {
    personalize_goal_save($agent->machine_name, 'clicks_option_set_1', 10, $goal->id);
    $this
      ->fail('Should not ever get here because a cannot be saved when agent is running.');
  } catch (Exception $e) {
    $this
      ->assertTrue($e instanceof PersonalizeException);
    $this
      ->assertEqual($e
      ->getMessage(), 'Goals cannot be modified until the personalization is paused.');
  }

  // Verify the agent still isn't loaded for front-end administration.
  $this
    ->drupalGet('');
  $settings = $this
    ->drupalGetSettings();
  $this
    ->assertTrue(empty($settings['acquia_lift']['campaigns'][$agent->machine_name]));
}