View source
<?php
namespace Drupal\block_content\Tests;
class BlockContentListTest extends BlockContentTestBase {
public static $modules = array(
'block',
'block_content',
'config_translation',
);
public function testListing() {
$this
->drupalLogin($this
->drupalCreateUser(array(
'administer blocks',
'translate configuration',
)));
$this
->drupalGet('admin/structure/block/block-content');
$this
->assertTitle(t('Custom block library') . ' | Drupal');
$element = $this
->xpath('//div[@class="layout-content"]//table');
$this
->assertTrue($element, 'Configuration entity list table found.');
$elements = $this
->xpath('//div[@class="layout-content"]//table/thead/tr/th');
$this
->assertEqual(count($elements), 2, 'Correct number of table header cells found.');
$expected_items = array(
t('Block description'),
t('Operations'),
);
foreach ($elements as $key => $element) {
$this
->assertEqual($element[0], $expected_items[$key]);
}
$label = 'Antelope';
$new_label = 'Albatross';
$link_text = t('Add custom block');
$this
->assertLink($link_text);
$this
->clickLink($link_text);
$this
->assertResponse(200);
$edit = array();
$edit['info[0][value]'] = $label;
$edit['body[0][value]'] = $this
->randomMachineName(16);
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertFieldByXpath('//td', $label, 'Label found for added block.');
$elements = $this
->xpath('//div[@class="layout-content"]//table/tbody/tr[@class="odd"]/td');
$this
->assertEqual(count($elements), 2, 'Correct number of table row cells found.');
$this
->assertIdentical((string) $elements[0], $label);
$blocks = $this->container
->get('entity.manager')
->getStorage('block_content')
->loadByProperties(array(
'info' => $label,
));
$block = reset($blocks);
if (!empty($block)) {
$this
->assertLinkByHref('block/' . $block
->id());
$this
->clickLink(t('Edit'));
$this
->assertResponse(200);
$this
->assertTitle(strip_tags(t('Edit custom block %label', array(
'%label' => $label,
)) . ' | Drupal'));
$edit = array(
'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', $new_label, 'Label found for updated custom block.');
$this
->assertLinkByHref('block/' . $block
->id() . '/delete');
$delete_text = t('Delete');
$this
->clickLink($delete_text);
$this
->assertResponse(200);
$this
->assertTitle(strip_tags(t('Are you sure you want to delete the custom block %label?', array(
'%label' => $new_label,
)) . ' | Drupal'));
$this
->drupalPostForm(NULL, array(), $delete_text);
$this
->assertNoFieldByXpath('//td', $new_label, 'No label found for deleted custom block.');
$this
->assertText(t('There is no Custom block yet.'));
}
}