View source
<?php
namespace Drupal\Tests\feeds_tamper\Unit;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\feeds\FeedTypeInterface;
use Drupal\feeds_tamper\FeedTypeTamperMeta;
use Drupal\tamper\TamperInterface;
use Drupal\tamper\TamperManagerInterface;
use Drupal\Tests\UnitTestCase;
class FeedTypeTamperMetaTest extends UnitTestCase {
protected $feedTypeTamperMeta;
protected $feedType;
public function setUp() {
parent::setUp();
$mapping_sources = [
'alpha' => [
'label' => 'Alpha',
],
'beta' => [
'label' => 'Beta',
],
];
$this->feedTypeTamperMeta = $this
->createFeedTypeTamperMeta($mapping_sources);
}
public function testGetTamper() {
$tamper = $this->feedTypeTamperMeta
->getTamper('uuid2');
$this
->assertInstanceOf(TamperInterface::class, $tamper);
}
public function testGetTampers() {
$tampers = iterator_to_array($this->feedTypeTamperMeta
->getTampers());
$this
->assertCount(2, $tampers);
$this
->assertArrayHasKey('uuid1', $tampers);
$this
->assertArrayHasKey('uuid2', $tampers);
}
public function testGetTampersGroupedBySource() {
$this
->assertIsArray($this->feedTypeTamperMeta
->getTampersGroupedBySource());
}
public function testAddTamper() {
$uuid = $this->feedTypeTamperMeta
->addTamper([
'plugin' => 'convert_case',
'operation' => 'ucfirst',
'source' => 'gamma',
'description' => 'Start text with uppercase character',
]);
$this
->assertEquals('uuid3', $uuid);
$tamper = $this->feedTypeTamperMeta
->getTamper($uuid);
$this
->assertInstanceOf(TamperInterface::class, $tamper);
$this
->assertCount(3, $this->feedTypeTamperMeta
->getTampers());
}
public function testSetTamperConfig() {
$separator = ':';
$description = 'Explode with colon character (updated)';
$this
->assertEquals($this->feedTypeTamperMeta, $this->feedTypeTamperMeta
->setTamperConfig('uuid1', [
'separator' => $separator,
'description' => $description,
]));
}
public function testRemoveTamper() {
$this->feedTypeTamperMeta
->removeTamper('uuid1');
$tampers = iterator_to_array($this->feedTypeTamperMeta
->getTampers());
$this
->assertArrayNotHasKey('uuid1', $tampers);
$this
->assertArrayHasKey('uuid2', $tampers);
$this
->assertCount(1, $tampers);
}
public function testGetSourceDefinition() {
$source = $this->feedTypeTamperMeta
->getSourceDefinition();
$source_list = $source
->getList();
$this
->assertEquals($source_list['alpha'], 'Alpha');
$this
->assertEquals($source_list['beta'], 'Beta');
}
public function testSourceDefinitionWithBlankLabels() {
$mapping_sources = [
'alpha' => [],
'beta' => [],
];
$feed_type_tamper_meta = $this
->createFeedTypeTamperMeta($mapping_sources);
$source = $feed_type_tamper_meta
->getSourceDefinition();
$source_list = $source
->getList();
$this
->assertEquals($source_list['alpha'], 'alpha');
$this
->assertEquals($source_list['beta'], 'beta');
}
public function createFeedTypeTamperMeta(array $mapping_sources) {
$uuid_generator = $this
->createMock(UuidInterface::class);
$uuid_generator
->expects($this
->any())
->method('generate')
->will($this
->returnValue('uuid3'));
$tamper_manager = $this
->createMock(TamperManagerInterface::class);
$tamper_manager
->expects($this
->any())
->method('createInstance')
->will($this
->returnValue($this
->createMock(TamperInterface::class)));
$this->feed_type = $this
->createMock(FeedTypeInterface::class);
$this->feed_type
->expects($this
->any())
->method('getThirdPartySetting')
->with('feeds_tamper', 'tampers')
->will($this
->returnValue([
'uuid1' => [
'uuid' => 'uuid1',
'plugin' => 'explode',
'separator' => '|',
'source' => 'alpha',
'description' => 'Explode with pipe character',
],
'uuid2' => [
'uuid' => 'uuid2',
'plugin' => 'convert_case',
'operation' => 'strtoupper',
'source' => 'beta',
'description' => 'Convert all characters to uppercase',
],
]));
$this->feed_type
->expects($this
->any())
->method('getMappingSources')
->will($this
->returnValue($mapping_sources));
return new FeedTypeTamperMeta($uuid_generator, $tamper_manager, $this->feed_type);
}
}