/** * @covers ::getFieldMapByFieldType */ public function testGetFieldMapByFieldType() { // Set up a content entity type. $entity_type = $this->prophesize(ContentEntityTypeInterface::class); $entity_class = EntityManagerTestEntity::class; // Set up the module handler to return two bundles for the fieldable entity // type. $this->moduleHandler->getImplementations('entity_base_field_info')->willReturn([]); $this->moduleHandler->invokeAll('entity_bundle_info')->willReturn(['test_entity_type' => ['first_bundle' => [], 'second_bundle' => []]]); $this->moduleHandler->alter('entity_bundle_info', Argument::type('array'))->willReturn(NULL); // Define an ID field definition as a base field. $id_definition = $this->prophesize(FieldDefinitionInterface::class); $id_definition->getType()->willReturn('integer'); $base_field_definitions = array('id' => $id_definition->reveal()); $entity_class::$baseFieldDefinitions = $base_field_definitions; // Set up the stored bundle field map. $key_value_store = $this->prophesize(KeyValueStoreInterface::class); $this->keyValueFactory->get('entity.definitions.bundle_field_map')->willReturn($key_value_store->reveal()); $key_value_store->getAll()->willReturn(['test_entity_type' => ['by_bundle' => ['type' => 'string', 'bundles' => ['second_bundle' => 'second_bundle']]]]); // Mock the base field definition override. $override_entity_type = $this->prophesize(EntityTypeInterface::class); $this->setUpEntityManager(array('test_entity_type' => $entity_type, 'base_field_override' => $override_entity_type)); $entity_type->getClass()->willReturn($entity_class); $entity_type->getKeys()->willReturn(['default_langcode' => 'default_langcode']); $entity_type->id()->willReturn('test_entity_type'); $entity_type->isSubclassOf(FieldableEntityInterface::class)->willReturn(TRUE); $entity_type->getBundleOf()->shouldBeCalled(); $entity_type->isTranslatable()->shouldBeCalled(); $entity_type->getProvider()->shouldBeCalled(); $override_entity_type->getClass()->willReturn($entity_class); $override_entity_type->isSubclassOf(FieldableEntityInterface::class)->willReturn(FALSE); $override_entity_type->getHandlerClass('storage')->willReturn(TestConfigEntityStorage::class); $override_entity_type->getBundleOf()->shouldBeCalled(); $override_entity_type->getLabel()->shouldBeCalled(); $integerFields = $this->entityManager->getFieldMapByFieldType('integer'); $this->assertCount(1, $integerFields['test_entity_type']); $this->assertArrayNotHasKey('non_fieldable', $integerFields); $this->assertArrayHasKey('id', $integerFields['test_entity_type']); $this->assertArrayNotHasKey('by_bundle', $integerFields['test_entity_type']); $stringFields = $this->entityManager->getFieldMapByFieldType('string'); $this->assertCount(1, $stringFields['test_entity_type']); $this->assertArrayNotHasKey('non_fieldable', $stringFields); $this->assertArrayHasKey('by_bundle', $stringFields['test_entity_type']); $this->assertArrayNotHasKey('id', $stringFields['test_entity_type']); }
/** * @covers ::getFieldMapByFieldType */ public function testGetFieldMapByFieldType() { // Set up a content entity type. $entity_type = $this->getMock('Drupal\\Core\\Entity\\ContentEntityTypeInterface'); $entity = $this->getMockBuilder('Drupal\\Tests\\Core\\Entity\\EntityManagerTestEntity')->disableOriginalConstructor()->getMockForAbstractClass(); $entity_class = get_class($entity); $entity_type->expects($this->any())->method('getClass')->will($this->returnValue($entity_class)); $entity_type->expects($this->any())->method('getKeys')->will($this->returnValue(array('default_langcode' => 'default_langcode'))); $entity_type->expects($this->any())->method('id')->will($this->returnValue('test_entity_type')); $entity_type->expects($this->any())->method('isSubclassOf')->with('\\Drupal\\Core\\Entity\\FieldableEntityInterface')->will($this->returnValue(TRUE)); // Set up the module handler to return two bundles for the fieldable entity // type. $this->moduleHandler = $this->getMock('Drupal\\Core\\Extension\\ModuleHandlerInterface'); $this->moduleHandler->expects($this->any())->method('alter'); $this->moduleHandler->expects($this->any())->method('getImplementations')->will($this->returnValue(array())); $module_implements_value_map = array(array('entity_bundle_info', array(), array('test_entity_type' => array('first_bundle' => array(), 'second_bundle' => array())))); $this->moduleHandler->expects($this->any())->method('invokeAll')->will($this->returnValueMap($module_implements_value_map)); // Define an ID field definition as a base field. $id_definition = $this->getMockBuilder('Drupal\\Core\\Field\\BaseFieldDefinition')->disableOriginalConstructor()->getMock(); $id_definition->expects($this->exactly(2))->method('getType')->will($this->returnValue('integer')); $base_field_definitions = array('id' => $id_definition); $entity_class::$baseFieldDefinitions = $base_field_definitions; // Set up a by bundle field definition that only exists on one bundle. $bundle_definition = $this->getMockBuilder('Drupal\\Core\\Field\\BaseFieldDefinition')->disableOriginalConstructor()->getMock(); $bundle_definition->expects($this->once())->method('getType')->will($this->returnValue('string')); $entity_class::$bundleFieldDefinitions = array('test_entity_type' => array('first_bundle' => array(), 'second_bundle' => array('by_bundle' => $bundle_definition))); // Mock the base field definition override. $override_entity_type = $this->getMock('Drupal\\Core\\Entity\\EntityTypeInterface'); $override_entity_type->expects($this->any())->method('getClass')->will($this->returnValue(get_class($entity))); $override_entity_type->expects($this->any())->method('getHandlerClass')->with('storage')->will($this->returnValue('\\Drupal\\Tests\\Core\\Entity\\TestConfigEntityStorage')); $this->setUpEntityManager(array('test_entity_type' => $entity_type, 'base_field_override' => $override_entity_type)); $integerFields = $this->entityManager->getFieldMapByFieldType('integer'); $this->assertCount(1, $integerFields['test_entity_type']); $this->assertArrayNotHasKey('non_fieldable', $integerFields); $this->assertArrayHasKey('id', $integerFields['test_entity_type']); $this->assertArrayNotHasKey('by_bundle', $integerFields['test_entity_type']); $stringFields = $this->entityManager->getFieldMapByFieldType('string'); $this->assertCount(1, $stringFields['test_entity_type']); $this->assertArrayNotHasKey('non_fieldable', $stringFields); $this->assertArrayHasKey('by_bundle', $stringFields['test_entity_type']); $this->assertArrayNotHasKey('id', $stringFields['test_entity_type']); }