Skip to content
Snippets Groups Projects
Commit 3c385464 authored by Vadym Abramchuk's avatar Vadym Abramchuk
Browse files

Issue #3400982: Simple field processor plugin import test

parent ccb31df4
No related branches found
No related tags found
1 merge request!80Resolve #3400982 "Field processor plugins"
......@@ -44,7 +44,7 @@ abstract class FieldProcessorTestBase extends KernelTestBase {
}
/**
* Returns an array of test data.
* Returns an array of export test data.
*
* Each array row is a single test case; rows are keyed by a human-readable
* label.
......@@ -64,7 +64,7 @@ abstract class FieldProcessorTestBase extends KernelTestBase {
public function testExportFieldValue(
array $fieldStorageDefinition,
mixed $fieldValue,
mixed $expectedExportOutput,
array $expectedExportOutput
): void {
$this->prepareNodeTypeWithField($fieldStorageDefinition);
......@@ -76,10 +76,48 @@ abstract class FieldProcessorTestBase extends KernelTestBase {
$node->save();
$this->assertEquals(
$expectedExportOutput,
$this->getFieldProcessor()->exportFieldValue($node->get('field_test_field'))
);
$this->assertExportedValueEquals(
$expectedExportOutput,
$this->getFieldProcessor()->exportFieldValue($node->get('field_test_field')),
);
}
/**
* Returns an array of import test data.
*
* Each array row is a single test case; rows are keyed by a human-readable
* label.
*
* Each row is an array with the following elements: field storage definition,
* data to import, expected field value.
*
* @return array
* An array of test data.
*/
abstract protected function importFieldValueDataProvider(): array;
/**
* @covers ::importFieldValue
* @dataProvider importFieldValueDataProvider
*/
public function testImportFieldValue(
array $fieldStorageDefinition,
mixed $dataToImport,
mixed $expectedFieldValue,
): void {
$this->prepareNodeTypeWithField($fieldStorageDefinition);
$node = Node::create([
'type' => 'article',
'title' => 'Test node',
]);
$this->getFieldProcessor()->importFieldValue($node, 'field_test_field', $dataToImport);
$this->assertImportedValueEquals(
$expectedFieldValue,
$node->get('field_test_field')->getValue(),
);
}
/**
......@@ -115,4 +153,34 @@ abstract class FieldProcessorTestBase extends KernelTestBase {
])->save();
}
/**
* Asserts equality of exported value.
*
* This method could be overridden in child classes to provide custom
* comparison logic.
*
* @param mixed $expectedExportOutput
* The expected export output.
* @param array $actualExportOutput
* The actual export output.
*/
protected function assertExportedValueEquals(mixed $expectedExportOutput, array $actualExportOutput) {
$this->assertEquals($expectedExportOutput, $actualExportOutput);
}
/**
* Asserts equality of imported value.
*
* This method could be overridden in child classes to provide custom
* comparison logic.
*
* @param mixed $expectedFieldValue
* The expected field value.
* @param mixed $actualImportedFieldValue
* The actual imported field value.
*/
protected function assertImportedValueEquals(mixed $expectedFieldValue, mixed $actualImportedFieldValue): void {
$this->assertEquals($expectedFieldValue, $actualImportedFieldValue);
}
}
......@@ -4,9 +4,29 @@ namespace Drupal\Tests\single_content_sync\Kernel\FieldProcessor;
/**
* @coversDefaultClass \Drupal\single_content_sync\Plugin\SingleContentSyncFieldProcessor\SimpleField
*
* @todo add test cases for all field types described in SimpleFieldDeriver.
*/
class SimpleFieldProcessorTest extends FieldProcessorTestBase {
/**
* {@inheritdoc}
*/
protected function importFieldValueDataProvider(): array {
return [
'string' => [
[
'type' => 'string',
'settings' => [
'max_length' => 255,
],
],
[0 => ['value' => 'hello']],
[0 => ['value' => 'hello']],
],
];
}
/**
* {@inheritdoc}
*/
......
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