Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
<?php
/**
* @file
* Contains \Drupal\Core\Plugin\DefaultPluginManagerTest.
*/
namespace Drupal\Tests\Core\Plugin;
use Drupal\Core\Language\Language;
use Drupal\Tests\UnitTestCase;
/**
* Tests the DefaultPluginManager.
*
* @group Plugin
*/
class DefaultPluginManagerTest extends UnitTestCase {
/**
* The expected plugin definitions.
*
* @var array
*/
protected $expectedDefinitions;
/**
* The namespaces to look for plugin definitions.
*
* @var \Traversable
*/
protected $namespaces;
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => 'Default Plugin Manager',
'description' => 'Tests the DefaultPluginManager class.',
'group' => 'Plugin',
);
}
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->expectedDefinitions = array(
'apple' => array(
'id' => 'apple',
'label' => 'Apple',
'color' => 'green',
'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Apple',
),
'banana' => array(
'id' => 'banana',
'label' => 'Banana',
'color' => 'yellow',
'uses' => array(
'bread' => 'Banana bread',
),
'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Banana',
),
);
$this->namespaces = new \ArrayObject(array('Drupal\plugin_test' => DRUPAL_ROOT . '/core/modules/system/tests/modules/plugin_test/lib'));
}
/**
* Tests the plugin manager with a disabled module.
*/
public function testDefaultPluginManagerWithDisabledModule() {
$definitions = $this->expectedDefinitions;
$definitions['cherry'] = 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 of a disabled module is not available');
}
/**
* Tests the plugin manager with no cache and altering.
*/
public function testDefaultPluginManager() {
$plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions);
$this->assertEquals($this->expectedDefinitions, $plugin_manager->getDefinitions());
$this->assertEquals($this->expectedDefinitions['banana'], $plugin_manager->getDefinition('banana'));
}
/**
* Tests the plugin manager with no cache and altering.
*/
public function testDefaultPluginManagerWithAlter() {
$module_handler = $this->getMockBuilder('Drupal\Core\Extension\ModuleHandler')
->disableOriginalConstructor()
->getMock();
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Configure the stub.
$alter_hook_name = $this->randomName();
$module_handler->expects($this->once())
->method('alter')
->with($this->equalTo($alter_hook_name), $this->equalTo($this->expectedDefinitions));
$plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, $module_handler, $alter_hook_name);
$this->assertEquals($this->expectedDefinitions, $plugin_manager->getDefinitions());
$this->assertEquals($this->expectedDefinitions['banana'], $plugin_manager->getDefinition('banana'));
}
/**
* Tests the plugin manager with caching and altering.
*/
public function testDefaultPluginManagerWithEmptyCache() {
$cid = $this->randomName();
$cache_backend = $this->getMockBuilder('Drupal\Core\Cache\MemoryBackend')
->disableOriginalConstructor()
->getMock();
$cache_backend
->expects($this->once())
->method('get')
->with($cid . ':en')
->will($this->returnValue(FALSE));
$cache_backend
->expects($this->once())
->method('set')
->with($cid . ':en', $this->expectedDefinitions);
$language = new Language(array('id' => 'en'));
$language_manager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
$language_manager->expects($this->once())
->method('getCurrentLanguage')
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
->with(Language::TYPE_INTERFACE)
->will($this->returnValue($language));
$plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions);
$plugin_manager->setCacheBackend($cache_backend, $language_manager, $cid);
$this->assertEquals($this->expectedDefinitions, $plugin_manager->getDefinitions());
$this->assertEquals($this->expectedDefinitions['banana'], $plugin_manager->getDefinition('banana'));
}
/**
* Tests the plugin manager with caching and altering.
*/
public function testDefaultPluginManagerWithFilledCache() {
$cid = $this->randomName();
$cache_backend = $this->getMockBuilder('Drupal\Core\Cache\MemoryBackend')
->disableOriginalConstructor()
->getMock();
$cache_backend
->expects($this->once())
->method('get')
->with($cid . ':en')
->will($this->returnValue((object) array('data' => $this->expectedDefinitions)));
$cache_backend
->expects($this->never())
->method('set');
$language = new Language(array('id' => 'en'));
$language_manager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
$language_manager->expects($this->once())
->method('getCurrentLanguage')
->with(Language::TYPE_INTERFACE)
->will($this->returnValue($language));
$plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions);
$plugin_manager->setCacheBackend($cache_backend, $language_manager, $cid);
$this->assertEquals($this->expectedDefinitions, $plugin_manager->getDefinitions());
}
Alex Pott
committed
/**
* Tests the plugin manager cache clear with tags.
*/
public function testCacheClearWithTags() {
$cid = $this->randomName();
$cache_backend = $this->getMockBuilder('Drupal\Core\Cache\MemoryBackend')
->disableOriginalConstructor()
->getMock();
$cache_backend
->expects($this->once())
->method('deleteTags')
->with(array('tag' => TRUE));
$cache_backend
->expects($this->never())
->method('deleteMultiple');
$this->getContainerWithCacheBins($cache_backend);
Alex Pott
committed
$language = new Language(array('id' => 'en'));
$language_manager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
Alex Pott
committed
$language_manager->expects($this->once())
->method('getCurrentLanguage')
Alex Pott
committed
->with(Language::TYPE_INTERFACE)
->will($this->returnValue($language));
$plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions);
$plugin_manager->setCacheBackend($cache_backend, $language_manager, $cid, array('tag' => TRUE));
$plugin_manager->clearCachedDefinitions();
}
if (!defined('DRUPAL_ROOT')) {
define('DRUPAL_ROOT', dirname(dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__)))));
}