Issue #2850341 by alexpott, xjm, timmillwood: Improve \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration::appliesToEntityTypeAndBundle() and ::getBundlesForEntityType()

8.4.x
xjm 2017-02-07 15:04:38 -06:00
parent 38b3dd1072
commit 05f42e4b07
2 changed files with 97 additions and 11 deletions

View File

@ -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 * @param string $entity_type_id
* The entity type ID to get the bundles for. * The entity type ID to get the bundles for.
* *
* @return string[] * @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) { 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. * 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 * @return bool
* TRUE if the workflow applies to the supplied entity type and bundle. * TRUE if the workflow applies to the supplied entity type ID and bundle
* FALSE if not. * ID. FALSE if not.
*/ */
public function appliesToEntityTypeAndBundle($entity_type_id, $bundle_id) { public function appliesToEntityTypeAndBundle($entity_type_id, $bundle_id) {
if (isset($this->configuration['entity_types'][$entity_type_id])) { return in_array($bundle_id, $this->getBundlesForEntityType($entity_type_id), TRUE);
return in_array($bundle_id, $this->configuration['entity_types'][$entity_type_id], TRUE);
}
return FALSE;
} }
/** /**
@ -133,9 +136,11 @@ class ContentModeration extends WorkflowTypeBase {
* Add an entity type ID / bundle ID to the workflow. * Add an entity type ID / bundle ID to the workflow.
* *
* @param string $entity_type_id * @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 * @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) { public function addEntityTypeAndBundle($entity_type_id, $bundle_id) {
if (!$this->appliesToEntityTypeAndBundle($entity_type_id, $bundle_id)) { if (!$this->appliesToEntityTypeAndBundle($entity_type_id, $bundle_id)) {

View File

@ -0,0 +1,81 @@
<?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'));
}
}