ModerationFormTest.php 21.9 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\content_moderation\Functional;
4

5
use Drupal\Core\Entity\Entity\EntityFormDisplay;
6
use Drupal\Core\Url;
7

8 9 10 11 12 13 14
/**
 * Tests the moderation form, specifically on nodes.
 *
 * @group content_moderation
 */
class ModerationFormTest extends ModerationStateTestBase {

15 16 17 18 19 20 21 22 23 24 25 26
  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = [
    'node',
    'content_moderation',
    'locale',
    'content_translation',
  ];

27 28 29 30 31 32
  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->drupalLogin($this->adminUser);
33
    $this->createContentTypeFromUi('Moderated content', 'moderated_content', TRUE);
34 35 36 37 38 39
    $this->grantUserPermissionToCreateContentOfType($this->adminUser, 'moderated_content');
  }

  /**
   * Tests the moderation form that shows on the latest version page.
   *
40
   * The latest version page only shows if there is a pending revision.
41 42
   *
   * @see \Drupal\content_moderation\EntityOperations
43
   * @see \Drupal\Tests\content_moderation\Functional\ModerationStateBlockTest::testCustomBlockModeration
44 45 46 47 48 49
   */
  public function testModerationForm() {
    // Create new moderated content in draft.
    $this->drupalPostForm('node/add/moderated_content', [
      'title[0][value]' => 'Some moderated content',
      'body[0][value]' => 'First version of the content.',
50 51
      'moderation_state[0][state]' => 'draft',
    ], t('Save'));
52 53 54 55 56 57 58 59

    $node = $this->drupalGetNodeByTitle('Some moderated content');
    $canonical_path = sprintf('node/%d', $node->id());
    $edit_path = sprintf('node/%d/edit', $node->id());
    $latest_version_path = sprintf('node/%d/latest', $node->id());

    $this->assertTrue($this->adminUser->hasPermission('edit any moderated_content content'));

60 61 62 63 64 65
    // The canonical view should have a moderation form, because it is not the
    // live revision.
    $this->drupalGet($canonical_path);
    $this->assertResponse(200);
    $this->assertField('edit-new-state', 'The node view page has a moderation form.');

66
    // The latest version page should not show, because there is no pending
67 68 69 70 71 72 73
    // revision.
    $this->drupalGet($latest_version_path);
    $this->assertResponse(403);

    // Update the draft.
    $this->drupalPostForm($edit_path, [
      'body[0][value]' => 'Second version of the content.',
74 75
      'moderation_state[0][state]' => 'draft',
    ], t('Save'));
76

77 78 79 80 81 82
    // The canonical view should have a moderation form, because it is not the
    // live revision.
    $this->drupalGet($canonical_path);
    $this->assertResponse(200);
    $this->assertField('edit-new-state', 'The node view page has a moderation form.');

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    // Preview the draft.
    $this->drupalPostForm($edit_path, [
      'body[0][value]' => 'Second version of the content.',
      'moderation_state[0][state]' => 'draft',
    ], t('Preview'));

    // The preview view should not have a moderation form.
    $preview_url = Url::fromRoute('entity.node.preview', [
      'node_preview' => $node->uuid(),
      'view_mode_id' => 'full',
    ]);
    $this->assertResponse(200);
    $this->assertUrl($preview_url);
    $this->assertNoField('edit-new-state', 'The node preview page has no moderation form.');

98
    // The latest version page should not show, because there is still no
99
    // pending revision.
100 101 102 103 104 105
    $this->drupalGet($latest_version_path);
    $this->assertResponse(403);

    // Publish the draft.
    $this->drupalPostForm($edit_path, [
      'body[0][value]' => 'Third version of the content.',
106 107
      'moderation_state[0][state]' => 'published',
    ], t('Save'));
108

109 110 111 112
    // Check widget default value.
    $this->drupalGet($edit_path);
    $this->assertFieldByName('moderation_state[0][state]', 'published', 'The moderation default value is set correctly.');

113
    // The published view should not have a moderation form, because it is the
114
    // live revision.
115 116
    $this->drupalGet($canonical_path);
    $this->assertResponse(200);
117
    $this->assertNoField('edit-new-state', 'The node view page has no moderation form.');
118 119

    // The latest version page should not show, because there is still no
120
    // pending revision.
121 122 123
    $this->drupalGet($latest_version_path);
    $this->assertResponse(403);

124
    // Make a pending revision.
125 126
    $this->drupalPostForm($edit_path, [
      'body[0][value]' => 'Fourth version of the content.',
127 128
      'moderation_state[0][state]' => 'draft',
    ], t('Save'));
129 130

    // The published view should not have a moderation form, because it is the
131
    // live revision.
132 133
    $this->drupalGet($canonical_path);
    $this->assertResponse(200);
134
    $this->assertNoField('edit-new-state', 'The node view page has no moderation form.');
135 136

    // The latest version page should show the moderation form and have "Draft"
137
    // status, because the pending revision is in "Draft".
138 139
    $this->drupalGet($latest_version_path);
    $this->assertResponse(200);
140
    $this->assertField('edit-new-state', 'The latest-version page has a moderation form.');
141 142 143 144 145 146 147 148
    $this->assertText('Draft', 'Correct status found on the latest-version page.');

    // Submit the moderation form to change status to published.
    $this->drupalPostForm($latest_version_path, [
      'new_state' => 'published',
    ], t('Apply'));

    // The latest version page should not show, because there is no
149
    // pending revision.
150 151 152 153
    $this->drupalGet($latest_version_path);
    $this->assertResponse(403);
  }

