You are here

protected function FeedsJavascriptTestBase::addMappings in Feeds 8.3

Adds mappings to the given feed type via the UI.

Parameters

string $feed_type_id: The feed type ID.

array $mappings: An array of mapping arrays. Each mapping array can consist of the following keys:

  • target: (required) the target to map to.
  • map: (required) an array of mapping sources. The keys are expected to represent the name of the subtarget (in most cases 'value'). The value is the source key to map to. If the value is a string, it is expected to represent an existing or a predefined source. If it is an array, it is expected to represent a custom source. In this case, specify the details of the custom source:

    • value: (required) the value to extract from the feed.
    • label: (optional) the custom source's label.
    • machine_name: (required) the custom source's machine name.
  • unique: (optional) an array of mapping targets to set as unique or not. The keys are the name of the subtargets, the value is a boolean: 'true' for setting as unique, 'false' for not.

array $edit: (optional) Additional field values to submit.

bool $assert_mappings: (optional) Whether or not to assert the mappings. Defaults to true.

1 call to FeedsJavascriptTestBase::addMappings()
CsvParserTest::testMapCustomSource in tests/src/FunctionalJavascript/Feeds/Parser/CsvParserTest.php
Tests adding a custom mapping source.

File

tests/src/FunctionalJavascript/FeedsJavascriptTestBase.php, line 99

Class

FeedsJavascriptTestBase
Base class for Feeds javascript tests.

Namespace

Drupal\Tests\feeds\FunctionalJavascript

Code

protected function addMappings($feed_type_id, array $mappings, array $edit = [], $assert_mappings = TRUE) {
  $this
    ->drupalGet('/admin/structure/feeds/manage/' . $feed_type_id . '/mapping');
  $session = $this
    ->getSession();
  $assert_session = $this
    ->assertSession();
  $page = $session
    ->getPage();

  // Compose edit values.
  $edit += $this
    ->mappingGetEditValues($mappings);
  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 target configuration, if supplied.
    if (!empty($mapping['settings'])) {
      $this
        ->mappingSetTargetConfiguration($i, $mapping['settings']);
    }
  }

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

  // Assert that the mappings and custom sources were successfully added.
  if ($assert_mappings) {
    $feed_type = FeedType::load($feed_type_id);
    $feed_type = $this
      ->reloadEntity($feed_type);
    $this
      ->assertMappings($mappings, $feed_type);
  }
}