You are here

function ConfigEntityListTest::testList in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/config/src/Tests/ConfigEntityListTest.php \Drupal\config\Tests\ConfigEntityListTest::testList()

Tests entity list builder methods.

File

core/modules/config/src/Tests/ConfigEntityListTest.php, line 42
Contains \Drupal\config\Tests\ConfigEntityListTest.

Class

ConfigEntityListTest
Tests the listing of configuration entities.

Namespace

Drupal\config\Tests

Code

function testList() {
  $controller = \Drupal::entityManager()
    ->getListBuilder('config_test');

  // Test getStorage() method.
  $this
    ->assertTrue($controller
    ->getStorage() instanceof EntityStorageInterface, 'EntityStorage 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.
  $expected_operations = array(
    'edit' => array(
      'title' => t('Edit'),
      'weight' => 10,
      'url' => $entity
        ->urlInfo(),
    ),
    'disable' => array(
      'title' => t('Disable'),
      'weight' => 40,
      'url' => $entity
        ->urlInfo('disable'),
    ),
    'delete' => array(
      'title' => t('Delete'),
      'weight' => 100,
      'url' => $entity
        ->urlInfo('delete-form'),
    ),
  );
  $actual_operations = $controller
    ->getOperations($entity);

  // Sort the operations to normalize link order.
  uasort($actual_operations, array(
    'Drupal\\Component\\Utility\\SortArray',
    'sortByWeightElement',
  ));
  $this
    ->assertEqual($expected_operations, $actual_operations, 'The operations are identical.');

  // Test buildHeader() method.
  $expected_items = array(
    'label' => 'Label',
    'id' => 'Machine name',
    'operations' => 'Operations',
  );
  $actual_items = $controller
    ->buildHeader();
  $this
    ->assertEqual($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
    ->assertEqual($expected_items, $actual_items, 'Return value from buildRow matches expected.');

  // Test sorting.
  $storage = $controller
    ->getStorage();
  $entity = $storage
    ->create(array(
    'id' => 'alpha',
    'label' => 'Alpha',
    'weight' => 1,
  ));
  $entity
    ->save();
  $entity = $storage
    ->create(array(
    'id' => 'omega',
    'label' => 'Omega',
    'weight' => 1,
  ));
  $entity
    ->save();
  $entity = $storage
    ->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('entity.manager')
    ->getListBuilder('config_test_no_status');
  $list = $controller
    ->load();
  $entity = $list['default'];

  // Test getOperations() method.
  $expected_operations = array(
    'edit' => array(
      'title' => t('Edit'),
      'weight' => 10,
      'url' => $entity
        ->urlInfo(),
    ),
    'delete' => array(
      'title' => t('Delete'),
      'weight' => 100,
      'url' => $entity
        ->urlInfo('delete-form'),
    ),
  );
  $actual_operations = $controller
    ->getOperations($entity);

  // Sort the operations to normalize link order.
  uasort($actual_operations, array(
    'Drupal\\Component\\Utility\\SortArray',
    'sortByWeightElement',
  ));
  $this
    ->assertEqual($expected_operations, $actual_operations, 'The operations are identical.');
}