154 155 156 157 158
  /**
   * Test moderation non-bundle entity type.
   */
  public function testNonBundleModerationForm() {
    $this->drupalLogin($this->rootUser);
159 160
    $this->workflow->getTypePlugin()->addEntityTypeAndBundle('entity_test_mulrevpub', 'entity_test_mulrevpub');
    $this->workflow->save();
161 162

    // Create new moderated content in draft.
163
    $this->drupalPostForm('entity_test_mulrevpub/add', ['moderation_state[0][state]' => 'draft'], t('Save'));
164

165
    // The latest version page should not show, because there is no pending
166 167 168 169 170
    // revision.
    $this->drupalGet('/entity_test_mulrevpub/manage/1/latest');
    $this->assertResponse(403);

    // Update the draft.
171
    $this->drupalPostForm('entity_test_mulrevpub/manage/1/edit', ['moderation_state[0][state]' => 'draft'], t('Save'));
172 173

    // The latest version page should not show, because there is still no
174
    // pending revision.
175 176 177 178
    $this->drupalGet('/entity_test_mulrevpub/manage/1/latest');
    $this->assertResponse(403);

    // Publish the draft.
179
    $this->drupalPostForm('entity_test_mulrevpub/manage/1/edit', ['moderation_state[0][state]' => 'published'], t('Save'));
180 181 182 183 184 185 186 187

    // The published view should not have a moderation form, because it is the
    // default revision.
    $this->drupalGet('entity_test_mulrevpub/manage/1');
    $this->assertResponse(200);
    $this->assertNoText('Status', 'The node view page has no moderation form.');

    // The latest version page should not show, because there is still no
188
    // pending revision.
189 190 191
    $this->drupalGet('entity_test_mulrevpub/manage/1/latest');
    $this->assertResponse(403);

192
    // Make a pending revision.
193
    $this->drupalPostForm('entity_test_mulrevpub/manage/1/edit', ['moderation_state[0][state]' => 'draft'], t('Save'));
194 195 196 197 198 199 200 201

    // The published view should not have a moderation form, because it is the
    // default revision.
    $this->drupalGet('entity_test_mulrevpub/manage/1');
    $this->assertResponse(200);
    $this->assertNoText('Status', 'The node view page has no moderation form.');

    // The latest version page should show the moderation form and have "Draft"
202
    // status, because the pending revision is in "Draft".
203 204
    $this->drupalGet('entity_test_mulrevpub/manage/1/latest');
    $this->assertResponse(200);
205
    $this->assertText('Moderation state', 'Form text found on the latest-version page.');
206 207 208 209 210 211 212 213
    $this->assertText('Draft', 'Correct status found on the latest-version page.');

    // Submit the moderation form to change status to published.
    $this->drupalPostForm('entity_test_mulrevpub/manage/1/latest', [
      'new_state' => 'published',
    ], t('Apply'));

    // The latest version page should not show, because there is no
214
    // pending revision.
215 216 217 218
    $this->drupalGet('entity_test_mulrevpub/manage/1/latest');
    $this->assertResponse(403);
  }

