You are here

public function HttpFetcherTest::testHttpImport in Feeds 8.3

Tests importing a RSS feed using the HTTP fetcher.

File

tests/src/Functional/Feeds/Fetcher/HttpFetcherTest.php, line 108

Class

HttpFetcherTest
@coversDefaultClass \Drupal\feeds\Feeds\Fetcher\HttpFetcher @group feeds

Namespace

Drupal\Tests\feeds\Functional\Feeds\Fetcher

Code

public function testHttpImport() {
  $filepath = drupal_get_path('module', 'feeds') . '/tests/resources/rss/googlenewstz.rss2';
  $feed = $this
    ->createFeed($this->feedType
    ->id(), [
    'source' => $this
      ->resourcesUrl() . '/rss/googlenewstz.rss2',
  ]);
  $this
    ->drupalGet('feed/' . $feed
    ->id());
  $this
    ->clickLink(t('Import'));
  $this
    ->drupalPostForm(NULL, [], t('Import'));
  $this
    ->assertText('Created 6');
  $this
    ->assertNodeCount(6);
  $xml = new SimpleXMLElement($filepath, 0, TRUE);
  $expected_terms = [
    1 => [],
    2 => [
      'Top Stories',
    ],
    3 => [
      'Top Stories',
    ],
    4 => [
      'Top Stories 2',
    ],
    5 => [
      'Top Stories 2',
    ],
    6 => [
      'Top Stories 3',
    ],
  ];
  foreach (range(1, 6) as $nid) {
    $item = $xml->channel->item[$nid - 1];
    $node = Node::load($nid);
    $this
      ->assertEquals($node->title->value, (string) $item->title);
    $this
      ->assertEquals($node->body->value, (string) $item->description);
    $this
      ->assertEquals($node->feeds_item->guid, (string) $item->guid);
    $this
      ->assertEquals($node->feeds_item->url, (string) $item->link);
    $this
      ->assertEquals($node->created->value, strtotime((string) $item->pubDate));
    $terms = [];
    foreach ($node->field_tags
      ->referencedEntities() as $term) {
      $terms[] = $term
        ->label();
    }
    $this
      ->assertEquals($expected_terms[$nid], $terms);
  }

  // Test cache.
  $this
    ->drupalPostForm('feed/' . $feed
    ->id() . '/import', [], t('Import'));
  $this
    ->assertText('The feed has not been updated.');

  // Import again.
  \Drupal::cache('feeds_download')
    ->deleteAll();
  $this
    ->drupalPostForm('feed/' . $feed
    ->id() . '/import', [], t('Import'));
  $this
    ->assertText('There are no new');

  // Test force-import.
  \Drupal::cache('feeds_download')
    ->deleteAll();
  $configuration = $this->feedType
    ->getProcessor()
    ->getConfiguration();
  $configuration['skip_hash_check'] = TRUE;
  $configuration['update_existing'] = ProcessorInterface::UPDATE_EXISTING;
  $this->feedType
    ->getProcessor()
    ->setConfiguration($configuration);
  $this->feedType
    ->save();
  $this
    ->drupalPostForm('feed/' . $feed
    ->id() . '/import', [], t('Import'));
  $this
    ->assertNodeCount(6);
  $this
    ->assertText('Updated 6');

  // Delete items.
  $this
    ->clickLink(t('Delete items'));
  $this
    ->drupalPostForm(NULL, [], t('Delete items'));
  $this
    ->assertNodeCount(0);
  $this
    ->assertText('Deleted 6');
}