first commit

This commit is contained in:
2024-07-15 12:33:27 +02:00
commit ce50ae282b
22084 changed files with 2623791 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\tour\Functional\Block;
use Drupal\Tests\tour\Functional\TourTestBase;
/**
* Tests the Block Layout tour.
*
* @group tour
* @group legacy
*/
class BlockLayoutTourTest extends TourTestBase {
/**
* An admin user with administrative permissions for Blocks.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = ['block', 'tour'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->adminUser = $this->drupalCreateUser(['administer blocks', 'access tour']);
$this->drupalLogin($this->adminUser);
$this->drupalPlaceBlock('local_actions_block');
}
/**
* Tests Block Layout tour tip availability.
*/
public function testBlockLayoutTourTips(): void {
$this->drupalGet('admin/structure/block');
$this->assertTourTips();
}
}

View File

@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\tour\Functional;
use Drupal\Tests\system\Functional\Module\GenericModuleTestBase;
/**
* Generic module test for tour.
*
* @group tour
* @group legacy
*/
class GenericTest extends GenericModuleTestBase {}

View File

@@ -0,0 +1,148 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\tour\Functional\Jsonapi;
use Drupal\Core\Url;
use Drupal\Tests\jsonapi\Functional\ConfigEntityResourceTestBase;
use Drupal\tour\Entity\Tour;
/**
* JSON:API integration test for the "Tour" config entity type.
*
* @group tour
* @group legacy
*/
class TourTest extends ConfigEntityResourceTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['tour'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected static $entityTypeId = 'tour';
/**
* {@inheritdoc}
*/
protected static $resourceTypeName = 'tour--tour';
/**
* {@inheritdoc}
*
* @var \Drupal\tour\TourInterface
*/
protected $entity;
/**
* {@inheritdoc}
*/
protected function setUpAuthorization($method) {
$this->grantPermissionsToTestedRole(['access tour']);
}
/**
* {@inheritdoc}
*/
protected function createEntity() {
$tour = Tour::create([
'id' => 'tour-llama',
'label' => 'Llama tour',
'langcode' => 'en',
'module' => 'tour',
'routes' => [
[
'route_name' => '<front>',
],
],
'tips' => [
'tour-llama-1' => [
'id' => 'tour-llama-1',
'plugin' => 'text',
'label' => 'Llama',
'body' => 'Who handle the awesomeness of llamas?',
'weight' => 100,
'selector' => '#tour-llama-1',
],
],
]);
$tour->save();
return $tour;
}
/**
* {@inheritdoc}
*/
protected function getExpectedDocument() {
$self_url = Url::fromUri('base:/jsonapi/tour/tour/' . $this->entity->uuid())->setAbsolute()->toString(TRUE)->getGeneratedUrl();
return [
'jsonapi' => [
'meta' => [
'links' => [
'self' => ['href' => 'http://jsonapi.org/format/1.0/'],
],
],
'version' => '1.0',
],
'links' => [
'self' => ['href' => $self_url],
],
'data' => [
'id' => $this->entity->uuid(),
'type' => 'tour--tour',
'links' => [
'self' => ['href' => $self_url],
],
'attributes' => [
'dependencies' => [],
'label' => 'Llama tour',
'langcode' => 'en',
'module' => 'tour',
'routes' => [
[
'route_name' => '<front>',
],
],
'status' => TRUE,
'tips' => [
'tour-llama-1' => [
'id' => 'tour-llama-1',
'plugin' => 'text',
'label' => 'Llama',
'body' => 'Who handle the awesomeness of llamas?',
'weight' => 100,
'selector' => '#tour-llama-1',
],
],
'drupal_internal__id' => 'tour-llama',
],
],
];
}
/**
* {@inheritdoc}
*/
protected function getPostDocument() {
// @todo Update in https://www.drupal.org/node/2300677.
return [];
}
/**
* {@inheritdoc}
*/
protected function getExpectedUnauthorizedAccessMessage($method) {
return "The following permissions are required: 'access tour' OR 'administer site configuration'.";
}
}

View File

@@ -0,0 +1,73 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\tour\Functional\Language;
use Drupal\Tests\tour\Functional\TourTestBase;
/**
* Tests tour functionality.
*
* @group tour
* @group legacy
*/
class LanguageTourTest extends TourTestBase {
/**
* An admin user with administrative permissions for views.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = ['block', 'language', 'tour'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->adminUser = $this->drupalCreateUser([
'administer languages',
'access tour',
]);
$this->drupalLogin($this->adminUser);
$this->drupalPlaceBlock('local_actions_block');
}
/**
* Tests language tour tip availability.
*/
public function testLanguageTour(): void {
$this->drupalGet('admin/config/regional/language');
$this->assertTourTips();
}
/**
* Go to add language page and check the tour tooltips.
*/
public function testLanguageAddTour(): void {
$this->drupalGet('admin/config/regional/language/add');
$this->assertTourTips();
}
/**
* Go to edit language page and check the tour tooltips.
*/
public function testLanguageEditTour(): void {
$this->drupalGet('admin/config/regional/language/edit/en');
$this->assertTourTips();
}
}

View File

@@ -0,0 +1,63 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\tour\Functional\Locale;
use Drupal\Tests\tour\Functional\TourTestBase;
/**
* Tests the Translate Interface tour.
*
* @group tour
* @group legacy
*/
class LocaleTranslateStringTourTest extends TourTestBase {
/**
* An admin user with administrative permissions to translate.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = ['locale', 'tour'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->adminUser = $this->drupalCreateUser([
'translate interface',
'access tour',
'administer languages',
]);
$this->drupalLogin($this->adminUser);
}
/**
* Tests locale tour tip availability.
*/
public function testTranslateStringTourTips(): void {
// Add another language so there are no missing form items.
$edit = [];
$edit['predefined_langcode'] = 'es';
$this->drupalGet('admin/config/regional/language/add');
$this->submitForm($edit, 'Add language');
$this->drupalGet('admin/config/regional/translate');
$this->assertTourTips();
}
}

