6d2b8b6b5774366faa3ff93ee6bb58aa6304f375
[yaffs-website] / web / core / modules / content_moderation / tests / src / Kernel / StateFormatterTest.php
1 <?php
2
3 namespace Drupal\Tests\content_moderation\Kernel;
4
5 use Drupal\Core\Render\RenderContext;
6 use Drupal\entity_test\Entity\EntityTestRev;
7 use Drupal\KernelTests\KernelTestBase;
8 use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
9
10 /**
11  * Test the state field formatter.
12  *
13  * @group content_moderation
14  */
15 class StateFormatterTest extends KernelTestBase {
16
17   use ContentModerationTestTrait;
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = [
25     'workflows',
26     'content_moderation',
27     'entity_test',
28     'user',
29   ];
30
31   /**
32    * {@inheritdoc}
33    */
34   protected function setUp() {
35     parent::setUp();
36
37     $this->installEntitySchema('entity_test_rev');
38     $this->installEntitySchema('content_moderation_state');
39     $this->installConfig('content_moderation');
40
41     $workflow = $this->createEditorialWorkflow();
42     $workflow->getTypePlugin()->addEntityTypeAndBundle('entity_test_rev', 'entity_test_rev');
43     $workflow->save();
44   }
45
46   /**
47    * Test the embed field.
48    *
49    * @dataProvider formatterTestCases
50    */
51   public function testStateFieldFormatter($field_value, $formatter_settings, $expected_output) {
52     $entity = EntityTestRev::create([
53       'moderation_state' => $field_value,
54     ]);
55     $entity->save();
56
57     $field_output = $this->container->get('renderer')->executeInRenderContext(new RenderContext(), function () use ($entity, $formatter_settings) {
58       return $entity->moderation_state->view($formatter_settings);
59     });
60
61     $this->assertEquals($expected_output, $field_output[0]);
62   }
63
64   /**
65    * Test cases for ::
66    */
67   public function formatterTestCases() {
68     return [
69       'Draft State' => [
70         'draft',
71         [
72           'type' => 'content_moderation_state',
73           'settings' => [],
74         ],
75         [
76           '#markup' => 'Draft',
77         ],
78       ],
79       'Published State' => [
80         'published',
81         [
82           'type' => 'content_moderation_state',
83           'settings' => [],
84         ],
85         [
86           '#markup' => 'Published',
87         ],
88       ],
89     ];
90   }
91
92 }