You are here

public function TourTestBase::assertTourTips in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/tour/tests/src/Functional/TourTestBase.php \Drupal\Tests\tour\Functional\TourTestBase::assertTourTips()
  2. 10 core/modules/tour/tests/src/Functional/TourTestBase.php \Drupal\Tests\tour\Functional\TourTestBase::assertTourTips()

Assert function to determine if tips rendered to the page have a corresponding page element.

// Basic example.
$this
  ->assertTourTips();

// Advanced example. The following would be used for multipage or
// targeting a specific subset of tips.
$tips = array();
$tips[] = array(
  'data-id' => 'foo',
);
$tips[] = array(
  'data-id' => 'bar',
);
$tips[] = array(
  'data-class' => 'baz',
);
$this
  ->assertTourTips($tips);

Parameters

array $tips: A list of tips which provide either a "data-id" or "data-class".

9 calls to TourTestBase::assertTourTips()
BlockLayoutTourTest::testBlockLayoutTourTips in core/modules/block/tests/src/Functional/BlockLayoutTourTest.php
Tests Block Layout tour tip availability.
LanguageTourTest::testLanguageAddTour in core/modules/language/tests/src/Functional/LanguageTourTest.php
Go to add language page and check the tour tooltips.
LanguageTourTest::testLanguageEditTour in core/modules/language/tests/src/Functional/LanguageTourTest.php
Go to edit language page and check the tour tooltips.
LanguageTourTest::testLanguageTour in core/modules/language/tests/src/Functional/LanguageTourTest.php
Tests language tour tip availability.
LocaleTranslateStringTourTest::testTranslateStringTourTips in core/modules/locale/tests/src/Functional/LocaleTranslateStringTourTest.php
Tests locale tour tip availability.

... See full list

File

core/modules/tour/tests/src/Functional/TourTestBase.php, line 33

Class

TourTestBase
Base class for testing Tour functionality.

Namespace

Drupal\Tests\tour\Functional

Code

public function assertTourTips($tips = []) {

  // Get the rendered tips and their data-id and data-class attributes.
  if (empty($tips)) {

    // Tips are rendered as drupalSettings values.
    $drupalSettings = $this
      ->getDrupalSettings();
    if (isset($drupalSettings['_tour_internal'])) {
      foreach ($drupalSettings['_tour_internal'] as $tip) {
        $tips[] = [
          'selector' => $tip['selector'] ?? NULL,
        ];
      }
    }
  }

  // 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.
    $total = 0;
    $modals = 0;
    foreach ($tips as $tip) {
      if (!empty($tip['data-id'])) {
        $elements = $this
          ->getSession()
          ->getPage()
          ->findAll('css', '#' . $tip['data-id']);
        $this
          ->assertCount(1, $elements, new FormattableMarkup('Found corresponding page element for tour tip with id #%data-id', [
          '%data-id' => $tip['data-id'],
        ]));
      }
      elseif (!empty($tip['data-class'])) {
        $elements = $this
          ->getSession()
          ->getPage()
          ->findAll('css', '.' . $tip['data-class']);
        $this
          ->assertFalse(empty($elements), new FormattableMarkup('Found corresponding page element for tour tip with class .%data-class', [
          '%data-class' => $tip['data-class'],
        ]));
      }
      else {

        // It's a modal.
        $modals++;
      }
      $total++;
    }
  }
}