View source
<?php
namespace Drupal\Tests\feeds\Kernel\Feeds\Processor;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\feeds\Feeds\Processor\EntityProcessorBase;
use Drupal\feeds\FeedInterface;
use Drupal\feeds\FeedTypeInterface;
use Drupal\feeds\Feeds\Item\ItemInterface;
use Drupal\feeds\Feeds\State\CleanState;
use Drupal\feeds\Feeds\Target\StringTarget;
use Drupal\feeds\FieldTargetDefinition;
use Drupal\feeds\State;
use Drupal\feeds\StateInterface;
use Drupal\Tests\feeds\Kernel\FeedsKernelTestBase;
class EntityProcessorBaseTest extends FeedsKernelTestBase {
protected $processor;
protected $feedType;
protected $feed;
protected $state;
public function setUp() {
parent::setUp();
$this->feedType = $this
->createMock(FeedTypeInterface::class);
$this->feedType
->expects($this
->any())
->method('getMappings')
->will($this
->returnValue([]));
$this->processor = $this
->getMockForAbstractClass(EntityProcessorBase::class, [
[
'values' => [
'type' => 'article',
],
'feed_type' => $this->feedType,
],
'entity:node',
[
'id' => 'entity:node',
'title' => 'Node',
'description' => 'Creates nodes from feed items.',
'entity_type' => 'node',
'arguments' => [
'@entity_type.manager',
'@entity_type.bundle.info',
],
'form' => [
'configuration' => 'Drupal\\feeds\\Feeds\\Processor\\Form\\DefaultEntityProcessorForm',
'option' => 'Drupal\\feeds\\Feeds\\Processor\\Form\\EntityProcessorOptionForm',
],
'class' => EntityProcessorBase::class,
'provider' => 'feeds',
'plugin_type' => 'processor',
],
\Drupal::service('entity_type.manager'),
\Drupal::service('entity_type.bundle.info'),
\Drupal::service('language_manager'),
]);
$this->feed = $this
->createMock(FeedInterface::class);
$this->feed
->expects($this
->any())
->method('id')
->will($this
->returnValue(1));
$this->feed
->expects($this
->any())
->method('getState')
->with(StateInterface::CLEAN)
->will($this
->returnValue(new CleanState($this->feed
->id())));
$this->state = new State();
$this
->installSchema('system', [
'key_value_expire',
]);
}
public function testProcess() {
$item = $this
->createMock(ItemInterface::class);
$item
->expects($this
->any())
->method('toArray')
->will($this
->returnValue([]));
$this->feedType
->expects($this
->any())
->method('getMappedSources')
->will($this
->returnValue([]));
$this->processor
->process($this->feed, $item, $this->state);
$this
->markTestIncomplete('Test is a stub.');
}
public function testCleanWithKeepNonExistent() {
$this
->callProtectedMethod($this->processor, 'prepareFeedsItemField');
$node = $this
->createNodeWithFeedsItem($this->feed);
$hash = $node->feeds_item->hash;
$this->processor
->clean($this->feed, $node, new CleanState($this->feed
->id()));
$this
->assertEquals($hash, $node->feeds_item->hash);
}
public function testCleanWithUnpublishAction() {
$config = $this->processor
->getConfiguration();
$config['update_non_existent'] = 'entity:unpublish_action:node';
$this->processor
->setConfiguration($config);
$this
->callProtectedMethod($this->processor, 'prepareFeedsItemField');
$node = $this
->createNodeWithFeedsItem($this->feed);
$this
->assertTrue($node
->isPublished());
$this->processor
->clean($this->feed, $node, new CleanState($this->feed
->id()));
$node = $this->container
->get('entity_type.manager')
->getStorage('node')
->load($node
->id());
$this
->assertFalse($node
->isPublished());
$this
->assertEquals('entity:unpublish_action:node', $node->feeds_item->hash);
}
public function testCleanWithDeleteAction() {
$config = $this->processor
->getConfiguration();
$config['update_non_existent'] = EntityProcessorBase::DELETE_NON_EXISTENT;
$this->processor
->setConfiguration($config);
$this
->callProtectedMethod($this->processor, 'prepareFeedsItemField');
$node = $this
->createNodeWithFeedsItem($this->feed);
$this
->assertNodeCount(1);
$this->processor
->clean($this->feed, $node, new CleanState($this->feed
->id()));
$this
->assertNodeCount(0);
}
public function testClear() {
$this
->markTestIncomplete('Test not yet implemented.');
$this->processor
->clear($this->feed, $this->state);
}
public function testEntityType() {
$this
->assertEquals('node', $this->processor
->entityType());
}
public function testBundleKey() {
$this
->assertEquals('type', $this->processor
->bundleKey());
}
public function testBundle() {
$this
->assertEquals('article', $this->processor
->bundle());
}
public function testBundleLabel() {
$this
->assertEquals('Content type', $this->processor
->bundleLabel());
}
public function testBundleOptions() {
$expected = [
'article' => 'Article',
];
$this
->assertEquals($expected, $this->processor
->bundleOptions());
}
public function testEntityTypeLabel() {
$this
->assertEquals('Content', $this->processor
->entityTypeLabel());
}
public function testEntityTypeLabelPlural() {
$this
->assertEquals('content items', $this->processor
->entityTypeLabelPlural());
}
public function testGetItemLabel() {
$this
->assertEquals('Article', $this->processor
->getItemLabel());
}
public function testGetItemLabelPlural() {
$this
->assertEquals('Article items', $this->processor
->getItemLabelPlural());
}
public function testDefaultConfiguration() {
$this
->assertIsArray($this->processor
->defaultConfiguration());
}
public function testOnFeedTypeSave() {
$this->processor
->onFeedTypeSave();
}
public function testOnFeedTypeDelete() {
$this->processor
->onFeedTypeDelete();
}
public function testExpiryTime() {
$this
->assertEquals(EntityProcessorBase::EXPIRE_NEVER, $this->processor
->expiryTime());
$config = $this->processor
->getConfiguration();
$config['expire'] = 100;
$this->processor
->setConfiguration($config);
$this
->assertEquals(100, $this->processor
->expiryTime());
}
public function testGetExpiredIds() {
$this->processor
->getExpiredIds($this->feed);
}
public function testExpireItem() {
$item_id = 1;
$this->processor
->expireItem($this->feed, $item_id, $this->state);
}
public function testGetItemCount() {
$this
->markTestIncomplete('Test not yet implemented.');
$this->processor
->getItemCount($this->feed);
}
public function testGetImportedItemIds() {
$feed_type = $this
->createFeedType();
$feed = $this
->createFeed($feed_type
->id());
$node = $this
->createNodeWithFeedsItem($feed);
$expected = [
$node
->id() => $node
->id(),
];
$this
->assertEquals($expected, $feed_type
->getProcessor()
->getImportedItemIds($this->feed));
$node2 = $this
->createNodeWithFeedsItem($feed);
$node3 = $this
->createNodeWithFeedsItem($feed);
$expected = [
$node
->id() => $node
->id(),
$node2
->id() => $node2
->id(),
$node3
->id() => $node3
->id(),
];
$this
->assertEquals($expected, $feed_type
->getProcessor()
->getImportedItemIds($this->feed));
}
public function testBuildAdvancedForm() {
$form = [];
$form_state = $this
->createMock(FormStateInterface::class);
$this
->assertIsArray($this->processor
->buildAdvancedForm($form, $form_state));
}
public function testIsLocked() {
$this->processor
->isLocked();
$this
->markTestIncomplete('Test is a stub.');
}
public function testMapWithEmptySource() {
$feed_type = $this
->createMock(FeedTypeInterface::class);
$feed_type
->expects($this
->once())
->method('getMappings')
->will($this
->returnValue([
[
'target' => 'title',
'map' => [
'value' => '',
],
],
]));
$this
->setProtectedProperty($this->processor, 'feedType', $feed_type);
$field_definition = $this
->createMock(FieldDefinitionInterface::class);
$definition = FieldTargetDefinition::createFromFieldDefinition($field_definition)
->addProperty('value');
$target = new StringTarget([
'feed_type' => $feed_type,
'target_definition' => $definition,
], 'string', [
'id' => 'string',
'field_types' => [
'string',
'string_long',
'list_string',
],
]);
$feed_type
->expects($this
->exactly(2))
->method('getTargetPlugin')
->will($this
->returnValue($target));
$this
->callProtectedMethod($this->processor, 'map', [
$this->feed,
$this
->createMock(EntityInterface::class),
$this
->createMock(ItemInterface::class),
]);
}
public function testOnFeedDeleteMultiple() {
$this
->callProtectedMethod($this->processor, 'prepareFeedsItemField');
$this->processor
->onFeedDeleteMultiple([
$this->feed,
]);
$this
->markTestIncomplete('Test is a stub.');
}
}