3 namespace Drupal\Tests\workflows\Functional;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\workflows\Entity\Workflow;
10 * Tests workflow creation UI.
14 class WorkflowUiTest extends BrowserTestBase {
21 public static $modules = ['workflows', 'workflow_type_test', 'block'];
26 protected function setUp() {
28 // We're testing local actions.
29 $this->drupalPlaceBlock('local_actions_block');
33 * Tests route access/permissions.
35 public function testAccess() {
36 // Create a minimal workflow for testing.
37 $workflow = Workflow::create(['id' => 'test', 'type' => 'workflow_type_test']);
39 ->addState('draft', 'Draft')
40 ->addState('published', 'Published')
41 ->addTransition('publish', 'Publish', ['draft', 'published'], 'published')
45 'admin/config/workflow/workflows',
46 'admin/config/workflow/workflows/add',
47 'admin/config/workflow/workflows/manage/test',
48 'admin/config/workflow/workflows/manage/test/delete',
49 'admin/config/workflow/workflows/manage/test/add_state',
50 'admin/config/workflow/workflows/manage/test/state/published',
51 'admin/config/workflow/workflows/manage/test/state/published/delete',
52 'admin/config/workflow/workflows/manage/test/add_transition',
53 'admin/config/workflow/workflows/manage/test/transition/publish',
54 'admin/config/workflow/workflows/manage/test/transition/publish/delete',
57 foreach ($paths as $path) {
58 $this->drupalGet($path);
60 $this->assertSession()->statusCodeEquals(403);
62 $this->drupalLogin($this->createUser(['administer workflows']));
63 foreach ($paths as $path) {
64 $this->drupalGet($path);
66 $this->assertSession()->statusCodeEquals(200);
69 // Ensure that default states can not be deleted.
70 \Drupal::state()->set('workflow_type_test.required_states', ['published']);
71 $this->drupalGet('admin/config/workflow/workflows/manage/test/state/published/delete');
72 $this->assertSession()->statusCodeEquals(403);
73 \Drupal::state()->set('workflow_type_test.required_states', []);
75 // Delete one of the states and ensure the other test cannot be deleted.
76 $this->drupalGet('admin/config/workflow/workflows/manage/test/state/published/delete');
77 $this->submitForm([], 'Delete');
78 $this->drupalGet('admin/config/workflow/workflows/manage/test/state/draft/delete');
79 $this->assertSession()->statusCodeEquals(403);
83 * Tests the creation of a workflow through the UI.
85 public function testWorkflowCreation() {
86 $workflow_storage = $this->container->get('entity_type.manager')->getStorage('workflow');
87 /** @var \Drupal\workflows\WorkflowInterface $workflow */
88 $this->drupalLogin($this->createUser(['access administration pages', 'administer workflows']));
89 $this->drupalGet('admin/config/workflow');
90 $this->assertSession()->linkByHrefExists('admin/config/workflow/workflows');
91 $this->clickLink('Workflows');
92 $this->assertSession()->pageTextContains('Workflows');
93 $this->assertSession()->pageTextContains('There is no Workflow yet.');
94 $this->clickLink('Add workflow');
95 $this->submitForm(['label' => 'Test', 'id' => 'test', 'workflow_type' => 'workflow_type_test'], 'Save');
96 $this->assertSession()->pageTextContains('Created the Test Workflow.');
97 $this->assertSession()->addressEquals('admin/config/workflow/workflows/manage/test/add_state');
98 $this->drupalGet('/admin/config/workflow/workflows/manage/test');
99 $this->assertSession()->pageTextContains('This workflow has no states and will be disabled until there is at least one, add a new state.');
100 $this->assertSession()->pageTextContains('There are no states yet.');
101 $this->clickLink('Add a new state');
102 $this->submitForm(['label' => 'Published', 'id' => 'published'], 'Save');
103 $this->assertSession()->pageTextContains('Created Published state.');
104 $workflow = $workflow_storage->loadUnchanged('test');
105 $this->assertFalse($workflow->getState('published')->canTransitionTo('published'), 'No default transition from published to published exists.');
107 $this->clickLink('Add a new state');
108 // Don't create a draft to draft transition by default.
109 $this->submitForm(['label' => 'Draft', 'id' => 'draft'], 'Save');
110 $this->assertSession()->pageTextContains('Created Draft state.');
111 $workflow = $workflow_storage->loadUnchanged('test');
112 $this->assertFalse($workflow->getState('draft')->canTransitionTo('draft'), 'Can not transition from draft to draft');
114 $this->clickLink('Add a new transition');
115 $this->submitForm(['id' => 'publish', 'label' => 'Publish', 'from[draft]' => 'draft', 'to' => 'published'], 'Save');
116 $this->assertSession()->pageTextContains('Created Publish transition.');
117 $workflow = $workflow_storage->loadUnchanged('test');
118 $this->assertTrue($workflow->getState('draft')->canTransitionTo('published'), 'Can transition from draft to published');
120 $this->clickLink('Add a new transition');
121 $this->submitForm(['id' => 'create_new_draft', 'label' => 'Create new draft', 'from[draft]' => 'draft', 'to' => 'draft'], 'Save');
122 $this->assertSession()->pageTextContains('Created Create new draft transition.');
123 $workflow = $workflow_storage->loadUnchanged('test');
124 $this->assertTrue($workflow->getState('draft')->canTransitionTo('draft'), 'Can transition from draft to draft');
126 // The fist state to edit on the page should be published.
127 $this->clickLink('Edit');
128 $this->assertSession()->fieldValueEquals('label', 'Published');
130 $this->submitForm(['label' => 'Live'], 'Save');
131 $this->assertSession()->pageTextContains('Saved Live state.');
133 // Allow published to draft.
134 $this->clickLink('Edit', 3);
135 $this->submitForm(['from[published]' => 'published'], 'Save');
136 $this->assertSession()->pageTextContains('Saved Create new draft transition.');
137 $workflow = $workflow_storage->loadUnchanged('test');
138 $this->assertTrue($workflow->getState('published')->canTransitionTo('draft'), 'Can transition from published to draft');
140 // Try creating a duplicate transition.
141 $this->clickLink('Add a new transition');
142 $this->submitForm(['id' => 'create_new_draft', 'label' => 'Create new draft', 'from[published]' => 'published', 'to' => 'draft'], 'Save');
143 $this->assertSession()->pageTextContains('The machine-readable name is already in use. It must be unique.');
144 // Try creating a transition which duplicates the states of another.
145 $this->submitForm(['id' => 'create_new_draft2', 'label' => 'Create new draft again', 'from[published]' => 'published', 'to' => 'draft'], 'Save');
146 $this->assertSession()->pageTextContains('The transition from Live to Draft already exists.');
148 // Create a new transition.
149 $this->submitForm(['id' => 'save_and_publish', 'label' => 'Save and publish', 'from[published]' => 'published', 'to' => 'published'], 'Save');
150 $this->assertSession()->pageTextContains('Created Save and publish transition.');
151 // Edit the new transition and try to add an existing transition.
152 $this->clickLink('Edit', 4);
153 $this->submitForm(['from[draft]' => 'draft'], 'Save');
154 $this->assertSession()->pageTextContains('The transition from Draft to Live already exists.');
156 // Delete the transition.
157 $workflow = $workflow_storage->loadUnchanged('test');
158 $this->assertTrue($workflow->hasTransitionFromStateToState('published', 'published'), 'Can transition from published to published');
159 $this->clickLink('Delete');
160 $this->assertSession()->pageTextContains('Are you sure you want to delete Save and publish from Test?');
161 $this->submitForm([], 'Delete');
162 $workflow = $workflow_storage->loadUnchanged('test');
163 $this->assertFalse($workflow->hasTransitionFromStateToState('published', 'published'), 'Cannot transition from published to published');
165 // Try creating a duplicate state.
166 $this->drupalGet('admin/config/workflow/workflows/manage/test');
167 $this->clickLink('Add a new state');
168 $this->submitForm(['label' => 'Draft', 'id' => 'draft'], 'Save');
169 $this->assertSession()->pageTextContains('The machine-readable name is already in use. It must be unique.');
171 // Ensure that weight changes the state ordering.
172 $workflow = $workflow_storage->loadUnchanged('test');
173 $this->assertEquals('published', $workflow->getTypePlugin()->getInitialState($workflow)->id());
174 $this->drupalGet('admin/config/workflow/workflows/manage/test');
175 $this->submitForm(['states[draft][weight]' => '-1'], 'Save');
176 $workflow = $workflow_storage->loadUnchanged('test');
177 $this->assertEquals('draft', $workflow->getTypePlugin()->getInitialState($workflow)->id());
179 // This will take us to the list of workflows, so we need to edit the
181 $this->clickLink('Edit');
183 // Ensure that weight changes the transition ordering.
184 $this->assertEquals(['publish', 'create_new_draft'], array_keys($workflow->getTransitions()));
185 $this->drupalGet('admin/config/workflow/workflows/manage/test');
186 $this->submitForm(['transitions[create_new_draft][weight]' => '-1'], 'Save');
187 $workflow = $workflow_storage->loadUnchanged('test');
188 $this->assertEquals(['create_new_draft', 'publish'], array_keys($workflow->getTransitions()));
190 // This will take us to the list of workflows, so we need to edit the
192 $this->clickLink('Edit');
194 // Ensure that a delete link for the published state exists before deleting
196 $published_delete_link = Url::fromRoute('entity.workflow.delete_state_form', [
197 'workflow' => $workflow->id(),
198 'workflow_state' => 'published',
200 $draft_delete_link = Url::fromRoute('entity.workflow.delete_state_form', [
201 'workflow' => $workflow->id(),
202 'workflow_state' => 'draft',
204 $this->assertSession()->elementContains('css', 'tr[data-drupal-selector="edit-states-published"]', 'Delete');
205 $this->assertSession()->linkByHrefExists($published_delete_link);
206 $this->assertSession()->linkByHrefExists($draft_delete_link);
208 // Make the published state a default state and ensure it is no longer
210 \Drupal::state()->set('workflow_type_test.required_states', ['published']);
211 $this->getSession()->reload();
212 $this->assertSession()->linkByHrefNotExists($published_delete_link);
213 $this->assertSession()->linkByHrefExists($draft_delete_link);
214 $this->assertSession()->elementNotContains('css', 'tr[data-drupal-selector="edit-states-published"]', 'Delete');
215 \Drupal::state()->set('workflow_type_test.required_states', []);
216 $this->getSession()->reload();
217 $this->assertSession()->elementContains('css', 'tr[data-drupal-selector="edit-states-published"]', 'Delete');
218 $this->assertSession()->linkByHrefExists($published_delete_link);
219 $this->assertSession()->linkByHrefExists($draft_delete_link);
221 // Delete the Draft state.
222 $this->clickLink('Delete');
223 $this->assertSession()->pageTextContains('Are you sure you want to delete Draft from Test?');
224 $this->submitForm([], 'Delete');
225 $this->assertSession()->pageTextContains('State Draft deleted.');
226 $workflow = $workflow_storage->loadUnchanged('test');
227 $this->assertFalse($workflow->hasState('draft'), 'Draft state deleted');
228 $this->assertTrue($workflow->hasState('published'), 'Workflow still has published state');
230 // The last state cannot be deleted so the only delete link on the page will
231 // be for the workflow.
232 $this->assertSession()->linkByHrefNotExists($published_delete_link);
233 $this->clickLink('Delete');
234 $this->assertSession()->pageTextContains('Are you sure you want to delete Test?');
235 $this->submitForm([], 'Delete');
236 $this->assertSession()->pageTextContains('Workflow Test deleted.');
237 $this->assertSession()->pageTextContains('There is no Workflow yet.');
238 $this->assertNull($workflow_storage->loadUnchanged('test'), 'The test workflow has been deleted');
240 // Ensure that workflow types that implement
241 // \Drupal\workflows\WorkflowTypeInterface::initializeWorkflow() are
242 // initialized correctly.
243 $this->drupalGet('admin/config/workflow/workflows');
244 $this->clickLink('Add workflow');
245 $this->submitForm(['label' => 'Test 2', 'id' => 'test2', 'workflow_type' => 'workflow_type_required_state_test'], 'Save');
246 $this->assertSession()->addressEquals('admin/config/workflow/workflows/manage/test2');
247 $workflow = $workflow_storage->loadUnchanged('test2');
248 $this->assertTrue($workflow->hasState('fresh'), 'The workflow has the "fresh" state');
249 $this->assertTrue($workflow->hasState('rotten'), 'The workflow has the "rotten" state');
250 $this->assertTrue($workflow->hasTransition('rot'), 'The workflow has the "rot" transition');
251 $this->assertSession()->pageTextContains('Fresh');
252 $this->assertSession()->pageTextContains('Rotten');
256 * Tests that workflow types can add form fields to states and transitions.
258 public function testWorkflowDecoration() {
259 // Create a minimal workflow for testing.
260 $workflow = Workflow::create(['id' => 'test', 'type' => 'workflow_type_complex_test']);
262 ->addState('published', 'Published')
263 ->addTransition('publish', 'Publish', ['published'], 'published')
266 $this->assertEquals('', $workflow->getState('published')->getExtra());
267 $this->assertEquals('', $workflow->getTransition('publish')->getExtra());
269 $this->drupalLogin($this->createUser(['administer workflows']));
271 // Add additional state information when editing.
272 $this->drupalGet('admin/config/workflow/workflows/manage/test/state/published');
273 $this->assertSession()->pageTextContains('Extra information added to state');
274 $this->submitForm(['type_settings[workflow_type_complex_test][extra]' => 'Extra state information'], 'Save');
276 // Add additional transition information when editing.
277 $this->drupalGet('admin/config/workflow/workflows/manage/test/transition/publish');
278 $this->assertSession()->pageTextContains('Extra information added to transition');
279 $this->submitForm(['type_settings[workflow_type_complex_test][extra]' => 'Extra transition information'], 'Save');
281 $workflow_storage = $this->container->get('entity_type.manager')->getStorage('workflow');
282 /** @var \Drupal\workflows\WorkflowInterface $workflow */
283 $workflow = $workflow_storage->loadUnchanged('test');
284 $this->assertEquals('Extra state information', $workflow->getState('published')->getExtra());
285 $this->assertEquals('Extra transition information', $workflow->getTransition('publish')->getExtra());
287 // Add additional state information when adding.
288 $this->drupalGet('admin/config/workflow/workflows/manage/test/add_state');
289 $this->assertSession()->pageTextContains('Extra information added to state');
290 $this->submitForm(['label' => 'Draft', 'id' => 'draft', 'type_settings[workflow_type_complex_test][extra]' => 'Extra state information on add'], 'Save');
292 // Add additional transition information when adding.
293 $this->drupalGet('admin/config/workflow/workflows/manage/test/add_transition');
294 $this->assertSession()->pageTextContains('Extra information added to transition');
295 $this->submitForm(['id' => 'draft_published', 'label' => 'Publish', 'from[draft]' => 'draft', 'to' => 'published', 'type_settings[workflow_type_complex_test][extra]' => 'Extra transition information on add'], 'Save');
297 $workflow = $workflow_storage->loadUnchanged('test');
298 $this->assertEquals('Extra state information on add', $workflow->getState('draft')->getExtra());
299 $this->assertEquals('Extra transition information on add', $workflow->getTransition('draft_published')->getExtra());