Commit 05f42e4b authored by xjm's avatar xjm

Issue #2850341 by alexpott, xjm, timmillwood: Improve...

Issue #2850341 by alexpott, xjm, timmillwood: Improve \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration::appliesToEntityTypeAndBundle() and ::getBundlesForEntityType()
parent 38b3dd10
......@@ -82,30 +82,33 @@ public function getEntityTypes() {
}
/**
* 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 @@ public function removeEntityTypeAndBundle($entity_type_id, $bundle_id) {
* 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)) {
......
<?php
namespace Drupal\Tests\content_moderation\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\workflows\Entity\Workflow;
/**
* Tests the API of the ContentModeration workflow type plugin.
*
* @group content_moderation
*
* @coversDefaultClass \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration
*/
class ContentModertaionWorkflowTypeApiTest extends KernelTestBase {
/**
* A workflow for testing.
*
* @var \Drupal\workflows\Entity\Workflow;
*/
protected $workflow;
/**
* Modules to install.
*
* @var array
*/
public static $modules = [
'workflows',
'content_moderation',
];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->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'));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment