Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / tour / tests / src / Functional / TourTestBase.php
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       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']]));
57         }
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']]));
61         }
62         else {
63           // It's a modal.
64           $modals++;
65         }
66         $total++;
67       }
68       $this->pass(format_string('Total %total Tips tested of which %modals modal(s).', ['%total' => $total, '%modals' => $modals]));
69     }
70   }
71
72 }