You are here

protected function NodeLanguageTest::setUp in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/node/tests/src/Functional/Views/NodeLanguageTest.php \Drupal\Tests\node\Functional\Views\NodeLanguageTest::setUp()
  2. 10 core/modules/node/tests/src/Functional/Views/NodeLanguageTest.php \Drupal\Tests\node\Functional\Views\NodeLanguageTest::setUp()

Overrides NodeTestBase::setUp

File

core/modules/node/tests/src/Functional/Views/NodeLanguageTest.php, line 46

Class

NodeLanguageTest
Tests node language fields, filters, and sorting.

Namespace

Drupal\Tests\node\Functional\Views

Code

protected function setUp($import_test_views = TRUE) {
  parent::setUp(FALSE);

  // Create Page content type.
  if ($this->profile != 'standard') {
    $this
      ->drupalCreateContentType([
      'type' => 'page',
      'name' => 'Basic page',
    ]);
    ViewTestData::createTestViews(get_class($this), [
      'node_test_views',
    ]);
  }

  // Add two new languages.
  ConfigurableLanguage::createFromLangcode('fr')
    ->save();
  ConfigurableLanguage::createFromLangcode('es')
    ->save();

  // Make the body field translatable. The title is already translatable by
  // definition.
  $field_storage = FieldStorageConfig::loadByName('node', 'body');
  $field_storage
    ->setTranslatable(TRUE);
  $field_storage
    ->save();

  // Set up node titles. They should not include the words "French",
  // "English", or "Spanish", as there is a language field in the view
  // that prints out those words.
  $this->nodeTitles = [
    LanguageInterface::LANGCODE_NOT_SPECIFIED => [
      'First node und',
    ],
    'es' => [
      'Primero nodo es',
      'Segundo nodo es',
      'Tercera nodo es',
    ],
    'en' => [
      'First node en',
      'Second node en',
    ],
    'fr' => [
      'Premier nœud fr',
    ],
  ];

  // Create nodes with translations.
  foreach ($this->nodeTitles['es'] as $index => $title) {
    $node = $this
      ->drupalCreateNode([
      'title' => $title,
      'langcode' => 'es',
      'type' => 'page',
      'promote' => 1,
    ]);
    foreach ([
      'en',
      'fr',
    ] as $langcode) {
      if (isset($this->nodeTitles[$langcode][$index])) {
        $translation = $node
          ->addTranslation($langcode, [
          'title' => $this->nodeTitles[$langcode][$index],
        ]);
        $translation->body->value = $this
          ->randomMachineName(32);
      }
    }
    $node
      ->save();
  }

  // Create non-translatable nodes.
  foreach ($this->nodeTitles[LanguageInterface::LANGCODE_NOT_SPECIFIED] as $index => $title) {
    $node = $this
      ->drupalCreateNode([
      'title' => $title,
      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
      'type' => 'page',
      'promote' => 1,
    ]);
    $node->body->value = $this
      ->randomMachineName(32);
    $node
      ->save();
  }
  $this->container
    ->get('router.builder')
    ->rebuild();
  $user = $this
    ->drupalCreateUser([
    'access content overview',
    'access content',
  ]);
  $this
    ->drupalLogin($user);
}