View source
<?php
namespace Drupal\Tests\feeds\Kernel\Feeds\Target;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\feeds\Plugin\Type\Processor\ProcessorInterface;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\taxonomy\Entity\Term;
use Drupal\Tests\feeds\Kernel\FeedsKernelTestBase;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
class EntityReferenceTest extends FeedsKernelTestBase {
use EntityReferenceTestTrait;
public function testUpdatingMissingReferences() {
$type = NodeType::create([
'type' => 'page',
'name' => 'Page',
]);
$type
->save();
$this
->createEntityReferenceField('node', 'page', 'field_article', 'Article', 'node', 'default', [
'target_bundles' => [
'article',
],
]);
$this
->createFeedType([
'id' => 'article_feed_type',
'label' => 'Article importer',
'fetcher' => 'directory',
'fetcher_configuration' => [
'allowed_extensions' => 'csv',
],
'parser' => 'csv',
'processor_configuration' => [
'authorize' => FALSE,
'update_existing' => ProcessorInterface::UPDATE_EXISTING,
'values' => [
'type' => 'article',
],
],
'custom_sources' => [
'guid' => [
'label' => 'guid',
'value' => 'guid',
'machine_name' => 'guid',
],
'title' => [
'label' => 'title',
'value' => 'title',
'machine_name' => 'title',
],
],
]);
$this
->createFeedType([
'id' => 'page_feed_type',
'label' => 'Page importer',
'fetcher' => 'directory',
'fetcher_configuration' => [
'allowed_extensions' => 'csv',
],
'parser' => 'csv',
'processor_configuration' => [
'authorize' => FALSE,
'update_existing' => ProcessorInterface::UPDATE_EXISTING,
'values' => [
'type' => 'page',
],
],
'custom_sources' => [
'guid' => [
'label' => 'guid',
'value' => 'guid',
'machine_name' => 'guid',
],
'title' => [
'label' => 'title',
'value' => 'title',
'machine_name' => 'title',
],
'article' => [
'label' => 'article',
'value' => 'article',
'machine_name' => 'article',
],
],
'mappings' => array_merge($this
->getDefaultMappings(), [
[
'target' => 'field_article',
'map' => [
'target_id' => 'article',
],
'settings' => [
'reference_by' => 'feeds_item',
'feeds_item' => 'guid',
'autocreate' => 0,
],
],
]),
]);
$feed = $this
->createFeed('page_feed_type', [
'source' => $this
->resourcesPath() . '/csv/content-with-reference.csv',
]);
$feed
->import();
$this
->assertNodeCount(2);
$node = Node::load(1);
$this
->assertEquals([], $node->field_article
->getValue());
$feed2 = $this
->createFeed('article_feed_type', [
'source' => $this
->resourcesPath() . '/csv/content.csv',
]);
$feed2
->import();
$this
->assertNodeCount(4);
$feed
->import();
$node = $this
->reloadEntity($node);
$this
->assertEquals(4, $node->field_article->target_id);
$node2 = Node::load(2);
$this
->assertEquals(3, $node2->field_article->target_id);
$node->title->value = 'Page 1';
$node
->save();
$node2->title->value = 'Page 2';
$node2
->save();
$feed
->import();
$node = $this
->reloadEntity($node);
$this
->assertEquals('Page 1', $node->title->value);
$node2 = $this
->reloadEntity($node2);
$this
->assertEquals('Page 2', $node2->title->value);
}
public function testUpdatingMissingNodeAuthors() {
$this
->createFeedType([
'id' => 'article_feed_type',
'label' => 'Article importer',
'fetcher' => 'directory',
'fetcher_configuration' => [
'allowed_extensions' => 'csv',
],
'parser' => 'csv',
'processor_configuration' => [
'authorize' => FALSE,
'update_existing' => ProcessorInterface::UPDATE_EXISTING,
'values' => [
'type' => 'article',
],
],
'custom_sources' => [
'guid' => [
'label' => 'guid',
'value' => 'guid',
'machine_name' => 'guid',
],
'title' => [
'label' => 'title',
'value' => 'title',
'machine_name' => 'title',
],
'author' => [
'label' => 'author',
'value' => 'author',
'machine_name' => 'author',
],
],
'mappings' => array_merge($this
->getDefaultMappings(), [
[
'target' => 'uid',
'map' => [
'target_id' => 'author',
],
'settings' => [
'reference_by' => 'name',
'autocreate' => 0,
],
],
]),
]);
$this
->createFeedType([
'id' => 'authors',
'label' => 'Authors',
'fetcher' => 'directory',
'fetcher_configuration' => [
'allowed_extensions' => 'csv',
],
'parser' => 'csv',
'processor' => 'entity:user',
'processor_configuration' => [
'authorize' => FALSE,
'values' => [],
],
'custom_sources' => [
'name' => [
'label' => 'name',
'value' => 'name',
'machine_name' => 'name',
],
'mail' => [
'label' => 'mail',
'value' => 'mail',
'machine_name' => 'mail',
],
'status' => [
'label' => 'status',
'value' => 'status',
'machine_name' => 'status',
],
],
'mappings' => [
[
'target' => 'name',
'map' => [
'value' => 'name',
],
'unique' => [
'value' => 1,
],
],
[
'target' => 'mail',
'map' => [
'value' => 'mail',
],
],
[
'target' => 'status',
'map' => [
'value' => 'status',
],
],
],
]);
$article_feed = $this
->createFeed('article_feed_type', [
'source' => $this
->resourcesPath() . '/csv/content-with-author.csv',
]);
$article_feed
->import();
$this
->assertNodeCount(3);
$node = Node::load(1);
$this
->assertEquals(0, $node->uid->target_id);
$author_feed = $this
->createFeed('authors', [
'source' => $this
->resourcesPath() . '/csv/users.csv',
]);
$author_feed
->import();
$article_feed
->import();
$nodes[1] = $this
->reloadEntity($node);
$this
->assertEquals(1, $nodes[1]->uid->target_id);
$nodes[2] = Node::load(2);
$this
->assertEquals(1, $nodes[2]->uid->target_id);
$nodes[3] = Node::load(3);
$this
->assertEquals(2, $nodes[3]->uid->target_id);
for ($i = 1; $i <= 3; $i++) {
$nodes[$i]->title->value = 'Article ' . $i;
$nodes[$i]
->save();
}
$article_feed
->import();
for ($i = 1; $i <= 3; $i++) {
$node = $this
->reloadEntity($nodes[$i]);
$this
->assertEquals('Article ' . $i, $node->title->value);
}
}
public function testUpdatingMissingParentTerms() {
$vocabulary = $this
->installTaxonomyModuleWithVocabulary();
$feed_type = $this
->createFeedType([
'fetcher' => 'directory',
'fetcher_configuration' => [
'allowed_extensions' => 'csv',
],
'parser' => 'csv',
'processor' => 'entity:taxonomy_term',
'processor_configuration' => [
'authorize' => FALSE,
'update_existing' => ProcessorInterface::UPDATE_EXISTING,
'values' => [
'vid' => 'tags',
],
],
'custom_sources' => [
'name' => [
'label' => 'name',
'value' => 'name',
'machine_name' => 'name',
],
'parent' => [
'label' => 'parent',
'value' => 'parent',
'machine_name' => 'parent',
],
],
'mappings' => [
[
'target' => 'name',
'map' => [
'value' => 'name',
],
'unique' => [
'value' => 1,
],
],
[
'target' => 'description',
'map' => [
'value' => 'name',
],
'settings' => [
[
'format' => 'plain_text',
],
],
],
[
'target' => 'parent',
'map' => [
'target_id' => 'parent',
],
'settings' => [
'reference_by' => 'name',
'autocreate' => 0,
],
],
],
]);
$feed = $this
->createFeed($feed_type
->id(), [
'source' => $this
->resourcesPath() . '/csv/terms-with-parent-later-in-file.csv',
]);
$feed
->import();
$terms = Term::loadMultiple();
$expected_term_names = [
1 => 'Belgium',
2 => 'Europe',
3 => 'Netherlands',
];
foreach ($expected_term_names as $term_id => $expected_term_name) {
$this
->assertEquals($expected_term_name, $terms[$term_id]->name->value);
$this
->assertEquals($expected_term_name, $terms[$term_id]->description->value);
}
$this
->assertEquals([], $this->entityTypeManager
->getStorage('taxonomy_term')
->loadParents(1));
$this
->assertEquals([
2,
], array_keys($this->entityTypeManager
->getStorage('taxonomy_term')
->loadParents(3)));
$feed
->import();
$this
->assertEquals([
2,
], array_keys($this->entityTypeManager
->getStorage('taxonomy_term')
->loadParents(1)));
for ($i = 1; $i <= 3; $i++) {
$terms[$i]->description->value = 'Description of term ' . $i;
$terms[$i]
->save();
}
$feed
->import();
for ($i = 1; $i <= 3; $i++) {
$term = $this
->reloadEntity($terms[$i]);
$this
->assertEquals('Description of term ' . $i, $term->description->value);
}
}
public function testWithSingleReference() {
$type = NodeType::create([
'type' => 'event',
'name' => 'Event',
]);
$type
->save();
$this
->createFieldWithStorage('field_alpha', [
'bundle' => 'event',
]);
Node::create([
'title' => 'Event 1',
'type' => 'event',
'field_alpha' => 'Lorem',
])
->save();
Node::create([
'title' => 'Event 2',
'type' => 'event',
'field_alpha' => 'Lorem',
])
->save();
$this
->createEntityReferenceField('node', 'article', 'field_event', 'Event', 'node', 'default', [
'target_bundles' => [
'event',
],
], FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$feed_type = $this
->createFeedTypeForCsv([
'title' => 'title',
'guid' => 'guid',
'alpha' => 'alpha',
], [
'mappings' => array_merge($this
->getDefaultMappings(), [
[
'target' => 'field_event',
'map' => [
'target_id' => 'alpha',
],
'settings' => [
'reference_by' => 'field_alpha',
],
],
]),
]);
$feed = $this
->createFeed($feed_type
->id(), [
'source' => $this
->resourcesPath() . '/csv/content.csv',
]);
$feed
->import();
$this
->assertNodeCount(4);
$expected_values_per_node = [
3 => [
[
'target_id' => 1,
],
],
4 => [],
];
foreach ($expected_values_per_node as $nid => $expected_value) {
$node = Node::load($nid);
$this
->assertEquals($expected_value, $node->field_event
->getValue());
}
}
}