ContentModerationWorkflowTypeApiTest.php 4.9 KB

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