You are here

class MediaWysiwygFilterTest in Media Migration 8

Tests the MediaWysiwygFilter migration process plugin.

@coversDefaultClass \Drupal\media_migration\Plugin\migrate\process\MediaWysiwygFilter @group media_migration

Hierarchy

Expanded class hierarchy of MediaWysiwygFilterTest

File

tests/src/Unit/Plugin/migrate/process/MediaWysiwygFilterTest.php, line 16

Namespace

Drupal\Tests\media_migration\Unit\Plugin\migrate\process
View source
class MediaWysiwygFilterTest extends ProcessTestBase {

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    new Settings([]);
    $this
      ->setSetting(MediaMigration::MEDIA_TOKEN_DESTINATION_FILTER_SETTINGS, MediaMigration::MEDIA_TOKEN_DESTINATION_FILTER_ENTITY_EMBED);
  }

  /**
   * Test the MediaWysiwygFilter plugin's transform.
   *
   * @param string|string[] $input_value
   *   The transformed value.
   * @param string|string[] $expected_value
   *   The expected value.
   *
   * @dataProvider providerTransformTest
   */
  public function testMediaWysiwygFilterTransform($input_value, $expected_value) {
    $migration_plugin_manager = $this
      ->prophesize(MigrationPluginManagerInterface::class);
    $this->plugin = new MediaWysiwygFilter([], 'media_wysiwyg_filter', [], $this->migration, NULL, $migration_plugin_manager
      ->reveal(), $this->uuidOracle
      ->reveal());
    $this
      ->assertEquals($expected_value, $this->plugin
      ->transform($input_value, $this->migrateExecutable, $this->row, 'destination_property'));
  }

  /**
   * Data provider for ::testMediaWysiwygFilterTransform.
   *
   * @return array[]
   *   The test cases.
   */
  public function providerTransformTest() {
    return [
      'Plain text' => [
        'input' => [
          'value' => 'Lorem ipsum dolor sit amet.',
        ],
        'expected' => [
          'value' => 'Lorem ipsum dolor sit amet.',
        ],
      ],
      'Plain text with non-media token' => [
        'input' => 'Blah
[[nid:1]]
Blah',
        'expected' => 'Blah
[[nid:1]]
Blah',
      ],
      'Plain text with non-media JSON' => [
        'input' => 'Blah
[[{"nid":"1"}]]
Blah',
        'expected' => 'Blah
[[{"nid":"1"}]]
Blah',
      ],
      'HTML text with non-media JSON' => [
        'input' => '<p>Blah</p>
[[{"nid":"1"}]]
<p><strong>Blah</strong></p>',
        'expected' => '<p>Blah</p>
[[{"nid":"1"}]]
<p><strong>Blah</strong></p>',
      ],
      'Plain text with non-media and media JSON tokens' => [
        'input' => '[[{"nid":"1"}]]
Lorem ipsum dolor sit amet.
[[{"fid":"3","view_mode":"default","type":"media"}]]
Nam finibus elit nec ipsum feugiat convallis.
[[{"fid":"1","view_mode":"wysiwyg","type":"media"}]]
Aliquam tellus nisi.
[[nid:1]]',
        'expected' => '[[{"nid":"1"}]]
Lorem ipsum dolor sit amet.
<drupal-entity data-embed-button="media" data-entity-type="media" data-entity-id="3" data-entity-embed-display="view_mode:media.default"></drupal-entity>
Nam finibus elit nec ipsum feugiat convallis.
<drupal-entity data-embed-button="media" data-entity-type="media" data-entity-id="1" data-entity-embed-display="view_mode:media.wysiwyg"></drupal-entity>
Aliquam tellus nisi.
[[nid:1]]',
      ],
      'HTML text with non-media and media JSON tokens' => [
        'input' => '<p class="lead">[[{"foo":"bar"}]]</p>
<p>Lorem ipsum dolor sit amet.</p>
[[{"fid":"453","view_mode":"default","type":"media"}]]
<p>Nam finibus elit nec ipsum feugiat convallis.</p>
<p>[[{"fid":"154","view_mode":"wysiwyg","type":"media"}]]</p>
<ul>
  <li>Aliquam tellus nisi.</li>
</ul>
[[tid:15]]
<p></p>',
        'expected' => '<p class="lead">[[{"foo":"bar"}]]</p>
<p>Lorem ipsum dolor sit amet.</p>
<drupal-entity data-embed-button="media" data-entity-type="media" data-entity-id="453" data-entity-embed-display="view_mode:media.default"></drupal-entity>
<p>Nam finibus elit nec ipsum feugiat convallis.</p>
<p><drupal-entity data-embed-button="media" data-entity-type="media" data-entity-id="154" data-entity-embed-display="view_mode:media.wysiwyg"></drupal-entity></p>
<ul>
  <li>Aliquam tellus nisi.</li>
</ul>
[[tid:15]]
<p></p>',
      ],
      'Text with invalid (?) JSON' => [
        'input' => '<p>Foo?</p>
[[{ "fid":"123456","view_mode":"default","type":"media","attributes":{ "class":"css-class1
css-class2 css-class3" ,"alt":"Overridden alt attribute for embed" ,"title":"Overridden title attribute for embed" },
"fields":{ "format":"default" ,"alt":"Overridden alt attribute for embed","field_file_image_alt_text[und][0][value]":"Media alt attribute" ,"title":"Overridden title attribute for embed","field_file_image_title_text[und][0][value]":"Media title attribute"} }]]
<p>Bar baz!</p>',
        'expected' => '<p>Foo?</p>
<drupal-entity data-embed-button="media" data-entity-type="media" data-entity-id="123456" data-entity-embed-display="view_mode:media.default" alt="Overridden alt attribute for embed" title="Overridden title attribute for embed"></drupal-entity>
<p>Bar baz!</p>',
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MediaWysiwygFilterTest::providerTransformTest public function Data provider for ::testMediaWysiwygFilterTransform.
MediaWysiwygFilterTest::setUp protected function Overrides ProcessTestBase::setUp
MediaWysiwygFilterTest::testMediaWysiwygFilterTransform public function Test the MediaWysiwygFilter plugin's transform.
MigrateProcessTestCase::$migrateExecutable protected property
MigrateProcessTestCase::$row protected property
MigrateTestCase::$idMap protected property The migration ID map.
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.
ProcessTestBase::$logger protected property A logger channel object prophecy.
ProcessTestBase::$migration protected property The migration the process plugin is tested with.
ProcessTestBase::$migrationConfiguration protected property An array of migration configuration values. Overrides MigrateTestCase::$migrationConfiguration
ProcessTestBase::$plugin protected property The process plugin we test. Overrides MigrateProcessTestCase::$plugin
ProcessTestBase::$sourcePlugin protected property A SqlBase migration source plugin object prophecy.
ProcessTestBase::$testDatabase protected property A source database to test with. 1
ProcessTestBase::$uuidOracle protected property A media migration UUID oracle object prophecy.
ProcessTestBase::setSetting protected function Sets an in-memory Settings variable.
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.