You are here

class MediaWysiwygPluginBaseTest in Media Migration 8

Tests the Bean Media WYSIWYG plugin.

@todo Test the conditions in MediaWysiwygPluginBase::appendProcessor().

@coversDefaultClass \Drupal\media_migration\MediaWysiwygPluginBase @group media_migration

Hierarchy

Expanded class hierarchy of MediaWysiwygPluginBaseTest

File

tests/src/Unit/MediaWysiwygPluginBaseTest.php, line 18

Namespace

Drupal\Tests\media_migration\Unit
View source
class MediaWysiwygPluginBaseTest extends UnitTestCase {

  /**
   * Tests Media WYSIWYG plugin construction.
   *
   * @covers ::__construct
   * @dataProvider providerTestPluginConstruct
   */
  public function testPluginConstruct(array $config, array $plugin_definition, $expected_exception_regex) {
    if ($expected_exception_regex) {
      $this
        ->expectException(PluginException::class);
      if (is_callable([
        $this,
        'expectExceptionMessageMatches',
      ])) {
        $this
          ->expectExceptionMessageMatches($expected_exception_regex);
      }
      else {
        $this
          ->expectExceptionMessageRegExp($expected_exception_regex);
      }
    }
    $plugin = $this
      ->getMockBuilder(MediaWysiwygPluginBase::class)
      ->setConstructorArgs([
      $config,
      'test_plugin_id',
      $plugin_definition,
    ])
      ->setMethods(NULL)
      ->getMock();
    $this
      ->assertInstanceOf(MediaWysiwygPluginBase::class, $plugin);
  }

  /**
   * Tests MediaWysiwygPluginBase::process.
   *
   * @covers ::process
   * @dataProvider providerTestProcess
   */
  public function testProcess(array $field_row_values, array $additional_migrations, array $expected_migrations) {
    $row = new Row($field_row_values, array_combine(array_keys($field_row_values), array_keys($field_row_values)));
    $plugin = $this
      ->getMockBuilder(MediaWysiwygPluginBase::class)
      ->setConstructorArgs([
      [],
      'test_plugin_id',
      [
        'entity_type_map' => [
          'source_entity_type' => 'dest_entity_type',
        ],
      ],
    ])
      ->setMethods(NULL)
      ->getMock();
    $migrations = static::UNRELATED_MIGRATIONS + $additional_migrations;
    $expected_migrations = static::UNRELATED_MIGRATIONS + $expected_migrations;
    $actual_processed_migrations = $plugin
      ->process($migrations, $row);
    $this
      ->assertEquals($expected_migrations, $actual_processed_migrations);
  }

  /**
   * Data provider for ::testPluginConstruct.
   *
   * @return array
   *   The test cases.
   */
  public function providerTestPluginConstruct() {
    return [
      'Explicit source config, multiple map item' => [
        'Config' => [
          'source_entity_type_id' => 'source_id_2',
        ],
        'Definition' => [
          'entity_type_map' => [
            'source_id' => 'dest_id',
            'source_id_2' => 'dest_id',
            'source_id_3' => 'dest_id_2',
          ],
        ],
        'Exception' => NULL,
      ],
      'Explicit but invalid config, multiple map item' => [
        'Config' => [
          'source_entity_type_id' => 'unmapped_source_id',
        ],
        'Definition' => [
          'entity_type_map' => [
            'source_id' => 'dest_id',
            'source_id_2' => 'dest_id',
            'source_id_3' => 'dest_id_2',
          ],
        ],
        'Exception' => "/^The MediaWysiwyg plugin instance of class '.*MediaWysiwygPluginBase.*' cannot be instantiated with the following configuration: array(.*)\$/s",
      ],
      'Undefined map' => [
        'Config' => [
          'source_entity_type_id' => 'source_id',
        ],
        'Definition' => [],
        'Exception' => "/^The MediaWysiwyg plugin instance of class '.*MediaWysiwygPluginBase.*' cannot be instantiated with the following configuration: array(.*)\$/s",
      ],
      'Empty map' => [
        'Config' => [
          'source_entity_type_id' => 'source_id',
        ],
        'Definition' => [
          'entity_type_map' => [],
        ],
        'Exception' => "/^The MediaWysiwyg plugin instance of class '.*MediaWysiwygPluginBase.*' cannot be instantiated with the following configuration: array(.*)\$/s",
      ],
      'Empty config, one map item' => [
        'Config' => [],
        'Definition' => [
          'entity_type_map' => [
            'source_id' => 'dest_id',
          ],
        ],
        'Exception' => NULL,
      ],
      'Empty config, multiple map item' => [
        'Config' => [],
        'Definition' => [
          'entity_type_map' => [
            'source_id' => 'dest_id',
            'source_id_2' => 'dest_id',
            'source_id_3' => 'dest_id_2',
          ],
        ],
        'Exception' => "/^The MediaWysiwyg plugin instance of class '.*MediaWysiwygPluginBase.*' cannot be instantiated with the following configuration: array(.*)\$/s",
      ],
    ];
  }

