You are here

public function SourcePluginsTest::testUpdateOnChangeInExtraSource in Feeds 8.3

Tests if an entity gets updated when a value in an extra source changes.

File

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

Class

SourcePluginsTest
Tests the behavior of Feeds source plugins.

Namespace

Drupal\Tests\feeds\Kernel

Code

public function testUpdateOnChangeInExtraSource() {

  // Create a feed type.
  $feed_type = $this
    ->createFeedType([
    'fetcher' => 'directory',
    'fetcher_configuration' => [
      'allowed_extensions' => 'atom rss rss1 rss2 opml xml',
    ],
    'processor_configuration' => [
      'authorize' => FALSE,
      'update_existing' => ProcessorInterface::UPDATE_EXISTING,
      'values' => [
        'type' => 'article',
      ],
    ],
    // Map the extra sources 'site:name' and 'site:slogan' to 'field_name' and
    // 'field_slogan'.
    'mappings' => array_merge($this
      ->getDefaultMappings(), [
      [
        'target' => 'field_name',
        'map' => [
          'value' => 'site:name',
        ],
        'settings' => [
          'format' => 'plain_text',
        ],
      ],
      [
        'target' => 'field_slogan',
        'map' => [
          'value' => 'site:slogan',
        ],
        'settings' => [
          'format' => 'plain_text',
        ],
      ],
    ]),
  ]);

  // Create a feed and import a file.
  $feed = $this
    ->createFeed($feed_type
    ->id(), [
    'source' => $this
      ->resourcesPath() . '/rss/googlenewstz.rss2',
  ]);
  $feed
    ->import();

  // Assert that 6 nodes have been created.
  static::assertEquals(6, $feed
    ->getItemCount());
  $this
    ->assertNodeCount(6);

  // Assert that on all 6 nodes, the site name and slogan are set.
  for ($i = 1; $i <= 6; $i++) {
    $node = Node::load($i);
    $this
      ->assertEquals('Feeds test site', $node->field_name->value);
    $this
      ->assertEquals('It feeds!', $node->field_slogan->value);
  }

  // Now update the site slogan and import again.
  $config = \Drupal::service('config.factory')
    ->getEditable('system.site');
  $config
    ->set('slogan', 'Feeds is awesome!');
  $config
    ->save();
  $feed
    ->import();

  // Assert that on all 6 nodes, the slogan is updated (and the site name is
  // not).
  for ($i = 1; $i <= 6; $i++) {
    $node = Node::load($i);
    $this
      ->assertEquals('Feeds test site', $node->field_name->value);
    $this
      ->assertEquals('Feeds is awesome!', $node->field_slogan->value);
  }
}