Security update for Core, with self-updated composer
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Form / ConfirmFormHelperTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Form;
4
5 use Drupal\Core\Form\ConfirmFormHelper;
6 use Drupal\Core\Url;
7 use Drupal\Tests\UnitTestCase;
8 use Symfony\Component\DependencyInjection\ContainerBuilder;
9 use Symfony\Component\HttpFoundation\Request;
10
11 /**
12  * @coversDefaultClass \Drupal\Core\Form\ConfirmFormHelper
13  * @group Form
14  */
15 class ConfirmFormHelperTest extends UnitTestCase {
16
17   /**
18    * @covers ::buildCancelLink
19    *
20    * Tests the cancel link title.
21    */
22   public function testCancelLinkTitle() {
23     $cancel_text = 'Cancel text';
24     $form = $this->getMock('Drupal\Core\Form\ConfirmFormInterface');
25     $form->expects($this->any())
26       ->method('getCancelText')
27       ->will($this->returnValue($cancel_text));
28
29     $link = ConfirmFormHelper::buildCancelLink($form, new Request());
30     $this->assertSame($cancel_text, $link['#title']);
31     $this->assertSame(['contexts' => ['url.query_args:destination']], $link['#cache']);
32   }
33
34   /**
35    * @covers ::buildCancelLink
36    *
37    * Tests a cancel link route.
38    */
39   public function testCancelLinkRoute() {
40     $route_name = 'foo_bar';
41     $cancel_route = new Url($route_name);
42     $form = $this->getMock('Drupal\Core\Form\ConfirmFormInterface');
43     $form->expects($this->any())
44       ->method('getCancelUrl')
45       ->will($this->returnValue($cancel_route));
46     $link = ConfirmFormHelper::buildCancelLink($form, new Request());
47     $this->assertEquals(Url::fromRoute($route_name), $link['#url']);
48     $this->assertSame(['contexts' => ['url.query_args:destination']], $link['#cache']);
49   }
50
51   /**
52    * @covers ::buildCancelLink
53    *
54    * Tests a cancel link route with parameters.
55    */
56   public function testCancelLinkRouteWithParams() {
57     $expected = Url::fromRoute('foo_bar.baz', ['baz' => 'banana'], ['absolute' => TRUE]);
58     $form = $this->getMock('Drupal\Core\Form\ConfirmFormInterface');
59     $form->expects($this->any())
60       ->method('getCancelUrl')
61       ->will($this->returnValue($expected));
62     $link = ConfirmFormHelper::buildCancelLink($form, new Request());
63     $this->assertEquals($expected, $link['#url']);
64     $this->assertSame(['contexts' => ['url.query_args:destination']], $link['#cache']);
65   }
66
67   /**
68    * @covers ::buildCancelLink
69    *
70    * Tests a cancel link route with a URL object.
71    */
72   public function testCancelLinkRouteWithUrl() {
73     $cancel_route = new Url(
74       'foo_bar.baz', [
75         'baz' => 'banana',
76       ],
77       [
78         'absolute' => TRUE,
79       ]
80     );
81     $form = $this->getMock('Drupal\Core\Form\ConfirmFormInterface');
82     $form->expects($this->any())
83       ->method('getCancelUrl')
84       ->will($this->returnValue($cancel_route));
85     $link = ConfirmFormHelper::buildCancelLink($form, new Request());
86     $this->assertSame($cancel_route, $link['#url']);
87     $this->assertSame(['contexts' => ['url.query_args:destination']], $link['#cache']);
88   }
89
90   /**
91    * @covers ::buildCancelLink
92    *
93    * Tests a cancel link provided by the destination.
94    *
95    * @dataProvider providerTestCancelLinkDestination
96    */
97   public function testCancelLinkDestination($destination) {
98     $query = ['destination' => $destination];
99     $form = $this->getMock('Drupal\Core\Form\ConfirmFormInterface');
100
101     $path_validator = $this->getMock('Drupal\Core\Path\PathValidatorInterface');
102     $container_builder = new ContainerBuilder();
103     $container_builder->set('path.validator', $path_validator);
104     \Drupal::setContainer($container_builder);
105
106     $url = Url::fromRoute('test_route');
107     $path_validator->expects($this->atLeastOnce())
108       ->method('getUrlIfValidWithoutAccessCheck')
109       ->with('baz')
110       ->willReturn($url);
111
112     $link = ConfirmFormHelper::buildCancelLink($form, new Request($query));
113     $this->assertSame($url, $link['#url']);
114     $this->assertSame(['contexts' => ['url.query_args:destination']], $link['#cache']);
115   }
116
117   /**
118    * Provides test data for testCancelLinkDestination().
119    */
120   public function providerTestCancelLinkDestination() {
121     $data = [];
122     $data[] = ['baz'];
123     $data[] = ['/baz'];
124     return $data;
125   }
126
127 }