You are here

public function AcquiaLiftWebTestAgentAdmin::testSaveAgent in Acquia Lift Connector 7

File

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

Class

AcquiaLiftWebTestAgentAdmin

Code

public function testSaveAgent() {

  // Create a new agent via the UI.
  $agent = $this
    ->createTestAgent(array(
    'control_rate' => 10,
    'explore_rate' => 30,
  ));
  $agent_name = $agent
    ->getTitle();
  $machine_name = $agent
    ->getMachineName();
  $expected_queue_items = $option_set_queue_items = array();

  // Add some dummy option sets to this agent.
  $option_set_values = array(
    array(
      'agent' => $machine_name,
      'plugin' => 'type1',
      'num_options' => 3,
    ),
    array(
      'agent' => $machine_name,
      'plugin' => 'type2',
      'num_options' => 2,
    ),
  );
  foreach ($option_set_values as $i => $values) {
    list($option_set, $new_queues) = $this
      ->createOptionSet($i, $values);
    $expected_queue_items = array_merge($expected_queue_items, $new_queues);

    // We need to keep track of the option set items that get added to the
    // queue separately from the other items as we need them again later.
    // Dirty way to avoid 'saveAgent' method be included to $option_set_queue_items
    $option_set_queue_items = array_merge($option_set_queue_items, $new_queues);
    if ($i == 0) {
      $expected_queue_items[] = array(
        'method' => 'saveAgent',
        'args' => array(
          $machine_name,
          $agent_name,
          'adaptive',
          PERSONALIZE_STATUS_NOT_STARTED,
          0.1,
          0.3,
          1,
        ),
        'agent' => $machine_name,
      );
    }
  }
  $this
    ->assertQueueItems($expected_queue_items);
  $this->personalizedQueue
    ->deleteQueue();
  $expected_queue_items = array();

  // Save a goal for the agent.
  $goal_name = 'form_submit';
  personalize_goal_save($machine_name, $goal_name, 2);
  $expected_queue_items[] = array(
    'method' => 'saveGoal',
    'args' => array(
      $machine_name,
      $goal_name,
    ),
    'agent' => $machine_name,
  );
  $expected_queue_items[] = array(
    'method' => 'saveAgent',
    'args' => array(
      $machine_name,
      $agent_name,
      'adaptive',
      PERSONALIZE_STATUS_NOT_STARTED,
      0.1,
      0.3,
      1,
    ),
    'agent' => $machine_name,
  );
  $this
    ->assertQueueItems($expected_queue_items);
  $this->personalizedQueue
    ->deleteQueue();
  $expected_queue_items = array();
  $this
    ->drupalPost("admin/structure/personalize/manage/{$machine_name}/edit", array(), $this
    ->getButton('agent'));
  $expected_queue_items = array(
    array(
      'method' => 'saveAgent',
      'args' => array(
        $machine_name,
        $agent_name,
        'adaptive',
        PERSONALIZE_STATUS_NOT_STARTED,
        0.1,
        0.3,
        1,
      ),
      'agent' => $machine_name,
    ),
  );
  $this
    ->assertQueueItems($expected_queue_items);
  $this->personalizedQueue
    ->deleteQueue();
  $expected_queue_items = array();
  $this
    ->drupalPost("admin/structure/personalize/manage/{$machine_name}/edit", array(), $this
    ->getButton('agent'));

  // Now the only thing that should get added is an item for the agent
  // because neither goals nor option sets will have changed.
  $expected_queue_items[] = array(
    'method' => 'saveAgent',
    'args' => array(
      $machine_name,
      $agent_name,
      'adaptive',
      PERSONALIZE_STATUS_NOT_STARTED,
      0.1,
      0.3,
      1,
    ),
    'agent' => $machine_name,
  );
  $this
    ->assertQueueItems($expected_queue_items);
  $this->personalizedQueue
    ->deleteQueue();

  // Create an MVT and add the two option sets to it.
  $mvt_label = $this
    ->randomName();
  $mvt_machine_name = personalize_generate_machine_name($mvt_label, 'personalize_mvt_machine_name_exists');
  $edit = array(
    'mvt[add][mvt_basic_info][label]' => $mvt_machine_name,
    'mvt[add][mvt_basic_info][option_sets][]' => array(
      1,
      2,
    ),
  );
  $this
    ->drupalPost("admin/structure/personalize/manage/{$machine_name}/edit", $edit, $this
    ->getButton('mvt'));

  // The option sets will get added to the queue
  foreach ($option_set_queue_items as &$item) {

    // The second argument, which is the decision point name, will have
    // changed to the MVT name.
    $item['args'][1] = $mvt_machine_name;
  }
  $expected_queue_items = array_slice($option_set_queue_items, 0, 5);

  // @todo Commenting this out as for some reason after saving the new
  // point and decision/choices for osid-1, it saves the old point and
  // decision/choices for osid-2, before deleting the old osid-1 point,
  // saving the second decision and deleting the osid-2 point.

  //$this->assertQueueItems($expected_queue_items);
}