class ContentLanguageSettingsUnitTest

Same name and namespace in other branches
  1. 9 core/modules/language/tests/src/Unit/ContentLanguageSettingsUnitTest.php \Drupal\Tests\language\Unit\ContentLanguageSettingsUnitTest
  2. 8.9.x core/modules/language/tests/src/Unit/ContentLanguageSettingsUnitTest.php \Drupal\Tests\language\Unit\ContentLanguageSettingsUnitTest
  3. 10 core/modules/language/tests/src/Unit/ContentLanguageSettingsUnitTest.php \Drupal\Tests\language\Unit\ContentLanguageSettingsUnitTest

@coversDefaultClass \Drupal\language\Entity\ContentLanguageSettings
@group language

Hierarchy

Expanded class hierarchy of ContentLanguageSettingsUnitTest

File

core/modules/language/tests/src/Unit/ContentLanguageSettingsUnitTest.php, line 19

Namespace

Drupal\Tests\language\Unit
View source
class ContentLanguageSettingsUnitTest extends UnitTestCase {
  
  /**
   * The entity type used for testing.
   *
   * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $entityType;
  
  /**
   * The entity type manager used for testing.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $entityTypeManager;
  
  /**
   * The ID of the type of the entity under test.
   *
   * @var string
   */
  protected $entityTypeId;
  
  /**
   * The UUID generator used for testing.
   *
   * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $uuid;
  
  /**
   * The typed configuration manager used for testing.
   *
   * @var \Drupal\Core\Config\TypedConfigManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $typedConfigManager;
  
  /**
   * The typed configuration manager used for testing.
   *
   * @var \Drupal\Core\Config\Entity\ConfigEntityStorage|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $configEntityStorageInterface;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->entityTypeId = $this->randomMachineName();
    $this->entityType = $this->createMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
    $this->entityTypeManager = $this->createMock(EntityTypeManagerInterface::class);
    $this->uuid = $this->createMock('\\Drupal\\Component\\Uuid\\UuidInterface');
    $this->typedConfigManager = $this->createMock('Drupal\\Core\\Config\\TypedConfigManagerInterface');
    $this->configEntityStorageInterface = $this->createMock('Drupal\\Core\\Entity\\EntityStorageInterface');
    $container = new ContainerBuilder();
    $container->set('entity_type.manager', $this->entityTypeManager);
    $container->set('uuid', $this->uuid);
    $container->set('config.typed', $this->typedConfigManager);
    $container->set('config.storage', $this->configEntityStorageInterface);
    \Drupal::setContainer($container);
  }
  
  /**
   * @covers ::calculateDependencies
   */
  public function testCalculateDependencies() : void {
    // Mock the interfaces necessary to create a dependency on a bundle entity.
    $target_entity_type = $this->createMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
    $target_entity_type->expects($this->any())
      ->method('getBundleConfigDependency')
      ->willReturn([
      'type' => 'config',
      'name' => 'test.test_entity_type.id',
    ]);
    $this->entityTypeManager
      ->expects($this->any())
      ->method('getDefinition')
      ->with('test_entity_type')
      ->willReturn($target_entity_type);
    $config = new ContentLanguageSettings([
      'target_entity_type_id' => 'test_entity_type',
      'target_bundle' => 'test_bundle',
    ], 'language_content_settings');
    $dependencies = $config->calculateDependencies()
      ->getDependencies();
    $this->assertContains('test.test_entity_type.id', $dependencies['config']);
  }
  
  /**
   * @covers ::id
   */
  public function testId() : void {
    $config = new ContentLanguageSettings([
      'target_entity_type_id' => 'test_entity_type',
      'target_bundle' => 'test_bundle',
    ], 'language_content_settings');
    $this->assertSame('test_entity_type.test_bundle', $config->id());
  }
  
  /**
   * @covers ::getTargetEntityTypeId
   */
  public function testTargetEntityTypeId() : void {
    $config = new ContentLanguageSettings([
      'target_entity_type_id' => 'test_entity_type',
      'target_bundle' => 'test_bundle',
    ], 'language_content_settings');
    $this->assertSame('test_entity_type', $config->getTargetEntityTypeId());
  }
  
  /**
   * @covers ::getTargetBundle
   */
  public function testTargetBundle() : void {
    $config = new ContentLanguageSettings([
      'target_entity_type_id' => 'test_entity_type',
      'target_bundle' => 'test_bundle',
    ], 'language_content_settings');
    $this->assertSame('test_bundle', $config->getTargetBundle());
  }
  
