You are here

public function MappingFormTest::testCustomSourceUniqueForUnsavedMappings in Feeds 8.3

Tests that custom source names are unique for unsaved mappings.

File

tests/src/FunctionalJavascript/Form/MappingFormTest.php, line 112

Class

MappingFormTest
@coversDefaultClass \Drupal\feeds\Form\MappingForm @group feeds

Namespace

Drupal\Tests\feeds\FunctionalJavascript\Form

Code

public function testCustomSourceUniqueForUnsavedMappings() {

  // Create a feed type with no mappings.
  $feed_type = $this
    ->createFeedTypeForCsv([], [
    'mappings' => [],
  ]);

  // Create a text field called 'alpha'.
  $this
    ->createFieldWithStorage('field_alpha');

  // Create mappings to title, alpha.
  $mappings = [
    [
      'target' => 'title',
      'map' => [
        'value' => [
          'value' => 'title',
          'machine_name' => 'source_1',
        ],
      ],
    ],
    [
      'target' => 'field_alpha',
      'map' => [
        'value' => [
          'value' => 'alpha',
          // Give second source the same name (which is not allowed).
          'machine_name' => 'source_1',
        ],
      ],
    ],
  ];
  $edit = $this
    ->mappingGetEditValues($mappings);

  // Go to the mapping form.
  $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) {

    // Add target.
    $assert_session
      ->fieldExists('add_target');
    $page
      ->selectFieldOption('add_target', $mapping['target']);
    $assert_session
      ->assertWaitOnAjaxRequest();

    // Select sources.
    foreach ($mapping['map'] as $key => $source) {
      if (is_array($source)) {

        // Custom source.
        $assert_session
          ->fieldExists("mappings[{$i}][map][{$key}][select]");
        $page
          ->selectFieldOption("mappings[{$i}][map][{$key}][select]", '__new');
      }
    }
  }

  // Set the form values, including machine name.
  $this
    ->mappingSetMappings($edit);

  // Try to save the form.
  $this
    ->submitForm($edit, 'Save');

  // Assert that the double source name is detected.
  $assert_session
    ->pageTextContains('The machine-readable name is already in use. It must be unique.');
}