You are here

class ConfigEntityCalculateDependencyTest in Business Rules 8

Same name and namespace in other branches
  1. 2.x tests/src/Unit/ConfigEntityCalculateDependencyTest.php \Drupal\Tests\business_rules\Unit\ConfigEntityCalculateDependencyTest

Test dependencies calculation.

@group business_rules

Hierarchy

Expanded class hierarchy of ConfigEntityCalculateDependencyTest

File

tests/src/Unit/ConfigEntityCalculateDependencyTest.php, line 34

Namespace

Drupal\Tests\business_rules\Unit
View source
class ConfigEntityCalculateDependencyTest extends UnitTestCase {

  /**
   * The Business rules Action plugin manager.
   *
   * @var \Drupal\business_rules\Plugin\BusinessRulesActionManager|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $businessRulesActionManager;

  /**
   * The Business rules Condition plugin manager.
   *
   * @var \Drupal\business_rules\Plugin\BusinessRulesConditionManager|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $businessRulesConditionManager;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $entityStorage = $this
      ->createMock(EntityStorageInterface::class);
    $entityTypeManager = $this
      ->createMock(EntityTypeManagerInterface::class);
    $this->businessRulesActionManager = $this
      ->createMock(BusinessRulesActionManager::class);
    $this->businessRulesConditionManager = $this
      ->createMock(BusinessRulesConditionManager::class);
    $container = new ContainerBuilder();
    $container
      ->set('uuid', $this
      ->createMock(UuidInterface::class));
    $container
      ->set('event_dispatcher', $this
      ->createMock(EventDispatcherInterface::class));
    $container
      ->set('config.factory', $this
      ->createMock(ConfigFactoryInterface::class));
    $container
      ->set('entity_type.manager', $entityTypeManager);
    $container
      ->set('entity_type.repository', $this
      ->createMock(EntityTypeRepositoryInterface::class));
    $container
      ->set('business_rules.processor', $this
      ->createMock(BusinessRulesProcessor::class));
    $container
      ->set('business_rules.util', $this
      ->createMock(BusinessRulesUtil::class));
    $container
      ->set('plugin.manager.business_rules.action', $this->businessRulesActionManager);
    $container
      ->set('plugin.manager.business_rules.condition', $this->businessRulesConditionManager);
    $container
      ->set('plugin.manager.business_rules.reacts_on', $this
      ->createMock(BusinessRulesReactsOnManager::class));
    $container
      ->set('plugin.manager.business_rules.variable', $this
      ->createMock(BusinessRulesVariableManager::class));
    $businessRulesUtil = $this
      ->createMock(BusinessRulesUtil::class);
    $businessRulesUtil->container = $container;
    $container
      ->set('business_rules.util', $businessRulesUtil);
    \Drupal::setContainer($container);
    $actionAConfigEntity = $this
      ->createMock(Action::class);
    $actionAConfigEntity
      ->expects($this
      ->any())
      ->method('getConfigDependencyName')
      ->willReturn('business_rules.action.test_action_a');
    $actionBConfigEntity = $this
      ->createMock(Action::class);
    $actionBConfigEntity
      ->expects($this
      ->any())
      ->method('getConfigDependencyName')
      ->willReturn('business_rules.action.test_action_b');
    $conditionAConfigEntity = $this
      ->createMock(Condition::class);
    $conditionAConfigEntity
      ->expects($this
      ->any())
      ->method('getConfigDependencyName')
      ->willReturn('business_rules.condition.test_condition_a');
    $conditionBConfigEntity = $this
      ->createMock(Condition::class);
    $conditionBConfigEntity
      ->expects($this
      ->any())
      ->method('getConfigDependencyName')
      ->willReturn('business_rules.condition.test_condition_b');
    $variableConfigEntity = $this
      ->createMock(Variable::class);
    $variableConfigEntity
      ->expects($this
      ->any())
      ->method('getConfigDependencyName')
      ->willReturn('business_rules.variable.test_variable');
    $entityTypeManager
      ->expects($this
      ->any())
      ->method('getStorage')
      ->willReturn($entityStorage);
    $entityStorage
      ->expects($this
      ->any())
      ->method('load')
      ->willReturnMap([
      [
        'test_action_a',
        $actionAConfigEntity,
      ],
      [
        'test_action_b',
        $actionBConfigEntity,
      ],
      [
        'test_condition_a',
        $conditionAConfigEntity,
      ],
      [
        'test_condition_b',
        $conditionBConfigEntity,
      ],
      [
        'test_variable',
        $variableConfigEntity,
      ],
    ]);
  }

  /**
   * @covers \Drupal\business_rules\Entity\BusinessRule::calculateDependencies
   */
  public function testBusinessRuleCalculateDependencies() {
    $business_rule = new BusinessRule([]);
    $dependencies = $business_rule
      ->calculateDependencies()
      ->getDependencies();
    $this
      ->assertArrayNotHasKey('config', $dependencies);
    $dependencyA = new BusinessRulesItemObject('test_action_a', 'action', 0);
    $business_rule
      ->addItem($dependencyA);
    $dependencies = $business_rule
      ->calculateDependencies()
      ->getDependencies();
    $this
      ->assertEquals([
      'business_rules.action.test_action_a',
    ], $dependencies['config']);
    $dependencyB = new BusinessRulesItemObject('test_condition_a', 'condition', 0);
    $business_rule
      ->addItem($dependencyB);
    $dependencies = $business_rule
      ->calculateDependencies()
      ->getDependencies();
    $this
      ->assertEquals([
      'business_rules.action.test_action_a',
      'business_rules.condition.test_condition_a',
    ], $dependencies['config']);
  }

