function LocaleProjectRepositoryTest::testSorting

Same name and namespace in other branches
  1. main core/modules/locale/tests/src/Unit/LocaleProjectRepositoryTest.php \Drupal\Tests\locale\Unit\LocaleProjectRepositoryTest::testSorting()

Tests that projects are sorted by weight and key.

File

core/modules/locale/tests/src/Unit/LocaleProjectRepositoryTest.php, line 101

Class

LocaleProjectRepositoryTest
Tests Drupal\locale\LocaleProjectRepository.

Namespace

Drupal\Tests\locale\Unit

Code

public function testSorting() : void {
  // Add project 'b'.
  $b = new LocaleTranslatableProject('b', 'b', 'b', 'b', 'b');
  $this->localeProjectRepository
    ->set($b);
  $this->assertSame([
    'b',
  ], array_keys($this->localeProjectRepository
    ->getAll()));
  // Add project 'c' and confirm alphabetical order.
  $c = new LocaleTranslatableProject('c', 'c', 'c', 'c', 'c');
  $this->localeProjectRepository
    ->set($c);
  $this->assertSame([
    'b',
    'c',
  ], array_keys($this->localeProjectRepository
    ->getAll()));
  // Add project 'a' and confirm 'a' is first.
  $a = new LocaleTranslatableProject('a', 'a', 'a', 'a', 'a');
  $this->localeProjectRepository
    ->set($a);
  $this->assertSame([
    'a',
    'b',
    'c',
  ], array_keys($this->localeProjectRepository
    ->getAll()));
  // Add project 'd' with a negative weight and confirm 'd' is first.
  $d = new LocaleTranslatableProject('d', 'd', 'd', 'd', 'd', weight: -1);
  $this->localeProjectRepository
    ->set($d);
  $this->assertSame([
    'd',
    'a',
    'b',
    'c',
  ], array_keys($this->localeProjectRepository
    ->getAll()));
  // Add project 'aa' with a positive weight and confirm 'aa' is last.
  $aa = new LocaleTranslatableProject('aa', 'aa', 'aa', 'aa', 'aa', weight: 1);
  $this->localeProjectRepository
    ->set($aa);
  $this->assertSame([
    'd',
    'a',
    'b',
    'c',
    'aa',
  ], array_keys($this->localeProjectRepository
    ->getAll()));
  // Delete project 'a'.
  $this->localeProjectRepository
    ->deleteMultiple([
    'a',
  ]);
  $this->assertSame([
    'd',
    'b',
    'c',
    'aa',
  ], array_keys($this->localeProjectRepository
    ->getAll()));
  // Add project 'e' with a lower negative weight than 'd' and confirm 'e' is
  // first.
  $e = new LocaleTranslatableProject('e', 'e', 'e', 'e', 'e', weight: -5);
  $this->localeProjectRepository
    ->set($e);
  $this->assertSame([
    'e',
    'd',
    'b',
    'c',
    'aa',
  ], array_keys($this->localeProjectRepository
    ->getAll()));
  // Pretend there is a container rebuild by generating a new
  // LocaleProjectRepository object with the same data.
  $this->localeProjectRepository = new LocaleProjectRepository($this->cache, $this->keyValueMemoryFactory, $this->moduleHandler, $this->moduleExtensionList, $this->themeExtensionList, $this->config);
  $z = new LocaleTranslatableProject('z', 'z', 'z', 'z', 'z');
  $this->localeProjectRepository
    ->set($z);
  $this->assertSame([
    'e',
    'd',
    'b',
    'c',
    'z',
    'aa',
  ], array_keys($this->localeProjectRepository
    ->getAll()));
  // Now delete all projects.
  $this->localeProjectRepository
    ->deleteAll();
  // Add project 'z' before project 'a' and confirm 'a' is first.
  $this->localeProjectRepository
    ->set($z);
  $this->localeProjectRepository
    ->set($a);
  $this->assertSame([
    'a',
    'z',
  ], array_keys($this->localeProjectRepository
    ->getAll()));
}

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