View source
<?php
namespace Drupal\Tests\feeds_tamper\Kernel;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\feeds\FeedTypeInterface;
use Drupal\feeds_tamper\FeedTypeTamperMeta;
use Drupal\tamper\Plugin\Tamper\ConvertCase;
class FeedTypeTamperMetaTest extends FeedsTamperKernelTestBase {
protected $feedTypeTamperMeta;
public function setUp() {
parent::setUp();
$container = \Drupal::getContainer();
$uuid_generator = $this
->createMock(UuidInterface::class);
$uuid_generator
->expects($this
->any())
->method('generate')
->will($this
->returnValue('uuid3'));
$tamper_manager = $container
->get('plugin.manager.tamper');
$feed_type = $this
->createMock(FeedTypeInterface::class);
$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',
],
]));
$feed_type
->expects($this
->any())
->method('getMappingSources')
->will($this
->returnValue([
'alpha' => [
'label' => 'Alpha',
],
'beta' => [
'label' => 'Beta',
],
]));
$this->feedTypeTamperMeta = new FeedTypeTamperMeta($uuid_generator, $tamper_manager, $feed_type);
}
public function testGetTamper() {
$tamper = $this->feedTypeTamperMeta
->getTamper('uuid2');
$this
->assertInstanceOf(ConvertCase::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->feedTypeTamperMeta
->addTamper([
'plugin' => 'convert_case',
'operation' => 'ucfirst',
'source' => 'alpha',
'description' => 'Start text with uppercase character',
]);
$tampers_by_source = $this->feedTypeTamperMeta
->getTampersGroupedBySource();
$this
->assertCount(2, $tampers_by_source);
$this
->assertArrayHasKey('alpha', $tampers_by_source);
$this
->assertArrayHasKey('beta', $tampers_by_source);
$this
->assertCount(2, $tampers_by_source['alpha']);
$this
->assertCount(1, $tampers_by_source['beta']);
}
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(ConvertCase::class, $tamper);
$this
->assertCount(3, $this->feedTypeTamperMeta
->getTampers());
}
public function testSetTamperConfig() {
$separator = ':';
$description = 'Explode with colon character (updated)';
$this->feedTypeTamperMeta
->setTamperConfig('uuid1', [
'separator' => $separator,
'description' => $description,
]);
$tampers_config = $this->feedTypeTamperMeta
->getTampers()
->getConfiguration();
$config = $tampers_config['uuid1'];
$this
->assertEquals($separator, $config['separator']);
$this
->assertEquals($description, $config['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);
}
}