1<?php
2
3namespace Drupal\Tests\menu_link_content\Kernel\Plugin\migrate\process;
4
5use Drupal\KernelTests\KernelTestBase;
6use Drupal\menu_link_content\Plugin\migrate\process\LinkUri;
7use Drupal\migrate\MigrateException;
8use Drupal\migrate\MigrateExecutableInterface;
9use Drupal\migrate\Row;
10use Drupal\node\Entity\Node;
11use Drupal\Tests\user\Traits\UserCreationTrait;
12
13/**
14 * Tests \Drupal\menu_link_content\Plugin\migrate\process\LinkUri.
15 *
16 * @group menu_link_content
17 *
18 * @coversDefaultClass \Drupal\menu_link_content\Plugin\migrate\process\LinkUri
19 */
20class LinkUriTest extends KernelTestBase {
21
22  use UserCreationTrait;
23
24  /**
25   * Modules to enable.
26   *
27   * @var array
28   */
29  public static $modules = ['node', 'user'];
30
31  /**
32   * {@inheritdoc}
33   */
34  public function setUp() {
35    parent::setUp();
36    $this->setUpCurrentUser();
37    $this->installEntitySchema('node');
38  }
39
40  /**
41   * Tests LinkUri::transform().
42   *
43   * @param string $value
44   *   The value to pass to LinkUri::transform().
45   * @param string $expected
46   *   The expected return value of LinkUri::transform().
47   *
48   * @dataProvider providerTestRouted
49   *
50   * @covers ::transform
51   */
52  public function testRouted($value, $expected) {
53    $actual = $this->doTransform($value);
54    $this->assertSame($expected, $actual);
55  }
56
57  /**
58   * Tests legacy handling for LinkUri::transform().
59   *
60   * @dataProvider providerTestRouted
61   * @covers ::transform
62   * @expectedDeprecation Passing an array as source value into the link_uri migrate process plugin is deprecated in drupal:8.8.0. The possibility to pass an array as source value to the plugin will be removed in drupal:9.0.0. Pass a string value instead. See https://www.drupal.org/node/3043694
63   * @group legacy
64   */
65  public function testRoutedLegacy($value, $expected) {
66    $this->testRouted([$value], $expected);
67  }
68
69  /**
70   * Provides test cases for LinkUriTest::testTransform().
71   *
72   * @return array
73   *   An array of test cases, each which the following values:
74   *   - The value array to pass to LinkUri::transform().
75   *   - The expected path returned by LinkUri::transform().
76   */
77  public function providerTestRouted() {
78    $tests = [];
79
80    $value = 'http://example.com';
81    $expected = 'http://example.com';
82    $tests['with_scheme'] = [$value, $expected];
83
84    $value = '<front>';
85    $expected = 'internal:/';
86    $tests['front'] = [$value, $expected];
87
88    $value = '<nolink>';
89    $expected = 'route:<nolink>';
90    $tests['nolink'] = [$value, $expected];
91
92    return $tests;
93  }
94
95  /**
96   * Tests that Non routed URLs throws an exception.
97   *
98   * @param string $value
99   *   The value to pass to LinkUri::transform().
100   * @param string $exception_message
101   *   The expected exception message.
102   *
103   * @dataProvider providerTestNotRouted
104   */
105  public function testNotRouted($value, $exception_message) {
106    $this->expectException(MigrateException::class);
107    $this->expectExceptionMessage($exception_message);
108    $this->doTransform($value);
109  }
110
111  /**
112   * Provides test cases for LinkUriTest::testNotRouted().
113   *
114   * @return array
115   *   An array of test cases, each which the following values:
116   *   - The value array to pass to LinkUri::transform().
117   *   - The expected path returned by LinkUri::transform().
118   *   - (optional) A URL object that the path validator prophecy will return.
119   */
120  public function providerTestNotRouted() {
121    $tests = [];
122
123    $message = 'The path "%s" failed validation.';
124
125    $value = '/test';
126    $expected = 'internal:/test';
127    $exception_message = sprintf($message, $expected);
128    $tests['leading_slash'] = [$value, $exception_message];
129
130    $value = 'test';
131    $expected = 'internal:/test';
132    $exception_message = sprintf($message, $expected);
133    $tests['without_scheme'] = [$value, $exception_message];
134
135    return $tests;
136  }
137
138  /**
139   * Tests disabling route validation in LinkUri::transform().
140   *
141   * @param string $value
142   *   The value to pass to LinkUri::transform().
143   * @param string $expected
144   *   The expected return value of LinkUri::transform().
145   *
146   * @dataProvider providerTestDisablingRouteValidation
147   *
148   * @covers ::transform
149   */
150  public function testDisablingRouteValidation($value, $expected) {
151    // Create a node so we have a valid route.
152    Node::create([
153      'nid' => 1,
154      'title' => 'test',
155      'type' => 'page',
156    ])->save();
157
158    $actual = $this->doTransform($value, ['validate_route' => FALSE]);
159    $this->assertSame($expected, $actual);
160  }
161
162  /**
163   * Provides test cases for LinkUriTest::testDisablingRouteValidation().
164   *
165   * @return array
166   *   An array of test cases, each which the following values:
167   *   - The value array to pass to LinkUri::transform().
168   *   - The expected path returned by LinkUri::transform().
169   */
170  public function providerTestDisablingRouteValidation() {
171    $tests = [];
172
173    $value = 'node/1';
174    $expected = 'entity:node/1';
175    $tests['routed'] = [$value, $expected];
176
177    $value = 'node/2';
178    $expected = 'base:node/2';
179    $tests['unrouted'] = [$value, $expected];
180
181    return $tests;
182  }
183
184  /**
185   * Transforms a link path into an 'internal:' or 'entity:' URI.
186   *
187   * @param string $value
188   *   The value to pass to LinkUri::transform().
189   * @param array $configuration
190   *   The plugin configuration.
191   *
192   * @return string
193   *   The transformed link.
194   */
195  public function doTransform($value, $configuration = []) {
196    $entityTypeManager = $this->container->get('entity_type.manager');
197    $routeBuilder = $this->container->get('router.builder');
198    $row = new Row();
199    $executable = $this->prophesize(MigrateExecutableInterface::class)->reveal();
200
201    $plugin = new LinkUri($configuration, 'link_uri', [], $entityTypeManager, $routeBuilder);
202    $actual = $plugin->transform($value, $executable, $row, 'destination_property');
203
204    return $actual;
205  }
206
207}
208