Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\statistics\Tests\Views;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\views\Tests\ViewTestBase;
|
Chris@0
|
6 use Drupal\views\Tests\ViewTestData;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * Tests basic integration of views data from the statistics module.
|
Chris@0
|
10 *
|
Chris@0
|
11 * @group statistics
|
Chris@0
|
12 * @see
|
Chris@0
|
13 */
|
Chris@0
|
14 class IntegrationTest extends ViewTestBase {
|
Chris@0
|
15
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * Modules to enable.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @var array
|
Chris@0
|
21 */
|
Chris@0
|
22 public static $modules = ['statistics', 'statistics_test_views', 'node'];
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * Stores the user object that accesses the page.
|
Chris@0
|
26 *
|
Chris@0
|
27 * @var \Drupal\user\UserInterface
|
Chris@0
|
28 */
|
Chris@0
|
29 protected $webUser;
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * Stores the node object which is used by the test.
|
Chris@0
|
33 *
|
Chris@0
|
34 * @var \Drupal\node\Entity\Node
|
Chris@0
|
35 */
|
Chris@0
|
36 protected $node;
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * Views used by this test.
|
Chris@0
|
40 *
|
Chris@0
|
41 * @var array
|
Chris@0
|
42 */
|
Chris@0
|
43 public static $testViews = ['test_statistics_integration'];
|
Chris@0
|
44
|
Chris@0
|
45 protected function setUp() {
|
Chris@0
|
46 parent::setUp();
|
Chris@0
|
47
|
Chris@0
|
48 ViewTestData::createTestViews(get_class($this), ['statistics_test_views']);
|
Chris@0
|
49
|
Chris@0
|
50 // Create a new user for viewing nodes and statistics.
|
Chris@0
|
51 $this->webUser = $this->drupalCreateUser(['access content', 'view post access counter']);
|
Chris@0
|
52
|
Chris@0
|
53 // Create a new user for viewing nodes only.
|
Chris@0
|
54 $this->deniedUser = $this->drupalCreateUser(['access content']);
|
Chris@0
|
55
|
Chris@0
|
56 $this->drupalCreateContentType(['type' => 'page']);
|
Chris@0
|
57 $this->node = $this->drupalCreateNode(['type' => 'page']);
|
Chris@0
|
58
|
Chris@0
|
59 // Enable counting of content views.
|
Chris@0
|
60 $this->config('statistics.settings')
|
Chris@0
|
61 ->set('count_content_views', 1)
|
Chris@0
|
62 ->save();
|
Chris@0
|
63
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 /**
|
Chris@0
|
67 * Tests the integration of the {node_counter} table in views.
|
Chris@0
|
68 */
|
Chris@0
|
69 public function testNodeCounterIntegration() {
|
Chris@0
|
70 $this->drupalLogin($this->webUser);
|
Chris@0
|
71
|
Chris@0
|
72 $this->drupalGet('node/' . $this->node->id());
|
Chris@0
|
73 // Manually calling statistics.php, simulating ajax behavior.
|
Chris@0
|
74 // @see \Drupal\statistics\Tests\StatisticsLoggingTest::testLogging().
|
Chris@0
|
75 global $base_url;
|
Chris@0
|
76 $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
|
Chris@0
|
77 $client = \Drupal::httpClient();
|
Chris@0
|
78 $client->post($stats_path, ['form_params' => ['nid' => $this->node->id()]]);
|
Chris@0
|
79 $this->drupalGet('test_statistics_integration');
|
Chris@0
|
80
|
Chris@0
|
81 $expected = statistics_get($this->node->id());
|
Chris@0
|
82 // Convert the timestamp to year, to match the expected output of the date
|
Chris@0
|
83 // handler.
|
Chris@0
|
84 $expected['timestamp'] = date('Y', $expected['timestamp']);
|
Chris@0
|
85
|
Chris@0
|
86 foreach ($expected as $field => $value) {
|
Chris@0
|
87 $xpath = "//div[contains(@class, views-field-$field)]/span[@class = 'field-content']";
|
Chris@0
|
88 $this->assertFieldByXpath($xpath, $value, "The $field output matches the expected.");
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 $this->drupalLogout();
|
Chris@0
|
92 $this->drupalLogin($this->deniedUser);
|
Chris@0
|
93 $this->drupalGet('test_statistics_integration');
|
Chris@0
|
94 $this->assertResponse(200);
|
Chris@0
|
95
|
Chris@0
|
96 foreach ($expected as $field => $value) {
|
Chris@0
|
97 $xpath = "//div[contains(@class, views-field-$field)]/span[@class = 'field-content']";
|
Chris@0
|
98 $this->assertNoFieldByXpath($xpath, $value, "The $field output is not displayed.");
|
Chris@0
|
99 }
|
Chris@0
|
100
|
Chris@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 }
|