diff --git a/src/FeedTypeTamperMeta.php b/src/FeedTypeTamperMeta.php index 92313a6..55b696f 100644 --- a/src/FeedTypeTamperMeta.php +++ b/src/FeedTypeTamperMeta.php @@ -98,7 +98,6 @@ class FeedTypeTamperMeta implements FeedTypeTamperMetaInterface { public function getTampersGroupedBySource() { $grouped_tampers = []; $tampers = $this->getTampers(); - $dit = $this; foreach ($this->getTampers() as $id => $tamper) { $grouped_tampers[(string) $tamper->getSetting('source')][$id] = $tamper; } @@ -125,18 +124,18 @@ class FeedTypeTamperMeta implements FeedTypeTamperMetaInterface { /** * {@inheritdoc} */ - public function updateTamper(TamperInterface $tamper, array $configuration) { - $configuration['uuid'] = $tamper->getSetting('uuid'); - $this->getTampers()->setInstanceConfiguration($tamper->getSetting('uuid'), $configuration); + public function setTamperConfig($instance_id, array $configuration) { + $configuration['uuid'] = $instance_id; + $this->getTampers()->setInstanceConfiguration($instance_id, $configuration); $this->updateFeedType(); - return $tamper; + return $this; } /** * {@inheritdoc} */ - public function removeTamper(TamperInterface $tamper) { - $this->getTampers()->removeInstanceId($tamper->getSetting('uuid')); + public function removeTamper($instance_id) { + $this->getTampers()->removeInstanceId($instance_id); $this->updateFeedType(); $this->feedType->save(); return $this; diff --git a/src/FeedTypeTamperMetaInterface.php b/src/FeedTypeTamperMetaInterface.php index 4cbacd4..6f05c84 100644 --- a/src/FeedTypeTamperMetaInterface.php +++ b/src/FeedTypeTamperMetaInterface.php @@ -49,26 +49,25 @@ interface FeedTypeTamperMetaInterface extends ObjectWithPluginCollectionInterfac public function addTamper(array $configuration); /** - * Updates the tamper plugin instance. + * Sets the configuration for a tamper plugin instance. * - * @param \Drupal\tamper\TamperInterface $tamper - * The tamper plugin instance. + * @param string $instance_id + * The ID of a tamper plugin to set the configuration for. * @param array $configuration - * An array of tamper configuration. + * The tamper plugin configuration to set. * - * @return \Drupal\tamper\TamperInterface - * The updated tamper plugin instance. + * @return $this */ - public function updateTamper(TamperInterface $tamper, array $configuration); + public function setTamperConfig($instance_id, array $configuration); /** * Removes a tamper plugin instance from this feed type. * - * @param \Drupal\tamper\TamperInterface $tamper - * The tamper plugin instance + * @param string $instance_id + * The ID of a tamper plugin to remove. * * @return $this */ - public function removeTamper(TamperInterface $tamper); + public function removeTamper($instance_id); } diff --git a/src/Form/TamperEditForm.php b/src/Form/TamperEditForm.php index 29a89c8..299aea8 100644 --- a/src/Form/TamperEditForm.php +++ b/src/Form/TamperEditForm.php @@ -55,7 +55,7 @@ class TamperEditForm extends TamperFormBase { $tampers_config = $tamper_meta->getTampers()->getConfiguration(); $config = $this->prepareConfig($tampers_config[$uuid]['source'], $form_state); - $tamper_meta->updateTamper($this->plugin, $config); + $tamper_meta->setTamperConfig($uuid, $config); $this->feedsFeedType->save(); drupal_set_message($this->t('The plugin %plugin_label has been updated.', [ diff --git a/src/Form/TamperFormBase.php b/src/Form/TamperFormBase.php index 6aba274..1a442f8 100644 --- a/src/Form/TamperFormBase.php +++ b/src/Form/TamperFormBase.php @@ -7,7 +7,6 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\SubformState; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Url; -use Drupal\tamper\ConfigurableTamperInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -89,9 +88,7 @@ abstract class TamperFormBase extends FormBase { $form[self::VAR_PLUGIN_CONFIGURATION]['description'] = [ '#markup' => $this->plugin->getPluginDefinition()['description'], ]; - } - if ($this->plugin instanceof ConfigurableTamperInterface) { $subform_state = SubformState::createForSubform($form[self::VAR_PLUGIN_CONFIGURATION], $form, $form_state); $form[self::VAR_PLUGIN_CONFIGURATION] = $this->plugin->buildConfigurationForm($form[self::VAR_PLUGIN_CONFIGURATION], $subform_state); } @@ -139,8 +136,7 @@ abstract class TamperFormBase extends FormBase { if (empty($this->plugin)) { $form_state->setError($form[self::VAR_TAMPER_ID], $this->t('Plugin not selected')); } - - if ($this->plugin instanceof ConfigurableTamperInterface) { + else { $subform_state = SubformState::createForSubform($form[self::VAR_PLUGIN_CONFIGURATION], $form, $form_state); $this->plugin->validateConfigurationForm($form[self::VAR_PLUGIN_CONFIGURATION], $subform_state); } @@ -183,8 +179,9 @@ abstract class TamperFormBase extends FormBase { 'source' => $source, ]; - if ($this->plugin instanceof ConfigurableTamperInterface) { - $config += $form_state->getValue(self::VAR_PLUGIN_CONFIGURATION); + $plugin_config = $form_state->getValue(self::VAR_PLUGIN_CONFIGURATION); + if ($plugin_config) { + $config += $plugin_config; } return $config; diff --git a/src/Form/TamperListForm.php b/src/Form/TamperListForm.php index 11865c3..51dc981 100644 --- a/src/Form/TamperListForm.php +++ b/src/Form/TamperListForm.php @@ -194,7 +194,7 @@ class TamperListForm extends FormBase { foreach ($this->tampers[$source] as $id => $tamper) { $row = [ '#attributes' => ['class' => ['draggable']], - '#weight' => $tamper->getWeight(), + '#weight' => $tamper->getSetting('weight'), ]; // Plugin instance description. $row['description'] = [ @@ -206,7 +206,7 @@ class TamperListForm extends FormBase { '#title' => $this->t('Weight'), '#title_display' => 'invisible', '#type' => 'number', - '#default_value' => $tamper->getWeight(), + '#default_value' => $tamper->getSetting('weight'), '#attributes' => ['class' => ['tamper-weight']], ]; $row['plugin'] = [ diff --git a/src/TamperPluginCollection.php b/src/TamperPluginCollection.php index aaffbaf..eb812eb 100644 --- a/src/TamperPluginCollection.php +++ b/src/TamperPluginCollection.php @@ -24,8 +24,8 @@ class TamperPluginCollection extends DefaultLazyPluginCollection { $a = $this->get($aID); $b = $this->get($bID); - if ($a->getWeight() != $b->getWeight()) { - return $a->getWeight() < $b->getWeight() ? -1 : 1; + if ($a->getSetting('weight') != $b->getSetting('weight')) { + return $a->getSetting('weight') < $b->getSetting('weight') ? -1 : 1; } return parent::sortHelper($aID, $bID); diff --git a/tests/src/Kernel/FeedTypeTamperMetaTest.php b/tests/src/Kernel/FeedTypeTamperMetaTest.php index df9230b..f6eadb3 100644 --- a/tests/src/Kernel/FeedTypeTamperMetaTest.php +++ b/tests/src/Kernel/FeedTypeTamperMetaTest.php @@ -90,6 +90,31 @@ class FeedTypeTamperMetaTest extends KernelTestBase { } /** + * @covers ::getTampersGroupedBySource + */ + public function testGetTampersGroupedBySource() { + // Add a second tamper to 'alpha' source. + $this->feedTypeTamperMeta->addTamper([ + 'plugin' => 'convert_case', + 'operation' => 'ucfirst', + 'source' => 'alpha', + 'description' => 'Start text with uppercase character', + ]); + + $tampers_by_source = $this->feedTypeTamperMeta->getTampersGroupedBySource(); + + // Assert tampers for two sources. + $this->assertCount(2, $tampers_by_source); + $this->assertArrayHasKey('alpha', $tampers_by_source); + $this->assertArrayHasKey('beta', $tampers_by_source); + + // Assert that for the first source two tampers exist. + $this->assertCount(2, $tampers_by_source['alpha']); + // And one for the second. + $this->assertCount(1, $tampers_by_source['beta']); + } + + /** * @covers ::addTamper */ public function testAddTamper() { @@ -109,13 +134,12 @@ class FeedTypeTamperMetaTest extends KernelTestBase { } /** - * @covers ::updateTamper + * @covers ::setTamperConfig */ - public function testUpdateTamper() { + public function testSetTamperConfig() { $separator = ':'; $description = 'Explode with pipe character updated'; - $tamper = $this->feedTypeTamperMeta->getTamper('uuid1'); - $this->feedTypeTamperMeta->updateTamper($tamper, [ + $this->feedTypeTamperMeta->setTamperConfig('uuid1', [ 'separator' => $separator, 'description' => $description, ]); @@ -130,8 +154,7 @@ class FeedTypeTamperMetaTest extends KernelTestBase { * @covers ::removeTamper */ public function testRemoveTamper() { - $tamper = $this->feedTypeTamperMeta->getTamper('uuid1'); - $this->feedTypeTamperMeta->removeTamper($tamper); + $this->feedTypeTamperMeta->removeTamper('uuid1'); $tampers = iterator_to_array($this->feedTypeTamperMeta->getTampers()); // Assert that uuid1 is removed, but uuid2 still exists. diff --git a/tests/src/Unit/FeedTypeTamperMetaTest.php b/tests/src/Unit/FeedTypeTamperMetaTest.php new file mode 100644 index 0000000..640670c --- /dev/null +++ b/tests/src/Unit/FeedTypeTamperMetaTest.php @@ -0,0 +1,147 @@ +getMock(UuidInterface::class); + $uuid_generator->expects($this->any()) + ->method('generate') + ->will($this->returnValue('uuid3')); + + // Get the tamper manager. + $tamper_manager = $this->getMock(TamperManagerInterface::class); + $tamper_manager->expects($this->any()) + ->method('createInstance') + ->will($this->returnValue($this->getMock(TamperInterface::class))); + + // Mock the feed type and let it always return two tampers. + $feed_type = $this->getMock(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', + ], + ])); + + // Instantiate a feeds type tamper meta object. + $this->feedTypeTamperMeta = new FeedTypeTamperMeta($uuid_generator, $tamper_manager, $feed_type); + } + + /** + * @covers ::getTamper + */ + public function testGetTamper() { + $tamper = $this->feedTypeTamperMeta->getTamper('uuid2'); + $this->assertInstanceOf(TamperInterface::class, $tamper); + } + + /** + * @covers ::getTampers + */ + public function testGetTampers() { + $tampers = iterator_to_array($this->feedTypeTamperMeta->getTampers()); + // Assert that two tampers exist in total. + $this->assertCount(2, $tampers); + // Assert that tampers with uuid 'uuid1' and 'uuid2' exist. + $this->assertArrayHasKey('uuid1', $tampers); + $this->assertArrayHasKey('uuid2', $tampers); + } + + /** + * @covers ::getTampersGroupedBySource + */ + public function testGetTampersGroupedBySource() { + $this->assertInternalType('array', $this->feedTypeTamperMeta->getTampersGroupedBySource()); + } + + /** + * @covers ::addTamper + */ + 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); + + // Assert that three tampers exist in total. + $this->assertCount(3, $this->feedTypeTamperMeta->getTampers()); + } + + /** + * @covers ::setTamperConfig + */ + public function testSetTamperConfig() { + $separator = ':'; + $description = 'Explode with pipe character updated'; + $this->assertEquals($this->feedTypeTamperMeta, $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']); + } + + /** + * @covers ::removeTamper + */ + public function testRemoveTamper() { + $this->feedTypeTamperMeta->removeTamper('uuid1'); + $tampers = iterator_to_array($this->feedTypeTamperMeta->getTampers()); + + // Assert that uuid1 is removed, but uuid2 still exists. + $this->assertArrayNotHasKey('uuid1', $tampers); + $this->assertArrayHasKey('uuid2', $tampers); + + // Assert that one tamper exists in total. + $this->assertCount(1, $tampers); + } + +}