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

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