View source
<?php
namespace Drupal\Tests\page_manager\Kernel;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\KernelTests\KernelTestBase;
use Drupal\page_manager\Entity\Page;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Plugin\Context\Context;
use Drupal\page_manager\Entity\PageVariant;
use Drupal\page_manager\Plugin\DisplayVariant\HttpStatusCodeDisplayVariant;
use Drupal\page_manager\Plugin\DisplayVariant\PageBlockDisplayVariant;
class SerializationTest extends KernelTestBase {
use StringTranslationTrait;
public static $modules = [
'page_manager',
'system',
'user',
];
public function setUp() {
parent::setUp();
$this
->installEntitySchema('user');
}
protected function assertSerialization($object, $message = '') {
$unserialized = unserialize(serialize($object));
$this
->assertInstanceOf(get_class($object), $unserialized, $message);
return $unserialized;
}
protected function createPage() {
return Page::create([
'id' => $this
->randomMachineName(),
'label' => $this
->randomString(),
'description' => $this
->randomString(),
'path' => 'admin/foo',
'use_admin_theme' => TRUE,
]);
}
protected function createPageVariant() {
return PageVariant::create([
'id' => $this
->randomMachineName(),
'label' => $this
->randomString(),
'weight' => 0,
'variant' => 'block_display',
]);
}
public function testPage() {
$page = $this
->createPage();
$unserialized = $this
->assertSerialization($page);
$this
->assertEquals($page
->id(), $unserialized
->id());
$this
->assertEquals($page
->label(), $unserialized
->label());
$this
->assertEquals($page
->getDescription(), $unserialized
->getDescription());
$this
->assertEquals($page
->getPath(), $unserialized
->getPath());
$this
->assertEquals($page
->usesAdminTheme(), $unserialized
->usesAdminTheme());
$page
->set('path', 'admin/foo/{id}');
$page
->setParameter('id', 'integer', 'ID');
$unserialized = $this
->assertSerialization($page);
$this
->assertEquals($page
->getPath(), $unserialized
->getPath());
$this
->assertEquals($page
->getParameters(), $unserialized
->getParameters());
$condition = [
'id' => 'request_path',
'pages' => '/admin/foo/*',
'negate' => FALSE,
'context_mapping' => [],
];
$page
->addAccessCondition($condition);
$unserialized = $this
->assertSerialization($page);
$this
->assertNull($unserialized
->get('accessConditionCollection'));
$this
->assertEquals($page
->getAccessConditions()
->getConfiguration(), $unserialized
->getAccessConditions()
->getConfiguration());
$context = new Context(new ContextDefinition('integer', 'ID'), 1);
$page
->addContext('id', $context);
$unserialized = $this
->assertSerialization($page);
$this
->assertEquals([], $unserialized
->get('contexts'));
$page_variant = $this
->createPageVariant();
$page
->addVariant($page_variant);
$unserialized = $this
->assertSerialization($page);
$this
->assertInstanceOf(PageVariant::class, $unserialized
->getVariant($page_variant
->id()));
$this
->assertEquals($page_variant
->id(), $unserialized
->getVariant($page_variant
->id())
->id());
}
public function testPageVariant() {
$page_variant = $this
->createPageVariant();
$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());
$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());
$page_variant
->setStaticContext('test', [
'label' => 'Test',
'type' => 'integer',
'value' => 1,
]);
$unserialized = $this
->assertSerialization($page_variant);
$this
->assertEquals($page_variant
->getStaticContexts(), $unserialized
->getStaticContexts());
$context = new Context(new ContextDefinition('integer', 'ID'), 1);
$page
->addContext('id', $context);
$page_variant
->getContexts();
$unserialized = $this
->assertSerialization($page_variant);
$this
->assertNull($unserialized
->get('contexts'));
$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());
$page_variant
->getVariantPlugin();
$unserialized = $this
->assertSerialization($page_variant);
$this
->assertNull($unserialized
->get('variantPluginCollection'));
$page_variant
->getVariantPlugin()
->setConfiguration([
'page_title' => $this
->randomString(),
'blocks' => [],
]);
$unserialized = $this
->assertSerialization($page_variant);
$this
->assertEquals($page_variant
->getVariantPlugin()
->getConfiguration(), $unserialized
->getVariantPlugin()
->getConfiguration());
}
public function testPageBlockVariantPlugin() {
$configuration = [
'page_title' => 'Test variant',
];
$variant_plugin = $this->container
->get('plugin.manager.display_variant')
->createInstance('block_display', $configuration);
$this
->assertInstanceOf(PageBlockDisplayVariant::class, $variant_plugin);
$unserialized = $this
->assertSerialization($variant_plugin);
$this
->assertEquals($variant_plugin
->getConfiguration(), $unserialized
->getConfiguration());
$context = new Context(new ContextDefinition('integer', 'ID'), 1);
$variant_plugin
->setContexts([
'id' => $context,
]);
$unserialized = $this
->assertSerialization($variant_plugin);
$this
->assertEquals([], $unserialized
->getContexts());
}
public function testHttpStatusCodeVariantPlugin() {
$configuration = [
'status_code' => '404',
];
$variant_plugin = $this->container
->get('plugin.manager.display_variant')
->createInstance('http_status_code', $configuration);
$this
->assertInstanceOf(HttpStatusCodeDisplayVariant::class, $variant_plugin);
$unserialized = $this
->assertSerialization($variant_plugin);
$this
->assertEquals($variant_plugin
->getConfiguration(), $unserialized
->getConfiguration());
}
}