class ExtraFieldDisplayManagerTest in Extra Field 8.2
@coversDefaultClass \Drupal\extra_field\Plugin\ExtraFieldDisplayManager
@group extra_field
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\extra_field\Unit\ExtraFieldDisplayManagerTest
Expanded class hierarchy of ExtraFieldDisplayManagerTest
File
- tests/
src/ Unit/ ExtraFieldDisplayManagerTest.php, line 12
Namespace
Drupal\Tests\extra_field\UnitView source
class ExtraFieldDisplayManagerTest extends UnitTestCase {
/**
* The plugin manager under test.
*
* @var \Drupal\extra_field\Plugin\ExtraFieldDisplayManager|\PHPUnit\Framework\MockObject\MockObject
*/
protected $displayManager;
/**
* Setup the SuT but allow selected methods to be overridden.
*
* @param string[] $methods
* The methods to override for test purpose.
*/
protected function prepareDisplayManager(array $methods) {
$this->displayManager = $this
->getMockBuilder('Drupal\\extra_field\\Plugin\\ExtraFieldDisplayManager')
->disableOriginalConstructor()
->setMethods($methods)
->getMock();
}
/**
* Prepare ::getDefinitions to return the right values.
*
* @param array $definitions
* The plugin definitions to return.
*/
protected function prepareDefinitions(array $definitions) {
$this->displayManager
->expects($this
->any())
->method('getDefinitions')
->will($this
->returnValue($definitions));
}
/**
* Prepare ::allEntityBundles to return the right values.
*
* @param array $bundlesMap
* Array of bundle names.
*/
protected function prepareEntityBundles(array $bundlesMap) {
$this->displayManager
->expects($this
->any())
->method('allEntityBundles')
->will($this
->returnValueMap($bundlesMap));
}
/**
* Prepare ::allEntityBundleKey to return the right values.
*
* @param string $key
* The key to return.
*/
protected function prepareEntityBundleKey($key) {
$this->displayManager
->expects($this
->any())
->method('entityBundleKey')
->will($this
->returnValue($key));
}
/**
* Returns a mocked ExtraFieldDisplay object.
*
* @return \Drupal\extra_field\Plugin\ExtraFieldDisplayInterface|\PHPUnit\Framework\MockObject\MockObject
* The mocked object.
*/
protected function createMockDisplayPlugin($entity, $display, $viewMode, $build) {
$plugin = $this
->createMock('\\Drupal\\extra_field\\Plugin\\ExtraFieldDisplayInterface');
$plugin
->expects($this
->any())
->method('setEntity')
->with($entity);
$plugin
->expects($this
->any())
->method('setEntityViewDisplay')
->with($display);
$plugin
->expects($this
->any())
->method('setViewMode')
->with($viewMode);
$plugin
->expects($this
->any())
->method('view')
->will($this
->returnValue($build));
return $plugin;
}
/**
* @covers ::fieldInfo
*
* @dataProvider fieldInfoProvider
*
* @param array $definitions
* Plugin definitions as returned by ::getDefinitions.
* @param array $bundles
* Entity bundles as returned by ::allEntityBundles.
* @param array $results
* Field info as returned by ::fieldInfo.
*/
public function testFieldInfo(array $definitions, array $bundles, array $results) {
$this
->prepareDisplayManager([
'getDefinitions',
'allEntityBundles',
]);
$this
->prepareDefinitions($definitions);
$this
->prepareEntityBundles($bundles);
$this
->assertEquals(count($definitions), count($this->displayManager
->getDefinitions()));
$this
->assertEquals($results, $this->displayManager
->fieldInfo());
}
/**
* Data provider for testFieldInfo().
*/
public function fieldInfoProvider() {
$info[] = [
// Definitions.
[
'test' => [
'id' => 'test',
'bundles' => [
'node.article',
],
'label' => 'test display node article',
'description' => 'test description display node article',
'weight' => 0,
'visible' => FALSE,
],
],
// Bundles.
[],
// Results.
[
'node' => [
'article' => [
'display' => [
'extra_field_test' => [
'label' => 'test display node article',
'description' => 'test description display node article',
'weight' => 0,
'visible' => FALSE,
],
],
],
],
],
];
$info[] = [
// Definitions.
[
'test' => [
'id' => 'test',
'bundles' => [
'node.article',
],
'label' => 'test display node article',
'description' => 'test description display node article',
'weight' => 88,
'visible' => TRUE,
],
],
// Bundles.
[],
// Results.
[
'node' => [
'article' => [
'display' => [
'extra_field_test' => [
'label' => 'test display node article',
'description' => 'test description display node article',
'weight' => 88,
'visible' => TRUE,
],
],
],
],
],
];
$info[] = [
// Definitions.
[
'test1' => [
'id' => 'test1',
'bundles' => [
'node.*',
'voucher.*',
],
'label' => 'test display 1',
'description' => 'test description display 1',
'weight' => 0,
'visible' => FALSE,
],
'test2' => [
'id' => 'test2',
'bundles' => [
'node.article',
],
'label' => 'test display 2',
'description' => 'test description display 2',
'weight' => 2,
'visible' => TRUE,
],
],
// Bundles.
[
[
'node',
[
'article',
'story',
'blog',
],
],
[
'voucher',
[
'simple',
'extended',
],
],
],
// Results.
[
'node' => [
'article' => [
'display' => [
'extra_field_test1' => [
'label' => 'test display 1',
'description' => 'test description display 1',
'weight' => 0,
'visible' => FALSE,
],
'extra_field_test2' => [
'label' => 'test display 2',
'description' => 'test description display 2',
'weight' => 2,
'visible' => TRUE,
],
],
],
'story' => [
'display' => [
'extra_field_test1' => [
'label' => 'test display 1',
'description' => 'test description display 1',
'weight' => 0,
'visible' => FALSE,
],
],
],
'blog' => [
'display' => [
'extra_field_test1' => [
'label' => 'test display 1',
'description' => 'test description display 1',
'weight' => 0,
'visible' => FALSE,
],
],
],
],
'voucher' => [
'simple' => [
'display' => [
'extra_field_test1' => [
'label' => 'test display 1',
'description' => 'test description display 1',
'weight' => 0,
'visible' => FALSE,
],
],
],
'extended' => [
'display' => [
'extra_field_test1' => [
'label' => 'test display 1',
'description' => 'test description display 1',
'weight' => 0,
'visible' => FALSE,
],
],
],
],
],
];
return $info;
}
/**
* @covers ::entityView
*
* @dataProvider entityViewProvider
*
* @param array $definitions
* The plugin definitions as returned by ::getDefinitions().
* @param string $entityType
* The content entity type.
* @param string $bundle
* The content entity bundle.
* @param bool $hasComponent
* The has component flag as returned by
* EntityDisplayInterface::getComponent().
* @param array $result
* The ::entityView result.
*/
public function testEntityView(array $definitions, $entityType, $bundle, $hasComponent, array $result) {
$this
->prepareDisplayManager([
'getDefinitions',
'getFactory',
]);
$this
->prepareDefinitions($definitions);
// Mock content entity.
/** @var \Drupal\Core\Entity\ContentEntityInterface|\PHPUnit\Framework\MockObject\MockObject $contentEntity */
$contentEntity = $this
->createMock('Drupal\\Core\\Entity\\ContentEntityInterface');
$contentEntity
->expects($this
->any())
->method('getEntityTypeId')
->will($this
->returnValue($entityType));
$contentEntity
->expects($this
->any())
->method('bundle')
->will($this
->returnValue($bundle));
// Mock entity view display.
/** @var \Drupal\Core\Entity\Entity\EntityViewDisplay|\PHPUnit\Framework\MockObject\MockObject $display */
$display = $this
->createMock('Drupal\\Core\\Entity\\Entity\\EntityViewDisplay');
$display
->expects($this
->any())
->method('getComponent')
->will($this
->returnValue($hasComponent));
// Mock extra field display plugin.
/** @var \Drupal\Component\Plugin\Factory\FactoryInterface|\PHPUnit\Framework\MockObject\MockObject $pluginFactory */
$pluginFactory = $this
->createMock('Drupal\\Component\\Plugin\\Factory\\FactoryInterface');
$pluginFactory
->expects($this
->any())
->method('createInstance')
->will($this
->returnValue($this
->createMockDisplayPlugin($contentEntity, $display, 'view_mode', [
'mock' => 'display markup',
])));
$this->displayManager
->expects($this
->any())
->method('getFactory')
->will($this
->returnValue($pluginFactory));
$build = [];
$this->displayManager
->entityView($build, $contentEntity, $display, 'view_mode');
$this
->assertEquals($result, $build);
}
/**
* Data provider for testFieldInfo().
*/
public function entityViewProvider() {
$info[] = [
// Definitions.
[],
// Entity type.
'',
// Bundle.
'',
// Has component.
FALSE,
// Result.
[],
];
$info[] = [
// Definitions.
[
'plugin_id' => [
'bundles' => [],
],
],
// Entity type.
'entity_type',
// Bundle.
'bundle',
// Has component.
FALSE,
// Result.
[],
];
$info[] = [
// Definitions.
[
'plugin_id' => [
'bundles' => [],
],
],
// Entity type.
'entity_type',
// Bundle.
'bundle',
// Has component.
FALSE,
// Result.
[],
];
$info[] = [
// Definitions.
[
'test' => [
'id' => 'test',
'bundles' => [
'node.*',
],
'label' => 'test display node article',
'weight' => 0,
'visible' => TRUE,
],
],
// Entity type.
'node',
// Bundle.
'article',
// Has component.
TRUE,
// Result.
[
'extra_field_test' => [
'mock' => 'display markup',
],
],
];
$info[] = [
// Definitions.
[
'first_test' => [
'id' => 'test',
'bundles' => [
'node.article',
],
'label' => 'test display node article',
'weight' => 0,
'visible' => TRUE,
],
'second_test' => [
'id' => 'test',
'bundles' => [
'node.page',
],
'label' => 'test display node page',
'weight' => 0,
'visible' => TRUE,
],
],
// Entity type.
'node',
// Bundle.
'page',
// Has component.
TRUE,
// Result.
[
'extra_field_second_test' => [
'mock' => 'display markup',
],
],
];
return $info;
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ExtraFieldDisplayManagerTest:: |
protected | property | The plugin manager under test. | |
ExtraFieldDisplayManagerTest:: |
protected | function | Returns a mocked ExtraFieldDisplay object. | |
ExtraFieldDisplayManagerTest:: |
public | function | Data provider for testFieldInfo(). | |
ExtraFieldDisplayManagerTest:: |
public | function | Data provider for testFieldInfo(). | |
ExtraFieldDisplayManagerTest:: |
protected | function | Prepare ::getDefinitions to return the right values. | |
ExtraFieldDisplayManagerTest:: |
protected | function | Setup the SuT but allow selected methods to be overridden. | |
ExtraFieldDisplayManagerTest:: |
protected | function | Prepare ::allEntityBundleKey to return the right values. | |
ExtraFieldDisplayManagerTest:: |
protected | function | Prepare ::allEntityBundles to return the right values. | |
ExtraFieldDisplayManagerTest:: |
public | function | @covers ::entityView | |
ExtraFieldDisplayManagerTest:: |
public | function | @covers ::fieldInfo | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 340 |