View source
<?php
namespace Drupal\Tests\config\Functional;
use Drupal\Core\Routing\RedirectDestinationTrait;
use Drupal\config_test\Entity\ConfigTest;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Tests\BrowserTestBase;
class ConfigEntityListTest extends BrowserTestBase {
use RedirectDestinationTrait;
protected static $modules = [
'block',
'config_test',
];
protected $defaultTheme = 'classy';
protected function setUp() : void {
parent::setUp();
\Drupal::entityTypeManager()
->getStorage('config_test')
->load('override')
->delete();
$this
->drupalPlaceBlock('local_actions_block');
}
public function testList() {
$controller = \Drupal::entityTypeManager()
->getListBuilder('config_test');
$this
->assertInstanceOf(EntityStorageInterface::class, $controller
->getStorage());
$list = $controller
->load();
$this
->assertCount(1, $list, '1 ConfigTest entity found.');
$entity = $list['dotted.default'];
$this
->assertInstanceOf(ConfigTest::class, $entity);
$expected_operations = [
'edit' => [
'title' => t('Edit'),
'weight' => 10,
'url' => $entity
->toUrl()
->setOption('query', $this
->getRedirectDestination()
->getAsArray()),
],
'disable' => [
'title' => t('Disable'),
'weight' => 40,
'url' => $entity
->toUrl('disable')
->setOption('query', $this
->getRedirectDestination()
->getAsArray()),
],
'delete' => [
'title' => t('Delete'),
'weight' => 100,
'url' => $entity
->toUrl('delete-form')
->setOption('query', $this
->getRedirectDestination()
->getAsArray()),
],
];
$actual_operations = $controller
->getOperations($entity);
uasort($actual_operations, [
'Drupal\\Component\\Utility\\SortArray',
'sortByWeightElement',
]);
$this
->assertEquals($expected_operations, $actual_operations, 'The operations are identical.');
$expected_items = [
'label' => 'Label',
'id' => 'Machine name',
'operations' => 'Operations',
];
$actual_items = $controller
->buildHeader();
$this
->assertEquals($expected_items, $actual_items, 'Return value from buildHeader matches expected.');
$build_operations = $controller
->buildOperations($entity);
$expected_items = [
'label' => 'Default',
'id' => 'dotted.default',
'operations' => [
'data' => $build_operations,
],
];
$actual_items = $controller
->buildRow($entity);
$this
->assertEquals($expected_items, $actual_items, 'Return value from buildRow matches expected.');
$storage = $controller
->getStorage();
$entity = $storage
->create([
'id' => 'alpha',
'label' => 'Alpha',
'weight' => 1,
]);
$entity
->save();
$entity = $storage
->create([
'id' => 'omega',
'label' => 'Omega',
'weight' => 1,
]);
$entity
->save();
$entity = $storage
->create([
'id' => 'beta',
'label' => 'Beta',
'weight' => 0,
]);
$entity
->save();
$list = $controller
->load();
$this
->assertSame([
'beta',
'dotted.default',
'alpha',
'omega',
], array_keys($list));
$controller = $this->container
->get('entity_type.manager')
->getListBuilder('config_test_no_status');
$list = $controller
->load();
$entity = $list['default'];
$expected_operations = [
'edit' => [
'title' => t('Edit'),
'weight' => 10,
'url' => $entity
->toUrl()
->setOption('query', $this
->getRedirectDestination()
->getAsArray()),
],
'delete' => [
'title' => t('Delete'),
'weight' => 100,
'url' => $entity
->toUrl('delete-form')
->setOption('query', $this
->getRedirectDestination()
->getAsArray()),
],
];
$actual_operations = $controller
->getOperations($entity);
uasort($actual_operations, [
'Drupal\\Component\\Utility\\SortArray',
'sortByWeightElement',
]);
$this
->assertEquals($expected_operations, $actual_operations, 'The operations are identical.');
}
public function testListUI() {
$this
->drupalLogin($this
->drupalCreateUser([
'access administration pages',
'administer site configuration',
]));
$this
->drupalGet('admin/structure/config_test');
$this
->assertSession()
->titleEquals('Test configuration | Drupal');
$this
->assertSession()
->elementsCount('xpath', '//div[@class="layout-content"]//table', 1);
$this
->assertSession()
->elementsCount('xpath', '//div[@class="layout-content"]//table/thead/tr/th', 3);
$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');
$this
->assertSession()
->elementsCount('xpath', '//div[@class="layout-content"]//table/tbody/tr[@class="odd"]/td', 3);
$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');
$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');
$this
->assertTrue(\Drupal::state()
->get('config_entity_sort'), 'ConfigTest::sort() was called.');
$this
->assertSession()
->elementExists('xpath', '//td[text() = "Antelope"]');
$this
->assertSession()
->elementExists('xpath', '//td[text() = "antelope"]');
$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');
$this
->assertSession()
->elementExists('xpath', '//td[text() = "Albatross"]');
$this
->assertSession()
->elementExists('xpath', '//td[text() = "albatross"]');
$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');
$this
->assertSession()
->elementNotExists('xpath', '//td[text() = "Albatross"]');
$this
->assertSession()
->elementNotExists('xpath', '//td[text() = "albatross"]');
$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');
$this
->assertSession()
->elementNotExists('xpath', '//td[text() = "Default"]');
$this
->assertSession()
->elementNotExists('xpath', '//td[text() = "dotted.default"]');
$this
->assertSession()
->pageTextContains('There are no test configuration entities yet.');
}
public function testPager() {
$this
->drupalLogin($this
->drupalCreateUser([
'administer site configuration',
]));
$storage = \Drupal::service('entity_type.manager')
->getListBuilder('config_test')
->getStorage();
for ($i = 1; $i < 52; $i++) {
$storage
->create([
'id' => str_pad($i, 2, '0', STR_PAD_LEFT),
'label' => 'Test config entity ' . $i,
'weight' => $i,
'protected_property' => $i,
])
->save();
}
$this
->drupalGet('admin/structure/config_test');
$this
->assertSession()
->pageTextContains('Test config entity 50');
$this
->assertSession()
->responseNotContains('Test config entity 51');
$this
->clickLink('Page 2');
$this
->assertSession()
->responseNotContains('Test config entity 50');
$this
->assertSession()
->responseContains('dotted.default');
$this
->assertSession()
->pageTextContains('Test config entity 51');
}
}