MachineNameTest.php 1.84 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\migrate\Unit\process;
4

5
use Drupal\migrate\Plugin\migrate\process\MachineName;
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

/**
 * Tests the machine name process plugin.
 *
 * @group migrate
 */
class MachineNameTest extends MigrateProcessTestCase {

  /**
   * The mock transliteration.
   *
   * @var \Drupal\Component\Transliteration\TransliterationInterface
   */
  protected $transliteration;

  /**
   * {@inheritdoc}
   */
24
  protected function setUp() {
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    $this->transliteration = $this->getMockBuilder('Drupal\Component\Transliteration\TransliterationInterface')
      ->disableOriginalConstructor()
      ->getMock();
    $this->row = $this->getMockBuilder('Drupal\migrate\Row')
      ->disableOriginalConstructor()
      ->getMock();
    $this->migrateExecutable = $this->getMockBuilder('Drupal\migrate\MigrateExecutable')
      ->disableOriginalConstructor()
      ->getMock();
    parent::setUp();
  }

  /**
   * Tests machine name transformation of non-alphanumeric characters.
   */
  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 ';
47
    $human_name = $human_name_ascii . 'áéő';
48 49 50 51 52 53 54 55
    $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'));

56
    $plugin = new MachineName([], 'machine_name', [], $this->transliteration);
57
    $value = $plugin->transform($human_name, $this->migrateExecutable, $this->row, 'destination_property');
58 59 60 61
    $this->assertEquals($expected_result, $value);
  }

}