You are here

function StatisticsAdminTest::testStatisticsSettings in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/statistics/src/Tests/StatisticsAdminTest.php \Drupal\statistics\Tests\StatisticsAdminTest::testStatisticsSettings()

Verifies that the statistics settings page works.

File

core/modules/statistics/src/Tests/StatisticsAdminTest.php, line 67
Contains \Drupal\statistics\Tests\StatisticsAdminTest.

Class

StatisticsAdminTest
Tests the statistics admin.

Namespace

Drupal\statistics\Tests

Code

function testStatisticsSettings() {
  $config = $this
    ->config('statistics.settings');
  $this
    ->assertFalse($config
    ->get('count_content_views'), 'Count content view log is disabled by default.');

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

  // Hit the node.
  $this
    ->drupalGet('node/' . $this->testNode
    ->id());

  // Manually calling statistics.php, simulating ajax behavior.
  $nid = $this->testNode
    ->id();
  $post = array(
    'nid' => $nid,
  );
  global $base_url;
  $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
  $this->client
    ->post($stats_path, array(
    'form_params' => $post,
  ));

  // 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->testNode
    ->id());
  $this->client
    ->post($stats_path, array(
    'form_params' => $post,
  ));
  $this
    ->assertText('1 view', 'Node is viewed once.');
  $this
    ->drupalGet('node/' . $this->testNode
    ->id());
  $this->client
    ->post($stats_path, array(
    'form_params' => $post,
  ));
  $this
    ->assertText('2 views', 'Node is viewed 2 times.');

  // Increase the max age to test that nodes are no longer immediately
  // updated, visit the node once more to populate the cache.
  $this
    ->config('statistics.settings')
    ->set('display_max_age', 3600)
    ->save();
  $this
    ->drupalGet('node/' . $this->testNode
    ->id());
  $this
    ->assertText('3 views', 'Node is viewed 3 times.');
  $this->client
    ->post($stats_path, array(
    'form_params' => $post,
  ));
  $this
    ->drupalGet('node/' . $this->testNode
    ->id());
  $this
    ->assertText('3 views', 'Views counter was not updated.');
}