You are here

public function AcquiaLiftWebTestAgentAdmin::testSyncGoalsFromVisitorUI in Acquia Lift Connector 7

File

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

Class

AcquiaLiftWebTestAgentAdmin

Code

public function testSyncGoalsFromVisitorUI() {

  // include visitor_actions_action_name_exists() function to validate action machine name
  module_load_include('inc', 'visitor_actions', 'visitor_actions.admin');
  $agent = $this
    ->createTestAgent();
  $actionTitle = $this
    ->randomName();
  $actionMachineName = personalize_generate_machine_name($actionTitle, 'visitor_actions_action_name_exists');

  // Create new Visitor Action but WITHOUT connection to active agent
  $edit = array(
    'title' => $actionTitle,
    'machine_name' => $actionMachineName,
    'actionable_element' => 'form',
    'identifier[form]' => 'some_form_id',
    'event[form]' => 'server::submit_server',
    'personalize_goal' => FALSE,
    'personalize_goal_value' => 50,
  );
  $this
    ->drupalPost('admin/structure/visitor_actions/add', $edit, $this
    ->getButton());

  // Without connection to the agent we should run any acquia_lift' syncing
  // @see personalize_visitor_action_form_submit()
  $expected_queues = array();
  $this
    ->assertQueueItems($expected_queues);

  // Try to create new with connection to the agent
  $actionTitle = $this
    ->randomName();
  $actionMachineName = personalize_generate_machine_name($actionTitle, 'visitor_actions_action_name_exists');

  // Create new Visitor Action but WITH connection to active agent
  $edit = array(
    'title' => $actionTitle,
    'machine_name' => $actionMachineName,
    'actionable_element' => 'form',
    'identifier[form]' => 'some_form_id',
    'event[form]' => 'server::submit_server',
    'personalize_goal' => TRUE,
    'personalize_goal_value' => 50,
  );
  $this
    ->drupalPost('admin/structure/visitor_actions/add', $edit, $this
    ->getButton());

  // With connection to the agent we should run AcquiaLiftAgent->syncGoals
  $expected_queues = array(
    array(
      'method' => 'saveGoal',
      'args' => array(
        $agent
          ->getMachineName(),
        $actionMachineName,
      ),
      'agent' => $agent
        ->getMachineName(),
    ),
    array(
      'method' => 'saveAgent',
      'args' => array(
        $agent
          ->getMachineName(),
        $agent
          ->getTitle(),
        'adaptive',
        PERSONALIZE_STATUS_NOT_STARTED,
        0.1,
        0.2,
        1,
      ),
      'agent' => $agent
        ->getMachineName(),
    ),
  );
  $this
    ->assertQueueItems($expected_queues);
  $this->personalizedQueue
    ->deleteQueue();

  // Remove machine name from post because it's not available as a field in the edit form.
  unset($edit['machine_name']);

  // Try to modify with same goal value to check that anything won't be synced
  $this
    ->drupalPost('admin/structure/visitor_actions/manage/' . $actionMachineName . '/edit', $edit, $this
    ->getButton());

  // With connection to the agent but with old goal value we should not run AcquiaLiftAgent->syncGoals or saveAgent
  $expected_queues = array();
  $this
    ->assertQueueItems($expected_queues);
  $this->personalizedQueue
    ->deleteQueue();

  // If goal value is changed syncGoals and saveAgent should be invoked
  $edit['personalize_goal_value'] += 10;
  $this
    ->drupalPost('admin/structure/visitor_actions/manage/' . $actionMachineName . '/edit', $edit, $this
    ->getButton());

  // With connection to the agent but with old goal value we should not run AcquiaLiftAgent->syncGoals or saveAgent
  $expected_queues = array(
    array(
      'method' => 'saveGoal',
      'args' => array(
        $agent
          ->getMachineName(),
        $actionMachineName,
      ),
      'agent' => $agent
        ->getMachineName(),
    ),
    array(
      'method' => 'saveAgent',
      'args' => array(
        $agent
          ->getMachineName(),
        $agent
          ->getTitle(),
        'adaptive',
        PERSONALIZE_STATUS_NOT_STARTED,
        0.1,
        0.2,
        1,
      ),
      'agent' => $agent
        ->getMachineName(),
    ),
  );
  $this
    ->assertQueueItems($expected_queues);
  $this->personalizedQueue
    ->deleteQueue();
  $this
    ->resetAll();

  // Verify that agent has last visitor action data
  $agentGoals = personalize_goal_load_by_conditions(array(
    'agent' => $agent
      ->getMachineName(),
  ));
  $firstGoal = reset($agentGoals);
  $this
    ->assertEqual($firstGoal->action, $actionMachineName);
  $this
    ->assertEqual($firstGoal->value, $edit['personalize_goal_value']);

  // Try to delete goal that connected to Agent from Visitor Action UI
  $this
    ->drupalPost('admin/structure/visitor_actions/manage/' . $actionMachineName . '/delete', array(), t('Delete'));
  $expected_queues = array(
    array(
      'method' => 'deleteGoal',
      'args' => array(
        $agent
          ->getMachineName(),
        $actionMachineName,
      ),
      'agent' => $agent
        ->getMachineName(),
    ),
    array(
      'method' => 'saveAgent',
      'args' => array(
        $agent
          ->getMachineName(),
        $agent
          ->getTitle(),
        'adaptive',
        PERSONALIZE_STATUS_NOT_STARTED,
        0.1,
        0.2,
        1,
      ),
      'agent' => $agent
        ->getMachineName(),
    ),
  );
  $this
    ->assertQueueItems($expected_queues);
  $this->personalizedQueue
    ->deleteQueue();
  $this
    ->resetAll();

  // No goal should be attached after deletion
  $agentGoals = personalize_goal_load_by_conditions(array(
    'agent' => $agent
      ->getMachineName(),
  ));
  $this
    ->assertEqual($agentGoals, array());
}