You are here

public function ConfigEntityListTest::testListUI in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/config/tests/src/Functional/ConfigEntityListTest.php \Drupal\Tests\config\Functional\ConfigEntityListTest::testListUI()

Tests the listing UI.

File

core/modules/config/tests/src/Functional/ConfigEntityListTest.php, line 157

Class

ConfigEntityListTest
Tests the listing of configuration entities.

Namespace

Drupal\Tests\config\Functional

Code

public function testListUI() {

  // Log in as an administrative user to access the full menu trail.
  $this
    ->drupalLogin($this
    ->drupalCreateUser([
    'access administration pages',
    'administer site configuration',
  ]));

  // Get the list callback page.
  $this
    ->drupalGet('admin/structure/config_test');

  // Test for the page title.
  $this
    ->assertSession()
    ->titleEquals('Test configuration | Drupal');

  // Test for the table.
  $this
    ->assertSession()
    ->elementsCount('xpath', '//div[@class="layout-content"]//table', 1);

  // Test the table header.
  $this
    ->assertSession()
    ->elementsCount('xpath', '//div[@class="layout-content"]//table/thead/tr/th', 3);

  // Test the contents of each th cell.
  $this
    ->assertSession()
    ->elementTextEquals('xpath', '//div[@class="layout-content"]//table/thead/tr/th[1]', 'Label');
  $this
    ->assertSession()
    ->elementTextEquals('xpath', '//div[@class="layout-content"]//table/thead/tr/th[2]', 'Machine name');
  $this
    ->assertSession()
    ->elementTextEquals('xpath', '//div[@class="layout-content"]//table/thead/tr/th[3]', 'Operations');

  // Check the number of table row cells.
  $this
    ->assertSession()
    ->elementsCount('xpath', '//div[@class="layout-content"]//table/tbody/tr[@class="odd"]/td', 3);

  // 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
    ->assertSession()
    ->elementTextEquals('xpath', '//div[@class="layout-content"]//table/tbody/tr[@class="odd"]/td[1]', 'Default');
  $this
    ->assertSession()
    ->elementTextEquals('xpath', '//div[@class="layout-content"]//table/tbody/tr[@class="odd"]/td[2]', 'dotted.default');
  $this
    ->assertSession()
    ->elementExists('xpath', '//div[@class="layout-content"]//table/tbody/tr[@class="odd"]/td[3]//ul');

  // Add a new entity using the operations link.
  $this
    ->assertSession()
    ->linkExists('Add test configuration');
  $this
    ->clickLink('Add test configuration');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $edit = [
    'label' => 'Antelope',
    'id' => 'antelope',
    'weight' => 1,
  ];
  $this
    ->submitForm($edit, 'Save');

  // Ensure that the entity's sort method was called.
  $this
    ->assertTrue(\Drupal::state()
    ->get('config_entity_sort'), 'ConfigTest::sort() was called.');

  // Confirm that the user is returned to the listing, and verify that the
  // text of the label and machine name appears in the list (versus elsewhere
  // on the page).
  $this
    ->assertSession()
    ->elementExists('xpath', '//td[text() = "Antelope"]');
  $this
    ->assertSession()
    ->elementExists('xpath', '//td[text() = "antelope"]');

  // Edit the entity using the operations link.
  $this
    ->assertSession()
    ->linkByHrefExists('admin/structure/config_test/manage/antelope');
  $this
    ->clickLink('Edit', 1);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->titleEquals('Edit Antelope | Drupal');
  $edit = [
    'label' => 'Albatross',
    'id' => 'albatross',
  ];
  $this
    ->submitForm($edit, 'Save');

  // Confirm that the user is returned to the listing, and verify that the
  // text of the label and machine name appears in the list (versus elsewhere
  // on the page).
  $this
    ->assertSession()
    ->elementExists('xpath', '//td[text() = "Albatross"]');
  $this
    ->assertSession()
    ->elementExists('xpath', '//td[text() = "albatross"]');

  // Delete the added entity using the operations link.
  $this
    ->assertSession()
    ->linkByHrefExists('admin/structure/config_test/manage/albatross/delete');
  $this
    ->clickLink('Delete', 1);
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->titleEquals('Are you sure you want to delete the test configuration Albatross? | Drupal');
  $this
    ->submitForm([], 'Delete');

  // 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
    ->assertSession()
    ->elementNotExists('xpath', '//td[text() = "Albatross"]');
  $this
    ->assertSession()
    ->elementNotExists('xpath', '//td[text() = "albatross"]');

  // Delete the original entity using the operations link.
  $this
    ->clickLink('Delete');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->titleEquals('Are you sure you want to delete the test configuration Default? | Drupal');
  $this
    ->submitForm([], 'Delete');

  // 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
    ->assertSession()
    ->elementNotExists('xpath', '//td[text() = "Default"]');
  $this
    ->assertSession()
    ->elementNotExists('xpath', '//td[text() = "dotted.default"]');

  // Confirm that the empty text is displayed.
  $this
    ->assertSession()
    ->pageTextContains('There are no test configuration entities yet.');
}