  /**
   * @covers ::getDefaultLangcode
   * @covers ::setDefaultLangcode
   *
   * @dataProvider providerDefaultLangcode
   */
  public function testDefaultLangcode(ContentLanguageSettings $config, $expected) : void {
    $this->assertSame($expected, $config->getDefaultLangcode());
  }
  
  /**
   * Provides data to testDefaultLangcode().
   */
  public static function providerDefaultLangcode() {
    $langcode = Random::machineName();
    $config = new ContentLanguageSettings([
      'target_entity_type_id' => 'test_entity_type',
      'target_bundle' => 'test_bundle',
    ], 'language_content_settings');
    $config->setDefaultLangcode($langcode);
    $defaultConfig = new ContentLanguageSettings([
      'target_entity_type_id' => 'test_entity_type',
      'target_bundle' => 'test_default_language_bundle',
    ], 'language_content_settings');
    return [
      [
        $config,
        $langcode,
      ],
      [
        $defaultConfig,
        LanguageInterface::LANGCODE_SITE_DEFAULT,
      ],
    ];
  }
  
  /**
   * @covers ::setLanguageAlterable
   * @covers ::isLanguageAlterable
   *
   * @dataProvider providerLanguageAlterable
   */
  public function testLanguageAlterable(ContentLanguageSettings $config, $expected) : void {
    $this->assertSame($expected, $config->isLanguageAlterable());
  }
  
  /**
   * Provides data to testLanguageAlterable().
   */
  public static function providerLanguageAlterable() {
    $alterableConfig = new ContentLanguageSettings([
      'target_entity_type_id' => 'test_entity_type',
      'target_bundle' => 'test_bundle',
    ], 'language_content_settings');
    $alterableConfig->setLanguageAlterable(TRUE);
    $nonAlterableConfig = new ContentLanguageSettings([
      'target_entity_type_id' => 'test_entity_type',
      'target_bundle' => 'test_fixed_language_bundle',
    ], 'language_content_settings');
    $nonAlterableConfig->setLanguageAlterable(FALSE);
    $defaultConfig = new ContentLanguageSettings([
      'target_entity_type_id' => 'test_entity_type',
      'target_bundle' => 'test_default_language_bundle',
    ], 'language_content_settings');
    return [
      [
        $alterableConfig,
        TRUE,
      ],
      [
        $nonAlterableConfig,
        FALSE,
      ],
      [
        $defaultConfig,
        FALSE,
      ],
    ];
  }
  
  /**
   * @covers ::isDefaultConfiguration
   *
   * @dataProvider providerIsDefaultConfiguration
   */
  public function testIsDefaultConfiguration(ContentLanguageSettings $config, $expected) : void {
    $this->assertSame($expected, $config->isDefaultConfiguration());
  }
  
  /**
   * Provides data to testIsDefaultConfiguration().
   */
  public static function providerIsDefaultConfiguration() {
    $alteredLanguage = new ContentLanguageSettings([
      'target_entity_type_id' => 'test_entity_type',
      'target_bundle' => 'test_bundle',
    ], 'language_content_settings');
    $alteredLanguage->setLanguageAlterable(TRUE);
    $alteredDefaultLangcode = new ContentLanguageSettings([
      'target_entity_type_id' => 'test_entity_type',
      'target_bundle' => 'test_fixed_language_bundle',
    ], 'language_content_settings');
    $alteredDefaultLangcode->setDefaultLangcode(Random::machineName());
    $defaultConfig = new ContentLanguageSettings([
      'target_entity_type_id' => 'test_entity_type',
      'target_bundle' => 'test_default_language_bundle',
    ], 'language_content_settings');
    return [
      [
        $alteredLanguage,
        FALSE,
      ],
      [
        $alteredDefaultLangcode,
        FALSE,
      ],
      [
        $defaultConfig,
        TRUE,
      ],
    ];
  }
  
  /**
   * @covers ::loadByEntityTypeBundle
   *
   * @dataProvider providerLoadByEntityTypeBundle
   */
  public function testLoadByEntityTypeBundle($config_id, ?ContentLanguageSettings $existing_config, $expected_langcode, $expected_language_alterable) : void {
    [
      $type,
      $bundle,
    ] = explode('.', $config_id);
    $nullConfig = new ContentLanguageSettings([
      'target_entity_type_id' => $type,
      'target_bundle' => $bundle,
    ], 'language_content_settings');
    $this->configEntityStorageInterface
      ->expects($this->any())
      ->method('load')
      ->with($config_id)
      ->willReturn($existing_config);
    $this->configEntityStorageInterface
      ->expects($this->any())
      ->method('create')
      ->willReturn($nullConfig);
    $this->entityTypeManager
      ->expects($this->any())
      ->method('getStorage')
      ->with('language_content_settings')
      ->willReturn($this->configEntityStorageInterface);
    $entity_type_repository = $this->createMock(EntityTypeRepositoryInterface::class);
    $entity_type_repository->expects($this->any())
      ->method('getEntityTypeFromClass')
      ->with(ContentLanguageSettings::class)
      ->willReturn('language_content_settings');
    \Drupal::getContainer()->set('entity_type.repository', $entity_type_repository);
    $config = ContentLanguageSettings::loadByEntityTypeBundle($type, $bundle);
    $this->assertSame($expected_langcode, $config->getDefaultLangcode());
    $this->assertSame($expected_language_alterable, $config->isLanguageAlterable());
  }
  
