You are here

abstract class RulesEntityIntegrationTestBase in Rules 8.3

Base class for Rules integration tests with entities.

This base class makes sure the base-entity system is available and its data types are registered. It enables entity_test module, such that some test entity types are available.

Hierarchy

Expanded class hierarchy of RulesEntityIntegrationTestBase

36 files declare their use of RulesEntityIntegrationTestBase
AutoSaveTest.php in tests/src/Unit/Integration/Engine/AutoSaveTest.php
EntityCreateTest.php in tests/src/Unit/Integration/RulesAction/EntityCreateTest.php
EntityDeleteTest.php in tests/src/Unit/Integration/RulesAction/EntityDeleteTest.php
EntityFetchByFieldTest.php in tests/src/Unit/Integration/RulesAction/EntityFetchByFieldTest.php
EntityFetchByIdTest.php in tests/src/Unit/Integration/RulesAction/EntityFetchByIdTest.php

... See full list

File

tests/src/Unit/Integration/RulesEntityIntegrationTestBase.php, line 23

Namespace

Drupal\Tests\rules\Unit\Integration
View source
abstract class RulesEntityIntegrationTestBase extends RulesIntegrationTestBase {

  /**
   * The language manager mock.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $languageManager;

  /**
   * The mocked entity access handler.
   *
   * @var \Drupal\Core\Entity\EntityAccessControlHandlerInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $entityAccess;

  /**
   * The field type manager.
   *
   * @var \Drupal\Core\Field\FieldTypePluginManager
   */
  protected $fieldTypeManager;

  /**
   * {@inheritdoc}
   */
  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(),
    ]);

    // We need to support multiple entity types, including a test type:
    $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);
    }

    // We need a user_role mock as well.
    $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());

    // The base field definitions for our test entity aren't used, and would
    // require additional mocking. It doesn't appear that any of our tests rely
    // on this for any other entity type that we are mocking.
    $this->entityFieldManager
      ->getBaseFieldDefinitions(Argument::any())
      ->willReturn([]);

    // Return some dummy bundle information for now, so that the entity manager
    // does not call out to the config entity system to get bundle information.
    $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);
  }

  /**
   * Helper to mock a context definition with a mocked data definition.
   *
   * @param string $data_type
   *   The data type, example "entity:node".
   * @param \Prophecy\Prophecy\ProphecyInterface $data_definition
   *   A prophecy that represents a data definition object.
   *
   * @return \Drupal\rules\Context\ContextDefinition
   *   The context definition with the data definition prophecy in it.
   */
  protected function getContextDefinitionFor($data_type, ProphecyInterface $data_definition) {

    // Mock all the setter calls on the data definition that can be ignored.
    $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);

    // Inject a fake typed data manager that will return our data definition
    // prophecy if asked for it in the ContextDefinition class.
    $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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
RulesEntityIntegrationTestBase::$entityAccess protected property The mocked entity access handler.
RulesEntityIntegrationTestBase::$fieldTypeManager protected property The field type manager.
RulesEntityIntegrationTestBase::$languageManager protected property The language manager mock.
RulesEntityIntegrationTestBase::getContextDefinitionFor protected function Helper to mock a context definition with a mocked data definition.
RulesEntityIntegrationTestBase::setUp protected function Overrides RulesIntegrationTestBase::setUp 33
RulesIntegrationTestBase::$actionManager protected property
RulesIntegrationTestBase::$cacheBackend protected property
RulesIntegrationTestBase::$classResolver protected property The class resolver mock for the typed data manager.
RulesIntegrationTestBase::$conditionManager protected property
RulesIntegrationTestBase::$container protected property The Drupal service container.
RulesIntegrationTestBase::$dataFetcher protected property The data fetcher service.
RulesIntegrationTestBase::$dataFilterManager protected property The data filter manager.
RulesIntegrationTestBase::$enabledModules protected property Array object keyed with module names and TRUE as value.
RulesIntegrationTestBase::$entityFieldManager protected property
RulesIntegrationTestBase::$entityTypeBundleInfo protected property
RulesIntegrationTestBase::$entityTypeManager protected property
RulesIntegrationTestBase::$logger protected property A mocked Rules logger.channel.rules_debug service. 6
RulesIntegrationTestBase::$messenger protected property The messenger service.
RulesIntegrationTestBase::$moduleHandler protected property
RulesIntegrationTestBase::$namespaces protected property All setup'ed namespaces.
RulesIntegrationTestBase::$placeholderResolver protected property The placeholder resolver service.
RulesIntegrationTestBase::$rulesDataProcessorManager protected property
RulesIntegrationTestBase::$rulesExpressionManager protected property
RulesIntegrationTestBase::$typedDataManager protected property
RulesIntegrationTestBase::constructModulePath protected function Determines the path to a module's class files.
RulesIntegrationTestBase::enableModule protected function Fakes the enabling of a module and adds its namespace for plugin loading.
RulesIntegrationTestBase::getTypedData protected function Returns a typed data object.
RulesIntegrationTestBase::prophesizeEntity protected function Helper method to mock irrelevant cache methods on entities.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.