3 namespace Drupal\Tests\menu_link_content\Unit\Plugin\migrate\process;
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
7 use Drupal\menu_link_content\Plugin\migrate\process\LinkUri;
8 use Drupal\migrate\MigrateExecutableInterface;
9 use Drupal\migrate\Row;
10 use Drupal\Tests\UnitTestCase;
11 use Drupal\Core\Entity\EntityTypeManagerInterface;
12 use Drupal\Core\Path\PathValidator;
15 * Tests \Drupal\menu_link_content\Plugin\migrate\process\LinkUri.
17 * @group menu_link_content
19 * @coversDefaultClass \Drupal\menu_link_content\Plugin\migrate\process\LinkUri
21 class LinkUriTest extends UnitTestCase {
24 * The entity type manager prophecy used in the test.
26 * @var \Prophecy\Prophecy\ProphecyInterface|\Drupal\Core\Entity\EntityTypeManagerInterface
28 protected $entityTypeManager;
31 * The 'link_uri' process plugin being tested.
33 * @var \Drupal\menu_link_content\Plugin\migrate\process\LinkUri
35 protected $processPlugin;
38 * The path validator prophecy used in the test.
40 * @var \Drupal\Core\Path\PathValidator
42 protected $pathValidator;
45 * The fake entity type ID used in the test.
49 protected $entityTypeId = 'the_entity_type_id';
54 protected function setUp() {
57 $this->entityTypeManager = $this->prophesize(EntityTypeManagerInterface::class);
58 $this->entityTypeManager->getDefinitions()->willReturn([$this->entityTypeId => '']);
59 $this->processPlugin = new LinkUri([], 'link_uri', [], $this->entityTypeManager->reveal());
61 // Url::fromInternalUri() accesses the path validator from the global
63 // @see \Drupal\Core\Url::fromInternalUri()
64 $this->pathValidator = $this->prophesize(PathValidator::class);
65 $container = new ContainerBuilder();
66 $container->set('path.validator', $this->pathValidator->reveal());
67 \Drupal::setContainer($container);
71 * Tests LinkUri::transform().
74 * The value to pass to LinkUri::transform().
75 * @param string $expected
76 * The expected return value of LinkUri::transform().
77 * @param \Drupal\Core\Url $url
78 * (optional) The URL that the path validator prophecy will return.
80 * @dataProvider providerTestTransform
84 public function testTransform(array $value, $expected, Url $url = NULL) {
85 $migrate_executable = $this->prophesize(MigrateExecutableInterface::class);
86 $row = $this->prophesize(Row::class);
89 $this->pathValidator->getUrlIfValidWithoutAccessCheck(reset($value))->willReturn($url);
92 $actual = $this->processPlugin->transform($value, $migrate_executable->reveal(), $row->reveal(), 'link/uri');
93 $this->assertEquals($expected, $actual);
97 * Provides test cases for LinkUriTest::testTransform().
100 * An array of test cases, each which the following values:
101 * - The value array to pass to LinkUri::transform().
102 * - The expected path returned by LinkUri::transform().
103 * - (optional) A URL object that the path validator prophecy will return.
105 public function providerTestTransform() {
108 $value = ['http://example.com'];
109 $expected = 'http://example.com';
110 $tests['with_scheme'] = [$value, $expected];
113 $expected = 'internal:/test';
114 $tests['leading_slash'] = [$value, $expected];
117 $expected = 'internal:/test';
118 $tests['without_scheme'] = [$value, $expected];
120 $value = ['<front>'];
121 $expected = 'internal:/';
122 $tests['front'] = [$value, $expected];
124 $url = Url::fromRoute('route_name');
125 $tests['with_route'] = [$value, $expected, $url];
127 $url = Url::fromRoute('entity.not_an_entity_type_id.canonical');
128 $tests['without_entity_type'] = [$value, $expected, $url];
130 $url = Url::fromRoute('entity.the_entity_type_id.canonical');
131 $tests['without_route_parameter'] = [$value, $expected, $url];
133 $url = Url::fromRoute('entity.the_entity_type_id.canonical', ['the_entity_type_id' => 'the_entity_id']);
134 $expected = 'entity:the_entity_type_id/the_entity_id';
135 $tests['entity_path'] = [$value, $expected, $url];