ModerationStateWidgetTest.php in Drupal 10
File
core/modules/content_moderation/tests/src/Kernel/ModerationStateWidgetTest.php
View source
<?php
namespace Drupal\Tests\content_moderation\Kernel;
use Drupal\content_moderation\Plugin\Field\FieldWidget\ModerationStateWidget;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Form\FormState;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
class ModerationStateWidgetTest extends KernelTestBase {
use ContentModerationTestTrait;
protected static $modules = [
'system',
'user',
'workflows',
'content_moderation',
'node',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('content_moderation_state');
$this
->installEntitySchema('user');
$this
->installConfig([
'content_moderation',
'system',
]);
NodeType::create([
'type' => 'moderated',
])
->save();
NodeType::create([
'type' => 'unmoderated',
])
->save();
$workflow = $this
->createEditorialWorkflow();
$workflow
->getTypePlugin()
->addEntityTypeAndBundle('node', 'moderated');
$workflow
->save();
}
public function testWidgetNonModeratedEntity() {
$entity = Node::create([
'type' => 'unmoderated',
]);
$entity_form_display = EntityFormDisplay::create([
'targetEntityType' => 'node',
'bundle' => 'unmoderated',
'mode' => 'default',
'status' => TRUE,
]);
$form = [];
$form_state = new FormState();
$entity_form_display
->buildForm($entity, $form, $form_state);
$entity_form_display
->extractFormValues($entity, $form, $form_state);
$this
->assertEquals(0, $entity->moderation_state
->count());
}
public function testIsApplicable() {
$fields = $this->container
->get('entity_field.manager')
->getFieldDefinitions('node', 'test_type');
$this
->assertTrue(ModerationStateWidget::isApplicable($fields['moderation_state']));
$this
->assertFalse(ModerationStateWidget::isApplicable($fields['status']));
$field_config = $fields['moderation_state']
->getConfig('moderated');
$this
->assertTrue(ModerationStateWidget::isApplicable($field_config));
}
}