View File

@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\tour\Functional\Rest;
use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
/**
* @group rest
* @group legacy
*/
class TourJsonAnonTest extends TourResourceTestBase {
use AnonResourceTestTrait;
/**
* {@inheritdoc}
*/
protected static $format = 'json';
/**
* {@inheritdoc}
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
}

View File

@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\tour\Functional\Rest;
use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
/**
* @group rest
* @group legacy
*/
class TourJsonBasicAuthTest extends TourResourceTestBase {
use BasicAuthResourceTestTrait;
/**
* {@inheritdoc}
*/
protected static $modules = ['basic_auth'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected static $format = 'json';
/**
* {@inheritdoc}
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $auth = 'basic_auth';
}

View File

@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\tour\Functional\Rest;
use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
/**
* @group rest
* @group legacy
*/
class TourJsonCookieTest extends TourResourceTestBase {
use CookieResourceTestTrait;
/**
* {@inheritdoc}
*/
protected static $format = 'json';
/**
* {@inheritdoc}
*/
protected static $mimeType = 'application/json';
/**
* {@inheritdoc}
*/
protected static $auth = 'cookie';
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
}

View File

@@ -0,0 +1,118 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\tour\Functional\Rest;
use Drupal\Tests\rest\Functional\EntityResource\ConfigEntityResourceTestBase;
use Drupal\tour\Entity\Tour;
abstract class TourResourceTestBase extends ConfigEntityResourceTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['tour'];
/**
* {@inheritdoc}
*/
protected static $entityTypeId = 'tour';
/**
* @var \Drupal\tour\TourInterface
*/
protected $entity;
/**
* {@inheritdoc}
*/
protected function setUpAuthorization($method) {
$this->grantPermissionsToTestedRole(['access tour']);
}
/**
* {@inheritdoc}
*/
protected function createEntity() {
$tour = Tour::create([
'id' => 'tour-llama',
'label' => 'Llama tour',
'langcode' => 'en',
'module' => 'tour',
'routes' => [
[
'route_name' => '<front>',
],
],
'tips' => [
'tour-llama-1' => [
'id' => 'tour-llama-1',
'plugin' => 'text',
'label' => 'Llama',
'body' => 'Who handle the awesomeness of llamas?',
'weight' => 100,
'selector' => '#tour-llama-1',
],
],
]);
$tour->save();
return $tour;
}
/**
* {@inheritdoc}
*/
protected function getExpectedNormalizedEntity() {
return [
'dependencies' => [],
'id' => 'tour-llama',
'label' => 'Llama tour',
'langcode' => 'en',
'module' => 'tour',
'routes' => [
[
'route_name' => '<front>',
],
],
'status' => TRUE,
'tips' => [
'tour-llama-1' => [
'id' => 'tour-llama-1',
'plugin' => 'text',
'label' => 'Llama',
'body' => 'Who handle the awesomeness of llamas?',
'weight' => 100,
'selector' => '#tour-llama-1',
],
],
'uuid' => $this->entity->uuid(),
];
}
/**
* {@inheritdoc}
*/
protected function getNormalizedPostEntity() {
// @todo Update in https://www.drupal.org/node/2300677.
return [];
}
/**
* {@inheritdoc}
*/
protected function getExpectedCacheContexts() {
return [
'user.permissions',
];
}
/**
* {@inheritdoc}
*/
protected function getExpectedUnauthorizedAccessMessage($method) {
return "The following permissions are required: 'access tour' OR 'administer site configuration'.";
}
}

View File

@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\tour\Functional\Rest;
use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
use Drupal\Tests\rest\Functional\EntityResource\XmlEntityNormalizationQuirksTrait;
/**
* @group rest
* @group legacy
*/
class TourXmlAnonTest extends TourResourceTestBase {
use AnonResourceTestTrait;
use XmlEntityNormalizationQuirksTrait;
/**
* {@inheritdoc}
*/
protected static $format = 'xml';
/**
* {@inheritdoc}
*/
protected static $mimeType = 'text/xml; charset=UTF-8';
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
}

View File

@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\tour\Functional\Rest;
use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
use Drupal\Tests\rest\Functional\EntityResource\XmlEntityNormalizationQuirksTrait;
/**
* @group rest
* @group legacy
*/
class TourXmlBasicAuthTest extends TourResourceTestBase {
use BasicAuthResourceTestTrait;
use XmlEntityNormalizationQuirksTrait;
/**
* {@inheritdoc}
*/
protected static $modules = ['basic_auth'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected static $format = 'xml';
/**
* {@inheritdoc}
*/
protected static $mimeType = 'text/xml; charset=UTF-8';
/**
* {@inheritdoc}
*/
protected static $auth = 'basic_auth';
}

View File

@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\tour\Functional\Rest;
use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
use Drupal\Tests\rest\Functional\EntityResource\XmlEntityNormalizationQuirksTrait;
/**
* @group rest
* @group legacy
*/
class TourXmlCookieTest extends TourResourceTestBase {
use CookieResourceTestTrait;
use XmlEntityNormalizationQuirksTrait;
/**
* {@inheritdoc}
*/
protected static $format = 'xml';
/**
* {@inheritdoc}
*/
protected static $mimeType = 'text/xml; charset=UTF-8';
/**
* {@inheritdoc}
*/
protected static $auth = 'cookie';
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
}

View File

@@ -0,0 +1,84 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\tour\Functional;
use Drupal\Core\Url;
use Drupal\Tests\system\Functional\Cache\PageCacheTagsTestBase;
use Drupal\tour\Entity\Tour;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
/**
* Tests the Tour entity's cache tags.
*
* @group tour
* @group legacy
*/
class TourCacheTagsTest extends PageCacheTagsTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['tour', 'tour_test'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Give anonymous users permission to view nodes, so that we can verify the
// cache tags of cached versions of node pages.
Role::load(RoleInterface::ANONYMOUS_ID)->grantPermission('access tour')
->save();
}
/**
* Tests cache tags presence and invalidation of the Tour entity.
*
* Tests the following cache tags:
* - 'tour:<tour ID>'
*/
public function testRenderedTour(): void {
$url = Url::fromRoute('tour_test.1');
// Prime the page cache.
$this->verifyPageCache($url, 'MISS');
// Verify a cache hit, but also the presence of the correct cache tags.
$expected_tags = [
'config:tour.tour.tour-test',
'config:user.role.anonymous',
'http_response',
'rendered',
];
$this->verifyPageCache($url, 'HIT', $expected_tags);
// Verify that after modifying the tour, there is a cache miss.
Tour::load('tour-test')->save();
$this->verifyPageCache($url, 'MISS');
// Verify a cache hit.
$this->verifyPageCache($url, 'HIT', $expected_tags);
// Verify that after deleting the tour, there is a cache miss.
Tour::load('tour-test')->delete();
$this->verifyPageCache($url, 'MISS');
// Verify a cache hit.
$expected_tags = [
'config:user.role.anonymous',
'http_response',
'rendered',
];
$this->verifyPageCache($url, 'HIT', $expected_tags);
}
}

View File

@@ -0,0 +1,155 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\tour\Functional;
use Drupal\Tests\BrowserTestBase;
/**
* Verifies help page display of tours.
*
* @group help
* @group legacy
*/
class TourHelpPageTest extends BrowserTestBase {
/**
* Modules to enable, including some providing tours.
*
* @var array
*/
protected static $modules = ['help', 'tour', 'locale', 'language'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* User that can access tours and help.
*
* @var \Drupal\user\UserInterface
*/
protected $tourUser;
/**
* A user who can access help but not tours.
*
* @var \Drupal\user\UserInterface
*/
protected $noTourUser;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Create users. For the Tour user, include permissions for the language
// tours' parent pages, but not the translation tour's parent page. See
// self:getTourList().
$this->tourUser = $this->drupalCreateUser([
'access help pages',
'access tour',
'administer languages',
]);
$this->noTourUser = $this->drupalCreateUser([
'access help pages',
]);
}
/**
* Logs in users, tests help pages.
*/
public function testHelp(): void {
$this->drupalLogin($this->tourUser);
$this->verifyHelp();
$this->drupalLogin($this->noTourUser);
$this->verifyHelp(FALSE);
}
/**
* Verifies the logged in user has access to the help properly.
*
* @param bool $tours_ok
* (optional) TRUE (default) if the user should see tours, FALSE if not.
*/
protected function verifyHelp($tours_ok = TRUE) {
$this->drupalGet('admin/help');
// All users should be able to see the module section.
$this->assertSession()->pageTextContains('Module overviews are provided by modules');
foreach ($this->getModuleList() as $name) {
$this->assertSession()->linkExists($name);
}
// Some users should be able to see the tour section.
if ($tours_ok) {
$this->assertSession()->pageTextContains('Tours guide you through workflows');
}
else {
$this->assertSession()->pageTextNotContains('Tours guide you through workflows');
}
$titles = $this->getTourList();
// Test the titles that should be links.
foreach ($titles[0] as $title) {
if ($tours_ok) {
$this->assertSession()->linkExists($title);
}
else {
$this->assertSession()->linkNotExists($title);
// Just test the first item in the list of links that should not
// be there, because the second matches the name of a module that is
// in the Module overviews section, so the link will be there and
// this test will fail. Testing one should be sufficient to verify
// the page is working correctly.
break;
}
}
// Test the titles that should not be links.
foreach ($titles[1] as $title) {
if ($tours_ok) {
$this->assertSession()->pageTextContains($title);
$this->assertSession()->linkNotExistsExact($title);
}
else {
$this->assertSession()->pageTextNotContains($title);
// Just test the first item in the list of text that should not
// be there, because the second matches part of the name of a module
// that is in the Module overviews section, so the text will be there
// and this test will fail. Testing one should be sufficient to verify
// the page is working correctly.
break;
}
}
}
/**
* Gets a list of modules to test for hook_help() pages.
*
* @return array
* A list of module names to test.
*/
protected function getModuleList() {
return ['Help', 'Tour'];
}
/**
* Gets a list of tours to test.
*
* @return array
* A list of tour titles to test. The first array element is a list of tours
* with links, and the second is a list of tours without links. Assumes
* that the user being tested has 'administer languages' permission but
* not 'translate interface'.
*/
protected function getTourList() {
return [['Adding languages', 'Language'], ['Editing languages', 'Translation']];
}
}

View File

@@ -0,0 +1,311 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\tour\Functional;
use Drupal\Core\Url;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\tour\Entity\Tour;
// cspell:ignore pioggia spagna
/**
* Tests the functionality of tour tips.
*
* @group tour
* @group legacy
*/
class TourTest extends TourTestBasic {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'block',
'tour',
'locale',
'language',
'tour_test',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* The permissions required for a logged in user to test tour tips.
*
* @var array
* A list of permissions.
*/
protected $permissions = ['access tour', 'administer languages'];
/**
* Tour tip attributes to be tested. Keyed by the path.
*
* @var array
* An array of tip attributes, keyed by path.
*/
protected $tips = [
'tour-test-1' => [],
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->drupalPlaceBlock('local_actions_block', [
'theme' => 'claro',
'region' => 'content',
]);
}
/**
* Tests tour functionality.
*/
public function testTourFunctionality(): void {
// Navigate to tour-test-1 and verify the tour_test_1 tip is found with appropriate classes.
$this->drupalGet('tour-test-1');
// Test the TourTestBase class assertTourTips() method.
$tips = [];
$tips[] = ['data-id' => 'tour-test-1'];
$tips[] = ['data-class' => 'tour-test-5'];
$this->assertTourTips($tips);
$this->assertTourTips();
$tips = $this->getTourTips();
$href = Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString();
$elements = [];
foreach ($tips as $tip) {
if ($tip['id'] == 'tour-test-1' && $tip['module'] == 'tour_test' && $tip['type'] == 'text' && str_contains($tip['body'], $href) && str_contains($tip['body'], 'Drupal')) {
$elements[] = $tip;
}
}
$this->assertCount(1, $elements, 'Found Token replacement.');
$elements = $this->findTip([
'id' => 'tour-test-1',
'title' => 'The first tip',
]);
$this->assertCount(1, $elements, 'Found English variant of tip 1.');
$elements = $this->findTip([
'id' => 'tour-test-2',
'title' => 'The quick brown fox',
]);
$this->assertNotCount(1, $elements, 'Did not find English variant of tip 2.');
$elements = $this->findTip([
'id' => 'tour-test-1',
'title' => 'La pioggia cade in spagna',
]);
$this->assertNotCount(1, $elements, 'Did not find Italian variant of tip 1.');
// Ensure that plugins work.
$elements = [];
foreach ($tips as $tip) {
if (str_contains($tip['body'], 'http://local/image.png')) {
$elements[] = $tip;
}
}
$this->assertCount(1, $elements, 'Image plugin tip found.');
// Navigate to tour-test-2/subpath and verify the tour_test_2 tip is found.
$this->drupalGet('tour-test-2/subpath');
$elements = $this->findTip([
'id' => 'tour-test-2',
'title' => 'The quick brown fox',
]);
$this->assertCount(1, $elements, 'Found English variant of tip 2.');
$elements = $this->findTip([
'id' => 'tour-test-1',
'title' => 'The first tip',
]);
$this->assertNotCount(1, $elements, 'Did not find English variant of tip 1.');
// Enable Italian language and navigate to it/tour-test1 and verify italian
// version of tip is found.
ConfigurableLanguage::createFromLangcode('it')->save();
$this->drupalGet('it/tour-test-1');
$elements = $this->findTip([
'id' => 'tour-test-1',
'title' => 'La pioggia cade in spagna',
]);
$this->assertCount(1, $elements, 'Found Italian variant of tip 1.');
$elements = $this->findTip([
'id' => 'tour-test-2',
'title' => 'The quick brown fox',
]);
$this->assertNotCount(1, $elements, 'Did not find English variant of tip 1.');
// Programmatically create a tour for use through the remainder of the test.
$tour = Tour::create([
'id' => 'tour-entity-create-test-en',
'label' => 'Tour test english',
'langcode' => 'en',
'module' => 'system',
'routes' => [
['route_name' => 'tour_test.1'],
],
'tips' => [
'tour-test-1' => [
'id' => 'tour-code-test-1',
'plugin' => 'text',
'label' => 'The rain in spain is <strong>strong</strong>',
'body' => 'Falls mostly on the plain.',
'weight' => '100',
'selector' => '#tour-code-test-1',
],
'tour-code-test-2' => [
'id' => 'tour-code-test-2',
'plugin' => 'image',
'label' => 'The awesome image',
'url' => 'http://local/image.png',
'weight' => 1,
'selector' => '#tour-code-test-2',
],
],
]);
$tour->save();
// Ensure that a tour entity has the expected dependencies based on plugin
// providers and the module named in the configuration entity.
$dependencies = $tour->calculateDependencies()->getDependencies();
$this->assertEquals(['system', 'tour_test'], $dependencies['module']);
$this->drupalGet('tour-test-1');
// Load it back from the database and verify storage worked.
$entity_save_tip = Tour::load('tour-entity-create-test-en');
// Verify that hook_ENTITY_TYPE_load() integration worked.
$this->assertEquals('Load hooks work', $entity_save_tip->loaded);
// Verify that hook_ENTITY_TYPE_presave() integration worked.
$this->assertEquals('Tour test english alter', $entity_save_tip->label());
// Navigate to tour-test-1 and verify the new tip is found.
$this->drupalGet('tour-test-1');
$elements = $this->findTip([
'id' => 'tour-code-test-1',
'title' => 'The rain in spain is <strong>strong</strong>',
]);
$this->assertCount(1, $elements, 'Found the required tip markup for tip 4');
// Verify that the weight sorting works by ensuring the lower weight item
// (tip 4) has the 'End tour' button.
$elements = $this->findTip([
'id' => 'tour-code-test-1',
'text' => 'End tour',
]);
$this->assertCount(1, $elements, 'Found code tip was weighted last and had "End tour".');
// Test hook_tour_alter().
$this->assertSession()->responseContains('Altered by hook_tour_tips_alter');
// Navigate to tour-test-3 and verify the tour_test_1 tip is found with
// appropriate classes.
$this->drupalGet('tour-test-3/foo');
$elements = $this->findTip([
'id' => 'tour-test-1',
'module' => 'tour_test',
'type' => 'text',
'title' => 'The first tip',
]);
$this->assertCount(1, $elements, 'Found English variant of tip 1.');
// Navigate to tour-test-3 and verify the tour_test_1 tip is not found with
// appropriate classes.
$this->drupalGet('tour-test-3/bar');
$elements = $this->findTip([
'id' => 'tour-test-1',
'module' => 'tour_test',
'type' => 'text',
'title' => 'The first tip',
]);
$this->assertCount(0, $elements, 'Did not find English variant of tip 1.');
}
/**
* Tests enabling and disabling the tour tip functionality.
*/
public function testStatus(): void {
// Set tour tip status as enabled.
$tour = Tour::load('tour-test');
$tour->setStatus(TRUE);
$tour->save();
$this->drupalGet('tour-test-1');
$this->assertSession()->statusCodeEquals(200);
// Tour tips should be visible on the page.
$this->assertTourTips();
$tour->setStatus(FALSE);
$tour->save();
// Navigate and verify the tour_test_1 tip is not found with
// appropriate classes.
$this->drupalGet('tour-test-1');
$this->assertSession()->statusCodeEquals(200);
// No tips expected as tour is disabled.
$this->assertTourTips(expectEmpty: TRUE);
}
/**
* Gets tour tips from the JavaScript drupalSettings variable.
*
* @return array
* A list of tips and their data.
*/
protected function getTourTips() {
$tips = [];
$drupalSettings = $this->getDrupalSettings();
if (isset($drupalSettings['_tour_internal'])) {
foreach ($drupalSettings['_tour_internal'] as $tip) {
$tips[] = $tip;
}
}
return $tips;
}
/**
* Find specific tips by their parameters in the list of tips.
*
* @param array $params
* The list of search parameters and their values.
*
* @return array
* A list of tips which match the parameters.
*/
protected function findTip(array $params) {
$tips = $this->getTourTips();
$elements = [];
foreach ($tips as $tip) {
foreach ($params as $param => $value) {
if (isset($tip[$param]) && $tip[$param] != $value) {
continue 2;
}
}
$elements[] = $tip;
}
return $elements;
}
}

View File

@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\tour\Functional;
use Drupal\Tests\BrowserTestBase;
/**
* Base class for testing Tour functionality.
*/
abstract class TourTestBase extends BrowserTestBase {
/**
* Asserts the presence of page elements for tour tips.
*
* @code
* // Basic example.
* $this->assertTourTips();
*
* // Advanced example. The following would be used for multi-page or
* // targeting a specific subset of tips.
* $tips = [];
* $tips[] = ['data-id' => 'foo'];
* $tips[] = ['data-id' => 'bar'];
* $tips[] = ['data-class' => 'baz'];
* $this->assertTourTips($tips);
* @endcode
*
* @param array $tips
* A list of tips which provide either a "data-id" or "data-class".
* @param bool $expectEmpty
* Whether or not the field is expected to be Empty.
*/
public function assertTourTips(array $tips = [], bool $expectEmpty = FALSE) {
// 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,
];
}
}
}
$tip_count = count($tips);
if ($tip_count === 0 && $expectEmpty) {
// No tips found as expected.
return;
}
if ($tip_count > 0 && $expectEmpty) {
$this->fail("No tips were expected but $tip_count were found");
}
$this->assertGreaterThan(0, $tip_count);
// 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, sprintf('Found corresponding page element for tour tip with id #%s', $tip['data-id']));
}
elseif (!empty($tip['data-class'])) {
$elements = $this->getSession()->getPage()->findAll('css', '.' . $tip['data-class']);
$this->assertNotEmpty($elements, sprintf("Page element for tour tip with class .%s should be present", $tip['data-class']));
}
else {
// It's a modal.
$modals++;
}
$total++;
}
}
}

