Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\system\Tests\Common;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Serialization\Json;
|
Chris@0
|
6 use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
|
Chris@0
|
7 use Drupal\Core\Url;
|
Chris@0
|
8 use Drupal\simpletest\WebTestBase;
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * Performs integration tests on drupal_render().
|
Chris@0
|
12 *
|
Chris@0
|
13 * @group Common
|
Chris@0
|
14 */
|
Chris@0
|
15 class RenderWebTest extends WebTestBase {
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * Modules to enable.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @var array
|
Chris@0
|
21 */
|
Chris@0
|
22 public static $modules = ['common_test'];
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * Asserts the cache context for the wrapper format is always present.
|
Chris@0
|
26 */
|
Chris@0
|
27 public function testWrapperFormatCacheContext() {
|
Chris@0
|
28 $this->drupalGet('common-test/type-link-active-class');
|
Chris@0
|
29 $this->assertIdentical(0, strpos($this->getRawContent(), "<!DOCTYPE html>\n<html"));
|
Chris@0
|
30 $this->assertIdentical('text/html; charset=UTF-8', $this->drupalGetHeader('Content-Type'));
|
Chris@0
|
31 $this->assertTitle('Test active link class | Drupal');
|
Chris@0
|
32 $this->assertCacheContext('url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT);
|
Chris@0
|
33
|
Chris@0
|
34 $this->drupalGet('common-test/type-link-active-class', ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'json']]);
|
Chris@0
|
35 $this->assertIdentical('application/json', $this->drupalGetHeader('Content-Type'));
|
Chris@0
|
36 $json = Json::decode($this->getRawContent());
|
Chris@0
|
37 $this->assertEqual(['content', 'title'], array_keys($json));
|
Chris@0
|
38 $this->assertIdentical('Test active link class', $json['title']);
|
Chris@0
|
39 $this->assertCacheContext('url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT);
|
Chris@0
|
40 }
|
Chris@0
|
41
|
Chris@0
|
42 /**
|
Chris@0
|
43 * Tests rendering form elements without passing through
|
Chris@0
|
44 * \Drupal::formBuilder()->doBuildForm().
|
Chris@0
|
45 */
|
Chris@0
|
46 public function testDrupalRenderFormElements() {
|
Chris@0
|
47 // Define a series of form elements.
|
Chris@0
|
48 $element = [
|
Chris@0
|
49 '#type' => 'button',
|
Chris@0
|
50 '#value' => $this->randomMachineName(),
|
Chris@0
|
51 ];
|
Chris@0
|
52 $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'submit']);
|
Chris@0
|
53
|
Chris@0
|
54 $element = [
|
Chris@0
|
55 '#type' => 'textfield',
|
Chris@0
|
56 '#title' => $this->randomMachineName(),
|
Chris@0
|
57 '#value' => $this->randomMachineName(),
|
Chris@0
|
58 ];
|
Chris@0
|
59 $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'text']);
|
Chris@0
|
60
|
Chris@0
|
61 $element = [
|
Chris@0
|
62 '#type' => 'password',
|
Chris@0
|
63 '#title' => $this->randomMachineName(),
|
Chris@0
|
64 ];
|
Chris@0
|
65 $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'password']);
|
Chris@0
|
66
|
Chris@0
|
67 $element = [
|
Chris@0
|
68 '#type' => 'textarea',
|
Chris@0
|
69 '#title' => $this->randomMachineName(),
|
Chris@0
|
70 '#value' => $this->randomMachineName(),
|
Chris@0
|
71 ];
|
Chris@0
|
72 $this->assertRenderedElement($element, '//textarea');
|
Chris@0
|
73
|
Chris@0
|
74 $element = [
|
Chris@0
|
75 '#type' => 'radio',
|
Chris@0
|
76 '#title' => $this->randomMachineName(),
|
Chris@0
|
77 '#value' => FALSE,
|
Chris@0
|
78 ];
|
Chris@0
|
79 $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'radio']);
|
Chris@0
|
80
|
Chris@0
|
81 $element = [
|
Chris@0
|
82 '#type' => 'checkbox',
|
Chris@0
|
83 '#title' => $this->randomMachineName(),
|
Chris@0
|
84 ];
|
Chris@0
|
85 $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'checkbox']);
|
Chris@0
|
86
|
Chris@0
|
87 $element = [
|
Chris@0
|
88 '#type' => 'select',
|
Chris@0
|
89 '#title' => $this->randomMachineName(),
|
Chris@0
|
90 '#options' => [
|
Chris@0
|
91 0 => $this->randomMachineName(),
|
Chris@0
|
92 1 => $this->randomMachineName(),
|
Chris@0
|
93 ],
|
Chris@0
|
94 ];
|
Chris@0
|
95 $this->assertRenderedElement($element, '//select');
|
Chris@0
|
96
|
Chris@0
|
97 $element = [
|
Chris@0
|
98 '#type' => 'file',
|
Chris@0
|
99 '#title' => $this->randomMachineName(),
|
Chris@0
|
100 ];
|
Chris@0
|
101 $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'file']);
|
Chris@0
|
102
|
Chris@0
|
103 $element = [
|
Chris@0
|
104 '#type' => 'item',
|
Chris@0
|
105 '#title' => $this->randomMachineName(),
|
Chris@0
|
106 '#markup' => $this->randomMachineName(),
|
Chris@0
|
107 ];
|
Chris@0
|
108 $this->assertRenderedElement($element, '//div[contains(@class, :class) and contains(., :markup)]/label[contains(., :label)]', [
|
Chris@0
|
109 ':class' => 'js-form-type-item',
|
Chris@0
|
110 ':markup' => $element['#markup'],
|
Chris@0
|
111 ':label' => $element['#title'],
|
Chris@0
|
112 ]);
|
Chris@0
|
113
|
Chris@0
|
114 $element = [
|
Chris@0
|
115 '#type' => 'hidden',
|
Chris@0
|
116 '#title' => $this->randomMachineName(),
|
Chris@0
|
117 '#value' => $this->randomMachineName(),
|
Chris@0
|
118 ];
|
Chris@0
|
119 $this->assertRenderedElement($element, '//input[@type=:type]', [':type' => 'hidden']);
|
Chris@0
|
120
|
Chris@0
|
121 $element = [
|
Chris@0
|
122 '#type' => 'link',
|
Chris@0
|
123 '#title' => $this->randomMachineName(),
|
Chris@0
|
124 '#url' => Url::fromRoute('common_test.destination'),
|
Chris@0
|
125 '#options' => [
|
Chris@0
|
126 'absolute' => TRUE,
|
Chris@0
|
127 ],
|
Chris@0
|
128 ];
|
Chris@0
|
129 $this->assertRenderedElement($element, '//a[@href=:href and contains(., :title)]', [
|
Chris@0
|
130 ':href' => URL::fromRoute('common_test.destination')->setAbsolute()->toString(),
|
Chris@0
|
131 ':title' => $element['#title'],
|
Chris@0
|
132 ]);
|
Chris@0
|
133
|
Chris@0
|
134 $element = [
|
Chris@0
|
135 '#type' => 'details',
|
Chris@0
|
136 '#open' => TRUE,
|
Chris@0
|
137 '#title' => $this->randomMachineName(),
|
Chris@0
|
138 ];
|
Chris@0
|
139 $this->assertRenderedElement($element, '//details/summary[contains(., :title)]', [
|
Chris@0
|
140 ':title' => $element['#title'],
|
Chris@0
|
141 ]);
|
Chris@0
|
142
|
Chris@0
|
143 $element = [
|
Chris@0
|
144 '#type' => 'details',
|
Chris@0
|
145 '#open' => TRUE,
|
Chris@0
|
146 '#title' => $this->randomMachineName(),
|
Chris@0
|
147 ];
|
Chris@0
|
148 $this->assertRenderedElement($element, '//details');
|
Chris@0
|
149
|
Chris@0
|
150 $element['item'] = [
|
Chris@0
|
151 '#type' => 'item',
|
Chris@0
|
152 '#title' => $this->randomMachineName(),
|
Chris@0
|
153 '#markup' => $this->randomMachineName(),
|
Chris@0
|
154 ];
|
Chris@0
|
155 $this->assertRenderedElement($element, '//details/div/div[contains(@class, :class) and contains(., :markup)]', [
|
Chris@0
|
156 ':class' => 'js-form-type-item',
|
Chris@0
|
157 ':markup' => $element['item']['#markup'],
|
Chris@0
|
158 ]);
|
Chris@0
|
159 }
|
Chris@0
|
160
|
Chris@0
|
161 /**
|
Chris@0
|
162 * Tests that elements are rendered properly.
|
Chris@0
|
163 */
|
Chris@0
|
164 protected function assertRenderedElement(array $element, $xpath, array $xpath_args = []) {
|
Chris@0
|
165 $original_element = $element;
|
Chris@0
|
166 $this->setRawContent(drupal_render_root($element));
|
Chris@0
|
167 $this->verbose('<hr />' . $this->getRawContent());
|
Chris@0
|
168
|
Chris@0
|
169 // @see \Drupal\simpletest\WebTestBase::xpath()
|
Chris@0
|
170 $xpath = $this->buildXPathQuery($xpath, $xpath_args);
|
Chris@0
|
171 $element += ['#value' => NULL];
|
Chris@0
|
172 $this->assertFieldByXPath($xpath, $element['#value'], format_string('#type @type was properly rendered.', [
|
Chris@0
|
173 '@type' => var_export($element['#type'], TRUE),
|
Chris@0
|
174 ]));
|
Chris@0
|
175 }
|
Chris@0
|
176
|
Chris@0
|
177 }
|