You are here

public function BookMultilingualTest::testMultilingualBookManager in Drupal 9

Tests various book manager methods return correct translations.

@dataProvider langcodesProvider

File

core/modules/book/tests/src/Kernel/BookMultilingualTest.php, line 171

Class

BookMultilingualTest
Tests multilingual books.

Namespace

Drupal\Tests\book\Kernel

Code

public function testMultilingualBookManager(string $langcode) {
  $this
    ->setCurrentLanguage($langcode);

  /** @var \Drupal\book\BookManagerInterface $bm */
  $bm = $this->container
    ->get('book.manager');
  $books = $bm
    ->getAllBooks();
  $this
    ->assertNotEmpty($books);
  foreach ($books as $book) {
    $bid = $book['bid'];
    $build = $bm
      ->bookTreeOutput($bm
      ->bookTreeAllData($bid));
    $items = $build['#items'];
    $this
      ->assertBookItemIsCorrectlyTranslated($items[$bid], $langcode);
    $this
      ->assertBookItemIsCorrectlyTranslated($items[$bid]['below'][$bid + 1], $langcode);
    $this
      ->assertBookItemIsCorrectlyTranslated($items[$bid]['below'][$bid + 1]['below'][$bid + 3], $langcode);
    $this
      ->assertBookItemIsCorrectlyTranslated($items[$bid]['below'][$bid + 1]['below'][$bid + 4], $langcode);
    $this
      ->assertBookItemIsCorrectlyTranslated($items[$bid]['below'][$bid + 2], $langcode);
    $this
      ->assertBookItemIsCorrectlyTranslated($items[$bid]['below'][$bid + 2]['below'][$bid + 5], $langcode);
    $this
      ->assertBookItemIsCorrectlyTranslated($items[$bid]['below'][$bid + 2]['below'][$bid + 6], $langcode);
    $toc = $bm
      ->getTableOfContents($bid, 4);

    // Root entry does not have an indent.
    $this
      ->assertToCEntryIsCorrectlyTranslated($toc, $langcode, $bid, '');

    // The direct children of the root have one indent.
    $this
      ->assertToCEntryIsCorrectlyTranslated($toc, $langcode, $bid + 1, '--');
    $this
      ->assertToCEntryIsCorrectlyTranslated($toc, $langcode, $bid + 2, '--');

    // Their children have two indents.
    $this
      ->assertToCEntryIsCorrectlyTranslated($toc, $langcode, $bid + 3, '----');
    $this
      ->assertToCEntryIsCorrectlyTranslated($toc, $langcode, $bid + 4, '----');
    $this
      ->assertToCEntryIsCorrectlyTranslated($toc, $langcode, $bid + 5, '----');
    $this
      ->assertToCEntryIsCorrectlyTranslated($toc, $langcode, $bid + 6, '----');

    // $bid might be a string.
    $this
      ->assertSame([
      $bid + 0,
      $bid + 1,
      $bid + 3,
      $bid + 4,
      $bid + 2,
      $bid + 5,
      $bid + 6,
    ], array_keys($toc));
  }
}