class NaturalSortTest

Tests Drupal\Core\Utility\NaturalSort.

Attributes

#[CoversClass(NaturalSort::class)] #[Group('Utility')]

Hierarchy

Expanded class hierarchy of NaturalSortTest

File

core/tests/Drupal/Tests/Core/Utility/NaturalSortTest.php, line 21

Namespace

Drupal\Tests\Core\Utility
View source
class NaturalSortTest extends UnitTestCase {
  
  /**
   * Tests comparing strings with an explicit language code.
   */
  public function testExplicitLangcode() : void {
    // Numbers embedded in strings are compared by their numeric value.
    $this->assertGreaterThan(0, NaturalSort::strnatcasecmp('Format 100x100', 'Format 10x10', 'en'));
    $this->assertLessThan(0, NaturalSort::strnatcasecmp('Format 10x10', 'Format 100x100', 'en'));
    $this->assertSame(0, NaturalSort::strnatcasecmp('Format 10x10', 'Format 10x10', 'en'));
    // Accented characters are sorted by their base letter, unlike
    // strnatcasecmp(), which sorts them after all ASCII characters.
    $this->assertLessThan(0, NaturalSort::strnatcasecmp('Émission', 'Format', 'en'));
    $this->assertGreaterThan(0, NaturalSort::strnatcasecmp('Format', 'Émission', 'en'));
    // Comparing is case-insensitive for different letters.
    $this->assertLessThan(0, NaturalSort::strnatcasecmp('apple', 'Banana', 'en'));
    $this->assertGreaterThan(0, NaturalSort::strnatcasecmp('Banana', 'apple', 'en'));
  }
  
  /**
   * Tests that the current language is used when no language code is given.
   */
  public function testCurrentLanguage() : void {
    $language_manager = $this->createMock(LanguageManagerInterface::class);
    $language_manager->expects($this->atLeastOnce())
      ->method('getCurrentLanguage')
      ->willReturn(new Language([
      'id' => 'fr',
    ]));
    $container = new ContainerBuilder();
    $container->set('language_manager', $language_manager);
    \Drupal::setContainer($container);
    $this->assertLessThan(0, NaturalSort::strnatcasecmp('Émission', 'Format'));
    $this->assertGreaterThan(0, NaturalSort::strnatcasecmp('Format 100x100', 'Format 10x10'));
  }
  
  /**
   * Tests the fallback to strnatcasecmp() when no language is available.
   */
  public function testNoLanguageFallback() : void {
    // Without a language code and without a language manager service the
    // comparison falls back to strnatcasecmp(), which compares bytes and
    // sorts accented characters after all ASCII characters.
    $this->assertGreaterThan(0, NaturalSort::strnatcasecmp('Émission', 'Format'));
    $this->assertLessThan(0, NaturalSort::strnatcasecmp('Format', 'Émission'));
    $this->assertGreaterThan(0, NaturalSort::strnatcasecmp('Format 100x100', 'Format 10x10'));
  }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overrides
DrupalTestCase::$root protected property The Drupal root directory.
DrupalTestCase::getDrupalRoot Deprecated protected function Returns the Drupal root directory. 1
DrupalTestCase::setUpRoot final protected function Ensure that the $root property is set initially.
DrupalTestCase::skipTestWithAttribute final protected function Supports skipping tests with the Skip attribute.
DrupalTestCaseTrait::checkErrorHandlerOnTearDown public function Checks the test error handler after test execution. 1
DrupalTestCaseTrait::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.
ForwardCompatibilityTrait::expectExceptionMessageIs protected function Expects an exactly matching exception message.
ForwardCompatibilityTrait::expectExceptionMessageIsOrContains protected function Expects an exception message containing a specified string.
NaturalSortTest::testCurrentLanguage public function Tests that the current language is used when no language code is given.
NaturalSortTest::testExplicitLangcode public function Tests comparing strings with an explicit language code.
NaturalSortTest::testNoLanguageFallback public function Tests the fallback to strnatcasecmp() when no language is available.
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::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::setUp protected function 362
UnitTestCase::setupMockIterator protected function Set up a traversable class mock to return specific items when iterated.

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