function ConfigEntityBaseUnitTest::testSort

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php \Drupal\Tests\Core\Config\Entity\ConfigEntityBaseUnitTest::testSort()
  2. 8.9.x core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php \Drupal\Tests\Core\Config\Entity\ConfigEntityBaseUnitTest::testSort()
  3. 10 core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php \Drupal\Tests\Core\Config\Entity\ConfigEntityBaseUnitTest::testSort()

@covers ::sort

File

core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php, line 521

Class

ConfigEntityBaseUnitTest
@coversDefaultClass \Drupal\Core\Config\Entity\ConfigEntityBase @group Config

Namespace

Drupal\Tests\Core\Config\Entity

Code

public function testSort() : void {
    $this->entityType
        ->expects($this->atLeastOnce())
        ->method('getKey')
        ->with('label')
        ->willReturn('label');
    $entity_a = new SortTestConfigEntityWithWeight([
        'label' => 'foo',
    ], $this->entityTypeId);
    $entity_b = new SortTestConfigEntityWithWeight([
        'label' => 'bar',
    ], $this->entityTypeId);
    // Test sorting by label.
    $list = [
        $entity_a,
        $entity_b,
    ];
    usort($list, '\\Drupal\\Core\\Config\\Entity\\ConfigEntityBase::sort');
    $this->assertSame($entity_b, $list[0]);
    $list = [
        $entity_b,
        $entity_a,
    ];
    usort($list, '\\Drupal\\Core\\Config\\Entity\\ConfigEntityBase::sort');
    $this->assertSame($entity_b, $list[0]);
    // Test sorting by weight.
    $entity_a->weight = 0;
    $entity_b->weight = 1;
    $list = [
        $entity_b,
        $entity_a,
    ];
    usort($list, '\\Drupal\\Core\\Config\\Entity\\ConfigEntityBase::sort');
    $this->assertSame($entity_a, $list[0]);
    $list = [
        $entity_a,
        $entity_b,
    ];
    usort($list, '\\Drupal\\Core\\Config\\Entity\\ConfigEntityBase::sort');
    $this->assertSame($entity_a, $list[0]);
}

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