3 namespace Drupal\Tests\Core\Form;
5 use Drupal\Tests\UnitTestCase;
8 * @coversDefaultClass \Drupal\Core\Form\ConfigFormBaseTrait
11 class ConfigFormBaseTraitTest extends UnitTestCase {
16 public function testConfig() {
18 $trait = $this->getMockForTrait('Drupal\Core\Form\ConfigFormBaseTrait');
19 // Set up some configuration in a mocked config factory.
20 $trait->configFactory = $this->getConfigFactoryStub([
21 'editable.config' => [],
22 'immutable.config' => []
25 $trait->expects($this->any())
26 ->method('getEditableConfigNames')
27 ->willReturn(['editable.config']);
29 $config_method = new \ReflectionMethod($trait, 'config');
30 $config_method->setAccessible(TRUE);
32 // Ensure that configuration that is expected to be mutable is.
33 $result = $config_method->invoke($trait, 'editable.config');
34 $this->assertInstanceOf('\Drupal\Core\Config\Config', $result);
35 $this->assertNotInstanceOf('\Drupal\Core\Config\ImmutableConfig', $result);
37 // Ensure that configuration that is expected to be immutable is.
38 $result = $config_method->invoke($trait, 'immutable.config');
39 $this->assertInstanceOf('\Drupal\Core\Config\ImmutableConfig', $result);
45 public function testConfigFactoryException() {
46 $trait = $this->getMockForTrait('Drupal\Core\Form\ConfigFormBaseTrait');
47 $config_method = new \ReflectionMethod($trait, 'config');
48 $config_method->setAccessible(TRUE);
50 // There is no config factory available this should result in an exception.
51 $this->setExpectedException(\LogicException::class, 'No config factory available for ConfigFormBaseTrait');
52 $config_method->invoke($trait, 'editable.config');
58 public function testConfigFactoryExceptionInvalidProperty() {
59 $trait = $this->getMockForTrait('Drupal\Core\Form\ConfigFormBaseTrait');
60 $trait->configFactory = TRUE;
61 $config_method = new \ReflectionMethod($trait, 'config');
62 $config_method->setAccessible(TRUE);
64 // There is no config factory available this should result in an exception.
65 $this->setExpectedException(\LogicException::class, 'No config factory available for ConfigFormBaseTrait');
66 $config_method->invoke($trait, 'editable.config');