Skip to content
Snippets Groups Projects
Commit ed0b8011 authored by Alexey Korepov's avatar Alexey Korepov
Browse files

Add test for mocked entity fields

parent 9bab0f53
No related branches found
No related tags found
No related merge requests found
......@@ -219,27 +219,34 @@ class EntityStubFactory {
// Filling values to the entity array.
foreach ($values as $name => $value) {
if (isset($options['definitions'][$name])) {
// Legacy start.
// @todo Deprecate this.
$options['fields'][$name] = $options['definitions'][$name];
// Legacy end.
}
$newDefinition = NULL;
$fieldType = NULL;
if ($fieldTypeConfiguration = $options['fields'][$name] ?? NULL) {
if (isset($fieldTypeConfiguration['#type'])) {
TestHelpers::throwUserError('The "#type" key is deprecated to match the configuration naming, use "type" instead.');
$fieldTypeConfiguration['type'] ??= $fieldTypeConfiguration['#type'];
}
if (isset($fieldTypeConfiguration['#settings'])) {
TestHelpers::throwUserError('The "#settings" key is deprecated to match the configuration naming, use "settings" instead.');
$fieldTypeConfiguration['settings'] ??= $fieldTypeConfiguration['#settings'];
}
// Legacy start.
// @todo Deprecate and remove this.
if (is_array($fieldTypeConfiguration)) {
if (isset($fieldTypeConfiguration['#type'])) {
TestHelpers::throwUserError('The "#type" key is deprecated to match the configuration naming, use "type" instead.');
$fieldTypeConfiguration['type'] ??= $fieldTypeConfiguration['#type'];
}
if (isset($fieldTypeConfiguration['#settings'])) {
TestHelpers::throwUserError('The "#settings" key is deprecated to match the configuration naming, use "settings" instead.');
$fieldTypeConfiguration['settings'] ??= $fieldTypeConfiguration['#settings'];
}
}
// Legacy end.
if (is_object($fieldTypeConfiguration)) {
$newDefinition = $fieldTypeConfiguration;
$fieldTypeConfiguration = NULL;
}
if (is_string($fieldTypeConfiguration)) {
elseif (is_string($fieldTypeConfiguration)) {
// Parsing value as a field type scalar value.
$fieldType = $fieldTypeConfiguration;
if ($fieldType == 'entity_reference') {
......
......@@ -2,6 +2,9 @@
namespace Drupal\Tests\test_helpers\Unit\TestHelpersApi;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldItemList;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\UnitTestCase;
......@@ -154,7 +157,7 @@ class CreateEntityStubTest extends UnitTestCase {
}
/**
* Tests creating entities with custom methods.
* Tests creating entities with mocked methods.
*
* @covers ::createEntity
* @covers \Drupal\test_helpers\StubFactory\EntityStubFactory::create
......@@ -184,4 +187,35 @@ class CreateEntityStubTest extends UnitTestCase {
$this->assertEquals('Email successfully sent.', $entitySendResult);
}
/**
* Tests creating entities with mocked methods.
*
* @covers ::createEntity
* @covers \Drupal\test_helpers\StubFactory\EntityStubFactory::create
*/
public function testEntityWithMockedFields() {
$customField = $this->createMock(FieldItemListInterface::class);
$customField->method('getValue')->willReturn('My custom value');
$customFieldDefinition = $this->createMock(BaseFieldDefinition::class);
$customFieldDefinition->method('getDataType')->willReturn('string');
$customFieldDefinition->method('getClass')->willReturn(FieldItemList::class);
$customFieldDefinition->method('getPropertyNames')->willReturn(
['custom_property_1', 'custom_property_2']
);
$entity1 = TestHelpers::saveEntity(Node::class, [
'title' => 'Article 1',
'field_custom' => $customField,
'field_very_custom' => NULL,
], NULL, ['fields' => ['field_very_custom' => $customFieldDefinition]],
);
$entity2 = TestHelpers::saveEntity(Node::class, ['title' => 'Article 2']);
$this->assertEquals('My custom value', $entity1->field_custom->getValue());
$this->assertEquals(['custom_property_1', 'custom_property_2'],
$entity2->field_very_custom->getFieldDefinition()->getPropertyNames()
);
}
}
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