function ContextTypedDataTest::testSerialize

Same name in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/Core/Plugin/ContextTypedDataTest.php \Drupal\KernelTests\Core\Plugin\ContextTypedDataTest::testSerialize()
  2. 10 core/tests/Drupal/KernelTests/Core/Plugin/ContextTypedDataTest.php \Drupal\KernelTests\Core\Plugin\ContextTypedDataTest::testSerialize()
  3. 11.x core/tests/Drupal/KernelTests/Core/Plugin/ContextTypedDataTest.php \Drupal\KernelTests\Core\Plugin\ContextTypedDataTest::testSerialize()

Tests that contexts can be serialized.

File

core/tests/Drupal/KernelTests/Core/Plugin/ContextTypedDataTest.php, line 23

Class

ContextTypedDataTest
Tests that contexts work properly with the typed data manager.

Namespace

Drupal\KernelTests\Core\Plugin

Code

public function testSerialize() {
    $definition = new ContextDefinition('any');
    $data_definition = DataDefinition::create('string');
    $typed_data = new StringData($data_definition);
    $typed_data->setValue('example string');
    $context = new Context($definition, $typed_data);
    // getContextValue() will cause the context to reference the typed data
    // manager service.
    $value = $context->getContextValue();
    $context = serialize($context);
    $context = unserialize($context);
    $this->assertSame($value, $context->getContextValue());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.