View File

@@ -0,0 +1,75 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\tour\Functional;
/**
* Simple tour tips test base.
*/
abstract class TourTestBasic extends TourTestBase {
/**
* Tour tip attributes to be tested. Keyed by the path.
*
* @var array
* An array of tip attributes, keyed by path.
*
* @code
* protected $tips = [
* '/foo/bar' => [
* ['data-id' => 'foo'],
* ['data-class' => 'bar'],
* ],
* ];
* @endcode
*/
protected $tips = [];
/**
* An admin user with administrative permissions for tour.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;
/**
* The permissions required for a logged in user to test tour tips.
*
* @var array
* A list of permissions.
*/
protected $permissions = ['access tour'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Make sure we are using distinct default and administrative themes for
// the duration of these tests.
$this->container->get('theme_installer')->install(['olivero', 'claro']);
$this->config('system.theme')
->set('default', 'olivero')
->set('admin', 'claro')
->save();
$this->permissions[] = 'view the administration theme';
// Create an admin user to view tour tips.
$this->adminUser = $this->drupalCreateUser($this->permissions);
$this->drupalLogin($this->adminUser);
}
/**
* A simple tip test.
*/
public function testTips(): void {
foreach ($this->tips as $path => $attributes) {
$this->drupalGet($path);
$this->assertTourTips($attributes);
}
}
}

