Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / statistics / tests / src / FunctionalJavascript / StatisticsLoggingTest.php
1 <?php
2
3 namespace Drupal\Tests\statistics\FunctionalJavascript;
4
5 use Drupal\Core\Session\AccountInterface;
6 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
7 use Drupal\language\Entity\ConfigurableLanguage;
8 use Drupal\user\Entity\Role;
9
10 /**
11  * Tests that statistics works.
12  *
13  * @group system
14  */
15 class StatisticsLoggingTest extends WebDriverTestBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   public static $modules = ['node', 'statistics', 'language'];
21
22   /**
23    * Node for tests.
24    *
25    * @var \Drupal\node\Entity\Node
26    */
27   protected $node;
28
29   /**
30    * {@inheritdoc}
31    */
32   protected function setUp() {
33     parent::setUp();
34
35     $this->config('statistics.settings')
36       ->set('count_content_views', 1)
37       ->save();
38
39     Role::load(AccountInterface::ANONYMOUS_ROLE)
40       ->grantPermission('view post access counter')
41       ->save();
42
43     // Add another language to enable multilingual path processor.
44     ConfigurableLanguage::create(['id' => 'xx'])->save();
45     $this->config('language.negotiation')->set('url.prefixes.en', 'en')->save();
46
47     $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
48     $this->node = $this->drupalCreateNode();
49   }
50
51   /**
52    * Tests that statistics works with different addressing variants.
53    */
54   public function testLoggingPage() {
55     // At the first request, the page does not contain statistics counter.
56     $this->assertNull($this->getStatisticsCounter('node/1'));
57     $this->assertSame(1, $this->getStatisticsCounter('node/1'));
58     $this->assertSame(2, $this->getStatisticsCounter('en/node/1'));
59     $this->assertSame(3, $this->getStatisticsCounter('en/node/1'));
60     $this->assertSame(4, $this->getStatisticsCounter('index.php/node/1'));
61     $this->assertSame(5, $this->getStatisticsCounter('index.php/node/1'));
62     $this->assertSame(6, $this->getStatisticsCounter('index.php/en/node/1'));
63     $this->assertSame(7, $this->getStatisticsCounter('index.php/en/node/1'));
64   }
65
66   /**
67    * Gets counter of views by path.
68    *
69    * @param string $path
70    *   A path to node.
71    *
72    * @return int|null
73    *   A counter of views. Returns NULL if the page does not contain statistics.
74    */
75   protected function getStatisticsCounter($path) {
76     $this->drupalGet($path);
77     // Wait while statistics module send ajax request.
78     $this->assertSession()->assertWaitOnAjaxRequest();
79     // Resaving the node to call the hook_node_links_alter(), which is used to
80     // update information on the page. See statistics_node_links_alter().
81     $this->node->save();
82
83     $field_counter = $this->getSession()->getPage()->find('css', '.statistics-counter');
84     return $field_counter ? (int) explode(' ', $field_counter->getText())[0] : NULL;
85   }
86
87 }