Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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    * The mocked event dispatcher.
40    *
41    * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject
42    */
43   protected $eventDispatcher;
44
45   /**
46    * {@inheritdoc}
47    */
48   protected function setUp() {
49     $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
50     $this->entity = $this->getMock('Drupal\field\FieldConfigInterface');
51
52     $definition = [
53       'class' => '\Drupal\config_translation\ConfigFieldMapper',
54       'base_route_name' => 'entity.field_config.node_field_edit_form',
55       'title' => '@label field',
56       'names' => [],
57       'entity_type' => 'field_config',
58     ];
59
60     $locale_config_manager = $this->getMockBuilder('Drupal\locale\LocaleConfigManager')
61       ->disableOriginalConstructor()
62       ->getMock();
63
64     $this->eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
65
66     $this->configFieldMapper = new ConfigFieldMapper(
67       'node_fields',
68       $definition,
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(),
75       $this->entityManager,
76       $this->getMock('Drupal\Core\Language\LanguageManagerInterface'),
77       $this->eventDispatcher
78     );
79   }
80
81   /**
82    * Tests ConfigFieldMapper::setEntity().
83    *
84    * @covers ::setEntity
85    */
86   public function testSetEntity() {
87     $entity_type = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
88     $entity_type
89       ->expects($this->any())
90       ->method('getConfigPrefix')
91       ->will($this->returnValue('config_prefix'));
92
93     $this->entityManager
94       ->expects($this->any())
95       ->method('getDefinition')
96       ->will($this->returnValue($entity_type));
97
98     $field_storage = $this->getMock('Drupal\field\FieldStorageConfigInterface');
99     $field_storage
100       ->expects($this->any())
101       ->method('id')
102       ->will($this->returnValue('field_storage_id'));
103
104     $this->entity
105       ->expects($this->any())
106       ->method('getFieldStorageDefinition')
107       ->will($this->returnValue($field_storage));
108
109     $result = $this->configFieldMapper->setEntity($this->entity);
110     $this->assertTrue($result);
111
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']));
115
116     // Make sure setEntity() returns FALSE when called a second time.
117     $result = $this->configFieldMapper->setEntity($this->entity);
118     $this->assertFalse($result);
119   }
120
121 }