You are here

public function ChecklistapiModuleTest::testChecklistapiSortArray in Checklist API 8

Tests checklistapi_sort_array().

File

tests/src/Unit/ChecklistapiModuleTest.php, line 23

Class

ChecklistapiModuleTest
Tests the functions in checklistapi.module.

Namespace

Drupal\Tests\checklistapi\Unit

Code

public function testChecklistapiSortArray() {
  $input = [
    '#title' => 'Checklist API test',
    '#path' => 'admin/config/development/checklistapi-test',
    // @codingStandardsIgnoreLine
    '#description' => 'A test checklist.',
    '#help' => '<p>This is a test checklist.</p>',
    'group_two' => [
      '#title' => 'Group two',
    ],
    'group_one' => [
      '#title' => 'Group one',
      // @codingStandardsIgnoreLine
      '#description' => '<p>Group one description.</p>',
      '#weight' => -1,
      'item_three' => [
        '#title' => 'Item three',
        '#weight' => 1,
      ],
      'item_one' => [
        '#title' => 'Item one',
        // @codingStandardsIgnoreLine
        '#description' => 'Item one description',
        '#weight' => -1,
        'link_three' => [
          '#text' => 'Link three',
          '#url' => Url::fromUri('http://example.com/three'),
          '#weight' => 3,
        ],
        'link_two' => [
          '#text' => 'Link two',
          '#url' => Url::fromUri('http://example.com/two'),
          '#weight' => 2,
        ],
        'link_one' => [
          '#text' => 'Link one',
          '#url' => Url::fromUri('http://example.com/one'),
          '#weight' => 1,
        ],
      ],
      'item_two' => [
        '#title' => 'Item two',
      ],
    ],
    'group_four' => [
      '#title' => 'Group four',
      '#weight' => 1,
    ],
    'group_three' => [
      '#title' => 'Group three',
    ],
  ];
  $output = checklistapi_sort_array($input);
  $this
    ->assertEquals(0, $output['group_two']['#weight'], 'Failed to supply a default for omitted element weight.');
  $this
    ->assertEquals(0, $output['group_three']['#weight'], 'Failed to supply a default in place of invalid element weight.');
  $this
    ->assertEquals(-1, $output['group_one']['#weight'], 'Failed to retain a valid element weight.');
  $this
    ->assertEquals([
    'group_one',
    'group_two',
    'group_three',
    'group_four',
  ], Element::children($output), 'Failed to sort elements by weight.');
  $this
    ->assertEquals([
    'link_one',
    'link_two',
    'link_three',
  ], Element::children($output['group_one']['item_one']), 'Failed to recurse through element descendants.');
}