comparison core/modules/tour/tests/src/Functional/TourTestBase.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Tests\tour\Functional;
4
5 use Drupal\Tests\BrowserTestBase;
6
7 /**
8 * Base class for testing Tour functionality.
9 */
10 abstract class TourTestBase extends BrowserTestBase {
11
12 /**
13 * Assert function to determine if tips rendered to the page
14 * have a corresponding page element.
15 *
16 * @param array $tips
17 * A list of tips which provide either a "data-id" or "data-class".
18 *
19 * @code
20 * // Basic example.
21 * $this->assertTourTips();
22 *
23 * // Advanced example. The following would be used for multipage or
24 * // targeting a specific subset of tips.
25 * $tips = array();
26 * $tips[] = array('data-id' => 'foo');
27 * $tips[] = array('data-id' => 'bar');
28 * $tips[] = array('data-class' => 'baz');
29 * $this->assertTourTips($tips);
30 * @endcode
31 */
32 public function assertTourTips($tips = []) {
33 // Get the rendered tips and their data-id and data-class attributes.
34 if (empty($tips)) {
35 // Tips are rendered as <li> elements inside <ol id="tour">.
36 $rendered_tips = $this->xpath('//ol[@id = "tour"]//li[starts-with(@class, "tip")]');
37 foreach ($rendered_tips as $rendered_tip) {
38 $tips[] = [
39 'data-id' => $rendered_tip->getAttribute('data-id'),
40 'data-class' => $rendered_tip->getAttribute('data-class'),
41 ];
42 }
43 }
44
45 // If the tips are still empty we need to fail.
46 if (empty($tips)) {
47 $this->fail('Could not find tour tips on the current page.');
48 }
49 else {
50 // Check for corresponding page elements.
51 $total = 0;
52 $modals = 0;
53 $raw_content = $this->getSession()->getPage()->getContent();
54 foreach ($tips as $tip) {
55 if (!empty($tip['data-id'])) {
56 $elements = \PHPUnit_Util_XML::cssSelect('#' . $tip['data-id'], TRUE, $raw_content, TRUE);
57 $this->assertTrue(!empty($elements) && count($elements) === 1, format_string('Found corresponding page element for tour tip with id #%data-id', ['%data-id' => $tip['data-id']]));
58 }
59 elseif (!empty($tip['data-class'])) {
60 $elements = \PHPUnit_Util_XML::cssSelect('.' . $tip['data-class'], TRUE, $raw_content, TRUE);
61 $this->assertFalse(empty($elements), format_string('Found corresponding page element for tour tip with class .%data-class', ['%data-class' => $tip['data-class']]));
62 }
63 else {
64 // It's a modal.
65 $modals++;
66 }
67 $total++;
68 }
69 $this->pass(format_string('Total %total Tips tested of which %modals modal(s).', ['%total' => $total, '%modals' => $modals]));
70 }
71 }
72
73 }