  /**
   * Data provider for ::testProcess.
   *
   * @return array
   *   The test cases.
   */
  public function providerTestProcess() {
    return [
      'No matching migrations' => [
        'Field row values' => [
          'field_name' => 'field',
          'entity_type' => 'source_entity_type',
        ],
        'Additional migrations' => [],
        'Expected extra migrations' => [],
      ],
      'With a matching migration' => [
        'Field row values' => [
          'field_name' => 'field',
          'entity_type' => 'source_entity_type',
        ],
        'Additional migrations' => [
          'source_entity_type_migration' => [
            'migration_tags' => [
              'Drupal 7',
              'Content',
            ],
            'process' => [
              'field' => 'an_another_field',
              'destination_field' => 'field',
            ],
            'destination' => [
              'plugin' => 'entity:dest_entity_type',
            ],
          ],
        ],
        'Expected extra migrations' => [
          'source_entity_type_migration' => [
            'migration_tags' => [
              'Drupal 7',
              'Content',
            ],
            'process' => [
              'field' => 'an_another_field',
              'destination_field' => [
                [
                  'plugin' => 'get',
                  'source' => 'field',
                ],
                [
                  'plugin' => 'media_wysiwyg_filter',
                ],
              ],
            ],
            'destination' => [
              'plugin' => 'entity:dest_entity_type',
            ],
          ],
        ],
      ],
    ];
  }

  /**
   * Some unrelated content entity migration definitions.
   *
   * @const array[]
   */
  const UNRELATED_MIGRATIONS = [
    'custom_1' => [
      'migration_tags' => [
        'Drupal 7',
        'Content',
      ],
      'process' => [
        'field' => 'another_field',
        'destination_field' => 'field',
      ],
      'destination' => [
        'plugin' => 'entity:custom_entity',
      ],
    ],
    'custom_2' => [
      'migration_tags' => [
        'Drupal 7',
        'Content',
      ],
      'process' => [
        'field' => 'another_field',
        'destination_field' => 'field',
      ],
      'destination' => [
        'plugin' => 'entity:source_entity_type',
      ],
    ],
  ];

}

Members

Namesort descending Modifiers Type Description Overrides
MediaWysiwygPluginBaseTest::providerTestPluginConstruct public function Data provider for ::testPluginConstruct.
MediaWysiwygPluginBaseTest::providerTestProcess public function Data provider for ::testProcess.
MediaWysiwygPluginBaseTest::testPluginConstruct public function Tests Media WYSIWYG plugin construction.
MediaWysiwygPluginBaseTest::testProcess public function Tests MediaWysiwygPluginBase::process.
MediaWysiwygPluginBaseTest::UNRELATED_MIGRATIONS constant Some unrelated content entity migration definitions.
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.
UnitTestCase::setUp protected function 340