View File

@@ -0,0 +1,123 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\tour\Functional\ViewsUi;
use Drupal\Tests\tour\Functional\TourTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
/**
* Tests the Views UI tour.
*
* @group tour
* @group legacy
*/
class ViewsUITourTest extends TourTestBase {
/**
* An admin user with administrative permissions for views.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* String translation storage object.
*
* @var \Drupal\locale\StringStorageInterface
*/
protected $localeStorage;
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = ['views_ui', 'tour', 'language', 'locale'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->adminUser = $this->drupalCreateUser([
'administer views',
'access tour',
]);
$this->drupalLogin($this->adminUser);
}
/**
* Tests views_ui tour tip availability.
*/
public function testViewsUiTourTips(): void {
// Create a basic view that shows all content, with a page and a block
// display.
$view['label'] = $this->randomMachineName(16);
$view['id'] = $this->randomMachineName(16);
$view['page[create]'] = 1;
$view['page[path]'] = $this->randomMachineName(16);
$this->drupalGet('admin/structure/views/add');
$this->submitForm($view, 'Save and edit');
$this->assertTourTips();
}
/**
* Tests views_ui tour tip availability in a different language.
*/
public function testViewsUiTourTipsTranslated(): void {
$langcode = 'nl';
// Add a default locale storage for this test.
$this->localeStorage = $this->container->get('locale.storage');
// Add Dutch language programmatically.
ConfigurableLanguage::createFromLangcode($langcode)->save();
// Handler titles that need translations.
$handler_titles = [
'Format',
'Fields',
'Sort criteria',
'Filter criteria',
];
foreach ($handler_titles as $handler_title) {
// Create source string.
$source = $this->localeStorage->createString([
'source' => $handler_title,
]);
$source->save();
$this->createTranslation($source, $langcode);
}
// Create a basic view that shows all content, with a page and a block
// display.
$view['label'] = $this->randomMachineName(16);
$view['id'] = $this->randomMachineName(16);
$view['page[create]'] = 1;
$view['page[path]'] = $this->randomMachineName(16);
// Load the page in dutch.
$this->drupalGet($langcode . '/admin/structure/views/add');
$this->submitForm($view, 'Save and edit');
$this->assertTourTips();
}
/**
* Creates single translation for source string.
*/
public function createTranslation($source, $langcode) {
return $this->localeStorage->createTranslation([
'lid' => $source->lid,
'language' => $langcode,
'translation' => $this->randomMachineName(100),
])->save();
}
}

