View source
<?php
namespace Drupal\block_content\Tests;
use Drupal\block_content\Entity\BlockContentType;
use Drupal\Component\Utility\Html;
class BlockContentTypeTest extends BlockContentTestBase {
public static $modules = array(
'field_ui',
);
protected $permissions = array(
'administer blocks',
'administer block_content fields',
);
protected $autoCreateBasicBlockType = FALSE;
protected function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('page_title_block');
}
public function testBlockContentTypeCreation() {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('block/add');
$this
->assertResponse(200);
$this
->assertText('You have not created any block types yet');
$this
->clickLink('block type creation page');
$edit = array(
'id' => 'foo',
'label' => 'title for foo',
);
$this
->drupalPostForm(NULL, $edit, t('Save'));
$block_type = BlockContentType::load('foo');
$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.');
$default_langcode = \Drupal::languageManager()
->getDefaultLanguage()
->getId();
$this
->assertEqual($block_type
->language()
->getId(), $default_langcode);
$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);
}
public function testBlockContentTypeEditing() {
$this
->createBlockContentType('basic', TRUE);
$this
->drupalLogin($this->adminUser);
$this
->createBlockContentType('other');
$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.');
$this
->drupalGet('block/add/basic');
$this
->assertRaw('Block description', 'Block info field was found.');
$this
->assertRaw('Body', 'Body field was found.');
$edit = array(
'label' => 'Bar',
);
$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'));
\Drupal::entityManager()
->clearCachedFieldDefinitions();
$this
->drupalGet('block/add');
$this
->assertRaw('Bar', 'New name was displayed.');
$this
->clickLink('Bar');
$this
->assertUrl(\Drupal::url('block_content.add_form', [
'block_content_type' => 'basic',
], [
'absolute' => TRUE,
]), [], 'Original machine name was used in URL.');
$this
->drupalPostForm('admin/structure/block/block-content/manage/basic/fields/block_content.basic.body/delete', array(), t('Delete'));
$this
->drupalPostForm('admin/structure/block/block-content/manage/basic', array(), t('Save'));
$this
->drupalGet('block/add/basic');
$this
->assertNoRaw('Body', 'Body field was not found.');
}
public function testBlockContentTypeDeletion() {
$this
->createBlockContentType('basic', TRUE);
$type = $this
->createBlockContentType('foo');
$this
->drupalLogin($this->adminUser);
$block = $this
->createBlockContent(FALSE, 'foo');
$this
->drupalGet('admin/structure/block/block-content/manage/' . $type
->id() . '/delete');
$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.', array(
'%label' => $type
->label(),
)), 'The block type will not be deleted until all blocks of that type are removed.');
$this
->assertNoText(t('This action cannot be undone.'), 'The block type deletion confirmation form is not available.');
$block
->delete();
$this
->drupalGet('admin/structure/block/block-content/manage/' . $type
->id() . '/delete');
$this
->assertRaw(t('Are you sure you want to delete the custom block type %type?', array(
'%type' => $type
->id(),
)), 'The block type is available for deletion.');
$this
->assertText(t('This action cannot be undone.'), 'The custom block type deletion confirmation form is available.');
}
public function testsBlockContentAddTypes() {
$this
->createBlockContentType('basic', TRUE);
$this
->drupalLogin($this->adminUser);
$type = $this
->createBlockContentType('foo');
$type = $this
->createBlockContentType('bar');
$storage = $this->container
->get('entity.manager')
->getStorage('block_content');
\Drupal::service('theme_handler')
->install([
'bartik',
'seven',
'stark',
]);
$theme_settings = $this
->config('system.theme');
foreach ([
'bartik',
'seven',
'stark',
] as $default_theme) {
$theme_settings
->set('default', $default_theme)
->save();
\Drupal::service('router.builder')
->rebuild();
foreach ([
'bartik',
'seven',
'stark',
] as $theme) {
$path = $theme == $default_theme ? 'admin/structure/block' : "admin/structure/block/list/{$theme}";
$this
->drupalGet($path);
$this
->clickLinkPartialName('Place block');
$this
->clickLink(t('Add custom block'));
if ($default_theme == 'seven') {
$options = $theme != $default_theme ? array(
'query' => array(
'theme' => $theme,
),
) : array();
$this
->assertLinkByHref(\Drupal::url('block_content.add_form', array(
'block_content_type' => 'foo',
), $options));
$this
->drupalGet('block/add/foo', $options);
}
else {
$this
->clickLink('foo');
}
$edit = array(
'info[0][value]' => $this
->randomMachineName(8),
);
$this
->drupalPostForm(NULL, $edit, t('Save'));
$blocks = $storage
->loadByProperties(array(
'info' => $edit['info[0][value]'],
));
if (!empty($blocks)) {
$block = reset($blocks);
$this
->assertUrl(\Drupal::url('block.admin_add', array(
'plugin_id' => 'block_content:' . $block
->uuid(),
'theme' => $theme,
), array(
'absolute' => TRUE,
)));
$this
->drupalPostForm(NULL, array(), t('Save block'));
$this
->assertUrl(\Drupal::url('block.admin_display_theme', array(
'theme' => $theme,
), array(
'absolute' => TRUE,
'query' => array(
'block-placement' => Html::getClass($edit['info[0][value]']),
),
)));
}
else {
$this
->fail('Could not load created block.');
}
}
}
$this
->drupalGet('admin/structure/block/block-content');
$this
->clickLink(t('Add custom block'));
$this
->clickLink('foo');
$edit = array(
'info[0][value]' => $this
->randomMachineName(8),
);
$this
->drupalPostForm(NULL, $edit, t('Save'));
$blocks = $storage
->loadByProperties(array(
'info' => $edit['info[0][value]'],
));
if (!empty($blocks)) {
$this
->assertUrl(\Drupal::url('entity.block_content.collection', array(), array(
'absolute' => TRUE,
)));
}
else {
$this
->fail('Could not load created block.');
}
}
}