You are here

public function AcquiaLiftWebTestAgentAdmin::testSyncOptionSets in Acquia Lift Connector 7

Tests syncing of Option Set information to Acquia Lift.

File

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

Class

AcquiaLiftWebTestAgentAdmin

Code

public function testSyncOptionSets() {
  $agent = $this
    ->createTestAgent();

  // Create a couple of user profile fields for targeting.
  $user_profile_field_1 = $this
    ->createUserProfileField();
  $user_profile_field_2 = $this
    ->createUserProfileField();

  // Include _personalize_agent_from_form_values() function to build agent data.
  module_load_include('inc', 'personalize', 'personalize.admin');
  $agentStructure = _personalize_agent_from_form_values(array(
    'machine_name' => $agent
      ->getMachineName(),
    'title' => $agent
      ->getTitle(),
    'agent_type' => $agent
      ->getType(),
    'data' => $agent
      ->getData(),
  ));

  // Add the user profile fields as context.
  $agentStructure->data['visitor_context'] = array(
    'user_profile_context' => array(
      str_replace('field_', '', $user_profile_field_1['field_name']) => str_replace('field_', '', $user_profile_field_1['field_name']),
      str_replace('field_', '', $user_profile_field_2['field_name']) => str_replace('field_', '', $user_profile_field_2['field_name']),
    ),
  );
  personalize_agent_save($agentStructure);
  $agent = personalize_agent_load_agent($agent
    ->getMachineName(), TRUE);
  $agentData = $agent
    ->getData();
  $agent_queue_item = array(
    'method' => 'saveAgent',
    'args' => array(
      $agent
        ->getMachineName(),
      $agent
        ->getTitle(),
      $agentData['decision_style'],
      PERSONALIZE_STATUS_NOT_STARTED,
      $agentData['control_rate'] / 100,
      $agentData['explore_rate'] / 100,
      isset($agentData['cache_decisions']) && $agentData['cache_decisions'],
    ),
    'agent' => $agent
      ->getMachineName(),
  );
  $expected_queues = array(
    $agent_queue_item,
  );
  $this
    ->assertQueueItems($expected_queues);
  $this->personalizedQueue
    ->deleteQueue();
  $expected_queue_items = array();

  // Now add an option set to the agent.
  $personalized_blocks_form_state = array(
    'values' => array(
      'agent_select' => $agent
        ->getMachineName(),
      'title' => $this
        ->randomName(),
      'blocks' => array(
        array(
          'option_label' => 'Option A',
          'option_id' => 'option-A',
          'weight' => 0,
          'block' => array(
            'bid' => 'comment_delta_recent',
          ),
        ),
        array(
          'option_label' => 'Option B',
          'option_id' => 'option-B',
          'weight' => 1,
          'block' => array(
            'bid' => 'system_delta_main',
          ),
        ),
        array(
          'option_label' => 'Option C',
          'option_id' => 'option-C',
          'weight' => 2,
          'block' => array(
            'bid' => 'system_delta_help',
          ),
        ),
      ),
    ),
  );
  personalize_option_set_save(_personalize_blocks_convert_form_to_personalized_block($personalized_blocks_form_state));
  $option_sets = personalize_option_set_load_by_agent($agent
    ->getMachineName(), TRUE);
  $osid = key($option_sets);
  $option_set = $option_sets[$osid];
  $point_name = personalize_get_decision_point_name_for_option_set($option_set);
  $decision_name = personalize_get_decision_name_for_option_set($option_set);
  $agent = personalize_agent_load_agent($agent
    ->getMachineName(), TRUE);
  $agentData = $agent
    ->getData();
  $this
    ->assertTrue(isset($agentData['decisions']) && isset($agentData['decisions'][$osid]));
  $expected_queue_items['point'] = array(
    'method' => 'savePoint',
    'args' => array(
      $agent
        ->getMachineName(),
      $point_name,
    ),
    'agent' => $agent
      ->getMachineName(),
  );
  $expected_queue_items['decision'] = array(
    'method' => 'saveDecision',
    'args' => array(
      $agent
        ->getMachineName(),
      $point_name,
      $decision_name,
    ),
    'agent' => $agent
      ->getMachineName(),
  );
  foreach ($option_set->options as $key => $option) {
    $expected_queue_items[$option['option_id']] = array(
      'method' => 'saveChoice',
      'args' => array(
        $agent
          ->getMachineName(),
        $point_name,
        $decision_name,
        personalize_generate_option_id($key),
      ),
      'agent' => $agent
        ->getMachineName(),
    );
  }
  $expected_queue_items['agent'] = $agent_queue_item;
  $this
    ->assertQueueItems(array_values($expected_queue_items));
  $this->personalizedQueue
    ->deleteQueue();

  // Set up fixed targeting on Option A with two features OR'd together.
  $context_1 = str_replace('field_', '', $user_profile_field_1['field_name']);
  $context_2 = str_replace('field_', '', $user_profile_field_2['field_name']);

  // We can't use the form to add multiple fixed targeting contexts because we can't
  // make simpletest use the "Add context" button, so we send our form values directly
  // to the submit function.
  module_load_include('inc', 'personalize', 'personalize.admin');
  $form_state = array(
    'values' => array(
      'agent' => $agent
        ->getMachineName(),
      'option_sets' => array(
        'option_set_1' => array(
          'winner' => 'option-A',
          'advanced' => array(
            'label' => $personalized_blocks_form_state['values']['title'],
            'stateful' => 0,
          ),
          'options' => array(
            'option-A' => array(
              'explicit_targeting' => array(
                'mapping' => array(
                  'contexts' => array(
                    array(
                      'context' => 'user_profile_context' . PERSONALIZE_TARGETING_ADMIN_SEPARATOR . $context_1,
                      'value' => array(
                        'match' => 'some value',
                        'operator' => 'equals',
                      ),
                    ),
                    array(
                      'context' => 'user_profile_context' . PERSONALIZE_TARGETING_ADMIN_SEPARATOR . $context_2,
                      'value' => array(
                        'match' => 'some other value',
                        'operator' => 'contains',
                      ),
                    ),
                  ),
                ),
                'strategy' => 'OR',
              ),
            ),
          ),
        ),
      ),
    ),
  );
  personalize_agent_option_sets_form_submit(array(), $form_state);
  $feature_1 = $context_1 . '::some-value';
  $feature_2 = $context_2 . '::sc-some-other-value';
  $expected_queue_items['targeting'] = array(
    'method' => 'saveFixedTargetingMapping',
    'args' => array(
      $agent
        ->getMachineName(),
      $point_name,
      array(
        array(
          'feature' => $feature_1,
          'decision' => $decision_name . ':option-A',
        ),
        array(
          'feature' => $feature_2,
          'decision' => $decision_name . ':option-A',
        ),
      ),
    ),
    'agent' => $agent
      ->getMachineName(),
  );

  // Because the decision structure has not changed only the agent and targeting
  // should be sync'd.
  $this
    ->assertQueueItems(array(
    $expected_queue_items['agent'],
    $expected_queue_items['targeting'],
  ));
  $this->personalizedQueue
    ->deleteQueue();

  // Change the fixed targeting strategy to AND the features together.
  $edit = array(
    'option_sets[option_set_1][options][option-A][explicit_targeting][strategy]' => 'AND',
  );
  $this
    ->drupalPost('admin/structure/personalize/manage/' . $agent
    ->getMachineName() . '/edit', $edit, $this
    ->getButton('option'));

  // Now the fixed targeting mapping should have a single mapping with
  // comma-separated features.
  $expected_queue_items['targeting']['args'][2] = array(
    array(
      'feature' => $feature_1 . ',' . $feature_2,
      'decision' => $decision_name . ':option-A',
    ),
  );
  $this
    ->assertQueueItems(array(
    $expected_queue_items['agent'],
    $expected_queue_items['targeting'],
  ));
  $this->personalizedQueue
    ->deleteQueue();

  // Remove Option B
  $this
    ->drupalGet('admin/structure/personalize/variations/personalize-blocks/manage/' . $osid . '/edit');
  $this
    ->drupalPost(NULL, array(), 'remove_1');
  $this
    ->drupalPost(NULL, array(), t('Save'));
  $expected_queue_items['delete-B'] = array(
    'method' => 'deleteChoice',
    'args' => array(
      $agent
        ->getMachineName(),
      $point_name,
      $decision_name,
      'option-B',
    ),
    'agent' => $agent
      ->getMachineName(),
  );
  $new_queue_items = array();
  $expected_order = array(
    'point',
    'decision',
    'option-A',
    'option-C',
    'delete-B',
    'agent',
    'targeting',
  );
  foreach ($expected_order as $key) {
    $new_queue_items[$key] = $expected_queue_items[$key];
  }
  $this
    ->assertQueueItems(array_values($new_queue_items));
  $this->personalizedQueue
    ->deleteQueue();
}