Loading tests/modules/trailless_menu_render_count_test/trailless_menu_render_count_test.info.yml 0 → 100644 +5 −0 Original line number Diff line number Diff line name: 'Trailless menu render count test' type: module description: 'Support module for trailless_menu for the render counter testing.' package: Testing hidden: true tests/modules/trailless_menu_render_count_test/trailless_menu_render_count_test.module 0 → 100644 +26 −0 Original line number Diff line number Diff line <?php /** * @file * A module for testing menu block renders count. */ use Drupal\Core\Block\BlockPluginInterface; /** * Implements hook_block_view_alter(). */ function trailless_menu_render_count_test_block_view_alter(array &$build, BlockPluginInterface $block) { /* * Checking that we increase render_counter value for our main navigation * block only. */ if ( $block->getPluginId() === 'system_menu_block:main' && $block->getConfiguration()['label'] === 'trailless_menu main navigation' ) { $state = \Drupal::state(); $counter = $state->get('render_counter') ?? 0; $state->set('render_counter', ++$counter); } } tests/src/Functional/TraillessMenuTest.php 0 → 100644 +157 −0 Original line number Diff line number Diff line <?php namespace Drupal\Tests\trailless_menu\Functional; use Drupal\menu_link_content\Entity\MenuLinkContent; use Drupal\Tests\BrowserTestBase; /** * Tests that trailless menu doesn't expand and tests that render happens only * once for trailless menu. * * @group trailless_menu */ class TraillessMenuTest extends BrowserTestBase { /** * An editor user. * * @var \Drupal\user\UserInterface */ protected $editor; /** * An authenticated user to test block caching. * * @var object */ protected $normalUser; /** * Modules to enable. * * @var array */ protected static $modules = [ 'system', 'block', 'menu_link_content', 'node', 'trailless_menu', 'trailless_menu_render_count_test', ]; /** * {@inheritdoc} */ protected $defaultTheme = 'stark'; protected function setUp(): void { parent::setUp(); $this->drupalCreateContentType([ 'type' => 'page', 'name' => 'Basic page', 'display_submitted' => FALSE, ]); $this->block = $this->drupalPlaceBlock( 'system_menu_block:main', [ 'label' => 'trailless_menu main navigation' ] ); $this->editor = $this->drupalCreateUser([ 'access administration pages', 'administer blocks', 'administer menu', 'create page content', 'administer trailless menu', ]); $this->drupalLogin($this->editor); // Create additional users to test caching modes. $this->normalUser = $this->drupalCreateUser(); // Create nodes. $first = $this->drupalCreateNode(); $second = $this->drupalCreateNode(); // Create menu links for nodes. $this->first_item = MenuLinkContent::create([ 'link' => [['uri' => 'entity:node/' . $first->id()]], 'title' => 'first', 'menu_name' => 'main', ]); $this->first_item->save(); $this->second_item = MenuLinkContent::create([ 'link' => [['uri' => 'entity:node/' . $second->id()]], 'title' => 'second', 'menu_name' => 'main', 'parent' => 'menu_link_content:' . $this->first_item->uuid(), ]); $this->second_item->save(); // Goto trailless_menu setting page and tick main menu and save form. $edit = [ 'trailless_menus[main]' => TRUE, ]; $this->drupalGet('/admin/config/user-interface/trailless-menu'); $this->submitForm($edit, 'Save configuration'); } /** * Tests that trailless menu doesn't expand. */ public function testTraillessMenuLink() { $this->drupalLogin($this->normalUser); // Goto a parent menu link and verify that parent link exists on the page. $this->drupalGet($this->first_item->getUrlObject()); $this->assertSession() ->linkByHrefExists($this->first_item->getUrlObject()->toString()); // Goto its child and verify that child link doesn't exists on the page. $this->drupalGet($this->second_item->getUrlObject()); $this->assertSession() ->linkByHrefNotExists($this->second_item->getUrlObject()->toString()); } /** * Tests that render happens only once for trailless menu. */ public function testRenderHappensOnceForTraillessMenu() { $this->drupalLogin($this->normalUser); $state = \Drupal::state(); // Resetting state cache so that we don't get cache state value. $state->resetCache(); // Getting render counter, which is set from trailless_menu_render_count_test // test module. Its value is increased everytime system_menu_block:main // block is rendered. $render_counter = $state->get('render_counter', 0); // Invalidating cache tags so that it freshly renders the menu block. \Drupal::service('cache_tags.invalidator') ->invalidateTags(['config:system.menu.main']); // Goto a parent menu link and verify that it is not re-rendered. $this->drupalGet($this->first_item->getUrlObject()); // Resetting state cache so that we don't get cache state value. $state->resetCache(); $this->assertSame(++$render_counter, $state->get('render_counter')); // Goto its child and verify that it is not re-rendered. $this->drupalGet($this->second_item->getUrlObject()); // Resetting state cache so that we don't get cache state value. $state->resetCache(); $this->assertSame($render_counter, $state->get('render_counter')); } } Loading
tests/modules/trailless_menu_render_count_test/trailless_menu_render_count_test.info.yml 0 → 100644 +5 −0 Original line number Diff line number Diff line name: 'Trailless menu render count test' type: module description: 'Support module for trailless_menu for the render counter testing.' package: Testing hidden: true
tests/modules/trailless_menu_render_count_test/trailless_menu_render_count_test.module 0 → 100644 +26 −0 Original line number Diff line number Diff line <?php /** * @file * A module for testing menu block renders count. */ use Drupal\Core\Block\BlockPluginInterface; /** * Implements hook_block_view_alter(). */ function trailless_menu_render_count_test_block_view_alter(array &$build, BlockPluginInterface $block) { /* * Checking that we increase render_counter value for our main navigation * block only. */ if ( $block->getPluginId() === 'system_menu_block:main' && $block->getConfiguration()['label'] === 'trailless_menu main navigation' ) { $state = \Drupal::state(); $counter = $state->get('render_counter') ?? 0; $state->set('render_counter', ++$counter); } }
tests/src/Functional/TraillessMenuTest.php 0 → 100644 +157 −0 Original line number Diff line number Diff line <?php namespace Drupal\Tests\trailless_menu\Functional; use Drupal\menu_link_content\Entity\MenuLinkContent; use Drupal\Tests\BrowserTestBase; /** * Tests that trailless menu doesn't expand and tests that render happens only * once for trailless menu. * * @group trailless_menu */ class TraillessMenuTest extends BrowserTestBase { /** * An editor user. * * @var \Drupal\user\UserInterface */ protected $editor; /** * An authenticated user to test block caching. * * @var object */ protected $normalUser; /** * Modules to enable. * * @var array */ protected static $modules = [ 'system', 'block', 'menu_link_content', 'node', 'trailless_menu', 'trailless_menu_render_count_test', ]; /** * {@inheritdoc} */ protected $defaultTheme = 'stark'; protected function setUp(): void { parent::setUp(); $this->drupalCreateContentType([ 'type' => 'page', 'name' => 'Basic page', 'display_submitted' => FALSE, ]); $this->block = $this->drupalPlaceBlock( 'system_menu_block:main', [ 'label' => 'trailless_menu main navigation' ] ); $this->editor = $this->drupalCreateUser([ 'access administration pages', 'administer blocks', 'administer menu', 'create page content', 'administer trailless menu', ]); $this->drupalLogin($this->editor); // Create additional users to test caching modes. $this->normalUser = $this->drupalCreateUser(); // Create nodes. $first = $this->drupalCreateNode(); $second = $this->drupalCreateNode(); // Create menu links for nodes. $this->first_item = MenuLinkContent::create([ 'link' => [['uri' => 'entity:node/' . $first->id()]], 'title' => 'first', 'menu_name' => 'main', ]); $this->first_item->save(); $this->second_item = MenuLinkContent::create([ 'link' => [['uri' => 'entity:node/' . $second->id()]], 'title' => 'second', 'menu_name' => 'main', 'parent' => 'menu_link_content:' . $this->first_item->uuid(), ]); $this->second_item->save(); // Goto trailless_menu setting page and tick main menu and save form. $edit = [ 'trailless_menus[main]' => TRUE, ]; $this->drupalGet('/admin/config/user-interface/trailless-menu'); $this->submitForm($edit, 'Save configuration'); } /** * Tests that trailless menu doesn't expand. */ public function testTraillessMenuLink() { $this->drupalLogin($this->normalUser); // Goto a parent menu link and verify that parent link exists on the page. $this->drupalGet($this->first_item->getUrlObject()); $this->assertSession() ->linkByHrefExists($this->first_item->getUrlObject()->toString()); // Goto its child and verify that child link doesn't exists on the page. $this->drupalGet($this->second_item->getUrlObject()); $this->assertSession() ->linkByHrefNotExists($this->second_item->getUrlObject()->toString()); } /** * Tests that render happens only once for trailless menu. */ public function testRenderHappensOnceForTraillessMenu() { $this->drupalLogin($this->normalUser); $state = \Drupal::state(); // Resetting state cache so that we don't get cache state value. $state->resetCache(); // Getting render counter, which is set from trailless_menu_render_count_test // test module. Its value is increased everytime system_menu_block:main // block is rendered. $render_counter = $state->get('render_counter', 0); // Invalidating cache tags so that it freshly renders the menu block. \Drupal::service('cache_tags.invalidator') ->invalidateTags(['config:system.menu.main']); // Goto a parent menu link and verify that it is not re-rendered. $this->drupalGet($this->first_item->getUrlObject()); // Resetting state cache so that we don't get cache state value. $state->resetCache(); $this->assertSame(++$render_counter, $state->get('render_counter')); // Goto its child and verify that it is not re-rendered. $this->drupalGet($this->second_item->getUrlObject()); // Resetting state cache so that we don't get cache state value. $state->resetCache(); $this->assertSame($render_counter, $state->get('render_counter')); } }