8e6d0b903b6912294fa18f16239c7da81c707f3b
[yaffs-website] / web / core / modules / config_translation / tests / src / Unit / ConfigFieldMapperTest.php
1 <?php
2
3 namespace Drupal\Tests\config_translation\Unit;
4
5 use Drupal\config_translation\ConfigFieldMapper;
6 use Drupal\Tests\UnitTestCase;
7
8 /**
9  * Tests the functionality provided by the configuration field mapper.
10  *
11  * @group config_translation
12  *
13  * @coversDefaultClass \Drupal\config_translation\ConfigFieldMapper
14  */
15 class ConfigFieldMapperTest extends UnitTestCase {
16
17   /**
18    * The configuration field mapper to test.
19    *
20    * @var \Drupal\config_translation\ConfigFieldMapper
21    */
22   protected $configFieldMapper;
23
24   /**
25    * The field config instance used for testing.
26    *
27    * @var \Drupal\field\FieldConfigInterface|\PHPUnit_Framework_MockObject_MockObject
28    */
29   protected $entity;
30
31   /**
32    * The entity manager used for testing.
33    *
34    * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
35    */
36   protected $entityManager;
37
38   /**
39    * {@inheritdoc}
40    */
41   protected function setUp() {
42     $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
43     $this->entity = $this->getMock('Drupal\field\FieldConfigInterface');
44
45     $definition = [
46       'class' => '\Drupal\config_translation\ConfigFieldMapper',
47       'base_route_name' => 'entity.field_config.node_field_edit_form',
48       'title' => '@label field',
49       'names' => [],
50       'entity_type' => 'field_config',
51     ];
52
53     $locale_config_manager = $this->getMockBuilder('Drupal\locale\LocaleConfigManager')
54       ->disableOriginalConstructor()
55       ->getMock();
56
57     $this->configFieldMapper = new ConfigFieldMapper(
58       'node_fields',
59       $definition,
60       $this->getConfigFactoryStub(),
61       $this->getMock('Drupal\Core\Config\TypedConfigManagerInterface'),
62       $locale_config_manager,
63       $this->getMock('Drupal\config_translation\ConfigMapperManagerInterface'),
64       $this->getMock('Drupal\Core\Routing\RouteProviderInterface'),
65       $this->getStringTranslationStub(),
66       $this->entityManager,
67       $this->getMock('Drupal\Core\Language\LanguageManagerInterface')
68     );
69   }
70
71   /**
72    * Tests ConfigFieldMapper::setEntity().
73    *
74    * @covers ::setEntity
75    */
76   public function testSetEntity() {
77     $entity_type = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
78     $entity_type
79       ->expects($this->any())
80       ->method('getConfigPrefix')
81       ->will($this->returnValue('config_prefix'));
82
83     $this->entityManager
84       ->expects($this->any())
85       ->method('getDefinition')
86       ->will($this->returnValue($entity_type));
87
88     $field_storage = $this->getMock('Drupal\field\FieldStorageConfigInterface');
89     $field_storage
90       ->expects($this->any())
91       ->method('id')
92       ->will($this->returnValue('field_storage_id'));
93
94     $this->entity
95       ->expects($this->any())
96       ->method('getFieldStorageDefinition')
97       ->will($this->returnValue($field_storage));
98
99     $result = $this->configFieldMapper->setEntity($this->entity);
100     $this->assertTrue($result);
101
102     // Ensure that the configuration name was added to the mapper.
103     $plugin_definition = $this->configFieldMapper->getPluginDefinition();
104     $this->assertTrue(in_array('config_prefix.field_storage_id', $plugin_definition['names']));
105
106     // Make sure setEntity() returns FALSE when called a second time.
107     $result = $this->configFieldMapper->setEntity($this->entity);
108     $this->assertFalse($result);
109   }
110
111 }