3 namespace Drupal\Tests\Core\Form;
5 use Drupal\Core\Form\ConfirmFormHelper;
7 use Drupal\Tests\UnitTestCase;
8 use Symfony\Component\DependencyInjection\ContainerBuilder;
9 use Symfony\Component\HttpFoundation\Request;
12 * @coversDefaultClass \Drupal\Core\Form\ConfirmFormHelper
15 class ConfirmFormHelperTest extends UnitTestCase {
18 * @covers ::buildCancelLink
20 * Tests the cancel link title.
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));
29 $link = ConfirmFormHelper::buildCancelLink($form, new Request());
30 $this->assertSame($cancel_text, $link['#title']);
31 $this->assertSame(['contexts' => ['url.query_args:destination']], $link['#cache']);
35 * @covers ::buildCancelLink
37 * Tests a cancel link route.
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']);
52 * @covers ::buildCancelLink
54 * Tests a cancel link route with parameters.
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']);
68 * @covers ::buildCancelLink
70 * Tests a cancel link route with a URL object.
72 public function testCancelLinkRouteWithUrl() {
73 $cancel_route = new Url(
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']);
91 * @covers ::buildCancelLink
93 * Tests a cancel link provided by the destination.
95 * @dataProvider providerTestCancelLinkDestination
97 public function testCancelLinkDestination($destination) {
98 $query = ['destination' => $destination];
99 $form = $this->getMock('Drupal\Core\Form\ConfirmFormInterface');
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);
106 $url = Url::fromRoute('test_route');
107 $path_validator->expects($this->atLeastOnce())
108 ->method('getUrlIfValidWithoutAccessCheck')
112 $link = ConfirmFormHelper::buildCancelLink($form, new Request($query));
113 $this->assertSame($url, $link['#url']);
114 $this->assertSame(['contexts' => ['url.query_args:destination']], $link['#cache']);
118 * Provides test data for testCancelLinkDestination().
120 public function providerTestCancelLinkDestination() {