Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
drupal
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
drupal
Commits
b996ff0c
Commit
b996ff0c
authored
Oct 13, 2013
by
catch
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#2074407
by dawehner, pwolanin: Write tests for hook-based plugin discovery.
parent
8be763a6
No related branches found
No related tags found
2 merge requests
!7452
Issue #1797438. HTML5 validation is preventing form submit and not fully...
,
!789
Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
core/lib/Drupal/Core/Plugin/Discovery/HookDiscovery.php
+15
-4
15 additions, 4 deletions
core/lib/Drupal/Core/Plugin/Discovery/HookDiscovery.php
core/tests/Drupal/Tests/Core/Plugin/Discovery/HookDiscoveryTest.php
+150
-0
150 additions, 0 deletions
.../Drupal/Tests/Core/Plugin/Discovery/HookDiscoveryTest.php
with
165 additions
and
4 deletions
core/lib/Drupal/Core/Plugin/Discovery/HookDiscovery.php
+
15
−
4
View file @
b996ff0c
...
@@ -8,6 +8,7 @@
...
@@ -8,6 +8,7 @@
namespace
Drupal\Core\Plugin\Discovery
;
namespace
Drupal\Core\Plugin\Discovery
;
use
Drupal\Component\Plugin\Discovery\DiscoveryInterface
;
use
Drupal\Component\Plugin\Discovery\DiscoveryInterface
;
use
Drupal\Core\Extension\ModuleHandlerInterface
;
/**
/**
* Provides a hook-based plugin discovery class.
* Provides a hook-based plugin discovery class.
...
@@ -21,14 +22,24 @@ class HookDiscovery implements DiscoveryInterface {
...
@@ -21,14 +22,24 @@ class HookDiscovery implements DiscoveryInterface {
*/
*/
protected
$hook
;
protected
$hook
;
/**
* The module handler used to find and execute the plugin hook.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected
$moduleHandler
;
/**
/**
* Constructs a Drupal\Core\Plugin\Discovery\HookDiscovery object.
* Constructs a Drupal\Core\Plugin\Discovery\HookDiscovery object.
*
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param string $hook
* @param string $hook
* The Drupal hook that a module can implement in order to interface to
* The Drupal hook that a module can implement in order to interface to
* this discovery class.
* this discovery class.
*/
*/
function
__construct
(
$hook
)
{
function
__construct
(
ModuleHandlerInterface
$module_handler
,
$hook
)
{
$this
->
moduleHandler
=
$module_handler
;
$this
->
hook
=
$hook
;
$this
->
hook
=
$hook
;
}
}
...
@@ -45,9 +56,9 @@ public function getDefinition($plugin_id) {
...
@@ -45,9 +56,9 @@ public function getDefinition($plugin_id) {
*/
*/
public
function
getDefinitions
()
{
public
function
getDefinitions
()
{
$definitions
=
array
();
$definitions
=
array
();
foreach
(
\Drupal
::
moduleHandler
()
->
getImplementations
(
$this
->
hook
)
as
$module
)
{
foreach
(
$this
->
moduleHandler
->
getImplementations
(
$this
->
hook
)
as
$module
)
{
$
function
=
$module
.
'_'
.
$this
->
hook
;
$
result
=
$this
->
moduleHandler
->
invoke
(
$module
,
$this
->
hook
)
;
foreach
(
$
function
()
as
$plugin_id
=>
$definition
)
{
foreach
(
$
result
as
$plugin_id
=>
$definition
)
{
$definition
[
'module'
]
=
$module
;
$definition
[
'module'
]
=
$module
;
$definitions
[
$plugin_id
]
=
$definition
;
$definitions
[
$plugin_id
]
=
$definition
;
}
}
...
...
This diff is collapsed.
Click to expand it.
core/tests/Drupal/Tests/Core/Plugin/Discovery/HookDiscoveryTest.php
0 → 100644
+
150
−
0
View file @
b996ff0c
<?php
/**
* @file
* Contains \Drupal\Tests\Core\Plugin\Discovery\HookDiscoveryTest.
*/
namespace
Drupal\Tests\Core\Plugin\Discovery
{
use
Drupal\Core\Plugin\Discovery\HookDiscovery
;
use
Drupal\Tests\UnitTestCase
;
/**
* Tests the hook plugin discovery class.
*
* @group Drupal
* @group Plugin
*
* @see \Drupal\Core\Plugin\Discovery\HookDiscovery
*/
class
HookDiscoveryTest
extends
UnitTestCase
{
/**
* The mocked module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected
$moduleHandler
;
/**
* The tested hook discovery.
*
* @var \Drupal\Core\Plugin\Discovery\HookDiscovery
*/
protected
$hookDiscovery
;
public
static
function
getInfo
()
{
return
array
(
'name'
=>
'Hook Discovery'
,
'description'
=>
'Tests the hook plugin discovery class.'
,
'group'
=>
'Plugin'
,
);
}
/**
* {@inheritdoc}
*/
protected
function
setUp
()
{
$this
->
moduleHandler
=
$this
->
getMock
(
'Drupal\Core\Extension\ModuleHandlerInterface'
);
$this
->
hookDiscovery
=
new
HookDiscovery
(
$this
->
moduleHandler
,
'test_plugin'
);
}
/**
* Tests the getDefinitions() method without any plugins.
*
* @see \Drupal\Core\Plugin\Discovery::getDefinitions()
*/
public
function
testGetDefinitionsWithoutPlugins
()
{
$this
->
moduleHandler
->
expects
(
$this
->
once
())
->
method
(
'getImplementations'
)
->
with
(
'test_plugin'
)
->
will
(
$this
->
returnValue
(
array
()));
$this
->
assertCount
(
0
,
$this
->
hookDiscovery
->
getDefinitions
());
}
/**
* Tests the getDefinitions() method with some plugins.
*
* @see \Drupal\Core\Plugin\Discovery::getDefinitions()
*/
public
function
testGetDefinitions
()
{
$this
->
moduleHandler
->
expects
(
$this
->
once
())
->
method
(
'getImplementations'
)
->
with
(
'test_plugin'
)
->
will
(
$this
->
returnValue
(
array
(
'hook_discovery_test'
,
'hook_discovery_test2'
)));
$this
->
moduleHandler
->
expects
(
$this
->
at
(
1
))
->
method
(
'invoke'
)
->
with
(
'hook_discovery_test'
,
'test_plugin'
)
->
will
(
$this
->
returnValue
(
hook_discovery_test_test_plugin
()));
$this
->
moduleHandler
->
expects
(
$this
->
at
(
2
))
->
method
(
'invoke'
)
->
with
(
'hook_discovery_test2'
,
'test_plugin'
)
->
will
(
$this
->
returnValue
(
hook_discovery_test2_test_plugin
()));
$definitions
=
$this
->
hookDiscovery
->
getDefinitions
();
$this
->
assertCount
(
3
,
$definitions
);
$this
->
assertEquals
(
$definitions
[
'test_id_1'
][
'class'
],
'Drupal\plugin_test\Plugin\plugin_test\fruit\Apple'
);
$this
->
assertEquals
(
$definitions
[
'test_id_2'
][
'class'
],
'Drupal\plugin_test\Plugin\plugin_test\fruit\Orange'
);
$this
->
assertEquals
(
$definitions
[
'test_id_3'
][
'class'
],
'Drupal\plugin_test\Plugin\plugin_test\fruit\Cherry'
);
// Ensure that the module was set.
$this
->
assertEquals
(
$definitions
[
'test_id_1'
][
'module'
],
'hook_discovery_test'
);
$this
->
assertEquals
(
$definitions
[
'test_id_2'
][
'module'
],
'hook_discovery_test'
);
$this
->
assertEquals
(
$definitions
[
'test_id_3'
][
'module'
],
'hook_discovery_test2'
);
}
/**
* Tests the getDefinition method with some plugins.
*
* @see \Drupal\Core\Plugin\Discovery::getDefinition()
*/
public
function
testGetDefinition
()
{
$this
->
moduleHandler
->
expects
(
$this
->
exactly
(
4
))
->
method
(
'getImplementations'
)
->
with
(
'test_plugin'
)
->
will
(
$this
->
returnValue
(
array
(
'hook_discovery_test'
,
'hook_discovery_test2'
)));
$this
->
moduleHandler
->
expects
(
$this
->
any
())
->
method
(
'invoke'
)
->
will
(
$this
->
returnValueMap
(
array
(
array
(
'hook_discovery_test'
,
'test_plugin'
,
array
(),
hook_discovery_test_test_plugin
()),
array
(
'hook_discovery_test2'
,
'test_plugin'
,
array
(),
hook_discovery_test2_test_plugin
()),
)
));
$this
->
assertNull
(
$this
->
hookDiscovery
->
getDefinition
(
'test_non_existant'
));
$plugin_definition
=
$this
->
hookDiscovery
->
getDefinition
(
'test_id_1'
);
$this
->
assertEquals
(
$plugin_definition
[
'class'
],
'Drupal\plugin_test\Plugin\plugin_test\fruit\Apple'
);
$this
->
assertEquals
(
$plugin_definition
[
'module'
],
'hook_discovery_test'
);
$plugin_definition
=
$this
->
hookDiscovery
->
getDefinition
(
'test_id_2'
);
$this
->
assertEquals
(
$plugin_definition
[
'class'
],
'Drupal\plugin_test\Plugin\plugin_test\fruit\Orange'
);
$this
->
assertEquals
(
$plugin_definition
[
'module'
],
'hook_discovery_test'
);
$plugin_definition
=
$this
->
hookDiscovery
->
getDefinition
(
'test_id_3'
);
$this
->
assertEquals
(
$plugin_definition
[
'class'
],
'Drupal\plugin_test\Plugin\plugin_test\fruit\Cherry'
);
$this
->
assertEquals
(
$plugin_definition
[
'module'
],
'hook_discovery_test2'
);
}
}
}
namespace
{
function
hook_discovery_test_test_plugin
()
{
return
array
(
'test_id_1'
=>
array
(
'class'
=>
'Drupal\plugin_test\Plugin\plugin_test\fruit\Apple'
),
'test_id_2'
=>
array
(
'class'
=>
'Drupal\plugin_test\Plugin\plugin_test\fruit\Orange'
),
);
}
function
hook_discovery_test2_test_plugin
()
{
return
array
(
'test_id_3'
=>
array
(
'class'
=>
'Drupal\plugin_test\Plugin\plugin_test\fruit\Cherry'
),
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment