function StatisticsAdminTest::testStatisticsSettings

Verifies that the statistics settings page works.

File

drupal/core/modules/statistics/lib/Drupal/statistics/Tests/StatisticsAdminTest.php, line 63
Definition of Drupal\statistics\Tests\StatisticsAdminTest.

Class

StatisticsAdminTest
Tests the statistics administration screen.

Namespace

Drupal\statistics\Tests

Code

function testStatisticsSettings() {
  $config = config('statistics.settings');
  $this
    ->assertFalse($config
    ->get('access_log.enabled'), 'Access log is disabled by default.');
  $this
    ->assertFalse($config
    ->get('count_content_views'), 'Count content view log is disabled by default.');
  $this
    ->drupalGet('admin/reports/pages');
  $this
    ->assertRaw(t('No statistics available.'), 'Verifying text shown when no statistics is available.');

  // Enable access log and counter on content view.
  $edit['statistics_enable_access_log'] = 1;
  $edit['statistics_count_content_views'] = 1;
  $this
    ->drupalPost('admin/config/system/statistics', $edit, t('Save configuration'));
  $config = config('statistics.settings');
  $this
    ->assertTrue($config
    ->get('access_log.enabled'), 'Access log is enabled.');
  $this
    ->assertTrue($config
    ->get('count_content_views'), 'Count content view log is enabled.');

  // Hit the node.
  $this
    ->drupalGet('node/' . $this->test_node->nid);

  // Manually calling statistics.php, simulating ajax behavior.
  $nid = $this->test_node->nid;
  $post = http_build_query(array(
    'nid' => $nid,
  ));
  $headers = array(
    'Content-Type' => 'application/x-www-form-urlencoded',
  );
  global $base_url;
  $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
  drupal_http_request($stats_path, array(
    'method' => 'POST',
    'data' => $post,
    'headers' => $headers,
    'timeout' => 10000,
  ));
  $this
    ->drupalGet('admin/reports/pages');
  $this
    ->assertText('node/1', 'Test node found.');

  // Hit the node again (the counter is incremented after the hit, so
  // "1 view" will actually be shown when the node is hit the second time).
  $this
    ->drupalGet('node/' . $this->test_node->nid);
  drupal_http_request($stats_path, array(
    'method' => 'POST',
    'data' => $post,
    'headers' => $headers,
    'timeout' => 10000,
  ));
  $this
    ->assertText('1 view', 'Node is viewed once.');
  $this
    ->drupalGet('node/' . $this->test_node->nid);
  drupal_http_request($stats_path, array(
    'method' => 'POST',
    'data' => $post,
    'headers' => $headers,
    'timeout' => 10000,
  ));
  $this
    ->assertText('2 views', 'Node is viewed 2 times.');
}