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

3
namespace Drupal\Tests\system\Functional\Form;
4

5
use Drupal\Component\Render\FormattableMarkup;
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
  protected static $modules = ['form_test'];
22

23 24 25 26 27
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

28
  public function testConfirmForm() {
29 30
    // Test the building of the form.
    $this->drupalGet('form-test/confirm-form');
31
    $site_name = $this->config('system.site')->get('name');
32
    $this->assertTitle("ConfirmFormTestForm::getQuestion(). | $site_name");
33 34 35
    $this->assertText(t('ConfirmFormTestForm::getDescription().'), 'The description was used.');
    $this->assertFieldByXPath('//input[@id="edit-submit"]', t('ConfirmFormTestForm::getConfirmText().'), 'The confirm text was used.');

36
    // Test cancelling the form.
37
    $this->clickLink(t('ConfirmFormTestForm::getCancelText().'));
38
    $this->assertUrl('form-test/autocomplete', [], "The form's cancel link was followed.");
39 40

    // Test submitting the form.
41
    $this->drupalPostForm('form-test/confirm-form', NULL, t('ConfirmFormTestForm::getConfirmText().'));
42
    $this->assertText('The ConfirmFormTestForm::submitForm() method was used for this form.');
43
    $this->assertUrl('', [], "The form's redirect was followed.");
44 45

    // Test submitting the form with a destination.
46 47
    $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.");
48 49 50 51

    // Test cancelling the form with a complex destination.
    $this->drupalGet('form-test/confirm-form-array-path');
    $this->clickLink(t('ConfirmFormArrayPathTestForm::getCancelText().'));
52
    $this->assertUrl('form-test/confirm-form', ['query' => ['destination' => 'admin/config']], "The form's complex cancel link was followed.");
53 54
  }

55 56 57 58 59 60
  /**
   * 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'));
61
    $this->drupalGet('form-test/confirm-form', ['query' => ['destination' => 'node']]);
62
    $this->assertCancelLinkUrl(Url::fromUri('internal:/node'));
63
    $this->drupalGet('form-test/confirm-form', ['query' => ['destination' => 'http://example.com']]);
64
    $this->assertCancelLinkUrl(Url::fromRoute('form_test.route8'));
65
    $this->drupalGet('form-test/confirm-form', ['query' => ['destination' => '<front>']]);
66 67
    $this->assertCancelLinkUrl(Url::fromRoute('<front>'));
    // Other invalid destinations, should fall back to the form default.
68
    $this->drupalGet('form-test/confirm-form', ['query' => ['destination' => '/http://example.com']]);
69
    $this->assertCancelLinkUrl(Url::fromRoute('form_test.route8'));
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  }

  /**
   * 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()]);
87
    $message = ($message ? $message : new FormattableMarkup('Cancel link with URL %url found.', ['%url' => $url->toString()]));
88 89 90
    return $this->assertTrue(isset($links[0]), $message, $group);
  }

91
}