  /**
   * Provides data to testLoadByEntityTypeBundle().
   */
  public static function providerLoadByEntityTypeBundle() {
    $alteredLanguage = new ContentLanguageSettings([
      'target_entity_type_id' => 'test_entity_type',
      'target_bundle' => 'test_bundle',
    ], 'language_content_settings');
    $alteredLanguage->setLanguageAlterable(TRUE);
    $langcode = Random::machineName();
    $alteredDefaultLangcode = new ContentLanguageSettings([
      'target_entity_type_id' => 'test_entity_type',
      'target_bundle' => 'test_fixed_language_bundle',
    ], 'language_content_settings');
    $alteredDefaultLangcode->setDefaultLangcode($langcode);
    $defaultConfig = new ContentLanguageSettings([
      'target_entity_type_id' => 'test_entity_type',
      'target_bundle' => 'test_default_language_bundle',
    ], 'language_content_settings');
    return [
      [
        'test_entity_type.test_bundle',
        $alteredLanguage,
        LanguageInterface::LANGCODE_SITE_DEFAULT,
        TRUE,
      ],
      [
        'test_entity_type.test_fixed_language_bundle',
        $alteredDefaultLangcode,
        $langcode,
        FALSE,
      ],
      [
        'test_entity_type.test_default_language_bundle',
        $defaultConfig,
        LanguageInterface::LANGCODE_SITE_DEFAULT,
        FALSE,
      ],
      [
        'test_entity_type.null_bundle',
        NULL,
        LanguageInterface::LANGCODE_SITE_DEFAULT,
        FALSE,
      ],
    ];
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ContentLanguageSettingsUnitTest::$configEntityStorageInterface protected property The typed configuration manager used for testing.
ContentLanguageSettingsUnitTest::$entityType protected property The entity type used for testing.
ContentLanguageSettingsUnitTest::$entityTypeId protected property The ID of the type of the entity under test.
ContentLanguageSettingsUnitTest::$entityTypeManager protected property The entity type manager used for testing.
ContentLanguageSettingsUnitTest::$typedConfigManager protected property The typed configuration manager used for testing.
ContentLanguageSettingsUnitTest::$uuid protected property The UUID generator used for testing.
ContentLanguageSettingsUnitTest::providerDefaultLangcode public static function Provides data to testDefaultLangcode().
ContentLanguageSettingsUnitTest::providerIsDefaultConfiguration public static function Provides data to testIsDefaultConfiguration().
ContentLanguageSettingsUnitTest::providerLanguageAlterable public static function Provides data to testLanguageAlterable().
ContentLanguageSettingsUnitTest::providerLoadByEntityTypeBundle public static function Provides data to testLoadByEntityTypeBundle().
ContentLanguageSettingsUnitTest::setUp protected function Overrides UnitTestCase::setUp
ContentLanguageSettingsUnitTest::testCalculateDependencies public function @covers ::calculateDependencies[[api-linebreak]]
ContentLanguageSettingsUnitTest::testDefaultLangcode public function @covers ::getDefaultLangcode[[api-linebreak]]
@covers ::setDefaultLangcode[[api-linebreak]]
ContentLanguageSettingsUnitTest::testId public function @covers ::id[[api-linebreak]]
ContentLanguageSettingsUnitTest::testIsDefaultConfiguration public function @covers ::isDefaultConfiguration[[api-linebreak]]
ContentLanguageSettingsUnitTest::testLanguageAlterable public function @covers ::setLanguageAlterable[[api-linebreak]]
@covers ::isLanguageAlterable[[api-linebreak]]
ContentLanguageSettingsUnitTest::testLoadByEntityTypeBundle public function @covers ::loadByEntityTypeBundle[[api-linebreak]]
ContentLanguageSettingsUnitTest::testTargetBundle public function @covers ::getTargetBundle[[api-linebreak]]
ContentLanguageSettingsUnitTest::testTargetEntityTypeId public function @covers ::getTargetEntityTypeId[[api-linebreak]]
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
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::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::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.

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