Chris@17
|
1 <?php
|
Chris@17
|
2
|
Chris@17
|
3 namespace Drupal\KernelTests;
|
Chris@17
|
4
|
Chris@17
|
5 use Drupal\Component\Serialization\Json;
|
Chris@17
|
6 use Drupal\Component\Utility\Html;
|
Chris@17
|
7 use Drupal\Component\Render\FormattableMarkup;
|
Chris@17
|
8 use Drupal\Component\Utility\Xss;
|
Chris@17
|
9 use Drupal\Core\Render\RenderContext;
|
Chris@17
|
10 use Symfony\Component\CssSelector\CssSelectorConverter;
|
Chris@17
|
11
|
Chris@17
|
12 /**
|
Chris@17
|
13 * Provides test methods to assert content.
|
Chris@17
|
14 */
|
Chris@17
|
15 trait AssertContentTrait {
|
Chris@17
|
16
|
Chris@17
|
17 /**
|
Chris@17
|
18 * The current raw content.
|
Chris@17
|
19 *
|
Chris@17
|
20 * @var string
|
Chris@17
|
21 */
|
Chris@17
|
22 protected $content;
|
Chris@17
|
23
|
Chris@17
|
24 /**
|
Chris@17
|
25 * The plain-text content of raw $content (text nodes).
|
Chris@17
|
26 *
|
Chris@17
|
27 * @var string
|
Chris@17
|
28 */
|
Chris@17
|
29 protected $plainTextContent;
|
Chris@17
|
30
|
Chris@17
|
31 /**
|
Chris@17
|
32 * The drupalSettings value from the current raw $content.
|
Chris@17
|
33 *
|
Chris@17
|
34 * Variable drupalSettings refers to the drupalSettings JavaScript variable.
|
Chris@17
|
35 *
|
Chris@17
|
36 * @var array
|
Chris@17
|
37 */
|
Chris@17
|
38 protected $drupalSettings;
|
Chris@17
|
39
|
Chris@17
|
40 /**
|
Chris@17
|
41 * The XML structure parsed from the current raw $content.
|
Chris@17
|
42 *
|
Chris@17
|
43 * @var \SimpleXMLElement
|
Chris@17
|
44 */
|
Chris@17
|
45 protected $elements;
|
Chris@17
|
46
|
Chris@17
|
47 /**
|
Chris@17
|
48 * Gets the current raw content.
|
Chris@17
|
49 */
|
Chris@17
|
50 protected function getRawContent() {
|
Chris@17
|
51 return $this->content;
|
Chris@17
|
52 }
|
Chris@17
|
53
|
Chris@17
|
54 /**
|
Chris@17
|
55 * Sets the raw content (e.g. HTML).
|
Chris@17
|
56 *
|
Chris@17
|
57 * @param string $content
|
Chris@17
|
58 * The raw content to set.
|
Chris@17
|
59 */
|
Chris@17
|
60 protected function setRawContent($content) {
|
Chris@17
|
61 $this->content = $content;
|
Chris@17
|
62 $this->plainTextContent = NULL;
|
Chris@17
|
63 $this->elements = NULL;
|
Chris@17
|
64 $this->drupalSettings = [];
|
Chris@17
|
65 if (preg_match('@<script type="application/json" data-drupal-selector="drupal-settings-json">([^<]*)</script>@', $content, $matches)) {
|
Chris@17
|
66 $this->drupalSettings = Json::decode($matches[1]);
|
Chris@17
|
67 }
|
Chris@17
|
68 }
|
Chris@17
|
69
|
Chris@17
|
70 /**
|
Chris@17
|
71 * Retrieves the plain-text content from the current raw content.
|
Chris@17
|
72 */
|
Chris@17
|
73 protected function getTextContent() {
|
Chris@17
|
74 if (!isset($this->plainTextContent)) {
|
Chris@17
|
75 $raw_content = $this->getRawContent();
|
Chris@17
|
76 // Strip everything between the HEAD tags.
|
Chris@17
|
77 $raw_content = preg_replace('@<head>(.+?)</head>@si', '', $raw_content);
|
Chris@17
|
78 $this->plainTextContent = Xss::filter($raw_content, []);
|
Chris@17
|
79 }
|
Chris@17
|
80 return $this->plainTextContent;
|
Chris@17
|
81 }
|
Chris@17
|
82
|
Chris@17
|
83 /**
|
Chris@17
|
84 * Removes all white-space between HTML tags from the raw content.
|
Chris@17
|
85 *
|
Chris@17
|
86 * White-space is only removed if there are no non-white-space characters
|
Chris@17
|
87 * between HTML tags.
|
Chris@17
|
88 *
|
Chris@17
|
89 * Use this (once) after performing an operation that sets new raw content,
|
Chris@17
|
90 * and when you want to use e.g. assertText() but ignore potential white-space
|
Chris@17
|
91 * caused by HTML output templates.
|
Chris@17
|
92 */
|
Chris@17
|
93 protected function removeWhiteSpace() {
|
Chris@17
|
94 $this->content = preg_replace('@>\s+<@', '><', $this->content);
|
Chris@17
|
95 $this->plainTextContent = NULL;
|
Chris@17
|
96 $this->elements = NULL;
|
Chris@17
|
97 }
|
Chris@17
|
98
|
Chris@17
|
99 /**
|
Chris@17
|
100 * Gets the value of drupalSettings for the currently-loaded page.
|
Chris@17
|
101 *
|
Chris@17
|
102 * Variable drupalSettings refers to the drupalSettings JavaScript variable.
|
Chris@17
|
103 */
|
Chris@17
|
104 protected function getDrupalSettings() {
|
Chris@17
|
105 return $this->drupalSettings;
|
Chris@17
|
106 }
|
Chris@17
|
107
|
Chris@17
|
108 /**
|
Chris@17
|
109 * Sets the value of drupalSettings for the currently-loaded page.
|
Chris@17
|
110 *
|
Chris@17
|
111 * Variable drupalSettings refers to the drupalSettings JavaScript variable.
|
Chris@17
|
112 */
|
Chris@17
|
113 protected function setDrupalSettings($settings) {
|
Chris@17
|
114 $this->drupalSettings = $settings;
|
Chris@17
|
115 }
|
Chris@17
|
116
|
Chris@17
|
117 /**
|
Chris@17
|
118 * Parse content returned from curlExec using DOM and SimpleXML.
|
Chris@17
|
119 *
|
Chris@17
|
120 * @return \SimpleXMLElement|false
|
Chris@17
|
121 * A SimpleXMLElement or FALSE on failure.
|
Chris@17
|
122 */
|
Chris@17
|
123 protected function parse() {
|
Chris@17
|
124 if (!isset($this->elements)) {
|
Chris@17
|
125 // DOM can load HTML soup. But, HTML soup can throw warnings, suppress
|
Chris@17
|
126 // them.
|
Chris@17
|
127 $html_dom = new \DOMDocument();
|
Chris@17
|
128 @$html_dom->loadHTML('<?xml encoding="UTF-8">' . $this->getRawContent());
|
Chris@17
|
129 if ($html_dom) {
|
Chris@17
|
130 $this->pass(new FormattableMarkup('Valid HTML found on "@path"', ['@path' => $this->getUrl()]), 'Browser');
|
Chris@17
|
131 // It's much easier to work with simplexml than DOM, luckily enough
|
Chris@17
|
132 // we can just simply import our DOM tree.
|
Chris@17
|
133 $this->elements = simplexml_import_dom($html_dom);
|
Chris@17
|
134 }
|
Chris@17
|
135 }
|
Chris@17
|
136 if ($this->elements === FALSE) {
|
Chris@17
|
137 $this->fail('Parsed page successfully.', 'Browser');
|
Chris@17
|
138 }
|
Chris@17
|
139
|
Chris@17
|
140 return $this->elements;
|
Chris@17
|
141 }
|
Chris@17
|
142
|
Chris@17
|
143 /**
|
Chris@17
|
144 * Get the current URL from the cURL handler.
|
Chris@17
|
145 *
|
Chris@17
|
146 * @return string
|
Chris@17
|
147 * The current URL.
|
Chris@17
|
148 */
|
Chris@17
|
149 protected function getUrl() {
|
Chris@17
|
150 return isset($this->url) ? $this->url : 'no-url';
|
Chris@17
|
151 }
|
Chris@17
|
152
|
Chris@17
|
153 /**
|
Chris@17
|
154 * Builds an XPath query.
|
Chris@17
|
155 *
|
Chris@17
|
156 * Builds an XPath query by replacing placeholders in the query by the value
|
Chris@17
|
157 * of the arguments.
|
Chris@17
|
158 *
|
Chris@17
|
159 * XPath 1.0 (the version supported by libxml2, the underlying XML library
|
Chris@17
|
160 * used by PHP) doesn't support any form of quotation. This function
|
Chris@17
|
161 * simplifies the building of XPath expression.
|
Chris@17
|
162 *
|
Chris@17
|
163 * @param string $xpath
|
Chris@17
|
164 * An XPath query, possibly with placeholders in the form ':name'.
|
Chris@17
|
165 * @param array $args
|
Chris@17
|
166 * An array of arguments with keys in the form ':name' matching the
|
Chris@17
|
167 * placeholders in the query. The values may be either strings or numeric
|
Chris@17
|
168 * values.
|
Chris@17
|
169 *
|
Chris@17
|
170 * @return string
|
Chris@17
|
171 * An XPath query with arguments replaced.
|
Chris@17
|
172 */
|
Chris@17
|
173 protected function buildXPathQuery($xpath, array $args = []) {
|
Chris@17
|
174 // Replace placeholders.
|
Chris@17
|
175 foreach ($args as $placeholder => $value) {
|
Chris@17
|
176 // Cast MarkupInterface objects to string.
|
Chris@17
|
177 if (is_object($value)) {
|
Chris@17
|
178 $value = (string) $value;
|
Chris@17
|
179 }
|
Chris@17
|
180 // XPath 1.0 doesn't support a way to escape single or double quotes in a
|
Chris@17
|
181 // string literal. We split double quotes out of the string, and encode
|
Chris@17
|
182 // them separately.
|
Chris@17
|
183 if (is_string($value)) {
|
Chris@17
|
184 // Explode the text at the quote characters.
|
Chris@17
|
185 $parts = explode('"', $value);
|
Chris@17
|
186
|
Chris@17
|
187 // Quote the parts.
|
Chris@17
|
188 foreach ($parts as &$part) {
|
Chris@17
|
189 $part = '"' . $part . '"';
|
Chris@17
|
190 }
|
Chris@17
|
191
|
Chris@17
|
192 // Return the string.
|
Chris@17
|
193 $value = count($parts) > 1 ? 'concat(' . implode(', \'"\', ', $parts) . ')' : $parts[0];
|
Chris@17
|
194 }
|
Chris@17
|
195
|
Chris@17
|
196 // Use preg_replace_callback() instead of preg_replace() to prevent the
|
Chris@17
|
197 // regular expression engine from trying to substitute backreferences.
|
Chris@17
|
198 $replacement = function ($matches) use ($value) {
|
Chris@17
|
199 return $value;
|
Chris@17
|
200 };
|
Chris@17
|
201 $xpath = preg_replace_callback('/' . preg_quote($placeholder) . '\b/', $replacement, $xpath);
|
Chris@17
|
202 }
|
Chris@17
|
203 return $xpath;
|
Chris@17
|
204 }
|
Chris@17
|
205
|
Chris@17
|
206 /**
|
Chris@17
|
207 * Performs an xpath search on the contents of the internal browser.
|
Chris@17
|
208 *
|
Chris@17
|
209 * The search is relative to the root element (HTML tag normally) of the page.
|
Chris@17
|
210 *
|
Chris@17
|
211 * @param string $xpath
|
Chris@17
|
212 * The xpath string to use in the search.
|
Chris@17
|
213 * @param array $arguments
|
Chris@17
|
214 * An array of arguments with keys in the form ':name' matching the
|
Chris@17
|
215 * placeholders in the query. The values may be either strings or numeric
|
Chris@17
|
216 * values.
|
Chris@17
|
217 *
|
Chris@17
|
218 * @return \SimpleXMLElement[]|bool
|
Chris@17
|
219 * The return value of the xpath search or FALSE on failure. For details on
|
Chris@17
|
220 * the xpath string format and return values see the SimpleXML
|
Chris@17
|
221 * documentation.
|
Chris@17
|
222 *
|
Chris@17
|
223 * @see http://php.net/manual/function.simplexml-element-xpath.php
|
Chris@17
|
224 */
|
Chris@17
|
225 protected function xpath($xpath, array $arguments = []) {
|
Chris@17
|
226 if ($this->parse()) {
|
Chris@17
|
227 $xpath = $this->buildXPathQuery($xpath, $arguments);
|
Chris@17
|
228 $result = $this->elements->xpath($xpath);
|
Chris@17
|
229 // Some combinations of PHP / libxml versions return an empty array
|
Chris@17
|
230 // instead of the documented FALSE. Forcefully convert any falsish values
|
Chris@17
|
231 // to an empty array to allow foreach(...) constructions.
|
Chris@17
|
232 return $result ?: [];
|
Chris@17
|
233 }
|
Chris@17
|
234 return FALSE;
|
Chris@17
|
235 }
|
Chris@17
|
236
|
Chris@17
|
237 /**
|
Chris@17
|
238 * Searches elements using a CSS selector in the raw content.
|
Chris@17
|
239 *
|
Chris@17
|
240 * The search is relative to the root element (HTML tag normally) of the page.
|
Chris@17
|
241 *
|
Chris@17
|
242 * @param string $selector
|
Chris@17
|
243 * CSS selector to use in the search.
|
Chris@17
|
244 *
|
Chris@17
|
245 * @return \SimpleXMLElement[]
|
Chris@17
|
246 * The return value of the XPath search performed after converting the CSS
|
Chris@17
|
247 * selector to an XPath selector.
|
Chris@17
|
248 */
|
Chris@17
|
249 protected function cssSelect($selector) {
|
Chris@17
|
250 return $this->xpath((new CssSelectorConverter())->toXPath($selector));
|
Chris@17
|
251 }
|
Chris@17
|
252
|
Chris@17
|
253 /**
|
Chris@17
|
254 * Get all option elements, including nested options, in a select.
|
Chris@17
|
255 *
|
Chris@17
|
256 * @param \SimpleXMLElement $element
|
Chris@17
|
257 * The element for which to get the options.
|
Chris@17
|
258 *
|
Chris@17
|
259 * @return \SimpleXmlElement[]
|
Chris@17
|
260 * Option elements in select.
|
Chris@17
|
261 */
|
Chris@17
|
262 protected function getAllOptions(\SimpleXMLElement $element) {
|
Chris@17
|
263 $options = [];
|
Chris@17
|
264 // Add all options items.
|
Chris@17
|
265 foreach ($element->option as $option) {
|
Chris@17
|
266 $options[] = $option;
|
Chris@17
|
267 }
|
Chris@17
|
268
|
Chris@17
|
269 // Search option group children.
|
Chris@17
|
270 if (isset($element->optgroup)) {
|
Chris@17
|
271 foreach ($element->optgroup as $group) {
|
Chris@17
|
272 $options = array_merge($options, $this->getAllOptions($group));
|
Chris@17
|
273 }
|
Chris@17
|
274 }
|
Chris@17
|
275 return $options;
|
Chris@17
|
276 }
|
Chris@17
|
277
|
Chris@17
|
278 /**
|
Chris@17
|
279 * Passes if a link with the specified label is found.
|
Chris@17
|
280 *
|
Chris@17
|
281 * An optional link index may be passed.
|
Chris@17
|
282 *
|
Chris@17
|
283 * @param string|\Drupal\Component\Render\MarkupInterface $label
|
Chris@17
|
284 * Text between the anchor tags.
|
Chris@17
|
285 * @param int $index
|
Chris@17
|
286 * Link position counting from zero.
|
Chris@17
|
287 * @param string $message
|
Chris@17
|
288 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
289 * messages: use strtr() to embed variables in the message text, not
|
Chris@17
|
290 * t(). If left blank, a default message will be displayed.
|
Chris@17
|
291 * @param string $group
|
Chris@17
|
292 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
293 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
294 * translate this string. Defaults to 'Other'; most tests do not override
|
Chris@17
|
295 * this default.
|
Chris@17
|
296 *
|
Chris@17
|
297 * @return bool
|
Chris@17
|
298 * TRUE if the assertion succeeded, FALSE otherwise.
|
Chris@17
|
299 */
|
Chris@17
|
300 protected function assertLink($label, $index = 0, $message = '', $group = 'Other') {
|
Chris@17
|
301 // Cast MarkupInterface objects to string.
|
Chris@17
|
302 $label = (string) $label;
|
Chris@17
|
303 $links = $this->xpath('//a[normalize-space(text())=:label]', [':label' => $label]);
|
Chris@17
|
304 $message = ($message ? $message : strtr('Link with label %label found.', ['%label' => $label]));
|
Chris@17
|
305 return $this->assert(isset($links[$index]), $message, $group);
|
Chris@17
|
306 }
|
Chris@17
|
307
|
Chris@17
|
308 /**
|
Chris@17
|
309 * Passes if a link with the specified label is not found.
|
Chris@17
|
310 *
|
Chris@17
|
311 * @param string|\Drupal\Component\Render\MarkupInterface $label
|
Chris@17
|
312 * Text between the anchor tags.
|
Chris@17
|
313 * @param string $message
|
Chris@17
|
314 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
315 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
316 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
317 * will be displayed.
|
Chris@17
|
318 * @param string $group
|
Chris@17
|
319 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
320 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
321 * translate this string. Defaults to 'Other'; most tests do not override
|
Chris@17
|
322 * this default.
|
Chris@17
|
323 *
|
Chris@17
|
324 * @return bool
|
Chris@17
|
325 * TRUE if the assertion succeeded, FALSE otherwise.
|
Chris@17
|
326 */
|
Chris@17
|
327 protected function assertNoLink($label, $message = '', $group = 'Other') {
|
Chris@17
|
328 // Cast MarkupInterface objects to string.
|
Chris@17
|
329 $label = (string) $label;
|
Chris@17
|
330 $links = $this->xpath('//a[normalize-space(text())=:label]', [':label' => $label]);
|
Chris@17
|
331 $message = ($message ? $message : new FormattableMarkup('Link with label %label not found.', ['%label' => $label]));
|
Chris@17
|
332 return $this->assert(empty($links), $message, $group);
|
Chris@17
|
333 }
|
Chris@17
|
334
|
Chris@17
|
335 /**
|
Chris@17
|
336 * Passes if a link containing a given href (part) is found.
|
Chris@17
|
337 *
|
Chris@17
|
338 * @param string $href
|
Chris@17
|
339 * The full or partial value of the 'href' attribute of the anchor tag.
|
Chris@17
|
340 * @param string $index
|
Chris@17
|
341 * Link position counting from zero.
|
Chris@17
|
342 * @param string $message
|
Chris@17
|
343 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
344 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
345 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
346 * will be displayed.
|
Chris@17
|
347 * @param string $group
|
Chris@17
|
348 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
349 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
350 * translate this string. Defaults to 'Other'; most tests do not override
|
Chris@17
|
351 * this default.
|
Chris@17
|
352 *
|
Chris@17
|
353 * @return bool
|
Chris@17
|
354 * TRUE if the assertion succeeded, FALSE otherwise.
|
Chris@17
|
355 */
|
Chris@17
|
356 protected function assertLinkByHref($href, $index = 0, $message = '', $group = 'Other') {
|
Chris@17
|
357 $links = $this->xpath('//a[contains(@href, :href)]', [':href' => $href]);
|
Chris@17
|
358 $message = ($message ? $message : new FormattableMarkup('Link containing href %href found.', ['%href' => $href]));
|
Chris@17
|
359 return $this->assert(isset($links[$index]), $message, $group);
|
Chris@17
|
360 }
|
Chris@17
|
361
|
Chris@17
|
362 /**
|
Chris@17
|
363 * Passes if a link containing a given href (part) is not found.
|
Chris@17
|
364 *
|
Chris@17
|
365 * @param string $href
|
Chris@17
|
366 * The full or partial value of the 'href' attribute of the anchor tag.
|
Chris@17
|
367 * @param string $message
|
Chris@17
|
368 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
369 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
370 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
371 * will be displayed.
|
Chris@17
|
372 * @param string $group
|
Chris@17
|
373 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
374 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
375 * translate this string. Defaults to 'Other'; most tests do not override
|
Chris@17
|
376 * this default.
|
Chris@17
|
377 *
|
Chris@17
|
378 * @return bool
|
Chris@17
|
379 * TRUE if the assertion succeeded, FALSE otherwise.
|
Chris@17
|
380 */
|
Chris@17
|
381 protected function assertNoLinkByHref($href, $message = '', $group = 'Other') {
|
Chris@17
|
382 $links = $this->xpath('//a[contains(@href, :href)]', [':href' => $href]);
|
Chris@17
|
383 $message = ($message ? $message : new FormattableMarkup('No link containing href %href found.', ['%href' => $href]));
|
Chris@17
|
384 return $this->assert(empty($links), $message, $group);
|
Chris@17
|
385 }
|
Chris@17
|
386
|
Chris@17
|
387 /**
|
Chris@17
|
388 * Passes if a link containing a given href is not found in the main region.
|
Chris@17
|
389 *
|
Chris@17
|
390 * @param string $href
|
Chris@17
|
391 * The full or partial value of the 'href' attribute of the anchor tag.
|
Chris@17
|
392 * @param string $message
|
Chris@17
|
393 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
394 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
395 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
396 * will be displayed.
|
Chris@17
|
397 * @param string $group
|
Chris@17
|
398 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
399 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
400 * translate this string. Defaults to 'Other'; most tests do not override
|
Chris@17
|
401 * this default.
|
Chris@17
|
402 *
|
Chris@17
|
403 * @return bool
|
Chris@17
|
404 * TRUE if the assertion succeeded, FALSE otherwise.
|
Chris@17
|
405 */
|
Chris@17
|
406 protected function assertNoLinkByHrefInMainRegion($href, $message = '', $group = 'Other') {
|
Chris@17
|
407 $links = $this->xpath('//main//a[contains(@href, :href)]', [':href' => $href]);
|
Chris@17
|
408 $message = ($message ? $message : new FormattableMarkup('No link containing href %href found.', ['%href' => $href]));
|
Chris@17
|
409 return $this->assert(empty($links), $message, $group);
|
Chris@17
|
410 }
|
Chris@17
|
411
|
Chris@17
|
412 /**
|
Chris@17
|
413 * Passes if the raw text IS found on the loaded page, fail otherwise.
|
Chris@17
|
414 *
|
Chris@17
|
415 * Raw text refers to the raw HTML that the page generated.
|
Chris@17
|
416 *
|
Chris@17
|
417 * @param string $raw
|
Chris@17
|
418 * Raw (HTML) string to look for.
|
Chris@17
|
419 * @param string $message
|
Chris@17
|
420 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
421 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
422 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
423 * will be displayed.
|
Chris@17
|
424 * @param string $group
|
Chris@17
|
425 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
426 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
427 * translate this string. Defaults to 'Other'; most tests do not override
|
Chris@17
|
428 * this default.
|
Chris@17
|
429 *
|
Chris@17
|
430 * @return bool
|
Chris@17
|
431 * TRUE on pass, FALSE on fail.
|
Chris@17
|
432 */
|
Chris@17
|
433 protected function assertRaw($raw, $message = '', $group = 'Other') {
|
Chris@17
|
434 if (!$message) {
|
Chris@17
|
435 $message = 'Raw "' . Html::escape($raw) . '" found';
|
Chris@17
|
436 }
|
Chris@17
|
437 return $this->assert(strpos($this->getRawContent(), (string) $raw) !== FALSE, $message, $group);
|
Chris@17
|
438 }
|
Chris@17
|
439
|
Chris@17
|
440 /**
|
Chris@17
|
441 * Passes if the raw text is NOT found on the loaded page, fail otherwise.
|
Chris@17
|
442 *
|
Chris@17
|
443 * Raw text refers to the raw HTML that the page generated.
|
Chris@17
|
444 *
|
Chris@17
|
445 * @param string $raw
|
Chris@17
|
446 * Raw (HTML) string to look for.
|
Chris@17
|
447 * @param string $message
|
Chris@17
|
448 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
449 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
450 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
451 * will be displayed.
|
Chris@17
|
452 * @param string $group
|
Chris@17
|
453 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
454 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
455 * translate this string. Defaults to 'Other'; most tests do not override
|
Chris@17
|
456 * this default.
|
Chris@17
|
457 *
|
Chris@17
|
458 * @return bool
|
Chris@17
|
459 * TRUE on pass, FALSE on fail.
|
Chris@17
|
460 */
|
Chris@17
|
461 protected function assertNoRaw($raw, $message = '', $group = 'Other') {
|
Chris@17
|
462 if (!$message) {
|
Chris@17
|
463 $message = 'Raw "' . Html::escape($raw) . '" not found';
|
Chris@17
|
464 }
|
Chris@17
|
465 return $this->assert(strpos($this->getRawContent(), (string) $raw) === FALSE, $message, $group);
|
Chris@17
|
466 }
|
Chris@17
|
467
|
Chris@17
|
468 /**
|
Chris@17
|
469 * Passes if the raw text IS found escaped on the loaded page, fail otherwise.
|
Chris@17
|
470 *
|
Chris@17
|
471 * Raw text refers to the raw HTML that the page generated.
|
Chris@17
|
472 *
|
Chris@17
|
473 * @param string $raw
|
Chris@17
|
474 * Raw (HTML) string to look for.
|
Chris@17
|
475 * @param string $message
|
Chris@17
|
476 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
477 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
478 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
479 * will be displayed.
|
Chris@17
|
480 * @param string $group
|
Chris@17
|
481 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
482 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
483 * translate this string. Defaults to 'Other'; most tests do not override
|
Chris@17
|
484 * this default.
|
Chris@17
|
485 *
|
Chris@17
|
486 * @return bool
|
Chris@17
|
487 * TRUE on pass, FALSE on fail.
|
Chris@17
|
488 */
|
Chris@17
|
489 protected function assertEscaped($raw, $message = '', $group = 'Other') {
|
Chris@17
|
490 if (!$message) {
|
Chris@17
|
491 $message = 'Escaped "' . Html::escape($raw) . '" found';
|
Chris@17
|
492 }
|
Chris@17
|
493 return $this->assert(strpos($this->getRawContent(), Html::escape($raw)) !== FALSE, $message, $group);
|
Chris@17
|
494 }
|
Chris@17
|
495
|
Chris@17
|
496 /**
|
Chris@17
|
497 * Passes if the raw text IS NOT found escaped on the loaded page, fail
|
Chris@17
|
498 * otherwise.
|
Chris@17
|
499 *
|
Chris@17
|
500 * Raw text refers to the raw HTML that the page generated.
|
Chris@17
|
501 *
|
Chris@17
|
502 * @param string $raw
|
Chris@17
|
503 * Raw (HTML) string to look for.
|
Chris@17
|
504 * @param string $message
|
Chris@17
|
505 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
506 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
507 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
508 * will be displayed.
|
Chris@17
|
509 * @param string $group
|
Chris@17
|
510 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
511 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
512 * translate this string. Defaults to 'Other'; most tests do not override
|
Chris@17
|
513 * this default.
|
Chris@17
|
514 *
|
Chris@17
|
515 * @return bool
|
Chris@17
|
516 * TRUE on pass, FALSE on fail.
|
Chris@17
|
517 */
|
Chris@17
|
518 protected function assertNoEscaped($raw, $message = '', $group = 'Other') {
|
Chris@17
|
519 if (!$message) {
|
Chris@17
|
520 $message = 'Escaped "' . Html::escape($raw) . '" not found';
|
Chris@17
|
521 }
|
Chris@17
|
522 return $this->assert(strpos($this->getRawContent(), Html::escape($raw)) === FALSE, $message, $group);
|
Chris@17
|
523 }
|
Chris@17
|
524
|
Chris@17
|
525 /**
|
Chris@17
|
526 * Passes if the page (with HTML stripped) contains the text.
|
Chris@17
|
527 *
|
Chris@17
|
528 * Note that stripping HTML tags also removes their attributes, such as
|
Chris@17
|
529 * the values of text fields.
|
Chris@17
|
530 *
|
Chris@17
|
531 * @param string $text
|
Chris@17
|
532 * Plain text to look for.
|
Chris@17
|
533 * @param string $message
|
Chris@17
|
534 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
535 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
536 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
537 * will be displayed.
|
Chris@17
|
538 * @param string $group
|
Chris@17
|
539 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
540 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
541 * translate this string. Defaults to 'Other'; most tests do not override
|
Chris@17
|
542 * this default.
|
Chris@17
|
543 *
|
Chris@17
|
544 * @return bool
|
Chris@17
|
545 * TRUE on pass, FALSE on fail.
|
Chris@17
|
546 *
|
Chris@17
|
547 * @see \Drupal\simpletest\AssertContentTrait::assertRaw()
|
Chris@17
|
548 */
|
Chris@17
|
549 protected function assertText($text, $message = '', $group = 'Other') {
|
Chris@17
|
550 return $this->assertTextHelper($text, $message, $group, FALSE);
|
Chris@17
|
551 }
|
Chris@17
|
552
|
Chris@17
|
553 /**
|
Chris@17
|
554 * Passes if the page (with HTML stripped) does not contains the text.
|
Chris@17
|
555 *
|
Chris@17
|
556 * Note that stripping HTML tags also removes their attributes, such as
|
Chris@17
|
557 * the values of text fields.
|
Chris@17
|
558 *
|
Chris@17
|
559 * @param string $text
|
Chris@17
|
560 * Plain text to look for.
|
Chris@17
|
561 * @param string $message
|
Chris@17
|
562 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
563 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
564 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
565 * will be displayed.
|
Chris@17
|
566 * @param string $group
|
Chris@17
|
567 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
568 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
569 * translate this string. Defaults to 'Other'; most tests do not override
|
Chris@17
|
570 * this default.
|
Chris@17
|
571 *
|
Chris@17
|
572 * @return bool
|
Chris@17
|
573 * TRUE on pass, FALSE on fail.
|
Chris@17
|
574 *
|
Chris@17
|
575 * @see \Drupal\simpletest\AssertContentTrait::assertNoRaw()
|
Chris@17
|
576 */
|
Chris@17
|
577 protected function assertNoText($text, $message = '', $group = 'Other') {
|
Chris@17
|
578 return $this->assertTextHelper($text, $message, $group, TRUE);
|
Chris@17
|
579 }
|
Chris@17
|
580
|
Chris@17
|
581 /**
|
Chris@17
|
582 * Helper for assertText and assertNoText.
|
Chris@17
|
583 *
|
Chris@17
|
584 * It is not recommended to call this function directly.
|
Chris@17
|
585 *
|
Chris@17
|
586 * @param string $text
|
Chris@17
|
587 * Plain text to look for.
|
Chris@17
|
588 * @param string $message
|
Chris@17
|
589 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
590 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
591 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
592 * will be displayed.
|
Chris@17
|
593 * @param string $group
|
Chris@17
|
594 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
595 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
596 * translate this string. Defaults to 'Other'; most tests do not override
|
Chris@17
|
597 * this default. Defaults to 'Other'.
|
Chris@17
|
598 * @param bool $not_exists
|
Chris@17
|
599 * (optional) TRUE if this text should not exist, FALSE if it should.
|
Chris@17
|
600 * Defaults to TRUE.
|
Chris@17
|
601 *
|
Chris@17
|
602 * @return bool
|
Chris@17
|
603 * TRUE on pass, FALSE on fail.
|
Chris@17
|
604 */
|
Chris@17
|
605 protected function assertTextHelper($text, $message = '', $group = 'Other', $not_exists = TRUE) {
|
Chris@17
|
606 if (!$message) {
|
Chris@17
|
607 $message = !$not_exists ? new FormattableMarkup('"@text" found', ['@text' => $text]) : new FormattableMarkup('"@text" not found', ['@text' => $text]);
|
Chris@17
|
608 }
|
Chris@17
|
609 return $this->assert($not_exists == (strpos($this->getTextContent(), (string) $text) === FALSE), $message, $group);
|
Chris@17
|
610 }
|
Chris@17
|
611
|
Chris@17
|
612 /**
|
Chris@17
|
613 * Passes if the text is found ONLY ONCE on the text version of the page.
|
Chris@17
|
614 *
|
Chris@17
|
615 * The text version is the equivalent of what a user would see when viewing
|
Chris@17
|
616 * through a web browser. In other words the HTML has been filtered out of
|
Chris@17
|
617 * the contents.
|
Chris@17
|
618 *
|
Chris@17
|
619 * @param string|\Drupal\Component\Render\MarkupInterface $text
|
Chris@17
|
620 * Plain text to look for.
|
Chris@17
|
621 * @param string $message
|
Chris@17
|
622 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
623 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
624 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
625 * will be displayed.
|
Chris@17
|
626 * @param string $group
|
Chris@17
|
627 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
628 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
629 * translate this string. Defaults to 'Other'; most tests do not override
|
Chris@17
|
630 * this default.
|
Chris@17
|
631 *
|
Chris@17
|
632 * @return bool
|
Chris@17
|
633 * TRUE on pass, FALSE on fail.
|
Chris@17
|
634 */
|
Chris@17
|
635 protected function assertUniqueText($text, $message = '', $group = 'Other') {
|
Chris@17
|
636 return $this->assertUniqueTextHelper($text, $message, $group, TRUE);
|
Chris@17
|
637 }
|
Chris@17
|
638
|
Chris@17
|
639 /**
|
Chris@17
|
640 * Passes if the text is found MORE THAN ONCE on the text version of the page.
|
Chris@17
|
641 *
|
Chris@17
|
642 * The text version is the equivalent of what a user would see when viewing
|
Chris@17
|
643 * through a web browser. In other words the HTML has been filtered out of
|
Chris@17
|
644 * the contents.
|
Chris@17
|
645 *
|
Chris@17
|
646 * @param string|\Drupal\Component\Render\MarkupInterface $text
|
Chris@17
|
647 * Plain text to look for.
|
Chris@17
|
648 * @param string $message
|
Chris@17
|
649 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
650 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
651 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
652 * will be displayed.
|
Chris@17
|
653 * @param string $group
|
Chris@17
|
654 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
655 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
656 * translate this string. Defaults to 'Other'; most tests do not override
|
Chris@17
|
657 * this default.
|
Chris@17
|
658 *
|
Chris@17
|
659 * @return bool
|
Chris@17
|
660 * TRUE on pass, FALSE on fail.
|
Chris@17
|
661 */
|
Chris@17
|
662 protected function assertNoUniqueText($text, $message = '', $group = 'Other') {
|
Chris@17
|
663 return $this->assertUniqueTextHelper($text, $message, $group, FALSE);
|
Chris@17
|
664 }
|
Chris@17
|
665
|
Chris@17
|
666 /**
|
Chris@17
|
667 * Helper for assertUniqueText and assertNoUniqueText.
|
Chris@17
|
668 *
|
Chris@17
|
669 * It is not recommended to call this function directly.
|
Chris@17
|
670 *
|
Chris@17
|
671 * @param string|\Drupal\Component\Render\MarkupInterface $text
|
Chris@17
|
672 * Plain text to look for.
|
Chris@17
|
673 * @param string $message
|
Chris@17
|
674 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
675 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
676 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
677 * will be displayed.
|
Chris@17
|
678 * @param string $group
|
Chris@17
|
679 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
680 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
681 * translate this string. Defaults to 'Other'; most tests do not override
|
Chris@17
|
682 * this default. Defaults to 'Other'.
|
Chris@17
|
683 * @param bool $be_unique
|
Chris@17
|
684 * (optional) TRUE if this text should be found only once, FALSE if it
|
Chris@17
|
685 * should be found more than once. Defaults to FALSE.
|
Chris@17
|
686 *
|
Chris@17
|
687 * @return bool
|
Chris@17
|
688 * TRUE on pass, FALSE on fail.
|
Chris@17
|
689 */
|
Chris@17
|
690 protected function assertUniqueTextHelper($text, $message = '', $group = 'Other', $be_unique = FALSE) {
|
Chris@17
|
691 // Cast MarkupInterface objects to string.
|
Chris@17
|
692 $text = (string) $text;
|
Chris@17
|
693 if (!$message) {
|
Chris@17
|
694 $message = '"' . $text . '"' . ($be_unique ? ' found only once' : ' found more than once');
|
Chris@17
|
695 }
|
Chris@17
|
696 $first_occurrence = strpos($this->getTextContent(), $text);
|
Chris@17
|
697 if ($first_occurrence === FALSE) {
|
Chris@17
|
698 return $this->assert(FALSE, $message, $group);
|
Chris@17
|
699 }
|
Chris@17
|
700 $offset = $first_occurrence + strlen($text);
|
Chris@17
|
701 $second_occurrence = strpos($this->getTextContent(), $text, $offset);
|
Chris@17
|
702 return $this->assert($be_unique == ($second_occurrence === FALSE), $message, $group);
|
Chris@17
|
703 }
|
Chris@17
|
704
|
Chris@17
|
705 /**
|
Chris@17
|
706 * Triggers a pass if the Perl regex pattern is found in the raw content.
|
Chris@17
|
707 *
|
Chris@17
|
708 * @param string $pattern
|
Chris@17
|
709 * Perl regex to look for including the regex delimiters.
|
Chris@17
|
710 * @param string $message
|
Chris@17
|
711 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
712 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
713 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
714 * will be displayed.
|
Chris@17
|
715 * @param string $group
|
Chris@17
|
716 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
717 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
718 * translate this string. Defaults to 'Other'; most tests do not override
|
Chris@17
|
719 * this default.
|
Chris@17
|
720 *
|
Chris@17
|
721 * @return bool
|
Chris@17
|
722 * TRUE on pass, FALSE on fail.
|
Chris@17
|
723 */
|
Chris@17
|
724 protected function assertPattern($pattern, $message = '', $group = 'Other') {
|
Chris@17
|
725 if (!$message) {
|
Chris@17
|
726 $message = new FormattableMarkup('Pattern "@pattern" found', ['@pattern' => $pattern]);
|
Chris@17
|
727 }
|
Chris@17
|
728 return $this->assert((bool) preg_match($pattern, $this->getRawContent()), $message, $group);
|
Chris@17
|
729 }
|
Chris@17
|
730
|
Chris@17
|
731 /**
|
Chris@17
|
732 * Triggers a pass if the perl regex pattern is not found in raw content.
|
Chris@17
|
733 *
|
Chris@17
|
734 * @param string $pattern
|
Chris@17
|
735 * Perl regex to look for including the regex delimiters.
|
Chris@17
|
736 * @param string $message
|
Chris@17
|
737 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
738 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
739 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
740 * will be displayed.
|
Chris@17
|
741 * @param string $group
|
Chris@17
|
742 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
743 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
744 * translate this string. Defaults to 'Other'; most tests do not override
|
Chris@17
|
745 * this default.
|
Chris@17
|
746 *
|
Chris@17
|
747 * @return bool
|
Chris@17
|
748 * TRUE on pass, FALSE on fail.
|
Chris@17
|
749 */
|
Chris@17
|
750 protected function assertNoPattern($pattern, $message = '', $group = 'Other') {
|
Chris@17
|
751 if (!$message) {
|
Chris@17
|
752 $message = new FormattableMarkup('Pattern "@pattern" not found', ['@pattern' => $pattern]);
|
Chris@17
|
753 }
|
Chris@17
|
754 return $this->assert(!preg_match($pattern, $this->getRawContent()), $message, $group);
|
Chris@17
|
755 }
|
Chris@17
|
756
|
Chris@17
|
757 /**
|
Chris@17
|
758 * Asserts that a Perl regex pattern is found in the plain-text content.
|
Chris@17
|
759 *
|
Chris@17
|
760 * @param string $pattern
|
Chris@17
|
761 * Perl regex to look for including the regex delimiters.
|
Chris@17
|
762 * @param string $message
|
Chris@17
|
763 * (optional) A message to display with the assertion.
|
Chris@17
|
764 * @param string $group
|
Chris@17
|
765 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
766 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
767 * translate this string. Defaults to 'Other'; most tests do not override
|
Chris@17
|
768 * this default.
|
Chris@17
|
769 *
|
Chris@17
|
770 * @return bool
|
Chris@17
|
771 * TRUE on pass, FALSE on failure.
|
Chris@17
|
772 */
|
Chris@17
|
773 protected function assertTextPattern($pattern, $message = NULL, $group = 'Other') {
|
Chris@17
|
774 if (!isset($message)) {
|
Chris@17
|
775 $message = new FormattableMarkup('Pattern "@pattern" found', ['@pattern' => $pattern]);
|
Chris@17
|
776 }
|
Chris@17
|
777 return $this->assert((bool) preg_match($pattern, $this->getTextContent()), $message, $group);
|
Chris@17
|
778 }
|
Chris@17
|
779
|
Chris@17
|
780 /**
|
Chris@17
|
781 * Pass if the page title is the given string.
|
Chris@17
|
782 *
|
Chris@17
|
783 * @param string $title
|
Chris@17
|
784 * The string the title should be.
|
Chris@17
|
785 * @param string $message
|
Chris@17
|
786 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
787 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
788 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
789 * will be displayed.
|
Chris@17
|
790 * @param string $group
|
Chris@17
|
791 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
792 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
793 * translate this string. Defaults to 'Other'; most tests do not override
|
Chris@17
|
794 * this default.
|
Chris@17
|
795 *
|
Chris@17
|
796 * @return bool
|
Chris@17
|
797 * TRUE on pass, FALSE on fail.
|
Chris@17
|
798 */
|
Chris@17
|
799 protected function assertTitle($title, $message = '', $group = 'Other') {
|
Chris@17
|
800 // Don't use xpath as it messes with HTML escaping.
|
Chris@17
|
801 preg_match('@<title>(.*)</title>@', $this->getRawContent(), $matches);
|
Chris@17
|
802 if (isset($matches[1])) {
|
Chris@17
|
803 $actual = $matches[1];
|
Chris@17
|
804 $actual = $this->castSafeStrings($actual);
|
Chris@17
|
805 $title = $this->castSafeStrings($title);
|
Chris@17
|
806 if (!$message) {
|
Chris@17
|
807 $message = new FormattableMarkup('Page title @actual is equal to @expected.', [
|
Chris@17
|
808 '@actual' => var_export($actual, TRUE),
|
Chris@17
|
809 '@expected' => var_export($title, TRUE),
|
Chris@17
|
810 ]);
|
Chris@17
|
811 }
|
Chris@17
|
812 return $this->assertEqual($actual, $title, $message, $group);
|
Chris@17
|
813 }
|
Chris@17
|
814 return $this->fail('No title element found on the page.');
|
Chris@17
|
815 }
|
Chris@17
|
816
|
Chris@17
|
817 /**
|
Chris@17
|
818 * Pass if the page title is not the given string.
|
Chris@17
|
819 *
|
Chris@17
|
820 * @param string $title
|
Chris@17
|
821 * The string the title should not be.
|
Chris@17
|
822 * @param string $message
|
Chris@17
|
823 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
824 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
825 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
826 * will be displayed.
|
Chris@17
|
827 * @param string $group
|
Chris@17
|
828 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
829 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
830 * translate this string. Defaults to 'Other'; most tests do not override
|
Chris@17
|
831 * this default.
|
Chris@17
|
832 *
|
Chris@17
|
833 * @return bool
|
Chris@17
|
834 * TRUE on pass, FALSE on fail.
|
Chris@17
|
835 */
|
Chris@17
|
836 protected function assertNoTitle($title, $message = '', $group = 'Other') {
|
Chris@17
|
837 $actual = (string) current($this->xpath('//title'));
|
Chris@17
|
838 if (!$message) {
|
Chris@17
|
839 $message = new FormattableMarkup('Page title @actual is not equal to @unexpected.', [
|
Chris@17
|
840 '@actual' => var_export($actual, TRUE),
|
Chris@17
|
841 '@unexpected' => var_export($title, TRUE),
|
Chris@17
|
842 ]);
|
Chris@17
|
843 }
|
Chris@17
|
844 return $this->assertNotEqual($actual, $title, $message, $group);
|
Chris@17
|
845 }
|
Chris@17
|
846
|
Chris@17
|
847 /**
|
Chris@17
|
848 * Asserts themed output.
|
Chris@17
|
849 *
|
Chris@17
|
850 * @param string $callback
|
Chris@17
|
851 * The name of the theme hook to invoke; e.g. 'links' for links.html.twig.
|
Chris@17
|
852 * @param string $variables
|
Chris@17
|
853 * An array of variables to pass to the theme function.
|
Chris@17
|
854 * @param string $expected
|
Chris@17
|
855 * The expected themed output string.
|
Chris@17
|
856 * @param string $message
|
Chris@17
|
857 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
858 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
859 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
860 * will be displayed.
|
Chris@17
|
861 * @param string $group
|
Chris@17
|
862 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
863 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
864 * translate this string. Defaults to 'Other'; most tests do not override
|
Chris@17
|
865 * this default.
|
Chris@17
|
866 *
|
Chris@17
|
867 * @return bool
|
Chris@17
|
868 * TRUE on pass, FALSE on fail.
|
Chris@17
|
869 */
|
Chris@17
|
870 protected function assertThemeOutput($callback, array $variables = [], $expected = '', $message = '', $group = 'Other') {
|
Chris@17
|
871 /** @var \Drupal\Core\Render\RendererInterface $renderer */
|
Chris@17
|
872 $renderer = \Drupal::service('renderer');
|
Chris@17
|
873
|
Chris@17
|
874 // The string cast is necessary because theme functions return
|
Chris@17
|
875 // MarkupInterface objects. This means we can assert that $expected
|
Chris@17
|
876 // matches the theme output without having to worry about 0 == ''.
|
Chris@17
|
877 $output = (string) $renderer->executeInRenderContext(new RenderContext(), function () use ($callback, $variables) {
|
Chris@17
|
878 return \Drupal::theme()->render($callback, $variables);
|
Chris@17
|
879 });
|
Chris@17
|
880 $this->verbose(
|
Chris@17
|
881 '<hr />' . 'Result:' . '<pre>' . Html::escape(var_export($output, TRUE)) . '</pre>'
|
Chris@17
|
882 . '<hr />' . 'Expected:' . '<pre>' . Html::escape(var_export($expected, TRUE)) . '</pre>'
|
Chris@17
|
883 . '<hr />' . $output
|
Chris@17
|
884 );
|
Chris@17
|
885 if (!$message) {
|
Chris@17
|
886 $message = '%callback rendered correctly.';
|
Chris@17
|
887 }
|
Chris@17
|
888 $message = format_string($message, ['%callback' => 'theme_' . $callback . '()']);
|
Chris@17
|
889 return $this->assertIdentical($output, $expected, $message, $group);
|
Chris@17
|
890 }
|
Chris@17
|
891
|
Chris@17
|
892 /**
|
Chris@17
|
893 * Asserts that a field exists in the current page with a given Xpath result.
|
Chris@17
|
894 *
|
Chris@17
|
895 * @param \SimpleXmlElement[] $fields
|
Chris@17
|
896 * Xml elements.
|
Chris@17
|
897 * @param string $value
|
Chris@17
|
898 * (optional) Value of the field to assert. You may pass in NULL (default) to skip
|
Chris@17
|
899 * checking the actual value, while still checking that the field exists.
|
Chris@17
|
900 * @param string $message
|
Chris@17
|
901 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
902 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
903 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
904 * will be displayed.
|
Chris@17
|
905 * @param string $group
|
Chris@17
|
906 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
907 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
908 * translate this string. Defaults to 'Other'; most tests do not override
|
Chris@17
|
909 * this default.
|
Chris@17
|
910 *
|
Chris@17
|
911 * @return bool
|
Chris@17
|
912 * TRUE on pass, FALSE on fail.
|
Chris@17
|
913 */
|
Chris@17
|
914 protected function assertFieldsByValue($fields, $value = NULL, $message = '', $group = 'Other') {
|
Chris@17
|
915 // If value specified then check array for match.
|
Chris@17
|
916 $found = TRUE;
|
Chris@17
|
917 if (isset($value)) {
|
Chris@17
|
918 $found = FALSE;
|
Chris@17
|
919 if ($fields) {
|
Chris@17
|
920 foreach ($fields as $field) {
|
Chris@17
|
921 if (isset($field['value']) && $field['value'] == $value) {
|
Chris@17
|
922 // Input element with correct value.
|
Chris@17
|
923 $found = TRUE;
|
Chris@17
|
924 }
|
Chris@17
|
925 elseif (isset($field->option) || isset($field->optgroup)) {
|
Chris@17
|
926 // Select element found.
|
Chris@17
|
927 $selected = $this->getSelectedItem($field);
|
Chris@17
|
928 if ($selected === FALSE) {
|
Chris@17
|
929 // No item selected so use first item.
|
Chris@17
|
930 $items = $this->getAllOptions($field);
|
Chris@17
|
931 if (!empty($items) && $items[0]['value'] == $value) {
|
Chris@17
|
932 $found = TRUE;
|
Chris@17
|
933 }
|
Chris@17
|
934 }
|
Chris@17
|
935 elseif ($selected == $value) {
|
Chris@17
|
936 $found = TRUE;
|
Chris@17
|
937 }
|
Chris@17
|
938 }
|
Chris@17
|
939 elseif ((string) $field == $value) {
|
Chris@17
|
940 // Text area with correct text.
|
Chris@17
|
941 $found = TRUE;
|
Chris@17
|
942 }
|
Chris@17
|
943 }
|
Chris@17
|
944 }
|
Chris@17
|
945 }
|
Chris@17
|
946 return $this->assertTrue($fields && $found, $message, $group);
|
Chris@17
|
947 }
|
Chris@17
|
948
|
Chris@17
|
949 /**
|
Chris@17
|
950 * Asserts that a field exists in the current page by the given XPath.
|
Chris@17
|
951 *
|
Chris@17
|
952 * @param string $xpath
|
Chris@17
|
953 * XPath used to find the field.
|
Chris@17
|
954 * @param string $value
|
Chris@17
|
955 * (optional) Value of the field to assert. You may pass in NULL (default)
|
Chris@17
|
956 * to skip checking the actual value, while still checking that the field
|
Chris@17
|
957 * exists.
|
Chris@17
|
958 * @param string $message
|
Chris@17
|
959 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
960 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
961 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
962 * will be displayed.
|
Chris@17
|
963 * @param string $group
|
Chris@17
|
964 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
965 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
966 * translate this string. Defaults to 'Other'; most tests do not override
|
Chris@17
|
967 * this default.
|
Chris@17
|
968 *
|
Chris@17
|
969 * @return bool
|
Chris@17
|
970 * TRUE on pass, FALSE on fail.
|
Chris@17
|
971 */
|
Chris@17
|
972 protected function assertFieldByXPath($xpath, $value = NULL, $message = '', $group = 'Other') {
|
Chris@17
|
973 $fields = $this->xpath($xpath);
|
Chris@17
|
974
|
Chris@17
|
975 return $this->assertFieldsByValue($fields, $value, $message, $group);
|
Chris@17
|
976 }
|
Chris@17
|
977
|
Chris@17
|
978 /**
|
Chris@17
|
979 * Get the selected value from a select field.
|
Chris@17
|
980 *
|
Chris@17
|
981 * @param \SimpleXmlElement $element
|
Chris@17
|
982 * SimpleXMLElement select element.
|
Chris@17
|
983 *
|
Chris@17
|
984 * @return bool
|
Chris@17
|
985 * The selected value or FALSE.
|
Chris@17
|
986 */
|
Chris@17
|
987 protected function getSelectedItem(\SimpleXMLElement $element) {
|
Chris@17
|
988 foreach ($element->children() as $item) {
|
Chris@17
|
989 if (isset($item['selected'])) {
|
Chris@17
|
990 return $item['value'];
|
Chris@17
|
991 }
|
Chris@17
|
992 elseif ($item->getName() == 'optgroup') {
|
Chris@17
|
993 if ($value = $this->getSelectedItem($item)) {
|
Chris@17
|
994 return $value;
|
Chris@17
|
995 }
|
Chris@17
|
996 }
|
Chris@17
|
997 }
|
Chris@17
|
998 return FALSE;
|
Chris@17
|
999 }
|
Chris@17
|
1000
|
Chris@17
|
1001 /**
|
Chris@17
|
1002 * Asserts that a field does not exist or its value does not match, by XPath.
|
Chris@17
|
1003 *
|
Chris@17
|
1004 * @param string $xpath
|
Chris@17
|
1005 * XPath used to find the field.
|
Chris@17
|
1006 * @param string $value
|
Chris@17
|
1007 * (optional) Value of the field, to assert that the field's value on the
|
Chris@17
|
1008 * page does not match it.
|
Chris@17
|
1009 * @param string $message
|
Chris@17
|
1010 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
1011 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
1012 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
1013 * will be displayed.
|
Chris@17
|
1014 * @param string $group
|
Chris@17
|
1015 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
1016 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
1017 * translate this string. Defaults to 'Other'; most tests do not override
|
Chris@17
|
1018 * this default.
|
Chris@17
|
1019 *
|
Chris@17
|
1020 * @return bool
|
Chris@17
|
1021 * TRUE on pass, FALSE on fail.
|
Chris@17
|
1022 */
|
Chris@17
|
1023 protected function assertNoFieldByXPath($xpath, $value = NULL, $message = '', $group = 'Other') {
|
Chris@17
|
1024 $fields = $this->xpath($xpath);
|
Chris@17
|
1025
|
Chris@17
|
1026 // If value specified then check array for match.
|
Chris@17
|
1027 $found = TRUE;
|
Chris@17
|
1028 if (isset($value)) {
|
Chris@17
|
1029 $found = FALSE;
|
Chris@17
|
1030 if ($fields) {
|
Chris@17
|
1031 foreach ($fields as $field) {
|
Chris@17
|
1032 if ($field['value'] == $value) {
|
Chris@17
|
1033 $found = TRUE;
|
Chris@17
|
1034 }
|
Chris@17
|
1035 }
|
Chris@17
|
1036 }
|
Chris@17
|
1037 }
|
Chris@17
|
1038 return $this->assertFalse($fields && $found, $message, $group);
|
Chris@17
|
1039 }
|
Chris@17
|
1040
|
Chris@17
|
1041 /**
|
Chris@17
|
1042 * Asserts that a field exists with the given name and value.
|
Chris@17
|
1043 *
|
Chris@17
|
1044 * @param string $name
|
Chris@17
|
1045 * Name of field to assert.
|
Chris@17
|
1046 * @param string $value
|
Chris@17
|
1047 * (optional) Value of the field to assert. You may pass in NULL (default)
|
Chris@17
|
1048 * to skip checking the actual value, while still checking that the field
|
Chris@17
|
1049 * exists.
|
Chris@17
|
1050 * @param string $message
|
Chris@17
|
1051 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
1052 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
1053 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
1054 * will be displayed.
|
Chris@17
|
1055 * @param string $group
|
Chris@17
|
1056 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
1057 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
1058 * translate this string. Defaults to 'Browser'; most tests do not override
|
Chris@17
|
1059 * this default.
|
Chris@17
|
1060 *
|
Chris@17
|
1061 * @return bool
|
Chris@17
|
1062 * TRUE on pass, FALSE on fail.
|
Chris@17
|
1063 */
|
Chris@17
|
1064 protected function assertFieldByName($name, $value = NULL, $message = NULL, $group = 'Browser') {
|
Chris@17
|
1065 if (!isset($message)) {
|
Chris@17
|
1066 if (!isset($value)) {
|
Chris@17
|
1067 $message = new FormattableMarkup('Found field with name @name', [
|
Chris@17
|
1068 '@name' => var_export($name, TRUE),
|
Chris@17
|
1069 ]);
|
Chris@17
|
1070 }
|
Chris@17
|
1071 else {
|
Chris@17
|
1072 $message = new FormattableMarkup('Found field with name @name and value @value', [
|
Chris@17
|
1073 '@name' => var_export($name, TRUE),
|
Chris@17
|
1074 '@value' => var_export($value, TRUE),
|
Chris@17
|
1075 ]);
|
Chris@17
|
1076 }
|
Chris@17
|
1077 }
|
Chris@17
|
1078 return $this->assertFieldByXPath($this->constructFieldXpath('name', $name), $value, $message, $group);
|
Chris@17
|
1079 }
|
Chris@17
|
1080
|
Chris@17
|
1081 /**
|
Chris@17
|
1082 * Asserts that a field does not exist with the given name and value.
|
Chris@17
|
1083 *
|
Chris@17
|
1084 * @param string $name
|
Chris@17
|
1085 * Name of field to assert.
|
Chris@17
|
1086 * @param string $value
|
Chris@17
|
1087 * (optional) Value for the field, to assert that the field's value on the
|
Chris@17
|
1088 * page doesn't match it. You may pass in NULL to skip checking the
|
Chris@17
|
1089 * value, while still checking that the field doesn't exist. However, the
|
Chris@17
|
1090 * default value ('') asserts that the field value is not an empty string.
|
Chris@17
|
1091 * @param string $message
|
Chris@17
|
1092 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
1093 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
1094 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
1095 * will be displayed.
|
Chris@17
|
1096 * @param string $group
|
Chris@17
|
1097 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
1098 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
1099 * translate this string. Defaults to 'Browser'; most tests do not override
|
Chris@17
|
1100 * this default.
|
Chris@17
|
1101 *
|
Chris@17
|
1102 * @return bool
|
Chris@17
|
1103 * TRUE on pass, FALSE on fail.
|
Chris@17
|
1104 */
|
Chris@17
|
1105 protected function assertNoFieldByName($name, $value = '', $message = '', $group = 'Browser') {
|
Chris@17
|
1106 return $this->assertNoFieldByXPath($this->constructFieldXpath('name', $name), $value, $message ? $message : new FormattableMarkup('Did not find field by name @name', ['@name' => $name]), $group);
|
Chris@17
|
1107 }
|
Chris@17
|
1108
|
Chris@17
|
1109 /**
|
Chris@17
|
1110 * Asserts that a field exists with the given ID and value.
|
Chris@17
|
1111 *
|
Chris@17
|
1112 * @param string $id
|
Chris@17
|
1113 * ID of field to assert.
|
Chris@17
|
1114 * @param string|\Drupal\Component\Render\MarkupInterface $value
|
Chris@17
|
1115 * (optional) Value for the field to assert. You may pass in NULL to skip
|
Chris@17
|
1116 * checking the value, while still checking that the field exists.
|
Chris@17
|
1117 * However, the default value ('') asserts that the field value is an empty
|
Chris@17
|
1118 * string.
|
Chris@17
|
1119 * @param string|\Drupal\Component\Render\MarkupInterface $message
|
Chris@17
|
1120 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
1121 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
1122 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
1123 * will be displayed.
|
Chris@17
|
1124 * @param string $group
|
Chris@17
|
1125 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
1126 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
1127 * translate this string. Defaults to 'Browser'; most tests do not override
|
Chris@17
|
1128 * this default.
|
Chris@17
|
1129 *
|
Chris@17
|
1130 * @return bool
|
Chris@17
|
1131 * TRUE on pass, FALSE on fail.
|
Chris@17
|
1132 */
|
Chris@17
|
1133 protected function assertFieldById($id, $value = '', $message = '', $group = 'Browser') {
|
Chris@17
|
1134 // Cast MarkupInterface objects to string.
|
Chris@17
|
1135 if (isset($value)) {
|
Chris@17
|
1136 $value = (string) $value;
|
Chris@17
|
1137 }
|
Chris@17
|
1138 $message = (string) $message;
|
Chris@17
|
1139 return $this->assertFieldByXPath($this->constructFieldXpath('id', $id), $value, $message ? $message : new FormattableMarkup('Found field by id @id', ['@id' => $id]), $group);
|
Chris@17
|
1140 }
|
Chris@17
|
1141
|
Chris@17
|
1142 /**
|
Chris@17
|
1143 * Asserts that a field does not exist with the given ID and value.
|
Chris@17
|
1144 *
|
Chris@17
|
1145 * @param string $id
|
Chris@17
|
1146 * ID of field to assert.
|
Chris@17
|
1147 * @param string $value
|
Chris@17
|
1148 * (optional) Value for the field, to assert that the field's value on the
|
Chris@17
|
1149 * page doesn't match it. You may pass in NULL to skip checking the value,
|
Chris@17
|
1150 * while still checking that the field doesn't exist. However, the default
|
Chris@17
|
1151 * value ('') asserts that the field value is not an empty string.
|
Chris@17
|
1152 * @param string $message
|
Chris@17
|
1153 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
1154 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
1155 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
1156 * will be displayed.
|
Chris@17
|
1157 * @param string $group
|
Chris@17
|
1158 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
1159 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
1160 * translate this string. Defaults to 'Browser'; most tests do not override
|
Chris@17
|
1161 * this default.
|
Chris@17
|
1162 *
|
Chris@17
|
1163 * @return bool
|
Chris@17
|
1164 * TRUE on pass, FALSE on fail.
|
Chris@17
|
1165 */
|
Chris@17
|
1166 protected function assertNoFieldById($id, $value = '', $message = '', $group = 'Browser') {
|
Chris@17
|
1167 return $this->assertNoFieldByXPath($this->constructFieldXpath('id', $id), $value, $message ? $message : new FormattableMarkup('Did not find field by id @id', ['@id' => $id]), $group);
|
Chris@17
|
1168 }
|
Chris@17
|
1169
|
Chris@17
|
1170 /**
|
Chris@17
|
1171 * Asserts that a checkbox field in the current page is checked.
|
Chris@17
|
1172 *
|
Chris@17
|
1173 * @param string $id
|
Chris@17
|
1174 * ID of field to assert.
|
Chris@17
|
1175 * @param string $message
|
Chris@17
|
1176 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
1177 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
1178 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
1179 * will be displayed.
|
Chris@17
|
1180 * @param string $group
|
Chris@17
|
1181 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
1182 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
1183 * translate this string. Defaults to 'Browser'; most tests do not override
|
Chris@17
|
1184 * this default.
|
Chris@17
|
1185 *
|
Chris@17
|
1186 * @return bool
|
Chris@17
|
1187 * TRUE on pass, FALSE on fail.
|
Chris@17
|
1188 */
|
Chris@17
|
1189 protected function assertFieldChecked($id, $message = '', $group = 'Browser') {
|
Chris@17
|
1190 $elements = $this->xpath('//input[@id=:id]', [':id' => $id]);
|
Chris@17
|
1191 return $this->assertTrue(isset($elements[0]) && !empty($elements[0]['checked']), $message ? $message : new FormattableMarkup('Checkbox field @id is checked.', ['@id' => $id]), $group);
|
Chris@17
|
1192 }
|
Chris@17
|
1193
|
Chris@17
|
1194 /**
|
Chris@17
|
1195 * Asserts that a checkbox field in the current page is not checked.
|
Chris@17
|
1196 *
|
Chris@17
|
1197 * @param string $id
|
Chris@17
|
1198 * ID of field to assert.
|
Chris@17
|
1199 * @param string $message
|
Chris@17
|
1200 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
1201 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
1202 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
1203 * will be displayed.
|
Chris@17
|
1204 * @param string $group
|
Chris@17
|
1205 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
1206 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
1207 * translate this string. Defaults to 'Browser'; most tests do not override
|
Chris@17
|
1208 * this default.
|
Chris@17
|
1209 *
|
Chris@17
|
1210 * @return bool
|
Chris@17
|
1211 * TRUE on pass, FALSE on fail.
|
Chris@17
|
1212 */
|
Chris@17
|
1213 protected function assertNoFieldChecked($id, $message = '', $group = 'Browser') {
|
Chris@17
|
1214 $elements = $this->xpath('//input[@id=:id]', [':id' => $id]);
|
Chris@17
|
1215 return $this->assertTrue(isset($elements[0]) && empty($elements[0]['checked']), $message ? $message : new FormattableMarkup('Checkbox field @id is not checked.', ['@id' => $id]), $group);
|
Chris@17
|
1216 }
|
Chris@17
|
1217
|
Chris@17
|
1218 /**
|
Chris@17
|
1219 * Asserts that a select option in the current page exists.
|
Chris@17
|
1220 *
|
Chris@17
|
1221 * @param string $id
|
Chris@17
|
1222 * ID of select field to assert.
|
Chris@17
|
1223 * @param string $option
|
Chris@17
|
1224 * Option to assert.
|
Chris@17
|
1225 * @param string $message
|
Chris@17
|
1226 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
1227 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
1228 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
1229 * will be displayed.
|
Chris@17
|
1230 * @param string $group
|
Chris@17
|
1231 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
1232 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
1233 * translate this string. Defaults to 'Browser'; most tests do not override
|
Chris@17
|
1234 * this default.
|
Chris@17
|
1235 *
|
Chris@17
|
1236 * @return bool
|
Chris@17
|
1237 * TRUE on pass, FALSE on fail.
|
Chris@17
|
1238 */
|
Chris@17
|
1239 protected function assertOption($id, $option, $message = '', $group = 'Browser') {
|
Chris@17
|
1240 $options = $this->xpath('//select[@id=:id]//option[@value=:option]', [':id' => $id, ':option' => $option]);
|
Chris@17
|
1241 return $this->assertTrue(isset($options[0]), $message ? $message : new FormattableMarkup('Option @option for field @id exists.', ['@option' => $option, '@id' => $id]), $group);
|
Chris@17
|
1242 }
|
Chris@17
|
1243
|
Chris@17
|
1244 /**
|
Chris@17
|
1245 * Asserts that a select option with the visible text exists.
|
Chris@17
|
1246 *
|
Chris@17
|
1247 * @param string $id
|
Chris@17
|
1248 * The ID of the select field to assert.
|
Chris@17
|
1249 * @param string $text
|
Chris@17
|
1250 * The text for the option tag to assert.
|
Chris@17
|
1251 * @param string $message
|
Chris@17
|
1252 * (optional) A message to display with the assertion.
|
Chris@17
|
1253 *
|
Chris@17
|
1254 * @return bool
|
Chris@17
|
1255 * TRUE on pass, FALSE on fail.
|
Chris@17
|
1256 */
|
Chris@17
|
1257 protected function assertOptionByText($id, $text, $message = '') {
|
Chris@17
|
1258 $options = $this->xpath('//select[@id=:id]//option[normalize-space(text())=:text]', [':id' => $id, ':text' => $text]);
|
Chris@17
|
1259 return $this->assertTrue(isset($options[0]), $message ?: 'Option with text label ' . $text . ' for select field ' . $id . ' exits.');
|
Chris@17
|
1260 }
|
Chris@17
|
1261
|
Chris@17
|
1262 /**
|
Chris@17
|
1263 * Asserts that a select option in the current page exists.
|
Chris@17
|
1264 *
|
Chris@17
|
1265 * @param string $drupal_selector
|
Chris@17
|
1266 * The data drupal selector of select field to assert.
|
Chris@17
|
1267 * @param string $option
|
Chris@17
|
1268 * Option to assert.
|
Chris@17
|
1269 * @param string $message
|
Chris@17
|
1270 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
1271 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
1272 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
1273 * will be displayed.
|
Chris@17
|
1274 * @param string $group
|
Chris@17
|
1275 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
1276 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
1277 * translate this string. Defaults to 'Browser'; most tests do not override
|
Chris@17
|
1278 * this default.
|
Chris@17
|
1279 *
|
Chris@17
|
1280 * @return bool
|
Chris@17
|
1281 * TRUE on pass, FALSE on fail.
|
Chris@17
|
1282 */
|
Chris@17
|
1283 protected function assertOptionWithDrupalSelector($drupal_selector, $option, $message = '', $group = 'Browser') {
|
Chris@17
|
1284 $options = $this->xpath('//select[@data-drupal-selector=:data_drupal_selector]//option[@value=:option]', [':data_drupal_selector' => $drupal_selector, ':option' => $option]);
|
Chris@17
|
1285 return $this->assertTrue(isset($options[0]), $message ? $message : new FormattableMarkup('Option @option for field @data_drupal_selector exists.', ['@option' => $option, '@data_drupal_selector' => $drupal_selector]), $group);
|
Chris@17
|
1286 }
|
Chris@17
|
1287
|
Chris@17
|
1288 /**
|
Chris@17
|
1289 * Asserts that a select option in the current page does not exist.
|
Chris@17
|
1290 *
|
Chris@17
|
1291 * @param string $id
|
Chris@17
|
1292 * ID of select field to assert.
|
Chris@17
|
1293 * @param string $option
|
Chris@17
|
1294 * Option to assert.
|
Chris@17
|
1295 * @param string $message
|
Chris@17
|
1296 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
1297 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
1298 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
1299 * will be displayed.
|
Chris@17
|
1300 * @param string $group
|
Chris@17
|
1301 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
1302 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
1303 * translate this string. Defaults to 'Browser'; most tests do not override
|
Chris@17
|
1304 * this default.
|
Chris@17
|
1305 *
|
Chris@17
|
1306 * @return bool
|
Chris@17
|
1307 * TRUE on pass, FALSE on fail.
|
Chris@17
|
1308 */
|
Chris@17
|
1309 protected function assertNoOption($id, $option, $message = '', $group = 'Browser') {
|
Chris@17
|
1310 $selects = $this->xpath('//select[@id=:id]', [':id' => $id]);
|
Chris@17
|
1311 $options = $this->xpath('//select[@id=:id]//option[@value=:option]', [':id' => $id, ':option' => $option]);
|
Chris@17
|
1312 return $this->assertTrue(isset($selects[0]) && !isset($options[0]), $message ? $message : new FormattableMarkup('Option @option for field @id does not exist.', ['@option' => $option, '@id' => $id]), $group);
|
Chris@17
|
1313 }
|
Chris@17
|
1314
|
Chris@17
|
1315 /**
|
Chris@17
|
1316 * Asserts that a select option in the current page is checked.
|
Chris@17
|
1317 *
|
Chris@17
|
1318 * @param string $id
|
Chris@17
|
1319 * ID of select field to assert.
|
Chris@17
|
1320 * @param string $option
|
Chris@17
|
1321 * Option to assert.
|
Chris@17
|
1322 * @param string $message
|
Chris@17
|
1323 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
1324 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
1325 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
1326 * will be displayed.
|
Chris@17
|
1327 * @param string $group
|
Chris@17
|
1328 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
1329 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
1330 * translate this string. Defaults to 'Browser'; most tests do not override
|
Chris@17
|
1331 * this default.
|
Chris@17
|
1332 *
|
Chris@17
|
1333 * @return bool
|
Chris@17
|
1334 * TRUE on pass, FALSE on fail.
|
Chris@17
|
1335 *
|
Chris@17
|
1336 * @todo $id is unusable. Replace with $name.
|
Chris@17
|
1337 */
|
Chris@17
|
1338 protected function assertOptionSelected($id, $option, $message = '', $group = 'Browser') {
|
Chris@17
|
1339 $elements = $this->xpath('//select[@id=:id]//option[@value=:option]', [':id' => $id, ':option' => $option]);
|
Chris@17
|
1340 return $this->assertTrue(isset($elements[0]) && !empty($elements[0]['selected']), $message ? $message : new FormattableMarkup('Option @option for field @id is selected.', ['@option' => $option, '@id' => $id]), $group);
|
Chris@17
|
1341 }
|
Chris@17
|
1342
|
Chris@17
|
1343 /**
|
Chris@17
|
1344 * Asserts that a select option in the current page is checked.
|
Chris@17
|
1345 *
|
Chris@17
|
1346 * @param string $drupal_selector
|
Chris@17
|
1347 * The data drupal selector of select field to assert.
|
Chris@17
|
1348 * @param string $option
|
Chris@17
|
1349 * Option to assert.
|
Chris@17
|
1350 * @param string $message
|
Chris@17
|
1351 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
1352 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
1353 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
1354 * will be displayed.
|
Chris@17
|
1355 * @param string $group
|
Chris@17
|
1356 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
1357 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
1358 * translate this string. Defaults to 'Browser'; most tests do not override
|
Chris@17
|
1359 * this default.
|
Chris@17
|
1360 *
|
Chris@17
|
1361 * @return bool
|
Chris@17
|
1362 * TRUE on pass, FALSE on fail.
|
Chris@17
|
1363 *
|
Chris@17
|
1364 * @todo $id is unusable. Replace with $name.
|
Chris@17
|
1365 */
|
Chris@17
|
1366 protected function assertOptionSelectedWithDrupalSelector($drupal_selector, $option, $message = '', $group = 'Browser') {
|
Chris@17
|
1367 $elements = $this->xpath('//select[@data-drupal-selector=:data_drupal_selector]//option[@value=:option]', [':data_drupal_selector' => $drupal_selector, ':option' => $option]);
|
Chris@17
|
1368 return $this->assertTrue(isset($elements[0]) && !empty($elements[0]['selected']), $message ? $message : new FormattableMarkup('Option @option for field @data_drupal_selector is selected.', ['@option' => $option, '@data_drupal_selector' => $drupal_selector]), $group);
|
Chris@17
|
1369 }
|
Chris@17
|
1370
|
Chris@17
|
1371 /**
|
Chris@17
|
1372 * Asserts that a select option in the current page is not checked.
|
Chris@17
|
1373 *
|
Chris@17
|
1374 * @param string $id
|
Chris@17
|
1375 * ID of select field to assert.
|
Chris@17
|
1376 * @param string $option
|
Chris@17
|
1377 * Option to assert.
|
Chris@17
|
1378 * @param string $message
|
Chris@17
|
1379 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
1380 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
1381 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
1382 * will be displayed.
|
Chris@17
|
1383 * @param string $group
|
Chris@17
|
1384 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
1385 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
1386 * translate this string. Defaults to 'Browser'; most tests do not override
|
Chris@17
|
1387 * this default.
|
Chris@17
|
1388 *
|
Chris@17
|
1389 * @return bool
|
Chris@17
|
1390 * TRUE on pass, FALSE on fail.
|
Chris@17
|
1391 */
|
Chris@17
|
1392 protected function assertNoOptionSelected($id, $option, $message = '', $group = 'Browser') {
|
Chris@17
|
1393 $elements = $this->xpath('//select[@id=:id]//option[@value=:option]', [':id' => $id, ':option' => $option]);
|
Chris@17
|
1394 return $this->assertTrue(isset($elements[0]) && empty($elements[0]['selected']), $message ? $message : new FormattableMarkup('Option @option for field @id is not selected.', ['@option' => $option, '@id' => $id]), $group);
|
Chris@17
|
1395 }
|
Chris@17
|
1396
|
Chris@17
|
1397 /**
|
Chris@17
|
1398 * Asserts that a field exists with the given name or ID.
|
Chris@17
|
1399 *
|
Chris@17
|
1400 * @param string $field
|
Chris@17
|
1401 * Name or ID of field to assert.
|
Chris@17
|
1402 * @param string $message
|
Chris@17
|
1403 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
1404 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
1405 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
1406 * will be displayed.
|
Chris@17
|
1407 * @param string $group
|
Chris@17
|
1408 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
1409 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
1410 * translate this string. Defaults to 'Other'; most tests do not override
|
Chris@17
|
1411 * this default.
|
Chris@17
|
1412 *
|
Chris@17
|
1413 * @return bool
|
Chris@17
|
1414 * TRUE on pass, FALSE on fail.
|
Chris@17
|
1415 */
|
Chris@17
|
1416 protected function assertField($field, $message = '', $group = 'Other') {
|
Chris@17
|
1417 return $this->assertFieldByXPath($this->constructFieldXpath('name', $field) . '|' . $this->constructFieldXpath('id', $field), NULL, $message, $group);
|
Chris@17
|
1418 }
|
Chris@17
|
1419
|
Chris@17
|
1420 /**
|
Chris@17
|
1421 * Asserts that a field does not exist with the given name or ID.
|
Chris@17
|
1422 *
|
Chris@17
|
1423 * @param string $field
|
Chris@17
|
1424 * Name or ID of field to assert.
|
Chris@17
|
1425 * @param string $message
|
Chris@17
|
1426 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
1427 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
1428 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
1429 * will be displayed.
|
Chris@17
|
1430 * @param string $group
|
Chris@17
|
1431 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
1432 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
1433 * translate this string. Defaults to 'Other'; most tests do not override
|
Chris@17
|
1434 * this default.
|
Chris@17
|
1435 *
|
Chris@17
|
1436 * @return bool
|
Chris@17
|
1437 * TRUE on pass, FALSE on fail.
|
Chris@17
|
1438 */
|
Chris@17
|
1439 protected function assertNoField($field, $message = '', $group = 'Other') {
|
Chris@17
|
1440 return $this->assertNoFieldByXPath($this->constructFieldXpath('name', $field) . '|' . $this->constructFieldXpath('id', $field), NULL, $message, $group);
|
Chris@17
|
1441 }
|
Chris@17
|
1442
|
Chris@17
|
1443 /**
|
Chris@17
|
1444 * Asserts that each HTML ID is used for just a single element.
|
Chris@17
|
1445 *
|
Chris@17
|
1446 * @param string $message
|
Chris@17
|
1447 * (optional) A message to display with the assertion. Do not translate
|
Chris@17
|
1448 * messages: use \Drupal\Component\Render\FormattableMarkup to embed
|
Chris@17
|
1449 * variables in the message text, not t(). If left blank, a default message
|
Chris@17
|
1450 * will be displayed.
|
Chris@17
|
1451 * @param string $group
|
Chris@17
|
1452 * (optional) The group this message is in, which is displayed in a column
|
Chris@17
|
1453 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@17
|
1454 * translate this string. Defaults to 'Other'; most tests do not override
|
Chris@17
|
1455 * this default.
|
Chris@17
|
1456 * @param array $ids_to_skip
|
Chris@17
|
1457 * An optional array of ids to skip when checking for duplicates. It is
|
Chris@17
|
1458 * always a bug to have duplicate HTML IDs, so this parameter is to enable
|
Chris@17
|
1459 * incremental fixing of core code. Whenever a test passes this parameter,
|
Chris@17
|
1460 * it should add a "todo" comment above the call to this function explaining
|
Chris@17
|
1461 * the legacy bug that the test wishes to ignore and including a link to an
|
Chris@17
|
1462 * issue that is working to fix that legacy bug.
|
Chris@17
|
1463 *
|
Chris@17
|
1464 * @return bool
|
Chris@17
|
1465 * TRUE on pass, FALSE on fail.
|
Chris@17
|
1466 */
|
Chris@17
|
1467 protected function assertNoDuplicateIds($message = '', $group = 'Other', $ids_to_skip = []) {
|
Chris@17
|
1468 $status = TRUE;
|
Chris@17
|
1469 foreach ($this->xpath('//*[@id]') as $element) {
|
Chris@17
|
1470 $id = (string) $element['id'];
|
Chris@17
|
1471 if (isset($seen_ids[$id]) && !in_array($id, $ids_to_skip)) {
|
Chris@17
|
1472 $this->fail(new FormattableMarkup('The HTML ID %id is unique.', ['%id' => $id]), $group);
|
Chris@17
|
1473 $status = FALSE;
|
Chris@17
|
1474 }
|
Chris@17
|
1475 $seen_ids[$id] = TRUE;
|
Chris@17
|
1476 }
|
Chris@17
|
1477 return $this->assert($status, $message, $group);
|
Chris@17
|
1478 }
|
Chris@17
|
1479
|
Chris@17
|
1480 /**
|
Chris@17
|
1481 * Helper: Constructs an XPath for the given set of attributes and value.
|
Chris@17
|
1482 *
|
Chris@17
|
1483 * @param string $attribute
|
Chris@17
|
1484 * Field attributes.
|
Chris@17
|
1485 * @param string $value
|
Chris@17
|
1486 * Value of field.
|
Chris@17
|
1487 *
|
Chris@17
|
1488 * @return string
|
Chris@17
|
1489 * XPath for specified values.
|
Chris@17
|
1490 */
|
Chris@17
|
1491 protected function constructFieldXpath($attribute, $value) {
|
Chris@17
|
1492 $xpath = '//textarea[@' . $attribute . '=:value]|//input[@' . $attribute . '=:value]|//select[@' . $attribute . '=:value]';
|
Chris@17
|
1493 return $this->buildXPathQuery($xpath, [':value' => $value]);
|
Chris@17
|
1494 }
|
Chris@17
|
1495
|
Chris@17
|
1496 }
|