View source
<?php
namespace Drupal\Tests\rules\Unit\Integration;
use Drupal\Component\Uuid\Php;
use Drupal\Core\Cache\NullBackend;
use Drupal\Core\DependencyInjection\ClassResolverInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\Discovery\RecursiveExtensionFilterIterator;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\Plugin\Context\LazyContextRepository;
use Drupal\Core\TypedData\TypedDataManager;
use Drupal\rules\Core\ConditionManager;
use Drupal\rules\Context\DataProcessorManager;
use Drupal\rules\Core\RulesActionManager;
use Drupal\rules\Engine\ExpressionManager;
use Drupal\typed_data\DataFetcher;
use Drupal\typed_data\DataFilterManager;
use Drupal\typed_data\PlaceholderResolver;
use Drupal\Tests\UnitTestCase;
use Drupal\Tests\rules\Unit\TestMessenger;
use Prophecy\Argument;
abstract class RulesIntegrationTestBase extends UnitTestCase {
protected $entityTypeManager;
protected $entityFieldManager;
protected $entityTypeBundleInfo;
protected $typedDataManager;
protected $actionManager;
protected $conditionManager;
protected $rulesExpressionManager;
protected $rulesDataProcessorManager;
protected $logger;
protected $namespaces;
protected $cacheBackend;
protected $moduleHandler;
protected $enabledModules;
protected $container;
protected $classResolver;
protected $dataFetcher;
protected $placeholderResolver;
protected $dataFilterManager;
protected $messenger;
protected function setUp() : void {
parent::setUp();
$container = new ContainerBuilder();
$this->moduleHandler = $this
->prophesize(ModuleHandlerInterface::class);
$this->enabledModules = new \ArrayObject();
$this->enabledModules['rules'] = TRUE;
$this->enabledModules['rules_test'] = TRUE;
$enabled_modules = $this->enabledModules;
$this->moduleHandler
->moduleExists(Argument::type('string'))
->will(function ($arguments) use ($enabled_modules) {
if (isset($enabled_modules[$arguments[0]])) {
return [
$arguments[0],
$enabled_modules[$arguments[0]],
];
}
return [
$arguments[0],
FALSE,
];
});
$this->moduleHandler
->alter(Argument::any(), Argument::any(), Argument::any(), Argument::any())
->willReturn(NULL);
$this->cacheBackend = new NullBackend('rules');
$rules_directory = __DIR__ . '/../../../..';
$this->namespaces = new \ArrayObject([
'Drupal\\rules' => $rules_directory . '/src',
'Drupal\\rules_test' => $rules_directory . '/tests/modules/rules_test/src',
'Drupal\\Core\\TypedData' => $this->root . '/core/lib/Drupal/Core/TypedData',
'Drupal\\Core\\Validation' => $this->root . '/core/lib/Drupal/Core/Validation',
]);
$this->actionManager = new RulesActionManager($this->namespaces, $this->cacheBackend, $this->moduleHandler
->reveal());
$this->conditionManager = new ConditionManager($this->namespaces, $this->cacheBackend, $this->moduleHandler
->reveal());
$uuid_service = new Php();
$this->rulesExpressionManager = new ExpressionManager($this->namespaces, $this->moduleHandler
->reveal(), $uuid_service);
$this->classResolver = $this
->prophesize(ClassResolverInterface::class);
$this->typedDataManager = new TypedDataManager($this->namespaces, $this->cacheBackend, $this->moduleHandler
->reveal(), $this->classResolver
->reveal());
$this->rulesDataProcessorManager = new DataProcessorManager($this->namespaces, $this->moduleHandler
->reveal());
$this->entityTypeManager = $this
->prophesize(EntityTypeManagerInterface::class);
$this->entityTypeManager
->getDefinitions()
->willReturn([]);
$storage = $this
->prophesize(ConfigEntityStorageInterface::class);
$storage
->loadMultiple(NULL)
->willReturn([]);
$this->entityTypeManager
->getStorage('rules_component')
->willReturn($storage
->reveal());
$this->entityFieldManager = $this
->prophesize(EntityFieldManagerInterface::class);
$this->entityFieldManager
->getBaseFieldDefinitions()
->willReturn([]);
$this->entityTypeBundleInfo = $this
->prophesize(EntityTypeBundleInfoInterface::class);
$this->entityTypeBundleInfo
->getBundleInfo()
->willReturn([]);
$this->dataFetcher = new DataFetcher();
$this->messenger = new TestMessenger();
$this->dataFilterManager = new DataFilterManager($this->namespaces, $this->cacheBackend, $this->moduleHandler
->reveal());
$this->placeholderResolver = new PlaceholderResolver($this->dataFetcher, $this->dataFilterManager);
$this->logger = $this
->prophesize(LoggerChannelInterface::class);
$container
->set('entity_type.manager', $this->entityTypeManager
->reveal());
$container
->set('entity_field.manager', $this->entityFieldManager
->reveal());
$container
->set('entity_type.bundle.info', $this->entityTypeBundleInfo
->reveal());
$container
->set('context.repository', new LazyContextRepository($container, []));
$container
->set('logger.channel.rules_debug', $this->logger
->reveal());
$container
->set('plugin.manager.rules_action', $this->actionManager);
$container
->set('plugin.manager.condition', $this->conditionManager);
$container
->set('plugin.manager.rules_expression', $this->rulesExpressionManager);
$container
->set('plugin.manager.rules_data_processor', $this->rulesDataProcessorManager);
$container
->set('messenger', $this->messenger);
$container
->set('typed_data_manager', $this->typedDataManager);
$container
->set('string_translation', $this
->getStringTranslationStub());
$container
->set('uuid', $uuid_service);
$container
->set('typed_data.data_fetcher', $this->dataFetcher);
$container
->set('typed_data.placeholder_resolver', $this->placeholderResolver);
\Drupal::setContainer($container);
$this->container = $container;
}
protected function enableModule($name, array $namespaces = []) {
$this->enabledModules[$name] = TRUE;
if (empty($namespaces)) {
$namespaces = [
'Drupal\\' . $name => $this->root . '/' . $this
->constructModulePath($name) . '/src',
];
}
foreach ($namespaces as $namespace => $path) {
$this->namespaces[$namespace] = $path;
}
}
protected function constructModulePath($module) {
$flags = \FilesystemIterator::UNIX_PATHS;
$flags |= \FilesystemIterator::SKIP_DOTS;
$flags |= \FilesystemIterator::FOLLOW_SYMLINKS;
$flags |= \FilesystemIterator::CURRENT_AS_SELF;
$directory_iterator = new \RecursiveDirectoryIterator($this->root, $flags);
$filter = new RecursiveExtensionFilterIterator($directory_iterator);
$filter
->acceptTests(TRUE);
$iterator = new \RecursiveIteratorIterator($filter, \RecursiveIteratorIterator::LEAVES_ONLY, \RecursiveIteratorIterator::CATCH_GET_CHILD);
$info_files = new \RegexIterator($iterator, "/^{$module}.info.yml\$/");
foreach ($info_files as $file) {
return $file
->getSubPath();
}
}
protected function getTypedData($data_type, $value) {
$definition = $this->typedDataManager
->createDataDefinition($data_type);
$data = $this->typedDataManager
->create($definition);
$data
->setValue($value);
return $data;
}
protected function prophesizeEntity($interface) {
$entity = $this
->prophesize($interface);
$entity
->getCacheContexts()
->willReturn([]);
$entity
->getCacheTags()
->willReturn([]);
$entity
->getCacheMaxAge()
->willReturn(0);
return $entity;
}
}