View source
<?php
namespace Drupal\block_visibility_groups\Tests;
use Drupal\block_visibility_groups\Entity\BlockVisibilityGroup;
use Drupal\Core\Url;
class VisibilityTest extends BlockVisibilityGroupsTestBase {
protected function setUp() {
parent::setUp();
if ($this->profile != 'standard') {
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
'display_submitted' => FALSE,
]);
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
}
}
public static $modules = [
'block',
'block_visibility_groups',
'node',
];
public function testSingleConditions() {
$config = [
'id' => 'node_type',
'bundles' => [
'page',
],
'negate' => 0,
'context_mapping' => [
'node' => '@node.node_route_context:node',
],
];
$configs['request'] = [
'id' => 'request_path',
'pages' => '/node/*',
'negate' => 0,
];
$group = $this
->createGroup($configs);
$block_title = $this
->randomMachineName();
$block_id = $this
->placeBlockInGroupUI('system_powered_by_block', $group
->id(), $block_title);
$page_node = $this
->drupalCreateNode();
$this
->drupalGet('node/' . $page_node
->id());
$this
->assertText($block_title, 'Block shows up on page node when added via UI.');
$this
->drupalGet('user');
$this
->assertNoText($block_title, 'Block does not show up on user page when added via UI.');
$this
->updateBlockInGroupUI($block_id, $group
->id());
$default_theme = $this
->config('system.theme')
->get('default');
$option = Url::fromRoute('block.admin_display_theme', [
'theme' => $default_theme,
], [
'query' => [
'block_visibility_group' => $group
->id(),
],
])
->toString();
$this
->assertOptionSelected('edit-select', $option, "User gets redirected to the selected group's block layout page.");
$block = $this
->placeBlockInGroup('system_powered_by_block', $group
->id());
$this
->drupalGet('node/' . $page_node
->id());
$this
->assertText($block
->label(), 'Block shows up on page node.');
$this
->drupalGet('user');
$this
->assertNoText($block
->label(), 'Block does not show up on user page.');
$this->container
->get('module_installer')
->uninstall([
'block_visibility_groups',
]);
$this
->drupalGet('node/' . $page_node
->id());
$this
->assertText($block
->label(), 'Block shows up on page node.');
$this
->drupalGet('user');
$this
->assertText($block
->label(), 'Block shows up on user node.');
}
private function createGroup(array $configs) {
$group = BlockVisibilityGroup::create([
'id' => $this
->randomMachineName(),
'label' => $this
->randomString(),
]);
$group
->save();
foreach ($configs as $config) {
$group
->addCondition($config);
}
$group
->save();
return $group;
}
}