ModerationStateWidgetTest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace Drupal\Tests\content_moderation\Kernel;
  3. use Drupal\content_moderation\Plugin\Field\FieldWidget\ModerationStateWidget;
  4. use Drupal\Core\Entity\Entity\EntityFormDisplay;
  5. use Drupal\Core\Form\FormState;
  6. use Drupal\KernelTests\KernelTestBase;
  7. use Drupal\node\Entity\Node;
  8. use Drupal\node\Entity\NodeType;
  9. use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
  10. /**
  11. * @coversDefaultClass \Drupal\content_moderation\Plugin\Field\FieldWidget\ModerationStateWidget
  12. * @group content_moderation
  13. */
  14. class ModerationStateWidgetTest extends KernelTestBase {
  15. use ContentModerationTestTrait;
  16. /**
  17. * Modules to install.
  18. *
  19. * @var array
  20. */
  21. public static $modules = [
  22. 'system',
  23. 'user',
  24. 'workflows',
  25. 'content_moderation',
  26. 'node',
  27. ];
  28. /**
  29. * {@inheritdoc}
  30. */
  31. protected function setUp() {
  32. parent::setUp();
  33. $this->installEntitySchema('content_moderation_state');
  34. $this->installEntitySchema('user');
  35. $this->installConfig(['content_moderation', 'system']);
  36. NodeType::create([
  37. 'type' => 'moderated',
  38. ])->save();
  39. NodeType::create([
  40. 'type' => 'unmoderated',
  41. ])->save();
  42. $workflow = $this->createEditorialWorkflow();
  43. $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'moderated');
  44. $workflow->save();
  45. }
  46. /**
  47. * Test the widget does not impact a non-moderated entity.
  48. */
  49. public function testWidgetNonModeratedEntity() {
  50. // Create an unmoderated entity and build a form display which will include
  51. // the ModerationStateWidget plugin, in a hidden state.
  52. $entity = Node::create([
  53. 'type' => 'unmoderated',
  54. ]);
  55. $entity_form_display = EntityFormDisplay::create([
  56. 'targetEntityType' => 'node',
  57. 'bundle' => 'unmoderated',
  58. 'mode' => 'default',
  59. 'status' => TRUE,
  60. ]);
  61. $form = [];
  62. $form_state = new FormState();
  63. $entity_form_display->buildForm($entity, $form, $form_state);
  64. // The moderation_state field should have no values for an entity that isn't
  65. // being moderated.
  66. $entity_form_display->extractFormValues($entity, $form, $form_state);
  67. $this->assertEquals(0, $entity->moderation_state->count());
  68. }
  69. /**
  70. * @covers ::isApplicable
  71. */
  72. public function testIsApplicable() {
  73. // The moderation_state field definition should be applicable to our widget.
  74. $fields = $this->container->get('entity_field.manager')->getFieldDefinitions('node', 'test_type');
  75. $this->assertTrue(ModerationStateWidget::isApplicable($fields['moderation_state']));
  76. $this->assertFalse(ModerationStateWidget::isApplicable($fields['status']));
  77. // A config override should still be applicable.
  78. $field_config = $fields['moderation_state']->getConfig('moderated');
  79. $this->assertTrue(ModerationStateWidget::isApplicable($field_config));
  80. }
  81. }