198051d3702fe83c97a1a5a371ee9b79539fb13f
[yaffs-website] / web / core / modules / tour / src / Tests / TourTestBase.php
1 <?php
2
3 namespace Drupal\tour\Tests;
4
5 use Drupal\simpletest\WebTestBase;
6
7 /**
8  * Base class for testing Tour functionality.
9  */
10 abstract class TourTestBase extends WebTestBase {
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         $attributes = (array) $rendered_tip->attributes();
39         $tips[] = $attributes['@attributes'];
40       }
41     }
42
43     // If the tips are still empty we need to fail.
44     if (empty($tips)) {
45       $this->fail('Could not find tour tips on the current page.');
46     }
47     else {
48       // Check for corresponding page elements.
49       $total = 0;
50       $modals = 0;
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']]));
55         }
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']]));
59         }
60         else {
61           // It's a modal.
62           $modals++;
63         }
64         $total++;
65       }
66       $this->pass(format_string('Total %total Tips tested of which %modals modal(s).', ['%total' => $total, '%modals' => $modals]));
67     }
68   }
69
70 }