function RowEntityRenderersTest::setUp

Same name in other branches
  1. 8.9.x core/modules/views/tests/src/Kernel/Entity/RowEntityRenderersTest.php \Drupal\Tests\views\Kernel\Entity\RowEntityRenderersTest::setUp()
  2. 10 core/modules/views/tests/src/Kernel/Entity/RowEntityRenderersTest.php \Drupal\Tests\views\Kernel\Entity\RowEntityRenderersTest::setUp()
  3. 11.x core/modules/views/tests/src/Kernel/Entity/RowEntityRenderersTest.php \Drupal\Tests\views\Kernel\Entity\RowEntityRenderersTest::setUp()

Overrides ViewsKernelTestBase::setUp

File

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

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(),
            ];
        }
    }
}

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