3 namespace Drupal\system\Tests\Common;
5 use Drupal\Component\Serialization\Json;
6 use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
8 use Drupal\simpletest\WebTestBase;
11 * Performs integration tests on drupal_render().
15 class RenderWebTest extends WebTestBase {
22 public static $modules = ['common_test'];
25 * Asserts the cache context for the wrapper format is always present.
27 public function testWrapperFormatCacheContext() {
28 $this->drupalGet('common-test/type-link-active-class');
29 $this->assertIdentical(0, strpos($this->getRawContent(), "<!DOCTYPE html>\n<html"));
30 $this->assertIdentical('text/html; charset=UTF-8', $this->drupalGetHeader('Content-Type'));
31 $this->assertTitle('Test active link class | Drupal');
32 $this->assertCacheContext('url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT);
34 $this->drupalGet('common-test/type-link-active-class', ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'json']]);
35 $this->assertIdentical('application/json', $this->drupalGetHeader('Content-Type'));
36 $json = Json::decode($this->getRawContent());
37 $this->assertEqual(['content', 'title'], array_keys($json));
38 $this->assertIdentical('Test active link class', $json['title']);
39 $this->assertCacheContext('url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT);
43 * Tests rendering form elements without passing through
44 * \Drupal::formBuilder()->doBuildForm().
46 public function testDrupalRenderFormElements() {
47 // Define a series of form elements.
50 '#value' => $this->randomMachineName(),
52 $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'submit']);
55 '#type' => 'textfield',
56 '#title' => $this->randomMachineName(),
57 '#value' => $this->randomMachineName(),
59 $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'text']);
62 '#type' => 'password',
63 '#title' => $this->randomMachineName(),
65 $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'password']);
68 '#type' => 'textarea',
69 '#title' => $this->randomMachineName(),
70 '#value' => $this->randomMachineName(),
72 $this->assertRenderedElement($element, '//textarea');
76 '#title' => $this->randomMachineName(),
79 $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'radio']);
82 '#type' => 'checkbox',
83 '#title' => $this->randomMachineName(),
85 $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'checkbox']);
89 '#title' => $this->randomMachineName(),
91 0 => $this->randomMachineName(),
92 1 => $this->randomMachineName(),
95 $this->assertRenderedElement($element, '//select');
99 '#title' => $this->randomMachineName(),
101 $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'file']);
105 '#title' => $this->randomMachineName(),
106 '#markup' => $this->randomMachineName(),
108 $this->assertRenderedElement($element, '//div[contains(@class, :class) and contains(., :markup)]/label[contains(., :label)]', [
109 ':class' => 'js-form-type-item',
110 ':markup' => $element['#markup'],
111 ':label' => $element['#title'],
116 '#title' => $this->randomMachineName(),
117 '#value' => $this->randomMachineName(),
119 $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'hidden']);
123 '#title' => $this->randomMachineName(),
124 '#url' => Url::fromRoute('common_test.destination'),
129 $this->assertRenderedElement($element, '//a[@href=:href and contains(., :title)]', [
130 ':href' => URL::fromRoute('common_test.destination')->setAbsolute()->toString(),
131 ':title' => $element['#title'],
135 '#type' => 'details',
137 '#title' => $this->randomMachineName(),
139 $this->assertRenderedElement($element, '//details/summary[contains(., :title)]', [
140 ':title' => $element['#title'],
144 '#type' => 'details',
146 '#title' => $this->randomMachineName(),
148 $this->assertRenderedElement($element, '//details');
152 '#title' => $this->randomMachineName(),
153 '#markup' => $this->randomMachineName(),
155 $this->assertRenderedElement($element, '//details/div/div[contains(@class, :class) and contains(., :markup)]', [
156 ':class' => 'js-form-type-item',
157 ':markup' => $element['item']['#markup'],
162 * Tests that elements are rendered properly.
164 protected function assertRenderedElement(array $element, $xpath, array $xpath_args = []) {
165 $original_element = $element;
166 $this->setRawContent(drupal_render_root($element));
167 $this->verbose('<hr />' . $this->getRawContent());
169 // @see \Drupal\simpletest\WebTestBase::xpath()
170 $xpath = $this->buildXPathQuery($xpath, $xpath_args);
171 $element += ['#value' => NULL];
172 $this->assertFieldByXPath($xpath, $element['#value'], format_string('#type @type was properly rendered.', [
173 '@type' => var_export($element['#type'], TRUE),