Issue #2247903 by bdone: Fixed Add a substr options to DedupeBase process plugin.

8.0.x
Alex Pott 2014-06-11 11:29:16 +01:00
parent 7922db66c3
commit df301e7f12
13 changed files with 123 additions and 13 deletions

View File

@ -10,6 +10,8 @@ namespace Drupal\migrate\Plugin\migrate\process;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\MigrateExecutable;
use Drupal\migrate\Row;
use Drupal\migrate\MigrateException;
use Drupal\Component\Utility\Unicode;
/**
* This abstract base contains the dedupe logic.
@ -27,6 +29,16 @@ abstract class DedupeBase extends ProcessPluginBase {
public function transform($value, MigrateExecutable $migrate_executable, Row $row, $destination_property) {
$i = 1;
$postfix = isset($this->configuration['postfix']) ? $this->configuration['postfix'] : '';
$start = isset($this->configuration['start']) ? $this->configuration['start'] : 0;
if (!is_int($start)) {
throw new MigrateException('The start position configuration key should be an integer. Omit this key to capture from the beginning of the string.');
}
$length = isset($this->configuration['length']) ? $this->configuration['length'] : NULL;
if (!is_null($length) && !is_int($length)) {
throw new MigrateException('The character length configuration key should be an integer. Omit this key to capture the entire string.');
}
// Use optional start or length to return a portion of deduplicated value.
$value = Unicode::substr($value, $start, $length);
$new_value = $value;
while ($this->exists($new_value)) {
$new_value = $value . $postfix . $i++;

View File

@ -7,6 +7,7 @@
namespace Drupal\migrate\Tests\process;
use Drupal\migrate\Plugin\migrate\process\DedupeEntity;
use Drupal\Component\Utility\Unicode;
/**
* Test the deduplication entity process plugin.
@ -74,7 +75,7 @@ class DedupeEntityTest extends MigrateProcessTestCase {
*
* @dataProvider providerTestDedupe
*/
public function testDedupe($count, $postfix = '') {
public function testDedupe($count, $postfix = '', $start = NULL, $length = NULL) {
$configuration = array(
'entity_type' => 'test_entity_type',
'field' => 'test_field',
@ -82,10 +83,43 @@ class DedupeEntityTest extends MigrateProcessTestCase {
if ($postfix) {
$configuration['postfix'] = $postfix;
}
$configuration['start'] = isset($start) ? $start : NULL;
$configuration['length'] = isset($length) ? $length : NULL;
$plugin = new DedupeEntity($configuration, 'dedupe_entity', array(), $this->getMigration(), $this->entityQueryFactory);
$this->entityQueryExpects($count);
$return = $plugin->transform('test', $this->migrateExecutable, $this->row, 'testproperty');
$this->assertSame($return, 'test' . ($count ? $postfix . $count : ''));
$value = $this->randomName(32);
$actual = $plugin->transform($value, $this->migrateExecutable, $this->row, 'testproperty');
$expected = Unicode::substr($value, $start, $length);
$expected .= $count ? $postfix . $count : '';
$this->assertSame($expected, $actual);
}
/**
* Tests that invalid start position throws an exception.
*/
public function testDedupeEntityInvalidStart() {
$configuration = array(
'entity_type' => 'test_entity_type',
'field' => 'test_field',
'start' => 'foobar',
);
$plugin = new DedupeEntity($configuration, 'dedupe_entity', array(), $this->getMigration(), $this->entityQueryFactory);
$this->setExpectedException('Drupal\migrate\MigrateException', 'The start position configuration key should be an integer. Omit this key to capture from the beginning of the string.');
$plugin->transform('test_start', $this->migrateExecutable, $this->row, 'testproperty');
}
/**
* Tests that invalid length option throws an exception.
*/
public function testDedupeEntityInvalidLength() {
$configuration = array(
'entity_type' => 'test_entity_type',
'field' => 'test_field',
'length' => 'foobar',
);
$plugin = new DedupeEntity($configuration, 'dedupe_entity', array(), $this->getMigration(), $this->entityQueryFactory);
$this->setExpectedException('Drupal\migrate\MigrateException', 'The character length configuration key should be an integer. Omit this key to capture the entire string.');
$plugin->transform('test_length', $this->migrateExecutable, $this->row, 'testproperty');
}
/**
@ -93,18 +127,38 @@ class DedupeEntityTest extends MigrateProcessTestCase {
*/
public function providerTestDedupe() {
return array(
// Tests the entity deduplication plugin when there is no duplication
// and no postfix.
// Tests no duplication.
array(0),
// Tests the entity deduplication plugin when there is duplication but
// no postfix.
// Tests no duplication and start position.
array(0, NULL, 10),
// Tests no duplication, start position, and length.
array(0, NULL, 5, 10),
// Tests no duplication and length.
array(0, NULL, NULL, 10),
// Tests duplication.
array(3),
// Tests the entity deduplication plugin when there is no duplication
// but there is a postfix.
// Tests duplication and start position.
array(3, NULL, 10),
// Tests duplication, start position, and length.
array(3, NULL, 5, 10),
// Tests duplication and length.
array(3, NULL, NULL, 10),
// Tests no duplication and postfix.
array(0, '_'),
// Tests the entity deduplication plugin when there is duplication and
// there is a postfix.
// Tests no duplication, postfix, and start position.
array(0, '_', 5),
// Tests no duplication, postfix, start position, and length.
array(0, '_', 5, 10),
// Tests no duplication, postfix, and length.
array(0, '_', NULL, 10),
// Tests duplication and postfix.
array(2, '_'),
// Tests duplication, postfix, and start position.
array(2, '_', 5),
// Tests duplication, postfix, start position, and length.
array(2, '_', 5, 10),
// Tests duplication, postfix, and length.
array(2, '_', NULL, 10),
);
}

View File

@ -9,6 +9,7 @@ process:
entity_type: block
field: id
postfix: _
length: 32
source: module
plugin:
-

View File

@ -11,6 +11,7 @@ process:
plugin: dedupe_entity
entity_type: user_role
field: cid
length: 32
label: category
recipients: recipients
reply: reply

View File

@ -11,6 +11,7 @@ process:
plugin: dedupe_entity
entity_type: filter_format
field: format
length: 32
name: name
cache: cache
filters:

View File

@ -11,6 +11,7 @@ process:
plugin: dedupe_entity
entity_type: taxonomy_vocabulary
field: vid
length: 32
label: name
name: name
description: description

View File

@ -11,6 +11,7 @@ process:
plugin: dedupe_entity
entity_type: user_role
field: id
length: 32
-
plugin: user_update_8002
label: name

View File

@ -83,6 +83,14 @@ class Drupal6ContactCategory extends Drupal6DumpBase {
'weight' => '1',
'selected' => '1',
))
->values(array(
'cid' => '3',
'category' => 'A category much longer than thirty two characters',
'recipients' => 'fortyninechars@example.com',
'reply' => '',
'weight' => '2',
'selected' => '0',
))
->execute();
$this->setModuleVersion('contact', '6001');
}

View File

@ -179,6 +179,22 @@ class Drupal6TaxonomyVocabulary extends Drupal6DumpBase {
'module' => 'taxonomy',
'weight' => '6',
))
->values(array(
// Skipping vid 4 here, since it is already in use by
// Drupal6VocabularyField::load() and throws an
// IntegrityConstraintViolationException during MigrateDrupal6Test.
'vid' => '5',
'name' => 'vocabulary name much longer than thirty two characters',
'description' => 'description of vocabulary name much longer than thirty two characters',
'help' => '',
'relations' => '1',
'hierarchy' => '3',
'multiple' => '1',
'required' => '0',
'tags' => '0',
'module' => 'taxonomy',
'weight' => '7',
))
->execute();
$this->setModuleVersion('taxonomy', 6001);
}

