View source
<?php
namespace Drupal\Tests\feeds\Kernel\Feeds\Target;
use Drupal\feeds\Plugin\Type\Processor\ProcessorInterface;
use Drupal\node\Entity\Node;
use Drupal\Tests\feeds\Kernel\FeedsKernelTestBase;
class FieldTest extends FeedsKernelTestBase {
protected $feedType;
protected function setUp() {
parent::setUp();
$this
->setUpBodyField();
$this
->createFieldWithStorage('field_alpha');
$this
->createFieldWithStorage('field_beta', [
'type' => 'integer',
]);
$this
->createFieldWithStorage('field_gamma', [
'type' => 'decimal',
]);
$this
->createFieldWithStorage('field_delta', [
'type' => 'float',
]);
$this->feedType = $this
->createFeedTypeForCsv([
'title' => 'title',
'body' => 'body',
'alpha' => 'alpha',
'beta' => 'beta',
'gamma' => 'gamma',
'delta' => 'delta',
], [
'mappings' => [
[
'target' => 'title',
'map' => [
'value' => 'title',
],
],
[
'target' => 'body',
'map' => [
'value' => 'body',
],
'settings' => [
'format' => 'plain_text',
],
],
[
'target' => 'field_alpha',
'map' => [
'value' => 'alpha',
],
],
[
'target' => 'field_beta',
'map' => [
'value' => 'beta',
],
],
[
'target' => 'field_gamma',
'map' => [
'value' => 'gamma',
],
],
[
'target' => 'field_delta',
'map' => [
'value' => 'delta',
],
],
],
]);
}
protected function setUpFieldDisplay() {
$this
->installConfig([
'system',
]);
$formats = $this->container
->get('entity_type.manager')
->getStorage('date_format')
->loadMultiple([
'long',
'medium',
'short',
]);
$formats['long']
->setPattern('l, j. F Y - G:i')
->save();
$formats['medium']
->setPattern('j. F Y - G:i')
->save();
$formats['short']
->setPattern('Y M j - g:ia')
->save();
$this->container
->get('entity_display.repository')
->getViewDisplay('node', 'article', 'default')
->setComponent('field_alpha', [
'type' => 'text_default',
'label' => 'above',
])
->setComponent('field_beta', [
'type' => 'number_integer',
'label' => 'above',
])
->setComponent('field_gamma', [
'type' => 'number_decimal',
'label' => 'above',
])
->setComponent('field_delta', [
'type' => 'number_decimal',
'label' => 'above',
])
->save();
}
public function test() {
$feed = $this
->createFeed($this->feedType
->id(), [
'source' => $this
->resourcesPath() . '/csv/content.csv',
]);
$feed
->import();
$this
->assertNodeCount(2);
$expected_values_per_node = [
1 => [
'body' => 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.',
'field_alpha' => 'Lorem',
'field_beta' => '42',
'field_gamma' => '4.20',
'field_delta' => '3.14159',
],
2 => [
'body' => 'Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.',
'field_alpha' => 'Ut wisi',
'field_beta' => '32',
'field_gamma' => '1.20',
'field_delta' => '5.62951',
],
];
$this
->checkValues($expected_values_per_node);
}
public function testClearOutValues() {
$this
->setUpFieldDisplay();
$this->feedType
->addCustomSource('guid', [
'label' => 'GUID',
'value' => 'guid',
]);
$this->feedType
->addMapping([
'target' => 'feeds_item',
'map' => [
'guid' => 'guid',
],
'unique' => [
'guid' => TRUE,
],
]);
$this->feedType
->save();
$feed = $this
->createFeed($this->feedType
->id(), [
'source' => $this
->resourcesPath() . '/csv/content.csv',
]);
$feed
->import();
$this
->assertNodeCount(2);
$expected_values_per_node = [
1 => [
'body' => 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.',
'field_alpha' => 'Lorem',
'field_beta' => '42',
'field_gamma' => '4.20',
'field_delta' => '3.14159',
],
2 => [
'body' => 'Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.',
'field_alpha' => 'Ut wisi',
'field_beta' => '32',
'field_gamma' => '1.20',
'field_delta' => '5.62951',
],
];
$this
->checkValues($expected_values_per_node);
$this->feedType
->getProcessor()
->setConfiguration([
'update_existing' => ProcessorInterface::UPDATE_EXISTING,
] + $this->feedType
->getProcessor()
->getConfiguration());
$this->feedType
->save();
$feed = $this
->createFeed($this->feedType
->id(), [
'source' => $this
->resourcesPath() . '/csv/content_empty.csv',
]);
$feed
->import();
$this
->assertNodeCount(2);
$expected_values_per_node_empty = [
1 => [
'body' => '',
'field_alpha' => '',
'field_beta' => '',
'field_gamma' => '',
'field_delta' => '',
],
2 => [
'body' => 0,
'field_alpha' => 0,
'field_beta' => 0,
'field_gamma' => 0,
'field_delta' => 0,
],
];
$this
->checkValues($expected_values_per_node_empty);
$field_labels = [
'field_alpha label',
'field_beta label',
'field_gamma label',
'field_delta label',
];
$rendered_content = $this
->renderNode(Node::load(1));
foreach ($field_labels as $label) {
$this
->assertStringNotContainsString($label, $rendered_content);
}
$rendered_content = $this
->renderNode(Node::load(2));
foreach ($field_labels as $label) {
$this
->assertStringContainsString($label, $rendered_content);
}
$feed = $this
->createFeed($this->feedType
->id(), [
'source' => $this
->resourcesPath() . '/csv/content.csv',
]);
$feed
->import();
$this
->checkValues($expected_values_per_node);
$feed = $this
->createFeed($this->feedType
->id(), [
'source' => $this
->resourcesPath() . '/csv/content_non_existent.csv',
]);
$feed
->import();
$expected_values_per_node_non_existent = [
1 => [
'body' => '',
'field_alpha' => '',
'field_beta' => '',
'field_gamma' => '',
'field_delta' => '',
],
];
$this
->checkValues($expected_values_per_node_non_existent);
$rendered_content = $this
->renderNode(Node::load(1));
foreach ($field_labels as $label) {
$this
->assertStringNotContainsString($label, $rendered_content);
}
}
public function testTargetUnique($field, $subfield, $delta, array $values = []) {
$expected_values = [
'body' => 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.',
'field_alpha' => 'Lorem',
'field_beta' => '42',
'field_gamma' => '4.20',
'field_delta' => '3.14159',
];
$mappings = $this->feedType
->getMappings();
$mappings[$delta]['unique'] = [
$subfield => TRUE,
];
$this->feedType
->setMappings($mappings);
$this->feedType
->getProcessor()
->setConfiguration([
'update_existing' => ProcessorInterface::UPDATE_EXISTING,
] + $this->feedType
->getProcessor()
->getConfiguration());
$this->feedType
->save();
$values += [
'title' => $this
->randomMachineName(8),
'type' => 'article',
'uid' => 0,
$field => isset($expected_values[$field]) ? $expected_values[$field] : NULL,
];
$node = Node::create($values);
$node
->save();
$feed = $this
->createFeed($this->feedType
->id(), [
'source' => $this
->resourcesPath() . '/csv/content.csv',
]);
$feed
->import();
$this
->assertNodeCount(2);
$node = $this
->reloadEntity($node);
foreach ($expected_values as $field_name => $value) {
$this
->assertEquals($value, $node->{$field_name}->value);
}
}
public function dataProviderTargetUnique() {
return [
[
'field_alpha',
'value',
2,
],
[
'field_beta',
'value',
3,
],
[
'field_gamma',
'value',
4,
],
];
}
public function testListIntegerTargetUnique() {
$this
->createFieldWithStorage('field_jota', [
'type' => 'list_integer',
'storage' => [
'settings' => [
'allowed_values' => [
1 => 'One',
2 => 'Two',
],
],
],
]);
$this->feedType = $this
->reloadEntity($this->feedType);
$this->feedType
->addCustomSource('guid', [
'label' => 'GUID',
'value' => 'guid',
]);
$this->feedType
->addMapping([
'target' => 'field_jota',
'map' => [
'value' => 'guid',
],
'unique' => [
'value' => TRUE,
],
]);
$this
->testTargetUnique('field_jota', 'value', 6, [
'field_jota' => 1,
]);
}
protected function checkValues(array $expected_values_per_node) {
foreach ($expected_values_per_node as $node_id => $expected_values) {
$node = Node::load($node_id);
foreach ($expected_values as $field_name => $value) {
$this
->assertEquals($value, $node->{$field_name}->value);
}
}
}
protected function renderNode(Node $node) {
$display = \Drupal::service('entity_display.repository')
->getViewDisplay($node
->getEntityTypeId(), $node
->bundle(), 'default');
$content = $display
->build($node);
return (string) $this->container
->get('renderer')
->renderRoot($content);
}
}