You are here

public function FilterAndArgumentUserUidTest::testHandlers in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/comment/tests/src/Kernel/Views/FilterAndArgumentUserUidTest.php \Drupal\Tests\comment\Kernel\Views\FilterAndArgumentUserUidTest::testHandlers()
  2. 10 core/modules/comment/tests/src/Kernel/Views/FilterAndArgumentUserUidTest.php \Drupal\Tests\comment\Kernel\Views\FilterAndArgumentUserUidTest::testHandlers()

Tests the user posted or commented filter and argument handlers.

File

core/modules/comment/tests/src/Kernel/Views/FilterAndArgumentUserUidTest.php, line 53

Class

FilterAndArgumentUserUidTest
Tests the user posted or commented filter and argument handlers.

Namespace

Drupal\Tests\comment\Kernel\Views

Code

public function testHandlers() {
  $this
    ->installEntitySchema('user');
  $this
    ->installSchema('system', [
    'sequences',
  ]);
  $this
    ->installEntitySchema('node');
  $this
    ->installEntitySchema('comment');
  $this
    ->installSchema('comment', [
    'comment_entity_statistics',
  ]);
  $this
    ->installConfig([
    'filter',
  ]);
  NodeType::create([
    'type' => 'page',
  ])
    ->save();
  FieldStorageConfig::create([
    'type' => 'text_long',
    'entity_type' => 'comment',
    'field_name' => 'comment_body',
  ])
    ->save();
  $this
    ->addDefaultCommentField('node', 'page', 'comment');
  $account = $this
    ->createUser();
  $other_account = $this
    ->createUser();
  $node_authored_by_account = $this
    ->createNode([
    'uid' => $account
      ->id(),
    'title' => "authored by {$account->id()}",
  ]);
  $node_commented_by_account = $this
    ->createNode([
    'title' => "commented by {$account->id()}",
  ]);
  $arbitrary_node = $this
    ->createNode();

  // Comment added by $account.
  Comment::create([
    'uid' => $account
      ->id(),
    'entity_id' => $node_commented_by_account
      ->id(),
    'entity_type' => 'node',
    'field_name' => 'comment',
  ])
    ->save();

  // Comment added by $other_account on $node_commented_by_account
  Comment::create([
    'uid' => $other_account
      ->id(),
    'entity_id' => $node_commented_by_account
      ->id(),
    'entity_type' => 'node',
    'field_name' => 'comment',
  ])
    ->save();

  // Comment added by $other_account on an arbitrary node.
  Comment::create([
    'uid' => $other_account
      ->id(),
    'entity_id' => $arbitrary_node
      ->id(),
    'entity_type' => 'node',
    'field_name' => 'comment',
  ])
    ->save();
  ViewTestData::createTestViews(static::class, [
    'comment_test_views',
  ]);
  $expected_result = [
    [
      'nid' => $node_authored_by_account
        ->id(),
      'title' => "authored by {$account->id()}",
    ],
    [
      'nid' => $node_commented_by_account
        ->id(),
      'title' => "commented by {$account->id()}",
    ],
  ];
  $column_map = [
    'nid' => 'nid',
    'title' => 'title',
  ];
  $view = Views::getView('test_comment_user_uid');

  // Test the argument handler.
  $view
    ->preview(NULL, [
    $account
      ->id(),
  ]);
  $this
    ->assertIdenticalResultset($view, $expected_result, $column_map);

  // Test the filter handler. Reuse the same view but replace the argument
  // handler with a filter handler.
  $view
    ->removeHandler('default', 'argument', 'uid_touch');
  $options = [
    'id' => 'uid_touch',
    'table' => 'node_field_data',
    'field' => 'uid_touch',
    'value' => [
      $account
        ->id(),
    ],
  ];
  $view
    ->addHandler('default', 'filter', 'node_field_data', 'uid_touch', $options);
  $view
    ->preview();
  $this
    ->assertIdenticalResultset($view, $expected_result, $column_map);
}