Skip to content
Snippets Groups Projects
Commit e953c2b4 authored by Sean Adams-Hiett's avatar Sean Adams-Hiett
Browse files

Issue #3203657 by pyrello, Luke.Leber, malik.kotob: Add tests

parents cac140f8 d0696344
No related branches found
No related tags found
No related merge requests found
use_popover: 0
trigger_label: 'Add block'
use_label: 0
label: 'Add block'
......@@ -2,7 +2,7 @@ lb_direct_add.settings:
type: config_object
mapping:
use_label:
type: string
type: integer
label: "How to display the blocks to be added"
label:
type: string
......
......@@ -2,7 +2,7 @@
* @file
*/
(function ($, Drupal) {
(function ($, Drupal, once) {
function DirectAddWidget(directAdd, settings) {
var options = $.extend({
title: Drupal.t('List additional actions')
......@@ -28,10 +28,10 @@
*/
Drupal.behaviors.lbDirectAdd = {
attach: function (context, settings) {
var $directAddWidgets = $(context).find('.layout-builder__add-block').once('lb_direct_add');
var $directAddWidgets = $(once('lb_direct_add', '.layout-builder__add-block', context));
if ($directAddWidgets.length) {
var $body = $('body').once('lb_direct_add-click');
var $body = $(once('lb_direct_add-click', 'body', context));
if ($body.length) {
$body.on('click', '.layout-builder__direct-add__toggle', directAddClickHandler);
......@@ -75,5 +75,6 @@
this.hoverIn.call(this, e);
}
});
Drupal.lbDirectAdd = DirectAddWidget;
})(jQuery, Drupal);
})(jQuery, Drupal, once);
name: "Layout Builder Direct Add"
description: Converts the 'Add block' link in Layout Builder into a drop-button to directly add custom block types.
type: module
core_version_requirement: ^8 || ^9 || ^10
core_version_requirement: ^9.2 || ^10
configure: lb_direct_add.settings
dependencies:
- drupal:layout_builder
......
......@@ -5,4 +5,4 @@ direct_add:
js:
js/lb-direct-add.js: {}
dependencies:
- core/jquery.once
- core/once
<?php
namespace Drupal\Tests\lb_direct_add\FunctionalJavascript;
/**
* Contains test cases for dropbutton functionality.
*
* @group lb_direct_add
*/
class LayoutBuilderDirectAddDropbuttonTest extends LayoutBuilderDirectAddTestBase {
/**
* Test case for dropbutton functionality.
*/
public function testDropbutton() {
$this->drupalLogin($this->drupalCreateUser([], NULL, TRUE));
$this->drupalGet(static::FIELD_UI_PREFIX . '/display/default/layout');
$page = $this->getSession()->getPage();
// The basic block link should exist and be visible.
$dropbutton_action = $page->findLink('Basic block');
static::assertNotNull($dropbutton_action);
static::assertStringEndsWith(static::BASIC_BLOCK_HREF, $dropbutton_action->getAttribute('href'));
static::assertTrue($dropbutton_action->isVisible());
// More options should exist, but not be visible.
$dropbutton_more = $page->findLink('More options');
static::assertNotNull($dropbutton_more);
static::assertStringEndsWith(static::CHOOSE_BLOCK_HREF, $dropbutton_more->getAttribute('href'));
static::assertFalse($dropbutton_more->isVisible());
// Dropbutton should exist and be visible.
$dropbutton_toggle = $page->findButton('List additional actions');
static::assertNotNull($dropbutton_toggle);
static::assertTrue($dropbutton_toggle->isVisible());
// Click the dropbutton arrow.
$dropbutton_toggle->click();
// More options should be visible now.
static::assertTrue($dropbutton_more->isVisible());
// Click the dropbutton arrow again.
$dropbutton_toggle->click();
// More options should not be visible again.
static::assertFalse($dropbutton_more->isVisible());
}
}
<?php
namespace Drupal\Tests\lb_direct_add\FunctionalJavascript;
/**
* Contains test cases for popover functionality.
*
* @group lb_direct_add
*/
class LayoutBuilderDirectAddPopoverTest extends LayoutBuilderDirectAddTestBase {
/**
* Test case for popover functionality.
*/
public function testPopover() {
// We use the label for this version.
$this->setUseLabel(1);
$this->drupalLogin($this->drupalCreateUser([], NULL, TRUE));
$this->drupalGet(static::FIELD_UI_PREFIX . '/display/default/layout');
$page = $this->getSession()->getPage();
// The link with the default label should exist and be visible.
$add_block = $page->findLink('Add block');
static::assertNotNull($add_block);
static::assertTrue($add_block->isVisible());
// Change the label.
$this->setLabel('Add a block');
$this->drupalGet(static::FIELD_UI_PREFIX . '/display/default/layout');
// The default label should no longer exist.
$add_block = $page->findLink('Add block');
static::assertNull($add_block);
// The link with the new label should exist and be visible.
$add_block = $page->findLink('Add a block');
static::assertNotNull($add_block);
static::assertTrue($add_block->isVisible());
// Basic block link should exist but not be visible.
$basic_block = $page->findLink('Basic block');
static::assertNotNull($basic_block);
static::assertStringEndsWith(static::BASIC_BLOCK_HREF, $basic_block->getAttribute('href'));
static::assertFalse($basic_block->isVisible());
// More options link should exist but not be visible.
$more = $page->findLink('More options');
static::assertNotNull($more);
static::assertStringEndsWith(static::CHOOSE_BLOCK_HREF, $more->getAttribute('href'));
static::assertFalse($more->isVisible());
// Click the link to show blocks.
$add_block->click();
// Basic block and more options links should be visible now.
static::assertTrue($basic_block->isVisible());
static::assertTrue($more->isVisible());
// Click the link again to hide blocks.
$add_block->click();
// Basic block and more options links should not be visible now.
static::assertFalse($basic_block->isVisible());
static::assertFalse($more->isVisible());
}
}
<?php
namespace Drupal\Tests\lb_direct_add\FunctionalJavascript;
use Drupal\block_content\Entity\BlockContentType;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
/**
* Base class for layout builder direct add javascript tests.
*/
abstract class LayoutBuilderDirectAddTestBase extends WebDriverTestBase {
/**
* Path prefix for the field UI for the test bundle.
*/
protected const FIELD_UI_PREFIX = '/admin/structure/types/manage/bundle_with_section_field';
/**
* Known href attribute for adding a basic block.
*/
protected const BASIC_BLOCK_HREF = '/layout_builder/add/block/defaults/node.bundle_with_section_field.default/0/content/inline_block%3Abasic';
/**
* Known href attribute for adding a block through the core chooser.
*/
protected const CHOOSE_BLOCK_HREF = '/layout_builder/choose/block/defaults/node.bundle_with_section_field.default/0/content';
/**
* {@inheritdoc}
*/
protected static $modules = [
'block_content',
'layout_builder',
'block',
'node',
'contextual',
'field_ui',
'lb_direct_add',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Sets a new drop button display option.
*
* @param int $setting
* The new drop button display option.
*/
protected function setUseLabel($setting) {
$this->config('lb_direct_add.settings')
->set('use_label', $setting)
->save();
}
/**
* Sets a new trigger label.
*
* @param string $setting
* The new label.
*/
protected function setLabel($setting) {
$this->config('lb_direct_add.settings')
->set('label', $setting)
->save();
}
/**
* {@inheritdoc}
*
* Sets up the content types required for lb_direct_add tests.
*/
protected function setUp() : void {
parent::setUp();
$this->createContentType([
'type' => 'bundle_with_section_field',
'new_revision' => TRUE,
]);
/** @var \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface $display */
$display = \Drupal::service('entity_display.repository')->getViewDisplay('node', 'bundle_with_section_field');
$display->enableLayoutBuilder()->setOverridable()->save();
$bundle = BlockContentType::create([
'id' => 'basic',
'label' => 'Basic block',
'revision' => 1,
]);
$bundle->save();
block_content_add_body_field($bundle->id());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment