function ConfigEntityListTest::testListUI

Same name and namespace in other branches
  1. 11.x core/modules/config/tests/src/Functional/ConfigEntityListTest.php \Drupal\Tests\config\Functional\ConfigEntityListTest::testListUI()
  2. 10 core/modules/config/tests/src/Functional/ConfigEntityListTest.php \Drupal\Tests\config\Functional\ConfigEntityListTest::testListUI()
  3. 9 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->assertTitle('Test configuration | Drupal');
  // Test for the table.
  $element = $this->xpath('//div[@class="layout-content"]//table');
  $this->assertCount(1, $element, 'Configuration entity list table found.');
  // Test the table header.
  $elements = $this->xpath('//div[@class="layout-content"]//table/thead/tr/th');
  $this->assertCount(3, $elements, 'Correct number of table header cells found.');
  // Test the contents of each th cell.
  $expected_items = [
    'Label',
    'Machine name',
    'Operations',
  ];
  foreach ($elements as $key => $element) {
    $this->assertIdentical($element->getText(), $expected_items[$key]);
  }
  // Check the number of table row cells.
  $elements = $this->xpath('//div[@class="layout-content"]//table/tbody/tr[@class="odd"]/td');
  $this->assertCount(3, $elements, '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(), 'Default');
  $this->assertIdentical($elements[1]->getText(), 'dotted.default');
  $this->assertNotEmpty($elements[2]->find('xpath', '//ul'), 'Operations list found.');
  // 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->drupalPostForm(NULL, $edit, t('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->assertFieldByXpath('//td', 'Antelope', "Label found for added 'Antelope' entity.");
  $this->assertFieldByXpath('//td', 'antelope', "Machine name found for added 'Antelope' entity.");
  // Edit the entity using the operations link.
  $this->assertLinkByHref('admin/structure/config_test/manage/antelope');
  $this->clickLink('Edit', 1);
  $this->assertSession()
    ->statusCodeEquals(200);
  $this->assertTitle('Edit Antelope | Drupal');
  $edit = [
    'label' => 'Albatross',
    'id' => 'albatross',
  ];
  $this->drupalPostForm(NULL, $edit, t('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->assertFieldByXpath('//td', 'Albatross', "Label found for updated 'Albatross' entity.");
  $this->assertFieldByXpath('//td', 'albatross', "Machine name found for updated 'Albatross' entity.");
  // Delete the added entity using the operations link.
  $this->assertLinkByHref('admin/structure/config_test/manage/albatross/delete');
  $this->clickLink('Delete', 1);
  $this->assertSession()
    ->statusCodeEquals(200);
  $this->assertTitle('Are you sure you want to delete the test configuration Albatross? | Drupal');
  $this->drupalPostForm(NULL, [], t('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->assertNoFieldByXpath('//td', 'Albatross', "No label found for deleted 'Albatross' entity.");
  $this->assertNoFieldByXpath('//td', 'albatross', "No machine name found for deleted 'Albatross' entity.");
  // Delete the original entity using the operations link.
  $this->clickLink('Delete');
  $this->assertSession()
    ->statusCodeEquals(200);
  $this->assertTitle('Are you sure you want to delete the test configuration Default? | Drupal');
  $this->drupalPostForm(NULL, [], t('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->assertNoFieldByXpath('//td', 'Default', "No label found for deleted 'Default' entity.");
  $this->assertNoFieldByXpath('//td', 'dotted.default', "No machine name found for deleted 'Default' entity.");
  // Confirm that the empty text is displayed.
  $this->assertText('There are no test configuration entities yet.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.