function ConfigEntityListTest::testList

Same name in other branches
  1. 9 core/modules/config/tests/src/Functional/ConfigEntityListTest.php \Drupal\Tests\config\Functional\ConfigEntityListTest::testList()
  2. 8.9.x core/modules/config/tests/src/Functional/ConfigEntityListTest.php \Drupal\Tests\config\Functional\ConfigEntityListTest::testList()
  3. 10 core/modules/config/tests/src/Functional/ConfigEntityListTest.php \Drupal\Tests\config\Functional\ConfigEntityListTest::testList()

Tests entity list builder methods.

File

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

Class

ConfigEntityListTest
Tests the listing of configuration entities.

Namespace

Drupal\Tests\config\Functional

Code

public function testList() : void {
    $controller = \Drupal::entityTypeManager()->getListBuilder('config_test');
    // Test getStorage() method.
    $this->assertInstanceOf(EntityStorageInterface::class, $controller->getStorage());
    // Get a list of ConfigTest entities and confirm that it contains the
    // ConfigTest entity provided by the config_test module.
    // @see config_test.dynamic.dotted.default.yml
    $list = $controller->load();
    $this->assertCount(1, $list, '1 ConfigTest entity found.');
    $entity = $list['dotted.default'];
    $this->assertInstanceOf(ConfigTest::class, $entity);
    // Test getOperations() method.
    $edit_url = $entity->toUrl()
        ->setOption('query', $this->getRedirectDestination()
        ->getAsArray());
    $edit_url->setOption('attributes', [
        'aria-label' => 'Edit ' . $entity->label(),
    ]);
    $delete_url = $entity->toUrl('delete-form')
        ->setOption('query', $this->getRedirectDestination()
        ->getAsArray());
    $delete_url->setOption('attributes', [
        'aria-label' => 'Delete ' . $entity->label(),
    ]);
    $expected_operations = [
        'edit' => [
            'title' => 'Edit',
            'weight' => 10,
            'url' => $edit_url,
        ],
        'disable' => [
            'title' => 'Disable',
            'weight' => 40,
            'url' => $entity->toUrl('disable')
                ->setOption('query', $this->getRedirectDestination()
                ->getAsArray()),
        ],
        'delete' => [
            'title' => 'Delete',
            'weight' => 100,
            'attributes' => [
                'class' => [
                    'use-ajax',
                ],
                'data-dialog-type' => 'modal',
                'data-dialog-options' => Json::encode([
                    'width' => 880,
                ]),
            ],
            'url' => $delete_url,
        ],
    ];
    $actual_operations = $controller->getOperations($entity);
    // Sort the operations to normalize link order.
    uasort($actual_operations, [
        'Drupal\\Component\\Utility\\SortArray',
        'sortByWeightElement',
    ]);
    $this->assertEquals($expected_operations, $actual_operations, 'The operations are identical.');
    // Test buildHeader() method.
    $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.');
    // Test buildRow() method.
    $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.');
    // Test sorting.
    $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));
    // Test that config entities that do not support status, do not have
    // enable/disable operations.
    $controller = $this->container
        ->get('entity_type.manager')
        ->getListBuilder('config_test_no_status');
    $list = $controller->load();
    $entity = $list['default'];
    // Test getOperations() method.
    $edit_url = $entity->toUrl()
        ->setOption('query', $this->getRedirectDestination()
        ->getAsArray());
    $edit_url->setOption('attributes', [
        'aria-label' => 'Edit ' . $entity->label(),
    ]);
    $delete_url = $entity->toUrl('delete-form')
        ->setOption('query', $this->getRedirectDestination()
        ->getAsArray());
    $delete_url->setOption('attributes', [
        'aria-label' => 'Delete ' . $entity->label(),
    ]);
    $expected_operations = [
        'edit' => [
            'title' => 'Edit',
            'weight' => 10,
            'url' => $edit_url,
        ],
        'delete' => [
            'title' => 'Delete',
            'weight' => 100,
            'attributes' => [
                'class' => [
                    'use-ajax',
                ],
                'data-dialog-type' => 'modal',
                'data-dialog-options' => Json::encode([
                    'width' => 880,
                ]),
            ],
            'url' => $delete_url,
        ],
    ];
    $actual_operations = $controller->getOperations($entity);
    // Sort the operations to normalize link order.
    uasort($actual_operations, [
        'Drupal\\Component\\Utility\\SortArray',
        'sortByWeightElement',
    ]);
    $this->assertEquals($expected_operations, $actual_operations, 'The operations are identical.');
    // Test getOperations when label doesn't exist.
    $entity->set('label', '');
    $entity->save();
    $edit_url = $entity->toUrl()
        ->setOption('query', $this->getRedirectDestination()
        ->getAsArray());
    $edit_url->setOption('attributes', [
        'aria-label' => 'Edit ' . $entity->bundle() . ' ' . $entity->id(),
    ]);
    $delete_url = $entity->toUrl('delete-form')
        ->setOption('query', $this->getRedirectDestination()
        ->getAsArray());
    $delete_url->setOption('attributes', [
        'aria-label' => 'Delete ' . $entity->bundle() . ' ' . $entity->id(),
    ]);
    $expected_operations = [
        'edit' => [
            'title' => 'Edit',
            'weight' => 10,
            'url' => $edit_url,
        ],
        'delete' => [
            'title' => 'Delete',
            'weight' => 100,
            'attributes' => [
                'class' => [
                    'use-ajax',
                ],
                'data-dialog-type' => 'modal',
                'data-dialog-options' => Json::encode([
                    'width' => 880,
                ]),
            ],
            'url' => $delete_url,
        ],
    ];
    $actual_operations = $controller->getOperations($entity);
    // Sort the operations to normalize link order.
    uasort($actual_operations, [
        'Drupal\\Component\\Utility\\SortArray',
        'sortByWeightElement',
    ]);
    $this->assertEquals($expected_operations, $actual_operations, 'The operations are identical.');
}

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