class LocaleProjectStorageTest

Same name in other branches
  1. 10 core/modules/locale/tests/src/Unit/LocaleProjectStorageTest.php \Drupal\Tests\locale\Unit\LocaleProjectStorageTest

@coversDefaultClass \Drupal\locale\LocaleProjectStorage @group locale @runTestsInSeparateProcesses

Hierarchy

Expanded class hierarchy of LocaleProjectStorageTest

File

core/modules/locale/tests/src/Unit/LocaleProjectStorageTest.php, line 16

Namespace

Drupal\Tests\locale\Unit
View source
class LocaleProjectStorageTest extends UnitTestCase {
    
    /**
     * @var \Drupal\locale\LocaleProjectStorage
     */
    private LocaleProjectStorage $projectStorage;
    
    /**
     * @var \Drupal\Core\KeyValueStore\KeyValueMemoryFactory
     */
    private KeyValueMemoryFactory $keyValueMemoryFactory;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->keyValueMemoryFactory = new KeyValueMemoryFactory();
        $this->projectStorage = new LocaleProjectStorage($this->keyValueMemoryFactory);
    }
    
    /**
     * Tests that projects are sorted by weight and key.
     */
    public function testSorting() : void {
        // There are no projects.
        $this->assertSame([], $this->projectStorage
            ->getAll());
        // Add project 'b'.
        $this->projectStorage
            ->set('b', [
            'name' => 'b',
        ]);
        $this->assertSame([
            'b',
        ], array_keys($this->projectStorage
            ->getAll()));
        // Add project 'c' and confirm alphabetical order.
        $this->projectStorage
            ->set('c', [
            'name' => 'c',
        ]);
        $this->assertSame([
            'b',
            'c',
        ], array_keys($this->projectStorage
            ->getAll()));
        // Add project 'a' and confirm 'a' is first.
        $this->projectStorage
            ->set('a', [
            'name' => 'a',
        ]);
        $this->assertSame([
            'a',
            'b',
            'c',
        ], array_keys($this->projectStorage
            ->getAll()));
        // Add project 'd' with a negative weight and confirm 'd' is first.
        $this->projectStorage
            ->set('d', [
            'name' => 'd',
            'weight' => -1,
        ]);
        $this->assertSame([
            'd',
            'a',
            'b',
            'c',
        ], array_keys($this->projectStorage
            ->getAll()));
        // Add project 'aa' with a positive weight and confirm 'aa' is last.
        $this->projectStorage
            ->set('aa', [
            'name' => 'aa',
            'weight' => 1,
        ]);
        $this->assertSame([
            'd',
            'a',
            'b',
            'c',
            'aa',
        ], array_keys($this->projectStorage
            ->getAll()));
        // Delete project 'a'.
        $this->projectStorage
            ->delete('a');
        $this->assertSame([
            'd',
            'b',
            'c',
            'aa',
        ], array_keys($this->projectStorage
            ->getAll()));
        // Add project 'e' with a lower negative weight than 'd' and confirm 'e' is
        // first.
        $this->projectStorage
            ->set('e', [
            'name' => 'e',
            'weight' => -5,
        ]);
        $this->assertSame([
            'e',
            'd',
            'b',
            'c',
            'aa',
        ], array_keys($this->projectStorage
            ->getAll()));
        // Pretend there is a container rebuild by generating a new
        // LocaleProjectStorage object with the same data.
        $this->projectStorage = new LocaleProjectStorage($this->keyValueMemoryFactory);
        $this->projectStorage
            ->set('z', [
            'name' => 'z',
        ]);
        $this->assertSame([
            'e',
            'd',
            'b',
            'c',
            'z',
            'aa',
        ], array_keys($this->projectStorage
            ->getAll()));
        // Now delete all projects.
        $this->projectStorage
            ->deleteAll();
        $this->assertSame([], $this->projectStorage
            ->getAll());
        // Add project 'z' before project 'a' and confirm 'a' is first.
        $this->projectStorage
            ->set('z', [
            'name' => 'z',
        ]);
        $this->projectStorage
            ->set('a', [
            'name' => 'a',
        ]);
        $this->assertSame([
            'a',
            'z',
        ], array_keys($this->projectStorage
            ->getAll()));
    }
    
    /**
     * Tests deleted projects are not included in the count.
     */
    public function testDelete() : void {
        $this->projectStorage
            ->set('b', [
            'name' => 'b',
        ]);
        $this->assertSame([
            'name' => 'b',
        ], $this->projectStorage
            ->get('b'));
        $this->assertSame(1, $this->projectStorage
            ->countProjects());
        $this->projectStorage
            ->delete('b');
        $this->assertNull($this->projectStorage
            ->get('b'));
        $this->assertSame(0, $this->projectStorage
            ->countProjects());
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
LocaleProjectStorageTest::$keyValueMemoryFactory private property
LocaleProjectStorageTest::$projectStorage private property
LocaleProjectStorageTest::setUp protected function Overrides UnitTestCase::setUp
LocaleProjectStorageTest::testDelete public function Tests deleted projects are not included in the count.
LocaleProjectStorageTest::testSorting public function Tests that projects are sorted by weight and key.
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::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
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::setUpBeforeClass public static function

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