diff --git a/core/modules/migrate/lib/Drupal/migrate/Plugin/migrate/process/MachineName.php b/core/modules/migrate/lib/Drupal/migrate/Plugin/migrate/process/MachineName.php index 80532bc..3017590 100644 --- a/core/modules/migrate/lib/Drupal/migrate/Plugin/migrate/process/MachineName.php +++ b/core/modules/migrate/lib/Drupal/migrate/Plugin/migrate/process/MachineName.php @@ -11,6 +11,7 @@ use Drupal\migrate\ProcessPluginBase; use Drupal\migrate\MigrateExecutable; use Drupal\migrate\Row; +use Drupal\migrate\MigrateException; /** * This plugin creates a machine name. @@ -37,7 +38,20 @@ public function transform($value, MigrateExecutable $migrate_executable, Row $ro $new_value = $this->getTransliteration()->transliterate($value, Language::LANGCODE_DEFAULT, '_'); $new_value = strtolower($new_value); $new_value = preg_replace('/[^a-z0-9_]+/', '_', $new_value); - return preg_replace('/_+/', '_', $new_value); + $new_value = preg_replace('/_+/', '_', $new_value); + // Return a portion of the machine name, if an optional length is provided. + if (isset($this->configuration['length'])) { + $length = isset($this->configuration['length']) ? $this->configuration['length'] : NULL; + if (!is_null($length) && !is_int($length)) { + throw new MigrateException('Input substring length should be an integer.'); + } + $start = isset($this->configuration['start']) ? $this->configuration['start'] : 0; + if (!is_int($start)) { + throw new MigrateException('Input substring start position should be an integer.'); + } + $new_value = substr($new_value, $start, $length); + } + return $new_value; } /** diff --git a/core/modules/migrate/tests/Drupal/migrate/Tests/process/MachineNameTest.php b/core/modules/migrate/tests/Drupal/migrate/Tests/process/MachineNameTest.php index c1c41c6..1e6cefb 100644 --- a/core/modules/migrate/tests/Drupal/migrate/Tests/process/MachineNameTest.php +++ b/core/modules/migrate/tests/Drupal/migrate/Tests/process/MachineNameTest.php @@ -24,6 +24,16 @@ class MachineNameTest extends MigrateProcessTestCase { protected $transliteration; /** + * @var string $humanNameAscii + */ + protected $humanNameAscii; + + /** + * @var string $humanName + */ + protected $humanName; + + /** * {@inheritdoc} */ public static function getInfo() { @@ -47,34 +57,78 @@ protected function setUp() { $this->migrateExecutable = $this->getMockBuilder('Drupal\migrate\MigrateExecutable') ->disableOriginalConstructor() ->getMock(); + $this->humanNameAscii = 'foo2, the.bar;2*&the%baz!YEE____HaW '; + $this->humanName = $this->humanNameAscii . 'áéő'; + // Set transliteration to a mock object. + $this->transliteration + ->expects($this->any()) + ->method('transliterate') + ->with($this->humanName) + ->will($this->returnValue($this->humanNameAscii . 'aeo')); parent::setUp(); } /** * Tests machine name transformation of non-alphanumeric characters. + * + * The following transformations are performed: + * - non-alphanumeric character (including spaces) -> underscore, + * - Uppercase -> lowercase, + * - Multiple consecutive underscore -> single underscore. */ public function testMachineNames() { - - // Tests the following transformations: - // - non-alphanumeric character (including spaces) -> underscore, - // - Uppercase -> lowercase, - // - Multiple consecutive underscore -> single underscore. - $human_name_ascii = 'foo2, the.bar;2*&the%baz!YEE____HaW '; - $human_name = $human_name_ascii .'áéő'; + $plugin = new TestMachineName(array(), 'machine_name', array()); + $plugin->setTransliteration($this->transliteration); + $value = $plugin->transform($this->humanName, $this->migrateExecutable, $this->row, 'destinationproperty'); $expected_result = 'foo2_the_bar_2_the_baz_yee_haw_aeo'; - // Test for calling transliterate on mock object. - $this->transliteration - ->expects($this->once()) - ->method('transliterate') - ->with($human_name) - ->will($this->returnValue($human_name_ascii . 'aeo')); + $this->assertEquals($expected_result, $value); + } - $plugin = new TestMachineName(array(), 'machine_name', array()); + /** + * Tests substring length and start options. + */ + public function testMachineNameOptions() { + // Test using an optional substring length. + $configuration['length'] = -3; + $plugin = new TestMachineName($configuration, 'machine_name', array()); + $plugin->setTransliteration($this->transliteration); + $value = $plugin->transform($this->humanName, $this->migrateExecutable, $this->row, 'destinationproperty'); + $expected_result = 'foo2_the_bar_2_the_baz_yee_haw_'; + $this->assertEquals($expected_result, $value); + + // Test using an optional substring length and start position. + $configuration['start'] = 10; + $configuration['length'] = 15; + $plugin = new TestMachineName($configuration, 'machine_name', array()); $plugin->setTransliteration($this->transliteration); - $value = $plugin->transform($human_name, $this->migrateExecutable, $this->row, 'destinationproperty'); + $value = $plugin->transform($this->humanName, $this->migrateExecutable, $this->row, 'destinationproperty'); + $expected_result = 'ar_2_the_baz_ye'; $this->assertEquals($expected_result, $value); } + /** + * Tests that an invalid substring length option throws an exception. + */ + public function testMachineNameOptionsInvalidLength() { + $this->setExpectedException('Drupal\migrate\MigrateException', 'Input substring length should be an integer.'); + $configuration['length'] = 'foobar'; + $plugin = new TestMachineName($configuration, 'machine_name', array()); + $plugin->setTransliteration($this->transliteration); + $plugin->transform($this->humanName, $this->migrateExecutable, $this->row, 'destinationproperty'); + } + + /** + * Tests that an invalid substring start option throws an exception. + */ + public function testMachineNameOptionsInvalidStart() { + $this->setExpectedException('Drupal\migrate\MigrateException', 'Input substring start position should be an integer.'); + $configuration['start'] = 'foobar'; + $configuration['length'] = 10; + $plugin = new TestMachineName($configuration, 'machine_name', array()); + $plugin->setTransliteration($this->transliteration); + $plugin->transform($this->humanName, $this->migrateExecutable, $this->row, 'destinationproperty'); + } + } namespace Drupal\migrate\Plugin\migrate\process;