class ConfigMapperManagerTest

Same name in other branches
  1. 9 core/modules/config_translation/tests/src/Unit/ConfigMapperManagerTest.php \Drupal\Tests\config_translation\Unit\ConfigMapperManagerTest
  2. 8.9.x core/modules/config_translation/tests/src/Unit/ConfigMapperManagerTest.php \Drupal\Tests\config_translation\Unit\ConfigMapperManagerTest
  3. 10 core/modules/config_translation/tests/src/Unit/ConfigMapperManagerTest.php \Drupal\Tests\config_translation\Unit\ConfigMapperManagerTest

Tests the functionality provided by configuration translation mapper manager.

@group config_translation

Hierarchy

Expanded class hierarchy of ConfigMapperManagerTest

File

core/modules/config_translation/tests/src/Unit/ConfigMapperManagerTest.php, line 21

Namespace

Drupal\Tests\config_translation\Unit
View source
class ConfigMapperManagerTest extends UnitTestCase {
    
    /**
     * The configuration mapper manager to test.
     *
     * @var \Drupal\config_translation\ConfigMapperManager
     */
    protected $configMapperManager;
    
    /**
     * The typed configuration manager used for testing.
     *
     * @var \Drupal\Core\Config\TypedConfigManagerInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $typedConfigManager;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $language = new Language([
            'id' => 'en',
        ]);
        $language_manager = $this->createMock('Drupal\\Core\\Language\\LanguageManagerInterface');
        $language_manager->expects($this->once())
            ->method('getCurrentLanguage')
            ->with(LanguageInterface::TYPE_INTERFACE)
            ->willReturn($language);
        $this->typedConfigManager = $this->getMockBuilder('Drupal\\Core\\Config\\TypedConfigManagerInterface')
            ->getMock();
        $module_handler = $this->createMock('Drupal\\Core\\Extension\\ModuleHandlerInterface');
        $theme_handler = $this->createMock('Drupal\\Core\\Extension\\ThemeHandlerInterface');
        $this->configMapperManager = new ConfigMapperManager($this->createMock('Drupal\\Core\\Cache\\CacheBackendInterface'), $language_manager, $module_handler, $this->typedConfigManager, $theme_handler);
    }
    
    /**
     * Tests ConfigMapperManager::hasTranslatable().
     *
     * @param \Drupal\Core\TypedData\TypedDataInterface $element
     *   The schema element to test.
     * @param bool $expected
     *   The expected return value of ConfigMapperManager::hasTranslatable().
     *
     * @dataProvider providerTestHasTranslatable
     */
    public function testHasTranslatable(TypedDataInterface $element, $expected) : void {
        $this->typedConfigManager
            ->expects($this->once())
            ->method('get')
            ->with('test')
            ->willReturn($element);
        $result = $this->configMapperManager
            ->hasTranslatable('test');
        $this->assertSame($expected, $result);
    }
    
    /**
     * Provides data for ConfigMapperManager::testHasTranslatable()
     *
     * @return array
     *   An array of arrays, where each inner array contains the schema element
     *   to test as the first key and the expected result of
     *   ConfigMapperManager::hasTranslatable() as the second key.
     */
    public static function providerTestHasTranslatable() {
        return [
            [
                static::getElement([]),
                FALSE,
            ],
            [
                static::getElement([
                    'aaa' => 'bbb',
                ]),
                FALSE,
            ],
            [
                static::getElement([
                    'translatable' => FALSE,
                ]),
                FALSE,
            ],
            [
                static::getElement([
                    'translatable' => TRUE,
                ]),
                TRUE,
            ],
            [
                static::getNestedElement([
                    static::getElement([]),
                ]),
                FALSE,
            ],
            [
                static::getNestedElement([
                    static::getElement([
                        'translatable' => TRUE,
                    ]),
                ]),
                TRUE,
            ],
            [
                static::getNestedElement([
                    static::getElement([
                        'aaa' => 'bbb',
                    ]),
                    static::getElement([
                        'ccc' => 'ddd',
                    ]),
                    static::getElement([
                        'eee' => 'fff',
                    ]),
                ]),
                FALSE,
            ],
            [
                static::getNestedElement([
                    static::getElement([
                        'aaa' => 'bbb',
                    ]),
                    static::getElement([
                        'ccc' => 'ddd',
                    ]),
                    static::getElement([
                        'translatable' => TRUE,
                    ]),
                ]),
                TRUE,
            ],
            [
                static::getNestedElement([
                    static::getElement([
                        'aaa' => 'bbb',
                    ]),
                    static::getNestedElement([
                        static::getElement([
                            'ccc' => 'ddd',
                        ]),
                        static::getElement([
                            'eee' => 'fff',
                        ]),
                    ]),
                    static::getNestedElement([
                        static::getElement([
                            'ggg' => 'hhh',
                        ]),
                        static::getElement([
                            'iii' => 'jjj',
                        ]),
                    ]),
                ]),
                FALSE,
            ],
            [
                static::getNestedElement([
                    static::getElement([
                        'aaa' => 'bbb',
                    ]),
                    static::getNestedElement([
                        static::getElement([
                            'ccc' => 'ddd',
                        ]),
                        static::getElement([
                            'eee' => 'fff',
                        ]),
                    ]),
                    static::getNestedElement([
                        static::getElement([
                            'ggg' => 'hhh',
                        ]),
                        static::getElement([
                            'translatable' => TRUE,
                        ]),
                    ]),
                ]),
                TRUE,
            ],
        ];
    }
    
    /**
     * Returns a mocked schema element.
     *
     * @param array $definition
     *   The definition of the schema element.
     *
     * @return \Drupal\Core\Config\Schema\Element
     *   The mocked schema element.
     */
    protected static function getElement(array $definition) {
        $data_definition = new DataDefinition($definition);
        $element = (new Prophet())->prophesize(TypedDataInterface::class);
        $element->getDataDefinition()
            ->willReturn($data_definition);
        return $element->reveal();
    }
    
    /**
     * Returns a mocked nested schema element.
     *
     * @param array $elements
     *   An array of simple schema elements.
     *
     * @return \Drupal\Core\Config\Schema\Mapping
     *   A nested schema element, containing the passed-in elements.
     */
    protected static function getNestedElement(array $elements) {
        // ConfigMapperManager::findTranslatable() checks for
        // \Drupal\Core\TypedData\TraversableTypedDataInterface, but mocking that
        // directly does not work, because we need to implement \IteratorAggregate
        // in order for getIterator() to be called. Therefore we need to mock
        // \Drupal\Core\Config\Schema\ArrayElement, but that is abstract, so we
        // need to mock one of the subclasses of it.
        $nested_element = (new Prophet())->prophesize(Mapping::class);
        $nested_element->getIterator()
            ->shouldBeCalledTimes(1)
            ->willReturn(new \ArrayIterator($elements));
        return $nested_element->reveal();
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ConfigMapperManagerTest::$configMapperManager protected property The configuration mapper manager to test.
ConfigMapperManagerTest::$typedConfigManager protected property The typed configuration manager used for testing.
ConfigMapperManagerTest::getElement protected static function Returns a mocked schema element.
ConfigMapperManagerTest::getNestedElement protected static function Returns a mocked nested schema element.
ConfigMapperManagerTest::providerTestHasTranslatable public static function Provides data for ConfigMapperManager::testHasTranslatable()
ConfigMapperManagerTest::setUp protected function Overrides UnitTestCase::setUp
ConfigMapperManagerTest::testHasTranslatable public function Tests ConfigMapperManager::hasTranslatable().
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.