View source
<?php
namespace Drupal\Tests\feeds\Kernel;
use Drupal\feeds\Event\FeedsEvents;
use Drupal\feeds\Event\ImportFinishedEvent;
use Drupal\feeds\Plugin\Type\Processor\ProcessorInterface;
use Drupal\feeds\StateInterface;
use Drupal\node\Entity\Node;
class SkipNewTest extends FeedsKernelTestBase {
protected $processState;
protected function setUp() {
parent::setUp();
$this->container
->get('event_dispatcher')
->addListener(FeedsEvents::IMPORT_FINISHED, [
$this,
'importFinished',
]);
$this
->setUpBodyField();
}
public function importFinished(ImportFinishedEvent $event) {
$this->processState = $event
->getFeed()
->getState(StateInterface::PROCESS);
}
protected function createFeedTypeForThisTest(array $processor_configuration = []) {
return $this
->createFeedType([
'fetcher' => 'directory',
'fetcher_configuration' => [
'allowed_extensions' => 'rss2',
],
'processor_configuration' => $processor_configuration + [
'authorize' => FALSE,
'values' => [
'type' => 'article',
],
],
'mappings' => [
[
'target' => 'title',
'map' => [
'value' => 'title',
],
'unique' => [
'value' => TRUE,
],
'settings' => [
'language' => NULL,
],
],
[
'target' => 'body',
'map' => [
'value' => 'description',
],
'settings' => [
'format' => 'plain_text',
'language' => NULL,
],
],
],
]);
}
public function testSkipNewItems() {
$feed_type = $this
->createFeedTypeForThisTest([
'insert_new' => ProcessorInterface::SKIP_NEW,
]);
$feed = $this
->createFeed($feed_type
->id(), [
'source' => $this
->resourcesPath() . '/rss/drupalplanet.rss2',
]);
$feed
->import();
$this
->assertNodeCount(0);
$this
->assertEquals(0, $this->processState->created);
$this
->assertEquals(25, $this->processState->skipped);
}
public function testSkipNewAndSkipExisting() {
$feed_type = $this
->createFeedTypeForThisTest([
'insert_new' => ProcessorInterface::SKIP_NEW,
'update_existing' => ProcessorInterface::SKIP_EXISTING,
]);
$node1 = Node::create([
'type' => 'article',
'title' => 'Dries Buytaert: Eén using Drupal',
'body' => 'Foo',
]);
$node1
->save();
$node2 = Node::create([
'type' => 'article',
'title' => 'NodeOne: The new Feeds module',
'body' => 'Feeds exists for more than a decade now.',
]);
$node2
->save();
$feed = $this
->createFeed($feed_type
->id(), [
'source' => $this
->resourcesPath() . '/rss/drupalplanet.rss2',
]);
$feed
->import();
$this
->assertEquals(0, $feed
->getItemCount());
$this
->assertNodeCount(2);
$this
->assertEquals(0, $this->processState->created);
$this
->assertEquals(0, $this->processState->updated);
$this
->assertEquals(25, $this->processState->skipped);
$node1 = $this
->reloadEntity($node1);
$this
->assertEquals('Foo', $node1->body->value);
$this
->assertEmpty($node1->feeds_item);
$node2 = $this
->reloadEntity($node2);
$this
->assertEquals('Feeds exists for more than a decade now.', $node2->body->value);
$this
->assertEmpty($node2->feeds_item);
}
public function testSkipNewAndUpdateExisting() {
$feed_type = $this
->createFeedTypeForThisTest([
'insert_new' => ProcessorInterface::SKIP_NEW,
'update_existing' => ProcessorInterface::UPDATE_EXISTING,
]);
$node1 = Node::create([
'type' => 'article',
'title' => 'Dries Buytaert: Eén using Drupal',
'body' => 'Foo',
]);
$node1
->save();
$node2 = Node::create([
'type' => 'article',
'title' => 'NodeOne: The new Feeds module',
'body' => 'Feeds exists for more than a decade now.',
]);
$node2
->save();
$feed = $this
->createFeed($feed_type
->id(), [
'source' => $this
->resourcesPath() . '/rss/drupalplanet.rss2',
]);
$feed
->import();
$this
->assertEquals(2, $feed
->getItemCount());
$this
->assertNodeCount(2);
$this
->assertEquals(0, $this->processState->created);
$this
->assertEquals(2, $this->processState->updated);
$node1 = $this
->reloadEntity($node1);
$this
->assertStringContainsString('a public TV station reaching millions of people in Belgium', $node1->body->value);
$this
->assertNotEmpty($node1->feeds_item->imported);
$node2 = $this
->reloadEntity($node2);
$this
->assertStringContainsString('FeedAPI has for long been the mainstream solution for this kind of problems.', $node2->body->value);
$this
->assertNotEmpty($node2->feeds_item->imported);
$config = $feed_type
->getProcessor()
->getConfiguration();
$config['insert_new'] = ProcessorInterface::INSERT_NEW;
$feed_type
->getProcessor()
->setConfiguration($config);
$feed_type
->save();
$feed = $this
->reloadEntity($feed);
$feed
->import();
$this
->assertEquals(25, $feed
->getItemCount());
$this
->assertNodeCount(25);
$this
->assertEquals(23, $this->processState->created);
$this
->assertEquals(0, $this->processState->updated);
$this
->assertEquals(2, $this->processState->skipped);
}
}