Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\statistics\Functional;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Tests\BrowserTestBase;
|
Chris@0
|
6 use Drupal\node\Entity\Node;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * Tests request logging for cached and uncached pages.
|
Chris@0
|
10 *
|
Chris@0
|
11 * We subclass WebTestBase rather than StatisticsTestBase, because we
|
Chris@0
|
12 * want to test requests from an anonymous user.
|
Chris@0
|
13 *
|
Chris@0
|
14 * @group statistics
|
Chris@0
|
15 */
|
Chris@0
|
16 class StatisticsLoggingTest extends BrowserTestBase {
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * Modules to enable.
|
Chris@0
|
20 *
|
Chris@0
|
21 * @var array
|
Chris@0
|
22 */
|
Chris@0
|
23 public static $modules = ['node', 'statistics', 'block', 'locale'];
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * User with permissions to create and edit pages.
|
Chris@0
|
27 *
|
Chris@0
|
28 * @var \Drupal\user\UserInterface
|
Chris@0
|
29 */
|
Chris@0
|
30 protected $authUser;
|
Chris@0
|
31
|
Chris@0
|
32 /**
|
Chris@0
|
33 * Associative array representing a hypothetical Drupal language.
|
Chris@0
|
34 *
|
Chris@0
|
35 * @var array
|
Chris@0
|
36 */
|
Chris@0
|
37 protected $language;
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@0
|
40 * The Guzzle HTTP client.
|
Chris@0
|
41 *
|
Chris@17
|
42 * @var \GuzzleHttp\Client
|
Chris@0
|
43 */
|
Chris@0
|
44 protected $client;
|
Chris@0
|
45
|
Chris@0
|
46 protected function setUp() {
|
Chris@0
|
47 parent::setUp();
|
Chris@0
|
48
|
Chris@0
|
49 // Create Basic page node type.
|
Chris@0
|
50 if ($this->profile != 'standard') {
|
Chris@0
|
51 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 $this->authUser = $this->drupalCreateUser([
|
Chris@0
|
55 // For node creation.
|
Chris@0
|
56 'access content',
|
Chris@0
|
57 'create page content',
|
Chris@0
|
58 'edit own page content',
|
Chris@0
|
59 // For language negotiation administration.
|
Chris@0
|
60 'administer languages',
|
Chris@0
|
61 'access administration pages',
|
Chris@0
|
62 ]);
|
Chris@0
|
63
|
Chris@0
|
64 // Ensure we have a node page to access.
|
Chris@0
|
65 $this->node = $this->drupalCreateNode(['title' => $this->randomMachineName(255), 'uid' => $this->authUser->id()]);
|
Chris@0
|
66
|
Chris@0
|
67 // Add a custom language and enable path-based language negotiation.
|
Chris@0
|
68 $this->drupalLogin($this->authUser);
|
Chris@0
|
69 $this->language = [
|
Chris@0
|
70 'predefined_langcode' => 'custom',
|
Chris@0
|
71 'langcode' => 'xx',
|
Chris@0
|
72 'label' => $this->randomMachineName(16),
|
Chris@0
|
73 'direction' => 'ltr',
|
Chris@0
|
74 ];
|
Chris@0
|
75 $this->drupalPostForm('admin/config/regional/language/add', $this->language, t('Add custom language'));
|
Chris@0
|
76 $this->drupalPostForm('admin/config/regional/language/detection', ['language_interface[enabled][language-url]' => 1], t('Save settings'));
|
Chris@0
|
77 $this->drupalLogout();
|
Chris@0
|
78
|
Chris@0
|
79 // Enable access logging.
|
Chris@0
|
80 $this->config('statistics.settings')
|
Chris@0
|
81 ->set('count_content_views', 1)
|
Chris@0
|
82 ->save();
|
Chris@0
|
83
|
Chris@0
|
84 // Clear the logs.
|
Chris@0
|
85 db_truncate('node_counter');
|
Chris@0
|
86 $this->client = \Drupal::httpClient();
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@0
|
89 /**
|
Chris@0
|
90 * Verifies node hit counter logging and script placement.
|
Chris@0
|
91 */
|
Chris@0
|
92 public function testLogging() {
|
Chris@0
|
93 $path = 'node/' . $this->node->id();
|
Chris@0
|
94 $module_path = drupal_get_path('module', 'statistics');
|
Chris@0
|
95 $stats_path = base_path() . $module_path . '/statistics.php';
|
Chris@0
|
96 $lib_path = base_path() . $module_path . '/statistics.js';
|
Chris@0
|
97 $expected_library = '/<script src=".*?' . preg_quote($lib_path, '/.') . '.*?">/is';
|
Chris@0
|
98
|
Chris@0
|
99 // Verify that logging scripts are not found on a non-node page.
|
Chris@0
|
100 $this->drupalGet('node');
|
Chris@0
|
101 $settings = $this->getDrupalSettings();
|
Chris@0
|
102 $this->assertNoPattern($expected_library, 'Statistics library JS not found on node page.');
|
Chris@0
|
103 $this->assertFalse(isset($settings['statistics']), 'Statistics settings not found on node page.');
|
Chris@0
|
104
|
Chris@0
|
105 // Verify that logging scripts are not found on a non-existent node page.
|
Chris@0
|
106 $this->drupalGet('node/9999');
|
Chris@0
|
107 $settings = $this->getDrupalSettings();
|
Chris@0
|
108 $this->assertNoPattern($expected_library, 'Statistics library JS not found on non-existent node page.');
|
Chris@0
|
109 $this->assertFalse(isset($settings['statistics']), 'Statistics settings not found on node page.');
|
Chris@0
|
110
|
Chris@0
|
111 // Verify that logging scripts are found on a valid node page.
|
Chris@0
|
112 $this->drupalGet($path);
|
Chris@0
|
113 $settings = $this->getDrupalSettings();
|
Chris@0
|
114 $this->assertPattern($expected_library, 'Found statistics library JS on node page.');
|
Chris@0
|
115 $this->assertIdentical($this->node->id(), $settings['statistics']['data']['nid'], 'Found statistics settings on node page.');
|
Chris@0
|
116
|
Chris@0
|
117 // Verify the same when loading the site in a non-default language.
|
Chris@0
|
118 $this->drupalGet($this->language['langcode'] . '/' . $path);
|
Chris@0
|
119 $settings = $this->getDrupalSettings();
|
Chris@0
|
120 $this->assertPattern($expected_library, 'Found statistics library JS on a valid node page in a non-default language.');
|
Chris@0
|
121 $this->assertIdentical($this->node->id(), $settings['statistics']['data']['nid'], 'Found statistics settings on valid node page in a non-default language.');
|
Chris@0
|
122
|
Chris@0
|
123 // Manually call statistics.php to simulate ajax data collection behavior.
|
Chris@0
|
124 global $base_root;
|
Chris@0
|
125 $post = ['nid' => $this->node->id()];
|
Chris@0
|
126 $this->client->post($base_root . $stats_path, ['form_params' => $post]);
|
Chris@0
|
127 $node_counter = statistics_get($this->node->id());
|
Chris@16
|
128 $this->assertIdentical($node_counter['totalcount'], 1);
|
Chris@0
|
129
|
Chris@0
|
130 // Try fetching statistics for an invalid node ID and verify it returns
|
Chris@0
|
131 // FALSE.
|
Chris@0
|
132 $node_id = 1000000;
|
Chris@0
|
133 $node = Node::load($node_id);
|
Chris@0
|
134 $this->assertNull($node);
|
Chris@0
|
135
|
Chris@0
|
136 // This is a test specifically for the deprecated statistics_get() function
|
Chris@0
|
137 // and so should remain unconverted until that function is removed.
|
Chris@0
|
138 $result = statistics_get($node_id);
|
Chris@0
|
139 $this->assertIdentical($result, FALSE);
|
Chris@0
|
140 }
|
Chris@0
|
141
|
Chris@0
|
142 }
|