You are here

protected function SearchMultilingualEntityTest::setUp in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/search/src/Tests/SearchMultilingualEntityTest.php \Drupal\search\Tests\SearchMultilingualEntityTest::setUp()

Sets up a Drupal site for running functional and integration tests.

Installs Drupal with the installation profile specified in \Drupal\simpletest\WebTestBase::$profile into the prefixed database.

Afterwards, installs any additional modules specified in the static \Drupal\simpletest\WebTestBase::$modules property of each class in the class hierarchy.

After installation all caches are flushed and several configuration values are reset to the values of the parent site executing the test, since the default values may be incompatible with the environment in which tests are being executed.

Overrides SearchTestBase::setUp

File

core/modules/search/src/Tests/SearchMultilingualEntityTest.php, line 36
Contains \Drupal\search\Tests\SearchMultilingualEntityTest.

Class

SearchMultilingualEntityTest
Tests entities with multilingual fields.

Namespace

Drupal\search\Tests

Code

protected function setUp() {
  parent::setUp();

  // Create a user who can administer search, do searches, see the status
  // report, and administer cron. Log in.
  $user = $this
    ->drupalCreateUser(array(
    '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 = array(
    array(
      'title' => 'First node en',
      'type' => 'page',
      'body' => array(
        array(
          'value' => $this
            ->randomMachineName(32),
          'format' => $default_format,
        ),
      ),
      'langcode' => 'en',
    ),
    array(
      'title' => 'Second node this is the English title',
      'type' => 'page',
      'body' => array(
        array(
          'value' => $this
            ->randomMachineName(32),
          'format' => $default_format,
        ),
      ),
      'langcode' => 'en',
    ),
    array(
      'title' => 'Third node en',
      'type' => 'page',
      'body' => array(
        array(
          '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.
    array(),
    array(),
    array(),
    array(),
    array(),
  );
  $this->searchableNodes = array();
  foreach ($nodes as $setting) {
    $this->searchableNodes[] = $this
      ->drupalCreateNode($setting);
  }

  // Add a single translation to the second node.
  $translation = $this->searchableNodes[1]
    ->addTranslation('hu', array(
    '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', array(
    'title' => 'Third node this is the Hungarian title',
  ));
  $translation->body->value = $this
    ->randomMachineName(32);
  $translation = $this->searchableNodes[2]
    ->addTranslation('sv', array(
    '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');
}