Skip to content
EntityManagerTest.php 51.9 KiB
Newer Older
<?php

/**
 * @file
 * Contains \Drupal\Tests\Core\Entity\EntityManagerTest.
 */

namespace Drupal\Tests\Core\Entity {

use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Config\Entity\ConfigEntityStorage;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityHandlerBase;
use Drupal\Core\Entity\EntityHandlerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * @coversDefaultClass \Drupal\Core\Entity\EntityManager
 * @group Entity
 */
class EntityManagerTest extends UnitTestCase {

  /**
   * The entity manager.
   *
   * @var \Drupal\Tests\Core\Entity\TestEntityManager
   */
  protected $entityManager;

  /**
   * The plugin discovery.
   *
   * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $discovery;

  /**
   * The dependency injection container.
   *
   * @var \Symfony\Component\DependencyInjection\ContainerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $container;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $moduleHandler;

  /**
   * The cache backend to use.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $cache;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $languageManager;

  /**
   * The string translationManager.
   *
   * @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $translationManager;

   * The controller resolver.
   * @var \Drupal\Core\Controller\ControllerResolverInterface|\PHPUnit_Framework_MockObject_MockObject
  protected $controllerResolver;
   * The typed data manager.
   * @var \Drupal\Core\TypedData\TypedDataManager|\PHPUnit_Framework_MockObject_MockObject
  protected $typedDataManager;
  /**
   * The keyvalue collection for tracking installed definitions.
   *
   * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $installedDefinitions;

  /**
   * The event dispatcher.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $eventDispatcher;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    $this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
    $this->moduleHandler->expects($this->any())
      ->method('getImplementations')
      ->will($this->returnValue(array()));

    $this->cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');

    $language = $this->getMock('Drupal\Core\Language\LanguageInterface');
    $language->expects($this->any())
      ->method('getId')
      ->will($this->returnValue('en'));
    $this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
    $this->languageManager->expects($this->any())
      ->method('getCurrentLanguage')
      ->will($this->returnValue($language));
    $this->languageManager->expects($this->any())
      ->method('getLanguages')
      ->will($this->returnValue(array('en' => (object) array('id' => 'en'))));

    $this->translationManager = $this->getStringTranslationStub();

    $this->formBuilder = $this->getMock('Drupal\Core\Form\FormBuilderInterface');
    $this->controllerResolver = $this->getClassResolverStub();

    $this->container = $this->getContainerWithCacheBins($this->cache);
    $this->discovery = $this->getMock('Drupal\Component\Plugin\Discovery\DiscoveryInterface');

    $this->typedDataManager = $this->getMockBuilder('\Drupal\Core\TypedData\TypedDataManager')
      ->disableOriginalConstructor()
      ->getMock();
    $this->eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');

    $this->installedDefinitions = $this->getMock('Drupal\Core\KeyValueStore\KeyValueStoreInterface');
  }

  /**
   * Sets up the entity manager to be tested.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface[]|\PHPUnit_Framework_MockObject_MockObject[] $definitions
   *   (optional) An array of entity type definitions.
   */
  protected function setUpEntityManager($definitions = array()) {
    $class = $this->getMockClass('Drupal\Core\Entity\EntityInterface');
    foreach ($definitions as $entity_type) {
      $entity_type->expects($this->any())
        ->method('getClass')
        ->will($this->returnValue($class));
    }
    $this->discovery->expects($this->any())
      ->method('getDefinition')
      ->will($this->returnCallback(function ($entity_type_id, $exception_on_invalid = FALSE) use ($definitions) {
        if (isset($definitions[$entity_type_id])) {
          return $definitions[$entity_type_id];
        }
        elseif (!$exception_on_invalid) {
          return NULL;
        }
        else throw new PluginNotFoundException($entity_type_id);
      }));
    $this->discovery->expects($this->any())
      ->method('getDefinitions')
      ->will($this->returnValue($definitions));

    $this->entityManager = new TestEntityManager(new \ArrayObject(), $this->moduleHandler, $this->cache, $this->languageManager, $this->translationManager, $this->getClassResolverStub(), $this->typedDataManager, $this->installedDefinitions, $this->eventDispatcher);
    $this->entityManager->setContainer($this->container);
    $this->entityManager->setDiscovery($this->discovery);
  }

