diff --git a/config/install/replication.replication_settings.article.yml b/config/install/replication.replication_settings.article.yml index 94090a3..14f5a13 100644 --- a/config/install/replication.replication_settings.article.yml +++ b/config/install/replication.replication_settings.article.yml @@ -5,4 +5,5 @@ id: article label: Replicate only article entities filter_id: entity_type parameters: - types: node.article + types: + - node.article diff --git a/config/install/replication.replication_settings.page.yml b/config/install/replication.replication_settings.page.yml index f63453d..3465383 100644 --- a/config/install/replication.replication_settings.page.yml +++ b/config/install/replication.replication_settings.page.yml @@ -5,4 +5,5 @@ id: page label: Replicate only page entities filter_id: entity_type parameters: - types: node.page + types: + - node.page diff --git a/config/install/replication.replication_settings.published.yml b/config/install/replication.replication_settings.published.yml index 11c4688..87314bd 100644 --- a/config/install/replication.replication_settings.published.yml +++ b/config/install/replication.replication_settings.published.yml @@ -4,4 +4,5 @@ dependencies: { } id: published label: Replicate published entities filter_id: published -parameters: [] +parameters: + include_unpublishable_entities: FALSE diff --git a/src/Changes/Changes.php b/src/Changes/Changes.php index 1419d36..b16a092 100644 --- a/src/Changes/Changes.php +++ b/src/Changes/Changes.php @@ -16,8 +16,9 @@ class Changes implements ChangesInterface { use DependencySerializationTrait; /** + * The workspace to generate the changeset from. + * * @var string - * The workspace to generate the changeset from. */ protected $workspaceId; @@ -43,20 +44,23 @@ class Changes implements ChangesInterface { protected $filterName; /** + * The parameters passed to the filter plugin. + * * @var array - * The parameters passed to the filter plugin. */ protected $parameters; /** + * Whether to include entities in the changeset. + * * @var boolean - * Whether to include entities in the changeset. */ protected $includeDocs = FALSE; /** + * The sequence ID to start including changes from. Result includes $lastSeq. + * * @var int - * The sequence ID to start including changes from. Result includes $lastSeq. */ protected $lastSeq = 0; diff --git a/src/Plugin/ReplicationFilter/EntityTypeFilter.php b/src/Plugin/ReplicationFilter/EntityTypeFilter.php index 1be4820..ea105a4 100644 --- a/src/Plugin/ReplicationFilter/EntityTypeFilter.php +++ b/src/Plugin/ReplicationFilter/EntityTypeFilter.php @@ -8,9 +8,8 @@ use Drupal\replication\Plugin\ReplicationFilter\ReplicationFilterBase; /** * Provides a filter based on entity type. * - * Use the configuration "types" with values in the format - * "{entity_type_id}.{bundle}", for example, "node.article". This can also be a - * comma delimited list like "node.article,node.page". + * Use the configuration "types" which is an array of values in the format + * "{entity_type_id}.{bundle}". * * @ReplicationFilter( * id = "entity_type", @@ -23,12 +22,18 @@ class EntityTypeFilter extends ReplicationFilterBase { /** * {@inheritdoc} */ + public function defaultConfiguration() { + return [ + 'types' => [], + ]; + } + + /** + * {@inheritdoc} + */ public function filter(EntityInterface $entity) { $configuration = $this->getConfiguration(); - $types = isset($configuration['types']) ? $configuration['types'] : ''; - $types = str_replace(' ', '', $types); - $types = explode(',', $types); - $types = array_filter($types); + $types = $configuration['types']; foreach ($types as $type) { // Handle cases like "node.". diff --git a/src/Plugin/ReplicationFilter/PublishedFilter.php b/src/Plugin/ReplicationFilter/PublishedFilter.php index b637da3..a049fb1 100644 --- a/src/Plugin/ReplicationFilter/PublishedFilter.php +++ b/src/Plugin/ReplicationFilter/PublishedFilter.php @@ -11,10 +11,9 @@ use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides a filter for published entities. * - * Use the configuration "include_unpublisheable_entities" to determine what + * Use the configuration "include_unpublishable_entities" to determine what * happens to entities that do not have a "status" field, if set to TRUE they * will be included by the filter, else excluded. - * with values in the format * * @ReplicationFilter( * id = "published", @@ -25,8 +24,9 @@ use Symfony\Component\DependencyInjection\ContainerInterface; class PublishedFilter extends ReplicationFilterBase implements ContainerFactoryPluginInterface { /** + * The entity type manager to check for "status" entity key. + * * @var \Drupal\Core\Entity\EntityTypeManagerInterface - * The entity type manager to check for "status" entity key. */ protected $entityTypeManager; @@ -59,7 +59,7 @@ class PublishedFilter extends ReplicationFilterBase implements ContainerFactoryP */ public function defaultConfiguration() { return [ - 'include_unpublisheable_entities' => FALSE, + 'include_unpublishable_entities' => FALSE, ]; } @@ -78,7 +78,7 @@ class PublishedFilter extends ReplicationFilterBase implements ContainerFactoryP } // Determine what to do with entities without a 'status' field. $configuration = $this->getConfiguration(); - return $configuration['include_unpublisheable_entities']; + return $configuration['include_unpublishable_entities']; } } diff --git a/src/Plugin/ReplicationFilter/ReplicationFilterBase.php b/src/Plugin/ReplicationFilter/ReplicationFilterBase.php index bd82751..329a8cc 100644 --- a/src/Plugin/ReplicationFilter/ReplicationFilterBase.php +++ b/src/Plugin/ReplicationFilter/ReplicationFilterBase.php @@ -14,11 +14,15 @@ use Drupal\replication\Plugin\ReplicationFilterInterface; abstract class ReplicationFilterBase extends PluginBase implements ReplicationFilterInterface { /** + * The human readable label for the replication filter. + * * @var string */ protected $label; /** + * The description of what the replication filter does. + * * @var string */ protected $description; @@ -44,6 +48,9 @@ abstract class ReplicationFilterBase extends PluginBase implements ReplicationFi * passed as "query_params" to a CouchDB filter function. */ public function getConfiguration() { + if (empty($this->configuration)) { + return $this->defaultConfiguration(); + } return $this->configuration; } @@ -57,13 +64,6 @@ abstract class ReplicationFilterBase extends PluginBase implements ReplicationFi /** * {@inheritdoc} */ - public function defaultConfiguration() { - return array(); - } - - /** - * {@inheritdoc} - */ public function calculateDependencies() { return array(); } diff --git a/src/Plugin/ReplicationFilter/UuidFilter.php b/src/Plugin/ReplicationFilter/UuidFilter.php index 47d841f..c7bd4ae 100644 --- a/src/Plugin/ReplicationFilter/UuidFilter.php +++ b/src/Plugin/ReplicationFilter/UuidFilter.php @@ -8,7 +8,7 @@ use Drupal\replication\Plugin\ReplicationFilter\ReplicationFilterBase; /** * Provides filtering by UUID. * - * Use the configuration "uuids" with comma delimited values, e.g. "101,102". + * Use the configuration "uuids" which is an array of uuids, e.g. "101,102". * * Note: if the entity a UUID refers to references another entity, that * referenced entity's UUID must also be included in order to maintain data @@ -25,12 +25,18 @@ class UuidFilter extends ReplicationFilterBase { /** * {@inheritdoc} */ + public function defaultConfiguration() { + return [ + 'uuids' => [], + ]; + } + + /** + * {@inheritdoc} + */ public function filter(EntityInterface $entity) { $configuration = $this->getConfiguration(); - $uuids = isset($configuration['uuids']) ? $configuration['uuids'] : ''; - $uuids = explode(',', $uuids); - $uuids = array_map('trim', $uuids); - return in_array($entity->uuid(), $uuids); + return in_array($entity->uuid(), $configuration['uuids']); } } diff --git a/src/ReplicationTask/ReplicationTask.php b/src/ReplicationTask/ReplicationTask.php index 7653982..91ccc14 100644 --- a/src/ReplicationTask/ReplicationTask.php +++ b/src/ReplicationTask/ReplicationTask.php @@ -10,14 +10,16 @@ use Drupal\replication\ReplicationTask\ReplicationTaskInterface; class ReplicationTask implements ReplicationTaskInterface { /** + * The id of the filter plugin to use during replication. + * * @var string - * The id of the filter plugin to use during replication. */ protected $filterName; /** + * The parameters passed to the filter function. + * * @var array - * The parameters passed to the filter function. */ protected $parameters; diff --git a/tests/src/Functional/ReplicationFilterTest.php b/tests/src/Functional/ReplicationFilterTest.php index 08dd3ee..ee13dad 100644 --- a/tests/src/Functional/ReplicationFilterTest.php +++ b/tests/src/Functional/ReplicationFilterTest.php @@ -29,8 +29,9 @@ class ReplicationFilterTest extends WebTestBase { ]; /** + * The logged in user. + * * @var \Drupal\user\Entity\User - * The logged in user. */ protected $user; @@ -75,9 +76,9 @@ class ReplicationFilterTest extends WebTestBase { $entity2->workspace = $workspace; $entity2->save(); - $parameters = ['uuids' => $entity1->uuid()]; + $parameters = ['uuids' => [$entity1->uuid()]]; $changes = $changes_factory->get($workspace)->filter('uuid')->parameters($parameters)->getNormal(); - $this->assertEqual(1, count($changes), 'Expect there is 1 entity in the changeset.'); + $this->assertEqual(1, count($changes), 'Expect there is 1 entity in the changeset for UUIDs filter.'); } /** @@ -109,7 +110,7 @@ class ReplicationFilterTest extends WebTestBase { $entity2->save(); $changes = $changes_factory->get($workspace)->filter('published')->getNormal(); - $this->assertEqual(1, count($changes), 'Expect there is 1 entity in the changeset.'); + $this->assertEqual(1, count($changes), 'Expect there is 1 entity in the changeset for published filter.'); } /** @@ -142,9 +143,9 @@ class ReplicationFilterTest extends WebTestBase { $entity2->workspace = $workspace; $entity2->save(); - $parameters = ['entity_type_id' => 'node', 'bundle' => 'article']; + $parameters = ['types' => ['node.article']]; $changes = $changes_factory->get($workspace)->filter('entity_type')->parameters($parameters)->getNormal(); - $this->assertEqual(1, count($changes), 'Expect there is 1 entity in the changeset.'); + $this->assertEqual(1, count($changes), 'Expect there is 1 entity in the changeset for entity type filter.'); } } diff --git a/tests/src/Kernel/Plugin/ReplicationFilter/PublishedFilterTest.php b/tests/src/Kernel/Plugin/ReplicationFilter/PublishedFilterTest.php index 944da25..53e2e11 100644 --- a/tests/src/Kernel/Plugin/ReplicationFilter/PublishedFilterTest.php +++ b/tests/src/Kernel/Plugin/ReplicationFilter/PublishedFilterTest.php @@ -1,11 +1,9 @@ container->get('plugin.manager.replication_filter'); $configuration = [ - 'include_unpublisheable_entities' => $include_unpublisheable_entities, + 'include_unpublishable_entities' => $include_unpublishable_entities, ]; $filter = $filter_manager->createInstance('published', $configuration); - $entity = call_user_func($entity_class . '::create', $entity_values); + $entity = $this->container + ->get('entity_type.manager') + ->getStorage($entity_type_id) + ->create($entity_values); $value = $filter->filter($entity); - $this->assertEquals($expected, $value); + $this->assertSame($expected, $value); } /** * Test default configuration for published filter. */ - public function testDefaultConfig($include_unpublisheable_entities, $entity_class, $entity_values, $expected) { + public function testDefaultConfig() { /** @var \Drupal\replication\Plugin\ReplicationFilterManagerInterface $filter_manager */ $filter_manager = $this->container->get('plugin.manager.replication_filter'); $filter = $filter_manager->createInstance('published'); - $entity = BlockContent::create([ - 'type' => 'test', - ]); + $entity = $this->container + ->get('entity_type.manager') + ->getStorage('block_content') + ->create(['type' => 'test']); $value = $filter->filter($entity); @@ -124,10 +126,10 @@ class PublishedFilterTest extends KernelTestBase { ]; return [ - [FALSE, Node::class, $published_node, TRUE], - [FALSE, Node::class, $unpublished_node, FALSE], - [TRUE, BlockContent::class, $block, TRUE], - [FALSE, BlockContent::class, $block, FALSE], + [FALSE, 'node', $published_node, TRUE], + [FALSE, 'node', $unpublished_node, FALSE], + [TRUE, 'block_content', $block, TRUE], + [FALSE, 'block_content', $block, FALSE], ]; } diff --git a/tests/src/Unit/Plugin/ReplicationFilter/EntityTypeFilterTest.php b/tests/src/Unit/Plugin/ReplicationFilter/EntityTypeFilterTest.php index 0141c60..6b5d8c8 100644 --- a/tests/src/Unit/Plugin/ReplicationFilter/EntityTypeFilterTest.php +++ b/tests/src/Unit/Plugin/ReplicationFilter/EntityTypeFilterTest.php @@ -52,23 +52,23 @@ class EntityTypeFilterTest extends \PHPUnit_Framework_TestCase { public function filterTestProvider() { return [ // Test singular parameter values. - ['node', TRUE], - ['node.article', TRUE], - ['node.page', FALSE], + [['node'], TRUE], + [['node.article'], TRUE], + [['node.page'], FALSE], // Test multiple parameter values. - ['block,node', TRUE], - ['node.article,node.page', TRUE], - ['node.page,node.article', TRUE], - ['node.test,node.page', FALSE], + [['block', 'node'], TRUE], + [['node.article', 'node.page'], TRUE], + [['node.page', 'node.article'], TRUE], + [['node.test', 'node.page'], FALSE], // Test bad data that might be entered into the parameters: - ['', FALSE], - [',', FALSE], - [',node', TRUE], - ['..', FALSE], - [NULL, FALSE], - [FALSE, FALSE], - [TRUE, FALSE], - [0, FALSE], + [[''], FALSE], + [[','], FALSE], + [[',node'], FALSE], + [['..'], FALSE], + [[NULL], FALSE], + [[FALSE], FALSE], + [[TRUE], FALSE], + [[0], FALSE], ]; } diff --git a/tests/src/Unit/Plugin/ReplicationFilter/UuidFilterTest.php b/tests/src/Unit/Plugin/ReplicationFilter/UuidFilterTest.php index 50e59e1..0770074 100644 --- a/tests/src/Unit/Plugin/ReplicationFilter/UuidFilterTest.php +++ b/tests/src/Unit/Plugin/ReplicationFilter/UuidFilterTest.php @@ -45,16 +45,15 @@ class UuidFilterTest extends \PHPUnit_Framework_TestCase { public function filterTestProvider() { return [ // Test singular parameter values. - ['123', '123', TRUE], - ['123', '456', FALSE], + ['123', ['123'], TRUE], + ['123', ['456'], FALSE], // Test multiple parameter values. - ['123', '123,456', TRUE], - ['123', '456,789', FALSE], + ['123', ['123', '456'], TRUE], + ['123', ['456', '789'], FALSE], // Test bad data that might be entered into the parameters: - ['123', '123, 456', TRUE], - ['123', '123 , 456', TRUE], - ['123', '0', FALSE], - ['123', 'NULL', FALSE], + ['123', ['123 '], FALSE], + ['123', [0], FALSE], + ['123', [NULL], FALSE], ]; } @@ -72,9 +71,8 @@ class UuidFilterTest extends \PHPUnit_Framework_TestCase { $entity = $this->getMock(EntityInterface::class); $entity->method('uuid') ->willReturn('123'); - $parameters = []; - $value = $filter->filter($entity, $parameters); + $value = $filter->filter($entity, []); $this->assertEquals(FALSE, $value); }