You are here

protected function DefaultViewsTest::setUp in Drupal 9

Same name in this branch
  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()
Same name and namespace in other branches
  1. 8 core/modules/views/tests/src/Functional/DefaultViewsTest.php \Drupal\Tests\views\Functional\DefaultViewsTest::setUp()

Overrides ViewTestBase::setUp

File

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

Class

DefaultViewsTest
Tests the default views provided by views.

Namespace

Drupal\Tests\views\Functional

Code

protected function setUp($import_test_views = TRUE) : void {
  parent::setUp($import_test_views);
  $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' => mb_strtolower($this
      ->randomMachineName()),
    'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    'help' => '',
    'nodes' => [
      'page' => 'page',
    ],
    'weight' => mt_rand(0, 10),
  ]);
  $vocabulary
    ->save();

  // Create a field.
  $field_name = mb_strtolower($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 = REQUEST_TIME - 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);
}