function ConfigEntityBaseUnitTest::testSort

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php \Drupal\Tests\Core\Config\Entity\ConfigEntityBaseUnitTest::testSort()
  2. 10 core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityBaseUnitTest.php \Drupal\Tests\Core\Config\Entity\ConfigEntityBaseUnitTest::testSort()
  3. 11.x 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 513

Class

ConfigEntityBaseUnitTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Core%21Config%21Entity%21ConfigEntityBase.php/class/ConfigEntityBase/8.9.x" title="Defines a base configuration entity class." class="local">\Drupal\Core\Config\Entity\ConfigEntityBase</a> @group Config

Namespace

Drupal\Tests\Core\Config\Entity

Code

public function testSort() {
    $this->entityTypeManager
        ->expects($this->any())
        ->method('getDefinition')
        ->with($this->entityTypeId)
        ->will($this->returnValue([
        'entity_keys' => [
            'label' => 'label',
        ],
    ]));
    $entity_a = $this->createMock('\\Drupal\\Core\\Config\\Entity\\ConfigEntityInterface');
    $entity_a->expects($this->atLeastOnce())
        ->method('label')
        ->willReturn('foo');
    $entity_b = $this->createMock('\\Drupal\\Core\\Config\\Entity\\ConfigEntityInterface');
    $entity_b->expects($this->atLeastOnce())
        ->method('label')
        ->willReturn('bar');
    // Test sorting by label.
    $list = [
        $entity_a,
        $entity_b,
    ];
    // Suppress errors because of https://bugs.php.net/bug.php?id=50688.
    @usort($list, '\\Drupal\\Core\\Config\\Entity\\ConfigEntityBase::sort');
    $this->assertSame($entity_b, $list[0]);
    $list = [
        $entity_b,
        $entity_a,
    ];
    // Suppress errors because of https://bugs.php.net/bug.php?id=50688.
    @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,
    ];
    // Suppress errors because of https://bugs.php.net/bug.php?id=50688.
    @usort($list, '\\Drupal\\Core\\Config\\Entity\\ConfigEntityBase::sort');
    $this->assertSame($entity_a, $list[0]);
    $list = [
        $entity_a,
        $entity_b,
    ];
    // Suppress errors because of https://bugs.php.net/bug.php?id=50688.
    @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.