Commit 4862ed79 authored by webchick's avatar webchick

Issue #2039435 by Xano, Berdir | akshay.swnt22: Convert EntityManager to...

Issue #2039435 by Xano, Berdir | akshay.swnt22: Convert EntityManager to extend DefaultPluginManager.
parent 6131ff1d
......@@ -37,7 +37,7 @@ public function getDefinitions() {
/**
* Sets a plugin definition.
*/
public function setDefinition($plugin, array $definition) {
public function setDefinition($plugin, $definition) {
$this->definitions[$plugin] = $definition;
}
......
......@@ -270,6 +270,11 @@ protected function findDefinitions() {
// If this plugin was provided by a module that does not exist, remove the
// plugin definition.
foreach ($definitions as $plugin_id => $plugin_definition) {
// If the plugin definition is an object, attempt to convert it to an
// array, if that is not possible, skip further processing.
if (is_object($plugin_definition) && !($plugin_definition = (array) $plugin_definition)) {
continue;
}
if (isset($plugin_definition['provider']) && !in_array($plugin_definition['provider'], array('Core', 'Component')) && !$this->moduleHandler->moduleExists($plugin_definition['provider'])) {
unset($definitions[$plugin_id]);
}
......
......@@ -5,7 +5,7 @@
* Contains \Drupal\Tests\Core\Entity\EntityManagerTest.
*/
namespace Drupal\Tests\Core\Entity;
namespace Drupal\Tests\Core\Entity {
use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
......@@ -79,13 +79,6 @@ class EntityManagerTest extends UnitTestCase {
*/
protected $translationManager;
/**
* The form builder.
*
* @var \Drupal\Core\Form\FormBuilderInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $formBuilder;
/**
* {@inheritdoc}
*/
......@@ -111,20 +104,19 @@ protected function setUp() {
$this->cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
$this->languageManager = $this->getMockBuilder('Drupal\Core\Language\LanguageManager')
->disableOriginalConstructor()
->getMock();
$this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
$this->languageManager->expects($this->any())
->method('getCurrentLanguage')
->will($this->returnValue((object) array('id' => 'en')));
$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->container = $this->getContainerWithCacheBins($this->cache);
$this->discovery = $this->getMock('Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface');
$this->discovery = $this->getMock('Drupal\Component\Plugin\Discovery\DiscoveryInterface');
}
/**
......@@ -149,7 +141,7 @@ protected function setUpEntityManager($definitions = array()) {
->method('getDefinitions')
->will($this->returnValue($definitions));
$this->entityManager = new TestEntityManager(new \ArrayObject(), $this->moduleHandler, $this->cache, $this->languageManager, $this->translationManager, $this->formBuilder);
$this->entityManager = new TestEntityManager(new \ArrayObject(), $this->moduleHandler, $this->cache, $this->languageManager, $this->translationManager);
$this->entityManager->setContainer($this->container);
$this->entityManager->setDiscovery($this->discovery);
}
......@@ -162,8 +154,9 @@ protected function setUpEntityManager($definitions = array()) {
*/
public function testClearCachedDefinitions() {
$this->setUpEntityManager();
$this->discovery->expects($this->once())
->method('clearCachedDefinitions');
$this->cache->expects($this->once())
->method('deleteTags')
->with(array('entity_types' => TRUE));
$this->entityManager->clearCachedDefinitions();
}
......@@ -491,15 +484,13 @@ public function testGetFieldStorageDefinitions() {
->method('getName')
->will($this->returnValue('field_storage'));
$this->moduleHandler->expects($this->at(0))
->method('getImplementations')
->with('entity_base_field_info')
->will($this->returnValue(array()));
$this->moduleHandler->expects($this->at(2))
$this->moduleHandler->expects($this->any())
->method('getImplementations')
->with('entity_field_storage_info')
->will($this->returnValue(array('example_module')));
->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')
......@@ -527,9 +518,17 @@ public function testGetBaseFieldDefinitionsWithCaching() {
->method('get')
->with('entity_base_field_definitions:test_entity_type:en', FALSE)
->will($this->returnValue(FALSE));
$this->cache->expects($this->once())
->method('set');
$this->cache->expects($this->at(1))
->method('get')
->with('entity_type::en', FALSE)
->will($this->returnValue(FALSE));
$this->cache->expects($this->at(2))
->method('set')
->with('entity_type::en');
$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)));
......@@ -558,12 +557,18 @@ public function testGetFieldDefinitionsWithCaching() {
->with('entity_bundle_field_definitions:test_entity_type:test_bundle:en', FALSE)
->will($this->returnValue(FALSE));
$this->cache->expects($this->at(2))
->method('set');
->method('get')
->with('entity_type::en', 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(4))
$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)));
......@@ -587,8 +592,10 @@ public function testGetFieldStorageDefinitionsWithCaching() {
$this->moduleHandler->expects($this->any())
->method('getImplementations')
->with('entity_field_storage_info')
->will($this->returnValue(array('example_module')));
->will($this->returnValueMap(array(
array('entity_field_storage_info', array('example_module')),
array('entity_type_build', array())
)));
$this->moduleHandler->expects($this->once())
->method('invoke')
......@@ -609,12 +616,20 @@ public function testGetFieldStorageDefinitionsWithCaching() {
->with('entity_field_storage_definitions:test_entity_type:en', FALSE)
->will($this->returnValue(FALSE));
$this->cache->expects($this->at(2))
->method('set');
->method('get')
->with('entity_type::en', FALSE)
->will($this->returnValue(FALSE));
$this->cache->expects($this->at(3))
->method('set')
->with('entity_type::en');
$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(4))
$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)));
......@@ -809,14 +824,27 @@ public function testGetAllBundleInfo() {
'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::en", FALSE)
->will($this->returnValue(FALSE));
$this->cache->expects($this->at(2))
->method('set')
->with("entity_type::en");
$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' => TRUE));
$this->cache->expects($this->at(5))
->method('get')
->with("entity_bundle_info:en", FALSE)
->will($this->returnValue((object) array('data' => 'cached data')));
$this->cache->expects($this->once())
->method('set');
$expected = array(
'apple' => array(
......@@ -1245,3 +1273,13 @@ public static function create(ContainerInterface $container) {
if (!defined('DRUPAL_ROOT')) {
define('DRUPAL_ROOT', dirname(dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__)))));
}
}
namespace {
/**
* Implements hook_entity_type_build().
*/
function entity_manager_test_module_entity_type_build() {
}
}
......@@ -71,8 +71,8 @@ protected function setUp() {
* Tests the plugin manager with a disabled module.
*/
public function testDefaultPluginManagerWithDisabledModule() {
$definitions = $this->expectedDefinitions;
$definitions['cherry'] = array(
$definitions = $this->expectedDefinitions;
$definitions['cherry'] = array(
'id' => 'cherry',
'label' => 'Cherry',
'color' => 'red',
......@@ -92,6 +92,31 @@ public function testDefaultPluginManagerWithDisabledModule() {
$this->assertEmpty($plugin_manager->getDefinition('cherry'), 'Plugin information of a disabled module is not available');
}
/**
* Tests the plugin manager and object plugin definitions.
*/
public function testDefaultPluginManagerWithObjects() {
$definitions = $this->expectedDefinitions;
$definitions['cherry'] = (object) array(
'id' => 'cherry',
'label' => 'Cherry',
'color' => 'red',
'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Cherry',
'provider' => 'disabled_module',
);
$module_handler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
$module_handler->expects($this->once())
->method('moduleExists')
->with('disabled_module')
->will($this->returnValue(FALSE));
$plugin_manager = new TestPluginManager($this->namespaces, $definitions, $module_handler, 'test_alter_hook');
$this->assertEmpty($plugin_manager->getDefinition('cherry'), 'Plugin information is available');
}
/**
* Tests the plugin manager with no cache and altering.
*/
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment