TourTestBase.php 2.72 KB
Newer Older
1 2 3 4 5 6
<?php

namespace Drupal\tour\Tests;

use Drupal\simpletest\WebTestBase;

7 8
@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);

9
/**
10
 * Base class for testing Tour functionality.
11 12 13
 *
 * @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0.
 *   Use \Drupal\Tests\tour\Functional\TourTestBase instead.
14
 */
15
abstract class TourTestBase extends WebTestBase {
16 17 18 19 20 21 22 23 24 25 26 27 28

  /**
   * Assert function to determine if tips rendered to the page
   * have a corresponding page element.
   *
   * @param array $tips
   *   A list of tips which provide either a "data-id" or "data-class".
   *
   * @code
   * // Basic example.
   * $this->assertTourTips();
   *
   * // Advanced example. The following would be used for multipage or
29
   * // targeting a specific subset of tips.
30 31 32 33 34 35 36
   * $tips = array();
   * $tips[] = array('data-id' => 'foo');
   * $tips[] = array('data-id' => 'bar');
   * $tips[] = array('data-class' => 'baz');
   * $this->assertTourTips($tips);
   * @endcode
   */
37
  public function assertTourTips($tips = []) {
38 39 40
    // Get the rendered tips and their data-id and data-class attributes.
    if (empty($tips)) {
      // Tips are rendered as <li> elements inside <ol id="tour">.
41
      $rendered_tips = $this->xpath('//ol[@id = "tour"]//li[starts-with(@class, "tip")]');
42 43 44 45 46 47 48 49 50 51 52 53
      foreach ($rendered_tips as $rendered_tip) {
        $attributes = (array) $rendered_tip->attributes();
        $tips[] = $attributes['@attributes'];
      }
    }

    // If the tips are still empty we need to fail.
    if (empty($tips)) {
      $this->fail('Could not find tour tips on the current page.');
    }
    else {
      // Check for corresponding page elements.
54 55
      $total = 0;
      $modals = 0;
56 57 58
      foreach ($tips as $tip) {
        if (!empty($tip['data-id'])) {
          $elements = \PHPUnit_Util_XML::cssSelect('#' . $tip['data-id'], TRUE, $this->content, TRUE);
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 = \PHPUnit_Util_XML::cssSelect('.' . $tip['data-class'], TRUE, $this->content, TRUE);
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 66 67 68 69
        else {
          // It's a modal.
          $modals++;
        }
        $total++;
70
      }
71
      $this->pass(format_string('Total %total Tips tested of which %modals modal(s).', ['%total' => $total, '%modals' => $modals]));
72 73 74 75
    }
  }

}