Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / content_moderation / tests / src / Kernel / ModerationStateWidgetTest.php
1 <?php
2
3 namespace Drupal\Tests\content_moderation\Kernel;
4
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;
12
13 /**
14  * @coversDefaultClass \Drupal\content_moderation\Plugin\Field\FieldWidget\ModerationStateWidget
15  * @group content_moderation
16  */
17 class ModerationStateWidgetTest extends KernelTestBase {
18
19   /**
20    * Modules to install.
21    *
22    * @var array
23    */
24   public static $modules = [
25     'system',
26     'user',
27     'workflows',
28     'content_moderation',
29     'node',
30   ];
31
32   /**
33    * {@inheritdoc}
34    */
35   protected function setUp() {
36     parent::setUp();
37
38     $this->installEntitySchema('content_moderation_state');
39     $this->installEntitySchema('user');
40     $this->installConfig(['content_moderation', 'system']);
41
42     NodeType::create([
43       'type' => 'moderated',
44     ])->save();
45     NodeType::create([
46       'type' => 'unmoderated',
47     ])->save();
48
49     $workflow = Workflow::load('editorial');
50     $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'moderated');
51     $workflow->save();
52   }
53
54   /**
55    * Test the widget does not impact a non-moderated entity.
56    */
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',
62     ]);
63     $entity_form_display = EntityFormDisplay::create([
64       'targetEntityType' => 'node',
65       'bundle' => 'unmoderated',
66       'mode' => 'default',
67       'status' => TRUE,
68     ]);
69     $form = [];
70     $form_state = new FormState();
71     $entity_form_display->buildForm($entity, $form, $form_state);
72
73     // The moderation_state field should have no values for an entity that isn't
74     // being moderated.
75     $entity_form_display->extractFormValues($entity, $form, $form_state);
76     $this->assertEquals(0, $entity->moderation_state->count());
77   }
78
79   /**
80    * @covers ::isApplicable
81    */
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));
90   }
91
92 }