You are here

public function CsvParserTest::testAlterCsvSource in Feeds 8.3

Tests if data from a CSV file can be altered with an event subscriber.

File

tests/src/Kernel/Feeds/Parser/CsvParserTest.php, line 107

Class

CsvParserTest
@coversDefaultClass \Drupal\feeds\Feeds\Parser\CsvParser @group feeds

Namespace

Drupal\Tests\feeds\Kernel\Feeds\Parser

Code

public function testAlterCsvSource() {

  // Create a text field.
  $this
    ->createFieldWithStorage('field_a_la_carte');
  $feed_type = $this
    ->createFeedTypeForCsv([
    'unique_identifier' => 'Unique identifier',
    'service_description' => 'Service Description',
    'facade' => 'Façade',
    'apres_ski' => 'Après-ski',
    'a_la_carte' => 'à la carte',
  ], [
    // The module 'feeds_test_alter_source' alters the data for the feed type
    // 'csv'. In there, the title is converted to lower case and only the
    // first word is taken for 'à la carte'.
    'id' => 'csv',
    'mappings' => [
      [
        'target' => 'feeds_item',
        'map' => [
          'guid' => 'unique_identifier',
        ],
      ],
      [
        'target' => 'title',
        'map' => [
          'value' => 'service_description',
        ],
      ],
      [
        'target' => 'field_a_la_carte',
        'map' => [
          'value' => 'a_la_carte',
        ],
        'settings' => [
          'format' => 'plain_text',
        ],
      ],
    ],
  ]);

  // Create a feed and import file.
  $feed = $this
    ->createFeed($feed_type
    ->id(), [
    'source' => $this
      ->resourcesPath() . '/csv/with-non-machine-name-column-names.csv',
  ]);
  $feed
    ->import();

  // Assert that 1 node has been created.
  static::assertEquals(1, $feed
    ->getItemCount());
  $this
    ->assertNodeCount(1);

  // Check the values on the node.
  $node = Node::load(1);
  $this
    ->assertEquals(1, $node->feeds_item->guid);
  $this
    ->assertSame('window washer, chimney sweeper', $node->title->value);
  $this
    ->assertSame('Salmon', $node->field_a_la_carte->value);
}