View File

@@ -0,0 +1,134 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\tour\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
/**
* General Tour tests that require JavaScript.
*
* @group tour
* @group legacy
*/
class TourJavascriptTest extends WebDriverTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'tour',
'tour_test',
'toolbar',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$admin_user = $this->drupalCreateUser([
'access toolbar',
'access tour',
]);
$this->drupalLogin($admin_user);
}
/**
* Confirm the 'tips' and 'tour 'query arguments.
*/
public function testQueryArg(): void {
$assert_session = $this->assertSession();
$this->drupalGet('tour-test-1');
$assert_session->assertNoElementAfterWait('css', '.tip-tour-test-1');
$assert_session->pageTextContains('Where does the rain in Spain fail?');
$assert_session->pageTextNotContains('Im all these things');
$assert_session->pageTextNotContains('The first tip');
$this->drupalGet('tour-test-1', [
'query' => [
'tips' => 'tip-tour-test-6',
],
]);
$this->assertNotNull($assert_session->waitForElementVisible('css', '.tip-tour-test-6'));
$assert_session->pageTextContains('Im all these things');
$this->drupalGet('tour-test-1', [
'query' => [
'tour' => '1',
],
]);
$this->assertNotNull($assert_session->waitForElementVisible('css', '.tip-tour-test-1'));
$assert_session->pageTextContains('The first tip');
}
/**
* Tests stepping through a tour.
*/
public function testGeneralTourUse(): void {
$page = $this->getSession()->getPage();
$assert_session = $this->assertSession();
$this->drupalGet('tour-test-1');
$assert_session->assertNoElementAfterWait('css', '.tip-tour-test-1');
// Open the tour.
$page->find('css', '#toolbar-tab-tour button')->press();
// Confirm the tour can be cancelled.
$tip_to_close = $assert_session->waitForElementVisible('css', '.shepherd-enabled.tip-tour-test-1');
$this->assertNotNull($tip_to_close);
$tip_text = $tip_to_close->getText();
$this->assertStringContainsString('always the best dressed', $tip_text);
$this->assertStringContainsString('1 of 3', $tip_text);
$this->assertStringNotContainsString('End tour', $tip_text);
// Cancel the tour.
$tip_to_close->find('css', '.shepherd-cancel-icon')->press();
$assert_session->assertNoElementAfterWait('css', '.tip-tour-test-1');
$assert_session->assertNoElementAfterWait('css', '.shepherd-enabled');
// Navigate through the three steps of the tour.
$page->find('css', '#toolbar-tab-tour button')->press();
$tip1 = $assert_session->waitForElementVisible('css', '.shepherd-enabled.tip-tour-test-1');
$this->assertNotNull($tip1);
// Click the next button.
$tip1->find('css', '.button--primary:contains("Next")')->press();
// The second tour tip should appear, confirm it has the expected content.
$tip2 = $assert_session->waitForElementVisible('css', '.shepherd-enabled.tip-tour-test-3');
$assert_session->pageTextNotContains('always the best dressed');
$tip_text = $tip2->getText();
$this->assertStringContainsString('The awesome image', $tip_text);
$this->assertStringContainsString('2 of 3', $tip_text);
$this->assertStringNotContainsString('End tour', $tip_text);
// Click the next button.
$tip2->find('css', '.button--primary:contains("Next")')->press();
// The third tour tip should appear, confirm it has the expected content.
$tip3 = $assert_session->waitForElementVisible('css', '.shepherd-enabled.tip-tour-test-6');
$assert_session->pageTextNotContains('The awesome image');
$tip_text = $tip3->getText();
$this->assertStringContainsString('Im all these things', $tip_text);
$this->assertStringContainsString('3 of 3', $tip_text);
$this->assertStringNotContainsString('Next', $tip_text);
// The final tip should have a button to end the tour. Press and confirm all
// tips removed.
$tip3->find('css', '.button--primary:contains("End tour")')->press();
$assert_session->assertNoElementAfterWait('css', '.shepherd-enabled');
$assert_session->pageTextNotContains('The awesome image');
}
}

