3 namespace Drupal\Tests\content_moderation\Functional;
5 use Drupal\node\Entity\NodeType;
6 use Drupal\Tests\views\Functional\ViewTestBase;
7 use Drupal\views\ViewExecutable;
8 use Drupal\views\Views;
9 use Drupal\workflows\Entity\Workflow;
12 * Tests the views 'moderation_state_filter' filter plugin.
14 * @coversDefaultClass \Drupal\content_moderation\Plugin\views\filter\ModerationStateFilter
16 * @group content_moderation
18 class ViewsModerationStateFilterTest extends ViewTestBase {
23 public static $modules = [
24 'content_moderation_test_views',
31 'content_translation',
38 protected function setUp($import_test_views = TRUE) {
42 'type' => 'example_a',
45 'type' => 'example_b',
48 $new_workflow = Workflow::create([
49 'type' => 'content_moderation',
50 'id' => 'new_workflow',
51 'label' => 'New workflow',
53 $new_workflow->getTypePlugin()->addState('bar', 'Bar');
54 $new_workflow->save();
56 $this->drupalLogin($this->drupalCreateUser(['administer workflows', 'administer views']));
60 * Tests the dependency handling of the moderation state filter.
62 * @covers ::calculateDependencies
63 * @covers ::onDependencyRemoval
65 public function testModerationStateFilterDependencyHandling() {
66 // First, check that the view doesn't have any config dependency when there
67 // are no states configured in the filter.
68 $view_id = 'test_content_moderation_state_filter_base_table';
69 $view = Views::getView($view_id);
71 $this->assertWorkflowDependencies([], $view);
72 $this->assertTrue($view->storage->status());
74 // Configure the Editorial workflow for a node bundle, set the filter value
75 // to use one of its states and check that the workflow is now a dependency
77 $this->drupalPostForm('admin/config/workflow/workflows/manage/editorial/type/node', [
78 'bundles[example_a]' => TRUE,
81 $edit['options[value][]'] = ['editorial-published'];
82 $this->drupalPostForm("admin/structure/views/nojs/handler/$view_id/default/filter/moderation_state", $edit, 'Apply');
83 $this->drupalPostForm("admin/structure/views/view/$view_id", [], 'Save');
85 $view = Views::getView($view_id);
86 $this->assertWorkflowDependencies(['editorial'], $view);
87 $this->assertTrue($view->storage->status());
89 // Create another workflow and repeat the checks above.
90 $this->drupalPostForm('admin/config/workflow/workflows/add', [
91 'label' => 'Translation',
92 'id' => 'translation',
93 'workflow_type' => 'content_moderation',
95 $this->drupalPostForm('admin/config/workflow/workflows/manage/translation/add_state', [
96 'label' => 'Needs Review',
97 'id' => 'needs_review',
99 $this->drupalPostForm('admin/config/workflow/workflows/manage/translation/type/node', [
100 'bundles[example_b]' => TRUE,
103 $edit['options[value][]'] = ['editorial-published', 'translation-needs_review'];
104 $this->drupalPostForm("admin/structure/views/nojs/handler/$view_id/default/filter/moderation_state", $edit, 'Apply');
105 $this->drupalPostForm("admin/structure/views/view/$view_id", [], 'Save');
107 $view = Views::getView($view_id);
108 $this->assertWorkflowDependencies(['editorial', 'translation'], $view);
109 $this->assertTrue(isset($view->storage->getDisplay('default')['display_options']['filters']['moderation_state']));
110 $this->assertTrue($view->storage->status());
112 // Remove the 'Translation' workflow.
113 $this->drupalPostForm('admin/config/workflow/workflows/manage/translation/delete', [], 'Delete');
115 // Check that the view has been disabled, the filter has been deleted, the
116 // view can be saved and there are no more config dependencies.
117 $view = Views::getView($view_id);
118 $this->assertFalse($view->storage->status());
119 $this->assertFalse(isset($view->storage->getDisplay('default')['display_options']['filters']['moderation_state']));
120 $this->drupalPostForm("admin/structure/views/view/$view_id", [], 'Save');
121 $this->assertWorkflowDependencies([], $view);
125 * Tests the moderation state filter when the configured workflow is changed.
127 * @dataProvider providerTestWorkflowChanges
129 public function testWorkflowChanges($view_id, $filter_name) {
130 // Update the view and make the default filter not exposed anymore,
131 // otherwise all results will be shown when there are no more moderated
133 $this->drupalPostForm("admin/structure/views/nojs/handler/$view_id/default/filter/moderation_state", [], 'Hide filter');
134 $this->drupalPostForm("admin/structure/views/view/$view_id", [], 'Save');
136 // First, apply the Editorial workflow to both of our content types.
137 $this->drupalPostForm('admin/config/workflow/workflows/manage/editorial/type/node', [
138 'bundles[example_a]' => TRUE,
139 'bundles[example_b]' => TRUE,
141 \Drupal::service('entity_type.bundle.info')->clearCachedBundles();
143 // Add a few nodes in various moderation states.
144 $this->createNode(['type' => 'example_a', 'moderation_state' => 'published']);
145 $this->createNode(['type' => 'example_b', 'moderation_state' => 'published']);
146 $archived_node_a = $this->createNode(['type' => 'example_a', 'moderation_state' => 'archived']);
147 $archived_node_b = $this->createNode(['type' => 'example_b', 'moderation_state' => 'archived']);
149 // Configure the view to only show nodes in the 'archived' moderation state.
150 $edit['options[value][]'] = ['editorial-archived'];
151 $this->drupalPostForm("admin/structure/views/nojs/handler/$view_id/default/filter/moderation_state", $edit, 'Apply');
152 $this->drupalPostForm("admin/structure/views/view/$view_id", [], 'Save');
154 // Check that only the archived nodes from both bundles are displayed by the
156 $view = Views::getView($view_id);
157 $this->executeView($view);
158 $this->assertIdenticalResultset($view, [['nid' => $archived_node_a->id()], ['nid' => $archived_node_b->id()]], ['nid' => 'nid']);
160 // Remove the Editorial workflow from one of the bundles.
161 $this->drupalPostForm('admin/config/workflow/workflows/manage/editorial/type/node', [
162 'bundles[example_a]' => TRUE,
163 'bundles[example_b]' => FALSE,
165 \Drupal::service('entity_type.bundle.info')->clearCachedBundles();
167 $view = Views::getView($view_id);
168 $this->executeView($view);
169 $this->assertIdenticalResultset($view, [['nid' => $archived_node_a->id()]], ['nid' => 'nid']);
171 // Check that the view can still be edited and saved without any
173 $this->drupalPostForm("admin/structure/views/view/$view_id", [], 'Save');
175 // Remove the Editorial workflow from both bundles.
176 $this->drupalPostForm('admin/config/workflow/workflows/manage/editorial/type/node', [
177 'bundles[example_a]' => FALSE,
178 'bundles[example_b]' => FALSE,
180 \Drupal::service('entity_type.bundle.info')->clearCachedBundles();
182 $view = Views::getView($view_id);
183 $this->executeView($view);
185 // Check that the view doesn't return any result.
186 $this->assertEmpty($view->result);
188 // Check that the view can not be edited without any intervention anymore
189 // because the user needs to fix the filter.
190 $this->drupalPostForm("admin/structure/views/view/$view_id", [], 'Save');
191 $this->assertSession()->pageTextContains("No valid values found on filter: $filter_name.");
195 * Data provider for testWorkflowChanges.
198 * An array of view IDs.
200 public function providerTestWorkflowChanges() {
202 'view on base table, filter on base table' => [
203 'test_content_moderation_state_filter_base_table',
204 'Content: Moderation state'
206 'view on base table, filter on revision table' => [
207 'test_content_moderation_state_filter_base_table_filter_on_revision',
208 'Content revision: Moderation state'
214 * Tests the content moderation state filter caching is correct.
216 public function testFilterRenderCache() {
217 // Initially all states of the workflow are displayed.
218 $this->drupalPostForm('admin/config/workflow/workflows/manage/editorial/type/node', [
219 'bundles[example_a]' => TRUE,
221 $this->assertFilterStates(['All', 'editorial-draft', 'editorial-published', 'editorial-archived']);
223 // Adding a new state to the editorial workflow will display that state in
224 // the list of filters.
225 $this->drupalPostForm('admin/config/workflow/workflows/manage/editorial/add_state', [
229 $this->assertFilterStates(['All', 'editorial-draft', 'editorial-published', 'editorial-archived', 'editorial-foo']);
231 // Adding a second workflow to nodes will also show new states.
232 $this->drupalPostForm('admin/config/workflow/workflows/manage/new_workflow/type/node', [
233 'bundles[example_b]' => TRUE,
235 $this->assertFilterStates(['All', 'editorial-draft', 'editorial-published', 'editorial-archived', 'editorial-foo', 'new_workflow-draft', 'new_workflow-published', 'new_workflow-bar']);
237 // Add a few more states and change the exposed filter to allow multiple
238 // selections so we can check that the size of the select element does not
240 $this->drupalPostForm('admin/config/workflow/workflows/manage/editorial/add_state', [
244 $this->drupalPostForm('admin/config/workflow/workflows/manage/editorial/add_state', [
249 $view_id = 'test_content_moderation_state_filter_base_table';
250 $edit['options[expose][multiple]'] = TRUE;
251 $this->drupalPostForm("admin/structure/views/nojs/handler/$view_id/default/filter/moderation_state", $edit, 'Apply');
252 $this->drupalPostForm("admin/structure/views/view/$view_id", [], 'Save');
254 $this->assertFilterStates(['editorial-draft', 'editorial-published', 'editorial-archived', 'editorial-foo', 'editorial-foo2', 'editorial-foo3', 'new_workflow-draft', 'new_workflow-published', 'new_workflow-bar'], TRUE);
258 * Assert the states which appear in the filter.
260 * @param array $states
261 * The states which should appear in the filter.
262 * @param bool $check_size
263 * (optional) Whether to check that size of the select element is not
264 * greater than 8. Defaults to FALSE.
266 protected function assertFilterStates($states, $check_size = FALSE) {
267 $this->drupalGet('/filter-test-path');
269 $assert_session = $this->assertSession();
271 // Check that the select contains the correct number of options.
272 $assert_session->elementsCount('css', '#edit-default-revision-state option', count($states));
274 // Check that the size of the select element does not exceed 8 options.
276 $this->assertGreaterThan(8, count($states));
277 $assert_session->elementAttributeContains('css', '#edit-default-revision-state', 'size', 8);
280 // Check that an option exists for each of the expected states.
281 foreach ($states as $state) {
282 $assert_session->optionExists('Default Revision State', $state);
287 * Asserts the views dependencies on workflow config entities.
289 * @param string[] $workflow_ids
290 * An array of workflow IDs to check.
291 * @param \Drupal\views\ViewExecutable $view
292 * An executable View object.
294 protected function assertWorkflowDependencies(array $workflow_ids, ViewExecutable $view) {
295 $dependencies = $view->getDependencies();
298 foreach (Workflow::loadMultiple($workflow_ids) as $workflow) {
299 $expected[] = $workflow->getConfigDependencyName();
303 $this->assertSame($expected, $dependencies['config']);
306 $this->assertTrue(!isset($dependencies['config']));