/** * Prepares an entity that defines a field definition. * * @param bool $custom_invoke_all * (optional) Whether the test will set up its own * ModuleHandlerInterface::invokeAll() implementation. Defaults to FALSE. * @param string $field_definition_id * (optional) The ID to use for the field definition. Defaults to 'id'. * @param array $entity_keys * (optional) An array of entity keys for the mocked entity type. Defaults * to an empty array. * * @return \Drupal\Core\Field\BaseFieldDefinition|\Prophecy\Prophecy\ProphecyInterface * A field definition object. */ protected function setUpEntityWithFieldDefinition($custom_invoke_all = FALSE, $field_definition_id = 'id', $entity_keys = array()) { $field_type_manager = $this->prophesize(FieldTypePluginManagerInterface::class); $field_type_manager->getDefaultStorageSettings('boolean')->willReturn([]); $field_type_manager->getDefaultFieldSettings('boolean')->willReturn([]); $this->container->get('plugin.manager.field.field_type')->willReturn($field_type_manager->reveal()); $string_translation = $this->prophesize(TranslationInterface::class); $this->container->get('string_translation')->willReturn($string_translation->reveal()); $entity_class = EntityManagerTestEntity::class; $field_definition = $this->prophesize()->willImplement(FieldDefinitionInterface::class)->willImplement(FieldStorageDefinitionInterface::class); $entity_class::$baseFieldDefinitions = array($field_definition_id => $field_definition->reveal()); $entity_class::$bundleFieldDefinitions = array(); if (!$custom_invoke_all) { $this->moduleHandler->getImplementations(Argument::cetera())->willReturn([]); } // Mock the base field definition override. $override_entity_type = $this->prophesize(EntityTypeInterface::class); $this->entityType = $this->prophesize(EntityTypeInterface::class); $this->setUpEntityManager(array('test_entity_type' => $this->entityType, 'base_field_override' => $override_entity_type)); $override_entity_type->getClass()->willReturn($entity_class); $override_entity_type->getHandlerClass('storage')->willReturn(TestConfigEntityStorage::class); $this->entityType->getClass()->willReturn($entity_class); $this->entityType->getKeys()->willReturn($entity_keys + ['default_langcode' => 'default_langcode']); $this->entityType->isSubclassOf(FieldableEntityInterface::class)->willReturn(TRUE); $this->entityType->isTranslatable()->willReturn(FALSE); $this->entityType->getProvider()->willReturn('the_provider'); $this->entityType->id()->willReturn('the_entity_id'); return $field_definition->reveal(); }
/** * 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 manger 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; }