diff --git a/src/Changes/Changes.php b/src/Changes/Changes.php index cc22532..8e00450 100644 --- a/src/Changes/Changes.php +++ b/src/Changes/Changes.php @@ -39,12 +39,6 @@ class Changes implements ChangesInterface { protected $filterManager; /** - * @var string[] - * The UUIDs to include in the changeset. - */ - protected $uuids = []; - - /** * @var string * The id of the filter plugin to use during replication. */ @@ -86,14 +80,6 @@ class Changes implements ChangesInterface { /** * {@inheritdoc} */ - public function uuids(array $uuids) { - $this->uuids = $uuids; - return $this; - } - - /** - * {@inheritdoc} - */ public function filter($filter_name) { $this->filterName = $filter_name; return $this; @@ -145,11 +131,6 @@ class Changes implements ChangesInterface { continue; } - // Filter by UUID. - if (is_array($this->uuids) && count($this->uuids) && !in_array($sequence['entity_uuid'], $this->uuids)) { - continue; - } - // Get the document. $revision = NULL; if ($this->includeDocs == TRUE || $filter !== NULL) { diff --git a/src/Changes/ChangesInterface.php b/src/Changes/ChangesInterface.php index 347c589..d964e04 100644 --- a/src/Changes/ChangesInterface.php +++ b/src/Changes/ChangesInterface.php @@ -16,21 +16,6 @@ use Symfony\Component\HttpFoundation\ParameterBag; interface ChangesInterface { /** - * Filter out changes only for the given UUIDs. - * - * 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 - * integrity. - * - * @param array $uuids - * The UUIDs to include in the change set. - * - * @return \Drupal\replication\Changes\ChangesInterface - * Returns $this. - */ - public function uuids(array $uuids); - - /** * Set the id of the filter plugin to use to refine the changeset. * * @param string $filter_name diff --git a/src/Plugin/ReplicationFilter/UuidFilter.php b/src/Plugin/ReplicationFilter/UuidFilter.php new file mode 100644 index 0000000..61fdfa0 --- /dev/null +++ b/src/Plugin/ReplicationFilter/UuidFilter.php @@ -0,0 +1,34 @@ +get('uuids') ?: ''; + $uuids = explode(',', $uuids); + $uuids = array_map('trim', $uuids); + return in_array($entity->uuid(), $uuids); + } + +} diff --git a/src/ReplicationTask/ReplicationTask.php b/src/ReplicationTask/ReplicationTask.php index db60c91..a332c6e 100644 --- a/src/ReplicationTask/ReplicationTask.php +++ b/src/ReplicationTask/ReplicationTask.php @@ -17,12 +17,6 @@ class ReplicationTask implements ReplicationTaskInterface { protected $filterName; /** - * @var string[] - * The UUIDs to include during replication. - */ - protected $uuids = []; - - /** * @var ParameterBag * The parameters passed to the filter function. */ @@ -46,21 +40,6 @@ class ReplicationTask implements ReplicationTaskInterface { /** * {@inheritdoc} */ - public function setUuids(array $uuids = NULL) { - $this->uuids = $uuids; - return $this; - } - - /** - * {@inheritdoc} - */ - public function getUuids() { - return $this->uuids; - } - - /** - * {@inheritdoc} - */ public function setParameters(ParameterBag $parameters = NULL) { $this->parameters = $parameters; return $this; diff --git a/src/ReplicationTask/ReplicationTaskInterface.php b/src/ReplicationTask/ReplicationTaskInterface.php index 56fcd34..f88978c 100644 --- a/src/ReplicationTask/ReplicationTaskInterface.php +++ b/src/ReplicationTask/ReplicationTaskInterface.php @@ -35,25 +35,6 @@ interface ReplicationTaskInterface { public function getFilter(); /** - * Set the UUIDs to include during replication. - * - * @param array $uuids - * The UUIDs to include during replication. - * - * @return ReplicationTaskInterface - * Returns $this. - */ - public function setUuids(array $uuids = NULL); - - /** - * Get the UUIDs to use during replication. - * - * @return string[string] - * The UUIDs to include during replication. - */ - public function getUuids(); - - /** * Set the parameters for the filter plugin. * * @param ParameterBag $parameters diff --git a/tests/src/Functional/ReplicationFilterTest.php b/tests/src/Functional/ReplicationFilterTest.php index c9ca41e..67fab18 100644 --- a/tests/src/Functional/ReplicationFilterTest.php +++ b/tests/src/Functional/ReplicationFilterTest.php @@ -70,7 +70,8 @@ class ReplicationFilterTest extends WebTestBase { $entity2->workspace = $workspace; $entity2->save(); - $changes = $changes_factory->get($workspace)->uuids([$entity1->uuid()])->getNormal(); + $parameters = new ParameterBag(['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.'); } diff --git a/tests/src/Unit/Plugin/ReplicationFilter/UuidFilterTest.php b/tests/src/Unit/Plugin/ReplicationFilter/UuidFilterTest.php new file mode 100644 index 0000000..a16be60 --- /dev/null +++ b/tests/src/Unit/Plugin/ReplicationFilter/UuidFilterTest.php @@ -0,0 +1,79 @@ +getMockBuilder(UuidFilter::class) + ->disableOriginalConstructor() + ->setMethods(NULL) + ->getMock(); + $entity = $this->getMock(EntityInterface::class); + $entity->method('uuid') + ->willReturn($uuid); + $parameters = new ParameterBag(['uuids' => $parameter_value]); + + $value = $filter->filter($entity, $parameters); + + $this->assertEquals($expected, $value); + } + + /** + * Provide test cases for the "uuids" parameter. + */ + public function filterTestProvider() { + return [ + // Test singular parameter values. + ['123', '123', TRUE], + ['123', '456', FALSE], + // Test multiple parameter values. + ['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], + ]; + } + + /** + * Test filtering UUIDs with no parameter. + */ + public function testFilterNoParameter() { + // Use a mock builder for the class under test to eliminate the need to + // mock all the dependencies. This is OK since the method under test is a + // pure function, i.e. does not use the state createdy by the constructor. + $filter = $this->getMockBuilder(UuidFilter::class) + ->disableOriginalConstructor() + ->setMethods(NULL) + ->getMock(); + $entity = $this->getMock(EntityInterface::class); + $entity->method('uuid') + ->willReturn('123'); + $parameters = new ParameterBag(); + + $value = $filter->filter($entity, $parameters); + + $this->assertEquals(FALSE, $value); + } + +}