function ViewsModerationStateFilterTest::testWorkflowChanges

Tests the moderation state filter when the configured workflow is changed.

@dataProvider providerTestWorkflowChanges

File

core/modules/content_moderation/tests/src/Functional/ViewsModerationStateFilterTest.php, line 165

Class

ViewsModerationStateFilterTest
Tests the views 'moderation_state_filter' filter plugin.

Namespace

Drupal\Tests\content_moderation\Functional

Code

public function testWorkflowChanges($view_id) {
  // First, apply the Editorial workflow to both of our content types.
  $this->drupalPostForm('admin/config/workflow/workflows/manage/editorial/type/node', [
    'bundles[example_a]' => TRUE,
    'bundles[example_b]' => TRUE,
  ], 'Save');
  \Drupal::service('entity_type.bundle.info')->clearCachedBundles();
  // Update the view and make the default filter not exposed anymore,
  // otherwise all results will be shown when there are no more moderated
  // bundles left.
  $this->drupalPostForm("admin/structure/views/nojs/handler/{$view_id}/default/filter/moderation_state", [], 'Hide filter');
  $this->drupalPostForm("admin/structure/views/view/{$view_id}", [], 'Save');
  // Add a few nodes in various moderation states.
  $this->createNode([
    'type' => 'example_a',
    'moderation_state' => 'published',
  ]);
  $this->createNode([
    'type' => 'example_b',
    'moderation_state' => 'published',
  ]);
  $archived_node_a = $this->createNode([
    'type' => 'example_a',
    'moderation_state' => 'archived',
  ]);
  $archived_node_b = $this->createNode([
    'type' => 'example_b',
    'moderation_state' => 'archived',
  ]);
  // Configure the view to only show nodes in the 'archived' moderation state.
  $edit['options[value][]'] = [
    'editorial-archived',
  ];
  $this->drupalPostForm("admin/structure/views/nojs/handler/{$view_id}/default/filter/moderation_state", $edit, 'Apply');
  $this->drupalPostForm("admin/structure/views/view/{$view_id}", [], 'Save');
  // Check that only the archived nodes from both bundles are displayed by the
  // view.
  $view = $this->loadViewUnchanged($view_id);
  $this->executeAndAssertIdenticalResultset($view, [
    [
      'nid' => $archived_node_a->id(),
    ],
    [
      'nid' => $archived_node_b->id(),
    ],
  ], [
    'nid' => 'nid',
  ]);
  // Remove the Editorial workflow from one of the bundles.
  $this->drupalPostForm('admin/config/workflow/workflows/manage/editorial/type/node', [
    'bundles[example_a]' => TRUE,
    'bundles[example_b]' => FALSE,
  ], 'Save');
  \Drupal::service('entity_type.bundle.info')->clearCachedBundles();
  $view = $this->loadViewUnchanged($view_id);
  $this->executeAndAssertIdenticalResultset($view, [
    [
      'nid' => $archived_node_a->id(),
    ],
  ], [
    'nid' => 'nid',
  ]);
  // Check that the view can still be edited and saved without any
  // intervention.
  $this->drupalPostForm("admin/structure/views/view/{$view_id}", [], 'Save');
  // Remove the Editorial workflow from both bundles.
  $this->drupalPostForm('admin/config/workflow/workflows/manage/editorial/type/node', [
    'bundles[example_a]' => FALSE,
    'bundles[example_b]' => FALSE,
  ], 'Save');
  \Drupal::service('entity_type.bundle.info')->clearCachedBundles();
  // Check that the view doesn't return any result.
  $view = $this->loadViewUnchanged($view_id);
  $this->executeAndAssertIdenticalResultset($view, [], []);
  // Check that the view contains a broken filter, since the moderation_state
  // field was removed from the entity type.
  $this->drupalPostForm("admin/structure/views/view/{$view_id}", [], 'Save');
  $this->assertSession()
    ->pageTextContains("Broken/missing handler");
}

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