  /**
   * Tests the clearCachedDefinitions() method.
   *
   * @covers ::clearCachedDefinitions
   *
   */
  public function testClearCachedDefinitions() {
    $this->setUpEntityManager();
    $this->cache->expects($this->at(0))
    $this->cache->expects($this->at(1))
      ->method('deleteTags')
    $this->cache->expects($this->at(2))
      ->method('deleteTags')
      ->with(array('entity_field_info'));

    $this->entityManager->clearCachedDefinitions();
  }

  /**
   * Tests the getDefinition() method.
   *
   *
   * @dataProvider providerTestGetDefinition
   */
  public function testGetDefinition($entity_type_id, $expected) {
    $entity = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
    $this->setUpEntityManager(array(
      'apple' => $entity,
      'banana' => $entity,
    ));

    $entity_type = $this->entityManager->getDefinition($entity_type_id, FALSE);
    if ($expected) {
      $this->assertInstanceOf('Drupal\Core\Entity\EntityTypeInterface', $entity_type);
    }
    else {
      $this->assertNull($entity_type);
    }
  }

  /**
   * Provides test data for testGetDefinition().
   *
   * @return array
   *   Test data.
   */
  public function providerTestGetDefinition() {
    return array(
      array('apple', TRUE),
      array('banana', TRUE),
      array('pear', FALSE),
    );
  }

  /**
   * Tests the getDefinition() method with an invalid definition.
   *
   * @expectedException \Drupal\Component\Plugin\Exception\PluginNotFoundException
   * @expectedExceptionMessage The "pear" entity type does not exist.
   */
  public function testGetDefinitionInvalidException() {
    $this->setUpEntityManager();

    $this->entityManager->getDefinition('pear', TRUE);
  }

  /**
   * @dataProvider providerTestHasHandler
  public function testHasHandler($entity_type_id, $expected) {
    $apple = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
    $apple->expects($this->any())
      ->with('storage')
      ->will($this->returnValue(TRUE));
    $banana = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
    $banana->expects($this->any())
      ->with('storage')
      ->will($this->returnValue(FALSE));
    $this->setUpEntityManager(array(
      'apple' => $apple,
      'banana' => $banana,
    ));

    $entity_type = $this->entityManager->hasHandler($entity_type_id, 'storage');
    $this->assertSame($expected, $entity_type);
  }

  /**
   * Provides test data for testHasHandler().
  public function providerTestHasHandler() {
    return array(
      array('apple', TRUE),
      array('banana', FALSE),
      array('pear', FALSE),
    );
  }

  /**
    $class = $this->getTestHandlerClass();
    $entity = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
    $entity->expects($this->once())
      ->method('getHandlerClass')
      ->with('storage')
      ->will($this->returnValue($class));
    $this->setUpEntityManager(array('test_entity_type' => $entity));

    $this->assertInstanceOf($class, $this->entityManager->getStorage('test_entity_type'));
   * Tests the getListBuilder() method.
  public function testGetListBuilder() {
    $class = $this->getTestHandlerClass();
    $entity = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
    $entity->expects($this->once())
      ->method('getHandlerClass')
      ->with('list_builder')
      ->will($this->returnValue($class));
    $this->setUpEntityManager(array('test_entity_type' => $entity));

    $this->assertInstanceOf($class, $this->entityManager->getListBuilder('test_entity_type'));
   */
  public function testGetViewBuilder() {
    $class = $this->getTestHandlerClass();
    $entity = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
    $entity->expects($this->once())
      ->method('getHandlerClass')
      ->with('view_builder')
      ->will($this->returnValue($class));
    $this->setUpEntityManager(array('test_entity_type' => $entity));

    $this->assertInstanceOf($class, $this->entityManager->getViewBuilder('test_entity_type'));
  }

