View source  
  <?php
namespace Drupal\Tests\migrate_plus\Unit\process;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Utility\Html;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate_plus\Plugin\migrate\process\DomApplyStyles;
use Drupal\Tests\migrate\Unit\process\MigrateProcessTestCase;
class DomApplyStylesTest extends MigrateProcessTestCase {
  
  protected $exampleConfiguration = [
    'format' => 'test_format',
    'rules' => [
      [
        'xpath' => '//b',
        'style' => 'Bold',
      ],
      [
        'xpath' => '//span/i',
        'style' => 'Italic',
        'depth' => 1,
      ],
    ],
  ];
  
  protected $configFactory = NULL;
  
  protected function setUp() : void {
    
    $prophecy = $this
      ->prophesize(ImmutableConfig::class);
    $prophecy
      ->get('settings.plugins.stylescombo.styles')
      ->willReturn("strong.foo|Bold\r\nem.foo.bar|Italic\r\n");
    $style_config = $prophecy
      ->reveal();
    
    $prophecy = $this
      ->prophesize(ConfigFactory::class);
    $prophecy
      ->get('editor.editor.test_format')
      ->willReturn($style_config);
    $this->configFactory = $prophecy
      ->reveal();
    parent::setUp();
  }
  
  public function testValidateRules(array $config_overrides, $message) : void {
    $configuration = $config_overrides + $this->exampleConfiguration;
    $value = '<p>A simple paragraph.</p>';
    $this
      ->expectException(InvalidPluginDefinitionException::class);
    $this
      ->expectExceptionMessage($message);
    (new DomApplyStyles($configuration, 'dom_apply_styles', [], $this->configFactory))
      ->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
  }
  
  public function providerTestConfig() : array {
    $cases = [
      'format-empty' => [
        [
          'format' => '',
        ],
        'The "format" option must be a non-empty string.',
      ],
      'format-not-string' => [
        [
          'format' => [
            1,
            2,
            3,
          ],
        ],
        'The "format" option must be a non-empty string.',
      ],
      'rules-not-array' => [
        [
          'rules' => 'invalid',
        ],
        'The "rules" option must be an array.',
      ],
      'xpath-null' => [
        [
          'rules' => [
            [
              'xpath' => NULL,
              'style' => 'Bold',
            ],
          ],
        ],
        'The "xpath" and "style" options are required for each rule.',
      ],
      'style-invalid' => [
        [
          'rules' => [
            [
              'xpath' => '//b',
              'style' => 'invalid-style',
            ],
          ],
        ],
        'The style "invalid-style" is not defined.',
      ],
    ];
    return $cases;
  }
  
  public function testTransformInvalidInput() : void {
    $value = 'string';
    $this
      ->expectException(MigrateSkipRowException::class);
    $this
      ->expectExceptionMessage('The dom_apply_styles plugin in the destinationproperty process pipeline requires a \\DOMDocument object. You can use the dom plugin to convert a string to \\DOMDocument.');
    (new DomApplyStyles($this->exampleConfiguration, 'dom_apply_styles', [], $this->configFactory))
      ->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
  }
  
  public function testTransform() : void {
    $input_string = '<div><span><b>Bold text</b></span><span><i>Italic text</i></span></div>';
    $output_string = '<div><span><strong class="foo">Bold text</strong></span><em class="foo bar">Italic text</em></div>';
    $value = Html::load($input_string);
    $document = (new DomApplyStyles($this->exampleConfiguration, 'dom_apply_styles', [], $this->configFactory))
      ->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
    $this
      ->assertTrue($document instanceof \DOMDocument);
    $this
      ->assertEquals($output_string, Html::serialize($document));
  }
}