Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Enhancer / ParamConversionEnhancerTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Enhancer;
4
5 use Drupal\Core\Routing\Enhancer\ParamConversionEnhancer;
6 use Drupal\Tests\UnitTestCase;
7 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
8 use Symfony\Component\HttpFoundation\ParameterBag;
9 use Symfony\Component\HttpFoundation\Request;
10 use Symfony\Component\Routing\Route;
11
12 /**
13  * @coversDefaultClass \Drupal\Core\Routing\Enhancer\ParamConversionEnhancer
14  * @group Enhancer
15  */
16 class ParamConversionEnhancerTest extends UnitTestCase {
17
18   /**
19    * @var \Drupal\Core\Routing\Enhancer\ParamConversionEnhancer
20    */
21   protected $paramConversionEnhancer;
22
23   /**
24    * @var \Drupal\Core\ParamConverter\ParamConverterManagerInterface|\PHPUnit_Framework_MockObject_MockObject
25    */
26   protected $paramConverterManager;
27
28   /**
29    * {@inheritdoc}
30    */
31   protected function setUp() {
32     parent::setUp();
33
34     $this->paramConverterManager = $this->getMock('Drupal\Core\ParamConverter\ParamConverterManagerInterface');
35     $this->paramConversionEnhancer = new ParamConversionEnhancer($this->paramConverterManager);
36   }
37
38   /**
39    * @covers ::enhance
40    */
41   public function testEnhance() {
42     $route = new Route('/test/{id}/{literal}/{null}');
43
44     $raw_variables = [
45       'id' => 1,
46       'literal' => 'this is a literal',
47       'null' => NULL,
48     ];
49     $defaults = [
50       RouteObjectInterface::ROUTE_OBJECT => $route,
51     ] + $raw_variables;
52
53     $expected = $defaults;
54     $expected['id'] = 'something_better!';
55     $expected['_raw_variables'] = new ParameterBag($raw_variables);
56
57     $this->paramConverterManager->expects($this->once())
58       ->method('convert')
59       ->with($this->isType('array'))
60       ->will($this->returnValue($expected));
61
62     $result = $this->paramConversionEnhancer->enhance($defaults, new Request());
63
64     $this->assertEquals($expected, $result);
65
66     // Now run with the results as the new defaults to ensure that the
67     // conversion is just run once.
68     $result = $this->paramConversionEnhancer->enhance($result, new Request());
69
70     $this->assertEquals($expected, $result);
71   }
72
73   /**
74    * @covers ::copyRawVariables
75    */
76   public function testCopyRawVariables() {
77     $route = new Route('/test/{id}');
78     $defaults = [
79       RouteObjectInterface::ROUTE_OBJECT => $route,
80       'id' => '1',
81     ];
82     // Set one default to mirror another by reference.
83     $defaults['bar'] = &$defaults['id'];
84     $this->paramConverterManager->expects($this->any())
85       ->method('convert')
86       ->with($this->isType('array'))
87       ->will($this->returnCallback(function ($defaults) {
88         // Convert the mirrored default to another value.
89         $defaults['bar'] = '2';
90         return $defaults;
91       }));
92     $expected = new ParameterBag(['id' => 1]);
93     $result = $this->paramConversionEnhancer->enhance($defaults, new Request());
94     $this->assertEquals($result['_raw_variables'], $expected);
95   }
96
97 }