3 namespace Drupal\Tests\tour\Functional;
5 use Drupal\Tests\BrowserTestBase;
8 * Base class for testing Tour functionality.
10 abstract class TourTestBase extends BrowserTestBase {
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) {
39 'data-id' => $rendered_tip->getAttribute('data-id'),
40 'data-class' => $rendered_tip->getAttribute('data-class'),
45 // If the tips are still empty we need to fail.
47 $this->fail('Could not find tour tips on the current page.');
50 // Check for corresponding page elements.
53 foreach ($tips as $tip) {
54 if (!empty($tip['data-id'])) {
55 $elements = $this->getSession()->getPage()->findAll('css', '#' . $tip['data-id']);
56 $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 elseif (!empty($tip['data-class'])) {
59 $elements = $this->getSession()->getPage()->findAll('css', '.' . $tip['data-class']);
60 $this->assertFalse(empty($elements), format_string('Found corresponding page element for tour tip with class .%data-class', ['%data-class' => $tip['data-class']]));
68 $this->pass(format_string('Total %total Tips tested of which %modals modal(s).', ['%total' => $total, '%modals' => $modals]));