ContentModerationStateTest.php 25.3 KB
Newer Older
1 2 3 4 5
<?php

namespace Drupal\Tests\content_moderation\Kernel;

use Drupal\content_moderation\Entity\ContentModerationState;
6
use Drupal\Core\Entity\EntityInterface;
7
use Drupal\Core\Entity\EntityPublishedInterface;
8
use Drupal\Core\Entity\EntityStorageException;
9 10
use Drupal\Core\Language\LanguageInterface;
use Drupal\entity_test\Entity\EntityTestRev;
11 12 13 14
use Drupal\KernelTests\KernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
15
use Drupal\workflows\Entity\Workflow;
16 17 18 19 20 21 22 23 24 25 26 27

/**
 * Tests links between a content entity and a content_moderation_state entity.
 *
 * @group content_moderation
 */
class ContentModerationStateTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  public static $modules = [
28
    'entity_test',
29
    'node',
30
    'block',
31
    'block_content',
32 33 34 35 36
    'media',
    'media_test_source',
    'image',
    'file',
    'field',
37 38 39 40 41
    'content_moderation',
    'user',
    'system',
    'language',
    'content_translation',
42
    'text',
43
    'workflows',
44 45
  ];

46 47 48 49 50
  /**
   * @var \Drupal\Core\Entity\EntityTypeManager
   */
  protected $entityTypeManager;

51 52 53 54 55 56 57 58 59
  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    $this->installSchema('node', 'node_access');
    $this->installEntitySchema('node');
    $this->installEntitySchema('user');
60
    $this->installEntitySchema('entity_test_rev');
61
    $this->installEntitySchema('entity_test_no_bundle');
62 63
    $this->installEntitySchema('entity_test_mulrevpub');
    $this->installEntitySchema('block_content');
64 65
    $this->installEntitySchema('media');
    $this->installEntitySchema('file');
66 67
    $this->installEntitySchema('content_moderation_state');
    $this->installConfig('content_moderation');
68 69
    $this->installSchema('file', 'file_usage');
    $this->installConfig(['field', 'system', 'image', 'file', 'media']);
70 71

    $this->entityTypeManager = $this->container->get('entity_type.manager');
72 73
  }

74 75 76 77 78 79
  /**
   * Tests basic monolingual content moderation through the API.
   *
   * @dataProvider basicModerationTestCases
   */
  public function testBasicModeration($entity_type_id) {
80
    $entity = $this->createEntity($entity_type_id);
81 82 83
    if ($entity instanceof EntityPublishedInterface) {
      $entity->setUnpublished();
    }
84 85 86
    $entity->save();
    $entity = $this->reloadEntity($entity);
    $this->assertEquals('draft', $entity->moderation_state->value);
87

88 89
    $entity->moderation_state->value = 'published';
    $entity->save();
90

91 92
    $entity = $this->reloadEntity($entity);
    $this->assertEquals('published', $entity->moderation_state->value);
93 94 95 96 97 98 99

    // Change the state without saving the node.
    $content_moderation_state = ContentModerationState::load(1);
    $content_moderation_state->set('moderation_state', 'draft');
    $content_moderation_state->setNewRevision(TRUE);
    $content_moderation_state->save();

100 101 102 103 104
    $entity = $this->reloadEntity($entity, 3);
    $this->assertEquals('draft', $entity->moderation_state->value);
    if ($entity instanceof EntityPublishedInterface) {
      $this->assertFalse($entity->isPublished());
    }
105 106

    // Get the default revision.
107 108 109 110 111
    $entity = $this->reloadEntity($entity);
    if ($entity instanceof EntityPublishedInterface) {
      $this->assertTrue((bool) $entity->isPublished());
    }
    $this->assertEquals(2, $entity->getRevisionId());
112

113 114
    $entity->moderation_state->value = 'published';
    $entity->save();
115

116 117
    $entity = $this->reloadEntity($entity, 4);
    $this->assertEquals('published', $entity->moderation_state->value);
118 119

    // Get the default revision.
120 121 122 123 124
    $entity = $this->reloadEntity($entity);
    if ($entity instanceof EntityPublishedInterface) {
      $this->assertTrue((bool) $entity->isPublished());
    }
    $this->assertEquals(4, $entity->getRevisionId());
125

126
    // Update the node to archived which will then be the default revision.
127 128
    $entity->moderation_state->value = 'archived';
    $entity->save();
129 130

    // Revert to the previous (published) revision.
131
    $entity_storage = $this->entityTypeManager->getStorage($entity_type_id);
132
    $previous_revision = $entity_storage->loadRevision(4);
133 134 135 136 137
    $previous_revision->isDefaultRevision(TRUE);
    $previous_revision->setNewRevision(TRUE);
    $previous_revision->save();

    // Get the default revision.
138 139 140 141 142
    $entity = $this->reloadEntity($entity);
    $this->assertEquals('published', $entity->moderation_state->value);
    if ($entity instanceof EntityPublishedInterface) {
      $this->assertTrue($entity->isPublished());
    }
143 144 145 146 147

    // Set an invalid moderation state.
    $this->setExpectedException(EntityStorageException::class);
    $entity->moderation_state->value = 'foobar';
    $entity->save();
148 149 150 151 152 153 154 155 156 157 158 159 160
  }

  /**
   * Test cases for basic moderation test.
   */
  public function basicModerationTestCases() {
    return [
      'Nodes' => [
        'node',
      ],
      'Block content' => [
        'block_content',
      ],
161 162 163
      'Media' => [
        'media',
      ],
164 165 166 167 168
      'Test entity - revisions, data table, and published interface' => [
        'entity_test_mulrevpub',
      ],
      'Entity Test with revisions' => [
        'entity_test_rev',
169 170 171 172
      ],
      'Entity without bundle' => [
        'entity_test_no_bundle',
      ],
173
    ];
174 175
  }

