View source
<?php
namespace Drupal\Tests\migrate_plus\Unit\process;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Utility\Html;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate_plus\Plugin\migrate\process\DomStrReplace;
use Drupal\Tests\migrate\Unit\process\MigrateProcessTestCase;
class DomStrReplaceTest extends MigrateProcessTestCase {
protected $exampleConfiguration = [
'mode' => 'attribute',
'expression' => '//a',
'attribute_options' => [
'name' => 'href',
],
'search' => 'foo',
'replace' => 'bar',
];
public function testConfigValidation(array $config_overrides, $message) {
$configuration = $config_overrides + $this->exampleConfiguration;
$value = '<p>A simple paragraph.</p>';
$this
->setExpectedException(InvalidPluginDefinitionException::class, $message);
(new DomStrReplace($configuration, 'dom_str_replace', []))
->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
}
public function providerTestConfigEmpty() {
$cases = [
'expression-null' => [
[
'expression' => NULL,
],
"Configuration option 'expression' is required.",
],
'mode-null' => [
[
'mode' => NULL,
],
"Configuration option 'mode' is required.",
],
'mode-invalid' => [
[
'mode' => 'invalid',
],
'Configuration option "mode" only accepts the following values: attribute.',
],
'attribute_options-null' => [
[
'attribute_options' => NULL,
],
"Configuration option 'attribute_options' is required.",
],
'search-null' => [
[
'search' => NULL,
],
"Configuration option 'search' is required.",
],
'replace-null' => [
[
'replace' => NULL,
],
"Configuration option 'replace' is required.",
],
];
return $cases;
}
public function testTransformInvalidInput() {
$configuration = [
'expression' => '//a',
'mode' => 'attribute',
'attribute_options' => [
'name' => 'href',
],
'search' => 'foo',
'replace' => 'bar',
];
$value = 'string';
$this
->setExpectedException(MigrateSkipRowException::class, 'The dom_str_replace plugin in the destinationproperty process pipeline requires a \\DOMDocument object. You can use the dom plugin to convert a string to \\DOMDocument.');
(new DomStrReplace($configuration, 'dom_str_replace', []))
->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
}
public function testTransform($input_string, $configuration, $output_string) {
$value = Html::load($input_string);
$document = (new DomStrReplace($configuration, 'dom_str_replace', []))
->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty');
$this
->assertTrue($document instanceof \DOMDocument);
$this
->assertEquals($output_string, Html::serialize($document));
}
public function providerTestTransform() {
$cases = [
'string:case_sensitive' => [
'<a href="/foo/Foo/foo">text</a>',
$this->exampleConfiguration,
'<a href="/bar/Foo/bar">text</a>',
],
'string:case_insensitive' => [
'<a href="/foo/Foo/foo">text</a>',
[
'case_insensitive' => TRUE,
] + $this->exampleConfiguration,
'<a href="/bar/bar/bar">text</a>',
],
'regex' => [
'<a href="/foo/Foo/foo">text</a>',
[
'search' => '/(.)\\1/',
'regex' => TRUE,
] + $this->exampleConfiguration,
'<a href="/fbar/Fbar/fbar">text</a>',
],
];
return $cases;
}
}