protected function RenderCacheIntegrationTest::assertCacheTagsForFieldBasedView in Zircon Profile 8.0
Same name and namespace in other branches
- 8 core/modules/views/src/Tests/RenderCacheIntegrationTest.php \Drupal\views\Tests\RenderCacheIntegrationTest::assertCacheTagsForFieldBasedView()
Tests cache tags on output & result cache items for a field-based view.
Parameters
bool $do_assert_views_caches: Whether to check Views' result & output caches.
3 calls to RenderCacheIntegrationTest::assertCacheTagsForFieldBasedView()
- RenderCacheIntegrationTest::testFieldBasedViewCacheTagsWithCachePluginNone in core/
modules/ views/ src/ Tests/ RenderCacheIntegrationTest.php - Tests a field-based view's cache tags when using the "none" cache plugin.
- RenderCacheIntegrationTest::testFieldBasedViewCacheTagsWithCachePluginTag in core/
modules/ views/ src/ Tests/ RenderCacheIntegrationTest.php - Tests a field-based view's cache tags when using the "tag" cache plugin.
- RenderCacheIntegrationTest::testFieldBasedViewCacheTagsWithCachePluginTime in core/
modules/ views/ src/ Tests/ RenderCacheIntegrationTest.php - Tests a field-based view's cache tags when using the "time" cache plugin.
File
- core/
modules/ views/ src/ Tests/ RenderCacheIntegrationTest.php, line 95 - Contains \Drupal\views\Tests\RenderCacheIntegrationTest.
Class
- RenderCacheIntegrationTest
- Tests the general integration between views and the render cache.
Namespace
Drupal\views\TestsCode
protected function assertCacheTagsForFieldBasedView($do_assert_views_caches) {
$this
->pass('Checking cache tags for field-based view.');
$view = Views::getview('entity_test_fields');
// Empty result (no entities yet).
$this
->pass('Test without entities');
$base_tags = [
'config:views.view.entity_test_fields',
'entity_test_list',
];
$this
->assertViewsCacheTags($view, $base_tags, $do_assert_views_caches, $base_tags);
$this
->assertViewsCacheTagsFromStaticRenderArray($view, $base_tags, $do_assert_views_caches);
// Non-empty result (1 entity).
/** @var \Drupal\Core\Entity\EntityInterface[] $entities */
$entities[] = $entity = EntityTest::create();
$entity
->save();
$this
->pass('Test with entities');
$tags_with_entity = Cache::mergeTags($base_tags, $entities[0]
->getCacheTags());
$this
->assertViewsCacheTags($view, $tags_with_entity, $do_assert_views_caches, $tags_with_entity);
$this
->assertViewsCacheTagsFromStaticRenderArray($view, $tags_with_entity, $do_assert_views_caches);
// Paged result (more entities than the items-per-page limit).
for ($i = 0; $i < 5; $i++) {
$entities[] = $entity = EntityTest::create();
$entity
->save();
}
// Page 1.
$this
->pass('Test pager');
$this
->pass('Page 1');
\Drupal::request()->query
->set('page', 0);
$tags_page_1 = Cache::mergeTags($base_tags, $entities[1]
->getCacheTags());
$tags_page_1 = Cache::mergeTags($tags_page_1, $entities[2]
->getCacheTags());
$tags_page_1 = Cache::mergeTags($tags_page_1, $entities[3]
->getCacheTags());
$tags_page_1 = Cache::mergeTags($tags_page_1, $entities[4]
->getCacheTags());
$tags_page_1 = Cache::mergeTags($tags_page_1, $entities[5]
->getCacheTags());
$this
->assertViewsCacheTags($view, $tags_page_1, $do_assert_views_caches, $tags_page_1);
$this
->assertViewsCacheTagsFromStaticRenderArray($view, $tags_page_1, $do_assert_views_caches);
$view
->destroy();
// Page 2.
$this
->pass('Page 2');
$view
->setCurrentPage(1);
\Drupal::request()->query
->set('page', 1);
$tags_page_2 = Cache::mergeTags($base_tags, $entities[0]
->getCacheTags());
$this
->assertViewsCacheTags($view, $tags_page_2, $do_assert_views_caches, $tags_page_2);
$view
->destroy();
// Ensure that invalidation works on both pages.
$this
->pass('Page invalidations');
$this
->pass('Page 2');
$view
->setCurrentPage(1);
\Drupal::request()->query
->set('page', 1);
$entities[0]->name->value = $random_name = $this
->randomMachineName();
$entities[0]
->save();
$build = $this
->assertViewsCacheTags($view, $tags_page_2, $do_assert_views_caches, $tags_page_2);
// @todo Static render arrays don't support different pages yet, see
// https://www.drupal.org/node/2500701.
// $this->assertViewsCacheTagsFromStaticRenderArray($view, $tags_page_2, $do_assert_views_caches);
$this
->assertTrue(strpos($build['#markup'], $random_name) !== FALSE);
$view
->destroy();
$this
->pass('Page 1');
$view
->setCurrentPage(0);
\Drupal::request()->query
->set('page', 0);
$entities[1]->name->value = $random_name = $this
->randomMachineName();
$entities[1]
->save();
$build = $this
->assertViewsCacheTags($view, $tags_page_1, $do_assert_views_caches, $tags_page_1);
$this
->assertViewsCacheTagsFromStaticRenderArray($view, $tags_page_1, $do_assert_views_caches);
$this
->assertTrue(strpos($build['#markup'], $random_name) !== FALSE);
$view
->destroy();
// Setup arguments to ensure that render caching also varies by them.
$this
->pass('Test arguments');
// Custom assert for a single result row.
$single_entity_assertions = function (array $build, EntityInterface $entity) {
$this
->setRawContent($build['#markup']);
$result = $this
->cssSelect('div.views-row');
$count = count($result);
$this
->assertEqual($count, 1);
$this
->assertEqual((string) $result[0]->div->span, (string) $entity
->id());
};
// Execute the view once with a static renderable and one with a full
// prepared render array.
$tags_argument = Cache::mergeTags($base_tags, $entities[0]
->getCacheTags());
$view
->setArguments([
$entities[0]
->id(),
]);
$build = $this
->assertViewsCacheTags($view, $tags_argument, $do_assert_views_caches, $tags_argument);
$single_entity_assertions($build, $entities[0]);
$view
->setArguments([
$entities[0]
->id(),
]);
$build = $this
->assertViewsCacheTagsFromStaticRenderArray($view, $tags_argument, $do_assert_views_caches);
$single_entity_assertions($build, $entities[0]);
// Set a different argument and ensure that the result is different.
$tags2_argument = Cache::mergeTags($base_tags, $entities[1]
->getCacheTags());
$view
->setArguments([
$entities[1]
->id(),
]);
$build = $this
->assertViewsCacheTagsFromStaticRenderArray($view, $tags2_argument, $do_assert_views_caches);
$single_entity_assertions($build, $entities[1]);
$view
->destroy();
}