ElementTest.php 5.98 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php

/**
 * @file
 * Definition of Drupal\system\Tests\Form\ElementTest.
 */

namespace Drupal\system\Tests\Form;

use Drupal\simpletest\WebTestBase;

/**
 * Tests building and processing of core form elements.
 */
class ElementTest extends WebTestBase {
16 17 18 19 20 21 22 23

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('form_test');

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
  public static function getInfo() {
    return array(
      'name' => 'Element processing',
      'description' => 'Tests building and processing of core form elements.',
      'group' => 'Form API',
    );
  }

  /**
   * Tests placeholder text for elements that support placeholders.
   */
  function testPlaceHolderText() {
    $this->drupalGet('form-test/placeholder-text');
    $expected = 'placeholder-text';
    // Test to make sure non-textarea elements have the proper placeholder text.
    foreach (array('textfield', 'tel', 'url', 'password', 'email', 'number') as $type) {
      $element = $this->xpath('//input[@id=:id and @placeholder=:expected]', array(
        ':id' => 'edit-' . $type,
        ':expected' => $expected,
      ));
44
      $this->assertTrue(!empty($element), format_string('Placeholder text placed in @type.', array('@type' => $type)));
45 46 47 48 49 50 51
    }

    // Test to make sure textarea has the proper placeholder text.
    $element = $this->xpath('//textarea[@id=:id and @placeholder=:expected]', array(
      ':id' => 'edit-textarea',
      ':expected' => $expected,
    ));
52
    $this->assertTrue(!empty($element), 'Placeholder text placed in textarea.');
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  }

  /**
   * Tests expansion of #options for #type checkboxes and radios.
   */
  function testOptions() {
    $this->drupalGet('form-test/checkboxes-radios');

    // Verify that all options appear in their defined order.
    foreach (array('checkbox', 'radio') as $type) {
      $elements = $this->xpath('//input[@type=:type]', array(':type' => $type));
      $expected_values = array('0', 'foo', '1', 'bar', '>');
      foreach ($elements as $element) {
        $expected = array_shift($expected_values);
        $this->assertIdentical((string) $element['value'], $expected);
      }
    }

    // Enable customized option sub-elements.
    $this->drupalGet('form-test/checkboxes-radios/customize');

    // Verify that all options appear in their defined order, taking a custom
    // #weight into account.
    foreach (array('checkbox', 'radio') as $type) {
      $elements = $this->xpath('//input[@type=:type]', array(':type' => $type));
      $expected_values = array('0', 'foo', 'bar', '>', '1');
      foreach ($elements as $element) {
        $expected = array_shift($expected_values);
        $this->assertIdentical((string) $element['value'], $expected);
      }
    }
    // Verify that custom #description properties are output.
    foreach (array('checkboxes', 'radios') as $type) {
      $elements = $this->xpath('//input[@id=:id]/following-sibling::div[@class=:class]', array(
        ':id' => 'edit-' . $type . '-foo',
        ':class' => 'description',
      ));
90
      $this->assertTrue(count($elements), format_string('Custom %type option description found.', array(
91 92 93 94
        '%type' => $type,
      )));
    }
  }
95 96 97 98 99 100

  /**
   * Tests button classes.
   */
  function testButtonClasses() {
    $this->drupalGet('form-test/button-class');
101 102 103
    // Just contains(@class, "button") won't do because then
    // "button-foo" would contain "button". Instead, check
    // " button ". Make sure it matches in the beginning and the end too
104
    // by adding a space before and after.
105 106 107
    $this->assertEqual(2, count($this->xpath('//*[contains(concat(" ", @class, " "), " button ")]')));
    $this->assertEqual(1, count($this->xpath('//*[contains(concat(" ", @class, " "), " button-foo ")]')));
    $this->assertEqual(1, count($this->xpath('//*[contains(concat(" ", @class, " "), " button-danger ")]')));
108 109
  }

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
  /**
   * Tests the #group property.
   */
  function testGroupElements() {
    $this->drupalGet('form-test/group-details');
    $elements = $this->xpath('//div[@class="details-wrapper"]//div[@class="details-wrapper"]//label');
    $this->assertTrue(count($elements) == 1);
    $this->drupalGet('form-test/group-container');
    $elements = $this->xpath('//div[@id="edit-container"]//div[@class="details-wrapper"]//label');
    $this->assertTrue(count($elements) == 1);
    $this->drupalGet('form-test/group-fieldset');
    $elements = $this->xpath('//fieldset[@id="edit-fieldset"]//div[@id="edit-meta"]//label');
    $this->assertTrue(count($elements) == 1);
    $this->drupalGet('form-test/group-vertical-tabs');
    $elements = $this->xpath('//div[@class="vertical-tabs-panes"]//details[@id="edit-meta"]//label');
    $this->assertTrue(count($elements) == 1);
    $elements = $this->xpath('//div[@class="vertical-tabs-panes"]//details[@id="edit-meta-2"]//label');
    $this->assertTrue(count($elements) == 1);
  }
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

  /**
   * Tests a form with a autocomplete setting..
   */
  public function testFormAutocomplete() {
    $this->drupalGet('form-test/autocomplete');

    $result = $this->xpath('//input[@id = "edit-autocomplete-1-autocomplete"]');
    $this->assertEqual(count($result), 0, 'Ensure that the user does not have access to the autocompletion');
    $result = $this->xpath('//input[@id = "edit-autocomplete-2-autocomplete"]');
    $this->assertEqual(count($result), 0, 'Ensure that the user did not had access to the autocompletion');

    $user = $this->drupalCreateUser(array('access autocomplete test'));
    $this->drupalLogin($user);
    $this->drupalGet('form-test/autocomplete');

    $result = $this->xpath('//input[@id = "edit-autocomplete-1-autocomplete"]');
    $this->assertEqual((string) $result[0]['value'], url('form-test/autocomplete-1'));
    $this->assertEqual(count($result), 1, 'Ensure that the user does have access to the autocompletion');
    $result = $this->xpath('//input[@id = "edit-autocomplete-2-autocomplete"]');
    $this->assertEqual((string) $result[0]['value'], url('form-test/autocomplete-2/value'));
    $this->assertEqual(count($result), 1, 'Ensure that the user does have access to the autocompletion');
  }

153
}