ConfirmFormTest.php 3.66 KB
Newer Older
1
2
<?php

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
18
19
20

  /**
   * Modules to enable.
   *
   * @var array
   */
21
  public static $modules = ['form_test'];
22

23
  public function testConfirmForm() {
24
25
    // Test the building of the form.
    $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
29
30
    $this->assertText(t('ConfirmFormTestForm::getDescription().'), 'The description was used.');
    $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.");
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
42
    $this->drupalPostForm('form-test/confirm-form', NULL, t('ConfirmFormTestForm::getConfirmText().'), ['query' => ['destination' => 'admin/config']]);
    $this->assertUrl('admin/config', [], "The form's redirect was not followed, the destination query string was followed.");
43
44
45
46

    // Test cancelling the form with a complex destination.
    $this->drupalGet('form-test/confirm-form-array-path');
    $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
52
53
54
55
  /**
   * Tests that the confirm form does not use external destinations.
   */
  public function testConfirmFormWithExternalDestination() {
    $this->drupalGet('form-test/confirm-form');
    $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
62
    $this->assertCancelLinkUrl(Url::fromRoute('<front>'));
    // 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
69
70
71
72
73
74
75
76
77
78
79
80
81
  }

  /**
   * Asserts that a cancel link is present pointing to the provided URL.
   *
   * @param \Drupal\Core\Url $url
   *   The url to check for.
   * @param string $message
   *   The assert message.
   * @param string $group
   *   The assertion group.
   *
   * @return bool
   *   Result of the assertion.
   */
  public function assertCancelLinkUrl(Url $url, $message = '', $group = 'Other') {
    $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
84
85
    return $this->assertTrue(isset($links[0]), $message, $group);
  }

86
}