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