3 namespace Drupal\tour\Tests;
5 use Drupal\simpletest\WebTestBase;
8 * Base class for testing Tour functionality.
10 abstract class TourTestBase extends WebTestBase {
13 * Assert function to determine if tips rendered to the page
14 * have a corresponding page element.
17 * A list of tips which provide either a "data-id" or "data-class".
21 * $this->assertTourTips();
23 * // Advanced example. The following would be used for multipage or
24 * // targeting a specific subset of tips.
26 * $tips[] = array('data-id' => 'foo');
27 * $tips[] = array('data-id' => 'bar');
28 * $tips[] = array('data-class' => 'baz');
29 * $this->assertTourTips($tips);
32 public function assertTourTips($tips = []) {
33 // Get the rendered tips and their data-id and data-class attributes.
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 $attributes = (array) $rendered_tip->attributes();
39 $tips[] = $attributes['@attributes'];
43 // If the tips are still empty we need to fail.
45 $this->fail('Could not find tour tips on the current page.');
48 // Check for corresponding page elements.
51 foreach ($tips as $tip) {
52 if (!empty($tip['data-id'])) {
53 $elements = \PHPUnit_Util_XML::cssSelect('#' . $tip['data-id'], TRUE, $this->content, TRUE);
54 $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']]));
56 elseif (!empty($tip['data-class'])) {
57 $elements = \PHPUnit_Util_XML::cssSelect('.' . $tip['data-class'], TRUE, $this->content, TRUE);
58 $this->assertFalse(empty($elements), format_string('Found corresponding page element for tour tip with class .%data-class', ['%data-class' => $tip['data-class']]));
66 $this->pass(format_string('Total %total Tips tested of which %modals modal(s).', ['%total' => $total, '%modals' => $modals]));