3 namespace Drupal\Tests\content_moderation\Kernel;
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\workflows\Entity\Workflow;
9 * Tests the API of the ContentModeration workflow type plugin.
11 * @group content_moderation
13 * @coversDefaultClass \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration
15 class ContentModerationWorkflowTypeApiTest extends KernelTestBase {
18 * A workflow for testing.
20 * @var \Drupal\workflows\Entity\Workflow;
29 public static $modules = [
37 protected function setUp() {
39 $this->workflow = Workflow::create(['id' => 'test', 'type' => 'content_moderation']);
43 * @covers ::getBundlesForEntityType
44 * @covers ::addEntityTypeAndBundle
45 * @covers ::removeEntityTypeAndBundle
47 public function testGetBundlesForEntityType() {
48 /** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration $workflow_plugin */
49 $workflow_plugin = $this->workflow->getTypePlugin();
50 // The content moderation plugin does not validate the existence of the
51 // entity type or bundle.
52 $this->assertEquals([], $workflow_plugin->getBundlesForEntityType('fake_node'));
53 $workflow_plugin->addEntityTypeAndBundle('fake_node', 'fake_page');
54 $this->assertEquals(['fake_page'], $workflow_plugin->getBundlesForEntityType('fake_node'));
55 $this->assertEquals([], $workflow_plugin->getBundlesForEntityType('fake_block'));
56 $workflow_plugin->removeEntityTypeAndBundle('fake_node', 'fake_page');
57 $this->assertEquals([], $workflow_plugin->getBundlesForEntityType('fake_node'));
61 * @covers ::appliesToEntityTypeAndBundle
62 * @covers ::addEntityTypeAndBundle
63 * @covers ::removeEntityTypeAndBundle
65 public function testAppliesToEntityTypeAndBundle() {
66 /** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration $workflow_plugin */
67 $workflow_plugin = $this->workflow->getTypePlugin();
68 // The content moderation plugin does not validate the existence of the
69 // entity type or bundle.
70 $this->assertFalse($workflow_plugin->appliesToEntityTypeAndBundle('fake_node', 'fake_page'));
71 $workflow_plugin->addEntityTypeAndBundle('fake_node', 'fake_page');
72 $this->assertTrue($workflow_plugin->appliesToEntityTypeAndBundle('fake_node', 'fake_page'));
73 $this->assertFalse($workflow_plugin->appliesToEntityTypeAndBundle('fake_block', 'fake_custom'));
74 $workflow_plugin->removeEntityTypeAndBundle('fake_node', 'fake_page');
75 $this->assertFalse($workflow_plugin->appliesToEntityTypeAndBundle('fake_node', 'fake_page'));
79 * @covers ::addEntityTypeAndBundle
81 public function testAddEntityTypeAndBundle() {
82 /** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration $workflow_plugin */
83 $workflow_plugin = $this->workflow->getTypePlugin();
85 // The bundles are intentionally added in reverse alphabetical order.
86 $workflow_plugin->addEntityTypeAndBundle('fake_node', 'fake_page');
87 $workflow_plugin->addEntityTypeAndBundle('fake_node', 'fake_article');
89 // Add another entity type that comes alphabetically before 'fake_node'.
90 $workflow_plugin->addEntityTypeAndBundle('fake_block', 'fake_custom');
92 // The entity type keys and bundle values should be sorted alphabetically.
93 // The bundle array index should not reflect the order in which they are
96 ['fake_block' => ['fake_custom'], 'fake_node' => ['fake_article', 'fake_page']],
97 $workflow_plugin->getConfiguration()['entity_types']
102 * @covers ::addEntityTypeAndBundle
103 * @covers ::removeEntityTypeAndBundle
105 public function testRemoveEntityTypeAndBundle() {
106 /** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration $workflow_plugin */
107 $workflow_plugin = $this->workflow->getTypePlugin();
109 // There should be no bundles for fake_node to start with.
110 $this->assertEquals([], $workflow_plugin->getBundlesForEntityType('fake_node'));
111 // Removing a bundle which is not set on the workflow should not throw an
112 // error and should still result in none being returned.
113 $workflow_plugin->removeEntityTypeAndBundle('fake_node', 'fake_page');
114 $this->assertEquals([], $workflow_plugin->getBundlesForEntityType('fake_node'));
115 // Adding a bundle for fake_node should result it in being returned, but
116 // then removing it will return no bundles for fake_node.
117 $workflow_plugin->addEntityTypeAndBundle('fake_node', 'fake_page');
118 $this->assertEquals(['fake_page'], $workflow_plugin->getBundlesForEntityType('fake_node'));
119 $workflow_plugin->removeEntityTypeAndBundle('fake_node', 'fake_page');
120 $this->assertEquals([], $workflow_plugin->getBundlesForEntityType('fake_node'));