3 namespace Drupal\Tests\system\Functional\Form;
5 use Drupal\Component\Utility\SafeMarkup;
7 use Drupal\Tests\BrowserTestBase;
10 * Tests confirmation forms.
14 class ConfirmFormTest extends BrowserTestBase {
21 public static $modules = ['form_test'];
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.');
31 // Test cancelling the form.
32 $this->clickLink(t('ConfirmFormTestForm::getCancelText().'));
33 $this->assertUrl('form-test/autocomplete', [], "The form's cancel link was followed.");
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.");
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.");
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.");
51 * Tests that the confirm form does not use external destinations.
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'));
68 * Asserts that a cancel link is present pointing to the provided URL.
70 * @param \Drupal\Core\Url $url
71 * The url to check for.
72 * @param string $message
74 * @param string $group
75 * The assertion group.
78 * Result of the assertion.
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);