You are here

public function FeedsEventsTest::testPrevalidateEvent in Feeds 8.3

Ensure that the prevalidate event is dispatched at the right moment.

File

tests/src/Kernel/FeedsEventsTest.php, line 55

Class

FeedsEventsTest
Tests for dispatching feeds events.

Namespace

Drupal\Tests\feeds\Kernel

Code

public function testPrevalidateEvent() {

  // Create a feed type. Do not map to 'title'.
  $feed_type = $this
    ->createFeedTypeForCsv([
    'guid' => 'guid',
  ], [
    'id' => 'my_feed_type',
    'mappings' => [
      [
        'target' => 'feeds_item',
        'map' => [
          'guid' => 'guid',
        ],
        'unique' => [
          'guid' => TRUE,
        ],
      ],
    ],
  ]);

  // Try to import a feed.
  $feed = $this
    ->createFeed($feed_type
    ->id(), [
    'source' => $this
      ->resourcesPath() . '/csv/content.csv',
  ]);
  $feed
    ->import();

  // Ensure that the import failed because of validation errors.
  $messages = \Drupal::messenger()
    ->all();
  $this
    ->assertStringContainsString('This value should not be null.', (string) $messages['warning'][0]);
  $this
    ->assertNodeCount(0);

  // Clear messages.
  \Drupal::messenger()
    ->deleteAll();

  // Now create a feed type with the same settings. This time, ensure that
  // \Drupal\feeds_test_events\EventSubscriber::prevalidate() sets a title on
  // the entity, which it does only for the feed type 'no_title'.
  $feed_type = $this
    ->createFeedTypeForCsv([
    'guid' => 'guid',
  ], [
    'id' => 'no_title',
    'mappings' => [
      [
        'target' => 'feeds_item',
        'map' => [
          'guid' => 'guid',
        ],
        'unique' => [
          'guid' => TRUE,
        ],
      ],
    ],
  ]);

  // Try to import a feed.
  $feed = $this
    ->createFeed($feed_type
    ->id(), [
    'source' => $this
      ->resourcesPath() . '/csv/content.csv',
  ]);
  $feed
    ->import();

  // Assert that there are no warnings this time.
  $messages = \Drupal::messenger()
    ->all();
  $this
    ->assertArrayNotHasKey('warning', $messages);

  // Assert that 2 nodes were created.
  $this
    ->assertNodeCount(2);

  // Check title of the first created node.
  $node = Node::load(1);
  $this
    ->assertEquals('foo', $node
    ->getTitle());
}