Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] first try to add tests, fixed schema errors #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions config/schema/widget.schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Block settings schema.
block.settings.widget_block:
type: block_settings
label: 'Widget block'
mapping:
blocks:
type: sequence
label: 'Blocks'
sequence:
- type: widget_block.block
layout:
type: string
label: 'Layout'
classes:
type: sequence
label: 'Classes'
sequence:
- type: string
regions:
type: sequence
label: 'Regions'
sequence:
- type: widget_block.region

widget_block.block:
type: block.settings.[id]
label: 'Widget block settings'
mapping:
id:
type: string
label: 'Id'
region:
type: string
label: 'Region'

widget_block.region:
type: mapping
label: 'Widget region settings'
mapping:
id:
type: string
label: 'Id'
region_id:
type: string
label: 'Region id'
label:
type: string
label: 'Label'
weight:
type: integer
label: 'Weight'
uuid:
type: string
label: 'Uuid'
11 changes: 4 additions & 7 deletions src/Plugin/Block/WidgetBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,6 @@ public function build() {
*/
public function blockForm($form, FormStateInterface $form_state) {
// Fish the page object from the form args.

// Prevent serialization error.
$form['admin_label']['#markup'] = (string) $form['admin_label']['#markup'];

foreach ($form_state->getBuildInfo()['args'] as $arg) {
if ($arg instanceof Page) {
$this->page = $arg->getExecutable();
Expand All @@ -174,7 +170,7 @@ public function blockForm($form, FormStateInterface $form_state) {

$widget_blocks = $form_state->hasValue(array('settings', 'blocks')) ? $form_state->getValue(array('settings', 'blocks')) : $this->configuration['blocks'];
$layout = $form_state->hasValue(array('settings', 'layout')) ? $form_state->getValue(array('settings', 'layout')) : $this->configuration['layout'];
$classes = $form_state->hasValue(array('settings', 'classes')) ? $form_state->getValue(array('settings', 'classes')) : $this->configuration['classes'];
$classes = $form_state->hasValue(array('settings', 'classes')) && !empty($form_state->getValue(array('settings', 'classes'))) ? $form_state->getValue(array('settings', 'classes')) : $this->configuration['classes'];

$ajax_properties = array(
'#ajax' => array(
Expand Down Expand Up @@ -210,7 +206,8 @@ public function blockForm($form, FormStateInterface $form_state) {
$form['classes'] = array(
'#type' => 'textfield',
'#title' => t('CSS Classes'),
'#default_value' => $classes,
'#default_value' => implode(", ", $this->configuration['classes']),
'#description' => t("A list of CSS Classes that will be applied in the widget. Put each on comma separated."),
);

if (!$layout) {
Expand Down Expand Up @@ -269,7 +266,7 @@ public function blockForm($form, FormStateInterface $form_state) {
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['blocks'] = array();
$this->configuration['layout'] = $form_state->getValue('layout');
$this->configuration['classes'] = $form_state->getValue('classes');
$this->configuration['classes'] = array_filter(explode(",", $form_state->getValue('classes')), 'trim');
// Set empty block ID's to NULL.
foreach ($form_state->getValue('blocks') as $region_id => $block) {
if (!empty($block['id'])) {
Expand Down
59 changes: 59 additions & 0 deletions src/Tests/WidgetBlockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* @file
* Contains \Drupal\widget\Tests\WidgetBlockTest.
*/

namespace Drupal\widget\Tests;

use Drupal\simpletest\WebTestBase;

/**
* Generic widget block tests.
*
* @group widget
*/
class WidgetBlockTest extends WebTestBase {

public static $modules = [
'block',
'layout',
'ctools',
'page_manager',
'page_layout',
'widget',
];

/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->drupalPlaceBlock('widget_block');
$admin = $this->drupalCreateUser([
'administer blocks',
'administer pages',
], $this->randomMachineName(), TRUE);
$this->drupalLogin($admin);
}

/**
* Tests a form without submit handler.
*/
public function testFormNoSubmitHandler() {
$edit = [
'settings[layout]' => 'widget_1col',
];
$this->drupalPostAjaxForm('admin/structure/block/add/widget_block/classy?region=content', $edit, 'settings[layout]');
$edit = [
'settings[layout]' => 'widget_1col',
'settings[blocks][links][id]' => 'system_menu_block:main',
'settings[blocks][main][id]' => 'system_main_block',
];
$this->drupalPostForm(NULL, $edit, 'Save block');
$this->assertText(t('The block configuration has been saved.'));
$this->drupalPlaceBlock('widget_block');
$this->drupalGet('<front>');
}

}