You are here

public function SerializationTest::testPageVariant in Page Manager 8.4

Same name and namespace in other branches
  1. 8 tests/src/Kernel/SerializationTest.php \Drupal\Tests\page_manager\Kernel\SerializationTest::testPageVariant()

Test serialization of a variant.

@covers \Drupal\page_manager\Entity\PageVariant::__sleep

File

tests/src/Kernel/SerializationTest.php, line 138

Class

SerializationTest
Tests the serialization of the entities we provide.

Namespace

Drupal\Tests\page_manager\Kernel

Code

public function testPageVariant() {
  $page_variant = $this
    ->createPageVariant();

  // Test that a very simple page variant successfully serializes.

  /* @var \Drupal\page_manager\Entity\PageVariant $unserialized */
  $unserialized = $this
    ->assertSerialization($page_variant);
  $this
    ->assertEquals($page_variant
    ->id(), $unserialized
    ->id());
  $this
    ->assertEquals($page_variant
    ->label(), $unserialized
    ->label());
  $this
    ->assertEquals($page_variant
    ->getWeight(), $unserialized
    ->getWeight());
  $this
    ->assertEquals($page_variant
    ->getVariantPluginId(), $unserialized
    ->getVariantPluginId());

  // Test setting the page.
  $page = $this
    ->createPage();
  $page_variant
    ->setPageEntity($page);
  $unserialized = $this
    ->assertSerialization($page_variant);
  $this
    ->assertInstanceOf(Page::class, $unserialized
    ->getPage());
  $this
    ->assertEquals($page
    ->id(), $unserialized
    ->getPage()
    ->id());

  // Test adding static context.
  $page_variant
    ->setStaticContext('test', [
    'label' => 'Test',
    'type' => 'integer',
    'value' => 1,
  ]);
  $unserialized = $this
    ->assertSerialization($page_variant);
  $this
    ->assertEquals($page_variant
    ->getStaticContexts(), $unserialized
    ->getStaticContexts());

  // Add context to the page directly to avoid the
  // \Drupal\page_manager\Event\PageManagerEvents::PAGE_CONTEXT event which
  // relies on the router.
  $context = new Context(new ContextDefinition('integer', 'ID'), 1);
  $page
    ->addContext('id', $context);

  // Test initializing context.
  $page_variant
    ->getContexts();
  $unserialized = $this
    ->assertSerialization($page_variant);
  $this
    ->assertNull($unserialized
    ->get('contexts'));

  // Test adding selection criteria.
  $condition = [
    'id' => 'request_path',
    'pages' => '/admin/foo/*',
    'negate' => FALSE,
    'context_mapping' => [],
  ];
  $page_variant
    ->addSelectionCondition($condition);
  $unserialized = $this
    ->assertSerialization($page_variant);
  $this
    ->assertNull($unserialized
    ->get('selectionConditionCollection'));
  $this
    ->assertEquals($page_variant
    ->getSelectionConditions()
    ->getConfiguration(), $unserialized
    ->getSelectionConditions()
    ->getConfiguration());

  // Initialize the variant plugin.
  $page_variant
    ->getVariantPlugin();
  $unserialized = $this
    ->assertSerialization($page_variant);
  $this
    ->assertNull($unserialized
    ->get('variantPluginCollection'));

  // Test adding variant settings.
  $page_variant
    ->getVariantPlugin()
    ->setConfiguration([
    'page_title' => $this
      ->randomString(),
    'blocks' => [],
  ]);
  $unserialized = $this
    ->assertSerialization($page_variant);
  $this
    ->assertEquals($page_variant
    ->getVariantPlugin()
    ->getConfiguration(), $unserialized
    ->getVariantPlugin()
    ->getConfiguration());
}