Issue #3308449 by owenbush: CategorizingPluginManagerTrait::getSortedDefinitions() sometimes returns plugins in the incorrect order if they have categories or labels translated

merge-requests/2817/merge
Alex Pott 2022-10-04 10:48:27 +01:00
parent 243783522d
commit d3cf1e9277
No known key found for this signature in database
GPG Key ID: BDA67E7EE836E5CE
2 changed files with 13 additions and 9 deletions

View File

@ -91,7 +91,7 @@ trait CategorizingPluginManagerTrait {
/** @var \Drupal\Core\Plugin\CategorizingPluginManagerTrait|\Drupal\Component\Plugin\PluginManagerInterface $this */ /** @var \Drupal\Core\Plugin\CategorizingPluginManagerTrait|\Drupal\Component\Plugin\PluginManagerInterface $this */
$definitions = $definitions ?? $this->getDefinitions(); $definitions = $definitions ?? $this->getDefinitions();
uasort($definitions, function ($a, $b) use ($label_key) { uasort($definitions, function ($a, $b) use ($label_key) {
if ($a['category'] != $b['category']) { if ((string) $a['category'] != (string) $b['category']) {
return strnatcasecmp($a['category'], $b['category']); return strnatcasecmp($a['category'], $b['category']);
} }
return strnatcasecmp($a[$label_key], $b[$label_key]); return strnatcasecmp($a[$label_key], $b[$label_key]);

View File

@ -11,6 +11,7 @@ use Drupal\Core\Session\AccountInterface;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/** /**
* @coversDefaultClass \Drupal\Core\Block\BlockManager * @coversDefaultClass \Drupal\Core\Block\BlockManager
@ -19,6 +20,8 @@ use Drupal\Core\DependencyInjection\ContainerBuilder;
*/ */
class BlockManagerTest extends UnitTestCase { class BlockManagerTest extends UnitTestCase {
use StringTranslationTrait;
/** /**
* The block manager under test. * The block manager under test.
* *
@ -42,6 +45,7 @@ class BlockManagerTest extends UnitTestCase {
$container = new ContainerBuilder(); $container = new ContainerBuilder();
$current_user = $this->prophesize(AccountInterface::class); $current_user = $this->prophesize(AccountInterface::class);
$container->set('current_user', $current_user->reveal()); $container->set('current_user', $current_user->reveal());
$container->set('string_translation', $this->getStringTranslationStub());
\Drupal::setContainer($container); \Drupal::setContainer($container);
$cache_backend = $this->prophesize(CacheBackendInterface::class); $cache_backend = $this->prophesize(CacheBackendInterface::class);
@ -55,22 +59,22 @@ class BlockManagerTest extends UnitTestCase {
// that are purposefully not in alphabetical order. // that are purposefully not in alphabetical order.
$discovery->getDefinitions()->willReturn([ $discovery->getDefinitions()->willReturn([
'broken' => [ 'broken' => [
'admin_label' => 'Broken/Missing', 'admin_label' => $this->t('Broken/Missing'),
'category' => 'Block', 'category' => $this->t('Block'),
'class' => Broken::class, 'class' => Broken::class,
'provider' => 'core', 'provider' => 'core',
], ],
'block1' => [ 'block1' => [
'admin_label' => 'Coconut', 'admin_label' => $this->t('Coconut'),
'category' => 'Group 2', 'category' => $this->t('Group 2'),
], ],
'block2' => [ 'block2' => [
'admin_label' => 'Apple', 'admin_label' => $this->t('Apple'),
'category' => 'Group 1', 'category' => $this->t('Group 1'),
], ],
'block3' => [ 'block3' => [
'admin_label' => 'Banana', 'admin_label' => $this->t('Banana'),
'category' => 'Group 2', 'category' => $this->t('Group 2'),
], ],
]); ]);
// Force the discovery object onto the block manager. // Force the discovery object onto the block manager.