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;
Alex Pott
committed
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;
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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;
Alex Pott
committed
/**
Alex Pott
committed
*
Alex Pott
committed
* @var \Drupal\Core\Controller\ControllerResolverInterface|\PHPUnit_Framework_MockObject_MockObject
Alex Pott
committed
*/
protected $controllerResolver;
Alex Pott
committed
/**
Alex Pott
committed
*
* @var \Drupal\Core\TypedData\TypedDataManager|\PHPUnit_Framework_MockObject_MockObject
Alex Pott
committed
*/
protected $typedDataManager;
Alex Pott
committed
Alex Pott
committed
/**
* The keyvalue collection for tracking installed definitions.
*
* @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $installedDefinitions;
catch
committed
/**
* 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')
->with('entity_type_build')
->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();
Alex Pott
committed
$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();
Alex Pott
committed
catch
committed
$this->eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
Alex Pott
committed
$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');
Alex Pott
committed
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));
catch
committed
$this->entityManager = new TestEntityManager(new \ArrayObject(), $this->moduleHandler, $this->cache, $this->languageManager, $this->translationManager, $this->getClassResolverStub(), $this->typedDataManager, $this->installedDefinitions, $this->eventDispatcher);
Alex Pott
committed
$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))
->method('deleteTags')
->with(array('entity_types'));
$this->cache->expects($this->at(1))
->method('deleteTags')
->with(array('entity_bundles'));
$this->cache->expects($this->at(2))
->method('deleteTags')
->with(array('entity_field_info'));
$this->entityManager->clearCachedDefinitions();
}
/**
* Tests the getDefinition() method.
*
* @covers ::getDefinition
*
* @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.
*
* @covers ::getDefinition
*
Angie Byron
committed
* @expectedException \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @expectedExceptionMessage The "pear" entity type does not exist.
*/
public function testGetDefinitionInvalidException() {
$this->setUpEntityManager();
$this->entityManager->getDefinition('pear', TRUE);
}
/**
Alex Pott
committed
* Tests the hasHandler() method.
*
* @covers ::hasHandler
*
Alex Pott
committed
* @dataProvider providerTestHasHandler
*/
Alex Pott
committed
public function testHasHandler($entity_type_id, $expected) {
$apple = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
$apple->expects($this->any())
Alex Pott
committed
->method('hasHandlerClass')
->with('storage')
->will($this->returnValue(TRUE));
$banana = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
$banana->expects($this->any())
Alex Pott
committed
->method('hasHandlerClass')
->with('storage')
->will($this->returnValue(FALSE));
$this->setUpEntityManager(array(
'apple' => $apple,
'banana' => $banana,
));
Alex Pott
committed
$entity_type = $this->entityManager->hasHandler($entity_type_id, 'storage');
$this->assertSame($expected, $entity_type);
}
/**
Alex Pott
committed
* Provides test data for testHasHandler().
*
* @return array
* Test data.
*/
Alex Pott
committed
public function providerTestHasHandler() {
return array(
array('apple', TRUE),
array('banana', FALSE),
array('pear', FALSE),
);
}
/**
catch
committed
* Tests the getStorage() method.
*
* @covers ::getStorage
*/
catch
committed
public function testGetStorage() {
Alex Pott
committed
$class = $this->getTestHandlerClass();
$entity = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
$entity->expects($this->once())
Alex Pott
committed
->method('getHandlerClass')
->with('storage')
->will($this->returnValue($class));
$this->setUpEntityManager(array('test_entity_type' => $entity));
catch
committed
$this->assertInstanceOf($class, $this->entityManager->getStorage('test_entity_type'));
}
/**
* Tests the getListBuilder() method.
*
* @covers ::getListBuilder
*/
public function testGetListBuilder() {
Alex Pott
committed
$class = $this->getTestHandlerClass();
$entity = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
$entity->expects($this->once())
Alex Pott
committed
->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'));
}
/**
* Tests the getViewBuilder() method.
*
* @covers ::getViewBuilder
*/
public function testGetViewBuilder() {
Alex Pott
committed
$class = $this->getTestHandlerClass();
$entity = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
$entity->expects($this->once())
Alex Pott
committed
->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() {
Alex Pott
committed
$class = $this->getTestHandlerClass();
$entity = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
$entity->expects($this->once())
Alex Pott
committed
->method('getHandlerClass')
->with('access')
->will($this->returnValue($class));
$this->setUpEntityManager(array('test_entity_type' => $entity));
$this->assertInstanceOf($class, $this->entityManager->getAccessControlHandler('test_entity_type'));
}
/**
Alex Pott
committed
* Tests the getFormObject() method.
*
* @covers ::getFormObject
*/
Alex Pott
committed
public function testGetFormObject() {
$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,
));
Alex Pott
committed
$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);
Alex Pott
committed
$this->assertAttributeInstanceOf('Drupal\Core\StringTranslation\TranslationInterface', 'stringTranslation', $apple_form);
Alex Pott
committed
$banana_form = $this->entityManager->getFormObject('banana', 'default');
$this->assertInstanceOf('Drupal\Tests\Core\Entity\TestEntityFormInjected', $banana_form);
$this->assertAttributeEquals('yellow', 'color', $banana_form);
}
/**
Alex Pott
committed
* Tests the getFormObject() method with an invalid operation.
*
* @covers ::getFormObject
*
* @expectedException \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
*/
Alex Pott
committed
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));
Alex Pott
committed
$this->entityManager->getFormObject('test_entity_type', 'edit');
}
/**
Alex Pott
committed
* Tests the getHandler() method.
*
* @covers ::getHandler
*/
Alex Pott
committed
public function testGetHandler() {
$class = $this->getTestHandlerClass();
$apple = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
$apple->expects($this->once())
Alex Pott
committed
->method('getHandlerClass')
->with('storage')
->will($this->returnValue($class));
$this->setUpEntityManager(array(
'apple' => $apple,
));
Alex Pott
committed
$apple_controller = $this->entityManager->getHandler('apple', 'storage');
$this->assertInstanceOf($class, $apple_controller);
$this->assertAttributeInstanceOf('Drupal\Core\Extension\ModuleHandlerInterface', 'moduleHandler', $apple_controller);
Alex Pott
committed
$this->assertAttributeInstanceOf('Drupal\Core\StringTranslation\TranslationInterface', 'stringTranslation', $apple_controller);
}
/**
Alex Pott
committed
* Tests the getHandler() method when no controller is defined.
*
* @covers ::getHandler
*
* @expectedException \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
*/
Alex Pott
committed
public function testGetHandlerMissingHandler() {
$entity = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
$entity->expects($this->once())
Alex Pott
committed
->method('getHandlerClass')
->with('storage')
->will($this->returnValue(''));
$this->setUpEntityManager(array('test_entity_type' => $entity));
Alex Pott
committed
$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'));
}
Alex Pott
committed
/**
* Tests the getFieldStorageDefinitions() method.
*
* @covers ::getFieldStorageDefinitions
* @covers ::buildFieldStorageDefinitions
Alex Pott
committed
*/
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())
Alex Pott
committed
->method('getImplementations')
->will($this->returnValueMap(array(
array('entity_type_build', array()),
array('entity_base_field_info', array()),
array('entity_field_storage_info', array('example_module')),
)));
Alex Pott
committed
$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')
->with('entity_type', FALSE)
->will($this->returnValue(FALSE));
$this->cache->expects($this->at(2))
->method('set')
->with('entity_type');
$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.
*
* @covers ::getFieldDefinitions
*/
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))
->method('get')
->with('entity_type', FALSE)
->will($this->returnValue(FALSE));
$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'));
}
Alex Pott
committed
/**
* Tests the getFieldStorageDefinitions() method with caching.
*
* @covers ::getFieldStorageDefinitions
Alex Pott
committed
*/
public function testGetFieldStorageDefinitionsWithCaching() {
$field_definition = $this->setUpEntityWithFieldDefinition(TRUE, 'id');
Alex Pott
committed
$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())
)));
Alex Pott
committed
$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))
->method('get')
->with('entity_type', FALSE)
->will($this->returnValue(FALSE));
Alex Pott
committed
$this->cache->expects($this->at(3))
->method('set')
->with('entity_type');
$this->cache->expects($this->at(4))
->method('set')
->with('entity_field_storage_definitions:test_entity_type:en');
$this->cache->expects($this->at(5))
Alex Pott
committed
->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))
Alex Pott
committed
->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
*
* @expectedException \LogicException
*/
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');
}
catch
committed
/**
* Tests that getFieldDefinitions() method sets the 'provider' definition key.
*
* @covers ::getFieldDefinitions
* @covers ::buildBundleFieldDefinitions
catch
committed
*/
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.
Alex Pott
committed
$field_definition = $this->getMockBuilder('Drupal\Core\Field\BaseFieldDefinition')
catch
committed
->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.
*
Alex Pott
committed
* @return \Drupal\Core\Field\BaseFieldDefinition|\PHPUnit_Framework_MockObject_MockObject
* A field definition object.
*/
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_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($entity_keys));
Angie Byron
committed
$entity_type->expects($this->any())
->method('isSubclassOf')
Alex Pott
committed
->with($this->equalTo('\Drupal\Core\Entity\FieldableEntityInterface'))
Angie Byron
committed
->will($this->returnValue(TRUE));
Alex Pott
committed
$field_definition = $this->getMockBuilder('Drupal\Core\Field\BaseFieldDefinition')
->disableOriginalConstructor()
->getMock();
$entity_class::$baseFieldDefinitions = array(
$field_definition_id => $field_definition,
);
$entity_class::$bundleFieldDefinitions = array();
catch
committed
$this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
$this->moduleHandler->expects($this->any())
->method('alter');
if (!$custom_invoke_all) {
$this->moduleHandler->expects($this->any())
catch
committed
->method('getImplementations')
->will($this->returnValue(array()));
}
// 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())
Alex Pott
committed
->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.
*
* @covers ::clearCachedBundles
*/
public function testClearCachedBundles() {
$this->setUpEntityManager();
$this->cache->expects($this->once())
->method('deleteTags')
->with(array('entity_bundles'));
$this->entityManager->clearCachedBundles();
}
/**
* Tests the getBundleInfo() method.
*
* @covers ::getBundleInfo
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
*
* @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.
*
* @covers ::getAllBundleInfo
*/
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')
->with("entity_type", FALSE)
->will($this->returnValue(FALSE));
$this->cache->expects($this->at(2))
->method('set')
->with("entity_type");
$this->cache->expects($this->at(3))
->method('set')
->with("entity_bundle_info:en");
$this->cache->expects($this->at(4))
->method('deleteTags')
->with(array('entity_types'));
$this->cache->expects($this->at(5))
->with(array('entity_bundles'));
$this->cache->expects($this->at(6))
->method('deleteTags')
->with(array('entity_field_info'));
$this->cache->expects($this->at(7))
->method('get')
->with("entity_bundle_info:en", FALSE)
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
->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.
*
* @covers ::getEntityTypeLabels
*/
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()) {
$candidates = 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();
Alex Pott
committed
$entity_type_id = $this->randomMachineName();
$bundle = $this->randomMachineName();