function SearchMultilingualEntityTest::setUp

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

Overrides BrowserTestBase::setUp

File

core/modules/search/tests/src/Functional/SearchMultilingualEntityTest.php, line 48

Class

SearchMultilingualEntityTest
Tests entities with multilingual fields.

Namespace

Drupal\Tests\search\Functional

Code

protected function setUp() {
    parent::setUp();
    $this->drupalCreateContentType([
        'type' => 'page',
        'name' => 'Basic page',
    ]);
    // Create a user who can administer search, do searches, see the status
    // report, and administer cron. Log in.
    $user = $this->drupalCreateUser([
        'administer search',
        'search content',
        'use advanced search',
        'access content',
        'access site reports',
        'administer site configuration',
    ]);
    $this->drupalLogin($user);
    // Set up the search plugin.
    $this->plugin = $this->container
        ->get('plugin.manager.search')
        ->createInstance('node_search');
    // Check indexing counts before adding any nodes.
    $this->assertIndexCounts(0, 0, 'before adding nodes');
    $this->assertDatabaseCounts(0, 0, 'before adding nodes');
    // Add two new languages.
    ConfigurableLanguage::createFromLangcode('hu')->save();
    ConfigurableLanguage::createFromLangcode('sv')->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 English title',
            'type' => 'page',
            'body' => [
                [
                    'value' => $this->randomMachineName(32),
                    'format' => $default_format,
                ],
            ],
            'langcode' => 'en',
        ],
        [
            'title' => 'Third node en',
            'type' => 'page',
            'body' => [
                [
                    'value' => $this->randomMachineName(32),
                    'format' => $default_format,
                ],
            ],
            'langcode' => 'en',
        ],
        // After the third node, we don't care what the settings are. But we
        // need to have at least 5 to make sure the throttling is working
        // correctly. So, let's make 8 total.
[],
        [],
        [],
        [],
        [],
    ];
    $this->searchableNodes = [];
    foreach ($nodes as $setting) {
        $this->searchableNodes[] = $this->drupalCreateNode($setting);
    }
    // Add a single translation to the second node.
    $translation = $this->searchableNodes[1]
        ->addTranslation('hu', [
        'title' => 'Second node hu',
    ]);
    $translation->body->value = $this->randomMachineName(32);
    $this->searchableNodes[1]
        ->save();
    // Add two translations to the third node.
    $translation = $this->searchableNodes[2]
        ->addTranslation('hu', [
        'title' => 'Third node this is the Hungarian title',
    ]);
    $translation->body->value = $this->randomMachineName(32);
    $translation = $this->searchableNodes[2]
        ->addTranslation('sv', [
        'title' => 'Third node sv',
    ]);
    $translation->body->value = $this->randomMachineName(32);
    $this->searchableNodes[2]
        ->save();
    // Verify that we have 8 nodes left to do.
    $this->assertIndexCounts(8, 8, 'before updating the search index');
    $this->assertDatabaseCounts(0, 0, 'before updating the search index');
}

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