You are here

class DomApplyStylesTest in Migrate Plus 8.5

Tests the dom_apply_styles process plugin.

@group migrate @coversDefaultClass \Drupal\migrate_plus\Plugin\migrate\process\DomApplyStyles

Hierarchy

Expanded class hierarchy of DomApplyStylesTest

File

tests/src/Unit/process/DomApplyStylesTest.php, line 19

Namespace

Drupal\Tests\migrate_plus\Unit\process
View source
class DomApplyStylesTest extends MigrateProcessTestCase {

  /**
   * Example configuration for the dom_apply_styles process plugin.
   *
   * @var array
   */
  protected $exampleConfiguration = [
    'format' => 'test_format',
    'rules' => [
      [
        'xpath' => '//b',
        'style' => 'Bold',
      ],
      [
        'xpath' => '//span/i',
        'style' => 'Italic',
        'depth' => 1,
      ],
    ],
  ];

  /**
   * Mock a config factory object.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory = NULL;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {

    // Mock a config object.
    $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();

    // Mock the config factory.
    $prophecy = $this
      ->prophesize(ConfigFactory::class);
    $prophecy
      ->get('editor.editor.test_format')
      ->willReturn($style_config);
    $this->configFactory = $prophecy
      ->reveal();
    parent::setUp();
  }

  /**
   * @covers ::__construct
   *
   * @dataProvider providerTestConfig
   */
  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');
  }

  /**
   * Dataprovider for testValidateRules().
   */
  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;
  }

  /**
   * @covers ::transform
   */
  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');
  }

  /**
   * @covers ::transform
   */
  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));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DomApplyStylesTest::$configFactory protected property Mock a config factory object.
DomApplyStylesTest::$exampleConfiguration protected property Example configuration for the dom_apply_styles process plugin.
DomApplyStylesTest::providerTestConfig public function Dataprovider for testValidateRules().
DomApplyStylesTest::setUp protected function Overrides MigrateProcessTestCase::setUp
DomApplyStylesTest::testTransform public function @covers ::transform
DomApplyStylesTest::testTransformInvalidInput public function @covers ::transform
DomApplyStylesTest::testValidateRules public function @covers ::__construct
MigrateProcessTestCase::$migrateExecutable protected property
MigrateProcessTestCase::$plugin protected property
MigrateProcessTestCase::$row protected property
MigrateTestCase::$idMap protected property The migration ID map.
MigrateTestCase::$migrationConfiguration protected property An array of migration configuration values. 16
MigrateTestCase::$migrationStatus protected property Local store for mocking setStatus()/getStatus().
MigrateTestCase::createSchemaFromRow protected function Generates a table schema from a row.
MigrateTestCase::getDatabase protected function Gets an SQLite database connection object for use in tests.
MigrateTestCase::getMigration protected function Retrieves a mocked migration. 1
MigrateTestCase::getValue protected function Gets the value on a row for a given key. 1
MigrateTestCase::queryResultTest public function Tests a query.
MigrateTestCase::retrievalAssertHelper protected function Asserts tested values during test retrieval.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.