You are here

public function StatisticsAdminTest::testStatisticsSettings in Drupal 9

Same name and namespace in other branches
  1. 8 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
    ->drupalGet('admin/config/system/statistics');
  $this
    ->submitForm($edit, '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 . '/' . $this
    ->getModulePath('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
    ->assertSession()
    ->pageTextContains('1 view');
  $this
    ->drupalGet('node/' . $this->testNode
    ->id());
  $this->client
    ->post($stats_path, [
    'form_params' => $post,
  ]);
  $this
    ->assertSession()
    ->pageTextContains('2 views');

  // 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
    ->assertSession()
    ->pageTextContains('3 views');
  $this->client
    ->post($stats_path, [
    'form_params' => $post,
  ]);
  $this
    ->drupalGet('node/' . $this->testNode
    ->id());

  // Verify that views counter was not updated.
  $this
    ->assertSession()
    ->pageTextContains('3 views');
}