function ViewExecutableTest::testSerialization
Same name in other branches
- 9 core/modules/views/tests/src/Kernel/ViewExecutableTest.php \Drupal\Tests\views\Kernel\ViewExecutableTest::testSerialization()
- 8.9.x core/modules/views/tests/src/Kernel/ViewExecutableTest.php \Drupal\Tests\views\Kernel\ViewExecutableTest::testSerialization()
- 10 core/modules/views/tests/src/Kernel/ViewExecutableTest.php \Drupal\Tests\views\Kernel\ViewExecutableTest::testSerialization()
Tests serialization of the ViewExecutable object.
File
-
core/
modules/ views/ tests/ src/ Kernel/ ViewExecutableTest.php, line 499
Class
- ViewExecutableTest
- Tests the ViewExecutable class.
Namespace
Drupal\Tests\views\KernelCode
public function testSerialization() : void {
$view = Views::getView('test_executable_displays');
$view->setDisplay('page_1');
$view->setArguments([
'test',
]);
$view->setCurrentPage(2);
$serialized = serialize($view);
// Test the view storage object is not present in the actual serialized
// string.
$this->assertStringNotContainsString('"Drupal\\views\\Entity\\View"', $serialized, 'The Drupal\\views\\Entity\\View class was not found in the serialized string.');
/** @var \Drupal\views\ViewExecutable $unserialized */
$unserialized = unserialize($serialized);
$this->assertInstanceOf(ViewExecutable::class, $unserialized);
$this->assertSame($unserialized->storage
->id(), $view->storage
->id(), 'The expected storage entity was loaded on the unserialized view.');
$this->assertSame('page_1', $unserialized->current_display, 'The expected display was set on the unserialized view.');
$this->assertSame([
'test',
], $unserialized->args, 'The expected argument was set on the unserialized view.');
$this->assertSame(2, $unserialized->getCurrentPage(), 'The expected current page was set on the unserialized view.');
// Get the definition of node's nid field, for example. Only get it not from
// the field manager directly, but from the item data definition. It should
// be the same base field definition object (the field and item definitions
// refer to each other).
// See https://bugs.php.net/bug.php?id=66052
$field_manager = $this->container
->get('entity_field.manager');
$nid_definition_before = $field_manager->getBaseFieldDefinitions('node')['nid']
->getItemDefinition()
->getFieldDefinition();
// Load and execute a view.
$view_entity = View::load('content');
$view_executable = $view_entity->getExecutable();
$view_executable->execute('page_1');
// Reset the static cache. Don't use clearCachedFieldDefinitions() since
// that clears the persistent cache and we need to get the serialized cache
// data.
$field_manager->useCaches(FALSE);
$field_manager->useCaches(TRUE);
// Serialize the ViewExecutable as part of other data.
unserialize(serialize([
'SOMETHING UNEXPECTED',
$view_executable,
]));
// Make sure the serialization of the ViewExecutable didn't influence the
// field definitions.
$nid_definition_after = $field_manager->getBaseFieldDefinitions('node')['nid']
->getItemDefinition()
->getFieldDefinition();
$this->assertEquals($nid_definition_before->getPropertyDefinitions(), $nid_definition_after->getPropertyDefinitions());
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.