7c0e2fca953333f833babdaeb4084c3f9b1dd11f
[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   }
41
42   /**
43    * @covers ::getBundlesForEntityType
44    * @covers ::addEntityTypeAndBundle
45    * @covers ::removeEntityTypeAndBundle
46    */
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'));
58   }
59
60   /**
61    * @covers ::appliesToEntityTypeAndBundle
62    * @covers ::addEntityTypeAndBundle
63    * @covers ::removeEntityTypeAndBundle
64    */
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'));
76   }
77
78   /**
79    * @covers ::addEntityTypeAndBundle
80    */
81   public function testAddEntityTypeAndBundle() {
82     /** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration $workflow_plugin */
83     $workflow_plugin = $this->workflow->getTypePlugin();
84
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');
88
89     // Add another entity type that comes alphabetically before 'fake_node'.
90     $workflow_plugin->addEntityTypeAndBundle('fake_block', 'fake_custom');
91
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
94     // added.
95     $this->assertSame(
96       ['fake_block' => ['fake_custom'], 'fake_node' => ['fake_article', 'fake_page']],
97       $workflow_plugin->getConfiguration()['entity_types']
98     );
99   }
100
101   /**
102    * @covers ::addEntityTypeAndBundle
103    * @covers ::removeEntityTypeAndBundle
104    */
105   public function testRemoveEntityTypeAndBundle() {
106     /** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration $workflow_plugin */
107     $workflow_plugin = $this->workflow->getTypePlugin();
108
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'));
121   }
122
123 }