View File

@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\tour\Kernel;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests the functionality of tour plugins.
*
* @group tour
* @group legacy
*/
class TourPluginTest extends KernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = ['tour'];
/**
* Stores the tour plugin manager.
*
* @var \Drupal\tour\TipPluginManager
*/
protected $pluginManager;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installConfig(['tour']);
$this->pluginManager = $this->container->get('plugin.manager.tour.tip');
}
/**
* Tests tour plugins.
*/
public function testTourPlugins(): void {
$this->assertCount(1, $this->pluginManager->getDefinitions(), 'Only tour plugins for the enabled modules were returned.');
}
}

View File

@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\tour\Kernel;
use Drupal\tour\TourTipPluginInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
/**
* @coversDefaultClass \Drupal\tour\TourTipPluginInterface
* @group tour
* @group legacy
*/
class TourTipLegacyTest extends TestCase {
use ExpectDeprecationTrait;
public function testPluginHelperDeprecation(): void {
$this->expectDeprecation('The Drupal\tour\TourTipPluginInterface is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Implement Drupal\tour\TipPluginInterface instead. See https://www.drupal.org/node/3340701');
$plugin = $this->createMock(TourTipPluginInterface::class);
$this->assertInstanceOf(TourTipPluginInterface::class, $plugin);
}
}

View File