176
  /**
177
   * Tests removal of content moderation state entity.
178 179 180 181 182
   *
   * @dataProvider basicModerationTestCases
   */
  public function testContentModerationStateDataRemoval($entity_type_id) {
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
183
    $entity = $this->createEntity($entity_type_id);
184 185 186 187 188
    $entity->save();
    $entity = $this->reloadEntity($entity);
    $entity->delete();
    $content_moderation_state = ContentModerationState::loadFromModeratedEntity($entity);
    $this->assertFalse($content_moderation_state);
189
  }
190

191 192 193 194 195 196 197 198 199 200
  /**
   * Tests removal of content moderation state entity revisions.
   *
   * @dataProvider basicModerationTestCases
   */
  public function testContentModerationStateRevisionDataRemoval($entity_type_id) {
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $this->createEntity($entity_type_id);
    $entity->save();
    $revision = clone $entity;
201 202 203
    $revision->isDefaultRevision(FALSE);
    $content_moderation_state = ContentModerationState::loadFromModeratedEntity($revision);
    $this->assertTrue($content_moderation_state);
204 205 206
    $entity = $this->reloadEntity($entity);
    $entity->setNewRevision(TRUE);
    $entity->save();
207
    $entity_storage = $this->entityTypeManager->getStorage($entity_type_id);
208 209 210
    $entity_storage->deleteRevision($revision->getRevisionId());
    $content_moderation_state = ContentModerationState::loadFromModeratedEntity($revision);
    $this->assertFalse($content_moderation_state);
211
    $content_moderation_state = ContentModerationState::loadFromModeratedEntity($entity);
212
    $this->assertTrue($content_moderation_state);
213
  }
214

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
  /**
   * Tests removal of content moderation state pending entity revisions.
   *
   * @dataProvider basicModerationTestCases
   */
  public function testContentModerationStatePendingRevisionDataRemoval($entity_type_id) {
    $entity = $this->createEntity($entity_type_id);
    $entity->moderation_state = 'published';
    $entity->save();
    $entity->setNewRevision(TRUE);
    $entity->moderation_state = 'draft';
    $entity->save();

    $content_moderation_state = ContentModerationState::loadFromModeratedEntity($entity);
    $this->assertTrue($content_moderation_state);

    $entity_storage = $this->entityTypeManager->getStorage($entity_type_id);
    $entity_storage->deleteRevision($entity->getRevisionId());

    $content_moderation_state = ContentModerationState::loadFromModeratedEntity($entity);
    $this->assertFalse($content_moderation_state);
  }

  /**
   * Tests removal of content moderation state translations.
   *
   * @dataProvider basicModerationTestCases
   */
  public function testContentModerationStateTranslationDataRemoval($entity_type_id) {
244 245
    // Test content moderation state translation deletion.
    if ($this->entityTypeManager->getDefinition($entity_type_id)->isTranslatable()) {
246 247
      /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
      $entity = $this->createEntity($entity_type_id);
248 249 250
      $langcode = 'it';
      ConfigurableLanguage::createFromLangcode($langcode)
        ->save();
251 252
      $entity->save();
      $translation = $entity->addTranslation($langcode, ['title' => 'Titolo test']);
253 254 255 256
      // Make sure we add values for all of the required fields.
      if ($entity_type_id == 'block_content') {
        $translation->info = $this->randomString();
      }
257
      $translation->save();
258
      $content_moderation_state = ContentModerationState::loadFromModeratedEntity($entity);
259
      $this->assertTrue($content_moderation_state->hasTranslation($langcode));
260 261 262
      $entity->removeTranslation($langcode);
      $entity->save();
      $content_moderation_state = ContentModerationState::loadFromModeratedEntity($entity);
263 264 265 266
      $this->assertFalse($content_moderation_state->hasTranslation($langcode));
    }
  }

