You are here

public function TocTest::testIndex in TOC API 8

Tests parsing headers and creating a table of contents index.

See also

Toc::getIndex()

File

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

Class

TocTest
Tests TOC API formatter.

Namespace

Drupal\Tests\toc_api\Unit

Code

public function testIndex() {

  // Check default index. This covers type, tag, level, indent, keys, parent,
  // child, id, and title.
  $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([
    '1.0.0' => [
      'type' => 'decimal',
      'tag' => 'h2',
      'level' => 2,
      'key' => '1.0.0',
      'keys' => [
        'h2' => 1,
        'h3' => 0,
        'h4' => 0,
      ],
      'indent' => 0,
      'path' => '1',
      'number' => 1,
      'value' => '1',
      'parent' => NULL,
      'children' => [
        '1.1.0' => '1.1.0',
      ],
      'id' => 'header-2',
      'title' => 'header 2',
    ],
    '1.1.0' => [
      'type' => 'decimal',
      'tag' => 'h3',
      'level' => 3,
      'key' => '1.1.0',
      'keys' => [
        'h2' => 1,
        'h3' => 1,
        'h4' => 0,
      ],
      'indent' => 1,
      'path' => '1.1',
      'number' => 1,
      'value' => '1',
      'parent' => '1.0.0',
      'children' => [
        '1.1.1' => '1.1.1',
        '1.1.2' => '1.1.2',
      ],
      'id' => 'header-3',
      'title' => 'header 3',
    ],
    '1.1.1' => [
      'type' => 'decimal',
      'tag' => 'h4',
      'level' => 4,
      'key' => '1.1.1',
      'keys' => [
        'h2' => 1,
        'h3' => 1,
        'h4' => 1,
      ],
      'indent' => 2,
      'path' => '1.1.1',
      'number' => 1,
      'value' => '1',
      'parent' => '1.1.0',
      'children' => [],
      'id' => 'header-4',
      'title' => 'header 4',
    ],
    '1.1.2' => [
      'type' => 'decimal',
      'tag' => 'h4',
      'level' => 4,
      'key' => '1.1.2',
      'keys' => [
        'h2' => 1,
        'h3' => 1,
        'h4' => 2,
      ],
      'indent' => 2,
      'path' => '1.1.2',
      'number' => 2,
      'value' => '2',
      'parent' => '1.1.0',
      'children' => [],
      'id' => 'header-4-01',
      'title' => 'header 4',
    ],
    '2.0.0' => [
      'type' => 'decimal',
      'tag' => 'h2',
      'level' => 2,
      'key' => '2.0.0',
      'keys' => [
        'h2' => 2,
        'h3' => 0,
        'h4' => 0,
      ],
      'indent' => 0,
      'path' => '2',
      'number' => 2,
      'value' => '2',
      'parent' => NULL,
      'children' => [],
      'id' => 'header-2-01',
      'title' => 'header 2',
    ],
  ], $toc
    ->getIndex());

  // Check custom options. This covers type, value, and path.
  $options = [
    '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([
    '1.0.0' => [
      'type' => 'decimal',
      'path' => '1',
      'value' => '1',
    ],
    '1.1.0' => [
      'type' => 'lower-alpha',
      'path' => '1.a',
      'value' => 'a',
    ],
    '1.1.1' => [
      'type' => 'lower-roman',
      'path' => '1.a.i',
      'value' => 'i',
    ],
    '1.1.2' => [
      'type' => 'lower-roman',
      'path' => '1.a.ii',
      'value' => 'ii',
    ],
    '2.0.0' => [
      'type' => 'decimal',
      'path' => '2',
      'value' => '2',
    ],
  ], $toc
    ->getIndex());

  // 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([
    '1.0.0' => [
      'path' => '1.0.0',
    ],
    '1.1.0' => [
      'path' => '1.a.0',
    ],
    '1.1.1' => [
      'path' => '1.a.i',
    ],
    '1.1.2' => [
      'path' => '1.a.ii',
    ],
    '2.0.0' => [
      'path' => '2.0.0',
    ],
  ], $toc
    ->getIndex());

  // Check ids by keys.
  $options = [
    'number_path_truncate' => FALSE,
    'header_id' => 'key',
    '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([
    '1.0.0' => [
      'path' => '1.0.0',
      'id' => 'section-1.0.0',
    ],
    '1.1.0' => [
      'path' => '1.a.0',
      'id' => 'section-1.1.0',
    ],
    '1.1.1' => [
      'path' => '1.a.i',
      'id' => 'section-1.1.1',
    ],
    '1.1.2' => [
      'path' => '1.a.ii',
      'id' => 'section-1.1.2',
    ],
    '2.0.0' => [
      'path' => '2.0.0',
      'id' => 'section-2.0.0',
    ],
  ], $toc
    ->getIndex());

  // Check ids by path with prefix.
  $options = [
    'number_path_truncate' => FALSE,
    'header_id' => 'number_path',
    'header_id_prefix' => 'header',
    '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([
    '1.0.0' => [
      'path' => '1.0.0',
      'id' => 'header-1.0.0',
    ],
    '1.1.0' => [
      'id' => 'header-1.a.0',
    ],
    '1.1.1' => [
      'id' => 'header-1.a.i',
    ],
    '1.1.2' => [
      'id' => 'header-1.a.ii',
    ],
    '2.0.0' => [
      'path' => '2.0.0',
      'id' => 'header-2.0.0',
    ],
  ], $toc
    ->getIndex());

  // Check existing ids.
  $toc = new Toc('<h2>header 2</h2><h3 id="three">header 3</h3><h4 id="four">header 4</h4><h4 id="four">header 4</h4><h2>header 2</h2>', []);
  $this
    ->assertArraySubset([
    '1.0.0' => [
      'id' => 'header-2',
    ],
    '1.1.0' => [
      'id' => 'three',
    ],
    '1.1.1' => [
      'id' => 'four',
    ],
    '1.1.2' => [
      'id' => 'four-01',
    ],
    '2.0.0' => [
      'id' => 'header-2-01',
    ],
  ], $toc
    ->getIndex());

  // Check missing parent.
  $toc = new Toc('<h2>header 2</h2><h4>header 4</h4><h4>header 4</h4><h2>header 2</h2>', []);
  $this
    ->assertArraySubset([
    '1.0.0' => [
      'parent' => NULL,
      'children' => [
        '1.0.1' => '1.0.1',
        '1.0.2' => '1.0.2',
      ],
    ],
    '1.0.1' => [
      'parent' => '1.0.0',
      'children' => [],
    ],
    '1.0.2' => [
      'parent' => '1.0.0',
      'children' => [],
    ],
    '2.0.0' => [
      'parent' => NULL,
      'children' => [],
    ],
  ], $toc
    ->getIndex());

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