219 220 221 222 223 224
  /**
   * Tests the revision author is updated when the moderation form is used.
   */
  public function testModerationFormSetsRevisionAuthor() {
    // Create new moderated content in published.
    $node = $this->createNode(['type' => 'moderated_content', 'moderation_state' => 'published']);
225
    // Make a pending revision.
226 227
    $node->title = $this->randomMachineName();
    $node->moderation_state->value = 'draft';
228
    $node->setRevisionCreationTime(12345);
229 230 231 232 233 234 235 236 237 238 239
    $node->save();

    $another_user = $this->drupalCreateUser($this->permissions);
    $this->grantUserPermissionToCreateContentOfType($another_user, 'moderated_content');
    $this->drupalLogin($another_user);
    $this->drupalPostForm(sprintf('node/%d/latest', $node->id()), [
      'new_state' => 'published',
    ], t('Apply'));

    $this->drupalGet(sprintf('node/%d/revisions', $node->id()));
    $this->assertText('by ' . $another_user->getAccountName());
240 241 242 243

    // Verify the revision creation time has been updated.
    $node = $node->load($node->id());
    $this->assertGreaterThan(12345, $node->getRevisionCreationTime());
244 245
  }

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
  /**
   * Tests translated and moderated nodes.
   */
  public function testContentTranslationNodeForm() {
    $this->drupalLogin($this->rootUser);

    // Add French language.
    $edit = [
      'predefined_langcode' => 'fr',
    ];
    $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));

    // Enable content translation on articles.
    $this->drupalGet('admin/config/regional/content-language');
    $edit = [
      'entity_types[node]' => TRUE,
      'settings[node][moderated_content][translatable]' => TRUE,
      'settings[node][moderated_content][settings][language][language_alterable]' => TRUE,
    ];
    $this->drupalPostForm(NULL, $edit, t('Save configuration'));

    // Adding languages requires a container rebuild in the test running
    // environment so that multilingual services are used.
    $this->rebuildContainer();

    // Create new moderated content in draft (revision 1).
    $this->drupalPostForm('node/add/moderated_content', [
      'title[0][value]' => 'Some moderated content',
      'body[0][value]' => 'First version of the content.',
275 276
      'moderation_state[0][state]' => 'draft',
    ], t('Save'));
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
    $this->assertTrue($this->xpath('//ul[@class="entity-moderation-form"]'));

    $node = $this->drupalGetNodeByTitle('Some moderated content');
    $this->assertTrue($node->language(), 'en');
    $edit_path = sprintf('node/%d/edit', $node->id());
    $translate_path = sprintf('node/%d/translations/add/en/fr', $node->id());
    $latest_version_path = sprintf('node/%d/latest', $node->id());
    $french = \Drupal::languageManager()->getLanguage('fr');

    $this->drupalGet($latest_version_path);
    $this->assertSession()->statusCodeEquals('403');
    $this->assertFalse($this->xpath('//ul[@class="entity-moderation-form"]'));

    // Add french translation (revision 2).
    $this->drupalGet($translate_path);
292 293 294
    $this->assertSession()->optionExists('moderation_state[0][state]', 'draft');
    $this->assertSession()->optionExists('moderation_state[0][state]', 'published');
    $this->assertSession()->optionNotExists('moderation_state[0][state]', 'archived');
