From 05f42e4b07f00b74a4be02fb81686b12804aad3b Mon Sep 17 00:00:00 2001 From: xjm Date: Tue, 7 Feb 2017 15:04:38 -0600 Subject: [PATCH] Issue #2850341 by alexpott, xjm, timmillwood: Improve \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration::appliesToEntityTypeAndBundle() and ::getBundlesForEntityType() --- .../Plugin/WorkflowType/ContentModeration.php | 27 ++++--- .../ContentModertaionWorkflowTypeApiTest.php | 81 +++++++++++++++++++ 2 files changed, 97 insertions(+), 11 deletions(-) create mode 100644 core/modules/content_moderation/tests/src/Kernel/ContentModertaionWorkflowTypeApiTest.php diff --git a/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php b/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php index e61f25ea1f9..c6822137cb8 100644 --- a/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php +++ b/core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php @@ -82,30 +82,33 @@ class ContentModeration extends WorkflowTypeBase { } /** - * Gets the bundles of the entity type the workflow is applied to. + * Gets any bundles the workflow is applied to for the given entity type. * * @param string $entity_type_id * The entity type ID to get the bundles for. * * @return string[] - * The bundles of the entity type the workflow is applied to. + * The bundles of the entity type the workflow is applied to or an empty + * array if the entity type is not applied to the workflow. */ public function getBundlesForEntityType($entity_type_id) { - return $this->configuration['entity_types'][$entity_type_id]; + return isset($this->configuration['entity_types'][$entity_type_id]) ? $this->configuration['entity_types'][$entity_type_id] : []; } /** * Checks if the workflow applies to the supplied entity type and bundle. * + * @param string $entity_type_id + * The entity type ID to check. + * @param string $bundle_id + * The bundle ID to check. + * * @return bool - * TRUE if the workflow applies to the supplied entity type and bundle. - * FALSE if not. + * TRUE if the workflow applies to the supplied entity type ID and bundle + * ID. FALSE if not. */ public function appliesToEntityTypeAndBundle($entity_type_id, $bundle_id) { - if (isset($this->configuration['entity_types'][$entity_type_id])) { - return in_array($bundle_id, $this->configuration['entity_types'][$entity_type_id], TRUE); - } - return FALSE; + return in_array($bundle_id, $this->getBundlesForEntityType($entity_type_id), TRUE); } /** @@ -133,9 +136,11 @@ class ContentModeration extends WorkflowTypeBase { * Add an entity type ID / bundle ID to the workflow. * * @param string $entity_type_id - * The entity type ID to add. + * The entity type ID to add. It is responsibility of the caller to provide + * a valid entity type ID. * @param string $bundle_id - * The bundle ID to add. + * The bundle ID to add. It is responsibility of the caller to provide a + * valid bundle ID. */ public function addEntityTypeAndBundle($entity_type_id, $bundle_id) { if (!$this->appliesToEntityTypeAndBundle($entity_type_id, $bundle_id)) { diff --git a/core/modules/content_moderation/tests/src/Kernel/ContentModertaionWorkflowTypeApiTest.php b/core/modules/content_moderation/tests/src/Kernel/ContentModertaionWorkflowTypeApiTest.php new file mode 100644 index 00000000000..37464e0c2a8 --- /dev/null +++ b/core/modules/content_moderation/tests/src/Kernel/ContentModertaionWorkflowTypeApiTest.php @@ -0,0 +1,81 @@ +workflow = Workflow::create(['id' => 'test', 'type' => 'content_moderation']); + $this->workflow + ->addState('draft', 'Draft') + ->addState('published', 'Published'); + } + + /** + * @covers ::getBundlesForEntityType + * @covers ::addEntityTypeAndBundle + * @covers ::removeEntityTypeAndBundle + */ + public function testGetBundlesForEntityType() { + /** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration $workflow_plugin */ + $workflow_plugin = $this->workflow->getTypePlugin(); + // The content moderation plugin does not valid the existence of the entity + // type or bundle. + $this->assertEquals([], $workflow_plugin->getBundlesForEntityType('fake_node')); + $workflow_plugin->addEntityTypeAndBundle('fake_node', 'fake_page'); + $this->assertEquals(['fake_page'], $workflow_plugin->getBundlesForEntityType('fake_node')); + $this->assertEquals([], $workflow_plugin->getBundlesForEntityType('fake_block')); + $workflow_plugin->removeEntityTypeAndBundle('fake_node', 'fake_page'); + $this->assertEquals([], $workflow_plugin->getBundlesForEntityType('fake_node')); + } + + /** + * @covers ::appliesToEntityTypeAndBundle + * @covers ::addEntityTypeAndBundle + * @covers ::removeEntityTypeAndBundle + */ + public function testAppliesToEntityTypeAndBundle() { + /** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration $workflow_plugin */ + $workflow_plugin = $this->workflow->getTypePlugin(); + // The content moderation plugin does not valid the existence of the entity + // type or bundle. + $this->assertFalse($workflow_plugin->appliesToEntityTypeAndBundle('fake_node', 'fake_page')); + $workflow_plugin->addEntityTypeAndBundle('fake_node', 'fake_page'); + $this->assertTrue($workflow_plugin->appliesToEntityTypeAndBundle('fake_node', 'fake_page')); + $this->assertFalse($workflow_plugin->appliesToEntityTypeAndBundle('fake_block', 'fake_custom')); + $workflow_plugin->removeEntityTypeAndBundle('fake_node', 'fake_page'); + $this->assertFalse($workflow_plugin->appliesToEntityTypeAndBundle('fake_node', 'fake_page')); + } + +}