Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / system / tests / src / Functional / Form / ConfirmFormTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Form;
4
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\Core\Url;
7 use Drupal\Tests\BrowserTestBase;
8
9 /**
10  * Tests confirmation forms.
11  *
12  * @group Form
13  */
14 class ConfirmFormTest extends BrowserTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['form_test'];
22
23   public function testConfirmForm() {
24     // Test the building of the form.
25     $this->drupalGet('form-test/confirm-form');
26     $site_name = $this->config('system.site')->get('name');
27     $this->assertTitle(t('ConfirmFormTestForm::getQuestion(). | @site-name', ['@site-name' => $site_name]), 'The question was found as the page title.');
28     $this->assertText(t('ConfirmFormTestForm::getDescription().'), 'The description was used.');
29     $this->assertFieldByXPath('//input[@id="edit-submit"]', t('ConfirmFormTestForm::getConfirmText().'), 'The confirm text was used.');
30
31     // Test cancelling the form.
32     $this->clickLink(t('ConfirmFormTestForm::getCancelText().'));
33     $this->assertUrl('form-test/autocomplete', [], "The form's cancel link was followed.");
34
35     // Test submitting the form.
36     $this->drupalPostForm('form-test/confirm-form', NULL, t('ConfirmFormTestForm::getConfirmText().'));
37     $this->assertText('The ConfirmFormTestForm::submitForm() method was used for this form.');
38     $this->assertUrl('', [], "The form's redirect was followed.");
39
40     // Test submitting the form with a destination.
41     $this->drupalPostForm('form-test/confirm-form', NULL, t('ConfirmFormTestForm::getConfirmText().'), ['query' => ['destination' => 'admin/config']]);
42     $this->assertUrl('admin/config', [], "The form's redirect was not followed, the destination query string was followed.");
43
44     // Test cancelling the form with a complex destination.
45     $this->drupalGet('form-test/confirm-form-array-path');
46     $this->clickLink(t('ConfirmFormArrayPathTestForm::getCancelText().'));
47     $this->assertUrl('form-test/confirm-form', ['query' => ['destination' => 'admin/config']], "The form's complex cancel link was followed.");
48   }
49
50   /**
51    * Tests that the confirm form does not use external destinations.
52    */
53   public function testConfirmFormWithExternalDestination() {
54     $this->drupalGet('form-test/confirm-form');
55     $this->assertCancelLinkUrl(Url::fromRoute('form_test.route8'));
56     $this->drupalGet('form-test/confirm-form', ['query' => ['destination' => 'node']]);
57     $this->assertCancelLinkUrl(Url::fromUri('internal:/node'));
58     $this->drupalGet('form-test/confirm-form', ['query' => ['destination' => 'http://example.com']]);
59     $this->assertCancelLinkUrl(Url::fromRoute('form_test.route8'));
60     $this->drupalGet('form-test/confirm-form', ['query' => ['destination' => '<front>']]);
61     $this->assertCancelLinkUrl(Url::fromRoute('<front>'));
62     // Other invalid destinations, should fall back to the form default.
63     $this->drupalGet('form-test/confirm-form', ['query' => ['destination' => '/http://example.com']]);
64     $this->assertCancelLinkUrl(Url::fromRoute('form_test.route8'));
65   }
66
67   /**
68    * Asserts that a cancel link is present pointing to the provided URL.
69    *
70    * @param \Drupal\Core\Url $url
71    *   The url to check for.
72    * @param string $message
73    *   The assert message.
74    * @param string $group
75    *   The assertion group.
76    *
77    * @return bool
78    *   Result of the assertion.
79    */
80   public function assertCancelLinkUrl(Url $url, $message = '', $group = 'Other') {
81     $links = $this->xpath('//a[@href=:url]', [':url' => $url->toString()]);
82     $message = ($message ? $message : SafeMarkup::format('Cancel link with URL %url found.', ['%url' => $url->toString()]));
83     return $this->assertTrue(isset($links[0]), $message, $group);
84   }
85
86 }