diff --git a/help_topics.routing.yml b/help_topics.routing.yml index aa0df38..6183a62 100644 --- a/help_topics.routing.yml +++ b/help_topics.routing.yml @@ -6,21 +6,21 @@ help_topics.help_topic: _permission: 'view help topics' help_topics.topic_autocomplete: - path: '/config-help/autocomplete-topic' + path: '/help-topics/autocomplete-topic' defaults: _controller: '\Drupal\help_topics\Controller\AutocompleteController::topicAutocomplete' requirements: _permission: 'administer help topics' help_topics.module_autocomplete: - path: '/config-help/autocomplete-module' + path: '/help-topics/autocomplete-module' defaults: _controller: '\Drupal\help_topics\Controller\AutocompleteController::moduleAutocomplete' requirements: _permission: 'administer help topics' help_topics.theme_autocomplete: - path: '/config-help/autocomplete-theme' + path: '/help-topics/autocomplete-theme' defaults: _controller: '\Drupal\help_topics\Controller\AutocompleteController::themeAutocomplete' requirements: diff --git a/help_topics.tokens.inc b/help_topics.tokens.inc index cf7ce60..020d25e 100644 --- a/help_topics.tokens.inc +++ b/help_topics.tokens.inc @@ -68,7 +68,7 @@ function help_topics_tokens($type, $tokens, array $data, array $options, Bubblea $topics = $token_service->findWithPrefix($tokens, 'url'); foreach ($topics as $name => $original) { /** @var \Drupal\help_topics\Plugin\HelpTopic\HelpTopicPluginInterface $topic */ - if ($topic = $plugin_manager->createInstance($name, [])) { + if ($plugin_manager->hasDefinition($name) && $topic = $plugin_manager->createInstance($name, [])) { $url = $topic->toUrl($url_options)->toString(TRUE); $replacements[$original] = $url->getGeneratedUrl(); $bubbleable_metadata diff --git a/src/Controller/AutocompleteController.php b/src/Controller/AutocompleteController.php index e7c4de8..3522936 100644 --- a/src/Controller/AutocompleteController.php +++ b/src/Controller/AutocompleteController.php @@ -87,14 +87,20 @@ class AutocompleteController extends ControllerBase { */ public function topicAutocomplete(Request $request) { $matches = []; + $count = 0; if ($input = $this->getUserInput($request)) { $topics = $this->pluginManager->findMatches($input); if (!empty($topics)) { foreach ($topics as $title => $id) { $matches[] = $this->getMatch($id, $title); + $count++; + if ($count >= 10) { + break; + } } } } + return new JsonResponse($matches); } @@ -229,7 +235,7 @@ class AutocompleteController extends ControllerBase { $installed = $this->themeHandler->listInfo(); $themes = []; foreach ($installed as $name => $extension) { - $themes[$name] = $extension->getName(); + $themes[$name] = $extension->info['name']; } // Themes are sorted by file system discovery so sort them by name. asort($themes); diff --git a/src/Controller/HelpTopicPluginController.php b/src/Controller/HelpTopicPluginController.php index 2fed502..a66765d 100644 --- a/src/Controller/HelpTopicPluginController.php +++ b/src/Controller/HelpTopicPluginController.php @@ -6,6 +6,7 @@ use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Url; use Drupal\help_topics\Plugin\HelpTopic\HelpTopicPluginManagerInterface; use Drupal\Core\Render\BubbleableMetadata; +use Drupal\Core\Render\RendererInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Utility\Token; @@ -22,6 +23,13 @@ class HelpTopicPluginController extends ControllerBase { protected $token; /** + * The renderer service. + * + * @var \Drupal\Core\Render\RendererInterface + */ + protected $renderer; + + /** * The Help Topic plugin manager. * * @var \Drupal\help_topics\Plugin\HelpTopic\HelpTopicPluginManagerInterface @@ -35,10 +43,13 @@ class HelpTopicPluginController extends ControllerBase { * The help topic plugin manager service. * @param \Drupal\Core\Utility\Token $token * The token service. + * @param \Drupal\Core\Render\RendererInterface $renderer + * The renderer service. */ - public function __construct(HelpTopicPluginManagerInterface $help_topic_plugin_manager, Token $token) { + public function __construct(HelpTopicPluginManagerInterface $help_topic_plugin_manager, Token $token, RendererInterface $renderer) { $this->helpTopicPluginManager = $help_topic_plugin_manager; $this->token = $token; + $this->renderer = $renderer; } /** @@ -47,7 +58,8 @@ class HelpTopicPluginController extends ControllerBase { public static function create (ContainerInterface $container) { return new static( $container->get('plugin.manager.help_topic'), - $container->get('token') + $container->get('token'), + $container->get('renderer') ); } @@ -61,6 +73,8 @@ class HelpTopicPluginController extends ControllerBase { * A render array with the contents of a help topic page. */ public function viewHelpTopic($id) { + $build = []; + /** @var \Drupal\help_topics\Plugin\HelpTopic\HelpTopicPluginInterface $help_topic */ $help_topic = $this->helpTopicPluginManager->createInstance($id); @@ -92,13 +106,13 @@ class HelpTopicPluginController extends ControllerBase { ]; $bubbleable_metadata->applyTo($build['#body']); - // Figure out which topics to list as related, including topics this - // plugin lists as related, plus topics that have said "Add me to this - // topic's related list" using the list on field. - $related = $help_topic->getRelated(); + $this->renderer->addCacheableDependency($build, $help_topic); + // Build the related topics section, starting with the list this topic + // says are related. $links = []; + $related = $help_topic->getRelated(); foreach ($related as $other_id) { if ($other_id !== $help_topic->getPluginId()) { /** @var \Drupal\help_topics\Plugin\HelpTopic\HelpTopicPluginInterface $topic */ @@ -108,12 +122,29 @@ class HelpTopicPluginController extends ControllerBase { 'title' => $topic->getLabel(), 'url' => Url::fromRoute('help_topics.help_topic', ['id' => $other_id]), ]; + $this->renderer->addCacheableDependency($build, $topic); } } } + // Add in any plugins that have said "List me on this topic". + $liston = $this->helpTopicPluginManager->getAllListOn($id); + foreach ($liston as $topic) { + $other_id = $topic->getPluginId(); + $links[$other_id] = [ + 'title' => $topic->getLabel(), + 'url' => Url::fromRoute('help_topics.help_topic', ['id' => $other_id]), + ]; + $this->renderer->addCacheableDependency($build, $topic); + } + if (count($links)) { - ksort($links); + uasort($links, function($a, $b) { + if ($a['title'] == $b['title']) { + return 0; + } + return ($a['title'] < $b['title']) ? -1 : 1; + }); $build['#related'] = [ '#theme' => 'links', '#heading' => [ diff --git a/src/Entity/HelpTopic.php b/src/Entity/HelpTopic.php index 68fae3b..3ce2b05 100644 --- a/src/Entity/HelpTopic.php +++ b/src/Entity/HelpTopic.php @@ -283,7 +283,7 @@ class HelpTopic extends ConfigEntityBase implements HelpTopicInterface { */ protected function getPluginManager() { if (!isset($this->pluginManager)) { - $this->pluginManager = \Drupal::get('plugin.manager.help_topic'); + $this->pluginManager = \Drupal::service('plugin.manager.help_topic'); } return $this->pluginManager; } @@ -369,7 +369,7 @@ class HelpTopic extends ConfigEntityBase implements HelpTopicInterface { * {@inheritdoc} */ public function getPluginId() { - return 'entity.' . $this->id; + return $this->id; } /** diff --git a/src/Plugin/HelpSection/HelpTopicSection.php b/src/Plugin/HelpSection/HelpTopicSection.php index 8bf6ba1..f4dab75 100644 --- a/src/Plugin/HelpSection/HelpTopicSection.php +++ b/src/Plugin/HelpSection/HelpTopicSection.php @@ -27,6 +27,20 @@ class HelpTopicSection extends HelpSectionPluginBase implements ContainerFactory protected $pluginManager; /** + * The render array for the list of topics. + * + * @var array + */ + protected $topicList; + + /** + * The cache tags for the list of topics. + * + * @var string[] + */ + protected $cacheTagList; + + /** * Constructs a HelpTopicSection object. * * @param array $configuration @@ -59,7 +73,11 @@ class HelpTopicSection extends HelpSectionPluginBase implements ContainerFactory * {@inheritdoc} */ public function getCacheTags() { - return $this->pluginManager->getCacheTags(); + if (!isset($this->topicList)) { + $this->calculateTopics(); + } + + return $this->cacheTagList; } /** @@ -75,15 +93,30 @@ class HelpTopicSection extends HelpSectionPluginBase implements ContainerFactory * {@inheritdoc} */ public function listTopics() { + if (!isset($this->topicList)) { + $this->calculateTopics(); + } + + return $this->topicList; + } + + /** + * Calculates the topic list and cache tags. + */ + protected function calculateTopics() { /** @var \Drupal\help_topics\Plugin\HelpTopic\HelpTopicPluginInterface[] $plugins */ $plugins = $this->pluginManager->getTopLevelTopics(); - $topics = []; + $this->topicList = []; + $cache_tags = []; foreach ($plugins as $plugin) { - $topics[$plugin->getPluginId()] = $plugin->toLink(); + $this->topicList[$plugin->getPluginId()] = $plugin->toLink(); + foreach ($plugin->getCacheTagsForList() as $tag) { + $cache_tags[] = $tag; + } } - return $topics; + $this->cacheTagList = array_unique($cache_tags); } } diff --git a/src/Plugin/HelpTopic/DerivedHelpTopicPlugin.php b/src/Plugin/HelpTopic/DerivedHelpTopicPlugin.php index 2fe0288..cf7cbb8 100644 --- a/src/Plugin/HelpTopic/DerivedHelpTopicPlugin.php +++ b/src/Plugin/HelpTopic/DerivedHelpTopicPlugin.php @@ -4,6 +4,7 @@ namespace Drupal\help_topics\Plugin\HelpTopic; use Drupal\Component\Plugin\Exception\PluginException; use Drupal\Core\Entity\EntityStorageInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -27,6 +28,13 @@ class DerivedHelpTopicPlugin extends HelpTopicPluginBase implements ContainerFac protected $helpTopicStorage; /** + * The entity type manager. + * + * @var \Drupal\Core\Entity\EntityTypeManagerInterface + */ + protected $entityTypeManager; + + /** * Constructs a DerivedHelpTopicPlugin. * * @param string $plugin_id @@ -36,9 +44,10 @@ class DerivedHelpTopicPlugin extends HelpTopicPluginBase implements ContainerFac * @param \Drupal\Core\Entity\EntityStorageInterface $help_topic_storage * The help topic storage. */ - public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityStorageInterface $help_topic_storage) { + public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityStorageInterface $help_topic_storage, EntityTypeManagerInterface $entity_manager) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->helpTopicStorage = $help_topic_storage; + $this->entityTypeManager = $entity_manager; if (!isset($plugin_definition['metadata']['entity_id'])) { throw new PluginException($this->t('Missing entity ID in plugin definition')); @@ -55,8 +64,13 @@ class DerivedHelpTopicPlugin extends HelpTopicPluginBase implements ContainerFac * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { - return new static($configuration, $plugin_id, $plugin_definition, $container - ->get('entity.manager')->getStorage('help_topic')); + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('entity.manager')->getStorage('help_topic'), + $container->get('entity_type.manager') + ); } /** @@ -177,4 +191,16 @@ class DerivedHelpTopicPlugin extends HelpTopicPluginBase implements ContainerFac return $this->entity->getCacheTags(); } + /** + * {@inheritdoc} + */ + public function getCacheTagsForList() { + $tags = parent::getCacheTagsForList(); + foreach($this->entityTypeManager->getDefinition('help_topic')->getListCacheTags() as $tag) { + $tags[] = $tag; + } + + return $tags; + } + } diff --git a/src/Plugin/HelpTopic/HelpTopicPluginBase.php b/src/Plugin/HelpTopic/HelpTopicPluginBase.php index 76de049..72a231f 100644 --- a/src/Plugin/HelpTopic/HelpTopicPluginBase.php +++ b/src/Plugin/HelpTopic/HelpTopicPluginBase.php @@ -135,6 +135,20 @@ abstract class HelpTopicPluginBase extends PluginBase implements HelpTopicPlugin /** * {@inheritdoc} */ + public function getCacheTagsForList() { + $tags = []; + // By default, there are no list cache tags, but we do want the cache + // tags for this plugin. + if ($this->pluginDefinition['cache_tag']) { + $tags[] = $this->pluginDefinition['cache_tag']; + } + + return $tags; + } + + /** + * {@inheritdoc} + */ public function toUrl(array $options = []) { return Url::fromRoute('help_topics.help_topic', ['id' => $this->getPluginId()], $options); } diff --git a/src/Plugin/HelpTopic/HelpTopicPluginInterface.php b/src/Plugin/HelpTopic/HelpTopicPluginInterface.php index dedb0ae..4dd975d 100644 --- a/src/Plugin/HelpTopic/HelpTopicPluginInterface.php +++ b/src/Plugin/HelpTopic/HelpTopicPluginInterface.php @@ -89,4 +89,12 @@ interface HelpTopicPluginInterface extends PluginInspectionInterface, Derivative */ public function toLink($text = NULL, array $options = []); + /** + * Returns the cache tags appropriate for listings of plugins of this type. + * + * @return string[] + * Array of cache tags. + */ + public function getCacheTagsForList(); + } diff --git a/src/Plugin/HelpTopic/HelpTopicPluginManager.php b/src/Plugin/HelpTopic/HelpTopicPluginManager.php index b2568f2..13d625a 100644 --- a/src/Plugin/HelpTopic/HelpTopicPluginManager.php +++ b/src/Plugin/HelpTopic/HelpTopicPluginManager.php @@ -203,10 +203,9 @@ class HelpTopicPluginManager extends DefaultPluginManager implements HelpTopicPl */ public function findMatches($text) { $topics = []; - foreach ($this->getDefinitions() as $definition) { - if (strpos($definition['id'], $text) !== FALSE || - strpos($definition['label'], $text) !== FALSE) { + if ((stripos($definition['id'], $text) !== FALSE) || + (stripos($definition['label'], $text) !== FALSE)) { $topics[$definition['label']] = $definition['id']; } } diff --git a/src/Plugin/HelpTopic/HelpTopicPluginManagerInterface.php b/src/Plugin/HelpTopic/HelpTopicPluginManagerInterface.php index d980c3c..eb060d5 100644 --- a/src/Plugin/HelpTopic/HelpTopicPluginManagerInterface.php +++ b/src/Plugin/HelpTopic/HelpTopicPluginManagerInterface.php @@ -38,8 +38,8 @@ interface HelpTopicPluginManagerInterface extends PluginManagerInterface { * * @return string[] * Array of matching topic IDs, keyed by topic title, where $text matches - * a substring of the title (in its base language) or ID. Returned in - * alphabetic order by title. + * a substring of the title (in its base language) or ID, with case- + * insensitive matching. Returned in alphabetic order by title. */ public function findMatches($text); diff --git a/tests/modules/help_topics_test/config/optional/help_topics.topic.help_test.yml b/tests/modules/help_topics_test/config/optional/help_topics.topic.help_test.yml index fb3ae8a..6242644 100644 --- a/tests/modules/help_topics_test/config/optional/help_topics.topic.help_test.yml +++ b/tests/modules/help_topics_test/config/optional/help_topics.topic.help_test.yml @@ -13,7 +13,7 @@ top_level: true locked: false related: - help_system_building - - help_test_linked + - 'entity:help_test_linked' list_on: { } body: - diff --git a/tests/modules/help_topics_test/config/optional/help_topics.topic.help_test_additional.yml b/tests/modules/help_topics_test/config/optional/help_topics.topic.help_test_additional.yml index 0d8806e..6c464c9 100644 --- a/tests/modules/help_topics_test/config/optional/help_topics.topic.help_test_additional.yml +++ b/tests/modules/help_topics_test/config/optional/help_topics.topic.help_test_additional.yml @@ -13,7 +13,7 @@ top_level: false locked: false related: { } list_on: - - help_test + - 'entity:help_test' body: - text: 'This topic should get listed automatically on the Help test topic.' diff --git a/tests/src/Functional/HelpTopicAdminTest.php b/tests/src/Functional/HelpTopicAdminTest.php index 6d5a9bf..89a196f 100644 --- a/tests/src/Functional/HelpTopicAdminTest.php +++ b/tests/src/Functional/HelpTopicAdminTest.php @@ -7,7 +7,7 @@ use Drupal\language\Entity\ConfigurableLanguage; use Drupal\Tests\BrowserTestBase; /** - * Tests the administrative interface for help topics. + * Tests the administrative interface for help topic entities. * * @group help */ @@ -251,7 +251,7 @@ class HelpTopicAdminTest extends BrowserTestBase { $session->statusCodeEquals($response); // Verify autocomplete page. - $this->drupalGet('config-help/autocomplete-topic'); + $this->drupalGet('help-topics/autocomplete-topic'); $session = $this->assertSession(); $session->statusCodeEquals($response); @@ -282,10 +282,10 @@ class HelpTopicAdminTest extends BrowserTestBase { $session->pageTextContains($title); $session->pageTextContains($body); - $this->clickLink('Edit'); + // Edit the topic. $new_title = 'Foo longer topic'; $new_id = 'foo2'; - $this->drupalPostForm(NULL, [ + $this->drupalPostForm('admin/config/development/help/manage/' . $id, [ 'label' => $new_title, 'id' => $new_id, ], 'Save'); @@ -299,18 +299,18 @@ class HelpTopicAdminTest extends BrowserTestBase { // Test a few autocomplete values. $autocompletes = [ // ID of a topic we just added. - $new_id => [$new_title, $new_id], + $new_id => [$new_title, 'entity:' . $new_id], // Title word of a topic we just edited. - 'longer' => [$new_title, $new_id], + 'longer' => [$new_title, 'entity:' . $new_id], 'help_test' => [ 'Additional topic', - 'help_test_additional', + 'entity:' . 'help_test_additional', 'Help Test module', ], ]; foreach ($autocompletes as $query => $texts) { - $this->drupalGet('config-help/autocomplete-topic', ['query' => ['q' => $query]]); + $this->drupalGet('help-topics/autocomplete-topic', ['query' => ['q' => $query]]); $session = $this->assertSession(); $session->statusCodeEquals($response); foreach ($texts as $text) { @@ -405,8 +405,8 @@ class HelpTopicAdminTest extends BrowserTestBase { 'translate configuration', ]); $this->drupalLogin($user); - foreach (['View', 'Edit', 'Delete', 'Translate help topic'] as $label) { - $this->drupalGet('admin/help-topic/help_test'); + foreach (['Edit', 'Delete', 'Translate help topic'] as $label) { + $this->drupalGet('admin/config/development/help/manage/help_test'); $this->clickLink($label); $session = $this->assertSession(); $session->statusCodeEquals(200); @@ -417,7 +417,7 @@ class HelpTopicAdminTest extends BrowserTestBase { * Tests autocomplete for topics, modules & themes. */ public function testAutocomplete() { - $path = 'config-help/autocomplete-'; + $path = 'help-topics/autocomplete-'; $cases = [ 'topic' => [ '|' => '[]', diff --git a/tests/src/Functional/HelpTopicTest.php b/tests/src/Functional/HelpTopicTest.php index 33d8db4..09c7e31 100644 --- a/tests/src/Functional/HelpTopicTest.php +++ b/tests/src/Functional/HelpTopicTest.php @@ -85,8 +85,8 @@ class HelpTopicTest extends BrowserTestBase { // Verify that help topics text appears on admin/help. $this->drupalGet('admin/help'); $session = $this->assertSession(); - $session->responseContains('

Configured topics

'); - $session->pageTextContains('Configured topics can be provided by modules, themes'); + $session->responseContains('

Topics

'); + $session->pageTextContains('Topics can be provided by modules, themes'); // Verify the cache tag for the list of topics is present, as well as // the cache context for user permissions. @@ -95,7 +95,7 @@ class HelpTopicTest extends BrowserTestBase { // Verify links for for configurable topics, and order. $page_text = $this->getTextContent(); - $start = strpos($page_text, 'Configured topics'); + $start = strpos($page_text, 'Topics can be provided'); $pos = $start; foreach ($this->getTopicList() as $info) { $name = $info['name']; @@ -130,7 +130,7 @@ class HelpTopicTest extends BrowserTestBase { // Verify access to configurable help topic pages. foreach ($this->getTopicList() as $topic => $info) { // View help topic page. - $this->drupalGet('admin/help-topic/' . $topic); + $this->drupalGet('admin/help/topic/' . $topic); $session = $this->assertSession(); $session->statusCodeEquals($response); if ($response == 200) { @@ -158,7 +158,7 @@ class HelpTopicTest extends BrowserTestBase { */ protected function verifyHelpLinks() { // Verify links on the test top-level page. - $page = 'admin/help-topic/help_test'; + $page = 'admin/help/topic/entity:help_test'; $links = [ 'link to the Help module topic' => 'Building a help system', 'link:to the help admin page' => 'Add new help topic', @@ -192,11 +192,10 @@ class HelpTopicTest extends BrowserTestBase { */ protected function getTopicList() { return [ - 'help_test' => [ + 'entity:help_test' => [ 'name' => 'ABC Help Test module', 'cache_tags' => [ 'config:help_topics.topic.help_test', - 'config:help_topics.topic.help_system_building', 'config:help_topics.topic.help_test_linked', 'config:filter.format.help', ], @@ -204,7 +203,6 @@ class HelpTopicTest extends BrowserTestBase { 'help_system_building' => [ 'name' => 'Building a help system', 'cache_tags' => [ - 'config:help_topics.topic.help_system_building', 'config:filter.format.help', ], ], diff --git a/tests/src/Functional/HelpTopicTranslateTest.php b/tests/src/Functional/HelpTopicTranslateTest.php index 291a156..5323123 100644 --- a/tests/src/Functional/HelpTopicTranslateTest.php +++ b/tests/src/Functional/HelpTopicTranslateTest.php @@ -79,7 +79,7 @@ class HelpTopicTranslateTest extends BrowserTestBase { ], 'Save translation'); // Visit the page in English and verify. - $this->drupalGet('admin/help-topic/help_test'); + $this->drupalGet('admin/help/topic/entity:help_test'); $session = $this->assertSession(); $session->pageTextContains('ABC Help Test module'); $session->pageTextContains('This is a test.'); @@ -87,7 +87,7 @@ class HelpTopicTranslateTest extends BrowserTestBase { $session->pageTextNotContains($es_body); // Visit the page in Spanish and verify. - $this->drupalGet('es/admin/help-topic/help_test'); + $this->drupalGet('es/admin/help/topic/entity:help_test'); $session = $this->assertSession(); $session->pageTextNotContains('ABC Help Test module'); $session->pageTextNotContains('This is a test.'); @@ -117,7 +117,7 @@ class HelpTopicTranslateTest extends BrowserTestBase { ], 'Save translation'); // Visit the page in English and verify. - $this->drupalGet('admin/help-topic/foo'); + $this->drupalGet('admin/help/topic/entity:foo'); $session = $this->assertSession(); $session->pageTextContains($second_en_title); $session->pageTextContains($second_en_body); @@ -125,7 +125,7 @@ class HelpTopicTranslateTest extends BrowserTestBase { $session->pageTextNotContains($second_es_body); // Visit the page in Spanish and verify. - $this->drupalGet('es/admin/help-topic/foo'); + $this->drupalGet('es/admin/help/topic/entity:foo'); $session = $this->assertSession(); $session->pageTextNotContains($second_en_title); $session->pageTextNotContains($second_en_body); diff --git a/tests/src/Kernel/HelpTopicTokensTest.php b/tests/src/Kernel/HelpTopicTokensTest.php index 7a723c2..10859de 100644 --- a/tests/src/Kernel/HelpTopicTokensTest.php +++ b/tests/src/Kernel/HelpTopicTokensTest.php @@ -22,6 +22,7 @@ class HelpTopicTokensTest extends KernelTestBase { public static $modules = [ 'system', 'help_topics', + 'help_topics_test', 'user', 'help', 'filter', @@ -37,7 +38,7 @@ class HelpTopicTokensTest extends KernelTestBase { // Set up as multi-lingual with English and Spanish, so that we can // test for cache metadata from URL generation. - $this->installConfig(['help_topics', 'language']); + $this->installConfig(['help_topics', 'help_topics_test', 'language']); $this->installEntitySchema('configurable_language'); ConfigurableLanguage::create(['id' => 'es'])->save(); $this @@ -55,17 +56,15 @@ class HelpTopicTokensTest extends KernelTestBase { * Tests that help topic tokens work. */ public function testHelpTopicTokens() { - // Verify a URL token for a help topic. + // Verify a URL token for a help topic that is a plugin. $bubbleable_metadata = new BubbleableMetadata(); $text = 'This should Link to help topic'; $replaced = \Drupal::token()->replace($text, [], [], $bubbleable_metadata); $this->assertTrue(strpos($replaced, 'getCacheTags(); + // Check for cache metadata. It should have a cache context for language + // negotiation. As this is a plugin, there is no cache tag for the topic. $contexts = $bubbleable_metadata->getCacheContexts(); - $this->assertTrue(in_array('config:help_topics.topic.help_system_building', $tags), 'Cache tag for the linked topic was added'); $this->assertTrue(in_array('languages:language_url', $contexts, 'Language negotiation cache context was added')); // Verify correct URL if we tell the Token system to use Spanish. @@ -79,6 +78,18 @@ class HelpTopicTokensTest extends KernelTestBase { $replaced = \Drupal::token()->replace($text); $this->assertTrue(strpos($replaced, '[help_topic:url:nonexistent]') !== FALSE, 'Nonexistent help topic did not get replaced'); + // Verify a URL for a help topic that is an entity. + $bubbleable_metadata = new BubbleableMetadata(); + $text = 'This should Link to entity help topic'; + $replaced = \Drupal::token()->replace($text, [], [], $bubbleable_metadata); + $this->assertTrue(strpos($replaced, 'getCacheTags(); + $contexts = $bubbleable_metadata->getCacheContexts(); + $this->assertTrue(in_array('config:help_topics.topic.help_test', $tags), 'Cache tag for the linked topic was added'); + $this->assertTrue(in_array('languages:language_url', $contexts, 'Language negotiation cache context was added')); } /**