267 268 269 270 271 272 273 274 275 276
  /**
   * Tests basic multilingual content moderation through the API.
   */
  public function testMultilingualModeration() {
    // Enable French.
    ConfigurableLanguage::createFromLangcode('fr')->save();
    $node_type = NodeType::create([
      'type' => 'example',
    ]);
    $node_type->save();
277 278 279 280 281

    $workflow = Workflow::load('editorial');
    $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
    $workflow->save();

282 283 284 285 286 287
    $english_node = Node::create([
      'type' => 'example',
      'title' => 'Test title',
    ]);
    // Revision 1 (en).
    $english_node
288
      ->setUnpublished()
289
      ->save();
290
    $this->assertEquals('draft', $english_node->moderation_state->value);
291 292 293 294
    $this->assertFalse($english_node->isPublished());

    // Create a French translation.
    $french_node = $english_node->addTranslation('fr', ['title' => 'French title']);
295
    $french_node->setUnpublished();
296 297
    // Revision 1 (fr).
    $french_node->save();
298
    $french_node = $this->reloadEntity($english_node)->getTranslation('fr');
299
    $this->assertEquals('draft', $french_node->moderation_state->value);
300 301 302
    $this->assertFalse($french_node->isPublished());

    // Move English node to create another draft.
303
    $english_node = $this->reloadEntity($english_node);
304
    $english_node->moderation_state->value = 'draft';
305 306
    // Revision 2 (en, fr).
    $english_node->save();
307
    $english_node = $this->reloadEntity($english_node);
308
    $this->assertEquals('draft', $english_node->moderation_state->value);
309 310

    // French node should still be in draft.
311
    $french_node = $this->reloadEntity($english_node)->getTranslation('fr');
312
    $this->assertEquals('draft', $french_node->moderation_state->value);
313 314

    // Publish the French node.
315
    $french_node->moderation_state->value = 'published';
316 317
    // Revision 3 (en, fr).
    $french_node->save();
318
    $french_node = $this->reloadEntity($french_node)->getTranslation('fr');
319
    $this->assertTrue($french_node->isPublished());
320
    $this->assertEquals('published', $french_node->moderation_state->value);
321 322
    $this->assertTrue($french_node->isPublished());
    $english_node = $french_node->getTranslation('en');
323
    $this->assertEquals('draft', $english_node->moderation_state->value);
324 325

    // Publish the English node.
326
    $english_node->moderation_state->value = 'published';
327 328
    // Revision 4 (en, fr).
    $english_node->save();
329
    $english_node = $this->reloadEntity($english_node);
330 331 332
    $this->assertTrue($english_node->isPublished());

    // Move the French node back to draft.
333
    $french_node = $this->reloadEntity($english_node)->getTranslation('fr');
334
    $this->assertTrue($french_node->isPublished());
335
    $french_node->moderation_state->value = 'draft';
336 337
    // Revision 5 (en, fr).
    $french_node->save();
338
    $french_node = $this->reloadEntity($english_node, 5)->getTranslation('fr');
339 340 341 342
    $this->assertFalse($french_node->isPublished());
    $this->assertTrue($french_node->getTranslation('en')->isPublished());

    // Republish the French node.
343
    $french_node->moderation_state->value = 'published';
344 345
    // Revision 6 (en, fr).
    $french_node->save();
346
    $french_node = $this->reloadEntity($english_node)->getTranslation('fr');
347 348 349 350 351 352 353 354
    $this->assertTrue($french_node->isPublished());

    // Change the EN state without saving the node.
    $content_moderation_state = ContentModerationState::load(1);
    $content_moderation_state->set('moderation_state', 'draft');
    $content_moderation_state->setNewRevision(TRUE);
    // Revision 7 (en, fr).
    $content_moderation_state->save();
355
    $english_node = $this->reloadEntity($french_node, $french_node->getRevisionId() + 1);
356

357
    $this->assertEquals('draft', $english_node->moderation_state->value);
358
    $french_node = $this->reloadEntity($english_node)->getTranslation('fr');
359
    $this->assertEquals('published', $french_node->moderation_state->value);
360 361 362 363 364 365 366 367 368

    // This should unpublish the French node.
    $content_moderation_state = ContentModerationState::load(1);
    $content_moderation_state = $content_moderation_state->getTranslation('fr');
    $content_moderation_state->set('moderation_state', 'draft');
    $content_moderation_state->setNewRevision(TRUE);
    // Revision 8 (en, fr).
    $content_moderation_state->save();

369
    $english_node = $this->reloadEntity($english_node, $english_node->getRevisionId());
370
    $this->assertEquals('draft', $english_node->moderation_state->value);
371
    $french_node = $this->reloadEntity($english_node, '8')->getTranslation('fr');
372
    $this->assertEquals('draft', $french_node->moderation_state->value);
373 374 375 376 377
    // Switching the moderation state to an unpublished state should update the
    // entity.
    $this->assertFalse($french_node->isPublished());

    // Get the default english node.
378
    $english_node = $this->reloadEntity($english_node);
379 380 381 382
    $this->assertTrue($english_node->isPublished());
    $this->assertEquals(6, $english_node->getRevisionId());
  }

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
  /**
   * Tests moderation when the moderation_state field has a config override.
   */
  public function testModerationWithFieldConfigOverride() {
    NodeType::create([
      'type' => 'test_type',
    ])->save();

    $workflow = Workflow::load('editorial');
    $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'test_type');
    $workflow->save();

    $fields = $this->container->get('entity_field.manager')->getFieldDefinitions('node', 'test_type');
    $field_config = $fields['moderation_state']->getConfig('test_type');
    $field_config->setLabel('Field Override!');
    $field_config->save();

    $node = Node::create([
      'title' => 'Test node',
      'type' => 'test_type',
    ]);
    $node->save();
    $this->assertFalse($node->isPublished());
    $this->assertEquals('draft', $node->moderation_state->value);

    $node->moderation_state = 'published';
    $node->save();
    $this->assertTrue($node->isPublished());
    $this->assertEquals('published', $node->moderation_state->value);
  }

