Mercurial > hg > cmmr2012-drupal-site
diff modules/contrib/migrate_plus/tests/src/Unit/process/DomStrReplaceTest.php @ 5:12f9dff5fda9 tip
Update to Drupal core 8.7.1
author | Chris Cannam |
---|---|
date | Thu, 09 May 2019 15:34:47 +0100 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modules/contrib/migrate_plus/tests/src/Unit/process/DomStrReplaceTest.php Thu May 09 15:34:47 2019 +0100 @@ -0,0 +1,143 @@ +<?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; + +/** + * Tests the dom_str_replace process plugin. + * + * @group migrate + * @coversDefaultClass \Drupal\migrate_plus\Plugin\migrate\process\DomStrReplace + */ +class DomStrReplaceTest extends MigrateProcessTestCase { + + /** + * Example configuration for the dom_str_replace process plugin. + * + * @var array + */ + protected $exampleConfiguration = [ + 'mode' => 'attribute', + 'expression' => '//a', + 'attribute_options' => [ + 'name' => 'href', + ], + 'search' => 'foo', + 'replace' => 'bar', + ]; + + /** + * @covers ::__construct + * + * @dataProvider providerTestConfigEmpty + */ + 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'); + } + + /** + * Dataprovider for testConfigValidation(). + */ + 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; + } + + /** + * @covers ::transform + */ + 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'); + } + + /** + * @covers ::transform + * + * @dataProvider providerTestTransform + */ + 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)); + } + + /** + * Dataprovider for testTransform(). + */ + 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; + } + +}