c2ea8c576d514d106e5ba92f0778ff9e686af831
[yaffs-website] / web / core / modules / content_moderation / tests / src / Kernel / ContentModerationWorkflowTypeApiTest.php
1 <?php
2
3 namespace Drupal\Tests\content_moderation\Kernel;
4
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\workflows\Entity\Workflow;
7
8 /**
9  * Tests the API of the ContentModeration workflow type plugin.
10  *
11  * @group content_moderation
12  *
13  * @coversDefaultClass \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration
14  */
15 class ContentModerationWorkflowTypeApiTest extends KernelTestBase {
16
17   /**
18    * A workflow for testing.
19    *
20    * @var \Drupal\workflows\Entity\Workflow;
21    */
22   protected $workflow;
23
24   /**
25    * Modules to install.
26    *
27    * @var array
28    */
29   public static $modules = [
30     'workflows',
31     'content_moderation',
32   ];
33
34   /**
35    * {@inheritdoc}
36    */
37   protected function setUp() {
38     parent::setUp();
39     $this->workflow = Workflow::create(['id' => 'test', 'type' => 'content_moderation']);
40     $this->workflow
41       ->addState('draft', 'Draft')
42       ->addState('published', 'Published');
43   }
44
45   /**
46    * @covers ::getBundlesForEntityType
47    * @covers ::addEntityTypeAndBundle
48    * @covers ::removeEntityTypeAndBundle
49    */
50   public function testGetBundlesForEntityType() {
51     /** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration $workflow_plugin */
52     $workflow_plugin = $this->workflow->getTypePlugin();
53     // The content moderation plugin does not validate the existence of the
54     // entity type or bundle.
55     $this->assertEquals([], $workflow_plugin->getBundlesForEntityType('fake_node'));
56     $workflow_plugin->addEntityTypeAndBundle('fake_node', 'fake_page');
57     $this->assertEquals(['fake_page'], $workflow_plugin->getBundlesForEntityType('fake_node'));
58     $this->assertEquals([], $workflow_plugin->getBundlesForEntityType('fake_block'));
59     $workflow_plugin->removeEntityTypeAndBundle('fake_node', 'fake_page');
60     $this->assertEquals([], $workflow_plugin->getBundlesForEntityType('fake_node'));
61   }
62
63   /**
64    * @covers ::appliesToEntityTypeAndBundle
65    * @covers ::addEntityTypeAndBundle
66    * @covers ::removeEntityTypeAndBundle
67    */
68   public function testAppliesToEntityTypeAndBundle() {
69     /** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration $workflow_plugin */
70     $workflow_plugin = $this->workflow->getTypePlugin();
71     // The content moderation plugin does not validate the existence of the
72     // entity type or bundle.
73     $this->assertFalse($workflow_plugin->appliesToEntityTypeAndBundle('fake_node', 'fake_page'));
74     $workflow_plugin->addEntityTypeAndBundle('fake_node', 'fake_page');
75     $this->assertTrue($workflow_plugin->appliesToEntityTypeAndBundle('fake_node', 'fake_page'));
76     $this->assertFalse($workflow_plugin->appliesToEntityTypeAndBundle('fake_block', 'fake_custom'));
77     $workflow_plugin->removeEntityTypeAndBundle('fake_node', 'fake_page');
78     $this->assertFalse($workflow_plugin->appliesToEntityTypeAndBundle('fake_node', 'fake_page'));
79   }
80
81   /**
82    * @covers ::addEntityTypeAndBundle
83    */
84   public function testAddEntityTypeAndBundle() {
85     /** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration $workflow_plugin */
86     $workflow_plugin = $this->workflow->getTypePlugin();
87
88     // The bundles are intentionally added in reverse alphabetical order.
89     $workflow_plugin->addEntityTypeAndBundle('fake_node', 'fake_page');
90     $workflow_plugin->addEntityTypeAndBundle('fake_node', 'fake_article');
91
92     // Add another entity type that comes alphabetically before 'fake_node'.
93     $workflow_plugin->addEntityTypeAndBundle('fake_block', 'fake_custom');
94
95     // The entity type keys and bundle values should be sorted alphabetically.
96     // The bundle array index should not reflect the order in which they are
97     // added.
98     $this->assertSame(
99       ['fake_block' => ['fake_custom'], 'fake_node' => ['fake_article', 'fake_page']],
100       $workflow_plugin->getConfiguration()['entity_types']
101     );
102   }
103
104 }