function StatisticsAdminTest::testStatisticsSettings

Same name in other branches
  1. 8.9.x 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()
  3. 11.x 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 77

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');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.