View source  
  <?php
namespace Drupal\Tests\migrate\Unit\process;
use Drupal\migrate\Plugin\migrate\process\MachineName;
use Drupal\migrate\MigrateException;
class MachineNameTest extends MigrateProcessTestCase {
  
  protected $transliteration;
  
  protected function setUp() : void {
    $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();
  }
  
  public function testMachineNames(string $human_name, array $configuration, string $expected_result) : void {
    
    $this->transliteration
      ->expects($this
      ->once())
      ->method('transliterate')
      ->with($human_name)
      ->willReturnCallback(function (string $string) : string {
      return str_replace([
        'á',
        'é',
        'ő',
      ], [
        'a',
        'e',
        'o',
      ], $string);
    });
    $plugin = new MachineName($configuration, 'machine_name', [], $this->transliteration);
    $value = $plugin
      ->transform($human_name, $this->migrateExecutable, $this->row, 'destination_property');
    $this
      ->assertEquals($expected_result, $value);
  }
  
  public function providerTestMachineNames() : array {
    return [
      
      'default' => [
        'human_name' => 'foo2, the.bar;2*&the%baz!YEE____HaW áéő',
        'configuration' => [],
        'expected_result' => 'foo2_the_bar_2_the_baz_yee_haw_aeo',
      ],
      
      'period_allowed' => [
        'human_name' => '2*&the%baz!YEE____HaW áéő.jpg',
        'configuration' => [
          'replace_pattern' => '/[^a-z0-9_.]+/',
        ],
        'expected_result' => '2_the_baz_yee_haw_aeo.jpg',
      ],
    ];
  }
  
  public function testInvalidConfiguration() : void {
    $configuration['replace_pattern'] = 1;
    $this
      ->expectException(MigrateException::class);
    $this
      ->expectExceptionMessage('The replace pattern should be a string');
    new MachineName($configuration, 'machine_name', [], $this->transliteration);
  }
}