You are here

protected function RowEntityRenderersTest::setUp in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/views/tests/src/Kernel/Entity/RowEntityRenderersTest.php \Drupal\Tests\views\Kernel\Entity\RowEntityRenderersTest::setUp()

Parameters

bool $import_test_views: Should the views specified on the test class be imported. If you need to setup some additional stuff, like fields, you need to call false and then call createTestViews for your own.

Overrides ViewsKernelTestBase::setUp

File

core/modules/views/tests/src/Kernel/Entity/RowEntityRenderersTest.php, line 75

Class

RowEntityRenderersTest
Tests the entity row renderers.

Namespace

Drupal\Tests\views\Kernel\Entity

Code

protected function setUp($import_test_views = TRUE) : void {
  parent::setUp();
  $this
    ->installEntitySchema('node');
  $this
    ->installEntitySchema('user');
  $this
    ->installSchema('node', [
    'node_access',
  ]);
  $this
    ->installConfig([
    'node',
    'language',
  ]);
  $this->langcodes = [
    \Drupal::languageManager()
      ->getDefaultLanguage()
      ->getId(),
  ];
  for ($i = 0; $i < 2; $i++) {
    $langcode = 'l' . $i;
    $this->langcodes[] = $langcode;
    ConfigurableLanguage::createFromLangcode($langcode)
      ->save();
  }
  $this->testAuthor = User::create([
    'name' => 'foo',
  ]);
  $this->testAuthor
    ->save();

  // Make sure we do not try to render non-existing user data.
  $node_type = NodeType::create([
    'type' => 'test',
  ]);
  $node_type
    ->setDisplaySubmitted(FALSE);
  $node_type
    ->save();
  $this->values = [];
  $this->ids = [];
  $controller = \Drupal::entityTypeManager()
    ->getStorage('node');
  $langcode_index = 0;
  for ($i = 0; $i < count($this->langcodes); $i++) {

    // Create a node with a different default language each time.
    $default_langcode = $this->langcodes[$langcode_index++];
    $node = $controller
      ->create([
      'type' => 'test',
      'uid' => $this->testAuthor
        ->id(),
      'langcode' => $default_langcode,
    ]);

    // Ensure the default language is processed first.
    $langcodes = array_merge([
      $default_langcode,
    ], array_diff($this->langcodes, [
      $default_langcode,
    ]));
    foreach ($langcodes as $langcode) {

      // Ensure we have a predictable result order.
      $this->values[$i][$langcode] = $i . '-' . $langcode . '-' . $this
        ->randomMachineName();
      if ($langcode != $default_langcode) {
        $node
          ->addTranslation($langcode, [
          'title' => $this->values[$i][$langcode],
        ]);
      }
      else {
        $node
          ->setTitle($this->values[$i][$langcode]);
      }
      $node
        ->save();
      $this->ids[] = [
        'nid' => $node
          ->id(),
        'uid' => $this->testAuthor
          ->id(),
      ];
    }
  }
}