3 namespace Drupal\Tests\system\Kernel\Common;
6 use Drupal\KernelTests\KernelTestBase;
9 * Performs integration tests on drupal_render().
13 class FormElementsRenderTest extends KernelTestBase {
18 protected static $modules = ['common_test', 'system'];
21 * Tests rendering form elements without passing through
22 * \Drupal::formBuilder()->doBuildForm().
24 public function testDrupalRenderFormElements() {
25 // Define a series of form elements.
28 '#value' => $this->randomMachineName(),
30 $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'submit']);
33 '#type' => 'textfield',
34 '#title' => $this->randomMachineName(),
35 '#value' => $this->randomMachineName(),
37 $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'text']);
40 '#type' => 'password',
41 '#title' => $this->randomMachineName(),
43 $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'password']);
46 '#type' => 'textarea',
47 '#title' => $this->randomMachineName(),
48 '#value' => $this->randomMachineName(),
50 $this->assertRenderedElement($element, '//textarea');
54 '#title' => $this->randomMachineName(),
57 $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'radio']);
60 '#type' => 'checkbox',
61 '#title' => $this->randomMachineName(),
63 $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'checkbox']);
67 '#title' => $this->randomMachineName(),
69 0 => $this->randomMachineName(),
70 1 => $this->randomMachineName(),
73 $this->assertRenderedElement($element, '//select');
77 '#title' => $this->randomMachineName(),
79 $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'file']);
83 '#title' => $this->randomMachineName(),
84 '#markup' => $this->randomMachineName(),
86 $this->assertRenderedElement($element, '//div[contains(@class, :class) and contains(., :markup)]/label[contains(., :label)]', [
87 ':class' => 'js-form-type-item',
88 ':markup' => $element['#markup'],
89 ':label' => $element['#title'],
94 '#title' => $this->randomMachineName(),
95 '#value' => $this->randomMachineName(),
97 $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'hidden']);
101 '#title' => $this->randomMachineName(),
102 '#url' => Url::fromRoute('common_test.destination'),
107 $this->assertRenderedElement($element, '//a[@href=:href and contains(., :title)]', [
108 ':href' => URL::fromRoute('common_test.destination')->setAbsolute()->toString(),
109 ':title' => $element['#title'],
113 '#type' => 'details',
115 '#title' => $this->randomMachineName(),
117 $this->assertRenderedElement($element, '//details/summary[contains(., :title)]', [
118 ':title' => $element['#title'],
122 '#type' => 'details',
124 '#title' => $this->randomMachineName(),
126 $this->assertRenderedElement($element, '//details');
130 '#title' => $this->randomMachineName(),
131 '#markup' => $this->randomMachineName(),
133 $this->assertRenderedElement($element, '//details/div[contains(@class, :class) and contains(., :markup)]', [
134 ':class' => 'js-form-type-item',
135 ':markup' => $element['item']['#markup'],
140 * Tests that elements are rendered properly.
142 protected function assertRenderedElement(array $element, $xpath, array $xpath_args = []) {
143 $this->render($element);
145 // @see \Drupal\simpletest\WebTestBase::xpath()
146 $xpath = $this->buildXPathQuery($xpath, $xpath_args);
147 $element += ['#value' => NULL];
148 $this->assertFieldByXPath($xpath, $element['#value'], format_string('#type @type was properly rendered.', [
149 '@type' => var_export($element['#type'], TRUE),