d64761257c4d9a12401b729d5fb8da2923772516
[yaffs-website] / process / LinkUriTest.php
1 <?php
2
3 namespace Drupal\Tests\menu_link_content\Unit\Plugin\migrate\process;
4
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Core\Url;
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;
13
14 /**
15  * Tests \Drupal\menu_link_content\Plugin\migrate\process\LinkUri.
16  *
17  * @group menu_link_content
18  *
19  * @coversDefaultClass \Drupal\menu_link_content\Plugin\migrate\process\LinkUri
20  */
21 class LinkUriTest extends UnitTestCase {
22
23   /**
24    * The entity type manager prophecy used in the test.
25    *
26    * @var \Prophecy\Prophecy\ProphecyInterface|\Drupal\Core\Entity\EntityTypeManagerInterface
27    */
28   protected $entityTypeManager;
29
30   /**
31    * The 'link_uri' process plugin being tested.
32    *
33    * @var \Drupal\menu_link_content\Plugin\migrate\process\LinkUri
34    */
35   protected $processPlugin;
36
37   /**
38    * The path validator prophecy used in the test.
39    *
40    * @var \Drupal\Core\Path\PathValidator
41    */
42   protected $pathValidator;
43
44   /**
45    * The fake entity type ID used in the test.
46    *
47    * @var string
48    */
49   protected $entityTypeId = 'the_entity_type_id';
50
51   /**
52    * {@inheritdoc}
53    */
54   protected function setUp() {
55     parent::setUp();
56
57     $this->entityTypeManager = $this->prophesize(EntityTypeManagerInterface::class);
58     $this->entityTypeManager->getDefinitions()->willReturn([$this->entityTypeId => '']);
59     $this->processPlugin = new LinkUri([], 'link_uri', [], $this->entityTypeManager->reveal());
60
61     // Url::fromInternalUri() accesses the path validator from the global
62     // container.
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);
68   }
69
70   /**
71    * Tests LinkUri::transform().
72    *
73    * @param array $value
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.
79    *
80    * @dataProvider providerTestTransform
81    *
82    * @covers ::transform
83    */
84   public function testTransform(array $value, $expected, Url $url = NULL) {
85     $migrate_executable = $this->prophesize(MigrateExecutableInterface::class);
86     $row = $this->prophesize(Row::class);
87
88     if ($url) {
89       $this->pathValidator->getUrlIfValidWithoutAccessCheck(reset($value))->willReturn($url);
90     }
91
92     $actual = $this->processPlugin->transform($value, $migrate_executable->reveal(), $row->reveal(), 'link/uri');
93     $this->assertEquals($expected, $actual);
94   }
95
96   /**
97    * Provides test cases for LinkUriTest::testTransform().
98    *
99    * @return array
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.
104    */
105   public function providerTestTransform() {
106     $tests = [];
107
108     $value = ['http://example.com'];
109     $expected = 'http://example.com';
110     $tests['with_scheme'] = [$value, $expected];
111
112     $value = ['/test'];
113     $expected = 'internal:/test';
114     $tests['leading_slash'] = [$value, $expected];
115
116     $value = ['test'];
117     $expected = 'internal:/test';
118     $tests['without_scheme'] = [$value, $expected];
119
120     $value = ['<front>'];
121     $expected = 'internal:/';
122     $tests['front'] = [$value, $expected];
123
124     $url = Url::fromRoute('route_name');
125     $tests['with_route'] = [$value, $expected, $url];
126
127     $url = Url::fromRoute('entity.not_an_entity_type_id.canonical');
128     $tests['without_entity_type'] = [$value, $expected, $url];
129
130     $url = Url::fromRoute('entity.the_entity_type_id.canonical');
131     $tests['without_route_parameter'] = [$value, $expected, $url];
132
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];
136
137     return $tests;
138   }
139
140 }