View source
<?php
namespace Drupal\Tests\business_rules\Kernel;
use Drupal\business_rules\Events\BusinessRulesEvent;
use Drupal\business_rules\Plugin\BusinessRulesActionManager;
use Drupal\business_rules\Plugin\BusinessRulesConditionManager;
use Drupal\business_rules\Plugin\BusinessRulesReactsOnManager;
use Drupal\business_rules\Plugin\BusinessRulesVariableManager;
use Drupal\business_rules\Util\BusinessRulesProcessor;
use Drupal\business_rules\Util\BusinessRulesUtil;
use Drupal\Component\Uuid\Php as Uuid;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\KernelTests\KernelTestBase;
use Symfony\Component\EventDispatcher\EventDispatcher;
class BusinessRulesProcessorTest extends KernelTestBase {
protected $sutContainer;
protected function setUp() {
parent::setUp();
$this->sutContainer = new ContainerBuilder();
$this->sutContainer
->set('business_rules.util', $this
->createMock(BusinessRulesUtil::class));
$this->sutContainer
->set('plugin.manager.business_rules.action', $this
->createMock(BusinessRulesActionManager::class));
$this->sutContainer
->set('plugin.manager.business_rules.condition', $this
->createMock(BusinessRulesConditionManager::class));
$this->sutContainer
->set('plugin.manager.business_rules.variable', $this
->createMock(BusinessRulesVariableManager::class));
$this->sutContainer
->set('entity_type.manager', $this
->createMock(EntityTypeManagerInterface::class));
$this->sutContainer
->set('messenger', $this
->createMock(MessengerInterface::class));
$this->sutContainer
->set('plugin.manager.business_rules.reacts_on', $this
->createMock(BusinessRulesReactsOnManager::class));
}
public function testProcessesMultipleEvents() {
$brEventOne = $this
->createMock(BusinessRulesEvent::class);
$brEventOne
->expects($this
->exactly(3))
->method('hasArgument')
->willReturnMap([
[
'loop_control',
TRUE,
],
[
'variables',
TRUE,
],
]);
$brEventOne
->expects($this
->exactly(7))
->method('getArgument')
->willReturnMap([
[
'loop_control',
'node1',
],
[
'reacts_on',
[
'id' => 'page_load',
],
],
[
'entity_type_id',
NULL,
],
[
'bundle',
NULL,
],
]);
$brEventTwo = $this
->createMock(BusinessRulesEvent::class);
$brEventTwo
->expects($this
->exactly(1))
->method('hasArgument')
->willReturnMap([
[
'loop_control',
TRUE,
],
[
'variables',
TRUE,
],
]);
$brEventTwo
->expects($this
->exactly(2))
->method('getArgument')
->willReturnMap([
[
'loop_control',
'node1',
],
[
'reacts_on',
[
'id' => 'page_load',
],
],
[
'entity_type_id',
NULL,
],
[
'bundle',
NULL,
],
]);
$uuidService = $this
->createMock(Uuid::class);
$uuidService
->expects($this
->exactly(1))
->method('generate')
->willReturn('b1e38d76-94a2-408b-977f-00a0f39ea73d');
$this->sutContainer
->set('uuid', $uuidService);
$configObject = $this
->createMock(ImmutableConfig::class);
$configObject
->expects($this
->exactly(8))
->method('get')
->willReturnMap([
[
'enable_safemode',
FALSE,
],
[
'clear_render_cache',
FALSE,
],
]);
$configFactory = $this
->createMock(ConfigFactoryInterface::class);
$configFactory
->expects($this
->exactly(1))
->method('get')
->with('business_rules.settings')
->willReturn($configObject);
$this->sutContainer
->set('config.factory', $configFactory);
$configStorage = $this
->createMock(StorageInterface::class);
$testRuleName = $this
->randomMachineName();
$testFqRuleName = sprintf('business_rules.business_rule.%s', $testRuleName);
$configStorage
->expects($this
->exactly(1))
->method('listAll')
->with('business_rules.business_rule')
->willReturn([
$testFqRuleName,
]);
$configStorage
->expects($this
->exactly(1))
->method('readMultiple')
->with([
$testFqRuleName,
])
->willReturn([
$testFqRuleName => [
'uuid' => '8a5154ff-e1c2-4526-83bb-4ee415cf1778',
'langcode' => 'en',
'status' => TRUE,
'dependencies' => [],
'description' => $this->randomGenerator
->string(),
'id' => $testRuleName,
'label' => $this->randomGenerator
->string(),
'enabled' => TRUE,
'reacts_on' => 'page_load',
'items' => [],
'tags' => [],
'target_bundle' => NULL,
'target_entity_type' => NULL,
],
]);
$this->sutContainer
->set('config.storage', $configStorage);
$eventDispatcher = $this
->createMock(EventDispatcher::class);
$eventDispatcher
->expects($this
->exactly(6))
->method('dispatch')
->willReturnMap([
[
[
'business_rules.before_process_event',
$brEventOne,
],
$brEventOne,
],
[
[
'business_rules.before_check_the_triggered_rules',
$brEventOne,
],
$brEventOne,
],
[
[
'business_rules.after_check_the_triggered_rules',
$brEventOne,
],
$brEventOne,
],
[
[
'business_rules.after_process_event',
$brEventOne,
],
$brEventOne,
],
[
[
'business_rules.before_process_event',
$brEventTwo,
],
$brEventTwo,
],
[
[
'business_rules.before_check_the_triggered_rule',
$brEventTwo,
],
$brEventTwo,
],
[
[
'business_rules.after_check_the_triggered_rules',
$brEventTwo,
],
$brEventTwo,
],
[
[
'business_rules.after_process_event',
$brEventTwo,
],
$brEventTwo,
],
]);
$this->sutContainer
->set('event_dispatcher', $eventDispatcher);
\Drupal::setContainer($this->sutContainer);
$businessRulesProcessor = new BusinessRulesProcessor($this->sutContainer);
$businessRulesProcessor
->process($brEventOne);
$businessRulesProcessor
->process($brEventTwo);
$businessRulesProcessor
->process($brEventOne);
unset($businessRulesProcessor);
}
}