View source
<?php
namespace Drupal\Tests\feeds\FunctionalJavascript\Form;
use Drupal\filter\Entity\FilterFormat;
use Drupal\Tests\feeds\FunctionalJavascript\FeedsJavascriptTestBase;
class MappingFormTest extends FeedsJavascriptTestBase {
public function testSetMultipleMappingsWithCustomSources() {
$feed_type = $this
->createFeedTypeForCsv([], [
'mappings' => [],
]);
node_add_body_field($this->nodeType);
$format = FilterFormat::create([
'format' => 'empty_format',
'name' => 'Empty format',
]);
$format
->save();
$rid = $this
->createRole([
$format
->getPermissionName(),
]);
$this->adminUser
->addRole($rid);
$this->adminUser
->save();
$this
->drupalGet('/admin/structure/feeds/manage/' . $feed_type
->id() . '/mapping');
$session = $this
->getSession();
$assert_session = $this
->assertSession();
$page = $session
->getPage();
$mappings = [
[
'target' => 'body',
'map' => [
'value' => [
'value' => 'body',
'machine_name' => 'body_',
],
],
'settings' => [
'format' => $format
->id(),
],
],
[
'target' => 'title',
'map' => [
'value' => [
'value' => 'title',
'machine_name' => 'title_',
],
],
],
];
$edit = $this
->mappingGetEditValues($mappings);
foreach ($mappings as $i => $mapping) {
$assert_session
->fieldExists('add_target');
$page
->selectFieldOption('add_target', $mapping['target']);
$assert_session
->assertWaitOnAjaxRequest();
foreach ($mapping['map'] as $key => $source) {
if (is_array($source)) {
$assert_session
->fieldExists("mappings[{$i}][map][{$key}][select]");
$page
->selectFieldOption("mappings[{$i}][map][{$key}][select]", '__new');
}
}
}
$this
->mappingSetMappings($edit);
foreach ($mappings as $i => $mapping) {
if (!empty($mapping['settings'])) {
$this
->mappingSetTargetConfiguration($i, $mapping['settings']);
}
}
$submit_button = $assert_session
->buttonExists('Save');
$form = $assert_session
->elementExists('xpath', './ancestor::form', $submit_button);
$this
->prepareRequest();
$submit_button
->press();
$this
->refreshVariables();
$feed_type = $this
->reloadEntity($feed_type);
$this
->assertMappings($mappings, $feed_type);
}
public function testCustomSourceUniqueForUnsavedMappings() {
$feed_type = $this
->createFeedTypeForCsv([], [
'mappings' => [],
]);
$this
->createFieldWithStorage('field_alpha');
$mappings = [
[
'target' => 'title',
'map' => [
'value' => [
'value' => 'title',
'machine_name' => 'source_1',
],
],
],
[
'target' => 'field_alpha',
'map' => [
'value' => [
'value' => 'alpha',
'machine_name' => 'source_1',
],
],
],
];
$edit = $this
->mappingGetEditValues($mappings);
$this
->drupalGet('/admin/structure/feeds/manage/' . $feed_type
->id() . '/mapping');
$session = $this
->getSession();
$assert_session = $this
->assertSession();
$page = $session
->getPage();
foreach ($mappings as $i => $mapping) {
$assert_session
->fieldExists('add_target');
$page
->selectFieldOption('add_target', $mapping['target']);
$assert_session
->assertWaitOnAjaxRequest();
foreach ($mapping['map'] as $key => $source) {
if (is_array($source)) {
$assert_session
->fieldExists("mappings[{$i}][map][{$key}][select]");
$page
->selectFieldOption("mappings[{$i}][map][{$key}][select]", '__new');
}
}
}
$this
->mappingSetMappings($edit);
$this
->submitForm($edit, 'Save');
$assert_session
->pageTextContains('The machine-readable name is already in use. It must be unique.');
}
public function testEntityIdTargetIsUniqueByDefault() {
$feed_type = $this
->createFeedType();
$this
->drupalGet('/admin/structure/feeds/manage/' . $feed_type
->id() . '/mapping');
$session = $this
->getSession();
$assert_session = $this
->assertSession();
$page = $session
->getPage();
$assert_session
->fieldExists('add_target');
$page
->selectFieldOption('add_target', 'nid');
$assert_session
->assertWaitOnAjaxRequest();
$assert_session
->fieldValueEquals('mappings[2][unique][value]', '1');
$this
->submitForm([], 'Save');
$assert_session
->pageTextNotContains('When mapping to the entity ID (ID), it is recommended to set it as unique.');
$feed_type = $this
->reloadEntity($feed_type);
$mapping = $feed_type
->getMappings()[2];
$this
->assertEquals(1, $mapping['unique']['value']);
}
}