From 4cfa1b5ffb15e6f77dd62979978e729a8e6cb35d Mon Sep 17 00:00:00 2001 From: Dave Long <dave@longwaveconsulting.com> Date: Tue, 17 Oct 2023 11:39:26 +0200 Subject: [PATCH] Issue #3392396 by andypost, longwave: Improve AutowireTest to ignore TrustedCallbackInterface --- .../announcements_feed.services.yml | 1 + core/modules/comment/comment.services.yml | 1 + core/modules/editor/editor.services.yml | 1 + core/modules/shortcut/shortcut.services.yml | 1 + .../autowire_test/autowire_test.services.yml | 3 +++ .../autowire_test/src/TestInjection3.php | 19 +++++++++++++++++++ .../modules/autowire_test/src/TestService.php | 6 +++++- core/modules/user/user.services.yml | 1 + .../Core/DependencyInjection/AutowireTest.php | 7 +++++++ 9 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 core/modules/system/tests/modules/autowire_test/src/TestInjection3.php diff --git a/core/modules/announcements_feed/announcements_feed.services.yml b/core/modules/announcements_feed/announcements_feed.services.yml index 408837cdee07..d6cc2d80198d 100644 --- a/core/modules/announcements_feed/announcements_feed.services.yml +++ b/core/modules/announcements_feed/announcements_feed.services.yml @@ -14,3 +14,4 @@ services: announcements_feed.lazy_builders: class: Drupal\announcements_feed\LazyBuilders arguments: [ '@plugin.manager.element_info'] + Drupal\announcements_feed\LazyBuilders: '@announcements_feed.lazy_builders' diff --git a/core/modules/comment/comment.services.yml b/core/modules/comment/comment.services.yml index f356ea1b7ab7..770d4ab28782 100644 --- a/core/modules/comment/comment.services.yml +++ b/core/modules/comment/comment.services.yml @@ -20,6 +20,7 @@ services: comment.lazy_builders: class: Drupal\comment\CommentLazyBuilders arguments: ['@entity_type.manager', '@entity.form_builder', '@current_user', '@comment.manager', '@module_handler', '@renderer'] + Drupal\comment\CommentLazyBuilders: '@comment.lazy_builders' comment.link_builder: class: Drupal\comment\CommentLinkBuilder diff --git a/core/modules/editor/editor.services.yml b/core/modules/editor/editor.services.yml index 1a60125bbcc0..31d4027b1d9f 100644 --- a/core/modules/editor/editor.services.yml +++ b/core/modules/editor/editor.services.yml @@ -5,6 +5,7 @@ services: element.editor: class: Drupal\editor\Element arguments: ['@plugin.manager.editor'] + Drupal\editor\Element: '@element.editor' editor.config_translation_mapper_subscriber: class: Drupal\editor\EventSubscriber\EditorConfigTranslationSubscriber arguments: ['@config.factory'] diff --git a/core/modules/shortcut/shortcut.services.yml b/core/modules/shortcut/shortcut.services.yml index 49244aa440ef..b4a6a9ca1822 100644 --- a/core/modules/shortcut/shortcut.services.yml +++ b/core/modules/shortcut/shortcut.services.yml @@ -2,3 +2,4 @@ services: shortcut.lazy_builders: class: Drupal\shortcut\ShortcutLazyBuilders arguments: ['@renderer'] + Drupal\shortcut\ShortcutLazyBuilders: '@shortcut.lazy_builders' diff --git a/core/modules/system/tests/modules/autowire_test/autowire_test.services.yml b/core/modules/system/tests/modules/autowire_test/autowire_test.services.yml index 090db9679cfa..66fe06d15a46 100644 --- a/core/modules/system/tests/modules/autowire_test/autowire_test.services.yml +++ b/core/modules/system/tests/modules/autowire_test/autowire_test.services.yml @@ -6,6 +6,8 @@ services: public: false Drupal\autowire_test\TestInjection2: public: false + Drupal\autowire_test\TestInjection3: + public: false # An alias that specifies which service to use by default for arguments that # type-hint to the interface. @@ -14,6 +16,7 @@ services: Drupal\autowire_test\TestInjectionInterface: alias: 'Drupal\autowire_test\TestInjection' public: false + Drupal\autowire_test\TestInjectionInterface $testInjection3: '@Drupal\autowire_test\TestInjection3' # A service that tests autowiring for four constructor arguments: # - One type-hinted to TestInjectionInterface. diff --git a/core/modules/system/tests/modules/autowire_test/src/TestInjection3.php b/core/modules/system/tests/modules/autowire_test/src/TestInjection3.php new file mode 100644 index 000000000000..c43b002b20a9 --- /dev/null +++ b/core/modules/system/tests/modules/autowire_test/src/TestInjection3.php @@ -0,0 +1,19 @@ +<?php + +namespace Drupal\autowire_test; + +use Drupal\Core\Security\TrustedCallbackInterface; + +/** + * A service that is autowired. + */ +class TestInjection3 implements TrustedCallbackInterface, TestInjectionInterface { + + /** + * {@inheritdoc} + */ + public static function trustedCallbacks() { + return []; + } + +} diff --git a/core/modules/system/tests/modules/autowire_test/src/TestService.php b/core/modules/system/tests/modules/autowire_test/src/TestService.php index ad818b9fc525..415eb9af9711 100644 --- a/core/modules/system/tests/modules/autowire_test/src/TestService.php +++ b/core/modules/system/tests/modules/autowire_test/src/TestService.php @@ -27,7 +27,7 @@ class TestService { */ protected $kernel; - public function __construct(TestInjectionInterface $test_injection, TestInjection2 $test_injection2, Connection $database, DrupalKernelInterface $kernel) { + public function __construct(TestInjectionInterface $test_injection, TestInjection2 $test_injection2, Connection $database, DrupalKernelInterface $kernel, protected TestInjectionInterface $testInjection3) { $this->testInjection = $test_injection; $this->testInjection2 = $test_injection2; $this->database = $database; @@ -42,6 +42,10 @@ public function getTestInjection2(): TestInjection2 { return $this->testInjection2; } + public function getTestInjection3(): TestInjection3 { + return $this->testInjection3; + } + public function getDatabase(): Connection { return $this->database; } diff --git a/core/modules/user/user.services.yml b/core/modules/user/user.services.yml index 8aeee3fabf49..d2d2720bd18e 100644 --- a/core/modules/user/user.services.yml +++ b/core/modules/user/user.services.yml @@ -63,6 +63,7 @@ services: user.toolbar_link_builder: class: Drupal\user\ToolbarLinkBuilder arguments: ['@current_user'] + Drupal\user\ToolbarLinkBuilder: '@user.toolbar_link_builder' user.flood_control: class: Drupal\user\UserFloodControl arguments: ['@flood', '@event_dispatcher', '@request_stack'] diff --git a/core/tests/Drupal/KernelTests/Core/DependencyInjection/AutowireTest.php b/core/tests/Drupal/KernelTests/Core/DependencyInjection/AutowireTest.php index 89c2c05a06f1..0ba240384537 100644 --- a/core/tests/Drupal/KernelTests/Core/DependencyInjection/AutowireTest.php +++ b/core/tests/Drupal/KernelTests/Core/DependencyInjection/AutowireTest.php @@ -4,9 +4,11 @@ use Drupal\autowire_test\TestInjection; use Drupal\autowire_test\TestInjection2; +use Drupal\autowire_test\TestInjection3; use Drupal\autowire_test\TestService; use Drupal\Core\Database\Connection; use Drupal\Core\DrupalKernelInterface; +use Drupal\Core\Security\TrustedCallbackInterface; use Drupal\Core\Serialization\Yaml; use Drupal\KernelTests\FileSystemModuleDiscoveryDataProviderTrait; use Drupal\KernelTests\KernelTestBase; @@ -33,6 +35,7 @@ public function testAutowire(): void { // Ensure an autowired interface works. $this->assertInstanceOf(TestInjection::class, $service->getTestInjection()); + $this->assertInstanceOf(TestInjection3::class, $service->getTestInjection3()); // Ensure an autowired class works. $this->assertInstanceOf(TestInjection2::class, $service->getTestInjection2()); // Ensure an autowired core class works. @@ -100,6 +103,10 @@ public function testCoreServiceAliases(): void { if (!$implements) { $expected[$class] = $id; } + elseif (count($implements) === 1 && TrustedCallbackInterface::class === reset($implements)) { + // Classes implementing only TrustedCallbackInterface should be aliased. + $expected[$class] = $id; + } // Expect classes that are the only implementation of their interface to // be aliased. -- GitLab