Mercurial > hg > isophonics-drupal-site
comparison core/modules/statistics/tests/src/Functional/StatisticsLoggingTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | c2387f117808 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Tests\statistics\Functional; | |
4 | |
5 use Drupal\Tests\BrowserTestBase; | |
6 use Drupal\node\Entity\Node; | |
7 | |
8 /** | |
9 * Tests request logging for cached and uncached pages. | |
10 * | |
11 * We subclass WebTestBase rather than StatisticsTestBase, because we | |
12 * want to test requests from an anonymous user. | |
13 * | |
14 * @group statistics | |
15 */ | |
16 class StatisticsLoggingTest extends BrowserTestBase { | |
17 | |
18 /** | |
19 * Modules to enable. | |
20 * | |
21 * @var array | |
22 */ | |
23 public static $modules = ['node', 'statistics', 'block', 'locale']; | |
24 | |
25 /** | |
26 * User with permissions to create and edit pages. | |
27 * | |
28 * @var \Drupal\user\UserInterface | |
29 */ | |
30 protected $authUser; | |
31 | |
32 /** | |
33 * Associative array representing a hypothetical Drupal language. | |
34 * | |
35 * @var array | |
36 */ | |
37 protected $language; | |
38 | |
39 /** | |
40 * The Guzzle HTTP client. | |
41 * | |
42 * @var \GuzzleHttp\Client; | |
43 */ | |
44 protected $client; | |
45 | |
46 protected function setUp() { | |
47 parent::setUp(); | |
48 | |
49 // Create Basic page node type. | |
50 if ($this->profile != 'standard') { | |
51 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']); | |
52 } | |
53 | |
54 $this->authUser = $this->drupalCreateUser([ | |
55 // For node creation. | |
56 'access content', | |
57 'create page content', | |
58 'edit own page content', | |
59 // For language negotiation administration. | |
60 'administer languages', | |
61 'access administration pages', | |
62 ]); | |
63 | |
64 // Ensure we have a node page to access. | |
65 $this->node = $this->drupalCreateNode(['title' => $this->randomMachineName(255), 'uid' => $this->authUser->id()]); | |
66 | |
67 // Add a custom language and enable path-based language negotiation. | |
68 $this->drupalLogin($this->authUser); | |
69 $this->language = [ | |
70 'predefined_langcode' => 'custom', | |
71 'langcode' => 'xx', | |
72 'label' => $this->randomMachineName(16), | |
73 'direction' => 'ltr', | |
74 ]; | |
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(); | |
78 | |
79 // Enable access logging. | |
80 $this->config('statistics.settings') | |
81 ->set('count_content_views', 1) | |
82 ->save(); | |
83 | |
84 // Clear the logs. | |
85 db_truncate('node_counter'); | |
86 $this->client = \Drupal::httpClient(); | |
87 } | |
88 | |
89 /** | |
90 * Verifies node hit counter logging and script placement. | |
91 */ | |
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'; | |
98 | |
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.'); | |
104 | |
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.'); | |
110 | |
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.'); | |
116 | |
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.'); | |
122 | |
123 // Manually call statistics.php to simulate ajax data collection behavior. | |
124 global $base_root; | |
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'); | |
129 | |
130 // Try fetching statistics for an invalid node ID and verify it returns | |
131 // FALSE. | |
132 $node_id = 1000000; | |
133 $node = Node::load($node_id); | |
134 $this->assertNull($node); | |
135 | |
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); | |
140 } | |
141 | |
142 } |