View source
<?php
namespace Drupal\Tests\feeds\Kernel\Feeds\Target;
use Drupal\node\Entity\Node;
use Drupal\entity_test\Entity\EntityTestBundle;
use Drupal\feeds\Event\FeedsEvents;
use Drupal\feeds\Event\ParseEvent;
use Drupal\Tests\feeds\Kernel\FeedsKernelTestBase;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
class ConfigEntityReferenceTest extends FeedsKernelTestBase {
use EntityReferenceTestTrait;
public static $modules = [
'field',
'node',
'feeds',
'text',
'filter',
'entity_test',
];
protected function setUp() {
parent::setUp();
EntityTestBundle::create([
'id' => 'test',
'label' => 'Lorem',
'description' => 'My test description',
])
->save();
EntityTestBundle::create([
'id' => 'test2',
'label' => 'Ut wisi',
'description' => 'My test2 description',
])
->save();
$this
->createEntityReferenceField('node', 'article', 'field_entity_test_type', 'Type', 'entity_test_bundle');
}
public function testImportById() {
$feed_type = $this
->createFeedTypeForCsv([
'guid' => 'guid',
'title' => 'title',
'type' => 'type',
], [
'mappings' => array_merge($this
->getDefaultMappings(), [
[
'target' => 'field_entity_test_type',
'map' => [
'target_id' => 'type',
],
'settings' => [
'reference_by' => 'id',
],
],
]),
]);
$feed = $this
->createFeed($feed_type
->id(), [
'source' => $this
->resourcesPath() . '/csv/content-with-config-reference.csv',
]);
$feed
->import();
$this
->assertNodeCount(2);
$expected_values = [
1 => 'test',
2 => 'test2',
];
foreach ($expected_values as $nid => $expected_value) {
$node = Node::load($nid);
$this
->assertEquals($expected_value, $node->field_entity_test_type->target_id);
}
}
public function testImportByLabel() {
$feed_type = $this
->createFeedTypeForCsv([
'guid' => 'guid',
'title' => 'title',
'alpha' => 'alpha',
], [
'mappings' => array_merge($this
->getDefaultMappings(), [
[
'target' => 'field_entity_test_type',
'map' => [
'target_id' => 'alpha',
],
'settings' => [
'reference_by' => 'label',
],
],
]),
]);
$feed = $this
->createFeed($feed_type
->id(), [
'source' => $this
->resourcesPath() . '/csv/content.csv',
]);
$feed
->import();
$this
->assertNodeCount(2);
$expected_values = [
1 => 'test',
2 => 'test2',
];
foreach ($expected_values as $nid => $expected_value) {
$node = Node::load($nid);
$this
->assertEquals($expected_value, $node->field_entity_test_type->target_id);
}
}
public function testImportByUuid() {
$this->container
->get('event_dispatcher')
->addListener(FeedsEvents::PARSE, function (ParseEvent $event) {
$counter = 0;
$config_entities = [
'test',
'test2',
];
foreach ($event
->getParserResult() as $item) {
$uuid = $this->entityTypeManager
->getStorage('entity_test_bundle')
->load($config_entities[$counter])
->uuid();
$item
->set('type', $uuid);
$counter++;
}
}, FeedsEvents::AFTER);
$feed_type = $this
->createFeedTypeForCsv([
'guid' => 'guid',
'title' => 'title',
'type' => 'type',
], [
'mappings' => array_merge($this
->getDefaultMappings(), [
[
'target' => 'field_entity_test_type',
'map' => [
'target_id' => 'type',
],
'settings' => [
'reference_by' => 'uuid',
],
],
]),
]);
$feed = $this
->createFeed($feed_type
->id(), [
'source' => $this
->resourcesPath() . '/csv/content.csv',
]);
$feed
->import();
$this
->assertNodeCount(2);
$expected_values = [
1 => 'test',
2 => 'test2',
];
foreach ($expected_values as $nid => $expected_value) {
$node = Node::load($nid);
$this
->assertEquals($expected_value, $node->field_entity_test_type->target_id);
}
}
}