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