class EntityLabelTest

Same name and namespace in other branches
  1. 11.x core/modules/system/tests/src/Kernel/Entity/EntityLabelTest.php \Drupal\Tests\system\Kernel\Entity\EntityLabelTest

Tests that entity type labels use sentence-case.

@group Entity

Hierarchy

Expanded class hierarchy of EntityLabelTest

File

core/modules/system/tests/src/Kernel/Entity/EntityLabelTest.php, line 15

Namespace

Drupal\Tests\system\Kernel\Entity
View source
class EntityLabelTest extends KernelTestBase {
  
  /**
   * Tests that entity type labels use sentence-case.
   */
  public function testEntityLabelCasing() : void {
    $base_directory = $this->root . '/core/modules/';
    $modules = scandir($base_directory);
    $paths = [];
    foreach ($modules as $module) {
      $paths["\\Drupal\\{$module}\\Entity"] = $base_directory . $module . '/src/';
    }
    $namespaces = new \ArrayObject($paths);
    $discovery = new AnnotatedClassDiscovery('Entity', $namespaces, 'Drupal\\Core\\Entity\\Annotation\\EntityType');
    $definitions = $discovery->getDefinitions();
    foreach ($definitions as $definition) {
      /** @var \Drupal\Core\Entity\EntityType $definition */
      /** @var \Drupal\Core\StringTranslation\TranslatableMarkup $label */
      $label = $definition->getLabel();
      $collection_label = $definition->getCollectionLabel();
      $label_string = $label->getUntranslatedString();
      $collection_label_string = $collection_label->getUntranslatedString();
      // Keep the first word as it is for nouns that are all capital letters
      // (like RDF, URL alias etc.) so we can't run strtolower() for the entire
      // string. Special cases may need to be added to this test in the future
      // if an acronym is in a different position in the label.
      $first_word = strtok($label_string, " ");
      $remaining_string = strtolower((string) strstr($label_string, " "));
      $this->assertEquals($first_word . $remaining_string, $label_string);
      $first_word = strtok($collection_label_string, " ");
      $remaining_string = strtolower((string) strstr($collection_label_string, " "));
      $this->assertEquals($first_word . $remaining_string, $collection_label_string);
    }
  }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary
ConfigTestTrait::configImporter protected function Returns a ConfigImporter object to import test configuration.
ConfigTestTrait::copyConfig protected function Copies configuration objects from source storage to target storage.
EntityLabelTest::testEntityLabelCasing public function Tests that entity type labels use sentence-case.
ExtensionListTestTrait::getModulePath protected function Gets the path for the specified module.
ExtensionListTestTrait::getThemePath protected function Gets the path for the specified theme.
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.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
StorageCopyTrait::replaceStorageContents protected static function Copy the configuration from one storage to another and remove stale items.
TestRequirementsTrait::checkModuleRequirements Deprecated private function Checks missing module requirements.
TestRequirementsTrait::checkRequirements Deprecated protected function Check module requirements for the Drupal use case.
TestRequirementsTrait::getDrupalRoot protected static function Returns the Drupal root directory.

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