414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
  /**
   * Tests that entities with special languages can be moderated.
   */
  public function testModerationWithSpecialLanguages() {
    $workflow = Workflow::load('editorial');
    $workflow->getTypePlugin()->addEntityTypeAndBundle('entity_test_rev', 'entity_test_rev');
    $workflow->save();

    // Create a test entity.
    $entity = EntityTestRev::create([
      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    ]);
    $entity->save();
    $this->assertEquals('draft', $entity->moderation_state->value);

    $entity->moderation_state->value = 'published';
    $entity->save();

    $this->assertEquals('published', EntityTestRev::load($entity->id())->moderation_state->value);
  }

435 436 437 438
  /**
   * Tests that a non-translatable entity type with a langcode can be moderated.
   */
  public function testNonTranslatableEntityTypeModeration() {
439
    $workflow = Workflow::load('editorial');
440
    $workflow->getTypePlugin()->addEntityTypeAndBundle('entity_test_rev', 'entity_test_rev');
441 442
    $workflow->save();

443
    // Check that the tested entity type is not translatable.
444
    $entity_type = \Drupal::entityTypeManager()->getDefinition('entity_test_rev');
445 446 447
    $this->assertFalse($entity_type->isTranslatable(), 'The test entity type is not translatable.');

    // Create a test entity.
448 449 450
    $entity = EntityTestRev::create();
    $entity->save();
    $this->assertEquals('draft', $entity->moderation_state->value);
451

452 453
    $entity->moderation_state->value = 'published';
    $entity->save();
454

455
    $this->assertEquals('published', EntityTestRev::load($entity->id())->moderation_state->value);
456 457 458 459 460 461 462
  }

  /**
   * Tests that a non-translatable entity type without a langcode can be
   * moderated.
   */
  public function testNonLangcodeEntityTypeModeration() {
463 464 465 466 467 468 469 470 471
    // Unset the langcode entity key for 'entity_test_rev'.
    $entity_type = clone \Drupal::entityTypeManager()->getDefinition('entity_test_rev');
    $keys = $entity_type->getKeys();
    unset($keys['langcode']);
    $entity_type->set('entity_keys', $keys);
    \Drupal::state()->set('entity_test_rev.entity_type', $entity_type);

    // Update the entity type in order to remove the 'langcode' field.
    \Drupal::entityDefinitionUpdateManager()->applyUpdates();
472

473
    $workflow = Workflow::load('editorial');
474
    $workflow->getTypePlugin()->addEntityTypeAndBundle('entity_test_rev', 'entity_test_rev');
475 476
    $workflow->save();

477 478 479
    // Check that the tested entity type is not translatable and does not have a
    // 'langcode' entity key.
    $entity_type = \Drupal::entityTypeManager()->getDefinition('entity_test_rev');
480
    $this->assertFalse($entity_type->isTranslatable(), 'The test entity type is not translatable.');
481
    $this->assertFalse($entity_type->getKey('langcode'), "The test entity type does not have a 'langcode' entity key.");
482 483

    // Create a test entity.
484 485 486
    $entity = EntityTestRev::create();
    $entity->save();
    $this->assertEquals('draft', $entity->moderation_state->value);
487

488 489
    $entity->moderation_state->value = 'published';
    $entity->save();
490

491
    $this->assertEquals('published', EntityTestRev::load($entity->id())->moderation_state->value);
492 493
  }

