Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / tests / Drupal / Tests / Core / ParamConverter / ParamConverterManagerTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\ParamConverter;
4
5 use Drupal\Core\ParamConverter\ParamConverterManager;
6 use Drupal\Core\ParamConverter\ParamNotConvertedException;
7 use Drupal\Tests\UnitTestCase;
8 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
9 use Symfony\Component\Routing\Route;
10 use Symfony\Component\Routing\RouteCollection;
11
12 /**
13  * @coversDefaultClass \Drupal\Core\ParamConverter\ParamConverterManager
14  * @group ParamConverter
15  */
16 class ParamConverterManagerTest extends UnitTestCase {
17
18   /**
19    * @var \Drupal\Core\ParamConverter\ParamConverterManager
20    */
21   protected $manager;
22
23   /**
24    * {@inheritdoc}
25    */
26   protected function setUp() {
27     parent::setUp();
28
29     $this->manager = new ParamConverterManager();
30   }
31
32   /**
33    * Tests \Drupal\Core\ParamConverter\ParamConverterManager::getConverter().
34    *
35    * @dataProvider providerTestGetConverter
36    *
37    * @covers ::getConverter
38    */
39   public function testGetConverter($name, $class) {
40     $converter = $this->getMockBuilder('Drupal\Core\ParamConverter\ParamConverterInterface')
41       ->setMockClassName($class)
42       ->getMock();
43
44     $this->manager->addConverter($converter, $name);
45
46     $this->assertInstanceOf($class, $this->manager->getConverter($name));
47     // Assert that a second call to getConverter() does not use the container.
48     $this->assertInstanceOf($class, $this->manager->getConverter($name));
49   }
50
51   /**
52    * Tests \Drupal\Core\ParamConverter\ParamConverterManager::getConverter().
53    *
54    * @covers ::getConverter
55    */
56   public function testGetConverterException() {
57     $this->setExpectedException(\InvalidArgumentException::class);
58     $this->manager->getConverter('undefined.converter');
59   }
60
61   /**
62    * Provide data for parameter converter manager tests.
63    *
64    * @return array
65    *   An array of arrays, each containing the input parameters for
66    *   providerTestResolvers::testAddConverter().
67    *
68    * @see ParamConverterManagerTest::testAddConverter()
69    */
70   public function providerTestAddConverter() {
71     $converters[0]['unsorted'] = [
72       ['name' => 'strawberry'],
73       ['name' => 'raspberry'],
74       ['name' => 'pear'],
75       ['name' => 'peach'],
76       ['name' => 'pineapple'],
77       ['name' => 'banana'],
78       ['name' => 'apple'],
79     ];
80
81     $converters[0]['sorted'] = [
82       'strawberry', 'raspberry', 'pear', 'peach',
83       'pineapple', 'banana', 'apple',
84     ];
85
86     $converters[1]['unsorted'] = [
87       ['name' => 'giraffe'],
88       ['name' => 'zebra'],
89       ['name' => 'eagle'],
90       ['name' => 'ape'],
91       ['name' => 'cat'],
92       ['name' => 'puppy'],
93       ['name' => 'llama'],
94     ];
95
96     $converters[1]['sorted'] = [
97       'giraffe', 'zebra', 'eagle', 'ape',
98       'cat', 'puppy', 'llama',
99     ];
100
101     return $converters;
102   }
103
104   /**
105    * Provide data for parameter converter manager tests.
106    *
107    * @return array
108    *   An array of arrays, each containing the input parameters for
109    *   providerTestResolvers::testGetConverter().
110    *
111    * @see ParamConverterManagerTest::testGetConverter()
112    */
113   public function providerTestGetConverter() {
114     return [
115       ['ape', 'ApeConverterClass'],
116       ['cat', 'CatConverterClass'],
117       ['puppy', 'PuppyConverterClass'],
118       ['llama', 'LlamaConverterClass'],
119       ['giraffe', 'GiraffeConverterClass'],
120       ['zebra', 'ZebraConverterClass'],
121       ['eagle', 'EagleConverterClass'],
122     ];
123   }
124
125   /**
126    * @covers ::setRouteParameterConverters
127    *
128    * @dataProvider providerTestSetRouteParameterConverters
129    */
130   public function testSetRouteParameterConverters($path, $parameters = NULL, $expected = NULL) {
131     $converter = $this->getMock('Drupal\Core\ParamConverter\ParamConverterInterface');
132     $converter->expects($this->any())
133       ->method('applies')
134       ->with($this->anything(), 'id', $this->anything())
135       ->will($this->returnValue(TRUE));
136     $this->manager->addConverter($converter, 'applied');
137
138     $route = new Route($path);
139     if ($parameters) {
140       $route->setOption('parameters', $parameters);
141     }
142     $collection = new RouteCollection();
143     $collection->add('test_route', $route);
144
145     $this->manager->setRouteParameterConverters($collection);
146     foreach ($collection as $route) {
147       $result = $route->getOption('parameters');
148       if ($expected) {
149         $this->assertSame($expected, $result['id']['converter']);
150       }
151       else {
152         $this->assertNull($result);
153       }
154     }
155   }
156
157   /**
158    * Provides data for testSetRouteParameterConverters().
159    */
160   public function providerTestSetRouteParameterConverters() {
161     return [
162       ['/test'],
163       ['/test/{id}', ['id' => []], 'applied'],
164       ['/test/{id}', ['id' => ['converter' => 'predefined']], 'predefined'],
165     ];
166   }
167
168   /**
169    * @covers ::convert
170    */
171   public function testConvert() {
172     $route = new Route('/test/{id}/{literal}/{null}');
173     $parameters = [
174       'id' => [
175         'converter' => 'test_convert',
176       ],
177       'literal' => [],
178       'null' => [],
179     ];
180     $route->setOption('parameters', $parameters);
181
182     $defaults = [
183       RouteObjectInterface::ROUTE_OBJECT => $route,
184       RouteObjectInterface::ROUTE_NAME => 'test_route',
185       'id' => 1,
186       'literal' => 'this is a literal',
187       'null' => NULL,
188     ];
189
190     $expected = $defaults;
191     $expected['id'] = 'something_better!';
192
193     $converter = $this->getMock('Drupal\Core\ParamConverter\ParamConverterInterface');
194     $converter->expects($this->any())
195       ->method('convert')
196       ->with(1, $this->isType('array'), 'id', $this->isType('array'))
197       ->will($this->returnValue('something_better!'));
198     $this->manager->addConverter($converter, 'test_convert');
199
200     $result = $this->manager->convert($defaults);
201
202     $this->assertEquals($expected, $result);
203   }
204
205   /**
206    * @covers ::convert
207    */
208   public function testConvertNoConverting() {
209     $route = new Route('/test');
210     $defaults = [
211       RouteObjectInterface::ROUTE_OBJECT => $route,
212       RouteObjectInterface::ROUTE_NAME => 'test_route',
213     ];
214
215     $expected = $defaults;
216
217     $result = $this->manager->convert($defaults);
218     $this->assertEquals($expected, $result);
219   }
220
221   /**
222    * @covers ::convert
223    */
224   public function testConvertMissingParam() {
225     $route = new Route('/test/{id}');
226     $parameters = [
227       'id' => [
228         'converter' => 'test_convert',
229       ],
230     ];
231     $route->setOption('parameters', $parameters);
232
233     $defaults = [
234       RouteObjectInterface::ROUTE_OBJECT => $route,
235       RouteObjectInterface::ROUTE_NAME => 'test_route',
236       'id' => 1,
237     ];
238
239     $converter = $this->getMock('Drupal\Core\ParamConverter\ParamConverterInterface');
240     $converter->expects($this->any())
241       ->method('convert')
242       ->with(1, $this->isType('array'), 'id', $this->isType('array'))
243       ->will($this->returnValue(NULL));
244     $this->manager->addConverter($converter, 'test_convert');
245
246     $this->setExpectedException(ParamNotConvertedException::class, 'The "id" parameter was not converted for the path "/test/{id}" (route name: "test_route")');
247     $this->manager->convert($defaults);
248   }
249
250 }