View File

@ -117,7 +117,7 @@ class Drupal6UserRole extends Drupal6DumpBase {
array('rid' => 2, 'name' => 'authenticated user'),
array('rid' => 3, 'name' => 'migrate test role 1'),
array('rid' => 4, 'name' => 'migrate test role 2'),
array('rid' => 5, 'name' => 'migrate test role 3'),
array('rid' => 5, 'name' => 'migrate test role 3 that is longer than thirty two characters'),
),
);

View File

@ -64,6 +64,12 @@ class MigrateContactCategoryTest extends MigrateDrupalTestBase {
$this->assertEqual($contact_category->recipients, array('test@example.com'));
$this->assertEqual($contact_category->reply, 'Thanks for contacting us, we will reply ASAP!');
$this->assertEqual($contact_category->weight, 1);
$contact_category = entity_load('contact_category', 'a_category_much_longer_than_thir');
$this->assertEqual($contact_category->label, 'A category much longer than thirty two characters');
$this->assertEqual($contact_category->recipients, array('fortyninechars@example.com'));
$this->assertEqual($contact_category->reply, '');
$this->assertEqual($contact_category->weight, 2);
}
}

View File

@ -59,8 +59,13 @@ class MigrateTaxonomyVocabularyTest extends MigrateDrupalTestBase {
$this->assertEqual($vocabulary->name, "vocabulary $j (i=$i)");
$this->assertEqual($vocabulary->description, "description of vocabulary $j (i=$i)");
$this->assertEqual($vocabulary->hierarchy, $i);
$this->assertEqual($vocabulary->weight, 4 + $i);
$this->assertEqual($vocabulary->weight, 4 + $i);
}
$vocabulary = entity_load('taxonomy_vocabulary', 'vocabulary_name_much_longer_than');
$this->assertEqual($vocabulary->name, 'vocabulary name much longer than thirty two characters');
$this->assertEqual($vocabulary->description, 'description of vocabulary name much longer than thirty two characters');
$this->assertEqual($vocabulary->hierarchy, 3);
$this->assertEqual($vocabulary->weight, 7);
}
}

View File

@ -100,6 +100,10 @@ class MigrateUserRoleTest extends MigrateDrupalTestBase {
));
$this->assertEqual($migrate_test_role_2->id(), $rid);
$this->assertEqual(array($rid), $migration->getIdMap()->lookupDestinationId(array(4)));
$rid = 'migrate_test_role_3_that_is_long';
$migrate_test_role_3 = entity_load('user_role', $rid);
$this->assertEqual($migrate_test_role_3->id(), $rid);
$this->assertEqual(array($rid), $migration->getIdMap()->lookupDestinationId(array(5)));
}
}