EntityDisplayTraitTest.php in CCK Select Other 8
File
tests/src/Unit/EntityDisplayTraitTest.php
View source
<?php
namespace Drupal\Tests\cck_select_other\Unit;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Tests\UnitTestCase;
class EntityDisplayTraitTest extends UnitTestCase {
protected $mock;
protected $entityManager;
protected function setUp() {
$this->mock = $this
->getMockForTrait('Drupal\\cck_select_other\\EntityDisplayTrait');
$this->entityManager = $this
->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeManagerInterface')
->disableOriginalConstructor()
->getMock();
$container = new ContainerBuilder();
$container
->set('entity_type.manager', $this->entityManager);
\Drupal::setContainer($container);
}
public function testHasSelectOtherWidget($plugin_id, $expected) {
$fieldSettings = $this
->getMockBuilder('\\Drupal\\Core\\Field\\PluginSettingsInterface')
->disableOriginalConstructor()
->getMock();
$fieldSettings
->expects($this
->any())
->method('getPluginId')
->willReturn($plugin_id);
$entityDisplay = $this
->getMockBuilder('\\Drupal\\Core\\Entity\\Display\\EntityDisplayInterface')
->disableOriginalConstructor()
->getMock();
$entityDisplay
->expects($this
->any())
->method('getRenderer')
->with('field_list')
->willReturn($fieldSettings);
$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,
]);
$this->entityManager
->expects($this
->any())
->method('getStorage')
->with('entity_form_display')
->willReturn($entityStorage);
$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));
}
public function hasSelectOtherWidgetProvider() {
return [
[
'cck_select_other',
TRUE,
],
[
'textfield',
FALSE,
],
];
}
public function testGetWidgetSettings($plugin_id, array $expected) {
$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);
$entityDisplay = $this
->getMockBuilder('\\Drupal\\Core\\Entity\\Display\\EntityDisplayInterface')
->disableOriginalConstructor()
->getMock();
$entityDisplay
->expects($this
->any())
->method('getRenderer')
->with('field_list')
->willReturn($fieldSettings);
$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,
]);
$this->entityManager
->expects($this
->any())
->method('getStorage')
->with('entity_form_display')
->willReturn($entityStorage);
$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));
}
public function getWidgetSettingsProvider() {
return [
[
'cck_select_other',
[
'other_label' => 'Other',
],
],
[
'textfield',
[],
],
];
}
}