You are here

function AcquiaLiftWebTestTarget::testStopAndPickWinner in Acquia Lift Connector 7.2

File

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

Class

AcquiaLiftWebTestTarget

Code

function testStopAndPickWinner() {

  // Take stock of the current time so we can check the lift_retired property
  // against it after retiring tests.
  $now = time();
  $agent = $this
    ->createTargetingAgentWithNestedTest(3);
  $this
    ->resetAll();
  $option_sets = personalize_option_set_load_by_agent($agent->machine_name);
  $option_set = reset($option_sets);
  $nested_osid = $option_set->targeting[ACQUIA_LIFT_TARGETING_EVERYONE_ELSE]['osid'];
  $nested_os = personalize_option_set_load($nested_osid);
  $nested_agent_name = $nested_os->agent;
  $expected_targeting = array(
    ACQUIA_LIFT_TARGETING_EVERYONE_ELSE => array(
      'label' => 'Everyone',
      'weight' => 1,
      'targeting_features' => array(),
      'targeting_rules' => array(),
      'targeting_strategy' => 'OR',
      'osid' => $nested_osid,
    ),
  );
  $this
    ->assertEqual($expected_targeting, $option_set->targeting);
  $this
    ->drupalLogin($this->managerUser);
  $this
    ->drupalGet('admin/structure/personalize');
  $this
    ->assertNoLinkByHref('admin/structure/personalize/manage/' . $nested_agent_name . '/delete-all');
  $this
    ->assertText('Test only');
  acquia_lift_stop_test_for_audience($agent, ACQUIA_LIFT_TARGETING_EVERYONE_ELSE, 'option-2');
  $this
    ->resetAll();
  $option_set = personalize_option_set_load($option_set->osid, TRUE);
  $expected_targeting = array(
    ACQUIA_LIFT_TARGETING_EVERYONE_ELSE => array(
      'label' => 'Everyone',
      'weight' => 1,
      'targeting_features' => array(),
      'targeting_rules' => array(),
      'targeting_strategy' => 'OR',
      'option_id' => 'option-2',
    ),
  );
  $this
    ->assertEqual($expected_targeting, $option_set->targeting);
  $retired_test = personalize_agent_load($nested_agent_name, TRUE);
  $this
    ->assertTrue($retired_test->data['lift_retired'] >= $now);
  $this
    ->assertEqual($agent->machine_name, $retired_test->data['lift_parent']);
  $this
    ->assertEqual(ACQUIA_LIFT_TARGETING_EVERYONE_ELSE, $retired_test->data['lift_audience']);
  $status = personalize_agent_get_status($nested_agent_name);
  $this
    ->assertEqual(PERSONALIZE_STATUS_COMPLETED, $status);
  $this
    ->drupalGet('admin/structure/personalize');
  $date = date('Y-m-d');
  $this
    ->assertText("(retired test) {$agent->label}: Everyone (Completed {$date})");
  $this
    ->assertLinkByHref('admin/structure/personalize/manage/' . $nested_agent_name . '/delete-all');
  $this
    ->assertNoText('Test only');

  // Now delete the retired test.
  $this
    ->drupalGet('admin/structure/personalize/manage/' . $nested_agent_name . '/delete-all');
  $this
    ->assertText("Are you sure you want to delete the test {$agent->label}: Everyone (Completed {$date})?");
  $this
    ->drupalPost(NULL, array(), t('Delete'));

  // Confirm both the option set and test have been deleted.
  $nested_os = personalize_option_set_load($nested_osid, TRUE);
  $this
    ->assertFalse($nested_os);
  $nested_test = personalize_agent_load($nested_agent_name, TRUE);
  $this
    ->assertNull($nested_test);

  // Confirm that this has had no effect on the targeting for the parent agent.
  $option_set = personalize_option_set_load($option_set->osid, TRUE);
  $this
    ->assertEqual($expected_targeting, $option_set->targeting);

  // Create another personalization with a test and this time stop the test
  // without specifying a winner.
  $agent = $this
    ->createTargetingAgentWithNestedTest(3, 1);
  $this
    ->resetAll();
  $option_sets = personalize_option_set_load_by_agent($agent->machine_name);
  $option_set = reset($option_sets);
  $this
    ->assertTrue(isset($option_set->targeting[ACQUIA_LIFT_TARGETING_EVERYONE_ELSE]['osid']));
  $this
    ->assertFalse(isset($option_set->targeting[ACQUIA_LIFT_TARGETING_EVERYONE_ELSE]['option_id']));
  acquia_lift_stop_test_for_audience($agent, ACQUIA_LIFT_TARGETING_EVERYONE_ELSE);
  $this
    ->resetAll();
  $option_set = personalize_option_set_load($option_set->osid, TRUE);
  $this
    ->assertFalse(isset($option_set->targeting[ACQUIA_LIFT_TARGETING_EVERYONE_ELSE]['osid']));

  // The first option should now be assigned to the audience.
  $this
    ->assertEqual('option-1', $option_set->targeting[ACQUIA_LIFT_TARGETING_EVERYONE_ELSE]['option_id']);
}