improvements-2.x-dev/modules/improvements_menu/tests/src/Functional/ImprovementsMenuTest.php

modules/improvements_menu/tests/src/Functional/ImprovementsMenuTest.php
<?php

namespace Drupal\Tests\improvements_menu\Functional;

use Drupal\Tests\block\Traits\BlockCreationTrait;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\improvements\Traits\ImprovementsTestTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;

class ImprovementsMenuTest extends BrowserTestBase {

  use BlockCreationTrait;
  use ImprovementsTestTrait;
  use NodeCreationTrait;

  /**
   * {@inheritDoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * {@inheritDoc}
   */
  protected static $modules = [
    'improvements_menu',
    'menu_ui',
    'menu_link_content',
  ];

  /**
   * {@inheritDoc}
   */
  protected function setUp(): void {
    parent::setUp();
  }

  /**
   * Test menu form autofocus.
   */
  public function testMenuFormAutofocus(): void {
    $this->drupalLoginAsRoot();
    $this->drupalGet('/admin/structure/menu/add');
    $this->dontSeeErrorMessage();
    $web_assert = $this->assertSession();
    $web_assert->elementAttributeExists('css', '#edit-label', 'autofocus');
  }

  /**
   * Test menu item form improvements.
   */
  public function testMenuItemFormImprovements(): void {
    $this->drupalLoginAsRoot();
    $this->drupalGet('/admin/structure/menu/manage/main/add');
    $this->dontSeeErrorMessage();
    $web_assert = $this->assertSession();
    $web_assert->elementAttributeExists('css', '#edit-title-0-value', 'autofocus');
    $web_assert->elementExists('css', '#edit-link-0-uri');
    $web_assert->elementNotExists('css', '#edit-link-0-uri[required]');
  }

  /**
   * Test menu link advanced settings.
   */
  public function testMenuLinkAdvancedSettings(): void {
    $this->installModule('block', 'improvements');
    $this->placeBlock('system_menu_block:main', [
      'id' => 'main_menu_block',
      'region' => 'help',
      'weight' => 0,
      'label' => 'Main menu block',
    ]);

    $this->drupalLoginAsRoot();
    $web_assert = $this->assertSession();

    $this->drupalGet('/admin/structure/menu/manage/main/add');
    $this->dontSeeErrorMessage();
    $web_assert->elementExists('css', '#edit-open-in-dialog');
    $web_assert->elementExists('css', '#edit-target-blank');
    $web_assert->elementExists('css', '#edit-item-class');
    $web_assert->elementExists('css', '#edit-link-class');

    $this->submitForm([
      'title[0][value]' => 'Test link',
      'link[0][uri]' => '/user',
      'open_in_dialog' => TRUE,
      'hide_href' => TRUE,
      'target_blank' => TRUE,
      'item_class' => 'test-link-item',
      'link_class' => 'test-link',
    ], 'Save');
    $this->dontSeeErrorMessage();

    $this->drupalGetFrontPage();
    $this->dontSeeErrorMessage();
    $web_assert->elementExists('css', '#block-main-menu-block li.test-link-item');
    $web_assert->elementExists('css', '#block-main-menu-block a.test-link');
    $web_assert->elementExists('css', '#block-main-menu-block a.test-link.use-ajax');
    $web_assert->elementAttributeContains('css', '#block-main-menu-block a.test-link', 'data-dialog-type', 'modal');
    $web_assert->elementAttributeContains('css', '#block-main-menu-block a.test-link', 'target', '_blank');
    $web_assert->elementAttributeContains('css', '#block-main-menu-block a.test-link', 'href', '#');
    $web_assert->elementAttributeContains('css', '#block-main-menu-block a.test-link', 'data-ajax-url', '/user');
    $web_assert->elementExists('css', 'script[src^="/core/misc/ajax.js"]');
  }

