You are here

public function TocTest::testTree in TOC API 8

Tests converting table of contents index to hierarchical tree.

See also

Toc::getTree()

File

tests/src/Unit/TocTest.php, line 338
Contains \Drupal\Tests\toc_api\Unit\TocTest.

Class

TocTest
Tests TOC API formatter.

Namespace

Drupal\Tests\toc_api\Unit

Code

public function testTree() {

  // Check parent child relationship.
  $toc = new Toc('<h2>header 2</h2><h3>header 3</h3><h4>header 4</h4><h4>header 4</h4><h2>header 2</h2>', []);
  $this
    ->assertArraySubset([
    'title' => 'Table of Contents',
    'below_type' => 'decimal',
    'below' => [
      '1.0.0' => [
        'type' => 'decimal',
        'below_type' => 'decimal',
        'below' => [
          '1.1.0' => [
            'below_type' => 'decimal',
            'below' => [
              '1.1.1' => [
                'below_type' => '',
                'below' => [],
              ],
              '1.1.2' => [
                'below_type' => '',
                'below' => [],
              ],
            ],
          ],
        ],
      ],
      '2.0.0' => [
        'type' => 'decimal',
        'below_type' => '',
        'below' => [],
      ],
    ],
  ], $toc
    ->getTree());

  // Checkout below type
  // Check paths without truncation.
  $options = [
    'number_path_truncate' => FALSE,
    'headers' => [
      'h2' => [
        'number_type' => 'decimal',
      ],
      'h3' => [
        'number_type' => 'lower-alpha',
      ],
      'h4' => [
        'number_type' => 'lower-roman',
      ],
    ],
  ];
  $toc = new Toc('<h2>header 2</h2><h3>header 3</h3><h4>header 4</h4><h4>header 4</h4><h2>header 2</h2>', $options);
  $this
    ->assertArraySubset([
    'title' => 'Table of Contents',
    'below_type' => 'decimal',
    'below' => [
      '1.0.0' => [
        'type' => 'decimal',
        'below_type' => 'lower-alpha',
        'below' => [
          '1.1.0' => [
            'below_type' => 'lower-roman',
            'below' => [
              '1.1.1' => [
                'below_type' => '',
                'below' => [],
              ],
              '1.1.2' => [
                'below_type' => '',
                'below' => [],
              ],
            ],
          ],
        ],
      ],
    ],
  ], $toc
    ->getTree());

  // Check missing parent.
  $toc = new Toc('<h2>header 2</h2><h4>header 4</h4><h4>header 4</h4><h2>header 2</h2>', []);
  $this
    ->assertArraySubset([
    'below_type' => 'decimal',
    'below' => [
      '1.0.0' => [
        'below_type' => 'decimal',
        'below' => [
          '1.0.1' => [],
          '1.0.2' => [],
        ],
      ],
    ],
  ], $toc
    ->getTree());

  // $this->dumpArraySubset($toc->getTree(), '$toc->getTree()');
}