BlockContentListTest.php 4.65 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\block_content\Functional;
4

5 6
use Drupal\block_content\Entity\BlockContent;

7 8 9
/**
 * Tests the listing of custom blocks.
 *
10 11
 * Tests the fallback block content list when Views is disabled.
 *
12
 * @group block_content
13
 * @see \Drupal\block\BlockContentListBuilder
14
 * @see \Drupal\block_content\Tests\BlockContentListViewsTest
15
 */
16
class BlockContentListTest extends BlockContentTestBase {
17 18 19 20 21 22

  /**
   * Modules to enable.
   *
   * @var array
   */
23
  protected static $modules = ['block', 'block_content', 'config_translation'];
24

25 26 27 28 29
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'classy';

30 31 32 33
  /**
   * Tests the custom block listing page.
   */
  public function testListing() {
34
    $this->drupalLogin($this->drupalCreateUser(['administer blocks', 'translate configuration']));
35
    $this->drupalGet('admin/structure/block/block-content');
36 37

    // Test for the page title.
38
    $this->assertTitle('Custom block library | Drupal');
39 40

    // Test for the table.
41
    $element = $this->xpath('//div[@class="layout-content"]//table');
42
    $this->assertNotEmpty($element, 'Configuration entity list table found.');
43 44

    // Test the table header.
45
    $elements = $this->xpath('//div[@class="layout-content"]//table/thead/tr/th');
46
    $this->assertCount(2, $elements, 'Correct number of table header cells found.');
47 48

    // Test the contents of each th cell.
49
    $expected_items = [t('Block description'), t('Operations')];
50
    foreach ($elements as $key => $element) {
51
      $this->assertEqual($element->getText(), $expected_items[$key]);
52 53 54 55 56 57 58 59
    }

    $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);
60
    $this->assertSession()->statusCodeEquals(200);
61
    $edit = [];
62
    $edit['info[0][value]'] = $label;
63
    $edit['body[0][value]'] = $this->randomMachineName(16);
64
    $this->drupalPostForm(NULL, $edit, t('Save'));
65 66 67 68 69 70

    // 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.
71
    $elements = $this->xpath('//div[@class="layout-content"]//table/tbody/tr[@class="odd"]/td');
72
    $this->assertCount(2, $elements, 'Correct number of table row cells found.');
73 74 75
    // 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.
76
    $this->assertIdentical($elements[0]->getText(), $label);
77 78 79

    // Edit the entity using the operations link.
    $blocks = $this->container
80
      ->get('entity_type.manager')
81
      ->getStorage('block_content')
82
      ->loadByProperties(['info' => $label]);
83 84 85 86
    $block = reset($blocks);
    if (!empty($block)) {
      $this->assertLinkByHref('block/' . $block->id());
      $this->clickLink(t('Edit'));
87
      $this->assertSession()->statusCodeEquals(200);
88
      $this->assertTitle("Edit custom block $label | Drupal");
89
      $edit = ['info[0][value]' => $new_label];
90
      $this->drupalPostForm(NULL, $edit, t('Save'));
91 92 93 94 95 96 97 98 99 100 101 102 103
    }
    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);
104
    $this->assertSession()->statusCodeEquals(200);
105
    $this->assertTitle("Are you sure you want to delete the custom block $new_label? | Drupal");
106
    $this->drupalPostForm(NULL, [], $delete_text);
107 108 109 110 111 112

    // 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.
113
    $this->assertText(t('There are no custom blocks yet.'));
114 115 116 117 118 119 120 121 122 123 124 125 126

    $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');
127 128 129
  }

}