View source
<?php
namespace Drupal\Tests\feeds\Kernel;
use Drupal\feeds_test_events\EventSubscriber\FeedsSubscriber;
use Drupal\node\Entity\Node;
class FeedsEventsTest extends FeedsKernelTestBase {
public static $modules = [
'field',
'node',
'feeds',
'text',
'filter',
'feeds_test_events',
];
protected function assertEventSubscriberMessageOrder(array $messages) {
$positions = [];
foreach ($messages as $message) {
$position = array_search($message, $GLOBALS['feeds_test_events']);
if ($this
->assertTrue($position !== FALSE, $message)) {
$positions[] = $position;
}
}
$sorted = $positions;
sort($sorted);
$this
->assertTrue($sorted == $positions, 'The event subscriber messages appear in the correct order.');
}
public function testPrevalidateEvent() {
$feed_type = $this
->createFeedTypeForCsv([
'guid' => 'guid',
], [
'id' => 'my_feed_type',
'mappings' => [
[
'target' => 'feeds_item',
'map' => [
'guid' => 'guid',
],
'unique' => [
'guid' => TRUE,
],
],
],
]);
$feed = $this
->createFeed($feed_type
->id(), [
'source' => $this
->resourcesPath() . '/csv/content.csv',
]);
$feed
->import();
$messages = \Drupal::messenger()
->all();
$this
->assertStringContainsString('This value should not be null.', (string) $messages['warning'][0]);
$this
->assertNodeCount(0);
\Drupal::messenger()
->deleteAll();
$feed_type = $this
->createFeedTypeForCsv([
'guid' => 'guid',
], [
'id' => 'no_title',
'mappings' => [
[
'target' => 'feeds_item',
'map' => [
'guid' => 'guid',
],
'unique' => [
'guid' => TRUE,
],
],
],
]);
$feed = $this
->createFeed($feed_type
->id(), [
'source' => $this
->resourcesPath() . '/csv/content.csv',
]);
$feed
->import();
$messages = \Drupal::messenger()
->all();
$this
->assertArrayNotHasKey('warning', $messages);
$this
->assertNodeCount(2);
$node = Node::load(1);
$this
->assertEquals('foo', $node
->getTitle());
}
public function testSkipImportOnPresave() {
$feed_type = $this
->createFeedTypeForCsv([
'guid' => 'guid',
'title' => 'title',
], [
'id' => 'import_skip',
]);
$feed = $this
->createFeed($feed_type
->id(), [
'source' => $this
->resourcesPath() . '/csv/content.csv',
]);
$feed
->import();
$this
->assertNodeCount(1);
$node = Node::load(1);
$this
->assertEquals('Ut wisi enim ad minim veniam', $node
->getTitle());
}
public function testEventDispatchOrderOnImport() {
$GLOBALS['feeds_test_events'] = [];
$feed_type = $this
->createFeedTypeForCsv([
'guid' => 'guid',
'title' => 'title',
]);
$feed = $this
->createFeed($feed_type
->id(), [
'source' => $this
->resourcesPath() . '/csv/content.csv',
]);
$feed
->import();
$this
->assertEventSubscriberMessageOrder([
FeedsSubscriber::class . '::onInitImport called',
FeedsSubscriber::class . '::preFetch called',
FeedsSubscriber::class . '::postFetch called',
FeedsSubscriber::class . '::onInitImport called',
FeedsSubscriber::class . '::preParse called',
FeedsSubscriber::class . '::postParse called',
FeedsSubscriber::class . '::onInitImport called',
FeedsSubscriber::class . '::preProcess called',
FeedsSubscriber::class . '::prevalidate called',
FeedsSubscriber::class . '::preSave called',
FeedsSubscriber::class . '::postSave called',
FeedsSubscriber::class . '::postProcess called',
FeedsSubscriber::class . '::onInitImport called',
FeedsSubscriber::class . '::preProcess called',
FeedsSubscriber::class . '::prevalidate called',
FeedsSubscriber::class . '::preSave called',
FeedsSubscriber::class . '::postSave called',
FeedsSubscriber::class . '::postProcess called',
FeedsSubscriber::class . '::onFinish called',
]);
}
public function testEventDispatchOrderOnExpire() {
$feed_type = $this
->createFeedTypeForCsv([
'guid' => 'guid',
'title' => 'title',
], [
'processor_configuration' => [
'authorize' => FALSE,
'values' => [
'type' => 'article',
],
'expire' => 3600,
],
]);
$feed = $this
->createFeed($feed_type
->id(), [
'source' => $this
->resourcesPath() . '/csv/content.csv',
]);
$feed
->import();
for ($i = 1; $i <= 2; $i++) {
$node = Node::load($i);
$node->feeds_item->imported = \Drupal::service('datetime.time')
->getRequestTime() - 3601;
$node
->save();
}
$GLOBALS['feeds_test_events'] = [];
$feed
->startBatchExpire();
$batch =& batch_get();
$batch['progressive'] = FALSE;
batch_process();
$this
->assertEventSubscriberMessageOrder([
FeedsSubscriber::class . '::onInitExpire called',
FeedsSubscriber::class . '::onExpire called',
FeedsSubscriber::class . '::onInitExpire called',
FeedsSubscriber::class . '::onExpire called',
]);
}
public function testEventDispatchOrderOnClear() {
$feed_type = $this
->createFeedTypeForCsv([
'guid' => 'guid',
'title' => 'title',
]);
$feed = $this
->createFeed($feed_type
->id(), [
'source' => $this
->resourcesPath() . '/csv/content.csv',
]);
$feed
->import();
$GLOBALS['feeds_test_events'] = [];
$feed
->startBatchClear();
$batch =& batch_get();
$batch['progressive'] = FALSE;
batch_process();
$this
->assertEventSubscriberMessageOrder([
FeedsSubscriber::class . '::onInitClear called',
FeedsSubscriber::class . '::onClear called',
]);
}
}