ContentModerationStateTest.php 25.4 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\Tests\content_moderation\Traits\ContentModerationTestTrait;
16
use Drupal\workflows\Entity\Workflow;
17 18 19 20 21 22 23 24

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

25 26
  use ContentModerationTestTrait;

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

49 50 51 52 53
  /**
   * @var \Drupal\Core\Entity\EntityTypeManager
   */
  protected $entityTypeManager;

54 55 56 57 58 59 60 61 62
  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

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

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

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

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

94 95
    $entity = $this->reloadEntity($entity);
    $this->assertEquals('published', $entity->moderation_state->value);
96 97 98 99 100 101 102

    // 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();

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

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

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

119 120
    $entity = $this->reloadEntity($entity, 4);
    $this->assertEquals('published', $entity->moderation_state->value);
121 122

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

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

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

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

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

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

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

194 195 196 197 198 199 200 201 202 203
  /**
   * 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;
204 205 206
    $revision->isDefaultRevision(FALSE);
    $content_moderation_state = ContentModerationState::loadFromModeratedEntity($revision);
    $this->assertTrue($content_moderation_state);
207 208 209
    $entity = $this->reloadEntity($entity);
    $entity->setNewRevision(TRUE);
    $entity->save();
210
    $entity_storage = $this->entityTypeManager->getStorage($entity_type_id);
211 212 213
    $entity_storage->deleteRevision($revision->getRevisionId());
    $content_moderation_state = ContentModerationState::loadFromModeratedEntity($revision);
    $this->assertFalse($content_moderation_state);
214
    $content_moderation_state = ContentModerationState::loadFromModeratedEntity($entity);
215
    $this->assertTrue($content_moderation_state);
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 244 245 246
  /**
   * 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) {
247 248
    // Test content moderation state translation deletion.
    if ($this->entityTypeManager->getDefinition($entity_type_id)->isTranslatable()) {
249 250
      /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
      $entity = $this->createEntity($entity_type_id);
251 252 253
      $langcode = 'it';
      ConfigurableLanguage::createFromLangcode($langcode)
        ->save();
254 255
      $entity->save();
      $translation = $entity->addTranslation($langcode, ['title' => 'Titolo test']);
256 257 258 259
      // Make sure we add values for all of the required fields.
      if ($entity_type_id == 'block_content') {
        $translation->info = $this->randomString();
      }
260
      $translation->save();
261
      $content_moderation_state = ContentModerationState::loadFromModeratedEntity($entity);
262
      $this->assertTrue($content_moderation_state->hasTranslation($langcode));
263 264 265
      $entity->removeTranslation($langcode);
      $entity->save();
      $content_moderation_state = ContentModerationState::loadFromModeratedEntity($entity);
266 267 268 269
      $this->assertFalse($content_moderation_state->hasTranslation($langcode));
    }
  }

270 271 272 273 274 275 276 277 278 279
  /**
   * 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();
280

281
    $workflow = $this->createEditorialWorkflow();
282 283 284
    $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
    $workflow->save();

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

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

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

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

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

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

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

    // Republish the French node.
346
    $french_node->moderation_state->value = 'published';
347 348
    // Revision 6 (en, fr).
    $french_node->save();
349
    $french_node = $this->reloadEntity($english_node)->getTranslation('fr');
350 351 352 353 354 355 356 357
    $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();
358
    $english_node = $this->reloadEntity($french_node, $french_node->getRevisionId() + 1);
359

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

    // 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();

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

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

386 387 388 389 390 391 392 393
  /**
   * Tests moderation when the moderation_state field has a config override.
   */
  public function testModerationWithFieldConfigOverride() {
    NodeType::create([
      'type' => 'test_type',
    ])->save();

394
    $workflow = $this->createEditorialWorkflow();
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
    $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);
  }

417 418 419 420
  /**
   * Tests that entities with special languages can be moderated.
   */
  public function testModerationWithSpecialLanguages() {
421
    $workflow = $this->createEditorialWorkflow();
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
    $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);
  }

438 439 440 441
  /**
   * Tests that a non-translatable entity type with a langcode can be moderated.
   */
  public function testNonTranslatableEntityTypeModeration() {
442
    $workflow = $this->createEditorialWorkflow();
443
    $workflow->getTypePlugin()->addEntityTypeAndBundle('entity_test_rev', 'entity_test_rev');
444 445
    $workflow->save();

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

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

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

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

  /**
   * Tests that a non-translatable entity type without a langcode can be
   * moderated.
   */
  public function testNonLangcodeEntityTypeModeration() {
466 467 468 469 470 471 472 473 474
    // 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();
475

476
    $workflow = $this->createEditorialWorkflow();
477
    $workflow->getTypePlugin()->addEntityTypeAndBundle('entity_test_rev', 'entity_test_rev');
478 479
    $workflow->save();

480 481 482
    // 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');
483
    $this->assertFalse($entity_type->isTranslatable(), 'The test entity type is not translatable.');
484
    $this->assertFalse($entity_type->getKey('langcode'), "The test entity type does not have a 'langcode' entity key.");
485 486

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

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

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

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

506
    $workflow = $this->createEditorialWorkflow();
507 508 509
    // 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');
510
    $workflow->getTypePlugin()->addEntityTypeAndBundle('entity_test_no_bundle', 'entity_test_no_bundle');
511 512 513 514 515 516 517 518 519 520 521 522
    $workflow->save();

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

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

    // 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();
539
    $this->assertEquals([], $entity_types);
540 541
  }

542 543 544 545 546 547 548
  /**
   * 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');

549
    $workflow = $this->createEditorialWorkflow();
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
    $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());
  }

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 616 617 618
  /**
   * 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);
  }

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
  /**
   * 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();
      }
    }

659
    $workflow = $this->createEditorialWorkflow();
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
    $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;
  }

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

}