  /**
   * Test menu block tweak cache.
   */
  public function testMenuBlockTweakCache(): void {
    $this->installModule('block', 'node', 'improvements');
    $this->createContentType(['type' => 'page', 'name' => 'Page']);
    $this->drupalLoginAsRoot();

    $node1 = $this->createNode(['title' => 'Node 1 for ' . __FUNCTION__]);
    $node2 = $this->createNode(['title' => 'Node 2 for ' . __FUNCTION__]);
    $menu = $this->createMenu('test_menu', 'Menu for ' . __FUNCTION__);
    $menu_link1 = $this->createMenuLinkForEntity($node1, $menu->id());
    $menu_link2 = $this->createMenuLinkForEntity($node2, $menu->id());

    $this->drupalGet('/admin/structure/menu/manage/' . $menu->id());
    $this->dontSeeErrorMessage();
    $web_assert = $this->assertSession();
    $web_assert->pageTextContains($menu_link1->label());
    $web_assert->pageTextContains($menu_link2->label());

    $block = $this->drupalPlaceBlock('system_menu_block:' . $menu->id(), [
      'id' => 'test_menu_block',
      'region' => 'content',
      'weight' => 0,
      'label' => 'Block for ' . __FUNCTION__,
    ]);
    $block_html_id = '#block-test-menu-block';

    $this->drupalGetEntityPage($node1);
    $web_assert->elementExists('css', $block_html_id);
    $this->assertEquals(1, $this->countRenderCache('entity_view:block:' . $block->id()));

    $this->drupalGetEntityPage($node2);
    $web_assert->elementExists('css', $block_html_id);
    $this->assertEquals(2, $this->countRenderCache('entity_view:block:' . $block->id()));

    $this->drupalGet('/admin/structure/block/manage/' . $block->id());
    $this->dontSeeErrorMessage();
    $web_assert->fieldExists('tweak_cache');
    $web_assert->checkboxNotChecked('tweak_cache');
    $this->clearRenderCache();
    $this->submitForm([
      'tweak_cache' => 1,
    ], 'Save block');
    $this->dontSeeErrorMessage();
    $this->drupalGet('/admin/structure/block/manage/' . $block->id());
    $web_assert->checkboxChecked('tweak_cache');

    $this->drupalGetEntityPage($node1);
    $web_assert->elementExists('css', $block_html_id);
    $this->assertEquals(1, $this->countRenderCache('entity_view:block:' . $block->id()));

    $this->drupalGetEntityPage($node2);
    $web_assert->elementExists('css', $block_html_id);
    $this->assertEquals(1, $this->countRenderCache('entity_view:block:' . $block->id()));
  }

  /**
   * Test menu block items limitation.
   */
  public function testMenuBlockItemsLimitation(): void {
    $this->installModule('block', 'node', 'improvements');
    $this->createContentType(['type' => 'page', 'name' => 'Page']);
    $this->drupalLoginAsRoot();

    $node1 = $this->createNode(['title' => 'Node 1 for ' . __FUNCTION__]);
    $node2 = $this->createNode(['title' => 'Node 2 for ' . __FUNCTION__]);
    $menu = $this->createMenu('test_menu', 'Menu for ' . __FUNCTION__);
    $menu_link1 = $this->createMenuLinkForEntity($node1, $menu->id());
    $menu_link2 = $this->createMenuLinkForEntity($node2, $menu->id());

    $block = $this->drupalPlaceBlock('system_menu_block:' . $menu->id(), [
      'id' => 'test_menu_block',
      'region' => 'content',
      'weight' => 0,
      'label' => 'Block for ' . __FUNCTION__,
    ]);
    $block_html_id = '#block-test-menu-block';

    // Check menu block render
    $this->drupalGetFrontPage();
    $this->dontSeeErrorMessage();
    $web_assert = $this->assertSession();
    $web_assert->elementExists('css', $block_html_id);
    $web_assert->elementTextContains('css', $block_html_id, $menu_link1->label());
    $web_assert->elementTextContains('css', $block_html_id, $menu_link2->label());

    // Check menu block form
    $this->drupalGet('/admin/structure/block/manage/' . $block->id());
    $this->dontSeeErrorMessage();
    $web_assert->fieldExists('items_limitation[method]');
    $web_assert->fieldExists('items_limitation[items][]');

    // Save menu block settings
    $this->submitForm([
      'items_limitation[method]' => 'exclude_selected',
      'items_limitation[items][]' => 'menu_link_content:' . $menu_link1->uuid(),
    ], 'Save block');
    $this->dontSeeErrorMessage();
    $this->drupalGet('/admin/structure/block/manage/' . $block->id());
    $web_assert->fieldValueEquals('items_limitation[method]', 'exclude_selected');
    $web_assert->elementExists('css', 'option[value="menu_link_content:' . $menu_link1->uuid() . '"][selected]');

    // Check menu block with new settings
    $this->drupalGetFrontPage();
    $web_assert->elementExists('css', $block_html_id);
    $web_assert->elementTextNotContains('css', $block_html_id, $menu_link1->label());
    $web_assert->elementTextContains('css', $block_html_id, $menu_link2->label());

    // Change menu block settings and check result
    $this->drupalGet('/admin/structure/block/manage/' . $block->id());
    $this->submitForm([
      'items_limitation[method]' => 'exclude_non_selected',
      'items_limitation[items][]' => 'menu_link_content:' . $menu_link1->uuid(),
    ], 'Save block');
    $this->dontSeeErrorMessage();
    $this->drupalGetFrontPage();
    $web_assert->elementExists('css', $block_html_id);
    $web_assert->elementTextContains('css', $block_html_id, $menu_link1->label());
    $web_assert->elementTextNotContains('css', $block_html_id, $menu_link2->label());
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc