diff --git a/core/tests/Drupal/Tests/Core/Entity/ContentEntityDatabaseStorageTest.php b/core/tests/Drupal/Tests/Core/Entity/ContentEntityDatabaseStorageTest.php index 41358a6..ff20c08 100644 --- a/core/tests/Drupal/Tests/Core/Entity/ContentEntityDatabaseStorageTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/ContentEntityDatabaseStorageTest.php @@ -154,10 +154,7 @@ public function testCreate() { $connection = $this->getMockBuilder('Drupal\Core\Database\Connection') ->disableOriginalConstructor() ->getMock(); - $field_info = $this->getMockBuilder('\Drupal\field\FieldInfo') - ->disableOriginalConstructor() - ->getMock(); - $entity_storage = new ContentEntityDatabaseStorage($entity_type, $connection, $field_info); + $entity_storage = new ContentEntityDatabaseStorage($entity_type, $connection, $entity_manager); $entity = $entity_storage->create(); $entity->expects($this->atLeastOnce()) diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php index 1c260a3..57181c6 100644 --- a/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php @@ -456,6 +456,7 @@ public function testGetAdminRouteInfo() { * Tests the getBaseFieldDefinitions() method. * * @covers ::getBaseFieldDefinitions() + * @covers ::buildBaseFieldDefinitions() */ public function testGetBaseFieldDefinitions() { $field_definition = $this->setUpEntityWithFieldDefinition(); @@ -468,6 +469,7 @@ public function testGetBaseFieldDefinitions() { * Tests the getFieldDefinitions() method. * * @covers ::getFieldDefinitions() + * @covers ::buildBundleFieldDefinitions() */ public function testGetFieldDefinitions() { $field_definition = $this->setUpEntityWithFieldDefinition(); @@ -480,6 +482,7 @@ public function testGetFieldDefinitions() { * Tests the getFieldStorageDefinitions() method. * * @covers ::getFieldStorageDefinitions() + * @covers ::buildFieldStorageDefinitions() */ public function testGetFieldStorageDefinitions() { $field_definition = $this->setUpEntityWithFieldDefinition(TRUE); @@ -625,6 +628,7 @@ public function testGetFieldStorageDefinitionsWithCaching() { * Tests the getBaseFieldDefinitions() method with an invalid definition. * * @covers ::getBaseFieldDefinitions() + * @covers ::buildBaseFieldDefinitions() * * @expectedException \LogicException */ @@ -641,6 +645,7 @@ public function testGetBaseFieldDefinitionsInvalidDefinition() { * Tests that getFieldDefinitions() method sets the 'provider' definition key. * * @covers ::getFieldDefinitions() + * @covers ::buildBundleFieldDefinitions() */ public function testGetFieldDefinitionsProvider() { $this->setUpEntityWithFieldDefinition(TRUE); @@ -954,6 +959,141 @@ function testgetExtraFields() { } /** + * @covers ::getFieldMap + */ + public function testGetFieldMap() { + // Set up a fieldable entity type. + $entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface'); + $entity = $this->getMock('Drupal\Tests\Core\Entity\TestContentEntityInterface'); + $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())); + $entity_type->expects($this->once()) + ->method('isFieldable') + ->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\FieldDefinition') + ->disableOriginalConstructor() + ->getMock(); + $id_definition->expects($this->exactly(2)) + ->method('getType') + ->will($this->returnValue('integer')); + $base_field_definitions = array( + 'id' => $id_definition, + ); + $entity_class::staticExpects($this->once()) + ->method('baseFieldDefinitions') + ->will($this->returnValue($base_field_definitions)); + + // Set up a by bundle field definition that only exists on one bundle. + $bundle_definition = $this->getMockBuilder('Drupal\Core\Field\FieldDefinition') + ->disableOriginalConstructor() + ->getMock(); + $bundle_definition->expects($this->once()) + ->method('getType') + ->will($this->returnValue('string')); + $entity_class::staticExpects($this->any()) + ->method('bundleFieldDefinitions') + ->will($this->returnValueMap(array( + array( + $entity_type, + 'first_bundle', + $base_field_definitions, + array(), + ), + array( + $entity_type, + 'second_bundle', + $base_field_definitions, + array( + 'by_bundle' => $bundle_definition, + ), + ), + ))); + + // Set up a non-fieldable entity type. + $non_fieldable_entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface'); + $non_fieldable_entity_type->expects($this->once()) + ->method('isFieldable') + ->will($this->returnValue(FALSE)); + + $this->setUpEntityManager(array( + 'test_entity_type' => $entity_type, + 'non_fieldable' => $non_fieldable_entity_type, + )); + + $expected = array( + 'test_entity_type' => array( + 'id' => array( + 'type' => 'integer', + 'bundles' => array('first_bundle', 'second_bundle'), + ), + 'by_bundle' => array( + 'type' => 'string', + 'bundles' => array('second_bundle'), + ), + ) + ); + $this->assertEquals($expected, $this->entityManager->getFieldMap()); + } + + /** + * @covers ::getFieldMap + */ + public function testGetFieldMapFromCache() { + $expected = array( + 'test_entity_type' => array( + 'id' => array( + 'type' => 'integer', + 'bundles' => array('first_bundle', 'second_bundle'), + ), + 'by_bundle' => array( + 'type' => 'string', + 'bundles' => array('second_bundle'), + ), + ) + ); + $this->setUpEntityManager(); + $this->cache->expects($this->once()) + ->method('get') + ->with('entity_field_map') + ->will($this->returnValue((object) array('data' => $expected))); + + // Call the field map twice to make sure the static cache works. + $this->assertEquals($expected, $this->entityManager->getFieldMap()); + $this->assertEquals($expected, $this->entityManager->getFieldMap()); + } + + /** * Gets a mock controller class name. * * @return string