class LanguageUnitTest

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Language/LanguageUnitTest.php \Drupal\Tests\Core\Language\LanguageUnitTest
  2. 8.9.x core/tests/Drupal/Tests/Core/Language/LanguageUnitTest.php \Drupal\Tests\Core\Language\LanguageUnitTest
  3. 10 core/tests/Drupal/Tests/Core/Language/LanguageUnitTest.php \Drupal\Tests\Core\Language\LanguageUnitTest

@coversDefaultClass \Drupal\Core\Language\Language @group Language

Hierarchy

Expanded class hierarchy of LanguageUnitTest

File

core/tests/Drupal/Tests/Core/Language/LanguageUnitTest.php, line 15

Namespace

Drupal\Tests\Core\Language
View source
class LanguageUnitTest extends UnitTestCase {
    
    /**
     * @covers ::__construct
     */
    public function testConstruct() : void {
        $name = $this->randomMachineName();
        $language_code = $this->randomMachineName(2);
        $uuid = $this->randomMachineName();
        $language = new Language([
            'id' => $language_code,
            'name' => $name,
            'uuid' => $uuid,
        ]);
        // Test that nonexistent properties are not added to the language object.
        $this->assertTrue(property_exists($language, 'id'));
        $this->assertTrue(property_exists($language, 'name'));
        $this->assertFalse(property_exists($language, 'uuid'));
    }
    
    /**
     * @covers ::getName
     */
    public function testGetName() : void {
        $name = $this->randomMachineName();
        $language_code = $this->randomMachineName(2);
        $language = new Language([
            'id' => $language_code,
            'name' => $name,
        ]);
        $this->assertSame($name, $language->getName());
    }
    
    /**
     * @covers ::getId
     */
    public function testGetLangcode() : void {
        $language_code = $this->randomMachineName(2);
        $language = new Language([
            'id' => $language_code,
        ]);
        $this->assertSame($language_code, $language->getId());
    }
    
    /**
     * @covers ::getDirection
     */
    public function testGetDirection() : void {
        $language_code = $this->randomMachineName(2);
        $language = new Language([
            'id' => $language_code,
            'direction' => LanguageInterface::DIRECTION_RTL,
        ]);
        $this->assertSame(LanguageInterface::DIRECTION_RTL, $language->getDirection());
    }
    
    /**
     * @covers ::isDefault
     */
    public function testIsDefault() : void {
        $language_default = $this->getMockBuilder('Drupal\\Core\\Language\\LanguageDefault')
            ->disableOriginalConstructor()
            ->getMock();
        $container = $this->createMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
        $container->expects($this->any())
            ->method('get')
            ->with('language.default')
            ->willReturn($language_default);
        \Drupal::setContainer($container);
        $language = new Language([
            'id' => $this->randomMachineName(2),
        ]);
        // Set up the LanguageDefault to return different default languages on
        // consecutive calls.
        $language_default->expects($this->any())
            ->method('get')
            ->willReturnOnConsecutiveCalls($language, new Language([
            'id' => $this->randomMachineName(2),
        ]));
        $this->assertTrue($language->isDefault());
        $this->assertFalse($language->isDefault());
    }
    
    /**
     * Tests sorting an array of language objects.
     *
     * @covers ::sort
     *
     * @dataProvider providerTestSortArrayOfLanguages
     *
     * @param \Drupal\Core\Language\LanguageInterface[] $languages
     *   An array of language objects.
     * @param array $expected
     *   The expected array of keys.
     */
    public function testSortArrayOfLanguages(array $languages, array $expected) : void {
        Language::sort($languages);
        $this->assertSame($expected, array_keys($languages));
    }
    
    /**
     * Provides data for testSortArrayOfLanguages.
     *
     * @return array
     *   An array of test data.
     */
    public static function providerTestSortArrayOfLanguages() {
        $language9A = new Language([
            'id' => 'dd',
            'name' => 'A',
            'weight' => 9,
        ]);
        $language10A = new Language([
            'id' => 'ee',
            'name' => 'A',
            'weight' => 10,
        ]);
        $language10B = new Language([
            'id' => 'ff',
            'name' => 'B',
            'weight' => 10,
        ]);
        return [
            // Set up data set #0, already ordered by weight.
[
                // Set the data.
[
                    $language9A->getId() => $language9A,
                    $language10B->getId() => $language10B,
                ],
                // Set the expected key order.
[
                    $language9A->getId(),
                    $language10B->getId(),
                ],
            ],
            // Set up data set #1, out of order by weight.
[
                [
                    $language10B->getId() => $language10B,
                    $language9A->getId() => $language9A,
                ],
                [
                    $language9A->getId(),
                    $language10B->getId(),
                ],
            ],
            // Set up data set #2, tied by weight, already ordered by name.
[
                [
                    $language10A->getId() => $language10A,
                    $language10B->getId() => $language10B,
                ],
                [
                    $language10A->getId(),
                    $language10B->getId(),
                ],
            ],
            // Set up data set #3, tied by weight, out of order by name.
[
                [
                    $language10B->getId() => $language10B,
                    $language10A->getId() => $language10A,
                ],
                [
                    $language10A->getId(),
                    $language10B->getId(),
                ],
            ],
        ];
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
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.
LanguageUnitTest::providerTestSortArrayOfLanguages public static function Provides data for testSortArrayOfLanguages.
LanguageUnitTest::testConstruct public function @covers ::__construct
LanguageUnitTest::testGetDirection public function @covers ::getDirection
LanguageUnitTest::testGetLangcode public function @covers ::getId
LanguageUnitTest::testGetName public function @covers ::getName
LanguageUnitTest::testIsDefault public function @covers ::isDefault
LanguageUnitTest::testSortArrayOfLanguages public function Tests sorting an array of language objects.
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::setUp protected function 367
UnitTestCase::setUpBeforeClass public static function

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