function DefaultViewsTest::setUp

Same name in this branch
  1. 11.x core/modules/views_ui/tests/src/Functional/DefaultViewsTest.php \Drupal\Tests\views_ui\Functional\DefaultViewsTest::setUp()
Same name in other branches
  1. 9 core/modules/views/tests/src/Functional/DefaultViewsTest.php \Drupal\Tests\views\Functional\DefaultViewsTest::setUp()
  2. 9 core/modules/views_ui/tests/src/Functional/DefaultViewsTest.php \Drupal\Tests\views_ui\Functional\DefaultViewsTest::setUp()
  3. 8.9.x core/modules/views/tests/src/Functional/DefaultViewsTest.php \Drupal\Tests\views\Functional\DefaultViewsTest::setUp()
  4. 8.9.x core/modules/views_ui/tests/src/Functional/DefaultViewsTest.php \Drupal\Tests\views_ui\Functional\DefaultViewsTest::setUp()
  5. 10 core/modules/views/tests/src/Functional/DefaultViewsTest.php \Drupal\Tests\views\Functional\DefaultViewsTest::setUp()
  6. 10 core/modules/views_ui/tests/src/Functional/DefaultViewsTest.php \Drupal\Tests\views_ui\Functional\DefaultViewsTest::setUp()

Overrides ViewTestBase::setUp

File

core/modules/views/tests/src/Functional/DefaultViewsTest.php, line 61

Class

DefaultViewsTest
Tests the default views provided by views.

Namespace

Drupal\Tests\views\Functional

Code

protected function setUp($import_test_views = TRUE, $modules = []) : void {
    parent::setUp($import_test_views, $modules);
    $this->drupalPlaceBlock('page_title_block');
    // Create Basic page node type.
    $this->drupalCreateContentType([
        'type' => 'page',
        'name' => 'Basic page',
    ]);
    $vocabulary = Vocabulary::create([
        'name' => $this->randomMachineName(),
        'description' => $this->randomMachineName(),
        'vid' => $this->randomMachineName(),
        'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
        'help' => '',
        'nodes' => [
            'page' => 'page',
        ],
        'weight' => mt_rand(0, 10),
    ]);
    $vocabulary->save();
    // Create a field.
    $field_name = $this->randomMachineName();
    $handler_settings = [
        'target_bundles' => [
            $vocabulary->id() => $vocabulary->id(),
        ],
        'auto_create' => TRUE,
    ];
    $this->createEntityReferenceField('node', 'page', $field_name, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
    // Create a time in the past for the archive.
    $time = \Drupal::time()->getRequestTime() - 3600;
    $this->addDefaultCommentField('node', 'page');
    for ($i = 0; $i <= 10; $i++) {
        $user = $this->drupalCreateUser();
        $term = $this->createTerm($vocabulary);
        $values = [
            'created' => $time,
            'type' => 'page',
        ];
        $values[$field_name][]['target_id'] = $term->id();
        // Make every other node promoted.
        if ($i % 2) {
            $values['promote'] = TRUE;
        }
        $values['body'][]['value'] = Link::fromTextAndUrl('Node ' . 1, Url::fromRoute('entity.node.canonical', [
            'node' => 1,
        ]))->toString();
        $node = $this->drupalCreateNode($values);
        $comment = [
            'uid' => $user->id(),
            'status' => CommentInterface::PUBLISHED,
            'entity_id' => $node->id(),
            'entity_type' => 'node',
            'field_name' => 'comment',
        ];
        Comment::create($comment)->save();
        $unpublished_comment = [
            'uid' => $user->id(),
            'status' => CommentInterface::NOT_PUBLISHED,
            'entity_id' => $node->id(),
            'entity_type' => 'node',
            'field_name' => 'comment',
        ];
        Comment::create($unpublished_comment)->save();
    }
    // Some views, such as the "Who's Online" view, only return results if at
    // least one user is logged in.
    $account = $this->drupalCreateUser([]);
    $this->drupalLogin($account);
}

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