Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/tour/src/Tests/TourTestBase.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\tour\Tests; | |
4 | |
5 use Drupal\simpletest\WebTestBase; | |
6 | |
7 @trigger_error('\Drupal\tour\Tests\TourTestBase is deprecated in 8.4.0 and will be removed before Drupal 9.0.0. Instead, use \Drupal\Tests\tour\Functional\TourTestBase.', E_USER_DEPRECATED); | |
8 | |
9 /** | |
10 * Base class for testing Tour functionality. | |
11 * | |
12 * @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. | |
13 * Use \Drupal\Tests\tour\Functional\TourTestBase instead. | |
14 */ | |
15 abstract class TourTestBase extends WebTestBase { | |
16 | |
17 /** | |
18 * Assert function to determine if tips rendered to the page | |
19 * have a corresponding page element. | |
20 * | |
21 * @param array $tips | |
22 * A list of tips which provide either a "data-id" or "data-class". | |
23 * | |
24 * @code | |
25 * // Basic example. | |
26 * $this->assertTourTips(); | |
27 * | |
28 * // Advanced example. The following would be used for multipage or | |
29 * // targeting a specific subset of tips. | |
30 * $tips = array(); | |
31 * $tips[] = array('data-id' => 'foo'); | |
32 * $tips[] = array('data-id' => 'bar'); | |
33 * $tips[] = array('data-class' => 'baz'); | |
34 * $this->assertTourTips($tips); | |
35 * @endcode | |
36 */ | |
37 public function assertTourTips($tips = []) { | |
38 // Get the rendered tips and their data-id and data-class attributes. | |
39 if (empty($tips)) { | |
40 // Tips are rendered as <li> elements inside <ol id="tour">. | |
41 $rendered_tips = $this->xpath('//ol[@id = "tour"]//li[starts-with(@class, "tip")]'); | |
42 foreach ($rendered_tips as $rendered_tip) { | |
43 $attributes = (array) $rendered_tip->attributes(); | |
44 $tips[] = $attributes['@attributes']; | |
45 } | |
46 } | |
47 | |
48 // If the tips are still empty we need to fail. | |
49 if (empty($tips)) { | |
50 $this->fail('Could not find tour tips on the current page.'); | |
51 } | |
52 else { | |
53 // Check for corresponding page elements. | |
54 $total = 0; | |
55 $modals = 0; | |
56 foreach ($tips as $tip) { | |
57 if (!empty($tip['data-id'])) { | |
58 $elements = $this->xpath('//*[@id="' . $tip['data-id'] . '"]'); | |
59 $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']])); | |
60 } | |
61 elseif (!empty($tip['data-class'])) { | |
62 $elements = $this->xpath('//*[contain(@class, "' . $tip['data-id'] . '")]'); | |
63 $this->assertFalse(empty($elements), format_string('Found corresponding page element for tour tip with class .%data-class', ['%data-class' => $tip['data-class']])); | |
64 } | |
65 else { | |
66 // It's a modal. | |
67 $modals++; | |
68 } | |
69 $total++; | |
70 } | |
71 $this->pass(format_string('Total %total Tips tested of which %modals modal(s).', ['%total' => $total, '%modals' => $modals])); | |
72 } | |
73 } | |
74 | |
75 } |