295 296
    $this->drupalPostForm(NULL, [
      'body[0][value]' => 'Second version of the content.',
297 298
      'moderation_state[0][state]' => 'published',
    ], t('Save (this translation)'));
299 300 301 302 303

    $this->drupalGet($latest_version_path, ['language' => $french]);
    $this->assertSession()->statusCodeEquals('403');
    $this->assertFalse($this->xpath('//ul[@class="entity-moderation-form"]'));

304
    // Add french pending revision (revision 3).
305
    $this->drupalGet($edit_path, ['language' => $french]);
306 307 308
    $this->assertSession()->optionExists('moderation_state[0][state]', 'draft');
    $this->assertSession()->optionExists('moderation_state[0][state]', 'published');
    $this->assertSession()->optionExists('moderation_state[0][state]', 'archived');
309 310
    $this->drupalPostForm(NULL, [
      'body[0][value]' => 'Third version of the content.',
311 312
      'moderation_state[0][state]' => 'draft',
    ], t('Save (this translation)'));
313 314 315 316 317 318 319 320 321 322 323

    $this->drupalGet($latest_version_path, ['language' => $french]);
    $this->assertTrue($this->xpath('//ul[@class="entity-moderation-form"]'));

    $this->drupalGet($edit_path);
    $this->clickLink('Delete');
    $this->assertSession()->buttonExists('Delete');

    $this->drupalGet($latest_version_path);
    $this->assertFalse($this->xpath('//ul[@class="entity-moderation-form"]'));

324
    // Publish the french pending revision (revision 4).
325
    $this->drupalGet($edit_path, ['language' => $french]);
326 327 328
    $this->assertSession()->optionExists('moderation_state[0][state]', 'draft');
    $this->assertSession()->optionExists('moderation_state[0][state]', 'published');
    $this->assertSession()->optionNotExists('moderation_state[0][state]', 'archived');
329 330
    $this->drupalPostForm(NULL, [
      'body[0][value]' => 'Fifth version of the content.',
331 332
      'moderation_state[0][state]' => 'published',
    ], t('Save (this translation)'));
333 334 335 336

    $this->drupalGet($latest_version_path, ['language' => $french]);
    $this->assertFalse($this->xpath('//ul[@class="entity-moderation-form"]'));

337
    // Publish the English pending revision (revision 5).
338
    $this->drupalGet($edit_path);
339 340 341
    $this->assertSession()->optionExists('moderation_state[0][state]', 'draft');
    $this->assertSession()->optionExists('moderation_state[0][state]', 'published');
    $this->assertSession()->optionNotExists('moderation_state[0][state]', 'archived');
342 343
    $this->drupalPostForm(NULL, [
      'body[0][value]' => 'Sixth version of the content.',
344 345
      'moderation_state[0][state]' => 'published',
    ], t('Save (this translation)'));
346 347 348 349

    $this->drupalGet($latest_version_path);
    $this->assertFalse($this->xpath('//ul[@class="entity-moderation-form"]'));

350
    // Make sure we are allowed to create a pending French revision.
351
    $this->drupalGet($edit_path, ['language' => $french]);
352 353 354
    $this->assertSession()->optionExists('moderation_state[0][state]', 'draft');
    $this->assertSession()->optionExists('moderation_state[0][state]', 'published');
    $this->assertSession()->optionExists('moderation_state[0][state]', 'archived');
355

356
    // Add an English pending revision (revision 6).
357
    $this->drupalGet($edit_path);
358 359 360
    $this->assertSession()->optionExists('moderation_state[0][state]', 'draft');
    $this->assertSession()->optionExists('moderation_state[0][state]', 'published');
    $this->assertSession()->optionExists('moderation_state[0][state]', 'archived');
