Newer
Older
Dries Buytaert
committed
<?php
namespace Drupal\Tests\block_content\Functional;
use Drupal\block_content\Entity\BlockContentType;
use Drupal\Component\Utility\Html;
Alex Pott
committed
use Drupal\Core\Url;
use Drupal\Tests\system\Functional\Menu\AssertBreadcrumbTrait;
Dries Buytaert
committed
/**
* Ensures that custom block type functions work correctly.
*
* @group block_content
Dries Buytaert
committed
*/
class BlockContentTypeTest extends BlockContentTestBase {
Dries Buytaert
committed
Alex Pott
committed
use AssertBreadcrumbTrait;
Dries Buytaert
committed
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['field_ui'];
Dries Buytaert
committed
/**
* Permissions to grant admin user.
*
* @var array
*/
protected $permissions = [
Dries Buytaert
committed
'administer blocks',
'administer block_content fields',
];
Dries Buytaert
committed
/**
* Whether or not to create an initial block type.
*
* @var bool
*/
protected $autoCreateBasicBlockType = FALSE;
protected function setUp() {
parent::setUp();
$this->drupalPlaceBlock('page_title_block');
}
Dries Buytaert
committed
/**
* Tests creating a block type programmatically and via a form.
*/
public function testBlockContentTypeCreation() {
// Log in a test user.
$this->drupalLogin($this->adminUser);
// Test the page with no block-types.
$this->drupalGet('block/add');
$this->assertResponse(200);
$this->assertText('You have not created any block types yet');
$this->clickLink('block type creation page');
Dries Buytaert
committed
// Create a block type via the user interface.
$edit = [
Dries Buytaert
committed
'id' => 'foo',
'label' => 'title for foo',
];
$this->drupalPostForm(NULL, $edit, t('Save'));
$block_type = BlockContentType::load('foo');
Dries Buytaert
committed
$this->assertTrue($block_type, 'The new block type has been created.');
$field_definitions = \Drupal::entityManager()->getFieldDefinitions('block_content', 'foo');
$this->assertTrue(isset($field_definitions['body']), 'Body field created when using the UI to create block content types.');
// Check that the block type was created in site default language.
$default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();
$this->assertEqual($block_type->language()->getId(), $default_langcode);
// Create block types programmatically.
$this->createBlockContentType('basic', TRUE);
$field_definitions = \Drupal::entityManager()->getFieldDefinitions('block_content', 'basic');
$this->assertTrue(isset($field_definitions['body']), "Body field for 'basic' block type created when using the testing API to create block content types.");
$this->createBlockContentType('other');
$field_definitions = \Drupal::entityManager()->getFieldDefinitions('block_content', 'other');
$this->assertFalse(isset($field_definitions['body']), "Body field for 'other' block type not created when using the testing API to create block content types.");
$block_type = BlockContentType::load('other');
$this->assertTrue($block_type, 'The new block type has been created.');
$this->drupalGet('block/add/' . $block_type->id());
$this->assertResponse(200);
Dries Buytaert
committed
}
/**
* Tests editing a block type using the UI.
*/
public function testBlockContentTypeEditing() {
Alex Pott
committed
$this->drupalPlaceBlock('system_breadcrumb_block');
// Now create an initial block-type.
$this->createBlockContentType('basic', TRUE);
Dries Buytaert
committed
$this->drupalLogin($this->adminUser);
// We need two block types to prevent /block/add redirecting.
$this->createBlockContentType('other');
Dries Buytaert
committed
$field_definitions = \Drupal::entityManager()->getFieldDefinitions('block_content', 'other');
$this->assertFalse(isset($field_definitions['body']), 'Body field was not created when using the API to create block content types.');
Dries Buytaert
committed
// Verify that title and body fields are displayed.
$this->drupalGet('block/add/basic');
$this->assertRaw('Block description', 'Block info field was found.');
$this->assertNotEmpty($this->cssSelect('#edit-body-0-value'), 'Body field was found.');
Dries Buytaert
committed
// Change the block type name.
$edit = [
Dries Buytaert
committed
'label' => 'Bar',
];
Alex Pott
committed
$this->drupalGet('admin/structure/block/block-content/manage/basic');
$this->assertTitle(format_string('Edit @type custom block type | Drupal', ['@type' => 'basic']));
$this->drupalPostForm(NULL, $edit, t('Save'));
Alex Pott
committed
$front_page_path = Url::fromRoute('<front>')->toString();
$this->assertBreadcrumb('admin/structure/block/block-content/manage/basic/fields', [
$front_page_path => 'Home',
'admin/structure/block' => 'Block layout',
'admin/structure/block/block-content' => 'Custom block library',
Lee Rowlands
committed
'admin/structure/block/block-content/manage/basic' => 'Edit Bar',
Alex Pott
committed
]);
\Drupal::entityManager()->clearCachedFieldDefinitions();
Dries Buytaert
committed
$this->drupalGet('block/add');
$this->assertRaw('Bar', 'New name was displayed.');
$this->clickLink('Bar');
Angie Byron
committed
$this->assertUrl(\Drupal::url('block_content.add_form', ['block_content_type' => 'basic'], ['absolute' => TRUE]), [], 'Original machine name was used in URL.');
Dries Buytaert
committed
// Remove the body field.
$this->drupalPostForm('admin/structure/block/block-content/manage/basic/fields/block_content.basic.body/delete', [], t('Delete'));
Dries Buytaert
committed
// Resave the settings for this type.
$this->drupalPostForm('admin/structure/block/block-content/manage/basic', [], t('Save'));
Dries Buytaert
committed
// Check that the body field doesn't exist.
$this->drupalGet('block/add/basic');
$this->assertEmpty($this->cssSelect('#edit-body-0-value'), 'Body field was not found.');
Dries Buytaert
committed
}
/**
* Tests deleting a block type that still has content.
*/
public function testBlockContentTypeDeletion() {
// Now create an initial block-type.
$this->createBlockContentType('basic', TRUE);
Dries Buytaert
committed
// Create a block type programmatically.
$type = $this->createBlockContentType('foo');
Dries Buytaert
committed
$this->drupalLogin($this->adminUser);
// Add a new block of this type.
$block = $this->createBlockContent(FALSE, 'foo');
Dries Buytaert
committed
// Attempt to delete the block type, which should not be allowed.
$this->drupalGet('admin/structure/block/block-content/manage/' . $type->id() . '/delete');
Dries Buytaert
committed
$this->assertRaw(
t('%label is used by 1 custom block on your site. You can not remove this block type until you have removed all of the %label blocks.', ['%label' => $type->label()]),
Dries Buytaert
committed
'The block type will not be deleted until all blocks of that type are removed.'
);
Alex Pott
committed
$this->assertNoText(t('This action cannot be undone.'), 'The block type deletion confirmation form is not available.');
Dries Buytaert
committed
// Delete the block.
$block->delete();
// Attempt to delete the block type, which should now be allowed.
$this->drupalGet('admin/structure/block/block-content/manage/' . $type->id() . '/delete');
Dries Buytaert
committed
$this->assertRaw(
t('Are you sure you want to delete the custom block type %type?', ['%type' => $type->id()]),
Dries Buytaert
committed
'The block type is available for deletion.'
);
$this->assertText(t('This action cannot be undone.'), 'The custom block type deletion confirmation form is available.');
}
/**
* Tests that redirects work as expected when multiple block types exist.
*/
public function testsBlockContentAddTypes() {
// Now create an initial block-type.
$this->createBlockContentType('basic', TRUE);
$this->drupalLogin($this->adminUser);
// Create two block types programmatically.
$type = $this->createBlockContentType('foo');
$type = $this->createBlockContentType('bar');
catch
committed
// Get the custom block storage.
$storage = $this->container
->get('entity.manager')
->getStorage('block_content');
catch
committed
// Install all themes.
\Drupal::service('theme_handler')->install(['bartik', 'seven', 'stark']);
$theme_settings = $this->config('system.theme');
foreach (['bartik', 'seven', 'stark'] as $default_theme) {
// Change the default theme.
$theme_settings->set('default', $default_theme)->save();
Angie Byron
committed
\Drupal::service('router.builder')->rebuild();
catch
committed
// For each installed theme, go to its block page and test the redirects.
foreach (['bartik', 'seven', 'stark'] as $theme) {
// Test that adding a block from the 'place blocks' form sends you to the
// block configure form.
$path = $theme == $default_theme ? 'admin/structure/block' : "admin/structure/block/list/$theme";
$this->drupalGet($path);
$this->clickLink('Place block');
$this->clickLink(t('Add custom block'));
// The seven theme has markup inside the link, we cannot use clickLink().
if ($default_theme == 'seven') {
$options = $theme != $default_theme ? ['query' => ['theme' => $theme]] : [];
$this->assertLinkByHref(\Drupal::url('block_content.add_form', ['block_content_type' => 'foo'], $options));
$this->drupalGet('block/add/foo', $options);
}
else {
$this->clickLink('foo');
}
// Create a new block.
$edit = ['info[0][value]' => $this->randomMachineName(8)];
$this->drupalPostForm(NULL, $edit, t('Save'));
$blocks = $storage->loadByProperties(['info' => $edit['info[0][value]']]);
if (!empty($blocks)) {
$block = reset($blocks);
$this->assertUrl(\Drupal::url('block.admin_add', ['plugin_id' => 'block_content:' . $block->uuid(), 'theme' => $theme], ['absolute' => TRUE]));
$this->drupalPostForm(NULL, ['region' => 'content'], t('Save block'));
$this->assertUrl(\Drupal::url('block.admin_display_theme', ['theme' => $theme], ['absolute' => TRUE, 'query' => ['block-placement' => Html::getClass($edit['info[0][value]'])]]));
}
else {
$this->fail('Could not load created block.');
}
}
}
// Test that adding a block from the 'custom blocks list' doesn't send you
// to the block configure form.
$this->drupalGet('admin/structure/block/block-content');
$this->clickLink(t('Add custom block'));
$this->clickLink('foo');
$edit = ['info[0][value]' => $this->randomMachineName(8)];
$this->drupalPostForm(NULL, $edit, t('Save'));
$blocks = $storage->loadByProperties(['info' => $edit['info[0][value]']]);
if (!empty($blocks)) {
$this->assertUrl(\Drupal::url('entity.block_content.collection', [], ['absolute' => TRUE]));
}
else {
$this->fail('Could not load created block.');
}
}