View source
<?php
namespace Drupal\Tests\feeds\Kernel;
use Drupal\feeds\Plugin\Type\Processor\ProcessorInterface;
class UpdateExistingTest extends FeedsKernelTestBase {
public static $modules = [
'field',
'node',
'user',
'feeds',
'text',
'filter',
'options',
'entity_test',
'feeds_test_entity',
];
public function testUpdateTermsInSameVocabulary() {
$this
->installTaxonomyModuleWithVocabulary();
$this->entityTypeManager
->getStorage('taxonomy_vocabulary')
->create([
'vid' => 'vocab2',
'name' => 'Vocabulary 2',
])
->save();
$term_storage = $this->entityTypeManager
->getStorage('taxonomy_term');
$tags_term1 = $term_storage
->create([
'name' => 'Lorem ipsum',
'vid' => 'tags',
]);
$tags_term1
->save();
$vocab2_term1 = $term_storage
->create([
'name' => 'Ut wisi enim ad minim veniam',
'description' => 'Wisi Wisi',
'vid' => 'vocab2',
]);
$vocab2_term1
->save();
$feed_type = $this
->createFeedTypeForCsv([
'title' => 'title',
'body' => 'body',
], [
'processor' => 'entity:taxonomy_term',
'processor_configuration' => [
'update_existing' => ProcessorInterface::UPDATE_EXISTING,
'values' => [
'vid' => 'vocab2',
],
],
'mappings' => [
[
'target' => 'name',
'map' => [
'value' => 'title',
],
'unique' => [
'value' => TRUE,
],
],
[
'target' => 'description',
'map' => [
'value' => 'body',
],
'settings' => [
'format' => 'plain_text',
],
],
],
]);
$feed = $this
->createFeed($feed_type
->id(), [
'source' => $this
->resourcesPath() . '/csv/content.csv',
]);
$feed
->import();
$term_count = $term_storage
->getQuery()
->condition('vid', 'vocab2')
->count()
->execute();
$this
->assertEquals(2, $term_count, 'Two terms exist in vocabulary vocab2.');
$tags_term1 = $this
->reloadEntity($tags_term1);
$this
->assertEquals('Lorem ipsum', $tags_term1
->getName());
$this
->assertEquals('', $tags_term1
->getDescription());
$term = $term_storage
->load(3);
$this
->assertEquals('Lorem ipsum', $term
->getName());
$this
->assertEquals('Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.', $term
->getDescription());
$vocab2_term1 = $this
->reloadEntity($vocab2_term1);
$this
->assertEquals('Ut wisi enim ad minim veniam', $vocab2_term1
->getName());
$this
->assertEquals('Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.', $vocab2_term1
->getDescription());
}
public function testUpdateNonTranslatableEntity() {
$this
->installConfig([
'field',
'filter',
]);
$this
->installEntitySchema('entity_test_bundle');
$this
->installEntitySchema('entity_test_string_id');
$this
->installEntitySchema('user');
$entity_storage = $this->entityTypeManager
->getStorage('entity_test_string_id');
$this
->createUser();
$this
->createFieldWithStorage('field_alpha', [
'entity_type' => 'entity_test_string_id',
'bundle' => 'entity_test_string_id',
]);
$entity = $entity_storage
->create([
'id' => 'LRM',
'name' => 'Lorem ipsum',
'type' => 'entity_test_string_id',
]);
$entity
->save();
$feed_type = $this
->createFeedTypeForCsv([
'title' => 'title',
'alpha' => 'alpha',
], [
'processor' => 'entity:entity_test_string_id',
'processor_configuration' => [
'update_existing' => ProcessorInterface::UPDATE_EXISTING,
'values' => [
'type' => 'entity_test_string_id',
],
],
'mappings' => [
[
'target' => 'name',
'map' => [
'value' => 'title',
],
'unique' => [
'value' => TRUE,
],
],
[
'target' => 'field_alpha',
'map' => [
'value' => 'alpha',
],
'settings' => [
'format' => 'plain_text',
],
],
],
]);
$feed = $this
->createFeed($feed_type
->id(), [
'source' => $this
->resourcesPath() . '/csv/content.csv',
]);
$feed
->import();
$entity = $this
->reloadEntity($entity);
$this
->assertEquals('Lorem', $entity->field_alpha->value);
}
public function testImportIntoFieldWithDefaultValue() {
$this
->installConfig([
'field',
]);
\Drupal::state()
->set('entity_test.boolean_field', TRUE);
$this
->installEntitySchema('entity_test_bundle');
$this
->installEntitySchema('feeds_test_entity_test_no_links');
$this
->installEntitySchema('user');
$account = $this
->createUser();
$feed_type = $this
->createFeedTypeForCsv([
'title' => 'title',
'epsilon' => 'epsilon',
], [
'processor' => 'entity:feeds_test_entity_test_no_links',
'processor_configuration' => [
'owner_id' => $account
->id(),
'values' => [
'type' => 'feeds_test_entity_test_no_links',
],
],
'mappings' => [
[
'target' => 'name',
'map' => [
'value' => 'title',
],
'unique' => [
'value' => TRUE,
],
],
[
'target' => 'boolean_field',
'map' => [
'value' => 'epsilon',
],
],
],
]);
$feed = $this
->createFeed($feed_type
->id(), [
'source' => $this
->resourcesPath() . '/csv/content.csv',
]);
$feed
->import();
$entity_storage = $this->entityTypeManager
->getStorage('feeds_test_entity_test_no_links');
$entity = $entity_storage
->load(1);
$this
->assertEquals('1', $entity->boolean_field->value);
}
}