View source
<?php
namespace Drupal\Tests\config_translation\Unit;
use Drupal\config_translation\ConfigEntityMapper;
use Drupal\Core\Url;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\Routing\Route;
class ConfigEntityMapperTest extends UnitTestCase {
protected $configEntityMapper;
protected $entityTypeManager;
protected $entity;
protected $routeProvider;
protected $languageManager;
protected $eventDispatcher;
protected function setUp() : void {
$this->entityTypeManager = $this
->createMock('Drupal\\Core\\Entity\\EntityTypeManagerInterface');
$this->entity = $this
->createMock('Drupal\\Core\\Config\\Entity\\ConfigEntityInterface');
$this->routeProvider = $this
->createMock('Drupal\\Core\\Routing\\RouteProviderInterface');
$this->routeProvider
->expects($this
->any())
->method('getRouteByName')
->with('entity.configurable_language.edit_form')
->will($this
->returnValue(new Route('/admin/config/regional/language/edit/{configurable_language}')));
$definition = [
'class' => '\\Drupal\\config_translation\\ConfigEntityMapper',
'base_route_name' => 'entity.configurable_language.edit_form',
'title' => '@label language',
'names' => [],
'entity_type' => 'configurable_language',
'route_name' => 'config_translation.item.overview.entity.configurable_language.edit_form',
];
$typed_config_manager = $this
->createMock('Drupal\\Core\\Config\\TypedConfigManagerInterface');
$locale_config_manager = $this
->getMockBuilder('Drupal\\locale\\LocaleConfigManager')
->disableOriginalConstructor()
->getMock();
$this->languageManager = $this
->createMock('Drupal\\Core\\Language\\LanguageManagerInterface');
$this->eventDispatcher = $this
->createMock('Symfony\\Contracts\\EventDispatcher\\EventDispatcherInterface');
$this->configEntityMapper = new ConfigEntityMapper('configurable_language', $definition, $this
->getConfigFactoryStub(), $typed_config_manager, $locale_config_manager, $this
->createMock('Drupal\\config_translation\\ConfigMapperManagerInterface'), $this->routeProvider, $this
->getStringTranslationStub(), $this->entityTypeManager, $this->languageManager, $this->eventDispatcher);
}
public function testEntityGetterAndSetter() {
$this->entity
->expects($this
->once())
->method('id')
->with()
->will($this
->returnValue('entity_id'));
$entity_type = $this
->createMock('Drupal\\Core\\Config\\Entity\\ConfigEntityTypeInterface');
$entity_type
->expects($this
->any())
->method('getConfigPrefix')
->will($this
->returnValue('config_prefix'));
$this->entityTypeManager
->expects($this
->once())
->method('getDefinition')
->with('configurable_language')
->will($this
->returnValue($entity_type));
$this
->assertNull($this->configEntityMapper
->getEntity());
$result = $this->configEntityMapper
->setEntity($this->entity);
$this
->assertTrue($result);
$this
->assertEquals($this->entity, $this->configEntityMapper
->getEntity());
$plugin_definition = $this->configEntityMapper
->getPluginDefinition();
$this
->assertContains('config_prefix.entity_id', $plugin_definition['names']);
$result = $this->configEntityMapper
->setEntity($this->entity);
$this
->assertFalse($result);
}
public function testGetOverviewRouteParameters() {
$entity_type = $this
->createMock('Drupal\\Core\\Config\\Entity\\ConfigEntityTypeInterface');
$this->entityTypeManager
->expects($this
->once())
->method('getDefinition')
->with('configurable_language')
->will($this
->returnValue($entity_type));
$this->configEntityMapper
->setEntity($this->entity);
$this->entity
->expects($this
->once())
->method('id')
->with()
->will($this
->returnValue('entity_id'));
$result = $this->configEntityMapper
->getOverviewRouteParameters();
$this
->assertSame([
'configurable_language' => 'entity_id',
], $result);
}
public function testGetType() {
$result = $this->configEntityMapper
->getType();
$this
->assertSame('configurable_language', $result);
}
public function testGetTypeName() {
$entity_type = $this
->createMock('Drupal\\Core\\Config\\Entity\\ConfigEntityTypeInterface');
$entity_type
->expects($this
->once())
->method('getLabel')
->will($this
->returnValue('test'));
$this->entityTypeManager
->expects($this
->once())
->method('getDefinition')
->with('configurable_language')
->will($this
->returnValue($entity_type));
$result = $this->configEntityMapper
->getTypeName();
$this
->assertSame('test', $result);
}
public function testGetTypeLabel() {
$entity_type = $this
->createMock('Drupal\\Core\\Config\\Entity\\ConfigEntityTypeInterface');
$entity_type
->expects($this
->once())
->method('getLabel')
->will($this
->returnValue('test'));
$this->entityTypeManager
->expects($this
->once())
->method('getDefinition')
->with('configurable_language')
->will($this
->returnValue($entity_type));
$result = $this->configEntityMapper
->getTypeLabel();
$this
->assertSame('test', $result);
}
public function testGetOperations() {
$result = $this->configEntityMapper
->getOperations();
$expected = [
'list' => [
'title' => 'List',
'url' => Url::fromRoute('config_translation.entity_list', [
'mapper_id' => 'configurable_language',
]),
],
];
$this
->assertEquals($expected, $result);
}
}