function BookMultilingualTest::setUp

Same name in other branches
  1. 10 core/modules/book/tests/src/Kernel/BookMultilingualTest.php \Drupal\Tests\book\Kernel\BookMultilingualTest::setUp()
  2. 11.x core/modules/book/tests/src/Kernel/BookMultilingualTest.php \Drupal\Tests\book\Kernel\BookMultilingualTest::setUp()

Overrides KernelTestBase::setUp

File

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

Class

BookMultilingualTest
Tests multilingual books.

Namespace

Drupal\Tests\book\Kernel

Code

protected function setUp() : void {
    parent::setUp();
    // Create the translation language.
    $this->installConfig([
        'language',
    ]);
    ConfigurableLanguage::create([
        'id' => self::LANGCODE,
    ])->save();
    // Set up language negotiation.
    $config = $this->config('language.types');
    $config->set('configurable', [
        LanguageInterface::TYPE_INTERFACE,
        LanguageInterface::TYPE_CONTENT,
    ]);
    // The language being tested should only be available as the content
    // language so subsequent tests catch errors where the interface language
    // is used instead of the content language. For this, the interface
    // language is set to the user language and ::setCurrentLanguage() will
    // set the user language to the language not being tested.
    $config->set('negotiation', [
        LanguageInterface::TYPE_INTERFACE => [
            'enabled' => [
                LanguageNegotiationUser::METHOD_ID => 0,
            ],
        ],
        LanguageInterface::TYPE_CONTENT => [
            'enabled' => [
                LanguageNegotiationUrl::METHOD_ID => 0,
            ],
        ],
    ]);
    $config->save();
    $config = $this->config('language.negotiation');
    $config->set('url.source', LanguageNegotiationUrl::CONFIG_DOMAIN);
    $config->set('url.domains', [
        'en' => 'en.book.test.domain',
        self::LANGCODE => self::LANGCODE . '.book.test.domain',
    ]);
    $config->save();
    $this->container
        ->get('kernel')
        ->rebuildContainer();
    $this->installEntitySchema('node');
    $this->installEntitySchema('user');
    $this->installSchema('book', [
        'book',
    ]);
    $this->installSchema('node', [
        'node_access',
    ]);
    $this->installSchema('system', [
        'sequences',
    ]);
    $this->installConfig([
        'node',
        'book',
        'field',
    ]);
    $node_type = NodeType::create([
        'type' => $this->randomMachineName(),
        'name' => $this->randomString(),
    ]);
    $node_type->save();
    $this->container
        ->get('content_translation.manager')
        ->setEnabled('node', $node_type->id(), TRUE);
    $book_config = $this->config('book.settings');
    $allowed_types = $book_config->get('allowed_types');
    $allowed_types[] = $node_type->id();
    $book_config->set('allowed_types', $allowed_types)
        ->save();
    // To test every possible combination of root-child / child-child, two
    // trees are needed. The first level below the root needs to have two
    // leaves and similarly a second level is needed with two-two leaves each:
    //
    //        1
    //      /   \
    //     /     \
    //    2       3
    //   / \     / \
    //  /   \   /   \
    // 4     5 6     7
    //
    // These are the actual node IDs, these are enforced as auto increment is
    // not reliable.
    //
    // Similarly, the second tree root is node 8, the first two leaves are
    // 9 and 10, the third level is 11, 12, 13, 14.
    for ($root = 1; $root <= 8; $root += 7) {
        for ($i = 0; $i <= 6; $i++) {
            
            /** @var \Drupal\node\NodeInterface $node */
            $node = Node::create([
                'title' => $this->randomString(),
                'type' => $node_type->id(),
            ]);
            $node->addTranslation(self::LANGCODE, [
                'title' => $this->randomString(),
            ]);
            switch ($i) {
                case 0:
                    $node->book['bid'] = 'new';
                    $node->book['pid'] = 0;
                    $node->book['depth'] = 1;
                    break;
                case 1:
                case 2:
                    $node->book['bid'] = $root;
                    $node->book['pid'] = $root;
                    $node->book['depth'] = 2;
                    break;
                case 3:
                case 4:
                    $node->book['bid'] = $root;
                    $node->book['pid'] = $root + 1;
                    $node->book['depth'] = 3;
                    break;
                case 5:
                case 6:
                    $node->book['bid'] = $root;
                    $node->book['pid'] = $root + 2;
                    $node->book['depth'] = 3;
                    break;
            }
            // This is necessary to make the table of contents consistent across
            // test runs.
            $node->book['weight'] = $i;
            $node->nid->value = $root + $i;
            $node->enforceIsNew();
            $node->save();
        }
    }
    \Drupal::currentUser()->setAccount($this->createUser([
        'access content',
    ]));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.