class EntityDisplayTraitTest in CCK Select Other 8
Tests the entity display trait.
Note that prophecy cannot be used to mock because there is a conflict between the base mockForTrait method and prophecy. PHPUnitWTF.
@group cck_select_other
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\cck_select_other\Unit\EntityDisplayTraitTest
Expanded class hierarchy of EntityDisplayTraitTest
File
- tests/
src/ Unit/ EntityDisplayTraitTest.php, line 16
Namespace
Drupal\Tests\cck_select_other\UnitView source
class EntityDisplayTraitTest extends UnitTestCase {
protected $mock;
/**
* The entity manager mock.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityManager;
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->mock = $this
->getMockForTrait('Drupal\\cck_select_other\\EntityDisplayTrait');
// Mock the entity manager.
$this->entityManager = $this
->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeManagerInterface')
->disableOriginalConstructor()
->getMock();
$container = new ContainerBuilder();
$container
->set('entity_type.manager', $this->entityManager);
\Drupal::setContainer($container);
}
/**
* Assert that select other widget is detected on form displays.
*
* @param string $plugin_id
* The plugin ID.
* @param bool $expected
* The expected result.
*
* @dataProvider hasSelectOtherWidgetProvider
*/
public function testHasSelectOtherWidget($plugin_id, $expected) {
// Mock Field plugin settings.
$fieldSettings = $this
->getMockBuilder('\\Drupal\\Core\\Field\\PluginSettingsInterface')
->disableOriginalConstructor()
->getMock();
$fieldSettings
->expects($this
->any())
->method('getPluginId')
->willReturn($plugin_id);
// Mock Entity Form Display.
$entityDisplay = $this
->getMockBuilder('\\Drupal\\Core\\Entity\\Display\\EntityDisplayInterface')
->disableOriginalConstructor()
->getMock();
$entityDisplay
->expects($this
->any())
->method('getRenderer')
->with('field_list')
->willReturn($fieldSettings);
// Mock the entity storage interface.
$entityStorage = $this
->getMockBuilder('\\Drupal\\Core\\Entity\\EntityStorageInterface')
->disableOriginalConstructor()
->getMock();
$entityStorage
->expects($this
->any())
->method('loadByProperties')
->with([
'targetEntityType' => 'entity_test',
])
->willReturn([
'entity_test.display.mock' => $entityDisplay,
]);
// Mock entity manager methods.
$this->entityManager
->expects($this
->any())
->method('getStorage')
->with('entity_form_display')
->willReturn($entityStorage);
// Mock the definition.
$definition = $this
->getMockBuilder('\\Drupal\\Core\\Field\\FieldDefinitionInterface')
->disableOriginalConstructor()
->getMock();
$definition
->expects($this
->any())
->method('getTargetEntityTypeId')
->willReturn('entity_test');
$definition
->expects($this
->any())
->method('getName')
->willReturn('field_list');
$this
->assertEquals($expected, $this->mock
->hasSelectOtherWidget($definition));
}
/**
* Provide parameters for testHasSelectOtherWidget().
*
* @return array
* An array of test parameters.
*/
public function hasSelectOtherWidgetProvider() {
return [
[
'cck_select_other',
TRUE,
],
[
'textfield',
FALSE,
],
];
}
/**
* Asserts that select other widget settings are returned or not.
*
* @param string $plugin_id
* The plugin ID.
* @param array $expected
* The expected result.
*
* @dataProvider getWidgetSettingsProvider().
*/
public function testGetWidgetSettings($plugin_id, array $expected) {
// Mock Field plugin settings.
$fieldSettings = $this
->getMockBuilder('\\Drupal\\Core\\Field\\PluginSettingsInterface')
->disableOriginalConstructor()
->getMock();
$fieldSettings
->expects($this
->any())
->method('getPluginId')
->willReturn($plugin_id);
$fieldSettings
->expects($this
->any())
->method('getSettings')
->willReturn($expected);
// Mock Entity Form Display.
$entityDisplay = $this
->getMockBuilder('\\Drupal\\Core\\Entity\\Display\\EntityDisplayInterface')
->disableOriginalConstructor()
->getMock();
$entityDisplay
->expects($this
->any())
->method('getRenderer')
->with('field_list')
->willReturn($fieldSettings);
// Mock the entity storage interface.
$entityStorage = $this
->getMockBuilder('\\Drupal\\Core\\Entity\\EntityStorageInterface')
->disableOriginalConstructor()
->getMock();
$entityStorage
->expects($this
->any())
->method('loadByProperties')
->with([
'targetEntityType' => 'entity_test',
])
->willReturn([
'entity_test.display.mock' => $entityDisplay,
]);
// Mock entity manager methods.
$this->entityManager
->expects($this
->any())
->method('getStorage')
->with('entity_form_display')
->willReturn($entityStorage);
// Mock the definition.
$definition = $this
->getMockBuilder('\\Drupal\\Core\\Field\\FieldDefinitionInterface')
->disableOriginalConstructor()
->getMock();
$definition
->expects($this
->any())
->method('getTargetEntityTypeId')
->willReturn('entity_test');
$definition
->expects($this
->any())
->method('getName')
->willReturn('field_list');
$this
->assertEquals($expected, $this->mock
->getWidgetSettings($definition));
}
/**
* Get test parameters for testGetWidgetSettings().
*
* @return array
* Test parameters.
*/
public function getWidgetSettingsProvider() {
return [
[
'cck_select_other',
[
'other_label' => 'Other',
],
],
[
'textfield',
[],
],
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityDisplayTraitTest:: |
protected | property | The entity manager mock. | |
EntityDisplayTraitTest:: |
protected | property | ||
EntityDisplayTraitTest:: |
public | function | Get test parameters for testGetWidgetSettings(). | |
EntityDisplayTraitTest:: |
public | function | Provide parameters for testHasSelectOtherWidget(). | |
EntityDisplayTraitTest:: |
protected | function |
Overrides UnitTestCase:: |
|
EntityDisplayTraitTest:: |
public | function | Asserts that select other widget settings are returned or not. | |
EntityDisplayTraitTest:: |
public | function | Assert that select other widget is detected on form displays. | |
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. |