3 namespace Drupal\Tests\content_moderation\Kernel;
5 use Drupal\content_moderation\Plugin\Field\FieldWidget\ModerationStateWidget;
6 use Drupal\Core\Entity\Entity\EntityFormDisplay;
7 use Drupal\Core\Form\FormState;
8 use Drupal\KernelTests\KernelTestBase;
9 use Drupal\node\Entity\Node;
10 use Drupal\node\Entity\NodeType;
11 use Drupal\workflows\Entity\Workflow;
14 * @coversDefaultClass \Drupal\content_moderation\Plugin\Field\FieldWidget\ModerationStateWidget
15 * @group content_moderation
17 class ModerationStateWidgetTest extends KernelTestBase {
24 public static $modules = [
35 protected function setUp() {
38 $this->installEntitySchema('content_moderation_state');
39 $this->installEntitySchema('user');
40 $this->installConfig(['content_moderation', 'system']);
43 'type' => 'moderated',
46 'type' => 'unmoderated',
49 $workflow = Workflow::load('editorial');
50 $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'moderated');
55 * Test the widget does not impact a non-moderated entity.
57 public function testWidgetNonModeratedEntity() {
58 // Create an unmoderated entity and build a form display which will include
59 // the ModerationStateWidget plugin, in a hidden state.
60 $entity = Node::create([
61 'type' => 'unmoderated',
63 $entity_form_display = EntityFormDisplay::create([
64 'targetEntityType' => 'node',
65 'bundle' => 'unmoderated',
70 $form_state = new FormState();
71 $entity_form_display->buildForm($entity, $form, $form_state);
73 // The moderation_state field should have no values for an entity that isn't
75 $entity_form_display->extractFormValues($entity, $form, $form_state);
76 $this->assertEquals(0, $entity->moderation_state->count());
80 * @covers ::isApplicable
82 public function testIsApplicable() {
83 // The moderation_state field definition should be applicable to our widget.
84 $fields = $this->container->get('entity_field.manager')->getFieldDefinitions('node', 'test_type');
85 $this->assertTrue(ModerationStateWidget::isApplicable($fields['moderation_state']));
86 $this->assertFalse(ModerationStateWidget::isApplicable($fields['status']));
87 // A config override should still be applicable.
88 $field_config = $fields['moderation_state']->getConfig('moderated');
89 $this->assertTrue(ModerationStateWidget::isApplicable($field_config));