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;
public static $modules = [
'block',
'config_test',
];
protected $defaultTheme = 'classy';
protected function setUp() {
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
->assertIdentical(array_keys($list), [
'beta',
'dotted.default',
'alpha',
'omega',
]);
$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
->assertTitle('Test configuration | Drupal');
$element = $this
->xpath('//div[@class="layout-content"]//table');
$this
->assertCount(1, $element, 'Configuration entity list table found.');
$elements = $this
->xpath('//div[@class="layout-content"]//table/thead/tr/th');
$this
->assertCount(3, $elements, 'Correct number of table header cells found.');
$expected_items = [
'Label',
'Machine name',
'Operations',
];
foreach ($elements as $key => $element) {
$this
->assertIdentical($element
->getText(), $expected_items[$key]);
}
$elements = $this
->xpath('//div[@class="layout-content"]//table/tbody/tr[@class="odd"]/td');
$this
->assertCount(3, $elements, 'Correct number of table row cells found.');
$this
->assertIdentical($elements[0]
->getText(), 'Default');
$this
->assertIdentical($elements[1]
->getText(), 'dotted.default');
$this
->assertNotEmpty($elements[2]
->find('xpath', '//ul'), 'Operations list found.');
$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'));
$this
->assertTrue(\Drupal::state()
->get('config_entity_sort'), 'ConfigTest::sort() was called.');
$this
->assertFieldByXpath('//td', 'Antelope', "Label found for added 'Antelope' entity.");
$this
->assertFieldByXpath('//td', 'antelope', "Machine name found for added 'Antelope' entity.");
$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'));
$this
->assertFieldByXpath('//td', 'Albatross', "Label found for updated 'Albatross' entity.");
$this
->assertFieldByXpath('//td', 'albatross', "Machine name found for updated 'Albatross' entity.");
$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'));
$this
->assertNoFieldByXpath('//td', 'Albatross', "No label found for deleted 'Albatross' entity.");
$this
->assertNoFieldByXpath('//td', 'albatross', "No machine name found for deleted 'Albatross' entity.");
$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'));
$this
->assertNoFieldByXpath('//td', 'Default', "No label found for deleted 'Default' entity.");
$this
->assertNoFieldByXpath('//td', 'dotted.default', "No machine name found for deleted 'Default' entity.");
$this
->assertText('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
->assertRaw('Test config entity 50', 'Config entity 50 is shown.');
$this
->assertNoRaw('Test config entity 51', 'Config entity 51 is on the next page.');
$this
->clickLink(t('Page 2'));
$this
->assertNoRaw('Test config entity 50', 'Test config entity 50 is on the previous page.');
$this
->assertRaw('dotted.default', 'Default config entity appears on page 2.');
$this
->assertRaw('Test config entity 51', 'Test config entity 51 is on page 2.');
}
}