361 362
    $this->drupalPostForm(NULL, [
      'body[0][value]' => 'Seventh version of the content.',
363 364
      'moderation_state[0][state]' => 'draft',
    ], t('Save (this translation)'));
365 366 367 368 369 370

    $this->drupalGet($latest_version_path);
    $this->assertTrue($this->xpath('//ul[@class="entity-moderation-form"]'));
    $this->drupalGet($latest_version_path, ['language' => $french]);
    $this->assertFalse($this->xpath('//ul[@class="entity-moderation-form"]'));

371
    // Publish the English pending revision (revision 7)
372
    $this->drupalGet($edit_path);
373 374 375
    $this->assertSession()->optionExists('moderation_state[0][state]', 'draft');
    $this->assertSession()->optionExists('moderation_state[0][state]', 'published');
    $this->assertSession()->optionNotExists('moderation_state[0][state]', 'archived');
376 377
    $this->drupalPostForm(NULL, [
      'body[0][value]' => 'Eighth version of the content.',
378 379
      'moderation_state[0][state]' => 'published',
    ], t('Save (this translation)'));
380 381 382 383

    $this->drupalGet($latest_version_path);
    $this->assertFalse($this->xpath('//ul[@class="entity-moderation-form"]'));

384
    // Make sure we are allowed to create a pending French revision.
385
    $this->drupalGet($edit_path, ['language' => $french]);
386 387 388
    $this->assertSession()->optionExists('moderation_state[0][state]', 'draft');
    $this->assertSession()->optionExists('moderation_state[0][state]', 'published');
    $this->assertSession()->optionExists('moderation_state[0][state]', 'archived');
389

390
    // Make sure we are allowed to create a pending English revision.
391
    $this->drupalGet($edit_path);
392 393 394
    $this->assertSession()->optionExists('moderation_state[0][state]', 'draft');
    $this->assertSession()->optionExists('moderation_state[0][state]', 'published');
    $this->assertSession()->optionExists('moderation_state[0][state]', 'archived');
395 396 397 398

    // Create new moderated content (revision 1).
    $this->drupalPostForm('node/add/moderated_content', [
      'title[0][value]' => 'Third moderated content',
399 400
      'moderation_state[0][state]' => 'published',
    ], t('Save'));
401 402 403 404 405 406 407 408

    $node = $this->drupalGetNodeByTitle('Third moderated content');
    $this->assertTrue($node->language(), 'en');
    $edit_path = sprintf('node/%d/edit', $node->id());
    $translate_path = sprintf('node/%d/translations/add/en/fr', $node->id());

    // Translate it, without updating data (revision 2).
    $this->drupalGet($translate_path);
409 410 411 412 413 414
    $this->assertSession()->optionExists('moderation_state[0][state]', 'draft');
    $this->assertSession()->optionExists('moderation_state[0][state]', 'published');
    $this->assertSession()->optionExists('moderation_state[0][state]', 'archived');
    $this->drupalPostForm(NULL, [
      'moderation_state[0][state]' => 'draft',
    ], t('Save (this translation)'));
415 416 417

    // Add another draft for the translation (revision 3).
    $this->drupalGet($edit_path, ['language' => $french]);
418 419 420 421 422 423
    $this->assertSession()->optionExists('moderation_state[0][state]', 'draft');
    $this->assertSession()->optionExists('moderation_state[0][state]', 'published');
    $this->assertSession()->optionNotExists('moderation_state[0][state]', 'archived');
    $this->drupalPostForm(NULL, [
      'moderation_state[0][state]' => 'draft',
    ], t('Save (this translation)'));
424 425 426

    // Updating and publishing the french translation is still possible.
    $this->drupalGet($edit_path, ['language' => $french]);
427 428 429 430 431 432
    $this->assertSession()->optionExists('moderation_state[0][state]', 'draft');
    $this->assertSession()->optionExists('moderation_state[0][state]', 'published');
    $this->assertSession()->optionNotExists('moderation_state[0][state]', 'archived');
    $this->drupalPostForm(NULL, [
      'moderation_state[0][state]' => 'published',
    ], t('Save (this translation)'));
