Skip to content
Snippets Groups Projects
Commit e0efb879 authored by Nia Kathoni's avatar Nia Kathoni Committed by Daniel Cothran
Browse files

Issue #3353532 by nikathone: Implement basic test for the module

parent 3aefbe22
No related branches found
No related tags found
1 merge request!7Issue #3353532: Implement basic test for the module
denormalizer.table.*:
denormalizer.denormalizer_table.*:
type: config_entity
label: 'Denormalizer table'
mapping:
......@@ -15,7 +15,7 @@ denormalizer.table.*:
type: string
label: 'Source'
configuration:
type: denormalizer.table.source.[%parent.source]
type: denormalizer.denormalizer_table.source.[%parent.source]
sql_mode:
type: string
label: 'SQL mode'
......@@ -23,10 +23,17 @@ denormalizer.table.*:
type: string
label: 'View prefix'
denormalizer.table.soruce.*:
denormalizer_table_configuration:
type: mapping
mapping:
changed_key:
type: string
label: 'Changed key'
denormalizer.denormalizer_table.source.*:
type: denormalizer_table_configuration
denormalizer.table.source.entity:
denormalizer.denormalizer_table.source.entity:
type: denormalizer_table_configuration
mapping:
entity_type:
......@@ -50,7 +57,7 @@ denormalizer.table.source.entity:
sequence:
type: string
denormalizer.table.source.non_entity:
denormalizer.denormalizer_table.source.non_entity:
type: denormalizer_table_configuration
mapping:
base_table:
......@@ -62,10 +69,3 @@ denormalizer.table.source.non_entity:
db_prefix:
type: string
label: 'Database prefix'
denormalizer_table_configuration:
type: mapping
mapping:
changed_key:
type: string
label: 'Changed key'
<?php
namespace Drupal\Tests\denormalizer\Kernel;
use Drupal\Core\Database\Connection;
use Drupal\denormalizer\Entity\DenormalizerTable;
use Drupal\denormalizer\Entity\DenormalizerTableInterface;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\NodeType;
use Drupal\node\NodeTypeInterface;
/**
* Test the denormalizer table when the source is an entity.
*
* @group denormalizer
*/
class EntitySourceDenormalizedTableTest extends KernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'user',
'system',
'field',
'node',
'text',
'filter',
'denormalizer',
];
/**
* The node type to test with.
*
* @var \Drupal\node\Entity\NodeType
*/
protected NodeTypeInterface $nodeType;
/**
* The initialized denormalized table.
*
* @var \Drupal\denormalizer\Entity\DenormalizerTableInterface
*/
protected DenormalizerTableInterface $denormalizedTable;
/**
* The database connection used.
*
* @var \Drupal\Core\Database\Connection
*/
protected Connection $database;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->database = $this->container->get('database');
$this->installSchema('system', 'sequences');
$this->installEntitySchema('node');
$this->installConfig(['field', 'node']);
$this->nodeType = $this->setUpPageNodeType();
node_add_body_field($this->nodeType);
$this->denormalizedTable = $this->setDenormalizedTable([
'label' => $this->nodeType->label(),
'status' => TRUE,
'source' => 'entity',
'configuration' => [
'entity_type' => 'node',
'bundle' => $this->nodeType->id(),
'changed_key' => 'changed',
'fields' => [
'base' => [
'nid' => 'nid',
'uuid' => 'uuid',
'vid' => 'vid',
'langcode' => 'langcode',
'type' => 'type',
'status' => 'status',
'uid' => 'uid',
'title' => 'title',
],
'bundle' => [
'body' => 'body',
],
],
],
]);
}
/**
* Test that the stored configurations matched the expected value.
*
* @param mixed $expected
* The expected value.
* @param string $expected_key
* The expected key to retrieve stored value.
*
* @dataProvider provideTestSet
*/
public function testConfigurations($expected, string $expected_key) {
$this->assertEquals($expected, $this->getStoredValueBasedOnExpectedKey($expected_key));
}
/**
* Provides the test data set for the configurations test.
*
* @return array
* The test data set.
*/
public function provideTestSet(): array {
$expected_base_fields = [
'nid' => 'nid',
'uuid' => 'uuid',
'vid' => 'vid',
'langcode' => 'langcode',
'type' => 'type',
'status' => 'status',
'uid' => 'uid',
'title' => 'title',
];
$expected_bundle_fields = ['body' => 'body'];
return [
'Configuration label' => ['Page', 'label'],
'Table name' => ['denormalizer_page', 'table_name'],
'Is enabled' => [TRUE, 'status'],
'Source configuration entity type id' => ['node', 'source_configuration_entity_type'],
'Source configuration bundle' => ['page', 'source_configuration_bundle'],
'Source configuration bundle fields' => [$expected_bundle_fields, 'source_configuration_fields_bundle'],
'Source configuration base fields' => [$expected_base_fields, 'source_configuration_fields_base'],
];
}
/**
* Sets up the page node type.
*
* @return \Drupal\node\NodeTypeInterface
* The node type.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
protected function setUpPageNodeType(): NodeTypeInterface {
$node_type = NodeType::create([
'type' => 'page',
'name' => 'Page',
]);
$node_type->save();
return $node_type;
}
/**
* Sets the denormalized table.
*
* @param array $settings
* The settings.
*
* @return \Drupal\denormalizer\Entity\DenormalizerTableInterface
* The set denormalized table config entity.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
protected function setDenormalizedTable(array $settings): DenormalizerTableInterface {
$denormalizer_table = DenormalizerTable::create($settings + [
'id' => $this->nodeType->id(),
]);
$denormalizer_table->save();
return $denormalizer_table;
}
/**
* Gets the stored value based on the expected key.
*
* @param string $expected_key
* The expected key use to retrieve the value.
*
* @return mixed
* The stored value.
*/
protected function getStoredValueBasedOnExpectedKey(string $expected_key) {
$source_configurations = $this->denormalizedTable->getSourceConfiguration();
return match($expected_key) {
'label' => $this->denormalizedTable->label(),
'table_name' => $this->denormalizedTable->getDbTableName(),
'status' => $this->denormalizedTable->status(),
'source_configuration_entity_type' => $source_configurations['entity_type'],
'source_configuration_bundle' => $source_configurations['bundle'],
'source_configuration_fields_bundle' => $source_configurations['fields']['bundle'],
'source_configuration_fields_base' => $source_configurations['fields']['base'],
default => '',
};
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment