View source
<?php
namespace Drupal\Tests\Core\Language;
use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Tests\UnitTestCase;
class LanguageUnitTest extends UnitTestCase {
public function testConstruct() {
$name = $this
->randomMachineName();
$language_code = $this
->randomMachineName(2);
$uuid = $this
->randomMachineName();
$language = new Language(array(
'id' => $language_code,
'name' => $name,
'uuid' => $uuid,
));
$this
->assertTrue(property_exists($language, 'id'));
$this
->assertTrue(property_exists($language, 'name'));
$this
->assertFalse(property_exists($language, 'uuid'));
}
public function testGetName() {
$name = $this
->randomMachineName();
$language_code = $this
->randomMachineName(2);
$language = new Language(array(
'id' => $language_code,
'name' => $name,
));
$this
->assertSame($name, $language
->getName());
}
public function testGetLangcode() {
$language_code = $this
->randomMachineName(2);
$language = new Language(array(
'id' => $language_code,
));
$this
->assertSame($language_code, $language
->getId());
}
public function testGetDirection() {
$language_code = $this
->randomMachineName(2);
$language = new Language(array(
'id' => $language_code,
'direction' => LanguageInterface::DIRECTION_RTL,
));
$this
->assertSame(LanguageInterface::DIRECTION_RTL, $language
->getDirection());
}
public function testIsDefault() {
$language_default = $this
->getMockBuilder('Drupal\\Core\\Language\\LanguageDefault')
->disableOriginalConstructor()
->getMock();
$container = $this
->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
$container
->expects($this
->any())
->method('get')
->with('language.default')
->will($this
->returnValue($language_default));
\Drupal::setContainer($container);
$language = new Language(array(
'id' => $this
->randomMachineName(2),
));
$language_default
->expects($this
->any())
->method('get')
->willReturnOnConsecutiveCalls($language, new Language(array(
'id' => $this
->randomMachineName(2),
)));
$this
->assertTrue($language
->isDefault());
$this
->assertFalse($language
->isDefault());
}
public function testSortArrayOfLanguages(array $languages, array $expected) {
Language::sort($languages);
$this
->assertSame($expected, array_keys($languages));
}
public function providerTestSortArrayOfLanguages() {
$language9A = new Language(array(
'id' => 'dd',
'name' => 'A',
'weight' => 9,
));
$language10A = new Language(array(
'id' => 'ee',
'name' => 'A',
'weight' => 10,
));
$language10B = new Language(array(
'id' => 'ff',
'name' => 'B',
'weight' => 10,
));
return array(
array(
array(
$language9A
->getId() => $language9A,
$language10B
->getId() => $language10B,
),
array(
$language9A
->getId(),
$language10B
->getId(),
),
),
array(
array(
$language10B
->getId() => $language10B,
$language9A
->getId() => $language9A,
),
array(
$language9A
->getId(),
$language10B
->getId(),
),
),
array(
array(
$language10A
->getId() => $language10A,
$language10B
->getId() => $language10B,
),
array(
$language10A
->getId(),
$language10B
->getId(),
),
),
array(
array(
$language10B
->getId() => $language10B,
$language10A
->getId() => $language10A,
),
array(
$language10A
->getId(),
$language10B
->getId(),
),
),
);
}
}