Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / menu_link_content / tests / src / Kernel / Plugin / migrate / process / LinkUriTest.php
1 <?php
2
3 namespace Drupal\Tests\menu_link_content\Kernel\Plugin\migrate\process;
4
5 use Drupal\menu_link_content\Plugin\migrate\process\LinkUri;
6 use Drupal\migrate\MigrateException;
7 use Drupal\migrate\MigrateExecutableInterface;
8 use Drupal\migrate\Row;
9 use Drupal\KernelTests\KernelTestBase;
10
11 /**
12  * Tests \Drupal\menu_link_content\Plugin\migrate\process\LinkUri.
13  *
14  * @group menu_link_content
15  *
16  * @coversDefaultClass \Drupal\menu_link_content\Plugin\migrate\process\LinkUri
17  */
18 class LinkUriTest extends KernelTestBase {
19
20   /**
21    * Tests LinkUri::transform().
22    *
23    * @param array $value
24    *   The value to pass to LinkUri::transform().
25    * @param string $expected
26    *   The expected return value of LinkUri::transform().
27    *
28    * @dataProvider providerTestRouted
29    *
30    * @covers ::transform
31    */
32   public function testRouted(array $value, $expected) {
33     $actual = $this->doTransform($value);
34     $this->assertSame($expected, $actual);
35   }
36
37   /**
38    * Provides test cases for LinkUriTest::testTransform().
39    *
40    * @return array
41    *   An array of test cases, each which the following values:
42    *   - The value array to pass to LinkUri::transform().
43    *   - The expected path returned by LinkUri::transform().
44    */
45   public function providerTestRouted() {
46     $tests = [];
47
48     $value = ['http://example.com'];
49     $expected = 'http://example.com';
50     $tests['with_scheme'] = [$value, $expected];
51
52     $value = ['<front>'];
53     $expected = 'internal:/';
54     $tests['front'] = [$value, $expected];
55
56     return $tests;
57   }
58
59   /**
60    * Tests that Non routed URLs throws an exception.
61    *
62    * @param array $value
63    *   The value to pass to LinkUri::transform().
64    * @param string $exception_message
65    *   The expected exception message.
66    *
67    * @dataProvider providerTestNotRouted
68    */
69   public function testNotRouted(array $value, $exception_message) {
70     $this->setExpectedException(MigrateException::class, $exception_message);
71     $this->doTransform($value);
72   }
73
74   /**
75    * Provides test cases for LinkUriTest::testNotRouted().
76    *
77    * @return array
78    *   An array of test cases, each which the following values:
79    *   - The value array to pass to LinkUri::transform().
80    *   - The expected path returned by LinkUri::transform().
81    *   - (optional) A URL object that the path validator prophecy will return.
82    */
83   public function providerTestNotRouted() {
84     $tests = [];
85
86     $message = 'The path "%s" failed validation.';
87
88     $value = ['/test'];
89     $expected = 'internal:/test';
90     $exception_message = sprintf($message, $expected);
91     $tests['leading_slash'] = [$value, $exception_message];
92
93     $value = ['test'];
94     $expected = 'internal:/test';
95     $exception_message = sprintf($message, $expected);
96     $tests['without_scheme'] = [$value, $exception_message];
97
98     return $tests;
99   }
100
101   /**
102    * Transforms a link path into an 'internal:' or 'entity:' URI.
103    *
104    * @param array $value
105    *   The value to pass to LinkUri::transform().
106    *
107    * @return string
108    *   The transformed link.
109    */
110   public function doTransform(array $value) {
111     $entityTypeManager = $this->container->get('entity_type.manager');
112     $routeBuilder = $this->container->get('router.builder');
113     $row = new Row();
114     $executable = $this->prophesize(MigrateExecutableInterface::class)->reveal();
115
116     $plugin = new LinkUri([], 'link_uri', [], $entityTypeManager, $routeBuilder);
117     $actual = $plugin->transform($value, $executable, $row, 'destinationproperty');
118
119     return $actual;
120   }
121
122 }