Commit 80111e28 authored by pcambra's avatar pcambra

Added test support for WorkflowGroupManager

parent e6edda77
<?php
/**
* @file
* Contains \Drupal\Tests\state_machine\Unit\WorkflowGroupTest.
*/
namespace Drupal\Tests\state_machine\Unit;
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Plugin\Discovery\YamlDiscovery;
use org\bovigo\vfs\vfsStream;
/**
* @coversDefaultClass \Drupal\state_machine\Plugin\WorkflowGroup\WorkflowGroup
* @group Workflow
*/
class WorkflowGroupTest extends WorkflowBaseTestCase {
/**
* Tests the group with a config YAML file
*
* @covers ::getLabel
* @covers ::getEntityTypeId
* @covers ::getWorkflowClass
*/
public function testProcessValidDefinition() {
vfsStream::setup('root');
$group_config = [
'order' => [
'label' => 'Order',
'entity_type' => 'commerce_order',
]
];
$file = Yaml::encode($group_config);
vfsStream::create([
'state_machine_test' => [
'state_machine_test.workflow_groups.yml' => $file,
]]
);
$discovery = new YamlDiscovery('workflow_groups', ['state_machine_test' => vfsStream::url('root/state_machine_test')]);
$this->groupManager->setDiscovery($discovery);
/** @var $workflow_group \Drupal\state_machine\Plugin\WorkflowGroup\WorkflowGroup */
$workflow_group = $this->groupManager->createInstance('order');
$this->assertEquals('Order', $workflow_group->getLabel(), 'Workflow group label matches the expected one');
$this->assertEquals('commerce_order', $workflow_group->getEntityTypeId(), 'Workflow group entity type id matches the expected one');
$this->assertEquals('\Drupal\state_machine\Plugin\Workflow\Workflow', $workflow_group->getWorkflowClass(), 'Workflow group class matches the expected one');
}
}
<?php
/**
* @file
* Contains \Drupal\Tests\state_machine\Unit\WorkflowBaseTest.
*/
namespace Drupal\Tests\state_machine\Unit;
use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\state_machine\WorkflowGroupManager;
use Drupal\Tests\UnitTestCase;
class WorkflowBaseTestCase extends UnitTestCase {
/**
* The cache backend to use.
*
* @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $cache;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $moduleHandler;
/**
* The plugin discovery.
*
* @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $discovery;
/**
* The group manager under test.
*
* @var \Drupal\Tests\state_machine\Unit\TestWorkflowGroupManager
*/
protected $groupManager;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
// Prepare the default constructor arguments required by
// WorkflowGroupManager.
$this->cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
$this->moduleHandler = $this->prophesize(ModuleHandlerInterface::class);
$this->moduleHandler->moduleExists('state_machine_test')->willReturn(TRUE);
$this->groupManager = new TestWorkflowGroupManager($this->moduleHandler->reveal(), $this->cache);
}
}
/**
* Provides a testing version of WorkflowGroupManager with an empty constructor.
*/
class TestWorkflowGroupManager extends WorkflowGroupManager {
/**
* Sets the discovery for the manager.
*
* @param \Drupal\Component\Plugin\Discovery\DiscoveryInterface $discovery
* The discovery object.
*/
public function setDiscovery(DiscoveryInterface $discovery) {
$this->discovery = $discovery;
}
}
<?php
/**
* @file
* Contains \Drupal\Tests\state_machine\Unit\WorkflowGroupManagerTest.
*/
namespace Drupal\Tests\state_machine\Unit;
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Plugin\Discovery\YamlDiscovery;
use org\bovigo\vfs\vfsStream;
/**
* @coversDefaultClass \Drupal\state_machine\WorkflowGroupManager
* @group Workflow
*/
class WorkflowGroupManagerTest extends WorkflowBaseTestCase {
/**
* The group manager under test.
*
* @var \Drupal\Tests\state_machine\Unit\TestWorkflowGroupManager
*/
protected $groupManager;
/**
* The expected definitions array.
*
* @var array
*/
protected $expectedDefinitions = [
'order' => [
'id' => 'order',
'label' => 'Order',
'entity_type' => 'commerce_order',
'class' => 'Drupal\state_machine\Plugin\WorkflowGroup\WorkflowGroup',
'workflow_class' => '\Drupal\state_machine\Plugin\Workflow\Workflow',
'provider' => 'state_machine_test',
]
];
/**
* Provide a set of invalid config workflow groups to test the process
* definitions.
*/
public function invalidConfigWorkflowGroups() {
return [
[['workflow_group_1' => [
'entity_type' => 'commerce_order',
]]],
[['workflow_group_2' => [
'label' => 'order',
]]],
];
}
/**
* Tests the processDefinition method with missing keys.
*
* @param $group_config array
* Workflow group configuration that will be translated into YAML.
*
* @covers ::processDefinition
* @dataProvider invalidConfigWorkflowGroups
*/
public function testProcessInvalidDefinitions($group_config) {
vfsStream::setup('root');
$file = Yaml::encode($group_config);
vfsStream::create([
'state_machine_test' => [
'state_machine_test.workflow_groups.yml' => $file,
]]
);
$discovery = new YamlDiscovery('workflow_groups', ['state_machine_test' => vfsStream::url('root/state_machine_test')]);
$this->groupManager->setDiscovery($discovery);
$required_properties = ['label', 'entity_type'];
$definition = $discovery->getDefinitions();
$missing_properties = array_diff($required_properties, array_keys($group_config));
$this->setExpectedException('Drupal\Component\Plugin\Exception\PluginException',
sprintf('The workflow_group %s must define the %s property.', key($definition), reset($missing_properties)));
$this->groupManager->processDefinition($definition, key($definition));
}
/**
* @covers: getDefinitionsByEntityType
*/
public function testGetDefinitionsByEntityType() {
vfsStream::setup('root');
$group_config = [
'order' => [
'label' => 'Order',
'entity_type' => 'commerce_order',
]
];
$file = Yaml::encode($group_config);
vfsStream::create([
'state_machine_test' => [
'state_machine_test.workflow_groups.yml' => $file,
]]
);
$discovery = new YamlDiscovery('workflow_groups', ['state_machine_test' => vfsStream::url('root/state_machine_test')]);
$this->groupManager->setDiscovery($discovery);
$this->assertEquals($this->expectedDefinitions, $this->groupManager->getDefinitionsByEntityType('commerce_order'), 'Workflow group definition matches the expectations');
}
}
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