View source
<?php
namespace Drupal\Tests\block_content\Functional;
use Drupal\block_content\Entity\BlockContent;
class BlockContentListViewsTest extends BlockContentTestBase {
protected static $modules = [
'block',
'block_content',
'config_translation',
'views',
];
protected $defaultTheme = 'stark';
public function testListing() {
$this
->drupalLogin($this
->drupalCreateUser([
'administer blocks',
'translate configuration',
]));
$this
->drupalGet('admin/structure/block/block-content');
$this
->assertSession()
->titleEquals('Custom block library | Drupal');
$this
->assertSession()
->fieldExists('info');
$this
->assertSession()
->fieldExists('type');
$this
->assertSession()
->elementExists('xpath', '//div[@class="layout-content"]//table');
$this
->assertSession()
->elementsCount('xpath', '//div[@class="layout-content"]//table/thead/tr/th', 4);
$this
->assertSession()
->elementTextEquals('xpath', '//div[@class="layout-content"]//table/thead/tr/th[1]', 'Block description');
$this
->assertSession()
->elementTextEquals('xpath', '//div[@class="layout-content"]//table/thead/tr/th[2]', 'Block type');
$this
->assertSession()
->elementTextEquals('xpath', '//div[@class="layout-content"]//table/thead/tr/th[3]', 'Updated Sort ascending');
$this
->assertSession()
->elementTextEquals('xpath', '//div[@class="layout-content"]//table/thead/tr/th[4]', 'Operations');
$label = 'Antelope';
$new_label = 'Albatross';
$this
->clickLink('Add custom block');
$this
->assertSession()
->statusCodeEquals(200);
$edit = [];
$edit['info[0][value]'] = $label;
$edit['body[0][value]'] = $this
->randomMachineName(16);
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->elementTextContains('xpath', '//td/a', $label);
$this
->assertSession()
->elementsCount('xpath', '//div[@class="layout-content"]//table/tbody/tr/td', 4);
$this
->assertSession()
->elementTextEquals('xpath', '//div[@class="layout-content"]//table/tbody/tr/td/a', $label);
$blocks = $this->container
->get('entity_type.manager')
->getStorage('block_content')
->loadByProperties([
'info' => $label,
]);
$block = reset($blocks);
if (!empty($block)) {
$this
->assertSession()
->linkByHrefExists('block/' . $block
->id());
$this
->clickLink('Edit');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->titleEquals("Edit custom block {$label} | Drupal");
$edit = [
'info[0][value]' => $new_label,
];
$this
->submitForm($edit, 'Save');
}
else {
$this
->fail('Did not find Albatross block in the database.');
}
$this
->assertSession()
->elementTextContains('xpath', '//td/a', $new_label);
$this
->assertSession()
->linkByHrefExists('block/' . $block
->id() . '/delete');
$this
->clickLink('Delete');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->titleEquals("Are you sure you want to delete the custom block {$new_label}? | Drupal");
$this
->submitForm([], 'Delete');
$this
->assertSession()
->elementTextNotContains('xpath', '//td', $new_label);
$this
->assertSession()
->pageTextContains('There are no custom blocks available.');
$this
->assertSession()
->linkExists('custom block');
$block_content = BlockContent::create([
'info' => 'Non-reusable block',
'type' => 'basic',
'reusable' => FALSE,
]);
$block_content
->save();
$this
->drupalGet('admin/structure/block/block-content');
$this
->assertSession()
->pageTextContains('There are no custom blocks available.');
$this
->assertSession()
->pageTextNotContains('Non-reusable block');
}
}