@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\tour\Kernel;
use Drupal\KernelTests\Core\Config\ConfigEntityValidationTestBase;
use Drupal\tour\Entity\Tour;
/**
* Tests validation of tour entities.
*
* @group tour
* @group legacy
*/
class TourValidationTest extends ConfigEntityValidationTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['tour'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->entity = Tour::create([
'id' => 'test',
'label' => 'Test',
'module' => 'system',
]);
$this->entity->save();
}
/**
* Tour IDs are atypical in that they allow dashes in the machine name.
*/
public static function providerInvalidMachineNameCharacters(): array {
$cases = parent::providerInvalidMachineNameCharacters();
// Remove the existing test case that verifies a machine name containing
// periods is invalid.
self::assertSame(['dash-separated', FALSE], $cases['INVALID: dash separated']);
unset($cases['INVALID: dash separated']);
// And instead add a test case that verifies it is allowed for tours.
$cases['VALID: dash separated'] = ['dash-separated', TRUE];
return $cases;
}
}

View File

@@ -0,0 +1,142 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\tour\Unit\Entity;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\tour\Entity\Tour
*
* @group tour
* @group legacy
*/
class TourTest extends UnitTestCase {
/**
* Tests \Drupal\tour\Entity\Tour::hasMatchingRoute().
*
* @param array $routes
* Array of routes as per the Tour::routes property.
* @param string $route_name
* The route name to match.
* @param array $route_params
* Array of route params.
* @param bool $result
* Expected result.
*
* @covers ::hasMatchingRoute
*
* @dataProvider routeProvider
*/
public function testHasMatchingRoute($routes, $route_name, $route_params, $result): void {
$tour = $this->getMockBuilder('\Drupal\tour\Entity\Tour')
->disableOriginalConstructor()
->onlyMethods(['getRoutes'])
->getMock();
$tour->expects($this->any())
->method('getRoutes')
->willReturn($routes);
$this->assertSame($result, $tour->hasMatchingRoute($route_name, $route_params));
$tour->resetKeyedRoutes();
}
/**
* Provides sample routes for testing.
*/
public static function routeProvider() {
return [
// Simple match.
[
[
['route_name' => 'some.route'],
],
'some.route',
[],
TRUE,
],
// Simple non-match.
[
[
['route_name' => 'another.route'],
],
'some.route',
[],
FALSE,
],
// Empty params.
[
[
[
'route_name' => 'some.route',
'route_params' => ['foo' => 'bar'],
],
],
'some.route',
[],
FALSE,
],
// Match on params.
[
[
[
'route_name' => 'some.route',
'route_params' => ['foo' => 'bar'],
],
],
'some.route',
['foo' => 'bar'],
TRUE,
],
// Non-matching params.
[
[
[
'route_name' => 'some.route',
'route_params' => ['foo' => 'bar'],
],
],
'some.route',
['bar' => 'foo'],
FALSE,
],
// One matching, one not.
[
[
[
'route_name' => 'some.route',
'route_params' => ['foo' => 'bar'],
],
[
'route_name' => 'some.route',
'route_params' => ['bar' => 'foo'],
],
],
'some.route',
['bar' => 'foo'],
TRUE,
],
// One matching, one not.
[
[
[
'route_name' => 'some.route',
'route_params' => ['foo' => 'bar'],
],
[
'route_name' => 'some.route',
'route_params' => ['foo' => 'baz'],
],
],
'some.route',
['foo' => 'baz'],
TRUE,
],
];
}
}

View File

@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\tour\Unit;
use Drupal\Tests\UnitTestCase;
use Drupal\tour\TipPluginBase;
/**
* @coversDefaultClass \Drupal\tour\TipPluginBase
*
* @group tour
* @group legacy
*/
class TipPluginBaseTest extends UnitTestCase {
/**
* @covers ::getLocation
*/
public function testGetLocationAssertion(): void {
$base_plugin = $this->getMockForAbstractClass(TipPluginBase::class, [], '', FALSE);
$base_plugin->set('position', 'right');
$this->assertSame('right', $base_plugin->getLocation());
$base_plugin->set('position', 'not_valid');
$this->expectException(\AssertionError::class);
$this->expectExceptionMessage('not_valid is not a valid Tour Tip position value');
$base_plugin->getLocation();
}
}

View File

@@ -0,0 +1,74 @@
<?php
namespace Drupal\tour_test\Controller;
/**
* Controller routines for tour_test routes.
*/
class TourTestController {
/**
* Outputs some content for testing tours.
*
* @param string $locale
* (optional) Dummy locale variable for testing routing parameters. Defaults
* to 'foo'.
*
* @return array
* Array of markup.
*/
public function tourTest1($locale = 'foo') {
return [
'tip-1' => [
'#type' => 'container',
'#attributes' => [
'id' => 'tour-test-1',
],
'#children' => t('Where does the rain in Spain fail?'),
],
'tip-3' => [
'#type' => 'container',
'#attributes' => [
'id' => 'tour-test-3',
],
'#children' => t('Tip created now?'),
],
'tip-4' => [
'#type' => 'container',
'#attributes' => [
'id' => 'tour-test-4',
],
'#children' => t('Tip created later?'),
],
'tip-5' => [
'#type' => 'container',
'#attributes' => [
'class' => ['tour-test-5'],
],
'#children' => t('Tip created later?'),
],
'code-tip-1' => [
'#type' => 'container',
'#attributes' => [
'id' => 'tour-code-test-1',
],
'#children' => t('Tip created now?'),
],
];
}
/**
* Outputs some content for testing tours.
*/
public function tourTest2() {
return [
'#type' => 'container',
'#attributes' => [
'id' => 'tour-test-2',
],
'#children' => t('Pangram example'),
];
}
}

View File

