You are here

public function ViewsModerationStateFilterTest::testStateFilterViewsRelationship in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/content_moderation/tests/src/Kernel/ViewsModerationStateFilterTest.php \Drupal\Tests\content_moderation\Kernel\ViewsModerationStateFilterTest::testStateFilterViewsRelationship()

Tests the content moderation state filter.

File

core/modules/content_moderation/tests/src/Kernel/ViewsModerationStateFilterTest.php, line 83

Class

ViewsModerationStateFilterTest
Tests the views 'moderation_state_filter' filter plugin.

Namespace

Drupal\Tests\content_moderation\Kernel

Code

public function testStateFilterViewsRelationship() {
  $workflow = Workflow::load('editorial');
  $workflow
    ->getTypePlugin()
    ->addEntityTypeAndBundle('node', 'example');
  $workflow
    ->getTypePlugin()
    ->addState('translated_draft', 'Bar');
  $configuration = $workflow
    ->getTypePlugin()
    ->getConfiguration();
  $configuration['states']['translated_draft'] += [
    'published' => FALSE,
    'default_revision' => FALSE,
  ];
  $workflow
    ->getTypePlugin()
    ->setConfiguration($configuration);
  $workflow
    ->save();

  // Create a published default revision and one forward draft revision.
  $node = Node::create([
    'type' => 'example',
    'title' => 'Test Node',
    'moderation_state' => 'published',
  ]);
  $node
    ->save();
  $node
    ->setNewRevision();
  $node->moderation_state = 'draft';
  $node
    ->save();

  // Create a draft default revision.
  $second_node = Node::create([
    'type' => 'example',
    'title' => 'Second Node',
    'moderation_state' => 'draft',
  ]);
  $second_node
    ->save();

  // Create a published default revision.
  $third_node = Node::create([
    'type' => 'example',
    'title' => 'Third node',
    'moderation_state' => 'published',
  ]);
  $third_node
    ->save();

  // Add a non-moderated node.
  $fourth_node = Node::create([
    'type' => 'example_non_moderated',
    'title' => 'Fourth node',
  ]);
  $fourth_node
    ->save();

  // Create a translated published revision.
  $translated_forward_revision = $third_node
    ->addTranslation('fr');
  $translated_forward_revision->title = 'Translated Node';
  $translated_forward_revision
    ->setNewRevision(TRUE);
  $translated_forward_revision->moderation_state = 'translated_draft';
  $translated_forward_revision
    ->save();

  // Test the filter within an AND filter group (the default) and an OR filter
  // group.
  $base_table_views = [
    'test_content_moderation_state_filter_base_table',
    'test_content_moderation_state_filter_base_table_filter_group_or',
  ];
  foreach ($base_table_views as $view_id) {

    // The three default revisions are listed when no filter is specified.
    $this
      ->assertNodesWithFilters([
      $node,
      $second_node,
      $third_node,
    ], [], $view_id);

    // The default revision of node one and three are published.
    $this
      ->assertNodesWithFilters([
      $node,
      $third_node,
    ], [
      'default_revision_state' => 'editorial-published',
    ], $view_id);

    // The default revision of node two is draft.
    $this
      ->assertNodesWithFilters([
      $second_node,
    ], [
      'default_revision_state' => 'editorial-draft',
    ], $view_id);
  }

  // Test the same three revisions on a view displaying content revisions.
  // Both nodes have one draft revision.
  $this
    ->assertNodesWithFilters([
    $node,
    $second_node,
  ], [
    'moderation_state' => 'editorial-draft',
  ], 'test_content_moderation_state_filter_revision_table');

  // Creating a new forward revision of node three, creates a second published
  // revision of the original language, hence there are two published
  // revisions of node three.
  $this
    ->assertNodesWithFilters([
    $node,
    $third_node,
    $third_node,
  ], [
    'moderation_state' => 'editorial-published',
  ], 'test_content_moderation_state_filter_revision_table');

  // There is a single forward translated revision with a new state, which is
  // also filterable.
  $this
    ->assertNodesWithFilters([
    $translated_forward_revision,
  ], [
    'moderation_state' => 'editorial-translated_draft',
  ], 'test_content_moderation_state_filter_revision_table');
}