View source
<?php
namespace Drupal\Tests\block_content\Functional;
use Drupal\block_content\Entity\BlockContent;
class BlockContentListViewsTest extends BlockContentTestBase {
public 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
->assertTitle('Custom block library | Drupal');
$this
->assertFieldByName('info');
$this
->assertFieldByName('type');
$element = $this
->xpath('//div[@class="layout-content"]//table');
$this
->assertNotEmpty($element, 'Views table found.');
$elements = $this
->xpath('//div[@class="layout-content"]//table/thead/tr/th');
$this
->assertCount(4, $elements, 'Correct number of table header cells found.');
$expected_items = [
'Block description',
'Block type',
'Updated Sort ascending',
'Operations',
];
foreach ($elements as $key => $element) {
if ($element
->find('xpath', 'a')) {
$this
->assertIdentical(trim($element
->find('xpath', 'a')
->getText()), $expected_items[$key]);
}
else {
$this
->assertIdentical(trim($element
->getText()), $expected_items[$key]);
}
}
$label = 'Antelope';
$new_label = 'Albatross';
$link_text = t('Add custom block');
$this
->assertSession()
->linkExists($link_text);
$this
->clickLink($link_text);
$this
->assertSession()
->statusCodeEquals(200);
$edit = [];
$edit['info[0][value]'] = $label;
$edit['body[0][value]'] = $this
->randomMachineName(16);
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertFieldByXpath('//td/a', $label, 'Label found for added block.');
$elements = $this
->xpath('//div[@class="layout-content"]//table/tbody/tr/td');
$this
->assertCount(4, $elements, 'Correct number of table row cells found.');
$this
->assertIdentical($elements[0]
->find('xpath', 'a')
->getText(), $label);
$blocks = $this->container
->get('entity_type.manager')
->getStorage('block_content')
->loadByProperties([
'info' => $label,
]);
$block = reset($blocks);
if (!empty($block)) {
$this
->assertLinkByHref('block/' . $block
->id());
$this
->clickLink(t('Edit'));
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertTitle("Edit custom block {$label} | Drupal");
$edit = [
'info[0][value]' => $new_label,
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
}
else {
$this
->fail('Did not find Albatross block in the database.');
}
$this
->assertFieldByXpath('//td/a', $new_label, 'Label found for updated custom block.');
$this
->assertLinkByHref('block/' . $block
->id() . '/delete');
$delete_text = t('Delete');
$this
->clickLink($delete_text);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertTitle("Are you sure you want to delete the custom block {$new_label}? | Drupal");
$this
->drupalPostForm(NULL, [], $delete_text);
$this
->assertNoFieldByXpath('//td', $new_label, 'No label found for deleted custom block.');
$this
->assertText('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');
}
}