  /**
   * Tests the getAccessControlHandler() method.
   * @covers ::getAccessControlHandler
  public function testGetAccessControlHandler() {
    $class = $this->getTestHandlerClass();
    $entity = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
    $entity->expects($this->once())
      ->method('getHandlerClass')
      ->with('access')
      ->will($this->returnValue($class));
    $this->setUpEntityManager(array('test_entity_type' => $entity));

    $this->assertInstanceOf($class, $this->entityManager->getAccessControlHandler('test_entity_type'));
    $apple = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
    $apple->expects($this->once())
      ->method('getFormClass')
      ->with('default')
      ->will($this->returnValue('Drupal\Tests\Core\Entity\TestEntityForm'));
    $banana = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
    $banana->expects($this->once())
      ->method('getFormClass')
      ->with('default')
      ->will($this->returnValue('Drupal\Tests\Core\Entity\TestEntityFormInjected'));
    $this->setUpEntityManager(array(
      'apple' => $apple,
      'banana' => $banana,
    ));

    $apple_form = $this->entityManager->getFormObject('apple', 'default');
    $this->assertInstanceOf('Drupal\Tests\Core\Entity\TestEntityForm', $apple_form);
    $this->assertAttributeInstanceOf('Drupal\Core\Extension\ModuleHandlerInterface', 'moduleHandler', $apple_form);
    $this->assertAttributeInstanceOf('Drupal\Core\StringTranslation\TranslationInterface', 'stringTranslation', $apple_form);
    $banana_form = $this->entityManager->getFormObject('banana', 'default');
    $this->assertInstanceOf('Drupal\Tests\Core\Entity\TestEntityFormInjected', $banana_form);
    $this->assertAttributeEquals('yellow', 'color', $banana_form);

  }

  /**
   * Tests the getFormObject() method with an invalid operation.
   *
   * @expectedException \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   */
  public function testGetFormObjectInvalidOperation() {
    $entity = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
    $entity->expects($this->once())
      ->method('getFormClass')
      ->with('edit')
      ->will($this->returnValue(''));
    $this->setUpEntityManager(array('test_entity_type' => $entity));

    $this->entityManager->getFormObject('test_entity_type', 'edit');
  public function testGetHandler() {
    $class = $this->getTestHandlerClass();
    $apple = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
    $apple->expects($this->once())
      ->with('storage')
      ->will($this->returnValue($class));
    $this->setUpEntityManager(array(
      'apple' => $apple,
    ));

    $apple_controller = $this->entityManager->getHandler('apple', 'storage');
    $this->assertInstanceOf($class, $apple_controller);
    $this->assertAttributeInstanceOf('Drupal\Core\Extension\ModuleHandlerInterface', 'moduleHandler', $apple_controller);
    $this->assertAttributeInstanceOf('Drupal\Core\StringTranslation\TranslationInterface', 'stringTranslation', $apple_controller);
   * Tests the getHandler() method when no controller is defined.
   *
   * @expectedException \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   */
  public function testGetHandlerMissingHandler() {
    $entity = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
    $entity->expects($this->once())
      ->with('storage')
      ->will($this->returnValue(''));
    $this->setUpEntityManager(array('test_entity_type' => $entity));
    $this->entityManager->getHandler('test_entity_type', 'storage');
  /**
   * Tests the getBaseFieldDefinitions() method.
   *
   * @covers ::getBaseFieldDefinitions
   * @covers ::buildBaseFieldDefinitions
   */
  public function testGetBaseFieldDefinitions() {
    $field_definition = $this->setUpEntityWithFieldDefinition();

    $expected = array('id' => $field_definition);
    $this->assertSame($expected, $this->entityManager->getBaseFieldDefinitions('test_entity_type'));
  }

  /**
   * Tests the getFieldDefinitions() method.
   *
   * @covers ::getFieldDefinitions
   * @covers ::buildBundleFieldDefinitions
   */
  public function testGetFieldDefinitions() {
    $field_definition = $this->setUpEntityWithFieldDefinition();

    $expected = array('id' => $field_definition);
    $this->assertSame($expected, $this->entityManager->getFieldDefinitions('test_entity_type', 'test_entity_bundle'));
  }

  /**
   * Tests the getFieldStorageDefinitions() method.
   *
   * @covers ::getFieldStorageDefinitions
   * @covers ::buildFieldStorageDefinitions
   */
  public function testGetFieldStorageDefinitions() {
    $field_definition = $this->setUpEntityWithFieldDefinition(TRUE);
    $field_storage_definition = $this->getMock('\Drupal\Core\Field\FieldStorageDefinitionInterface');
    $field_storage_definition->expects($this->any())
      ->method('getName')
      ->will($this->returnValue('field_storage'));

    $this->moduleHandler->expects($this->any())
      ->will($this->returnValueMap(array(
        array('entity_type_build', array()),
        array('entity_base_field_info', array()),
        array('entity_field_storage_info', array('example_module')),
      )));

    $this->moduleHandler->expects($this->any())
      ->method('invoke')
      ->with('example_module', 'entity_field_storage_info')
      ->will($this->returnValue(array('field_storage' => $field_storage_definition)));

    $expected = array(
      'id' => $field_definition,
      'field_storage' => $field_storage_definition,
    );
    $this->assertSame($expected, $this->entityManager->getFieldStorageDefinitions('test_entity_type'));
  }

   * Tests the getBaseFieldDefinitions() method with caching.
   * @covers ::getBaseFieldDefinitions
  public function testGetBaseFieldDefinitionsWithCaching() {
    $field_definition = $this->setUpEntityWithFieldDefinition();

    $expected = array('id' => $field_definition);

    $this->cache->expects($this->at(0))
      ->method('get')
      ->with('entity_base_field_definitions:test_entity_type:en', FALSE)
      ->will($this->returnValue(FALSE));
    $this->cache->expects($this->at(1))
      ->method('get')
    $this->cache->expects($this->at(2))
    $this->cache->expects($this->at(3))
      ->method('set')
      ->with('entity_base_field_definitions:test_entity_type:en');
    $this->cache->expects($this->at(4))
      ->method('get')
      ->with('entity_base_field_definitions:test_entity_type:en', FALSE)
      ->will($this->returnValue((object) array('data' => $expected)));
    $this->assertSame($expected, $this->entityManager->getBaseFieldDefinitions('test_entity_type'));
    $this->entityManager->testClearEntityFieldInfo();
    $this->assertSame($expected, $this->entityManager->getBaseFieldDefinitions('test_entity_type'));
   * Tests the getFieldDefinitions() method with caching.
  public function testGetFieldDefinitionsWithCaching() {
    $field_definition = $this->setUpEntityWithFieldDefinition(FALSE, 'id');

    $expected = array('id' => $field_definition);

    $this->cache->expects($this->at(0))
      ->method('get')
      ->with('entity_base_field_definitions:test_entity_type:en', FALSE)
      ->will($this->returnValue((object) array('data' => $expected)));
    $this->cache->expects($this->at(1))
      ->method('get')
      ->with('entity_bundle_field_definitions:test_entity_type:test_bundle:en', FALSE)
      ->will($this->returnValue(FALSE));
    $this->cache->expects($this->at(2))
    $this->cache->expects($this->at(3))
      ->method('set');
    $this->cache->expects($this->at(4))
      ->method('set');
    $this->cache->expects($this->at(5))
      ->method('get')
      ->with('entity_base_field_definitions:test_entity_type:en', FALSE)
      ->will($this->returnValue((object) array('data' => $expected)));
    $this->cache->expects($this->at(6))
      ->method('get')
      ->with('entity_bundle_field_definitions:test_entity_type:test_bundle:en', FALSE)
      ->will($this->returnValue((object) array('data' => $expected)));

    $this->assertSame($expected, $this->entityManager->getFieldDefinitions('test_entity_type', 'test_bundle'));
    $this->entityManager->testClearEntityFieldInfo();
    $this->assertSame($expected, $this->entityManager->getFieldDefinitions('test_entity_type', 'test_bundle'));
  /**
   * Tests the getFieldStorageDefinitions() method with caching.
   *
   * @covers ::getFieldStorageDefinitions
   */
  public function testGetFieldStorageDefinitionsWithCaching() {
    $field_definition = $this->setUpEntityWithFieldDefinition(TRUE, 'id');
    $field_storage_definition = $this->getMock('\Drupal\Core\Field\FieldStorageDefinitionInterface');
    $field_storage_definition->expects($this->any())
      ->method('getName')
      ->will($this->returnValue('field_storage'));

    $this->moduleHandler->expects($this->any())
      ->method('getImplementations')
      ->will($this->returnValueMap(array(
        array('entity_field_storage_info', array('example_module')),
        array('entity_type_build', array())
      )));

    $this->moduleHandler->expects($this->once())
      ->method('invoke')
      ->with('example_module')
      ->will($this->returnValue(array('field_storage' => $field_storage_definition)));

    $expected = array(
      'id' => $field_definition,
      'field_storage' => $field_storage_definition,
    );

    $this->cache->expects($this->at(0))
      ->method('get')
      ->with('entity_base_field_definitions:test_entity_type:en', FALSE)
      ->will($this->returnValue((object) array('data' => array('id' => $expected['id']))));
    $this->cache->expects($this->at(1))
      ->method('get')
      ->with('entity_field_storage_definitions:test_entity_type:en', FALSE)
      ->will($this->returnValue(FALSE));
    $this->cache->expects($this->at(2))
    $this->cache->expects($this->at(4))
      ->method('set')
      ->with('entity_field_storage_definitions:test_entity_type:en');
    $this->cache->expects($this->at(5))
      ->method('get')
      ->with('entity_base_field_definitions:test_entity_type:en', FALSE)
      ->will($this->returnValue((object) array('data' => array('id' => $expected['id']))));
    $this->cache->expects($this->at(6))
      ->method('get')
      ->with('entity_field_storage_definitions:test_entity_type:en', FALSE)
      ->will($this->returnValue((object) array('data' => $expected)));

    $this->assertSame($expected, $this->entityManager->getFieldStorageDefinitions('test_entity_type'));
    $this->entityManager->testClearEntityFieldInfo();
    $this->assertSame($expected, $this->entityManager->getFieldStorageDefinitions('test_entity_type'));
  }

   * Tests the getBaseFieldDefinitions() method with an invalid definition.
   * @covers ::getBaseFieldDefinitions
   * @covers ::buildBaseFieldDefinitions
  public function testGetBaseFieldDefinitionsInvalidDefinition() {
    $langcode_definition = $this->setUpEntityWithFieldDefinition(FALSE, 'langcode', array('langcode' => 'langcode'));
    $langcode_definition->expects($this->once())
      ->method('isTranslatable')
      ->will($this->returnValue(TRUE));

    $this->entityManager->getBaseFieldDefinitions('test_entity_type');
  /**
   * Tests that getFieldDefinitions() method sets the 'provider' definition key.
   *
   * @covers ::getFieldDefinitions
   * @covers ::buildBundleFieldDefinitions
   */
  public function testGetFieldDefinitionsProvider() {
    $this->setUpEntityWithFieldDefinition(TRUE);

    $module = 'entity_manager_test_module';

    // @todo Mock FieldDefinitionInterface once it exposes a proper provider
    //   setter. See https://drupal.org/node/2225961.
    $field_definition = $this->getMockBuilder('Drupal\Core\Field\BaseFieldDefinition')
      ->disableOriginalConstructor()
      ->getMock();

    // We expect two calls as the field definition will be returned from both
    // base and bundle entity field info hook implementations.
    $field_definition
      ->expects($this->exactly(2))
      ->method('setProvider')
      ->with($this->matches($module));

    $this->moduleHandler->expects($this->any())
      ->method('getImplementations')
      ->will($this->returnValue(array($module)));

    $this->moduleHandler->expects($this->any())
      ->method('invoke')
      ->with($this->matches($module))
      ->will($this->returnValue(array($field_definition)));

    $this->entityManager->getFieldDefinitions('test_entity_type', 'test_bundle');
  }

  /**
   * 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|\PHPUnit_Framework_MockObject_MockObject
  protected function setUpEntityWithFieldDefinition($custom_invoke_all = FALSE, $field_definition_id = 'id', $entity_keys = array()) {
    $entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
    $entity = $this->getMockBuilder('Drupal\Tests\Core\Entity\EntityManagerTestEntity')
      ->disableOriginalConstructor()
      ->getMockForAbstractClass();
    $entity_type->expects($this->any())
      ->method('getClass')
      ->will($this->returnValue($entity_class));
    $entity_type->expects($this->any())
      ->will($this->returnValue($entity_keys));
    $entity_type->expects($this->any())
      ->method('isSubclassOf')
      ->with($this->equalTo('\Drupal\Core\Entity\FieldableEntityInterface'))
    $field_definition = $this->getMockBuilder('Drupal\Core\Field\BaseFieldDefinition')
      ->disableOriginalConstructor()
      ->getMock();
    $entity_class::$baseFieldDefinitions = array(
      $field_definition_id => $field_definition,
    );
    $entity_class::$bundleFieldDefinitions = array();
    $this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
    $this->moduleHandler->expects($this->any())
      ->method('alter');
    if (!$custom_invoke_all) {
      $this->moduleHandler->expects($this->any())
    // 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));

    return $field_definition;
  }

  /**
   * Tests the clearCachedFieldDefinitions() method.
   *
   * @covers ::clearCachedFieldDefinitions
   */
  public function testClearCachedFieldDefinitions() {
    $this->setUpEntityManager();
    $this->cache->expects($this->once())
      ->method('deleteTags')
      ->with(array('entity_field_info'));
    $this->typedDataManager->expects($this->once())
      ->method('clearCachedDefinitions');

    $this->entityManager->clearCachedFieldDefinitions();
  }

  /**
   * Tests the clearCachedBundles() method.
   *
   */
  public function testClearCachedBundles() {
    $this->setUpEntityManager();
    $this->cache->expects($this->once())
      ->method('deleteTags')

    $this->entityManager->clearCachedBundles();
  }

   *
   * @dataProvider providerTestGetBundleInfo
   */
  public function testGetBundleInfo($entity_type_id, $expected) {
    $apple = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
    $apple->expects($this->once())
      ->method('getLabel')
      ->will($this->returnValue('Apple'));
    $banana = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
    $banana->expects($this->once())
      ->method('getLabel')
      ->will($this->returnValue('Banana'));
    $this->setUpEntityManager(array(
      'apple' => $apple,
      'banana' => $banana,
    ));

    $bundle_info = $this->entityManager->getBundleInfo($entity_type_id);
    $this->assertSame($expected, $bundle_info);
  }

  /**
   * Provides test data for testGetBundleInfo().
   *
   * @return array
   *   Test data.
   */
  public function providerTestGetBundleInfo() {
    return array(
      array('apple', array(
        'apple' => array(
          'label' => 'Apple',
        ),
      )),
      array('banana', array(
        'banana' => array(
          'label' => 'Banana',
        ),
      )),
      array('pear', array()),
    );
  }

  /**
   * Tests the getAllBundleInfo() method.
   *
   */
  public function testGetAllBundleInfo() {
    $apple = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
    $apple->expects($this->once())
      ->method('getLabel')
      ->will($this->returnValue('Apple'));
    $banana = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
    $banana->expects($this->once())
      ->method('getLabel')
      ->will($this->returnValue('Banana'));
    $this->setUpEntityManager(array(
      'apple' => $apple,
      'banana' => $banana,
    ));
    $this->cache->expects($this->at(0))
      ->method('get')
      ->with("entity_bundle_info:en", FALSE)
      ->will($this->returnValue(FALSE));
    $this->cache->expects($this->at(1))
      ->method('get')
      ->will($this->returnValue(FALSE));
    $this->cache->expects($this->at(2))
    $this->cache->expects($this->at(3))
      ->method('set')
      ->with("entity_bundle_info:en");
    $this->cache->expects($this->at(4))
      ->method('deleteTags')
    $this->cache->expects($this->at(5))
      ->method('deleteTags')
    $this->cache->expects($this->at(6))
      ->method('deleteTags')
      ->with(array('entity_field_info'));
    $this->cache->expects($this->at(7))
      ->with("entity_bundle_info:en", FALSE)
      ->will($this->returnValue((object) array('data' => 'cached data')));

    $expected = array(
      'apple' => array(
        'apple' => array(
          'label' => 'Apple',
        ),
      ),
      'banana' => array(
        'banana' => array(
          'label' => 'Banana',
        ),
      ),
    );
    $bundle_info = $this->entityManager->getAllBundleInfo();
    $this->assertSame($expected, $bundle_info);

    $bundle_info = $this->entityManager->getAllBundleInfo();
    $this->assertSame($expected, $bundle_info);

    $this->entityManager->clearCachedDefinitions();

    $bundle_info = $this->entityManager->getAllBundleInfo();
    $this->assertSame('cached data', $bundle_info);
  }

  /**
   * Tests the getEntityTypeLabels() method.
   *
   */
  public function testGetEntityTypeLabels() {
    $apple = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
    $apple->expects($this->once())
      ->method('getLabel')
      ->will($this->returnValue('Apple'));
    $banana = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
    $banana->expects($this->once())
      ->method('getLabel')
      ->will($this->returnValue('Banana'));
    $this->setUpEntityManager(array(
      'apple' => $apple,
      'banana' => $banana,
    ));

    $expected = array(
      'apple' => 'Apple',
      'banana' => 'Banana',
    );
    $this->assertSame($expected, $this->entityManager->getEntityTypeLabels());
  }

  /**
   * Tests the getTranslationFromContext() method.
   *
   * @covers ::getTranslationFromContext
   */
  public function testGetTranslationFromContext() {
    $this->setUpEntityManager();

    $this->languageManager->expects($this->exactly(2))
      ->method('getFallbackCandidates')
      ->will($this->returnCallback(function (array $context = array()) {
        if (!empty($context['langcode'])) {
          $candidates[$context['langcode']] = $context['langcode'];
        }
        return $candidates;
      }));

    $entity = $this->getMock('Drupal\Tests\Core\Entity\TestContentEntityInterface');
    $entity->expects($this->exactly(2))
      ->method('getUntranslated')
      ->will($this->returnValue($entity));
    $language = $this->getMock('\Drupal\Core\Language\LanguageInterface');
    $language->expects($this->any())
      ->method('getId')
      ->will($this->returnValue('en'));
    $entity->expects($this->exactly(2))
      ->method('language')
      ->will($this->returnValue($language));
    $entity->expects($this->exactly(2))
      ->method('hasTranslation')
      ->will($this->returnValueMap(array(
        array(LanguageInterface::LANGCODE_DEFAULT, FALSE),
        array('custom_langcode', TRUE),
      )));

    $translated_entity = $this->getMock('Drupal\Tests\Core\Entity\TestContentEntityInterface');
    $entity->expects($this->once())
      ->method('getTranslation')
      ->with('custom_langcode')
      ->will($this->returnValue($translated_entity));

    $this->assertSame($entity, $this->entityManager->getTranslationFromContext($entity));
    $this->assertSame($translated_entity, $this->entityManager->getTranslationFromContext($entity, 'custom_langcode'));
  }

  /**
   * @covers ::getExtraFields
   */
  function testgetExtraFields() {
    $this->setUpEntityManager();

    $entity_type_id = $this->randomMachineName();
    $bundle = $this->randomMachineName();
    $language_code = 'en';
    $hook_bundle_extra_fields = array(
      $entity_type_id => array(
        $bundle => array(
          'form' => array(