/core/modules/block_content/src/Tests/BlockContentListTest.php

https://gitlab.com/reasonat/test8 · PHP · 109 lines · 57 code · 17 blank · 35 comment · 2 complexity · 6d935fe14be0ad909e6a24f692d9033f MD5 · raw file

  1. <?php
  2. namespace Drupal\block_content\Tests;
  3. /**
  4. * Tests the listing of custom blocks.
  5. *
  6. * Tests the fallback block content list when Views is disabled.
  7. *
  8. * @group block_content
  9. * @see \Drupal\block\BlockContentListBuilder
  10. * @see \Drupal\block_content\Tests\BlockContentListViewsTest
  11. */
  12. class BlockContentListTest extends BlockContentTestBase {
  13. /**
  14. * Modules to enable.
  15. *
  16. * @var array
  17. */
  18. public static $modules = array('block', 'block_content', 'config_translation');
  19. /**
  20. * Tests the custom block listing page.
  21. */
  22. public function testListing() {
  23. $this->drupalLogin($this->drupalCreateUser(array('administer blocks', 'translate configuration')));
  24. $this->drupalGet('admin/structure/block/block-content');
  25. // Test for the page title.
  26. $this->assertTitle(t('Custom block library') . ' | Drupal');
  27. // Test for the table.
  28. $element = $this->xpath('//div[@class="layout-content"]//table');
  29. $this->assertTrue($element, 'Configuration entity list table found.');
  30. // Test the table header.
  31. $elements = $this->xpath('//div[@class="layout-content"]//table/thead/tr/th');
  32. $this->assertEqual(count($elements), 2, 'Correct number of table header cells found.');
  33. // Test the contents of each th cell.
  34. $expected_items = array(t('Block description'), t('Operations'));
  35. foreach ($elements as $key => $element) {
  36. $this->assertEqual($element[0], $expected_items[$key]);
  37. }
  38. $label = 'Antelope';
  39. $new_label = 'Albatross';
  40. // Add a new entity using the operations link.
  41. $link_text = t('Add custom block');
  42. $this->assertLink($link_text);
  43. $this->clickLink($link_text);
  44. $this->assertResponse(200);
  45. $edit = array();
  46. $edit['info[0][value]'] = $label;
  47. $edit['body[0][value]'] = $this->randomMachineName(16);
  48. $this->drupalPostForm(NULL, $edit, t('Save'));
  49. // Confirm that once the user returns to the listing, the text of the label
  50. // (versus elsewhere on the page).
  51. $this->assertFieldByXpath('//td', $label, 'Label found for added block.');
  52. // Check the number of table row cells.
  53. $elements = $this->xpath('//div[@class="layout-content"]//table/tbody/tr[@class="odd"]/td');
  54. $this->assertEqual(count($elements), 2, 'Correct number of table row cells found.');
  55. // Check the contents of each row cell. The first cell contains the label,
  56. // the second contains the machine name, and the third contains the
  57. // operations list.
  58. $this->assertIdentical((string) $elements[0], $label);
  59. // Edit the entity using the operations link.
  60. $blocks = $this->container
  61. ->get('entity.manager')
  62. ->getStorage('block_content')
  63. ->loadByProperties(array('info' => $label));
  64. $block = reset($blocks);
  65. if (!empty($block)) {
  66. $this->assertLinkByHref('block/' . $block->id());
  67. $this->clickLink(t('Edit'));
  68. $this->assertResponse(200);
  69. $this->assertTitle(strip_tags(t('Edit custom block %label', array('%label' => $label)) . ' | Drupal'));
  70. $edit = array('info[0][value]' => $new_label);
  71. $this->drupalPostForm(NULL, $edit, t('Save'));
  72. }
  73. else {
  74. $this->fail('Did not find Albatross block in the database.');
  75. }
  76. // Confirm that once the user returns to the listing, the text of the label
  77. // (versus elsewhere on the page).
  78. $this->assertFieldByXpath('//td', $new_label, 'Label found for updated custom block.');
  79. // Delete the added entity using the operations link.
  80. $this->assertLinkByHref('block/' . $block->id() . '/delete');
  81. $delete_text = t('Delete');
  82. $this->clickLink($delete_text);
  83. $this->assertResponse(200);
  84. $this->assertTitle(strip_tags(t('Are you sure you want to delete the custom block %label?', array('%label' => $new_label)) . ' | Drupal'));
  85. $this->drupalPostForm(NULL, array(), $delete_text);
  86. // Verify that the text of the label and machine name does not appear in
  87. // the list (though it may appear elsewhere on the page).
  88. $this->assertNoFieldByXpath('//td', $new_label, 'No label found for deleted custom block.');
  89. // Confirm that the empty text is displayed.
  90. $this->assertText(t('There is no Custom block yet.'));
  91. }
  92. }