@@ -0,0 +1,87 @@
<?php
namespace Drupal\tour_test\Plugin\tour\tip;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Utility\Token;
use Drupal\tour\TipPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Displays an image as a tip.
*
* @Tip(
* id = "image",
* title = @Translation("Image")
* )
*/
class TipPluginImage extends TipPluginBase implements ContainerFactoryPluginInterface {
/**
* The URL which is used for the image in this Tip.
*
* @var string
* A URL used for the image.
*/
protected $url;
/**
* The alt text which is used for the image in this Tip.
*
* @var string
* An alt text used for the image.
*/
protected $alt;
/**
* Token service.
*
* @var \Drupal\Core\Utility\Token
*/
protected $token;
/**
* Constructs a \Drupal\tour\Plugin\tour\tip\TipPluginText object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Utility\Token $token
* The token service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, Token $token) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->token = $token;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container->get('token'));
}
/**
* {@inheritdoc}
*/
public function getBody(): array {
$image = [
'#theme' => 'image',
'#uri' => $this->get('url'),
'#alt' => $this->get('alt'),
];
return [
'#type' => 'html_tag',
'#tag' => 'p',
'#attributes' => [
'class' => ['tour-tip-image'],
],
'image' => $image,
];
}
}

View File

@@ -0,0 +1,84 @@
<?php
namespace Drupal\tour_test\Plugin\tour\tip;
use Drupal\Component\Utility\Html;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Utility\Token;
use Drupal\tour\TipPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Displays an image as a tip.
*
* @Tip(
* id = "image_legacy",
* title = @Translation("Image Legacy")
* )
*/
class TipPluginImageLegacy extends TipPluginBase implements ContainerFactoryPluginInterface {
/**
* The URL which is used for the image in this Tip.
*
* @var string
* A URL used for the image.
*/
protected $url;
/**
* The alt text which is used for the image in this Tip.
*
* @var string
* An alt text used for the image.
*/
protected $alt;
/**
* Token service.
*
* @var \Drupal\Core\Utility\Token
*/
protected $token;
/**
* Constructs a TipPluginImageLegacy object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Utility\Token $token
* The token service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, Token $token) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->token = $token;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container->get('token'));
}
/**
* {@inheritdoc}
*/
public function getConfigurationOrNot() {
$image = [
'#theme' => 'image',
'#uri' => $this->get('url'),
'#alt' => $this->get('alt'),
];
return [
'title' => Html::escape($this->get('label')),
'body' => $this->token->replace(\Drupal::service('renderer')->renderInIsolation($image)),
];
}
}

View File

@@ -0,0 +1,96 @@
<?php
namespace Drupal\tour_test\Plugin\tour\tip;
use Drupal\Component\Utility\Html;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Utility\Token;
use Drupal\tour\TipPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Displays some text as a tip.
*
* @Tip(
* id = "text_legacy",
* title = @Translation("Text Legacy")
* )
*/
class TipPluginTextLegacy extends TipPluginBase implements ContainerFactoryPluginInterface {
/**
* The body text which is used for render of this Text Tip.
*
* @var string
*/
protected $body;
/**
* Token service.
*
* @var \Drupal\Core\Utility\Token
*/
protected $token;
/**
* The forced position of where the tip will be located.
*
* @var string
*/
protected $location;
/**
* Unique aria-id.
*
* @var string
*/
protected $ariaId;
/**
* Constructs a TipPluginTextLegacy object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Utility\Token $token
* The token service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, Token $token) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->token = $token;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container->get('token'));
}
/**
* Returns an ID that is guaranteed uniqueness.
*
* @return string
* A unique id to be used to generate aria attributes.
*/
public function getAriaId() {
if (!$this->ariaId) {
$this->ariaId = Html::getUniqueId($this->get('id'));
}
return $this->ariaId;
}
/**
* Returns body of the text tip.
*
* @return array
* The tip body.
*/
public function getBody(): array {
return [$this->get('body')];
}
}

View File

@@ -0,0 +1,12 @@
name: Tour module tests
type: module
description: Tests module for tour module.
package: Testing
# version: VERSION
dependencies:
- drupal:tour
# Information added by Drupal.org packaging script on 2024-07-04
version: '10.3.1'
project: 'drupal'
datestamp: 1720094222

View File

@@ -0,0 +1,5 @@
tour_test_action:
route_name: tour_test.1_action
title: 'Tour test action'
appears_on:
- tour_test.1

View File

@@ -0,0 +1,37 @@
<?php
/**
* @file
* Provides tests for tour module.
*/
use Drupal\Core\Entity\EntityInterface;
/**
* Implements hook_ENTITY_TYPE_load() for tour.
*/
function tour_test_tour_load($entities) {
if (isset($entities['tour-entity-create-test-en'])) {
$entities['tour-entity-create-test-en']->loaded = 'Load hooks work';
}
}
/**
* Implements hook_ENTITY_TYPE_presave() for tour.
*/
function tour_test_tour_presave($entity) {
if ($entity->id() == 'tour-entity-create-test-en') {
$entity->set('label', $entity->label() . ' alter');
}
}
/**
* Implements hook_tour_tips_alter().
*/
function tour_test_tour_tips_alter(array &$tour_tips, EntityInterface $entity) {
foreach ($tour_tips as $tour_tip) {
if ($tour_tip->get('id') == 'tour-code-test-1') {
$tour_tip->set('body', 'Altered by hook_tour_tips_alter');
}
}
}

View File

@@ -0,0 +1,30 @@
tour_test.1:
path: '/tour-test-1'
defaults:
_controller: '\Drupal\tour_test\Controller\TourTestController::tourTest1'
options:
_admin_route: TRUE
requirements:
_access: 'TRUE'
tour_test.1_action:
path: '/tour-test-1/action'
defaults:
_controller: '\Drupal\tour_test\Controller\TourTestController::tourTest1'
requirements:
_access: 'TRUE'
tour_test.2:
path: '/tour-test-2/subpath'
defaults:
_controller: '\Drupal\tour_test\Controller\TourTestController::tourTest2'
requirements:
_access: 'TRUE'
tour_test.3:
path: '/tour-test-3/{locale}'
defaults:
locale: 'foo'
_controller: '\Drupal\tour_test\Controller\TourTestController::tourTest1'
requirements:
_access: 'TRUE'