Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / statistics / tests / src / Functional / StatisticsReportsTest.php
1 <?php
2
3 namespace Drupal\Tests\statistics\Functional;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
7
8 /**
9  * Tests display of statistics report blocks.
10  *
11  * @group statistics
12  */
13 class StatisticsReportsTest extends StatisticsTestBase {
14
15   use AssertPageCacheContextsAndTagsTrait;
16
17   /**
18    * Tests the "popular content" block.
19    */
20   public function testPopularContentBlock() {
21     // Clear the block cache to load the Statistics module's block definitions.
22     $this->container->get('plugin.manager.block')->clearCachedDefinitions();
23
24     // Visit a node to have something show up in the block.
25     $node = $this->drupalCreateNode(['type' => 'page', 'uid' => $this->blockingUser->id()]);
26     $this->drupalGet('node/' . $node->id());
27     // Manually calling statistics.php, simulating ajax behavior.
28     $nid = $node->id();
29     $post = http_build_query(['nid' => $nid]);
30     $headers = ['Content-Type' => 'application/x-www-form-urlencoded'];
31     global $base_url;
32     $stats_path = $base_url . '/' . drupal_get_path('module', 'statistics') . '/statistics.php';
33     $client = \Drupal::httpClient();
34     $client->post($stats_path, ['headers' => $headers, 'body' => $post]);
35
36     // Configure and save the block.
37     $block = $this->drupalPlaceBlock('statistics_popular_block', [
38       'label' => 'Popular content',
39       'top_day_num' => 3,
40       'top_all_num' => 3,
41       'top_last_num' => 3,
42     ]);
43
44     // Get some page and check if the block is displayed.
45     $this->drupalGet('user');
46     $this->assertText('Popular content', 'Found the popular content block.');
47     $this->assertText("Today's", "Found today's popular content.");
48     $this->assertText('All time', 'Found the all time popular content.');
49     $this->assertText('Last viewed', 'Found the last viewed popular content.');
50
51     $tags = Cache::mergeTags($node->getCacheTags(), $block->getCacheTags());
52     $tags = Cache::mergeTags($tags, $this->blockingUser->getCacheTags());
53     $tags = Cache::mergeTags($tags, ['block_view', 'config:block_list', 'node_list', 'rendered', 'user_view']);
54     $this->assertCacheTags($tags);
55     $contexts = Cache::mergeContexts($node->getCacheContexts(), $block->getCacheContexts());
56     $contexts = Cache::mergeContexts($contexts, ['url.query_args:_wrapper_format']);
57     $this->assertCacheContexts($contexts);
58
59     // Check if the node link is displayed.
60     $this->assertRaw(\Drupal::l($node->label(), $node->urlInfo('canonical')), 'Found link to visited node.');
61   }
62
63 }