494 495 496 497 498 499 500 501 502 503 504 505 506
  /**
   * Tests the dependencies of the workflow when using content moderation.
   */
  public function testWorkflowDependencies() {
    $node_type = NodeType::create([
      'type' => 'example',
    ]);
    $node_type->save();

    $workflow = Workflow::load('editorial');
    // Test both a config and non-config based bundle and entity type.
    $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
    $workflow->getTypePlugin()->addEntityTypeAndBundle('entity_test_rev', 'entity_test_rev');
507
    $workflow->getTypePlugin()->addEntityTypeAndBundle('entity_test_no_bundle', 'entity_test_no_bundle');
508 509 510 511 512 513 514 515 516 517 518 519
    $workflow->save();

    $this->assertEquals([
      'module' => [
        'content_moderation',
        'entity_test',
      ],
      'config' => [
        'node.type.example',
      ],
    ], $workflow->getDependencies());

520 521 522 523 524
    $this->assertEquals([
      'entity_test_no_bundle',
      'entity_test_rev',
      'node'
    ], $workflow->getTypePlugin()->getEntityTypes());
525 526 527 528 529 530 531 532 533 534 535

    // Delete the node type and ensure it is removed from the workflow.
    $node_type->delete();
    $workflow = Workflow::load('editorial');
    $entity_types = $workflow->getTypePlugin()->getEntityTypes();
    $this->assertFalse(in_array('node', $entity_types));

    // Uninstall entity test and ensure it's removed from the workflow.
    $this->container->get('config.manager')->uninstall('module', 'entity_test');
    $workflow = Workflow::load('editorial');
    $entity_types = $workflow->getTypePlugin()->getEntityTypes();
536
    $this->assertEquals([], $entity_types);
537 538
  }

539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
  /**
   * Test the content moderation workflow dependencies for non-config bundles.
   */
  public function testWorkflowNonConfigBundleDependencies() {
    // Create a bundle not based on any particular configuration.
    entity_test_create_bundle('test_bundle');

    $workflow = Workflow::load('editorial');
    $workflow->getTypePlugin()->addEntityTypeAndBundle('entity_test', 'test_bundle');
    $workflow->save();

    // Ensure the bundle is correctly added to the workflow.
    $this->assertEquals([
      'module' => [
        'content_moderation',
        'entity_test',
      ],
    ], $workflow->getDependencies());
    $this->assertEquals([
      'test_bundle',
    ], $workflow->getTypePlugin()->getBundlesForEntityType('entity_test'));

    // Delete the test bundle to ensure the workflow entity responds
    // appropriately.
    entity_test_delete_bundle('test_bundle');

    $workflow = Workflow::load('editorial');
    $this->assertEquals([], $workflow->getTypePlugin()->getBundlesForEntityType('entity_test'));
    $this->assertEquals([
      'module' => [
        'content_moderation',
      ],
    ], $workflow->getDependencies());
  }

