View source
<?php
namespace Drupal\Tests\feeds\Kernel;
use Drupal\feeds\Plugin\Type\Processor\ProcessorInterface;
use Drupal\node\Entity\Node;
class FeedsItemTest extends FeedsKernelTestBase {
protected function setUp() {
parent::setUp();
$this
->setUpBodyField();
}
public function testUpdateItemWithFeedsItem() {
$feed_type = $this
->createFeedType([
'fetcher' => 'directory',
'fetcher_configuration' => [
'allowed_extensions' => 'csv',
],
'parser' => 'csv',
'processor_configuration' => [
'update_existing' => ProcessorInterface::UPDATE_EXISTING,
'authorize' => FALSE,
'values' => [
'type' => 'article',
],
],
'custom_sources' => [
'guid' => [
'label' => 'guid',
'value' => 'guid',
'machine_name' => 'guid',
],
'title' => [
'label' => 'title',
'value' => 'title',
'machine_name' => 'title',
],
'body' => [
'label' => 'body',
'value' => 'body',
'machine_name' => 'body',
],
],
'mappings' => array_merge($this
->getDefaultMappings(), [
[
'target' => 'body',
'map' => [
'value' => 'body',
'summary' => '',
],
'settings' => [
'format' => 'plain_text',
],
],
]),
]);
$feed = $this
->createFeed($feed_type
->id(), [
'source' => $this
->resourcesPath() . '/csv/content.csv',
]);
$feed
->import();
$this
->assertNodeCount(2);
$feed
->setSource($this
->resourcesPath() . '/csv/content_updated.csv');
$feed
->save();
$feed
->import();
$node = Node::load(1);
$this
->assertEquals('Lorem ipsum dolor sit amet.', $node->body->value);
$node = Node::load(2);
$this
->assertEquals('Ut wisi enim ad minim veniam.', $node->body->value);
}
public function testUpdateItemsWithoutFeedsItem() {
$feed_type = $this
->createFeedType([
'fetcher' => 'directory',
'fetcher_configuration' => [
'allowed_extensions' => 'csv',
],
'parser' => 'csv',
'processor_configuration' => [
'update_existing' => ProcessorInterface::UPDATE_EXISTING,
'authorize' => FALSE,
'values' => [
'type' => 'article',
],
],
'custom_sources' => [
'title' => [
'label' => 'title',
'value' => 'title',
'machine_name' => 'title',
],
'body' => [
'label' => 'body',
'value' => 'body',
'machine_name' => 'body',
],
],
'mappings' => [
[
'target' => 'title',
'map' => [
'value' => 'title',
],
'unique' => [
'value' => TRUE,
],
],
[
'target' => 'body',
'map' => [
'value' => 'body',
'summary' => '',
],
'settings' => [
'format' => 'plain_text',
],
],
],
]);
$feed = $this
->createFeed($feed_type
->id(), [
'source' => $this
->resourcesPath() . '/csv/content.csv',
]);
$feed
->import();
$this
->assertNodeCount(2);
$feed
->setSource($this
->resourcesPath() . '/csv/content_updated.csv');
$feed
->save();
$feed
->import();
$node = Node::load(1);
$this
->assertEquals('Lorem ipsum dolor sit amet.', $node->body->value);
$node = Node::load(2);
$this
->assertEquals('Ut wisi enim ad minim veniam.', $node->body->value);
}
public function testDeleteFeed() {
$feed_type = $this
->createFeedType([
'fetcher' => 'directory',
'fetcher_configuration' => [
'allowed_extensions' => 'atom rss rss1 rss2 opml xml',
],
]);
$feed = $this
->createFeed($feed_type
->id(), [
'source' => $this
->resourcesPath() . '/rss/googlenewstz.rss2',
]);
$feed
->import();
$this
->assertNodeCount(6);
$feed
->delete();
$node = Node::load(1);
$this
->assertTrue($node->feeds_item
->isEmpty());
}
}