View source
<?php
namespace Drupal\Tests\rules\Unit\Integration;
use Drupal\Core\Config\Entity\ConfigEntityType;
use Drupal\Core\Entity\ContentEntityType;
use Drupal\Core\Entity\EntityAccessControlHandlerInterface;
use Drupal\Core\Field\FieldTypePluginManager;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\TypedData\TypedDataManagerInterface;
use Drupal\rules\Context\ContextDefinition;
use Prophecy\Argument;
use Prophecy\Prophecy\ProphecyInterface;
abstract class RulesEntityIntegrationTestBase extends RulesIntegrationTestBase {
protected $languageManager;
protected $entityAccess;
protected $fieldTypeManager;
protected function setUp() : void {
parent::setUp();
$this->namespaces['Drupal\\Core\\Entity'] = $this->root . '/core/lib/Drupal/Core/Entity';
$language = $this
->prophesize(LanguageInterface::class);
$language
->getId()
->willReturn('en');
$this->languageManager = $this
->prophesize(LanguageManagerInterface::class);
$this->languageManager
->getCurrentLanguage()
->willReturn($language
->reveal());
$this->languageManager
->getLanguages()
->willReturn([
$language
->reveal(),
]);
$type_info = [
'test' => [
'id' => 'test',
'label' => 'Test',
'entity_keys' => [
'bundle' => 'bundle',
],
],
'user' => [
'id' => 'user',
'label' => 'Test User',
'entity_keys' => [
'bundle' => 'user',
],
],
'node' => [
'id' => 'node',
'label' => 'Test Node',
'entity_keys' => [
'bundle' => 'dummy',
],
],
'path_alias' => [
'id' => 'path_alias',
'label' => 'URL alias',
'entity_keys' => [
'bundle' => 'path_alias',
],
],
];
$type_array = [];
foreach ($type_info as $type => $info) {
$entity_type = new ContentEntityType($info);
$type_array[$type] = $entity_type;
$this->entityTypeManager
->getDefinition($type)
->willReturn($entity_type);
}
$role_entity_info = [
'id' => 'user_role',
'label' => 'Test Role',
];
$role_type = new ConfigEntityType($role_entity_info);
$type_array['user_role'] = $role_type;
$this->entityTypeManager
->getDefinitions()
->willReturn($type_array);
$this->entityAccess = $this
->prophesize(EntityAccessControlHandlerInterface::class);
$this->entityTypeManager
->getAccessControlHandler(Argument::any())
->willReturn($this->entityAccess
->reveal());
$this->entityFieldManager
->getBaseFieldDefinitions(Argument::any())
->willReturn([]);
$this->entityTypeBundleInfo
->getBundleInfo(Argument::any())
->willReturn([
'test' => [
'label' => 'Test',
],
]);
$this->moduleHandler
->getImplementations('entity_type_build')
->willReturn([]);
$this->fieldTypeManager = new FieldTypePluginManager($this->namespaces, $this->cacheBackend, $this->moduleHandler
->reveal(), $this->typedDataManager);
$this->container
->set('plugin.manager.field.field_type', $this->fieldTypeManager);
}
protected function getContextDefinitionFor($data_type, ProphecyInterface $data_definition) {
$data_definition
->setLabel(Argument::any())
->willReturn($data_definition
->reveal());
$data_definition
->setDescription(Argument::any())
->willReturn($data_definition
->reveal());
$data_definition
->setRequired(Argument::any())
->willReturn($data_definition
->reveal());
$data_definition
->setLabel(Argument::any())
->willReturn($data_definition
->reveal());
$data_definition
->setConstraints(Argument::any())
->willReturn($data_definition
->reveal());
$data_definition
->getConstraints()
->willReturn([]);
$data_definition
->getDataType()
->willReturn($data_type);
$original_definition = $this->typedDataManager
->getDefinition($data_type);
$data_definition
->getClass()
->willReturn($original_definition['class']);
$context_definition = ContextDefinition::create($data_type);
$typed_data_manager = $this
->prophesize(TypedDataManagerInterface::class);
$typed_data_manager
->createDataDefinition($data_type)
->willReturn($data_definition
->reveal());
$context_definition
->setTypedDataManager($typed_data_manager
->reveal());
return $context_definition;
}
}