Newer
Older
Alex Pott
committed
<?php
namespace Drupal\Tests\block_content\Functional;
Alex Pott
committed
Alex Bronstein
committed
use Drupal\block_content\Entity\BlockContent;
Alex Pott
committed
/**
* Tests the listing of custom blocks.
*
* Tests the fallback block content list when Views is disabled.
*
* @group block_content
* @see \Drupal\block\BlockContentListBuilder
* @see \Drupal\block_content\Tests\BlockContentListViewsTest
Alex Pott
committed
*/
class BlockContentListTest extends BlockContentTestBase {
Alex Pott
committed
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['block', 'block_content', 'config_translation'];
Alex Pott
committed
/**
* Tests the custom block listing page.
*/
public function testListing() {
$this->drupalLogin($this->drupalCreateUser(['administer blocks', 'translate configuration']));
$this->drupalGet('admin/structure/block/block-content');
Alex Pott
committed
// Test for the page title.
Dries Buytaert
committed
$this->assertTitle(t('Custom block library') . ' | Drupal');
Alex Pott
committed
// Test for the table.
Angie Byron
committed
$element = $this->xpath('//div[@class="layout-content"]//table');
Alex Pott
committed
$this->assertTrue($element, 'Configuration entity list table found.');
// Test the table header.
Angie Byron
committed
$elements = $this->xpath('//div[@class="layout-content"]//table/thead/tr/th');
Alex Pott
committed
$this->assertEqual(count($elements), 2, 'Correct number of table header cells found.');
// Test the contents of each th cell.
$expected_items = [t('Block description'), t('Operations')];
Alex Pott
committed
foreach ($elements as $key => $element) {
$this->assertEqual($element->getText(), $expected_items[$key]);
Alex Pott
committed
}
$label = 'Antelope';
$new_label = 'Albatross';
// Add a new entity using the operations link.
$link_text = t('Add custom block');
$this->assertLink($link_text);
$this->clickLink($link_text);
$this->assertResponse(200);
$edit = [];
catch
committed
$edit['info[0][value]'] = $label;
Alex Pott
committed
$edit['body[0][value]'] = $this->randomMachineName(16);
$this->drupalPostForm(NULL, $edit, t('Save'));
Alex Pott
committed
// Confirm that once the user returns to the listing, the text of the label
// (versus elsewhere on the page).
$this->assertFieldByXpath('//td', $label, 'Label found for added block.');
// Check the number of table row cells.
Angie Byron
committed
$elements = $this->xpath('//div[@class="layout-content"]//table/tbody/tr[@class="odd"]/td');
Alex Pott
committed
$this->assertEqual(count($elements), 2, 'Correct number of table row cells found.');
// Check the contents of each row cell. The first cell contains the label,
// the second contains the machine name, and the third contains the
// operations list.
$this->assertIdentical($elements[0]->getText(), $label);
Alex Pott
committed
// Edit the entity using the operations link.
$blocks = $this->container
->get('entity.manager')
->getStorage('block_content')
->loadByProperties(['info' => $label]);
Alex Pott
committed
$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', ['%label' => $label]) . ' | Drupal'));
$edit = ['info[0][value]' => $new_label];
$this->drupalPostForm(NULL, $edit, t('Save'));
Alex Pott
committed
}
else {
$this->fail('Did not find Albatross block in the database.');
}
// Confirm that once the user returns to the listing, the text of the label
// (versus elsewhere on the page).
$this->assertFieldByXpath('//td', $new_label, 'Label found for updated custom block.');
// Delete the added entity using the operations link.
$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?', ['%label' => $new_label]) . ' | Drupal'));
$this->drupalPostForm(NULL, [], $delete_text);
Alex Pott
committed
// Verify that the text of the label and machine name does not appear in
// the list (though it may appear elsewhere on the page).
$this->assertNoFieldByXpath('//td', $new_label, 'No label found for deleted custom block.');
// Confirm that the empty text is displayed.
Alex Pott
committed
$this->assertText(t('There are no custom blocks yet.'));
Alex Bronstein
committed
$block_content = BlockContent::create([
'info' => 'Non-reusable block',
'type' => 'basic',
'reusable' => FALSE,
]);
$block_content->save();
$this->drupalGet('admin/structure/block/block-content');
// Confirm that the empty text is displayed.
$this->assertSession()->pageTextContains('There are no custom blocks yet.');
// Confirm the non-reusable block is not on the page.
$this->assertSession()->pageTextNotContains('Non-reusable block');