function SearchLanguageTest::setUp

Same name in other branches
  1. 8.9.x core/modules/search/tests/src/Functional/SearchLanguageTest.php \Drupal\Tests\search\Functional\SearchLanguageTest::setUp()
  2. 10 core/modules/search/tests/src/Functional/SearchLanguageTest.php \Drupal\Tests\search\Functional\SearchLanguageTest::setUp()
  3. 11.x core/modules/search/tests/src/Functional/SearchLanguageTest.php \Drupal\Tests\search\Functional\SearchLanguageTest::setUp()

Overrides BrowserTestBase::setUp

File

core/modules/search/tests/src/Functional/SearchLanguageTest.php, line 37

Class

SearchLanguageTest
Tests advanced search with different languages added.

Namespace

Drupal\Tests\search\Functional

Code

protected function setUp() : void {
    parent::setUp();
    $this->drupalCreateContentType([
        'type' => 'page',
        'name' => 'Basic page',
    ]);
    // Create and log in user.
    $test_user = $this->drupalCreateUser([
        'access content',
        'search content',
        'use advanced search',
        'administer nodes',
        'administer languages',
        'access administration pages',
        'administer site configuration',
    ]);
    $this->drupalLogin($test_user);
    // Add a new language.
    ConfigurableLanguage::createFromLangcode('es')->save();
    // Make the body field translatable. The title is already translatable by
    // definition. The parent class has already created the article and page
    // content types.
    $field_storage = FieldStorageConfig::loadByName('node', 'body');
    $field_storage->setTranslatable(TRUE);
    $field_storage->save();
    // Create a few page nodes with multilingual body values.
    $default_format = filter_default_format();
    $nodes = [
        [
            'title' => 'First node en',
            'type' => 'page',
            'body' => [
                [
                    'value' => $this->randomMachineName(32),
                    'format' => $default_format,
                ],
            ],
            'langcode' => 'en',
        ],
        [
            'title' => 'Second node this is the Spanish title',
            'type' => 'page',
            'body' => [
                [
                    'value' => $this->randomMachineName(32),
                    'format' => $default_format,
                ],
            ],
            'langcode' => 'es',
        ],
        [
            'title' => 'Third node en',
            'type' => 'page',
            'body' => [
                [
                    'value' => $this->randomMachineName(32),
                    'format' => $default_format,
                ],
            ],
            'langcode' => 'en',
        ],
    ];
    $this->searchableNodes = [];
    foreach ($nodes as $setting) {
        $this->searchableNodes[] = $this->drupalCreateNode($setting);
    }
    // Add English translation to the second node.
    $translation = $this->searchableNodes[1]
        ->addTranslation('en', [
        'title' => 'Second node en',
    ]);
    $translation->body->value = $this->randomMachineName(32);
    $this->searchableNodes[1]
        ->save();
    // Add Spanish translation to the third node.
    $translation = $this->searchableNodes[2]
        ->addTranslation('es', [
        'title' => 'Third node es',
    ]);
    $translation->body->value = $this->randomMachineName(32);
    $this->searchableNodes[2]
        ->save();
    // Update the index and then run the shutdown method.
    $plugin = $this->container
        ->get('plugin.manager.search')
        ->createInstance('node_search');
    $plugin->updateIndex();
}

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