4d95e0b0bcca51054226da9f7579bbd2bb0317db
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Form / ConfigFormBaseTraitTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Form;
4
5 use Drupal\Tests\UnitTestCase;
6
7 /**
8  * @coversDefaultClass \Drupal\Core\Form\ConfigFormBaseTrait
9  * @group Form
10  */
11 class ConfigFormBaseTraitTest extends UnitTestCase {
12
13   /**
14    * @covers ::config
15    */
16   public function testConfig() {
17
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' => [],
23     ]);
24
25     $trait->expects($this->any())
26       ->method('getEditableConfigNames')
27       ->willReturn(['editable.config']);
28
29     $config_method = new \ReflectionMethod($trait, 'config');
30     $config_method->setAccessible(TRUE);
31
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);
36
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);
40   }
41
42   /**
43    * @covers ::config
44    */
45   public function testConfigFactoryException() {
46     $trait = $this->getMockForTrait('Drupal\Core\Form\ConfigFormBaseTrait');
47     $config_method = new \ReflectionMethod($trait, 'config');
48     $config_method->setAccessible(TRUE);
49
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');
53   }
54
55   /**
56    * @covers ::config
57    */
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);
63
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');
67   }
68
69 }