View source
<?php
namespace Drupal\Tests\feeds\Unit\Entity;
use Drupal\feeds\Entity\FeedType;
use Drupal\feeds\Plugin\Type\FeedsPluginManager;
use Drupal\feeds\Plugin\Type\Parser\ParserInterface;
use Drupal\Tests\feeds\Unit\FeedsUnitTestCase;
class FeedTypeTest extends FeedsUnitTestCase {
protected $feedType;
protected function getFeedTypeMock($feed_type_id, array $stubs = []) {
$pluginManager = $this
->getMockBuilder(FeedsPluginManager::class)
->disableOriginalConstructor()
->getMock();
$pluginManager
->expects($this
->any())
->method('getDefinitions')
->will($this
->returnValue([]));
$default_stubs = [
'getParser',
'getSourcePluginManager',
'alter',
];
$feed_type = $this
->getMockBuilder(FeedType::class)
->setConstructorArgs([
[
'id' => $feed_type_id,
'label' => 'My Feed',
'custom_sources' => [
'source1' => [
'label' => 'Source 1',
'value' => 'Source 1',
],
],
],
'feeds_feed_type',
])
->setMethods(array_merge($default_stubs, $stubs))
->getMock();
$parser = $this
->getMockBuilder(ParserInterface::class)
->getMock();
$parser
->expects($this
->any())
->method('getMappingSources')
->will($this
->returnValue([]));
$feed_type
->expects($this
->any())
->method('getParser')
->will($this
->returnValue($parser));
$feed_type
->expects($this
->any())
->method('getSourcePluginManager')
->will($this
->returnValue($pluginManager));
return $feed_type;
}
public function setUp() {
parent::setUp();
$this->feedType = $this
->getFeedTypeMock($this
->randomMachineName());
}
public function testGetMappedSources() {
$feed_type = $this
->getFeedTypeMock($this
->randomMachineName(), [
'getMappings',
]);
$feed_type
->expects($this
->once())
->method('getMappings')
->will($this
->returnValue([
[
'target' => 'feeds_item',
'map' => [
'guid' => 'guid',
],
'unique' => [
'guid' => TRUE,
],
],
[
'target' => 'title',
'map' => [
'value' => 'title',
],
],
[
'target' => 'body',
'map' => [
'value' => 'description',
],
'settings' => [
[
'format' => 'plain_text',
],
],
],
]));
$expected = [
'guid' => 'guid',
'title' => 'title',
'description' => 'description',
];
$this
->assertSame($expected, $feed_type
->getMappedSources());
}
public function testAddCustomSource() {
$this
->assertSame($this->feedType, $this->feedType
->addCustomSource('source2', [
'label' => 'Source 2',
'value' => 'Source 2',
]));
$expected = [
'source1' => [
'label' => 'Source 1',
'value' => 'Source 1',
],
'source2' => [
'label' => 'Source 2',
'value' => 'Source 2',
],
];
$this
->assertSame($expected, $this->feedType
->getMappingSources());
}
public function testGetCustomSource() {
$expected = [
'label' => 'Source 1',
'value' => 'Source 1',
];
$this
->assertSame($expected, $this->feedType
->getCustomSource('source1'));
$this
->assertSame(NULL, $this->feedType
->getCustomSource('non_existing'));
}
public function testCustomSourceExists() {
$this
->assertSame(TRUE, $this->feedType
->customSourceExists('source1'));
$this
->assertSame(FALSE, $this->feedType
->customSourceExists('non_existing'));
}
public function testRemoveCustomSource() {
$this
->assertSame($this->feedType, $this->feedType
->removeCustomSource('source1'));
$this
->assertSame([], $this->feedType
->getMappingSources());
}
}