View source
<?php
namespace Drupal\Tests\feeds\Functional;
use Drupal\feeds\Event\FeedsEvents;
use Drupal\feeds\Event\ProcessEvent;
use Drupal\feeds\FeedTypeInterface;
use Drupal\feeds\Plugin\Type\Processor\ProcessorInterface;
class UpdateNonExistentTest extends FeedsBrowserTestBase {
protected $feedType;
protected function setUp() {
parent::setUp();
$this->feedType = $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',
],
],
]);
}
public function testUnpublishNonExistentItemsWithBatch() {
$config = $this->feedType
->getProcessor()
->getConfiguration();
$config['update_non_existent'] = 'entity:unpublish_action:node';
$this->feedType
->getProcessor()
->setConfiguration($config);
$this->feedType
->save();
$feed = $this
->createFeed($this->feedType
->id(), [
'source' => $this
->resourcesPath() . '/rss/googlenewstz.rss2',
]);
$this
->batchImport($feed);
$feed = $this
->reloadFeed($feed);
$this
->assertText('Created 6 Article items.');
static::assertEquals(6, $feed
->getItemCount());
$this
->assertNodeCount(6);
$feed
->setSource($this
->resourcesPath() . '/rss/googlenewstz_missing.rss2');
$feed
->save();
$this
->batchImport($feed);
$node = $this
->getNodeByTitle('Egypt, Hamas exchange fire on Gaza frontier, 1 dead - Reuters');
$this
->assertFalse($node
->isPublished());
$node->status = 1;
$node
->setTitle('Lorem');
$node
->save();
$this
->assertTrue($node
->isPublished(), 'Node is published');
$this
->batchImport($feed);
$node = $this
->reloadEntity($node);
$this
->assertTrue($node
->isPublished(), 'Node is not updated');
$feed = $this
->reloadFeed($feed);
$feed
->setSource($this
->resourcesPath() . '/rss/googlenewstz.rss2');
$feed
->save();
$this
->batchImport($feed);
$node = $this
->reloadEntity($node);
$this
->assertText('Updated 1 Article.');
static::assertEquals('Egypt, Hamas exchange fire on Gaza frontier, 1 dead - Reuters', $node
->getTitle());
}
public function testDeleteNonExistentItemsWithBatch() {
$config = $this->feedType
->getProcessor()
->getConfiguration();
$config['update_non_existent'] = ProcessorInterface::DELETE_NON_EXISTENT;
$this->feedType
->getProcessor()
->setConfiguration($config);
$this->feedType
->save();
$feed = $this
->createFeed($this->feedType
->id(), [
'source' => $this
->resourcesPath() . '/rss/googlenewstz.rss2',
]);
$this
->batchImport($feed);
$feed = $this
->reloadFeed($feed);
$this
->assertText('Created 6 Article items.');
static::assertEquals(6, $feed
->getItemCount());
$this
->assertNodeCount(6);
$feed
->setSource($this
->resourcesPath() . '/rss/googlenewstz_missing.rss2');
$feed
->save();
$this
->batchImport($feed);
$feed = $this
->reloadFeed($feed);
$this
->assertText('Cleaned 1 Article.');
static::assertEquals(5, $feed
->getItemCount());
$this
->assertNodeCount(5);
$feed
->setSource($this
->resourcesPath() . '/rss/googlenewstz.rss2');
$feed
->save();
$this
->batchImport($feed);
$feed = $this
->reloadFeed($feed);
$this
->assertText('Created 1 Article.');
static::assertEquals(6, $feed
->getItemCount());
$this
->assertNodeCount(6);
}
public function testUnpublishNonExistentItemsWithCron() {
$config = $this->feedType
->getProcessor()
->getConfiguration();
$config['update_non_existent'] = 'entity:unpublish_action:node';
$this->feedType
->getProcessor()
->setConfiguration($config);
$this->feedType
->setImportPeriod(FeedTypeInterface::SCHEDULE_CONTINUOUSLY);
$this->feedType
->save();
$feed = $this
->createFeed($this->feedType
->id(), [
'source' => $this
->resourcesPath() . '/rss/googlenewstz.rss2',
]);
$this
->cronRun();
$feed = $this
->reloadFeed($feed);
static::assertEquals(6, $feed
->getItemCount());
$this
->assertNodeCount(6);
$feed
->setSource($this
->resourcesPath() . '/rss/googlenewstz_missing.rss2');
$feed
->save();
$this
->cronRun();
$node = $this
->getNodeByTitle('Egypt, Hamas exchange fire on Gaza frontier, 1 dead - Reuters');
$this
->assertFalse($node
->isPublished());
$node->status = 1;
$node
->setTitle('Lorem');
$node
->save();
$this
->assertTrue($node
->isPublished(), 'Node is published');
$this
->cronRun();
$node = $this
->reloadEntity($node);
$this
->assertTrue($node
->isPublished(), 'Node is not updated');
$feed = $this
->reloadFeed($feed);
$feed
->setSource($this
->resourcesPath() . '/rss/googlenewstz.rss2');
$feed
->save();
$this
->cronRun();
$node = $this
->reloadEntity($node);
static::assertEquals('Egypt, Hamas exchange fire on Gaza frontier, 1 dead - Reuters', $node
->getTitle());
}
public function testDeleteNonExistentItemsWithCron() {
$config = $this->feedType
->getProcessor()
->getConfiguration();
$config['update_non_existent'] = ProcessorInterface::DELETE_NON_EXISTENT;
$this->feedType
->getProcessor()
->setConfiguration($config);
$this->feedType
->setImportPeriod(FeedTypeInterface::SCHEDULE_CONTINUOUSLY);
$this->feedType
->save();
$feed = $this
->createFeed($this->feedType
->id(), [
'source' => $this
->resourcesPath() . '/rss/googlenewstz.rss2',
]);
$this
->cronRun();
$feed = $this
->reloadFeed($feed);
static::assertEquals(6, $feed
->getItemCount());
$this
->assertNodeCount(6);
$feed
->setSource($this
->resourcesPath() . '/rss/googlenewstz_missing.rss2');
$feed
->save();
$this
->cronRun();
$feed = $this
->reloadFeed($feed);
static::assertEquals(5, $feed
->getItemCount());
$this
->assertNodeCount(5);
$feed
->setSource($this
->resourcesPath() . '/rss/googlenewstz.rss2');
$feed
->save();
$this
->cronRun();
$feed = $this
->reloadFeed($feed);
static::assertEquals(6, $feed
->getItemCount());
$this
->assertNodeCount(6);
}
public function testMultithreadImport() {
$config = $this->feedType
->getProcessor()
->getConfiguration();
$config['update_non_existent'] = ProcessorInterface::DELETE_NON_EXISTENT;
$this->feedType
->getProcessor()
->setConfiguration($config);
$this->feedType
->setImportPeriod(FeedTypeInterface::SCHEDULE_CONTINUOUSLY);
$this->feedType
->save();
$feed = $this
->createFeed($this->feedType
->id(), [
'source' => $this
->resourcesPath() . '/rss/googlenewstz.rss2',
]);
$this
->cronRun();
$feed = $this
->reloadFeed($feed);
static::assertEquals(6, $feed
->getItemCount());
$this
->assertNodeCount(6);
$feed
->setSource($this
->resourcesPath() . '/rss/googlenewstz_missing.rss2');
$feed
->save();
$feed
->startCronImport();
$queue_name = 'feeds_feed_refresh:' . $this->feedType
->id();
$queue = $this->container
->get('queue')
->get($queue_name);
$queue
->createQueue();
$queue_worker = $this->container
->get('plugin.manager.queue_worker')
->createInstance($queue_name);
for ($i = 0; $i < 3; $i++) {
$item = $queue
->claimItem();
$queue_worker
->processItem($item->data);
$queue
->deleteItem($item);
}
$this->container
->get('event_dispatcher')
->addListener(FeedsEvents::PROCESS, [
$this,
'onProcess',
], FeedsEvents::AFTER);
$item = $queue
->claimItem();
$queue_worker
->processItem($item->data);
$queue
->deleteItem($item);
while ($item = $queue
->claimItem()) {
$queue_worker
->processItem($item->data);
$queue
->deleteItem($item);
}
$feed = $this
->reloadFeed($feed);
static::assertEquals(5, $feed
->getItemCount());
$this
->assertNodeCount(5);
}
public function testWithNonExistentActionPlugin() {
$config = $this->feedType
->getProcessor()
->getConfiguration();
$config['update_non_existent'] = 'foo';
$this->feedType
->getProcessor()
->setConfiguration($config);
$this->feedType
->save();
$feed = $this
->createFeed($this->feedType
->id(), [
'source' => $this
->resourcesPath() . '/rss/googlenewstz.rss2',
]);
$this
->batchImport($feed);
$feed = $this
->reloadFeed($feed);
$this
->assertText('Created 6 Article items.');
static::assertEquals(6, $feed
->getItemCount());
$this
->assertNodeCount(6);
$feed
->setSource($this
->resourcesPath() . '/rss/googlenewstz_missing.rss2');
$feed
->save();
$this
->batchImport($feed);
$this
->assertText('Cleaning Egypt, Hamas exchange fire on Gaza frontier, 1 dead - Reuters failed because of non-existing action plugin foo.');
$this
->batchImport($feed);
$this
->assertText('Cleaning Egypt, Hamas exchange fire on Gaza frontier, 1 dead - Reuters failed because of non-existing action plugin foo.');
}
public function onProcess(ProcessEvent $event) {
$feed_type_id = $event
->getFeed()
->getType()
->id();
$queue_name = 'feeds_feed_refresh:' . $feed_type_id;
$queue = $this->container
->get('queue')
->get($queue_name);
$queue_worker = $this->container
->get('plugin.manager.queue_worker')
->createInstance($queue_name);
$item = $queue
->claimItem();
if ($item) {
$queue_worker
->processItem($item->data);
$queue
->deleteItem($item);
}
}
}