You are here

protected function EntityCreateTest::setUp in Rules 8.3

Overrides RulesEntityIntegrationTestBase::setUp

File

tests/src/Unit/Integration/RulesAction/EntityCreateTest.php, line 34

Class

EntityCreateTest
@coversDefaultClass \Drupal\rules\Plugin\RulesAction\EntityCreate @group RulesAction

Namespace

Drupal\Tests\rules\Unit\Integration\RulesAction

Code

protected function setUp() : void {
  parent::setUp();

  // Prepare some mocked bundle field definitions. This is needed because
  // EntityCreateDeriver adds required contexts for required fields, and
  // assumes that the bundle field is required.
  $bundle_field_definition = $this
    ->prophesize(BaseFieldDefinition::class);
  $bundle_field_definition_optional = $this
    ->prophesize(BaseFieldDefinition::class);
  $bundle_field_definition_required = $this
    ->prophesize(BaseFieldDefinition::class);
  $property_definition = $this
    ->prophesize(DataDefinitionInterface::class);
  $property_definition
    ->getDataType()
    ->willReturn('string');
  $item_definition = $this
    ->prophesize(FieldItemDataDefinition::class);
  $item_definition
    ->getPropertyDefinition(Argument::any())
    ->willReturn($property_definition
    ->reveal());
  $item_definition
    ->getMainPropertyName()
    ->willReturn('value');

  // The next methods are mocked because EntityCreateDeriver executes them,
  // and the mocked field definition is instantiated without the necessary
  // information.
  $bundle_field_definition
    ->getItemDefinition()
    ->willReturn($item_definition
    ->reveal());
  $bundle_field_definition
    ->getCardinality()
    ->willReturn(1)
    ->shouldBeCalledTimes(1);
  $bundle_field_definition
    ->getType()
    ->willReturn('string');
  $bundle_field_definition
    ->getLabel()
    ->willReturn('Bundle')
    ->shouldBeCalledTimes(1);
  $bundle_field_definition
    ->getDescription()
    ->willReturn('Bundle mock description')
    ->shouldBeCalledTimes(1);
  $bundle_field_definition_required
    ->getItemDefinition()
    ->willReturn($item_definition
    ->reveal());
  $bundle_field_definition_required
    ->getCardinality()
    ->willReturn(1)
    ->shouldBeCalledTimes(1);
  $bundle_field_definition_required
    ->getType()
    ->willReturn('string');
  $bundle_field_definition_required
    ->getLabel()
    ->willReturn('Required field')
    ->shouldBeCalledTimes(1);
  $bundle_field_definition_required
    ->getDescription()
    ->willReturn('Required field mock description')
    ->shouldBeCalledTimes(1);
  $bundle_field_definition_required
    ->isRequired()
    ->willReturn(TRUE)
    ->shouldBeCalledTimes(1);
  $bundle_field_definition_optional
    ->isRequired()
    ->willReturn(FALSE)
    ->shouldBeCalledTimes(1);

  // Prepare mocked entity storage.
  $entity_type_storage = $this
    ->prophesize(EntityStorageBase::class);
  $entity_type_storage
    ->create([
    'bundle' => 'test',
    'field_required' => NULL,
  ])
    ->willReturn(self::ENTITY_REPLACEMENT);

  // Return the mocked storage controller.
  $this->entityTypeManager
    ->getStorage('test')
    ->willReturn($entity_type_storage
    ->reveal());

  // Return a mocked list of base fields definitions.
  $this->entityFieldManager
    ->getBaseFieldDefinitions('test')
    ->willReturn([
    'bundle' => $bundle_field_definition
      ->reveal(),
    'field_required' => $bundle_field_definition_required
      ->reveal(),
    'field_optional' => $bundle_field_definition_optional
      ->reveal(),
  ]);

  // Instantiate the action we are testing.
  $this->action = $this->actionManager
    ->createInstance('rules_entity_create:test');
}