diff --git a/core/modules/migrate/lib/Drupal/migrate/Plugin/migrate/process/DedupeBase.php b/core/modules/migrate/lib/Drupal/migrate/Plugin/migrate/process/DedupeBase.php index 3ad5b99..ebc600d 100644 --- a/core/modules/migrate/lib/Drupal/migrate/Plugin/migrate/process/DedupeBase.php +++ b/core/modules/migrate/lib/Drupal/migrate/Plugin/migrate/process/DedupeBase.php @@ -10,6 +10,8 @@ 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. @@ -28,9 +30,27 @@ public function transform($value, MigrateExecutable $migrate_executable, Row $ro $i = 1; $postfix = isset($this->configuration['postfix']) ? $this->configuration['postfix'] : ''; $new_value = $value; + $postfix_with_count = ''; while ($this->exists($new_value)) { - $new_value = $value . $postfix . $i++; + $postfix_with_count = $postfix . $i++; } + $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)) { + if (!is_int($length)) { + throw new MigrateException('The character length configuration key should be an integer. Omit this key to capture the entire string.'); + } + // Subtract postfix and count characters if it exceeds desired length. + if (Unicode::strlen($new_value . $postfix_with_count) > $length) { + $length -= Unicode::strlen($postfix_with_count); + } + } + // Use optional start or length to return a portion of deduplicated value. + $new_value = Unicode::substr($new_value, $start, $length); + $new_value .= $postfix_with_count; return $new_value; } diff --git a/core/modules/migrate/tests/Drupal/migrate/Tests/process/DedupeEntityTest.php b/core/modules/migrate/tests/Drupal/migrate/Tests/process/DedupeEntityTest.php index c7a2db5..e46ac7c 100644 --- a/core/modules/migrate/tests/Drupal/migrate/Tests/process/DedupeEntityTest.php +++ b/core/modules/migrate/tests/Drupal/migrate/Tests/process/DedupeEntityTest.php @@ -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 @@ protected function setUp() { * * @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,50 @@ public function testDedupe($count, $postfix = '') { 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 = $value; + $postfix_with_count = $count ? $postfix . $count : ''; + if (!is_null($length) && Unicode::strlen($expected . $postfix_with_count) > $length) { + $length -= Unicode::strlen($postfix_with_count); + } + $expected = Unicode::substr($value, $start, $length); + $expected .= $postfix_with_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->entityQueryExpects(0); + $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->entityQueryExpects(0); + $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 +134,38 @@ public function testDedupe($count, $postfix = '') { */ 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), ); } diff --git a/core/modules/migrate_drupal/config/install/migrate.migration.d6_block.yml b/core/modules/migrate_drupal/config/install/migrate.migration.d6_block.yml index e4c269c..05bd07d 100644 --- a/core/modules/migrate_drupal/config/install/migrate.migration.d6_block.yml +++ b/core/modules/migrate_drupal/config/install/migrate.migration.d6_block.yml @@ -9,6 +9,7 @@ process: entity_type: block field: id postfix: _ + length: 32 source: module plugin: - diff --git a/core/modules/migrate_drupal/config/install/migrate.migration.d6_contact_category.yml b/core/modules/migrate_drupal/config/install/migrate.migration.d6_contact_category.yml index e73f6ad..e0ac378 100644 --- a/core/modules/migrate_drupal/config/install/migrate.migration.d6_contact_category.yml +++ b/core/modules/migrate_drupal/config/install/migrate.migration.d6_contact_category.yml @@ -11,6 +11,7 @@ process: plugin: dedupe_entity entity_type: user_role field: cid + length: 32 label: category recipients: recipients reply: reply diff --git a/core/modules/migrate_drupal/config/install/migrate.migration.d6_filter_format.yml b/core/modules/migrate_drupal/config/install/migrate.migration.d6_filter_format.yml index 88d5c0d..1dc7dbc 100644 --- a/core/modules/migrate_drupal/config/install/migrate.migration.d6_filter_format.yml +++ b/core/modules/migrate_drupal/config/install/migrate.migration.d6_filter_format.yml @@ -11,6 +11,7 @@ process: plugin: dedupe_entity entity_type: filter_format field: format + length: 32 name: name cache: cache filters: diff --git a/core/modules/migrate_drupal/config/install/migrate.migration.d6_taxonomy_vocabulary.yml b/core/modules/migrate_drupal/config/install/migrate.migration.d6_taxonomy_vocabulary.yml index d654323..68fd460 100644 --- a/core/modules/migrate_drupal/config/install/migrate.migration.d6_taxonomy_vocabulary.yml +++ b/core/modules/migrate_drupal/config/install/migrate.migration.d6_taxonomy_vocabulary.yml @@ -11,6 +11,7 @@ process: plugin: dedupe_entity entity_type: taxonomy_vocabulary field: vid + length: 32 label: name name: name description: description diff --git a/core/modules/migrate_drupal/config/install/migrate.migration.d6_user_role.yml b/core/modules/migrate_drupal/config/install/migrate.migration.d6_user_role.yml index cabd3c0..111a9a7 100644 --- a/core/modules/migrate_drupal/config/install/migrate.migration.d6_user_role.yml +++ b/core/modules/migrate_drupal/config/install/migrate.migration.d6_user_role.yml @@ -11,6 +11,7 @@ process: plugin: dedupe_entity entity_type: user_role field: id + length: 32 - plugin: user_update_8002 label: name diff --git a/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6ContactCategory.php b/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6ContactCategory.php index d9db247..6c9d31a 100644 --- a/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6ContactCategory.php +++ b/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6ContactCategory.php @@ -83,6 +83,14 @@ public function load() { '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'); } diff --git a/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6TaxonomyVocabulary.php b/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6TaxonomyVocabulary.php index 70b2429..ea1c073 100644 --- a/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6TaxonomyVocabulary.php +++ b/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6TaxonomyVocabulary.php @@ -179,6 +179,19 @@ public function load() { 'module' => 'taxonomy', 'weight' => '6', )) + ->values(array( + 'vid' => '4', + '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); } diff --git a/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6UserRole.php b/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6UserRole.php index 4c91ed9..41f5abe 100644 --- a/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6UserRole.php +++ b/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/Dump/Drupal6UserRole.php @@ -117,7 +117,7 @@ public static function getData($table) { 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'), ), ); diff --git a/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/d6/MigrateContactCategoryTest.php b/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/d6/MigrateContactCategoryTest.php index dec8ff3..e73c323 100644 --- a/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/d6/MigrateContactCategoryTest.php +++ b/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/d6/MigrateContactCategoryTest.php @@ -64,6 +64,12 @@ public function testContactCategory() { $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); } } diff --git a/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/d6/MigrateTaxonomyVocabularyTest.php b/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/d6/MigrateTaxonomyVocabularyTest.php index 719cd86..c2f6aad 100644 --- a/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/d6/MigrateTaxonomyVocabularyTest.php +++ b/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/d6/MigrateTaxonomyVocabularyTest.php @@ -59,8 +59,13 @@ public function testTaxonomyVocabulary() { $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); } } diff --git a/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/d6/MigrateUserRoleTest.php b/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/d6/MigrateUserRoleTest.php index dffb1d2..98183e8 100644 --- a/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/d6/MigrateUserRoleTest.php +++ b/core/modules/migrate_drupal/lib/Drupal/migrate_drupal/Tests/d6/MigrateUserRoleTest.php @@ -100,6 +100,10 @@ public function testUserRole() { )); $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))); } }