function LinksetControllerMultiLingualTest::setUp

Same name in other branches
  1. 10 core/modules/system/tests/src/Functional/Menu/LinksetControllerMultiLingualTest.php \Drupal\Tests\system\Functional\Menu\LinksetControllerMultiLingualTest::setUp()

Test set up.

Installs necessary database schemas, then creates test content and menu items. The concept of this set up is to replicate a typical site's menus.

Throws

\Drupal\Core\Entity\EntityStorageException

Overrides BrowserTestBase::setUp

File

core/modules/system/tests/src/Functional/Menu/LinksetControllerMultiLingualTest.php, line 66

Class

LinksetControllerMultiLingualTest
Tests the behavior of the linkset controller in multilingual setup.

Namespace

Drupal\Tests\system\Functional\Menu

Code

public function setUp() : void {
    parent::setUp();
    // Create Basic page node type.
    $this->drupalCreateContentType([
        'type' => 'page',
        'name' => 'Basic page',
    ]);
    // Add some custom languages.
    foreach ([
        'aa',
        'bb',
        'cc',
        'dd',
    ] as $index => $language_code) {
        ConfigurableLanguage::create([
            'id' => $language_code,
            'label' => $this->randomMachineName(),
            'weight' => $index,
        ])
            ->save();
    }
    // Set up an admin user with appropriate permissions.
    $admin_user = $this->drupalCreateUser([
        'view own unpublished content',
        'administer languages',
        'administer content types',
        'access administration pages',
        'create page content',
        'edit own page content',
    ]);
    $this->drupalLogin($admin_user);
    $config = $this->config('language.types');
    $config->set('configurable', [
        LanguageInterface::TYPE_INTERFACE,
    ]);
    $config->set('negotiation.language_content.enabled', [
        LanguageNegotiationUrl::METHOD_ID => 1,
        LanguageNegotiationSelected::METHOD_ID => 1,
    ]);
    $config->save();
    \Drupal::configFactory()->getEditable('language.negotiation')
        ->set('url.prefixes.aa', 'aa')
        ->set('url.prefixes.bb', 'bb')
        ->set('url.prefixes.cc', 'cc')
        ->set('selected_langcode', 'dd')
        ->save();
    // Set default language code for content type page to 'dd'.
    ContentLanguageSettings::loadByEntityTypeBundle('node', 'page')->setDefaultLangcode('dd')
        ->setLanguageAlterable(TRUE)
        ->save();
    // Set default language code to for menu_link_content 'dd'.
    ContentLanguageSettings::loadByEntityTypeBundle('menu_link_content', 'menu_link_content')->setDefaultLangcode('dd')
        ->setLanguageAlterable(TRUE)
        ->save();
    $this->config('system.feature_flags')
        ->set('linkset_endpoint', TRUE)
        ->save(TRUE);
    // Using rebuildIfNeeded here to implicitly test that router is only rebuilt
    // when necessary.
    $this->rebuildIfNeeded();
    $this->drupalLogout();
    $permissions = [
        'view own unpublished content',
    ];
    $this->authorAccount = $this->setUpCurrentUser([
        'name' => 'author',
        'pass' => 'authorPass',
    ], $permissions);
    // Generate some data which we can test against.
    $home_page_link = $this->createMenuItem([
        'title' => 'Home',
        'description' => 'Links to the home page.',
        'link' => 'internal:/<front>',
        'weight' => 0,
        'menu_name' => 'main',
    ]);
    // Multilingual test.
    $multi_lingual_node = $this->createNode([
        'nid' => 1,
        'title' => 'A multi-lingual-node',
        'type' => 'page',
        'path' => '/multi-lingual-node',
    ]);
    $multi_lingual_menu_item = $this->createMenuItem([
        'title' => 'A multi-lingual-node',
        'link' => 'entity:node/' . (int) $multi_lingual_node->id(),
        'menu_name' => 'main',
        'weight' => $home_page_link->getWeight() + 1,
    ]);
    foreach ([
        'aa',
        'bb',
        'cc',
    ] as $language_code) {
        $multi_lingual_menu_item->addTranslation($language_code, [
            'title' => $language_code . '|' . 'A multi-lingual-node',
        ]);
        $multi_lingual_menu_item->save();
    }
    // Multilingual Menu item with missing language using `entity:` route.
    $multi_lingual_node = $this->createNode([
        'nid' => 2,
        'title' => 'A multi-lingual-node',
        'type' => 'page',
        'path' => '/multi-lingual-node-two',
    ]);
    $multi_lingual_menu_item = $this->createMenuItem([
        'title' => 'Second multi-lingual-node',
        'link' => 'entity:node/' . (int) $multi_lingual_node->id(),
        'menu_name' => 'main',
        'weight' => $home_page_link->getWeight() + 2,
    ]);
    foreach ([
        'aa',
        'bb',
    ] as $language_code) {
        $multi_lingual_menu_item->addTranslation($language_code, [
            'title' => $language_code . '|' . 'Second multi-lingual-node',
        ]);
        $multi_lingual_menu_item->save();
    }
    // Multilingual Menu item with missing language using `internal` route.
    $multi_lingual_node = $this->createNode([
        'nid' => 3,
        'title' => 'A multi-lingual-node',
        'type' => 'page',
        'path' => '/multi-lingual-node-three',
    ]);
    $multi_lingual_menu_item = $this->createMenuItem([
        'title' => 'Third multi-lingual-node',
        'link' => 'internal:/node/' . (int) $multi_lingual_node->id(),
        'menu_name' => 'main',
        'weight' => $home_page_link->getWeight() + 3,
    ]);
    foreach ([
        'aa',
        'bb',
    ] as $language_code) {
        $multi_lingual_menu_item->addTranslation($language_code, [
            'title' => $language_code . '|' . 'Third multi-lingual-node',
        ]);
        $multi_lingual_menu_item->save();
    }
    $this->httpKernel = $this->container
        ->get('http_kernel');
}

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