View source
<?php
namespace Drupal\Tests\node\Functional\Views;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Url;
use Drupal\node\Entity\Node;
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\views\Tests\AssertViewsCacheTagsTrait;
use Drupal\views\ViewExecutable;
use Drupal\views\Views;
class FrontPageTest extends ViewTestBase {
use AssertViewsCacheTagsTrait;
protected $defaultTheme = 'classy';
protected $dumpHeaders = TRUE;
protected $nodeStorage;
protected static $modules = [
'node',
'contextual',
];
protected function setUp($import_test_views = TRUE) : void {
parent::setUp($import_test_views);
$this->nodeStorage = $this->container
->get('entity_type.manager')
->getStorage('node');
}
public function testFrontPage() {
$site_name = $this
->randomMachineName();
$this
->config('system.site')
->set('name', $site_name)
->save();
$view = Views::getView('frontpage');
$expected = [
'config' => [
'core.entity_view_mode.node.rss',
'core.entity_view_mode.node.teaser',
],
'module' => [
'node',
'user',
],
];
$this
->assertSame($expected, $view
->getDependencies());
$view
->setDisplay('page_1');
$this
->executeView($view);
$view
->preview();
$this
->assertEquals(new FormattableMarkup('Welcome to @site_name', [
'@site_name' => $site_name,
]), $view
->getTitle(), 'The welcome title is used for the empty view.');
$view
->destroy();
$expected = [];
for ($i = 0; $i < 20; $i++) {
$values = [];
$values['type'] = 'article';
$values['title'] = $this
->randomMachineName();
$values['promote'] = TRUE;
$values['status'] = TRUE;
$values['created'] = REQUEST_TIME - $i;
if ($i == 5) {
$values['sticky'] = TRUE;
$node = $this->nodeStorage
->create($values);
$node
->save();
array_unshift($expected, [
'nid' => $node
->id(),
]);
}
else {
$values['sticky'] = FALSE;
$node = $this->nodeStorage
->create($values);
$node
->save();
array_push($expected, [
'nid' => $node
->id(),
]);
}
}
$not_expected_nids = [];
$values = [];
$values['type'] = 'article';
$values['title'] = $this
->randomMachineName();
$values['status'] = TRUE;
$values['promote'] = FALSE;
$node = $this->nodeStorage
->create($values);
$node
->save();
$not_expected_nids[] = $node
->id();
$values['promote'] = TRUE;
$values['status'] = FALSE;
$values['title'] = $this
->randomMachineName();
$node = $this->nodeStorage
->create($values);
$node
->save();
$not_expected_nids[] = $node
->id();
$values['promote'] = TRUE;
$values['sticky'] = TRUE;
$values['status'] = FALSE;
$values['title'] = $this
->randomMachineName();
$node = $this->nodeStorage
->create($values);
$node
->save();
$not_expected_nids[] = $node
->id();
$column_map = [
'nid' => 'nid',
];
$view
->setDisplay('page_1');
$this
->executeView($view);
$this
->assertIdenticalResultset($view, array_slice($expected, 0, 10), $column_map, 'Ensure that the right nodes are displayed on the frontpage.');
$this
->assertNotInResultSet($view, $not_expected_nids, 'Ensure no unexpected node is in the result.');
$view
->destroy();
$view
->setDisplay('page_1');
$view
->setCurrentPage(1);
$this
->executeView($view);
$this
->assertIdenticalResultset($view, array_slice($expected, 10, 10), $column_map, 'Ensure that the right nodes are displayed on second page of the frontpage.');
$this
->assertNotInResultSet($view, $not_expected_nids, 'Ensure no unexpected node is in the result.');
$view
->destroy();
}
protected function assertNotInResultSet(ViewExecutable $view, array $not_expected_nids, $message = '') {
$found_nids = array_filter($view->result, function ($row) use ($not_expected_nids) {
return in_array($row->nid, $not_expected_nids);
});
$this
->assertEmpty($found_nids, $message);
}
public function testAdminFrontPage() {
\Drupal::service('module_installer')
->install([
'views_ui',
]);
$this
->drupalLogin($this->rootUser);
$this
->drupalGet('node');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->responseMatches('/class=".+view-frontpage/');
}
public function testCacheTagsWithCachePluginNone() {
$this
->enablePageCaching();
$this
->doTestFrontPageViewCacheTags(FALSE);
}
public function testCacheTagsWithCachePluginTag() {
$this
->enablePageCaching();
$view = Views::getView('frontpage');
$view
->setDisplay('page_1');
$view->display_handler
->overrideOption('cache', [
'type' => 'tag',
]);
$view
->save();
$this
->doTestFrontPageViewCacheTags(TRUE);
}
public function testCacheTagsWithCachePluginTime() {
$this
->enablePageCaching();
$view = Views::getView('frontpage');
$view
->setDisplay('page_1');
$view->display_handler
->overrideOption('cache', [
'type' => 'time',
'options' => [
'results_lifespan' => 3600,
'output_lifespan' => 3600,
],
]);
$view
->save();
$this
->doTestFrontPageViewCacheTags(TRUE);
}
protected function doTestFrontPageViewCacheTags($do_assert_views_caches) {
$view = Views::getView('frontpage');
$view
->setDisplay('page_1');
$cache_contexts = [
'user.node_grants:view',
'languages:' . LanguageInterface::TYPE_INTERFACE,
'user.permissions',
'theme',
'url.query_args',
'url.site',
];
$cache_context_tags = \Drupal::service('cache_contexts_manager')
->convertTokensToKeys($cache_contexts)
->getCacheTags();
$empty_node_listing_cache_tags = [
'config:views.view.frontpage',
'node_list',
];
$render_cache_tags = Cache::mergeTags($empty_node_listing_cache_tags, $cache_context_tags);
$render_cache_tags = Cache::mergeTags($render_cache_tags, [
'config:system.site',
]);
$this
->assertViewsCacheTags($view, $empty_node_listing_cache_tags, $do_assert_views_caches, $render_cache_tags);
$expected_tags = Cache::mergeTags($empty_node_listing_cache_tags, $cache_context_tags);
$expected_tags = Cache::mergeTags($expected_tags, [
'http_response',
'rendered',
'config:user.role.anonymous',
'config:system.site',
]);
$this
->assertPageCacheContextsAndTags(Url::fromRoute('view.frontpage.page_1'), $cache_contexts, $expected_tags);
$this
->drupalCreateContentType([
'type' => 'article',
]);
for ($i = 0; $i < 15; $i++) {
$node = Node::create([
'body' => [
[
'value' => $this
->randomMachineName(32),
'format' => filter_default_format(),
],
],
'type' => 'article',
'created' => $i,
'title' => $this
->randomMachineName(8),
'nid' => $i + 1,
]);
$node
->enforceIsNew(TRUE);
$node
->save();
}
$cache_contexts = Cache::mergeContexts($cache_contexts, [
'timezone',
]);
$first_page_result_cache_tags = [
'config:views.view.frontpage',
'node_list',
'node:6',
'node:7',
'node:8',
'node:9',
'node:10',
'node:11',
'node:12',
'node:13',
'node:14',
'node:15',
];
$cache_context_tags = \Drupal::service('cache_contexts_manager')
->convertTokensToKeys($cache_contexts)
->getCacheTags();
$first_page_output_cache_tags = Cache::mergeTags($first_page_result_cache_tags, $cache_context_tags);
$first_page_output_cache_tags = Cache::mergeTags($first_page_output_cache_tags, [
'config:filter.format.plain_text',
'node_view',
'user_view',
'user:0',
]);
$view
->setDisplay('page_1');
$view
->setCurrentPage(0);
$this
->assertViewsCacheTags($view, $first_page_result_cache_tags, $do_assert_views_caches, $first_page_output_cache_tags);
$this
->assertPageCacheContextsAndTags(Url::fromRoute('view.frontpage.page_1'), $cache_contexts, Cache::mergeTags($first_page_output_cache_tags, [
'http_response',
'rendered',
'config:user.role.anonymous',
]));
$this
->assertPageCacheContextsAndTags(Url::fromRoute('view.frontpage.page_1', [], [
'query' => [
'page' => 1,
],
]), $cache_contexts, [
'node:1',
'node:2',
'node:3',
'node:4',
'node:5',
'config:filter.format.plain_text',
'config:views.view.frontpage',
'node_list',
'node_view',
'user_view',
'user:0',
'http_response',
'rendered',
'config:user.role.anonymous',
]);
$node = Node::load(10);
$title = $node
->getTitle() . 'a';
$node
->setTitle($title);
$node
->save();
$this
->drupalGet(Url::fromRoute('view.frontpage.page_1'));
$this
->assertSession()
->pageTextContains($title);
}
}