574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
  /**
   * Test the revision default state of the moderation state entity revisions.
   *
   * @param string $entity_type_id
   *   The ID of entity type to be tested.
   *
   * @dataProvider basicModerationTestCases
   */
  public function testRevisionDefaultState($entity_type_id) {
    // Check that the revision default state of the moderated entity and the
    // content moderation state entity always match.
    /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
    $storage = $this->entityTypeManager->getStorage($entity_type_id);
    /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $cms_storage */
    $cms_storage = $this->entityTypeManager->getStorage('content_moderation_state');

    $entity = $this->createEntity($entity_type_id);
    $entity->get('moderation_state')->value = 'published';
    $storage->save($entity);
    /** @var \Drupal\Core\Entity\ContentEntityInterface $cms_entity */
    $cms_entity = $cms_storage->loadUnchanged(1);
    $this->assertEquals($entity->getLoadedRevisionId(), $cms_entity->get('content_entity_revision_id')->value);

    $entity->get('moderation_state')->value = 'published';
    $storage->save($entity);
    /** @var \Drupal\Core\Entity\ContentEntityInterface $cms_entity */
    $cms_entity = $cms_storage->loadUnchanged(1);
    $this->assertEquals($entity->getLoadedRevisionId(), $cms_entity->get('content_entity_revision_id')->value);

    $entity->get('moderation_state')->value = 'draft';
    $storage->save($entity);
    /** @var \Drupal\Core\Entity\ContentEntityInterface $cms_entity */
    $cms_entity = $cms_storage->loadUnchanged(1);
    $this->assertEquals($entity->getLoadedRevisionId() - 1, $cms_entity->get('content_entity_revision_id')->value);

    $entity->get('moderation_state')->value = 'published';
    $storage->save($entity);
    /** @var \Drupal\Core\Entity\ContentEntityInterface $cms_entity */
    $cms_entity = $cms_storage->loadUnchanged(1);
    $this->assertEquals($entity->getLoadedRevisionId(), $cms_entity->get('content_entity_revision_id')->value);
  }

616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
  /**
   * Creates an entity.
   *
   * The entity will have required fields populated and the corresponding bundle
   * will be enabled for content moderation.
   *
   * @param string $entity_type_id
   *   The entity type ID.
   *
   * @return \Drupal\Core\Entity\ContentEntityInterface
   *   The created entity.
   */
  protected function createEntity($entity_type_id) {
    $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);

    $bundle_id = $entity_type_id;
    // Set up a bundle entity type for the specified entity type, if needed.
    if ($bundle_entity_type_id = $entity_type->getBundleEntityType()) {
      $bundle_entity_type = $this->entityTypeManager->getDefinition($bundle_entity_type_id);
      $bundle_entity_storage = $this->entityTypeManager->getStorage($bundle_entity_type_id);

      $bundle_id = 'example';
      if (!$bundle_entity_storage->load($bundle_id)) {
        $bundle_entity = $bundle_entity_storage->create([
          $bundle_entity_type->getKey('id') => 'example',
        ]);
        if ($entity_type_id == 'media') {
          $bundle_entity->set('source', 'test');
          $bundle_entity->save();
          $source_field = $bundle_entity->getSource()->createSourceField($bundle_entity);
          $source_field->getFieldStorageDefinition()->save();
          $source_field->save();
          $bundle_entity->set('source_configuration', [
            'source_field' => $source_field->getName(),
          ]);
        }
        $bundle_entity->save();
      }
    }

    $workflow = Workflow::load('editorial');
    $workflow->getTypePlugin()->addEntityTypeAndBundle($entity_type_id, $bundle_id);
    $workflow->save();

    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity_storage = $this->entityTypeManager->getStorage($entity_type_id);
    $entity = $entity_storage->create([
      $entity_type->getKey('label') => 'Test title',
      $entity_type->getKey('bundle') => $bundle_id,
    ]);
    // Make sure we add values for all of the required fields.
    if ($entity_type_id == 'block_content') {
      $entity->info = $this->randomString();
    }
    return $entity;
  }

673
  /**
674
   * Reloads the entity after clearing the static cache.
675
   *
676 677 678
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity to reload.
   * @param int|bool $revision_id
679 680 681
   *   The specific revision ID to load. Defaults FALSE and just loads the
   *   default revision.
   *
682 683
   * @return \Drupal\Core\Entity\EntityInterface
   *   The reloaded entity.
684
   */
685 686 687
  protected function reloadEntity(EntityInterface $entity, $revision_id = FALSE) {
    $storage = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId());
    $storage->resetCache([$entity->id()]);
688 689 690
    if ($revision_id) {
      return $storage->loadRevision($revision_id);
    }
691
    return $storage->load($entity->id());
692 693 694
  }

}