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