annotate core/modules/tour/src/Tests/TourTestBase.php @ 19:fa3358dc1485 tip

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