  /**
   * @covers \Drupal\business_rules\Entity\Condition::calculateDependencies
   */
  public function testActionCalculateDependencies() {
    $this->businessRulesActionManager
      ->expects($this
      ->any())
      ->method('getDefinition')
      ->willReturnMap([
      [
        'action_set',
        TRUE,
        [
          'class' => ActionSet::class,
          'id' => 'action_set',
        ],
      ],
    ]);
    $action = new Action([
      'type' => 'action_set',
    ]);
    $dependencies = $action
      ->calculateDependencies()
      ->getDependencies();
    $this
      ->assertArrayNotHasKey('config', $dependencies);
    $action
      ->setSetting('items', [
      'test_action_a' => [
        'weight' => 0,
        'type' => 'action',
        'id' => 'test_action_a',
      ],
    ]);
    $dependencies = $action
      ->calculateDependencies()
      ->getDependencies();
    $this
      ->assertEquals([
      'business_rules.action.test_action_a',
    ], $dependencies['config']);
    $action
      ->setSetting('items', [
      'test_action_a' => [
        'weight' => 0,
        'type' => 'action',
        'id' => 'test_action_a',
      ],
      'test_action_b' => [
        'weight' => 0,
        'type' => 'action',
        'id' => 'test_action_b',
      ],
    ]);
    $dependencies = $action
      ->calculateDependencies()
      ->getDependencies();
    $this
      ->assertEquals([
      'business_rules.action.test_action_a',
      'business_rules.action.test_action_b',
    ], $dependencies['config']);
  }

