Commit 27360aff authored by jrockowitz's avatar jrockowitz

Write tests.

parent 45da6270
......@@ -64,17 +64,7 @@ Known Issues/Limitations
Todo
----
Write Tests
- Copy \Drupal\link\Tests\LinkFieldTest
- Check block field widget
- Check block field formatter
- Check block field type
- Check block field configuration form
- Check block field cache dependencies
- Check block field access result
- Write additional test for block field plugin ids setting.
Author/Maintainer
......
# Schema for the configuration files of the Link module.
field.formatter.settings.block_field:
type: mapping
label: 'Link format settings'
mapping: { }
field.widget.settings.block_field_default:
type: mapping
label: 'Link format settings'
mapping:
plugin_id:
type: string
label: 'Plugin ID'
settings:
type: sequence
label: 'Settings'
sequence:
type: string
field.storage_settings.block_field:
type: mapping
label: 'Block field settings'
field.field_settings.block_field:
type: mapping
label: 'Block field settings'
mapping:
plugin_ids:
type: sequence
label: 'Plugin ids'
field.value.block_field:
type: mapping
label: 'Default value'
mapping:
plugin_id:
type: string
label: 'Plugin ID'
settings:
type: sequence
label: 'Settings'
sequence:
type: stringd
......@@ -36,13 +36,6 @@ class BlockFieldFormatter extends FormatterBase {
continue;
}
try {
$build = $block_instance->build();
}
catch (\Exception $exception) {
continue;
}
// @see \Drupal\block\BlockViewBuilder::buildPreRenderableBlock
// @see template_preprocess_block()
$elements[$delta] = [
......@@ -53,7 +46,7 @@ class BlockFieldFormatter extends FormatterBase {
'#base_plugin_id' => $block_instance->getBaseId(),
'#derivative_plugin_id' => $block_instance->getDerivativeId(),
'#id' => $item->plugin_id,
'content' => $build,
'content' => $block_instance->build(),
];
/** @var \Drupal\Core\Render\RendererInterface $renderer */
......
<?php
namespace Drupal\block_field\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Tests block field widgets and formatters.
*
* @group block_field
*/
class BlockFieldTest extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['node', 'user', 'block', 'block_field', 'block_field_test'];
/**
* Tests block field.
*/
function testBlockField() {
$admin_user = $this->drupalCreateUser([
'access content',
'administer nodes',
'administer content types',
'bypass node access',
]);
$this->drupalLogin($admin_user);
// Create block field test using the three test blocks.
// Check that add more and ajax callbacks are working as expected.
$this->drupalPostForm('node/add/block_field_test', [
'title[0][value]' => 'Block field test',
], t('Add another item'));
$this->drupalPostForm(NULL, [], t('Add another item'));
$this->drupalPostForm(NULL, [
'field_block_field_test[0][plugin_id]' => 'block_field_test_authenticated',
'field_block_field_test[1][plugin_id]' => 'block_field_test_content',
'field_block_field_test[2][plugin_id]' => 'block_field_test_time',
], t('Add another item'));
$this->drupalPostForm(NULL, [
'field_block_field_test[0][plugin_id]' => 'block_field_test_authenticated',
'field_block_field_test[1][plugin_id]' => 'block_field_test_content',
'field_block_field_test[2][plugin_id]' => 'block_field_test_time',
], t('Add another item'));
$this->drupalPostForm(NULL, [], t('Save and publish'));
// Check blocks displayed to authenticated.
$this->drupalGet('node/1');
$this->assertRaw('<div class="field field--name-field-block-field-test field--type-block-field field--label-above">');
$this->assertRaw('<div class="field__label">Block field test</div>');
$this->assertRaw('<h2>You are logged in as...</h2>');
$this->assertRaw('<p><span>' . $admin_user->label() . '</span></p>');
$this->assertRaw('<h2>Block field test content</h2>');
$this->assertRaw('This block was created at');
$this->assertRaw('<h2>The time is...</h2>');
$this->assertPattern('/\d\d:\d\d:\d\d/');
// Create a block_field_test node.
$block_node = $this->drupalCreateNode([
'type' => 'block_field_test',
]);
// Check authenticated block.
$block_node->field_block_field_test->plugin_id = 'block_field_test_authenticated';
$block_node->field_block_field_test->settings = [
'label' => 'Authenticated',
'label_display' => TRUE,
];
$block_node->save();
$this->drupalGet('node/' . $block_node->id());
$this->assertRaw('<h2>Authenticated</h2>');
$this->assertRaw('<p><span>' . $admin_user->label() . '</span></p>');
// Check block_field_test_authenticated cache dependency is respected when
// the user's name is updated.
$admin_user->setUsername('admin_user');
$admin_user->save();
$this->drupalGet('node/' . $block_node->id());
$this->assertRaw('<h2>Authenticated</h2>');
$this->assertRaw('<p><span>admin_user</span></p>');
// Check authenticated block is not visible to anonymous users.
$this->drupalLogout();
$this->drupalGet('node/' . $block_node->id());
$this->assertNoRaw('<h2>Authenticated</h2>');
$this->assertNoRaw('<p><span>' . $admin_user->label() . '</span></p>');
// Check content block.
$block_node->field_block_field_test->plugin_id = 'block_field_test_content';
$block_node->field_block_field_test->settings = [
'label' => 'Hello',
'label_display' => TRUE,
'content' => '<p>World</p>',
];
$block_node->save();
$this->drupalGet('node/' . $block_node->id());
$this->assertRaw('<h2>Hello</h2>');
$this->assertRaw('<p>World</p>');
// ISSUE: Drupal's page cache it not respecting the time block max age,
// so we need to log in to bypass page caching.
$this->drupalLogin($admin_user);
// Check time block.
$block_node->field_block_field_test->plugin_id = 'block_field_test_time';
$block_node->field_block_field_test->settings = [
'label' => 'Time',
'label_display' => TRUE,
];
$block_node->save();
// Check that time is set.
$this->drupalGet('node/' . $block_node->id());
$this->assertPattern('/\d\d:\d\d:\d\d \(\d+\)/');
// Get the current time.
preg_match('/\d\d:\d\d:\d\d \(\d+\)/', $this->getRawContent(), $match);
$time = $match[0];
$this->assertRaw($time);
// Have delay test one second so that the time is updated.
sleep(1);
// Check that time is never cached by reloading the page.
$this->drupalGet('node/' . $block_node->id());
$this->assertPattern('/\d\d:\d\d:\d\d \(\d+\)/');
$this->assertNoRaw($time);
}
}
......@@ -20,8 +20,8 @@ content:
field_block_field_test:
weight: 32
settings:
id: ''
settings: { }
plugin_id: ''
third_party_settings: { }
type: block_field_default
path:
......
......@@ -20,4 +20,6 @@ content:
type: block_field
links:
weight: 100
settings: { }
third_party_settings: { }
hidden: { }
......@@ -29,6 +29,8 @@ class BlockFieldTestAuthenticatedBlock extends BlockBase {
return [
'#theme' => 'username',
'#account' => \Drupal::currentUser()->getAccount(),
'#prefix' => '<p>',
'#suffix' => '</p>',
];
}
......
......@@ -26,7 +26,7 @@ class BlockFieldTestContentBlock extends BlockBase {
*/
public function defaultConfiguration() {
return [
'content' => $this->t('A default value. This block was created at %time', ['%time' => date('c')]),
'content' => $this->t('A default value. This block was created at @time', ['@time' => date('Y-m-d h:i:sa')]),
];
}
......
......@@ -26,7 +26,7 @@ class BlockFieldTestTimeBlock extends BlockBase {
public function build() {
return [
'#type' => 'markup',
'#markup' => date('H:i:s'),
'#markup' => date('H:i:s') . ' (' . time() . ')',
];
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment