Issue #2247903 by bdone: Fixed Add a substr options to DedupeBase process plugin.
parent
7922db66c3
commit
df301e7f12
|
@ -10,6 +10,8 @@ namespace Drupal\migrate\Plugin\migrate\process;
|
||||||
use Drupal\migrate\ProcessPluginBase;
|
use Drupal\migrate\ProcessPluginBase;
|
||||||
use Drupal\migrate\MigrateExecutable;
|
use Drupal\migrate\MigrateExecutable;
|
||||||
use Drupal\migrate\Row;
|
use Drupal\migrate\Row;
|
||||||
|
use Drupal\migrate\MigrateException;
|
||||||
|
use Drupal\Component\Utility\Unicode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This abstract base contains the dedupe logic.
|
* 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) {
|
public function transform($value, MigrateExecutable $migrate_executable, Row $row, $destination_property) {
|
||||||
$i = 1;
|
$i = 1;
|
||||||
$postfix = isset($this->configuration['postfix']) ? $this->configuration['postfix'] : '';
|
$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;
|
$new_value = $value;
|
||||||
while ($this->exists($new_value)) {
|
while ($this->exists($new_value)) {
|
||||||
$new_value = $value . $postfix . $i++;
|
$new_value = $value . $postfix . $i++;
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
namespace Drupal\migrate\Tests\process;
|
namespace Drupal\migrate\Tests\process;
|
||||||
|
|
||||||
use Drupal\migrate\Plugin\migrate\process\DedupeEntity;
|
use Drupal\migrate\Plugin\migrate\process\DedupeEntity;
|
||||||
|
use Drupal\Component\Utility\Unicode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test the deduplication entity process plugin.
|
* Test the deduplication entity process plugin.
|
||||||
|
@ -74,7 +75,7 @@ class DedupeEntityTest extends MigrateProcessTestCase {
|
||||||
*
|
*
|
||||||
* @dataProvider providerTestDedupe
|
* @dataProvider providerTestDedupe
|
||||||
*/
|
*/
|
||||||
public function testDedupe($count, $postfix = '') {
|
public function testDedupe($count, $postfix = '', $start = NULL, $length = NULL) {
|
||||||
$configuration = array(
|
$configuration = array(
|
||||||
'entity_type' => 'test_entity_type',
|
'entity_type' => 'test_entity_type',
|
||||||
'field' => 'test_field',
|
'field' => 'test_field',
|
||||||
|
@ -82,10 +83,43 @@ class DedupeEntityTest extends MigrateProcessTestCase {
|
||||||
if ($postfix) {
|
if ($postfix) {
|
||||||
$configuration['postfix'] = $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);
|
$plugin = new DedupeEntity($configuration, 'dedupe_entity', array(), $this->getMigration(), $this->entityQueryFactory);
|
||||||
$this->entityQueryExpects($count);
|
$this->entityQueryExpects($count);
|
||||||
$return = $plugin->transform('test', $this->migrateExecutable, $this->row, 'testproperty');
|
$value = $this->randomName(32);
|
||||||
$this->assertSame($return, 'test' . ($count ? $postfix . $count : ''));
|
$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() {
|
public function providerTestDedupe() {
|
||||||
return array(
|
return array(
|
||||||
// Tests the entity deduplication plugin when there is no duplication
|
// Tests no duplication.
|
||||||
// and no postfix.
|
|
||||||
array(0),
|
array(0),
|
||||||
// Tests the entity deduplication plugin when there is duplication but
|
// Tests no duplication and start position.
|
||||||
// no postfix.
|
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),
|
array(3),
|
||||||
// Tests the entity deduplication plugin when there is no duplication
|
// Tests duplication and start position.
|
||||||
// but there is a postfix.
|
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, '_'),
|
array(0, '_'),
|
||||||
// Tests the entity deduplication plugin when there is duplication and
|
// Tests no duplication, postfix, and start position.
|
||||||
// there is a postfix.
|
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, '_'),
|
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),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@ process:
|
||||||
entity_type: block
|
entity_type: block
|
||||||
field: id
|
field: id
|
||||||
postfix: _
|
postfix: _
|
||||||
|
length: 32
|
||||||
source: module
|
source: module
|
||||||
plugin:
|
plugin:
|
||||||
-
|
-
|
||||||
|
|
|
@ -11,6 +11,7 @@ process:
|
||||||
plugin: dedupe_entity
|
plugin: dedupe_entity
|
||||||
entity_type: user_role
|
entity_type: user_role
|
||||||
field: cid
|
field: cid
|
||||||
|
length: 32
|
||||||
label: category
|
label: category
|
||||||
recipients: recipients
|
recipients: recipients
|
||||||
reply: reply
|
reply: reply
|
||||||
|
|
|
@ -11,6 +11,7 @@ process:
|
||||||
plugin: dedupe_entity
|
plugin: dedupe_entity
|
||||||
entity_type: filter_format
|
entity_type: filter_format
|
||||||
field: format
|
field: format
|
||||||
|
length: 32
|
||||||
name: name
|
name: name
|
||||||
cache: cache
|
cache: cache
|
||||||
filters:
|
filters:
|
||||||
|
|
|
@ -11,6 +11,7 @@ process:
|
||||||
plugin: dedupe_entity
|
plugin: dedupe_entity
|
||||||
entity_type: taxonomy_vocabulary
|
entity_type: taxonomy_vocabulary
|
||||||
field: vid
|
field: vid
|
||||||
|
length: 32
|
||||||
label: name
|
label: name
|
||||||
name: name
|
name: name
|
||||||
description: description
|
description: description
|
||||||
|
|
|
@ -11,6 +11,7 @@ process:
|
||||||
plugin: dedupe_entity
|
plugin: dedupe_entity
|
||||||
entity_type: user_role
|
entity_type: user_role
|
||||||
field: id
|
field: id
|
||||||
|
length: 32
|
||||||
-
|
-
|
||||||
plugin: user_update_8002
|
plugin: user_update_8002
|
||||||
label: name
|
label: name
|
||||||
|
|
|
@ -83,6 +83,14 @@ class Drupal6ContactCategory extends Drupal6DumpBase {
|
||||||
'weight' => '1',
|
'weight' => '1',
|
||||||
'selected' => '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();
|
->execute();
|
||||||
$this->setModuleVersion('contact', '6001');
|
$this->setModuleVersion('contact', '6001');
|
||||||
}
|
}
|
||||||
|
|
|
@ -179,6 +179,22 @@ class Drupal6TaxonomyVocabulary extends Drupal6DumpBase {
|
||||||
'module' => 'taxonomy',
|
'module' => 'taxonomy',
|
||||||
'weight' => '6',
|
'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();
|
->execute();
|
||||||
$this->setModuleVersion('taxonomy', 6001);
|
$this->setModuleVersion('taxonomy', 6001);
|
||||||
}
|
}
|
||||||
|
|
|
@ -117,7 +117,7 @@ class Drupal6UserRole extends Drupal6DumpBase {
|
||||||
array('rid' => 2, 'name' => 'authenticated user'),
|
array('rid' => 2, 'name' => 'authenticated user'),
|
||||||
array('rid' => 3, 'name' => 'migrate test role 1'),
|
array('rid' => 3, 'name' => 'migrate test role 1'),
|
||||||
array('rid' => 4, 'name' => 'migrate test role 2'),
|
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'),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -64,6 +64,12 @@ class MigrateContactCategoryTest extends MigrateDrupalTestBase {
|
||||||
$this->assertEqual($contact_category->recipients, array('test@example.com'));
|
$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->reply, 'Thanks for contacting us, we will reply ASAP!');
|
||||||
$this->assertEqual($contact_category->weight, 1);
|
$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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,8 +59,13 @@ class MigrateTaxonomyVocabularyTest extends MigrateDrupalTestBase {
|
||||||
$this->assertEqual($vocabulary->name, "vocabulary $j (i=$i)");
|
$this->assertEqual($vocabulary->name, "vocabulary $j (i=$i)");
|
||||||
$this->assertEqual($vocabulary->description, "description of vocabulary $j (i=$i)");
|
$this->assertEqual($vocabulary->description, "description of vocabulary $j (i=$i)");
|
||||||
$this->assertEqual($vocabulary->hierarchy, $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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -100,6 +100,10 @@ class MigrateUserRoleTest extends MigrateDrupalTestBase {
|
||||||
));
|
));
|
||||||
$this->assertEqual($migrate_test_role_2->id(), $rid);
|
$this->assertEqual($migrate_test_role_2->id(), $rid);
|
||||||
$this->assertEqual(array($rid), $migration->getIdMap()->lookupDestinationId(array(4)));
|
$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)));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue