3 namespace Drupal\Tests\config_translation\Unit;
5 use Drupal\config_translation\ConfigFieldMapper;
6 use Drupal\Tests\UnitTestCase;
9 * Tests the functionality provided by the configuration field mapper.
11 * @group config_translation
13 * @coversDefaultClass \Drupal\config_translation\ConfigFieldMapper
15 class ConfigFieldMapperTest extends UnitTestCase {
18 * The configuration field mapper to test.
20 * @var \Drupal\config_translation\ConfigFieldMapper
22 protected $configFieldMapper;
25 * The field config instance used for testing.
27 * @var \Drupal\field\FieldConfigInterface|\PHPUnit_Framework_MockObject_MockObject
32 * The entity manager used for testing.
34 * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
36 protected $entityManager;
39 * The mocked event dispatcher.
41 * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject
43 protected $eventDispatcher;
48 protected function setUp() {
49 $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
50 $this->entity = $this->getMock('Drupal\field\FieldConfigInterface');
53 'class' => '\Drupal\config_translation\ConfigFieldMapper',
54 'base_route_name' => 'entity.field_config.node_field_edit_form',
55 'title' => '@label field',
57 'entity_type' => 'field_config',
60 $locale_config_manager = $this->getMockBuilder('Drupal\locale\LocaleConfigManager')
61 ->disableOriginalConstructor()
64 $this->eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
66 $this->configFieldMapper = new ConfigFieldMapper(
69 $this->getConfigFactoryStub(),
70 $this->getMock('Drupal\Core\Config\TypedConfigManagerInterface'),
71 $locale_config_manager,
72 $this->getMock('Drupal\config_translation\ConfigMapperManagerInterface'),
73 $this->getMock('Drupal\Core\Routing\RouteProviderInterface'),
74 $this->getStringTranslationStub(),
76 $this->getMock('Drupal\Core\Language\LanguageManagerInterface'),
77 $this->eventDispatcher
82 * Tests ConfigFieldMapper::setEntity().
86 public function testSetEntity() {
87 $entity_type = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
89 ->expects($this->any())
90 ->method('getConfigPrefix')
91 ->will($this->returnValue('config_prefix'));
94 ->expects($this->any())
95 ->method('getDefinition')
96 ->will($this->returnValue($entity_type));
98 $field_storage = $this->getMock('Drupal\field\FieldStorageConfigInterface');
100 ->expects($this->any())
102 ->will($this->returnValue('field_storage_id'));
105 ->expects($this->any())
106 ->method('getFieldStorageDefinition')
107 ->will($this->returnValue($field_storage));
109 $result = $this->configFieldMapper->setEntity($this->entity);
110 $this->assertTrue($result);
112 // Ensure that the configuration name was added to the mapper.
113 $plugin_definition = $this->configFieldMapper->getPluginDefinition();
114 $this->assertTrue(in_array('config_prefix.field_storage_id', $plugin_definition['names']));
116 // Make sure setEntity() returns FALSE when called a second time.
117 $result = $this->configFieldMapper->setEntity($this->entity);
118 $this->assertFalse($result);