View source
<?php
namespace Drupal\Tests\schemata\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\NodeType;
use Drupal\schemata\Schema\SchemaInterface;
use Drupal\schemata\Schema\NodeSchema;
class SchemaFactoryTest extends KernelTestBase {
protected $factory;
public static $modules = [
'schemata',
'field',
'node',
'serialization',
'system',
'user',
];
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('node');
$this
->installEntitySchema('user');
$this
->installSchema('system', [
'sequences',
]);
$this
->installSchema('node', [
'node_access',
]);
$this
->installSchema('user', [
'users_data',
]);
$this->nodeType = NodeType::create([
'type' => 'article',
]);
$this->nodeType
->save();
$this->factory = \Drupal::service('schemata.schema_factory');
}
public function testCreateNodeBaseSchema() {
$schema = $this->factory
->create('node');
$this
->assertInstanceOf(SchemaInterface::class, $schema);
$this
->assertNotInstanceOf(NodeSchema::class, $schema);
$this
->assertSchemaHasNoBundle($schema);
$this
->assertSchemaHasTitle($schema);
$this
->assertSchemaHasDescription($schema);
$this
->assertSchemaHasProperties($schema);
}
public function testCreateNodeArticleSchema() {
$schema = $this->factory
->create('node', 'article');
$this
->assertInstanceOf(SchemaInterface::class, $schema);
$this
->assertInstanceOf(NodeSchema::class, $schema);
$this
->assertSchemaHasBundle($schema, 'article');
$this
->assertSchemaHasTitle($schema);
$this
->assertSchemaHasDescription($schema);
$this
->assertSchemaHasProperties($schema);
}
public function testCreateUserSchema() {
$schema = $this->factory
->create('user');
$this
->assertInstanceOf(SchemaInterface::class, $schema);
$this
->assertNotInstanceOf(NodeSchema::class, $schema);
$this
->assertSchemaHasNoBundle($schema);
$this
->assertSchemaHasTitle($schema);
$this
->assertSchemaHasDescription($schema);
$this
->assertSchemaHasProperties($schema);
}
public function testInvalidEntityOnCreate() {
$schema = $this->factory
->create('gastropod');
$this
->assertEmpty($schema, 'Schemata should not produce a schema for non-existant entity types.');
$schema = $this->factory
->create('node', 'gastropod');
$this
->assertEmpty($schema, 'Schemata should not produce a schema for non-existant bundles.');
}
public function testInvalidEntityOnGetPlugin() {
$this
->expectException('\\Drupal\\Component\\Plugin\\Exception\\PluginNotFoundException');
$this->factory
->getSourceEntityPlugin('gastropod');
}
public function testConfigEntityOnCreate() {
$schema = $this->factory
->create('node_type');
$this
->assertEmpty($schema, 'Schemata does not support Config entities.');
}
public function testConfigEntityOnGetPlugin() {
$this
->expectException('\\InvalidArgumentException');
$this->factory
->getSourceEntityPlugin('node_type');
}
protected function assertSchemaHasTitle(SchemaInterface $schema) {
$this
->assertNotEmpty($schema
->getMetadata()['title']);
}
protected function assertSchemaHasDescription(SchemaInterface $schema) {
$this
->assertNotEmpty($schema
->getMetadata()['description']);
}
protected function assertSchemaHasProperties(SchemaInterface $schema) {
$this
->assertGreaterThanOrEqual(1, count($schema
->getProperties()));
}
protected function assertSchemaHasBundle(SchemaInterface $schema, $bundle) {
$this
->assertEquals($bundle, $schema
->getBundleId());
}
protected function assertSchemaHasNoBundle(SchemaInterface $schema) {
$this
->assertEmpty($schema
->getBundleId());
}
}