View source
<?php
namespace Drupal\Tests\ds\Functional;
use Drupal\views\Tests\ViewTestData;
use Drupal\views\ViewExecutable;
use Drupal\ds_test\Plugin\Block\DsTestBlock;
class BlockFieldPluginTest extends TestBase {
public static $modules = [
'node',
'block',
'ds',
'ds_test',
'views',
];
public static $testViews = [
'ds-testing',
];
protected function setUp() {
parent::setUp();
foreach (ViewExecutable::getPluginTypes() as $plugin_type) {
$this->container
->get("plugin.manager.views.{$plugin_type}")
->clearCachedDefinitions();
}
ViewTestData::createTestViews(get_class($this), [
'ds_test',
]);
}
public function testBlockFieldTitleOverride() {
$edit = [
'name' => 'Test block title field',
'id' => 'test_block_title_field',
'entities[node]' => '1',
'block' => 'views_block:ds_testing-block_1',
];
$this
->dsCreateBlockField($edit);
$this
->dsSelectLayout();
$this
->drupalGet('admin/structure/types/manage/article/display');
$this
->assertSession()
->responseContains('fields[dynamic_block_field:node-test_block_title_field][weight]');
$fields = [
'fields[dynamic_block_field:node-test_block_title_field][region]' => 'left',
'fields[dynamic_block_field:node-test_block_title_field][label]' => 'above',
'fields[body][region]' => 'right',
];
$this
->dsSelectLayout();
$this
->dsConfigureUi($fields);
$settings = [
'type' => 'article',
'promote' => 1,
];
$node = $this
->drupalCreateNode($settings);
$this
->drupalGet('node/' . $node
->id());
$this
->assertSession()
->responseContains('Test block title field');
$edit = [
'use_block_title' => '1',
];
$this
->drupalPostForm('admin/structure/ds/fields/manage_block/test_block_title_field', $edit, t('Save'));
$text = t('The field %name has been saved', [
'%name' => 'Test block title field',
]);
$this
->assertSession()
->responseContains((string) $text);
$this
->drupalGet('node/' . $node
->id());
$this
->assertSession()
->responseContains('Block title from view');
}
public function testBlockAccess() {
$block_field_id = mb_strtolower($this
->randomMachineName());
$entity_type = 'node';
$edit = [
'name' => $this
->randomString(),
'id' => $block_field_id,
'entities[' . $entity_type . ']' => TRUE,
'block' => 'ds_test_block',
];
$this
->dsCreateBlockField($edit);
$fields['fields[dynamic_block_field:' . $entity_type . '-' . $block_field_id . '][region]'] = 'left';
$this
->dsSelectLayout();
$this
->dsConfigureUI($fields);
$settings['type'] = 'article';
$node = $this
->drupalCreateNode($settings);
\Drupal::state()
->set('ds_test_block__access', FALSE);
$this
->drupalGet($node
->toUrl());
$this
->assertSession()
->responseNotContains(DsTestBlock::BODY_TEXT);
$this
->resetAll();
\Drupal::state()
->set('ds_test_block__access', TRUE);
$this
->drupalGet($node
->toUrl());
$this
->assertSession()
->responseContains(DsTestBlock::BODY_TEXT);
}
public function testBlockCache() {
$block_field_id = mb_strtolower($this
->randomMachineName());
$entity_type = 'node';
$edit = [
'name' => $this
->randomString(),
'id' => $block_field_id,
'entities[' . $entity_type . ']' => TRUE,
'block' => 'ds_cache_test_block',
];
$this
->dsCreateBlockField($edit);
$fields['fields[dynamic_block_field:' . $entity_type . '-' . $block_field_id . '][region]'] = 'left';
$this
->dsSelectLayout();
$this
->dsConfigureUI($fields);
$settings['type'] = 'article';
$node = $this
->drupalCreateNode($settings);
$this
->drupalGet($node
->toUrl(), [
'query' => [
'cached' => 1,
],
]);
$this
->assertSession()
->responseContains('cached=1');
$this
->drupalGet($node
->toUrl(), [
'query' => [
'cached' => 2,
],
]);
$this
->assertSession()
->responseContains('cached=2');
}
public function testBlockAddWrappers() {
$block_wrapper_selector = '.field--name-dynamic-block-fieldnode-test-block-title-field .block';
$edit = [
'name' => 'Test block title field',
'id' => 'test_block_title_field',
'entities[node]' => '1',
'block' => 'views_block:ds_testing-block_1',
];
$this
->dsCreateBlockField($edit);
$fields = [
'fields[dynamic_block_field:node-test_block_title_field][region]' => 'left',
'fields[dynamic_block_field:node-test_block_title_field][label]' => 'above',
'fields[body][region]' => 'right',
];
$this
->dsSelectLayout();
$this
->dsConfigureUi($fields);
$settings = [
'type' => 'article',
];
$node = $this
->drupalCreateNode($settings);
$this
->drupalGet('node/' . $node
->id());
$this
->assertSession()
->elementNotExists('css', $block_wrapper_selector);
$edit = [
'add_block_wrappers' => '1',
];
$this
->drupalPostForm('admin/structure/ds/fields/manage_block/test_block_title_field', $edit, t('Save'));
$this
->drupalGet('node/' . $node
->id());
$this
->assertSession()
->elementExists('css', $block_wrapper_selector);
}
}