You are here

protected function SearchLanguageTest::setUp in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/search/src/Tests/SearchLanguageTest.php \Drupal\search\Tests\SearchLanguageTest::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/SearchLanguageTest.php, line 34
Contains \Drupal\search\Tests\SearchLanguageTest.

Class

SearchLanguageTest
Tests advanced search with different languages added.

Namespace

Drupal\search\Tests

Code

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

  // Create and login user.
  $test_user = $this
    ->drupalCreateUser(array(
    '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 = 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 Spanish title',
      'type' => 'page',
      'body' => array(
        array(
          'value' => $this
            ->randomMachineName(32),
          'format' => $default_format,
        ),
      ),
      'langcode' => 'es',
    ),
    array(
      'title' => 'Third node en',
      'type' => 'page',
      'body' => array(
        array(
          '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', array(
    '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', array(
    '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();
  search_update_totals();
}