You are here

public function StatisticsAdminTest::testStatisticsSettings in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/statistics/tests/src/Functional/StatisticsAdminTest.php \Drupal\Tests\statistics\Functional\StatisticsAdminTest::testStatisticsSettings()
  2. 10 core/modules/statistics/tests/src/Functional/StatisticsAdminTest.php \Drupal\Tests\statistics\Functional\StatisticsAdminTest::testStatisticsSettings()

Verifies that the statistics settings page works.

File

core/modules/statistics/tests/src/Functional/StatisticsAdminTest.php, line 74

Class

StatisticsAdminTest
Tests the statistics admin.

Namespace

Drupal\Tests\statistics\Functional

Code

public function testStatisticsSettings() {
  $config = $this
    ->config('statistics.settings');
  $this
    ->assertEmpty($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
    ->assertNotEmpty($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 = [
    'nid' => $nid,
  ];
  global $base_url;
  $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
  $this->client
    ->post($stats_path, [
    '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, [
    'form_params' => $post,
  ]);
  $this
    ->assertText('1 view', 'Node is viewed once.');
  $this
    ->drupalGet('node/' . $this->testNode
    ->id());
  $this->client
    ->post($stats_path, [
    '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, [
    'form_params' => $post,
  ]);
  $this
    ->drupalGet('node/' . $this->testNode
    ->id());
  $this
    ->assertText('3 views', 'Views counter was not updated.');
}