433 434 435

    // Now the french translation is published, an english draft can be added.
    $this->drupalGet($edit_path);
436 437 438 439 440 441
    $this->assertSession()->optionExists('moderation_state[0][state]', 'draft');
    $this->assertSession()->optionExists('moderation_state[0][state]', 'published');
    $this->assertSession()->optionExists('moderation_state[0][state]', 'archived');
    $this->drupalPostForm(NULL, [
      'moderation_state[0][state]' => 'draft',
    ], t('Save (this translation)'));
442 443
  }

444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
  /**
   * Test the moderation_state field when an alternative widget is set.
   */
  public function testAlternativeModerationStateWidget() {
    $entity_form_display = EntityFormDisplay::load('node.moderated_content.default');
    $entity_form_display->setComponent('moderation_state', [
      'type' => 'string_textfield',
      'region' => 'content',
    ]);
    $entity_form_display->save();
    $this->drupalPostForm('node/add/moderated_content', [
      'title[0][value]' => 'Test content',
      'moderation_state[0][value]' => 'published',
    ], 'Save');
    $this->assertSession()->pageTextContains('Moderated content Test content has been created.');
  }

461 462 463 464 465 466 467 468 469 470 471 472 473
  /**
   * Tests that workflows and states can not be deleted if they are in use.
   *
   * @covers \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration::workflowHasData
   * @covers \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration::workflowStateHasData
   */
  public function testWorkflowInUse() {
    $user = $this->createUser([
      'administer workflows',
      'create moderated_content content',
      'edit own moderated_content content',
      'use editorial transition create_new_draft',
      'use editorial transition publish',
474
      'use editorial transition archive',
475 476 477 478 479 480
    ]);
    $this->drupalLogin($user);
    $paths = [
      'archived_state' => 'admin/config/workflow/workflows/manage/editorial/state/archived/delete',
      'editorial_workflow' => 'admin/config/workflow/workflows/manage/editorial/delete',
    ];
481 482 483 484
    $messages = [
      'archived_state' => 'This workflow state is in use. You cannot remove this workflow state until you have removed all content using it.',
      'editorial_workflow' => 'This workflow is in use. You cannot remove this workflow until you have removed all content using it.',
    ];
485 486 487 488 489 490 491 492
    foreach ($paths as $path) {
      $this->drupalGet($path);
      $this->assertSession()->buttonExists('Delete');
    }
    // Create new moderated content in draft.
    $this->drupalPostForm('node/add/moderated_content', [
      'title[0][value]' => 'Some moderated content',
      'body[0][value]' => 'First version of the content.',
493 494
      'moderation_state[0][state]' => 'draft',
    ], 'Save');
495 496 497 498 499 500 501 502

    // The archived state is not used yet, so can still be deleted.
    $this->drupalGet($paths['archived_state']);
    $this->assertSession()->buttonExists('Delete');

    // The workflow is being used, so can't be deleted.
    $this->drupalGet($paths['editorial_workflow']);
    $this->assertSession()->buttonNotExists('Delete');
503 504
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->pageTextContains($messages['editorial_workflow']);
505 506

    $node = $this->drupalGetNodeByTitle('Some moderated content');
507 508 509 510 511 512
    $this->drupalPostForm('node/' . $node->id() . '/edit', [
      'moderation_state[0][state]' => 'published',
    ], 'Save');
    $this->drupalPostForm('node/' . $node->id() . '/edit', [
      'moderation_state[0][state]' => 'archived',
    ], 'Save');
513 514

    // Now the archived state is being used so it can not be deleted either.
515
    foreach ($paths as $type => $path) {
516 517
      $this->drupalGet($path);
      $this->assertSession()->buttonNotExists('Delete');
518 519
      $this->assertSession()->statusCodeEquals(200);
      $this->assertSession()->pageTextContains($messages[$type]);
520 521 522
    }
  }

523
}