3 namespace Drupal\Tests\statistics\Functional;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
9 * Tests display of statistics report blocks.
13 class StatisticsReportsTest extends StatisticsTestBase {
15 use AssertPageCacheContextsAndTagsTrait;
18 * Tests the "popular content" block.
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();
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.
29 $post = http_build_query(['nid' => $nid]);
30 $headers = ['Content-Type' => 'application/x-www-form-urlencoded'];
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]);
36 // Configure and save the block.
37 $block = $this->drupalPlaceBlock('statistics_popular_block', [
38 'label' => 'Popular content',
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.');
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);
59 // Check if the node link is displayed.
60 $this->assertRaw(\Drupal::l($node->label(), $node->urlInfo('canonical')), 'Found link to visited node.');