You are here

public function ConfigEntityListTest::testList 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::testList()

Tests entity list builder methods.

File

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

Class

ConfigEntityListTest
Tests the listing of configuration entities.

Namespace

Drupal\Tests\config\Functional

Code

public function testList() {
  $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.
  $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);

  // 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.
  $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);

  // 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.');
}