You are here

class MenuLinkParentTest in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/migrate/tests/src/Unit/process/MenuLinkParentTest.php \Drupal\Tests\migrate\Unit\process\MenuLinkParentTest

Tests the menu link parent process plugin.

@coversDefaultClass \Drupal\migrate\Plugin\migrate\process\MenuLinkParent @group migrate

Hierarchy

Expanded class hierarchy of MenuLinkParentTest

File

core/modules/migrate/tests/src/Unit/process/MenuLinkParentTest.php, line 22

Namespace

Drupal\Tests\migrate\Unit\process
View source
class MenuLinkParentTest extends MigrateProcessTestCase {

  /**
   * A MigrationInterface prophecy.
   *
   * @var \Prophecy\Prophecy\ObjectProphecy
   */
  protected $migration;

  /**
   * A MigrateLookupInterface prophecy.
   *
   * @var \Prophecy\Prophecy\ObjectProphecy
   */
  protected $migrateLookup;

  /**
   * A MigrationInterface prophecy.
   *
   * @var \Prophecy\Prophecy\ObjectProphecy
   */
  protected $menuLinkManager;

  /**
   * A MigrationInterface prophecy.
   *
   * @var \Prophecy\Prophecy\ObjectProphecy
   */
  protected $menuLinkStorage;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->migration = $this
      ->prophesize(MigrationInterface::class);
    $this->migrateLookup = $this
      ->prophesize(MigrateLookupInterface::class);
    $this->migrateLookup
      ->lookup(NULL, [
      1,
    ])
      ->willReturn([]);
    $this->menuLinkManager = $this
      ->prophesize(MenuLinkManagerInterface::class);
    $this->menuLinkStorage = $this
      ->prophesize(EntityStorageInterface::class);
    $container = new ContainerBuilder();
    $container
      ->set('migrate.lookup', $this->migrateLookup
      ->reveal());
    \Drupal::setContainer($container);
  }

  /**
   * @covers ::transform
   */
  public function testTransformException() {
    $plugin = new MenuLinkParent([], 'map', [], $this->migrateLookup
      ->reveal(), $this->menuLinkManager
      ->reveal(), $this->menuLinkStorage
      ->reveal(), $this->migration
      ->reveal());
    $this
      ->expectException(MigrateSkipRowException::class);
    $this
      ->expectExceptionMessage("No parent link found for plid '1' in menu 'admin'.");
    $plugin
      ->transform([
      1,
      'admin',
      NULL,
    ], $this->migrateExecutable, $this->row, 'destination_property');
  }

  /**
   * Tests the plugin when the parent is an external link.
   *
   * @covers ::transform
   */
  public function testTransformExternal() {
    $menu_link_content = $this
      ->prophesize(MenuLinkContentInterface::class);
    $menu_link_content
      ->getPluginId()
      ->willReturn('menu_link_content:fe151460-dfa2-4133-8864-c1746f28ab27');
    $this->menuLinkStorage
      ->loadByProperties([
      'link__uri' => 'http://example.com',
    ])
      ->willReturn([
      9054 => $menu_link_content,
    ]);
    $plugin = $this
      ->prophesize(PluginInspectionInterface::class);
    $this->menuLinkManager
      ->createInstance('menu_link_content:fe151460-dfa2-4133-8864-c1746f28ab27')
      ->willReturn($plugin
      ->reveal());
    $plugin = new MenuLinkParent([], 'map', [], $this->migrateLookup
      ->reveal(), $this->menuLinkManager
      ->reveal(), $this->menuLinkStorage
      ->reveal(), $this->migration
      ->reveal());
    $result = $plugin
      ->transform([
      1,
      'admin',
      'http://example.com',
    ], $this->migrateExecutable, $this->row, 'destination_property');
    $this
      ->assertEquals('menu_link_content:fe151460-dfa2-4133-8864-c1746f28ab27', $result);
  }

  /**
   * Tests the plugin when the parent is an external link.
   *
   * @covers ::transform
   *
   * @group legacy
   *
   * @expectedDeprecation Passing a migration process plugin as the fourth argument to Drupal\migrate\Plugin\migrate\process\MenuLinkParent::__construct is deprecated in drupal:8.8.0 and will throw an error in drupal:9.0.0. Pass the migrate.lookup service instead. See https://www.drupal.org/node/3047268
   */
  public function testLegacyTransformExternal() {
    $migration_plugin = $this
      ->prophesize(MigrateProcessInterface::class);
    $menu_link_manager = $this
      ->prophesize(MenuLinkManagerInterface::class);
    $menu_link_storage = $this
      ->prophesize(EntityStorageInterface::class);
    $menu_link_content = $this
      ->prophesize(MenuLinkContentInterface::class);
    $menu_link_content
      ->getPluginId()
      ->willReturn('menu_link_content:fe151460-dfa2-4133-8864-c1746f28ab27');
    $menu_link_storage
      ->loadByProperties([
      'link__uri' => 'http://example.com',
    ])
      ->willReturn([
      9054 => $menu_link_content,
    ]);
    $plugin = $this
      ->prophesize(PluginInspectionInterface::class);
    $menu_link_manager
      ->createInstance('menu_link_content:fe151460-dfa2-4133-8864-c1746f28ab27')
      ->willReturn($plugin
      ->reveal());
    $plugin = new MenuLinkParent([], 'map', [], $migration_plugin
      ->reveal(), $menu_link_manager
      ->reveal(), $menu_link_storage
      ->reveal());
    $result = $plugin
      ->transform([
      1,
      'admin',
      'http://example.com',
    ], $this->migrateExecutable, $this->row, 'destinationproperty');
    $this
      ->assertEquals('menu_link_content:fe151460-dfa2-4133-8864-c1746f28ab27', $result);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MenuLinkParentTest::$menuLinkManager protected property A MigrationInterface prophecy.
MenuLinkParentTest::$menuLinkStorage protected property A MigrationInterface prophecy.
MenuLinkParentTest::$migrateLookup protected property A MigrateLookupInterface prophecy.
MenuLinkParentTest::$migration protected property A MigrationInterface prophecy.
MenuLinkParentTest::setUp protected function Overrides MigrateProcessTestCase::setUp
MenuLinkParentTest::testLegacyTransformExternal public function Tests the plugin when the parent is an external link.
MenuLinkParentTest::testTransformException public function @covers ::transform
MenuLinkParentTest::testTransformExternal public function Tests the plugin when the parent is an external link.
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.