You are here

public function AcquiaLiftWebTestReports::testReadReportsFromFile in Acquia Lift Connector 7

File

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

Class

AcquiaLiftWebTestReports

Code

public function testReadReportsFromFile() {
  $this
    ->drupalLogin($this->managerUser);
  $first_agent = 'new-test-agent';
  $second_agent = 'second-test-agent';
  $control_rate = 10;
  $explore_rate = 30;
  $this
    ->createTestAgent(array(
    'name' => $first_agent,
    'control_rate' => $control_rate,
    'explore_rate' => $explore_rate,
  ));

  // Our second agent will continu to read reports from the API when our first switches
  // to reading from a file.
  $this
    ->createTestAgent(array(
    'name' => $second_agent,
    'control_rate' => $control_rate,
    'explore_rate' => $explore_rate,
  ));
  $test_data = array();

  // Set each agent up with an option set with 2 options, and a goal, and set it to
  // running.
  $option_sets = array();
  foreach (array(
    $first_agent,
    $second_agent,
  ) as $i => $agent_name) {
    list($option_set, $new_queue_items) = $this
      ->createOptionSet($i, array(
      'agent' => $agent_name,
      'plugin' => 'type1',
      'option_ids' => array(
        'option-1',
        'option-2',
      ),
    ));
    $option_sets[] = $option_set;
    $osid = $option_set->osid;

    // Add a goal.
    personalize_goal_save($agent_name, 'new-goal', 1);

    // We need to bypass the personalize_agent_set_status() function because it
    // does the verification check, which would fail.
    variable_set(_personalize_agent_get_status_variable($agent_name), PERSONALIZE_STATUS_RUNNING);

    // Set the started time of the agent to now.
    $agent = personalize_agent_load($agent_name);
    $started = time();
    $agent->started = $started;
    personalize_agent_save($agent);
    $report_start_date = 1407499650;

    // Test to ensure that an initial report at this point returns no data.
    AcquiaLiftAPI::setTestInstance();
    $agent_instance = personalize_agent_load_agent($first_agent);
    $report = $agent_instance
      ->buildCampaignReports(array(
      'decision' => 'osid-' . $option_sets[0]->osid,
      'start' => $report_start_date,
    ));
    $this
      ->assertFalse($report['#has_data'], 'Report has no data.');

    // Add some test data so that calls to get reports for this agent will
    // return basic reports.
    $test_data = array_merge_recursive($test_data, array(
      // We need to replicate this test agent on the server so that the errors()
      // check will pass.
      'agents' => array(
        array(
          'code' => $agent_name,
        ),
      ),
      'points' => array(
        $agent_name => array(
          $osid,
        ),
      ),
      'decisions' => array(
        $agent_name => array(
          $osid => array(
            $osid,
          ),
        ),
      ),
      'choices' => array(
        $agent_name => array(
          $osid => array(
            $osid => array(
              'option-1',
              'option-2',
            ),
          ),
        ),
      ),
      'goals' => array(
        $agent_name => array(
          // This goal should get deleted upon sync.
          'new-goal',
        ),
      ),
      // Now add the reports.
      'reports' => array(
        $agent_name => array(
          'confidence' => AcquiaLiftTestReports::getBasicConfidenceReport($agent_name),
          'targeting-features' => AcquiaLiftTestReports::getBasicTargetingReport($agent_name),
          'learning' => array(),
          'agent-status' => AcquiaLiftTestReports::getBasicStatusReport($agent_name),
          'context-filters' => AcquiaLiftTestReports::getBasicContextFilters(),
        ),
      ),
    ));
  }
  variable_set('acquia_lift_web_test_data', $test_data);
  $this
    ->resetAll();
  AcquiaLiftAPI::setTestInstance();
  $agent_instance = personalize_agent_load_agent($first_agent);

  // We use the date that the test report was created.
  $report = $agent_instance
    ->buildCampaignReports(array(
    'decision' => 'osid-' . $option_sets[0]->osid,
    'start' => $report_start_date,
  ));
  $this
    ->assertFalse($report['#has_data'], 'Empty reports have no data to show.');

  // The experiment report should show that Option A has a 0% conversion rate.
  $this
    ->assertEqual("Option A", $report['experiment']['reports']['conversion']['reports']['summary']['summary_holder']['summary_table']['#rows'][0]['data'][0]['data']);
  $this
    ->assertEqual("Control", $report['experiment']['reports']['conversion']['reports']['summary']['summary_holder']['summary_table']['#rows'][0]['data'][0]['data-acquia-lift-variation-label']);
  $this
    ->assertEqual("0%", $report['experiment']['reports']['conversion']['reports']['summary']['summary_holder']['summary_table']['#rows'][0]['data'][2]['data']);

  // The report for the second agent will be identical
  $agent_instance = personalize_agent_load_agent($second_agent);
  $report = $agent_instance
    ->buildCampaignReports(array(
    'decision' => 'osid-' . $option_sets[0]->osid,
    'start' => $report_start_date,
  ));
  $this
    ->assertEqual("Option A", $report['experiment']['reports']['conversion']['reports']['summary']['summary_holder']['summary_table']['#rows'][0]['data'][0]['data']);
  $this
    ->assertEqual("Control", $report['experiment']['reports']['conversion']['reports']['summary']['summary_holder']['summary_table']['#rows'][0]['data'][0]['data-acquia-lift-variation-label']);
  $this
    ->assertEqual("0%", $report['experiment']['reports']['conversion']['reports']['summary']['summary_holder']['summary_table']['#rows'][0]['data'][2]['data']);
  $agent_instance = NULL;
  $this
    ->resetAll();

  // Now set the first agent to use a report from a file.
  $path = drupal_get_path('module', 'acquia_lift');
  variable_set("acquia_lift_report_source_{$first_agent}", "/" . $path . '/tests/test_reports.json');
  $agent_instance = personalize_agent_load_agent($first_agent);
  $report = $agent_instance
    ->buildCampaignReports(array(
    'decision' => 'osid-' . $option_sets[0]->osid,
    'start' => $report_start_date,
  ));
  $this
    ->assertTrue($report['#has_data'], 'Report data file indicates data to show.');

  // Now the experiment report should show that Option A has a conversion rate of .19%
  $this
    ->assertEqual("osid-76:Option A", $report['experiment']['reports']['conversion']['reports']['summary']['summary_holder']['summary_table']['#rows'][0]['data'][0]['data']);
  $this
    ->assertEqual("Control", $report['experiment']['reports']['conversion']['reports']['summary']['summary_holder']['summary_table']['#rows'][0]['data'][0]['data-acquia-lift-variation-label']);
  $this
    ->assertEqual("0.19%", $report['experiment']['reports']['conversion']['reports']['summary']['summary_holder']['summary_table']['#rows'][0]['data'][2]['data']);

  // The "Winner" flag should appear in the third option's last column, but
  // nowhere else.
  $this
    ->assertEqual('<span class="lift-winner">Winner</span>', $report['experiment']['reports']['conversion']['reports']['summary']['summary_holder']['summary_table']['#rows'][2]['data'][5]);
  foreach (array(
    0,
    1,
    3,
    4,
  ) as $variation_index) {
    $this
      ->assertEqual('', $report['experiment']['reports']['conversion']['reports']['summary']['summary_holder']['summary_table']['#rows'][$variation_index]['data'][5]);
  }

  // The report for the second agent should still show the basic report data.
  $agent_instance = personalize_agent_load_agent($second_agent);
  $report = $agent_instance
    ->buildCampaignReports(array(
    'decision' => 'osid-' . $option_sets[0]->osid,
    'start' => $report_start_date,
  ));
  $this
    ->assertFalse($report['#has_data'], 'Empty report data still indicates no data to show.');
  $this
    ->assertEqual("Option A", $report['experiment']['reports']['conversion']['reports']['summary']['summary_holder']['summary_table']['#rows'][0]['data'][0]['data']);
  $this
    ->assertEqual("Control", $report['experiment']['reports']['conversion']['reports']['summary']['summary_holder']['summary_table']['#rows'][0]['data'][0]['data-acquia-lift-variation-label']);
  $this
    ->assertEqual("0%", $report['experiment']['reports']['conversion']['reports']['summary']['summary_holder']['summary_table']['#rows'][0]['data'][2]['data']);
}