function ConfigEntityListTest::testList

Tests entity list controller methods.

File

drupal/core/modules/config/lib/Drupal/config/Tests/ConfigEntityListTest.php, line 37
Definition of Drupal\config\Tests\ConfigEntityListTest.

Class

ConfigEntityListTest
Tests the listing of configuration entities.

Namespace

Drupal\config\Tests

Code

function testList() {
  $controller = $this->container
    ->get('plugin.manager.entity')
    ->getListController('config_test');

  // Test getStorageController() method.
  $this
    ->assertTrue($controller
    ->getStorageController() instanceof EntityStorageControllerInterface, 'EntityStorageController instance in storage.');

  // 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
    ->assertEqual(count($list), 1, '1 ConfigTest entity found.');
  $entity = $list['dotted.default'];
  $this
    ->assertTrue(!empty($entity), '"Default" ConfigTest entity ID found.');
  $this
    ->assertTrue($entity instanceof ConfigTest, '"Default" ConfigTest entity is an instance of ConfigTest.');

  // Test getOperations() method.
  $uri = $entity
    ->uri();
  $expected_operations = array(
    'edit' => array(
      'title' => t('Edit'),
      'href' => $uri['path'] . '/edit',
      'options' => $uri['options'],
      'weight' => 10,
    ),
    'disable' => array(
      'title' => t('Disable'),
      'href' => $uri['path'] . '/disable',
      'options' => $uri['options'],
      'weight' => 20,
    ),
    'delete' => array(
      'title' => t('Delete'),
      'href' => $uri['path'] . '/delete',
      'options' => $uri['options'],
      'weight' => 100,
    ),
  );
  $actual_operations = $controller
    ->getOperations($entity);

  // Sort the operations to normalize link order.
  uasort($actual_operations, 'drupal_sort_weight');
  $this
    ->assertIdentical($expected_operations, $actual_operations);

  // Test buildHeader() method.
  $expected_items = array(
    'label' => 'Label',
    'id' => 'Machine name',
    'operations' => 'Operations',
  );
  $actual_items = $controller
    ->buildHeader();
  $this
    ->assertIdentical($expected_items, $actual_items, 'Return value from buildHeader matches expected.');

  // Test buildRow() method.
  $build_operations = $controller
    ->buildOperations($entity);
  $expected_items = array(
    'label' => 'Default',
    'id' => 'dotted.default',
    'operations' => array(
      'data' => $build_operations,
    ),
  );
  $actual_items = $controller
    ->buildRow($entity);
  $this
    ->assertIdentical($expected_items, $actual_items, 'Return value from buildRow matches expected.');

  // Test sorting.
  $storage_controller = $controller
    ->getStorageController();
  $entity = $storage_controller
    ->create(array(
    'id' => 'alpha',
    'label' => 'Alpha',
    'weight' => 1,
  ));
  $entity
    ->save();
  $entity = $storage_controller
    ->create(array(
    'id' => 'omega',
    'label' => 'Omega',
    'weight' => 1,
  ));
  $entity
    ->save();
  $entity = $storage_controller
    ->create(array(
    'id' => 'beta',
    'label' => 'Beta',
    'weight' => 0,
  ));
  $entity
    ->save();
  $list = $controller
    ->load();
  $this
    ->assertIdentical(array_keys($list), array(
    'beta',
    'dotted.default',
    'alpha',
    'omega',
  ));

  // Test that config entities that do not support status, do not have
  // enable/disable operations.
  $controller = $this->container
    ->get('plugin.manager.entity')
    ->getListController('config_test_no_status');
  $list = $controller
    ->load();
  $entity = $list['default'];

  // Test getOperations() method.
  $uri = $entity
    ->uri();
  $expected_operations = array(
    'edit' => array(
      'title' => t('Edit'),
      'href' => $uri['path'] . '/edit',
      'options' => $uri['options'],
      'weight' => 10,
    ),
    'delete' => array(
      'title' => t('Delete'),
      'href' => $uri['path'] . '/delete',
      'options' => $uri['options'],
      'weight' => 100,
    ),
  );
  $actual_operations = $controller
    ->getOperations($entity);

  // Sort the operations to normalize link order.
  uasort($actual_operations, 'drupal_sort_weight');
  $this
    ->assertIdentical($expected_operations, $actual_operations);
}