3 namespace Drupal\Tests\statistics\Functional;
5 use Drupal\Tests\BrowserTestBase;
6 use Drupal\node\Entity\Node;
9 * Tests request logging for cached and uncached pages.
11 * We subclass WebTestBase rather than StatisticsTestBase, because we
12 * want to test requests from an anonymous user.
16 class StatisticsLoggingTest extends BrowserTestBase {
23 public static $modules = ['node', 'statistics', 'block', 'locale'];
26 * User with permissions to create and edit pages.
28 * @var \Drupal\user\UserInterface
33 * Associative array representing a hypothetical Drupal language.
40 * The Guzzle HTTP client.
42 * @var \GuzzleHttp\Client;
46 protected function setUp() {
49 // Create Basic page node type.
50 if ($this->profile != 'standard') {
51 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
54 $this->authUser = $this->drupalCreateUser([
57 'create page content',
58 'edit own page content',
59 // For language negotiation administration.
60 'administer languages',
61 'access administration pages',
64 // Ensure we have a node page to access.
65 $this->node = $this->drupalCreateNode(['title' => $this->randomMachineName(255), 'uid' => $this->authUser->id()]);
67 // Add a custom language and enable path-based language negotiation.
68 $this->drupalLogin($this->authUser);
70 'predefined_langcode' => 'custom',
72 'label' => $this->randomMachineName(16),
75 $this->drupalPostForm('admin/config/regional/language/add', $this->language, t('Add custom language'));
76 $this->drupalPostForm('admin/config/regional/language/detection', ['language_interface[enabled][language-url]' => 1], t('Save settings'));
77 $this->drupalLogout();
79 // Enable access logging.
80 $this->config('statistics.settings')
81 ->set('count_content_views', 1)
85 db_truncate('node_counter');
86 $this->client = \Drupal::httpClient();
90 * Verifies node hit counter logging and script placement.
92 public function testLogging() {
93 $path = 'node/' . $this->node->id();
94 $module_path = drupal_get_path('module', 'statistics');
95 $stats_path = base_path() . $module_path . '/statistics.php';
96 $lib_path = base_path() . $module_path . '/statistics.js';
97 $expected_library = '/<script src=".*?' . preg_quote($lib_path, '/.') . '.*?">/is';
99 // Verify that logging scripts are not found on a non-node page.
100 $this->drupalGet('node');
101 $settings = $this->getDrupalSettings();
102 $this->assertNoPattern($expected_library, 'Statistics library JS not found on node page.');
103 $this->assertFalse(isset($settings['statistics']), 'Statistics settings not found on node page.');
105 // Verify that logging scripts are not found on a non-existent node page.
106 $this->drupalGet('node/9999');
107 $settings = $this->getDrupalSettings();
108 $this->assertNoPattern($expected_library, 'Statistics library JS not found on non-existent node page.');
109 $this->assertFalse(isset($settings['statistics']), 'Statistics settings not found on node page.');
111 // Verify that logging scripts are found on a valid node page.
112 $this->drupalGet($path);
113 $settings = $this->getDrupalSettings();
114 $this->assertPattern($expected_library, 'Found statistics library JS on node page.');
115 $this->assertIdentical($this->node->id(), $settings['statistics']['data']['nid'], 'Found statistics settings on node page.');
117 // Verify the same when loading the site in a non-default language.
118 $this->drupalGet($this->language['langcode'] . '/' . $path);
119 $settings = $this->getDrupalSettings();
120 $this->assertPattern($expected_library, 'Found statistics library JS on a valid node page in a non-default language.');
121 $this->assertIdentical($this->node->id(), $settings['statistics']['data']['nid'], 'Found statistics settings on valid node page in a non-default language.');
123 // Manually call statistics.php to simulate ajax data collection behavior.
125 $post = ['nid' => $this->node->id()];
126 $this->client->post($base_root . $stats_path, ['form_params' => $post]);
127 $node_counter = statistics_get($this->node->id());
128 $this->assertIdentical($node_counter['totalcount'], '1');
130 // Try fetching statistics for an invalid node ID and verify it returns
133 $node = Node::load($node_id);
134 $this->assertNull($node);
136 // This is a test specifically for the deprecated statistics_get() function
137 // and so should remain unconverted until that function is removed.
138 $result = statistics_get($node_id);
139 $this->assertIdentical($result, FALSE);