Newer
Older
<?php
/**
* @file
* Definition of Drupal\system\Tests\Common\RenderTest.
*/
namespace Drupal\system\Tests\Common;
use Drupal\simpletest\WebTestBase;
/**
* Tests drupal_render().
*/
class RenderTest extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('common_test');
public static function getInfo() {
return array(
'name' => 'drupal_render()',
'description' => 'Performs functional tests on drupal_render().',
'group' => 'Common',
);
}
/**
* Test sorting by weight.
*/
function testDrupalRenderSorting() {
$first = $this->randomName();
$second = $this->randomName();
// Build an array with '#weight' set for each element.
$elements = array(
'second' => array(
'#weight' => 10,
'#markup' => $second,
),
'first' => array(
'#weight' => 0,
'#markup' => $first,
),
);
$output = drupal_render($elements);
// The lowest weight element should appear last in $output.
Jennifer Hodgdon
committed
$this->assertTrue(strpos($output, $second) > strpos($output, $first), 'Elements were sorted correctly by weight.');
// Confirm that the $elements array has '#sorted' set to TRUE.
Jennifer Hodgdon
committed
$this->assertTrue($elements['#sorted'], "'#sorted' => TRUE was added to the array");
// Pass $elements through element_children() and ensure it remains
// sorted in the correct order. drupal_render() will return an empty string
// if used on the same array in the same request.
$children = element_children($elements);
Jennifer Hodgdon
committed
$this->assertTrue(array_shift($children) == 'first', 'Child found in the correct order.');
$this->assertTrue(array_shift($children) == 'second', 'Child found in the correct order.');
// The same array structure again, but with #sorted set to TRUE.
$elements = array(
'second' => array(
'#weight' => 10,
'#markup' => $second,
),
'first' => array(
'#weight' => 0,
'#markup' => $first,
),
'#sorted' => TRUE,
);
$output = drupal_render($elements);
// The elements should appear in output in the same order as the array.
Jennifer Hodgdon
committed
$this->assertTrue(strpos($output, $second) < strpos($output, $first), 'Elements were not sorted.');
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
}
/**
* Test #attached functionality in children elements.
*/
function testDrupalRenderChildrenAttached() {
// The cache system is turned off for POST requests.
$request_method = $_SERVER['REQUEST_METHOD'];
$_SERVER['REQUEST_METHOD'] = 'GET';
// Create an element with a child and subchild. Each element loads a
// different JavaScript file using #attached.
$parent_js = drupal_get_path('module', 'user') . '/user.js';
$child_js = drupal_get_path('module', 'forum') . '/forum.js';
$subchild_js = drupal_get_path('module', 'book') . '/book.js';
$element = array(
'#type' => 'fieldset',
'#cache' => array(
'keys' => array('simpletest', 'drupal_render', 'children_attached'),
),
'#attached' => array('js' => array($parent_js)),
'#title' => 'Parent',
);
$element['child'] = array(
'#type' => 'fieldset',
'#attached' => array('js' => array($child_js)),
'#title' => 'Child',
);
$element['child']['subchild'] = array(
'#attached' => array('js' => array($subchild_js)),
'#markup' => 'Subchild',
);
// Render the element and verify the presence of #attached JavaScript.
drupal_render($element);
$scripts = drupal_get_js();
Jennifer Hodgdon
committed
$this->assertTrue(strpos($scripts, $parent_js), 'The element #attached JavaScript was included.');
$this->assertTrue(strpos($scripts, $child_js), 'The child #attached JavaScript was included.');
$this->assertTrue(strpos($scripts, $subchild_js), 'The subchild #attached JavaScript was included.');
// Load the element from cache and verify the presence of the #attached
// JavaScript.
drupal_static_reset('drupal_add_js');
Jennifer Hodgdon
committed
$this->assertTrue(drupal_render_cache_get($element), 'The element was retrieved from cache.');
$scripts = drupal_get_js();
Jennifer Hodgdon
committed
$this->assertTrue(strpos($scripts, $parent_js), 'The element #attached JavaScript was included when loading from cache.');
$this->assertTrue(strpos($scripts, $child_js), 'The child #attached JavaScript was included when loading from cache.');
$this->assertTrue(strpos($scripts, $subchild_js), 'The subchild #attached JavaScript was included when loading from cache.');
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
$_SERVER['REQUEST_METHOD'] = $request_method;
}
/**
* Test passing arguments to the theme function.
*/
function testDrupalRenderThemeArguments() {
$element = array(
'#theme' => 'common_test_foo',
);
// Test that defaults work.
$this->assertEqual(drupal_render($element), 'foobar', 'Defaults work');
$element = array(
'#theme' => 'common_test_foo',
'#foo' => $this->randomName(),
'#bar' => $this->randomName(),
);
// Test that passing arguments to the theme function works.
$this->assertEqual(drupal_render($element), $element['#foo'] . $element['#bar'], 'Passing arguments to theme functions works');
}
/**
* Test rendering form elements without passing through form_builder().
*/
function testDrupalRenderFormElements() {
// Define a series of form elements.
$element = array(
'#type' => 'button',
'#value' => $this->randomName(),
);
$this->assertRenderedElement($element, '//input[@type=:type]', array(':type' => 'submit'));
$element = array(
'#type' => 'textfield',
'#title' => $this->randomName(),
'#value' => $this->randomName(),
);
$this->assertRenderedElement($element, '//input[@type=:type]', array(':type' => 'text'));
$element = array(
'#type' => 'password',
'#title' => $this->randomName(),
);
$this->assertRenderedElement($element, '//input[@type=:type]', array(':type' => 'password'));
$element = array(
'#type' => 'textarea',
'#title' => $this->randomName(),
'#value' => $this->randomName(),
);
$this->assertRenderedElement($element, '//textarea');
$element = array(
'#type' => 'radio',
'#title' => $this->randomName(),
'#value' => FALSE,
);
$this->assertRenderedElement($element, '//input[@type=:type]', array(':type' => 'radio'));
$element = array(
'#type' => 'checkbox',
'#title' => $this->randomName(),
);
$this->assertRenderedElement($element, '//input[@type=:type]', array(':type' => 'checkbox'));
$element = array(
'#type' => 'select',
'#title' => $this->randomName(),
'#options' => array(
0 => $this->randomName(),
1 => $this->randomName(),
),
);
$this->assertRenderedElement($element, '//select');
$element = array(
'#type' => 'file',
'#title' => $this->randomName(),
);
$this->assertRenderedElement($element, '//input[@type=:type]', array(':type' => 'file'));
$element = array(
'#type' => 'item',
'#title' => $this->randomName(),
'#markup' => $this->randomName(),
);
$this->assertRenderedElement($element, '//div[contains(@class, :class) and contains(., :markup)]/label[contains(., :label)]', array(
':class' => 'form-type-item',
':markup' => $element['#markup'],
':label' => $element['#title'],
));
$element = array(
'#type' => 'hidden',
'#title' => $this->randomName(),
'#value' => $this->randomName(),
);
$this->assertRenderedElement($element, '//input[@type=:type]', array(':type' => 'hidden'));
$element = array(
'#type' => 'link',
'#title' => $this->randomName(),
'#href' => $this->randomName(),
'#options' => array(
'absolute' => TRUE,
),
);
$this->assertRenderedElement($element, '//a[@href=:href and contains(., :title)]', array(
':href' => url($element['#href'], array('absolute' => TRUE)),
':title' => $element['#title'],
));
$element = array(
'#type' => 'fieldset',
'#title' => $this->randomName(),
);
$this->assertRenderedElement($element, '//fieldset/legend[contains(., :title)]', array(
':title' => $element['#title'],
));
$element = array(
'#type' => 'fieldset',
'#title' => $this->randomName(),
'#collapsible' => TRUE,
);
$this->assertRenderedElement($element, '//fieldset[contains(@class, :class)]', array(
':class' => 'collapsible',
));
$element['item'] = array(
'#type' => 'item',
'#title' => $this->randomName(),
'#markup' => $this->randomName(),
);
$this->assertRenderedElement($element, '//fieldset/div/div[contains(@class, :class) and contains(., :markup)]', array(
':class' => 'form-type-item',
':markup' => $element['item']['#markup'],
));
}
/**
* Test rendering elements with invalid keys.
*/
function testDrupalRenderInvalidKeys() {
$error = array(
'%type' => 'User error',
'!message' => '"child" is an invalid render array key',
'%function' => 'element_children()',
);
$message = t('%type: !message in %function (line ', $error);
Angie Byron
committed
config('system.logging')->set('error_level', ERROR_REPORTING_DISPLAY_ALL)->save();
$this->drupalGet('common-test/drupal-render-invalid-keys');
Jennifer Hodgdon
committed
$this->assertResponse(200, 'Received expected HTTP status code.');
$this->assertRaw($message, format_string('Found error message: !message.', array('!message' => $message)));
}
protected function assertRenderedElement(array $element, $xpath, array $xpath_args = array()) {
$original_element = $element;
$this->drupalSetContent(drupal_render($element));
$this->verbose('<pre>' . check_plain(var_export($original_element, TRUE)) . '</pre>'
. '<pre>' . check_plain(var_export($element, TRUE)) . '</pre>'
. '<hr />' . $this->drupalGetContent()
);
// @see Drupal\simpletest\WebTestBase::xpath()
$xpath = $this->buildXPathQuery($xpath, $xpath_args);
$element += array('#value' => NULL);
Jennifer Hodgdon
committed
$this->assertFieldByXPath($xpath, $element['#value'], format_string('#type @type was properly rendered.', array(
'@type' => var_export($element['#type'], TRUE),
)));
}
/**
* Tests caching of an empty render item.
*/
function testDrupalRenderCache() {
// Force a request via GET.
$request_method = $_SERVER['REQUEST_METHOD'];
$_SERVER['REQUEST_METHOD'] = 'GET';
// Create an empty element.
$test_element = array(
'#cache' => array(
'cid' => 'render_cache_test',
),
'#markup' => '',
);
// Render the element and confirm that it goes through the rendering
// process (which will set $element['#printed']).
$element = $test_element;
drupal_render($element);
Jennifer Hodgdon
committed
$this->assertTrue(isset($element['#printed']), 'No cache hit');
// Render the element again and confirm that it is retrieved from the cache
// instead (so $element['#printed'] will not be set).
$element = $test_element;
drupal_render($element);
Jennifer Hodgdon
committed
$this->assertFalse(isset($element['#printed']), 'Cache hit');