View source
<?php
namespace Drupal\Tests\Core\Config\Entity;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Config\Entity\ConfigEntityType;
use Drupal\Core\Config\Entity\Exception\ConfigEntityStorageClassException;
class ConfigEntityTypeTest extends UnitTestCase {
protected $typedConfigManager;
protected function setUp() : void {
$this->typedConfigManager = $this
->createMock(TypedConfigManagerInterface::class);
$container = new ContainerBuilder();
$container
->set('config.typed', $this->typedConfigManager);
\Drupal::setContainer($container);
}
protected function setUpConfigEntityType($definition) {
if (!isset($definition['id'])) {
$definition += [
'id' => 'example_config_entity_type',
];
}
return new ConfigEntityType($definition);
}
public function testConfigPrefixLengthExceeds() {
$definition = [
'provider' => $this
->randomMachineName(24),
'config_prefix' => $this
->randomMachineName(59),
];
$config_entity = $this
->setUpConfigEntityType($definition);
$this
->expectException('\\Drupal\\Core\\Config\\ConfigPrefixLengthException');
$this
->expectExceptionMessage("The configuration file name prefix {$definition['provider']}.{$definition['config_prefix']} exceeds the maximum character limit of " . ConfigEntityType::PREFIX_LENGTH);
$this
->assertEmpty($config_entity
->getConfigPrefix());
}
public function testConfigPrefixLengthValid() {
$definition = [
'provider' => $this
->randomMachineName(24),
'config_prefix' => $this
->randomMachineName(58),
];
$config_entity = $this
->setUpConfigEntityType($definition);
$expected_prefix = $definition['provider'] . '.' . $definition['config_prefix'];
$this
->assertEquals($expected_prefix, $config_entity
->getConfigPrefix());
}
public function testConstruct() {
$config_entity = new ConfigEntityType([
'id' => 'example_config_entity_type',
]);
$this
->assertEquals('Drupal\\Core\\Config\\Entity\\ConfigEntityStorage', $config_entity
->getStorageClass());
}
public function testConstructBadStorage() {
$this
->expectException(ConfigEntityStorageClassException::class);
$this
->expectExceptionMessage('\\Drupal\\Core\\Entity\\KeyValueStore\\KeyValueEntityStorage is not \\Drupal\\Core\\Config\\Entity\\ConfigEntityStorage or it does not extend it');
new ConfigEntityType([
'id' => 'example_config_entity_type',
'handlers' => [
'storage' => '\\Drupal\\Core\\Entity\\KeyValueStore\\KeyValueEntityStorage',
],
]);
}
public function testSetStorageClass() {
$config_entity = $this
->setUpConfigEntityType([]);
$this
->expectException(ConfigEntityStorageClassException::class);
$this
->expectExceptionMessage('\\Drupal\\Core\\Entity\\KeyValueStore\\KeyValueEntityStorage is not \\Drupal\\Core\\Config\\Entity\\ConfigEntityStorage or it does not extend it');
$config_entity
->setStorageClass('\\Drupal\\Core\\Entity\\KeyValueStore\\KeyValueEntityStorage');
}
public function testGetConfigPrefix($definition, $expected) {
$entity_type = $this
->setUpConfigEntityType($definition);
$this
->assertSame($expected, $entity_type
->getConfigPrefix());
}
public function providerTestGetConfigPrefix() {
return [
[
[
'provider' => 'node',
'id' => 'node_type',
'config_prefix' => 'type',
],
'node.type',
],
[
[
'provider' => 'views',
'id' => 'view',
],
'views.view',
],
];
}
public function testGetPropertiesToExport($definition, $expected) {
$entity_type = $this
->setUpConfigEntityType($definition);
$properties_to_export = $entity_type
->getPropertiesToExport();
$this
->assertSame($expected, $properties_to_export);
$properties_to_export = $entity_type
->getPropertiesToExport();
$this
->assertSame($expected, $properties_to_export);
}
public function providerGetPropertiesToExport() {
$data = [];
$data[] = [
[
'config_export' => [
'id',
'custom_property' => 'customProperty',
],
],
[
'uuid' => 'uuid',
'langcode' => 'langcode',
'status' => 'status',
'dependencies' => 'dependencies',
'third_party_settings' => 'third_party_settings',
'_core' => '_core',
'id' => 'id',
'custom_property' => 'customProperty',
],
];
$data[] = [
[
'config_export' => [
'id',
],
'mergedConfigExport' => [
'random_key' => 'random_key',
],
],
[
'random_key' => 'random_key',
],
];
return $data;
}
public function testGetPropertiesToExportNoFallback() {
$config_entity_type = new ConfigEntityType([
'id' => 'example_config_entity_type',
]);
$this
->assertNull($config_entity_type
->getPropertiesToExport());
}
}