You are here

function AcquiaLiftWebTestVariationSets::testVariationSets in Acquia Lift Connector 7

Tests variation set creation.

A variation set can be made up of multiple option sets.

@todo This should probably be combined with testPersonalizeElementsVariationSets() above and simplified to just personalize_elements elements (but with the complexity of this test).

File

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

Class

AcquiaLiftWebTestVariationSets

Code

function testVariationSets() {
  module_load_include('inc', 'acquia_lift', 'acquia_lift.page_variations');
  $admin_user = $this
    ->drupalCreateUser(array(
    'access administration pages',
    'manage personalized content',
    'administer blocks',
  ));
  $this
    ->drupalLogin($admin_user);

  // Create a new agent via the UI.
  $agent_title = $this
    ->randomName();
  $agent_name = personalize_generate_machine_name($agent_title, 'personalize_agent_machine_name_exists');
  $edit = array(
    'agent_basic_info[title]' => $agent_title,
    'agent_basic_info[machine_name]' => $agent_name,
    'agent_basic_info[agent_type]' => 'test_agent',
  );
  $this
    ->drupalPost('admin/structure/personalize/add', $edit, $this
    ->getButton('agent'));
  $option_set = personalize_elements_get_option_set_for_variation('option-1', $agent_name, '#selector1', 'addCss', 'page1');
  $var_num = acquia_lift_page_variation_create('first_variation_set', $option_set, array(
    'personalize_elements_content' => 'test-class',
  ));
  $this
    ->assertEqual(1, $var_num);

  // Now create another option set and use it for the second variation.
  $option_set_2 = personalize_elements_get_option_set_for_variation('option-2', $agent_name, '#selector2', 'editHtml', 'page1');
  $var_num = acquia_lift_page_variation_create('first_variation_set', $option_set_2, array(
    'personalize_elements_content' => '<p>some html</p>',
  ));
  $this
    ->assertEqual(2, $var_num);

  // Now use the first option set for variation 3.
  $os_1 = personalize_option_set_load($option_set->osid, TRUE);
  $var_num = acquia_lift_page_variation_create('first_variation_set', $os_1, array(
    'personalize_elements_content' => 'another-test-class',
  ));
  $this
    ->assertEqual(3, $var_num);

  // Now add a different option to both for variation 4.
  $os_1 = personalize_option_set_load($option_set->osid, TRUE);
  $var_num = acquia_lift_page_variation_create('first_variation_set', $os_1, array(
    'personalize_elements_content' => 'third-test-class',
  ));
  $this
    ->assertEqual(4, $var_num);

  // Rename the third variation to test if the new names persist across changes.
  acquia_lift_page_variation_rename('first_variation_set', $agent_name, 3, 'three');
  $os_2 = personalize_option_set_load($option_set_2->osid, TRUE);
  $var_num = acquia_lift_page_variation_create('first_variation_set', $os_2, array(
    'personalize_elements_content' => '<p>some other html</p>',
  ), 4);
  $this
    ->assertEqual(4, $var_num);

  // Try to create variation 6, it should correct it to 5.
  $var_num = acquia_lift_page_variation_create('first_variation_set', $os_2, array(
    'personalize_elements_content' => '<p>third html</p>',
  ), 6);
  $this
    ->assertEqual(5, $var_num);

  // Create another option set and add it to variation 1. (We need to
  // make sure an option gets created for each subsequent variation.)
  $option_set_3 = personalize_elements_get_option_set_for_variation('option-3', $agent_name, '#selector3', 'editText', 'page1');
  $var_num = acquia_lift_page_variation_create('first_variation_set', $option_set_3, array(
    'personalize_elements_content' => 'edited text',
  ), 1);
  $this
    ->assertEqual(1, $var_num);
  $this
    ->resetAll();
  $option_sets = personalize_option_set_load_multiple(FALSE, array(
    'agent' => $agent_name,
  ));
  $osids = array_keys($option_sets);
  $this
    ->assertEqual(3, count($option_sets));
  foreach ($option_sets as $osid => $option_set) {
    $this
      ->assertEqual('first_variation_set', $option_set->decision_name);
    $this
      ->assertEqual(6, count($option_set->options));
  }
  $expected_options_os1 = array(
    array(
      'option_id' => PERSONALIZE_CONTROL_OPTION_ID,
      'option_label' => PERSONALIZE_CONTROL_OPTION_LABEL,
    ),
    array(
      'option_id' => 'variation-1',
      'option_label' => 'Variation #1',
      'personalize_elements_content' => 'test-class',
    ),
    array(
      'option_id' => 'variation-2',
      'option_label' => 'Variation #2',
    ),
    array(
      'option_id' => 'variation-3',
      'option_label' => 'three',
      'personalize_elements_content' => 'another-test-class',
    ),
    array(
      'option_id' => 'variation-4',
      'option_label' => 'Variation #4',
      'personalize_elements_content' => 'third-test-class',
    ),
    array(
      'option_id' => 'variation-5',
      'option_label' => 'Variation #5',
    ),
  );
  $this
    ->assertEqual($expected_options_os1, $option_sets[$osids[0]]->options);
  $expected_options_os2 = array(
    array(
      'option_id' => PERSONALIZE_CONTROL_OPTION_ID,
      'option_label' => PERSONALIZE_CONTROL_OPTION_LABEL,
    ),
    array(
      'option_id' => 'variation-1',
      'option_label' => 'Variation #1',
    ),
    array(
      'option_id' => 'variation-2',
      'option_label' => 'Variation #2',
      'personalize_elements_content' => '<p>some html</p>',
    ),
    array(
      'option_id' => 'variation-3',
      'option_label' => 'three',
    ),
    array(
      'option_id' => 'variation-4',
      'option_label' => 'Variation #4',
      'personalize_elements_content' => '<p>some other html</p>',
    ),
    array(
      'option_id' => 'variation-5',
      'option_label' => 'Variation #5',
      'personalize_elements_content' => '<p>third html</p>',
    ),
  );
  $this
    ->assertEqual($expected_options_os2, $option_sets[$osids[1]]->options);
  $expected_options_os3 = array(
    array(
      'option_id' => PERSONALIZE_CONTROL_OPTION_ID,
      'option_label' => PERSONALIZE_CONTROL_OPTION_LABEL,
    ),
    array(
      'option_id' => 'variation-1',
      'option_label' => 'Variation #1',
      'personalize_elements_content' => 'edited text',
    ),
    array(
      'option_id' => 'variation-2',
      'option_label' => 'Variation #2',
    ),
    array(
      'option_id' => 'variation-3',
      'option_label' => 'three',
    ),
    array(
      'option_id' => 'variation-4',
      'option_label' => 'Variation #4',
    ),
    array(
      'option_id' => 'variation-5',
      'option_label' => 'Variation #5',
    ),
  );
  $this
    ->assertEqual($expected_options_os3, $option_sets[$osids[2]]->options);

  // Rename the second variation.
  acquia_lift_page_variation_rename('first_variation_set', $agent_name, 2, 'Hello');
  $expected_options_os1[2]['option_label'] = 'Hello';
  $expected_options_os2[2]['option_label'] = 'Hello';
  $expected_options_os3[2]['option_label'] = 'Hello';
  $option_sets = personalize_option_set_load_multiple(FALSE, array(
    'decision_name' => 'first_variation_set',
    'agent' => $agent_name,
  ));
  $this
    ->assertEqual($expected_options_os1, $option_sets[$osids[0]]->options);
  $this
    ->assertEqual($expected_options_os2, $option_sets[$osids[1]]->options);
  $this
    ->assertEqual($expected_options_os3, $option_sets[$osids[2]]->options);

  // Delete the third variation.
  acquia_lift_page_variation_delete('first_variation_set', $agent_name, 3);
  unset($expected_options_os1[3]);
  unset($expected_options_os2[3]);
  unset($expected_options_os3[3]);
  $option_sets = personalize_option_set_load_multiple(FALSE, array(
    'decision_name' => 'first_variation_set',
    'agent' => $agent_name,
  ));
  $this
    ->assertEqual($expected_options_os1, $option_sets[$osids[0]]->options);
  $this
    ->assertEqual($expected_options_os2, $option_sets[$osids[1]]->options);
  $this
    ->assertEqual($expected_options_os3, $option_sets[$osids[2]]->options);

  // Delete another out of order and verify.
  acquia_lift_page_variation_delete('first_variation_set', $agent_name, 1);
  unset($expected_options_os1[1]);
  unset($expected_options_os2[1]);
  unset($expected_options_os3[1]);
  $option_sets = personalize_option_set_load_multiple(FALSE, array(
    'decision_name' => 'first_variation_set',
    'agent' => $agent_name,
  ));
  $this
    ->assertEqual($expected_options_os1, $option_sets[$osids[0]]->options);
  $this
    ->assertEqual($expected_options_os2, $option_sets[$osids[1]]->options);
  $this
    ->assertEqual($expected_options_os3, $option_sets[$osids[2]]->options);

  // Now delete down to one left
  acquia_lift_page_variation_delete('first_variation_set', $agent_name, 2);
  acquia_lift_page_variation_delete('first_variation_set', $agent_name, 4);
  unset($expected_options_os1[2]);
  unset($expected_options_os2[2]);
  unset($expected_options_os3[2]);
  unset($expected_options_os1[4]);
  unset($expected_options_os2[4]);
  unset($expected_options_os3[4]);
  $option_sets = personalize_option_set_load_multiple(FALSE, array(
    'decision_name' => 'first_variation_set',
    'agent' => $agent_name,
  ));
  $this
    ->assertEqual($expected_options_os1, $option_sets[$osids[0]]->options);
  $this
    ->assertEqual($expected_options_os2, $option_sets[$osids[1]]->options);
  $this
    ->assertEqual($expected_options_os3, $option_sets[$osids[2]]->options);

  // Delete the last and verify that all variations (including control) are
  // removed.
  acquia_lift_page_variation_delete('first_variation_set', $agent_name, 5);
  $option_sets = personalize_option_set_load_multiple(FALSE, array(
    'decision_name' => 'first_variation_set',
    'agent' => $agent_name,
  ), TRUE);
  $this
    ->assertTrue(count($option_sets) === 0);
}