  /**
   * @covers \Drupal\business_rules\Entity\Condition::calculateDependencies
   */
  public function testConditionCalculateDependencies() {
    $this->businessRulesConditionManager
      ->expects($this
      ->any())
      ->method('getDefinition')
      ->willReturnMap([
      [
        'user_has_role',
        TRUE,
        [
          'class' => UserHasRole::class,
          'id' => 'user_has_role',
        ],
      ],
      [
        'logical_and',
        TRUE,
        [
          'class' => LogicalAnd::class,
          'id' => 'logical_and',
        ],
      ],
    ]);
    $condition = new Condition([
      'type' => 'user_has_role',
    ]);
    $dependencies = $condition
      ->calculateDependencies()
      ->getDependencies();
    $this
      ->assertArrayNotHasKey('config', $dependencies);
    $dependencyA = new BusinessRulesItemObject('test_action_a', 'action', 0);
    $condition
      ->addSuccessItem($dependencyA);
    $dependencies = $condition
      ->calculateDependencies()
      ->getDependencies();
    $this
      ->assertEquals([
      'business_rules.action.test_action_a',
    ], $dependencies['config']);
    $dependencyB = new BusinessRulesItemObject('test_condition_a', 'condition', 0);
    $condition
      ->addFailItem($dependencyB);
    $dependencies = $condition
      ->calculateDependencies()
      ->getDependencies();
    $this
      ->assertEquals([
      'business_rules.action.test_action_a',
      'business_rules.condition.test_condition_a',
    ], $dependencies['config']);
    $condition = new Condition([
      'type' => 'logical_and',
    ]);
    $dependencies = $condition
      ->calculateDependencies()
      ->getDependencies();
    $this
      ->assertArrayNotHasKey('config', $dependencies);
    $condition
      ->setSetting('items', [
      'test_condition_a' => [
        'weight' => 0,
        'type' => 'condition',
        'id' => 'test_condition_a',
      ],
    ]);
    $dependencies = $condition
      ->calculateDependencies()
      ->getDependencies();
    $this
      ->assertEquals([
      'business_rules.condition.test_condition_a',
    ], $dependencies['config']);
    $condition
      ->setSetting('items', [
      'test_condition_a' => [
        'weight' => 0,
        'type' => 'condition',
        'id' => 'test_condition_a',
      ],
      'test_condition_b' => [
        'weight' => 0,
        'type' => 'condition',
        'id' => 'test_condition_b',
      ],
    ]);
    $dependencies = $condition
      ->calculateDependencies()
      ->getDependencies();
    $this
      ->assertEquals([
      'business_rules.condition.test_condition_a',
      'business_rules.condition.test_condition_b',
    ], $dependencies['config']);
  }

  /**
   * @covers \Drupal\business_rules\Entity\BusinessRulesItemBase::calculateDependencies
   */
  public function testBusinessRulesItemObjectCalculateDependencies() {
    $this->businessRulesConditionManager
      ->expects($this
      ->any())
      ->method('getDefinition')
      ->willReturnMap([
      [
        'user_variable_has_role',
        TRUE,
        [
          'class' => UserVariableHasRole::class,
          'id' => 'user_variable_has_role',
        ],
      ],
    ]);
    $condition = new Condition([
      'type' => 'user_variable_has_role',
      'settings' => [
        'user_variable' => 'test_variable',
      ],
    ]);
    $dependencies = $condition
      ->calculateDependencies()
      ->getDependencies();
    $this
      ->assertEquals([
      'business_rules.variable.test_variable',
    ], $dependencies['config']);
    $dependencyA = new BusinessRulesItemObject('test_action_a', 'action', 0);
    $condition
      ->addSuccessItem($dependencyA);
    $dependencies = $condition
      ->calculateDependencies()
      ->getDependencies();
    $this
      ->assertEquals([
      'business_rules.action.test_action_a',
      'business_rules.variable.test_variable',
    ], $dependencies['config']);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigEntityCalculateDependencyTest::$businessRulesActionManager protected property The Business rules Action plugin manager.
ConfigEntityCalculateDependencyTest::$businessRulesConditionManager protected property The Business rules Condition plugin manager.
ConfigEntityCalculateDependencyTest::setUp protected function Overrides UnitTestCase::setUp
ConfigEntityCalculateDependencyTest::testActionCalculateDependencies public function @covers \Drupal\business_rules\Entity\Condition::calculateDependencies
ConfigEntityCalculateDependencyTest::testBusinessRuleCalculateDependencies public function @covers \Drupal\business_rules\Entity\BusinessRule::calculateDependencies
ConfigEntityCalculateDependencyTest::testBusinessRulesItemObjectCalculateDependencies public function @covers \Drupal\business_rules\Entity\BusinessRulesItemBase::calculateDependencies
ConfigEntityCalculateDependencyTest::testConditionCalculateDependencies public function @covers \Drupal\business_rules\Entity\Condition::calculateDependencies
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.
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.