Skip to content
Snippets Groups Projects
Verified Commit d7602652 authored by Dave Long's avatar Dave Long
Browse files

Revert "Issue #3403337 by alexpott, quietone, Akhil Babu, vakulrai,...

Revert "Issue #3403337 by alexpott, quietone, Akhil Babu, vakulrai, smustgrave, penyaskito, Gábor Hojtsy: The order of the projects coming from locale_translation_get_projects() is not consistent"

This reverts commit 36fece71.
parent c5c85f18
No related branches found
No related tags found
No related merge requests found
......@@ -120,9 +120,6 @@ function hook_locale_translation_projects_alter(&$projects) {
'info' => [
'interface translation server pattern' => 'http://example.com/files/translations/%core/%project/%project-%version.%language.po',
],
// An optional key to change the order in which translation files are
// processed. By default, the projects are sorted alphabetically by key.
'weight' => 1,
];
}
......
......@@ -28,14 +28,7 @@ class LocaleProjectStorage implements LocaleProjectStorageInterface {
*
* @var bool
*/
protected bool $all = FALSE;
/**
* Sorted status flag.
*
* @var bool
*/
protected bool $sorted = FALSE;
protected static $all = FALSE;
/**
* Constructs a State object.
......@@ -105,7 +98,6 @@ public function setMultiple(array $data) {
$this->cache[$key] = $value;
}
$this->keyValueStore->setMultiple($data);
$this->sorted = FALSE;
}
/**
......@@ -130,7 +122,7 @@ public function deleteMultiple(array $keys) {
*/
public function resetCache() {
$this->cache = [];
$this->sorted = $this->all = FALSE;
static::$all = FALSE;
}
/**
......@@ -167,23 +159,11 @@ public function countProjects() {
* {@inheritdoc}
*/
public function getAll() {
if (!$this->all) {
if (!static::$all) {
$this->cache = $this->keyValueStore->getAll();
$this->all = TRUE;
}
if (!$this->sorted) {
uksort($this->cache, function ($a, $b) {
// Sort by weight, if available, and then by key. This allows locale
// projects to set a weight, if required, and keeps the order consistent
// regardless of whether the list is built from code or retrieved from
// the database.
$sort = ($this->cache[$a]['weight'] ?? 0) <=> ($this->cache[$b]['weight'] ?? 0);
return $sort === 0 ? $a <=> $b : $sort;
});
$this->sorted = TRUE;
static::$all = TRUE;
}
// Remove any NULL values as these are not valid projects.
return array_filter($this->cache, fn ($value) => $value !== NULL);
return $this->cache;
}
}
<?php
declare(strict_types=1);
namespace Drupal\Tests\locale\Unit;
use Drupal\Core\KeyValueStore\KeyValueMemoryFactory;
use Drupal\locale\LocaleProjectStorage;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\locale\LocaleProjectStorage
* @group locale
* @runTestsInSeparateProcesses
*/
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());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment