From 618aeb609d4db39d530067aa8a64119fc7bc87ed Mon Sep 17 00:00:00 2001 From: catch <6915-catch@users.noreply.drupalcode.org> Date: Sat, 3 Aug 2024 07:40:06 +0900 Subject: [PATCH] Issue #3458177 by mondrake, catch, quietone, godotislate, longwave, larowlan, xjm: Changing plugins from annotations to attributes in contrib leads to error if plugin extends from a missing dependency --- core/phpstan.neon.dist | 1 - .../Attribute/AttributeClassDiscoveryCachedTest.php | 6 +++--- .../Plugin/Attribute/AttributeClassDiscoveryTest.php | 12 +++++------- .../Discovery/AttributeBridgeDecoratorTest.php | 8 ++++---- .../Fixtures => fixtures/plugins}/CustomPlugin.php | 0 .../PluginNamespace/AttributeDiscoveryTest1.php | 0 .../PluginNamespace/AttributeDiscoveryTest2.php | 0 7 files changed, 12 insertions(+), 15 deletions(-) rename core/tests/{Drupal/Tests/Component/Plugin/Attribute/Fixtures => fixtures/plugins}/CustomPlugin.php (100%) rename core/tests/{Drupal/Tests/Component/Plugin/Attribute/Fixtures/Plugins => fixtures/plugins/Plugin}/PluginNamespace/AttributeDiscoveryTest1.php (100%) rename core/tests/{Drupal/Tests/Component/Plugin/Attribute/Fixtures/Plugins => fixtures/plugins/Plugin}/PluginNamespace/AttributeDiscoveryTest2.php (100%) diff --git a/core/phpstan.neon.dist b/core/phpstan.neon.dist index f7e1f0e2932..7356bcddb91 100644 --- a/core/phpstan.neon.dist +++ b/core/phpstan.neon.dist @@ -41,7 +41,6 @@ parameters: - modules/system/tests/modules/plugin_test/src/Plugin/plugin_test/fruit/ExtendingNonInstalledClass.php - modules/system/tests/modules/plugin_test/src/Plugin/plugin_test/custom_annotation/UsingNonInstalledTraitClass.php - modules/system/tests/modules/plugin_test/src/Plugin/plugin_test/custom_annotation/ExtendingNonInstalledClass.php - - tests/Drupal/Tests/Component/Plugin/Attribute/Fixtures/Plugins/PluginNamespace/AttributeDiscoveryTest2.php ignoreErrors: # new static() is a best practice in Drupal, so we cannot fix that. diff --git a/core/tests/Drupal/Tests/Component/Plugin/Attribute/AttributeClassDiscoveryCachedTest.php b/core/tests/Drupal/Tests/Component/Plugin/Attribute/AttributeClassDiscoveryCachedTest.php index 2b2f0c25abd..b3d11614cce 100644 --- a/core/tests/Drupal/Tests/Component/Plugin/Attribute/AttributeClassDiscoveryCachedTest.php +++ b/core/tests/Drupal/Tests/Component/Plugin/Attribute/AttributeClassDiscoveryCachedTest.php @@ -29,10 +29,10 @@ class AttributeClassDiscoveryCachedTest extends TestCase { FileCacheFactory::setPrefix('prefix'); // Normally the attribute classes would be autoloaded. - include_once __DIR__ . '/Fixtures/CustomPlugin.php'; + include_once __DIR__ . '/../../../../../fixtures/plugins/CustomPlugin.php'; $additionalClassLoader = new ClassLoader(); - $additionalClassLoader->addPsr4("com\\example\\PluginNamespace\\", __DIR__ . "/Fixtures/Plugins/PluginNamespace"); + $additionalClassLoader->addPsr4("com\\example\\PluginNamespace\\", __DIR__ . "/../../../../../fixtures/plugins/Plugin/PluginNamespace"); $additionalClassLoader->register(TRUE); } @@ -43,7 +43,7 @@ class AttributeClassDiscoveryCachedTest extends TestCase { */ public function testGetDefinitions(): void { // Path to the classes which we'll discover and parse annotation. - $discovery_path = __DIR__ . '/Fixtures/Plugins'; + $discovery_path = __DIR__ . "/../../../../../fixtures/plugins/Plugin"; // File path that should be discovered within that directory. $file_path = $discovery_path . '/PluginNamespace/AttributeDiscoveryTest1.php'; // Define a file path within the directory that should not be discovered. diff --git a/core/tests/Drupal/Tests/Component/Plugin/Attribute/AttributeClassDiscoveryTest.php b/core/tests/Drupal/Tests/Component/Plugin/Attribute/AttributeClassDiscoveryTest.php index 727405f4b90..aa20f8146f8 100644 --- a/core/tests/Drupal/Tests/Component/Plugin/Attribute/AttributeClassDiscoveryTest.php +++ b/core/tests/Drupal/Tests/Component/Plugin/Attribute/AttributeClassDiscoveryTest.php @@ -8,8 +8,6 @@ use Composer\Autoload\ClassLoader; use Drupal\Component\Plugin\Discovery\AttributeClassDiscovery; use Drupal\Component\FileCache\FileCacheFactory; use PHPUnit\Framework\TestCase; -use com\example\PluginNamespace\CustomPlugin; -use com\example\PluginNamespace\CustomPlugin2; /** * @coversDefaultClass \Drupal\Component\Plugin\Discovery\AttributeClassDiscovery @@ -30,10 +28,10 @@ class AttributeClassDiscoveryTest extends TestCase { FileCacheFactory::setPrefix('prefix'); // Normally the attribute classes would be autoloaded. - include_once __DIR__ . '/Fixtures/CustomPlugin.php'; + include_once __DIR__ . '/../../../../../fixtures/plugins/CustomPlugin.php'; $additionalClassLoader = new ClassLoader(); - $additionalClassLoader->addPsr4("com\\example\\PluginNamespace\\", __DIR__ . "/Fixtures/Plugins/PluginNamespace"); + $additionalClassLoader->addPsr4("com\\example\\PluginNamespace\\", __DIR__ . "/../../../../../fixtures/plugins/Plugin/PluginNamespace"); $additionalClassLoader->register(TRUE); } @@ -57,7 +55,7 @@ class AttributeClassDiscoveryTest extends TestCase { * @covers ::prepareAttributeDefinition */ public function testGetDefinitions(): void { - $discovery = new AttributeClassDiscovery(['com\example' => [__DIR__ . '/Fixtures/Plugins']]); + $discovery = new AttributeClassDiscovery(['com\example' => [__DIR__ . "/../../../../../fixtures/plugins/Plugin"]]); $this->assertEquals([ 'discovery_test_1' => [ 'id' => 'discovery_test_1', @@ -65,7 +63,7 @@ class AttributeClassDiscoveryTest extends TestCase { ], ], $discovery->getDefinitions()); - $custom_annotation_discovery = new AttributeClassDiscovery(['com\example' => [__DIR__ . '/Fixtures/Plugins']], CustomPlugin::class); + $custom_annotation_discovery = new AttributeClassDiscovery(['com\example' => [__DIR__ . "/../../../../../fixtures/plugins/Plugin"]], 'com\example\PluginNamespace\CustomPlugin'); $this->assertEquals([ 'discovery_test_1' => [ 'id' => 'discovery_test_1', @@ -74,7 +72,7 @@ class AttributeClassDiscoveryTest extends TestCase { ], ], $custom_annotation_discovery->getDefinitions()); - $empty_discovery = new AttributeClassDiscovery(['com\example' => [__DIR__ . '/Fixtures/Plugins']], CustomPlugin2::class); + $empty_discovery = new AttributeClassDiscovery(['com\example' => [__DIR__ . "/../../../../../fixtures/plugins/Plugin"]], 'com\example\PluginNamespace\CustomPlugin2'); $this->assertEquals([], $empty_discovery->getDefinitions()); } diff --git a/core/tests/Drupal/Tests/Component/Plugin/Discovery/AttributeBridgeDecoratorTest.php b/core/tests/Drupal/Tests/Component/Plugin/Discovery/AttributeBridgeDecoratorTest.php index d2deaa5ffb2..0772e684541 100644 --- a/core/tests/Drupal/Tests/Component/Plugin/Discovery/AttributeBridgeDecoratorTest.php +++ b/core/tests/Drupal/Tests/Component/Plugin/Discovery/AttributeBridgeDecoratorTest.php @@ -21,8 +21,8 @@ class AttributeBridgeDecoratorTest extends TestCase { */ public function testGetDefinitions(): void { // Normally the attribute classes would be autoloaded. - include_once __DIR__ . '/../Attribute/Fixtures/CustomPlugin.php'; - include_once __DIR__ . '/../Attribute/Fixtures/Plugins/PluginNamespace/AttributeDiscoveryTest1.php'; + include_once __DIR__ . '/../../../../../fixtures/plugins/CustomPlugin.php'; + include_once __DIR__ . '/../../../../../fixtures/plugins/Plugin/PluginNamespace/AttributeDiscoveryTest1.php'; $definitions = []; $definitions['object'] = new ObjectDefinition(['id' => 'foo']); @@ -51,8 +51,8 @@ class AttributeBridgeDecoratorTest extends TestCase { */ public function testOtherMethod(): void { // Normally the attribute classes would be autoloaded. - include_once __DIR__ . '/../Attribute/Fixtures/CustomPlugin.php'; - include_once __DIR__ . '/../Attribute/Fixtures/Plugins/PluginNamespace/AttributeDiscoveryTest1.php'; + include_once __DIR__ . '/../../../../../fixtures/plugins/CustomPlugin.php'; + include_once __DIR__ . '/../../../../../fixtures/plugins/Plugin/PluginNamespace/AttributeDiscoveryTest1.php'; $discovery = $this->createMock(ExtendedDiscoveryInterface::class); $discovery->expects($this->exactly(2)) diff --git a/core/tests/Drupal/Tests/Component/Plugin/Attribute/Fixtures/CustomPlugin.php b/core/tests/fixtures/plugins/CustomPlugin.php similarity index 100% rename from core/tests/Drupal/Tests/Component/Plugin/Attribute/Fixtures/CustomPlugin.php rename to core/tests/fixtures/plugins/CustomPlugin.php diff --git a/core/tests/Drupal/Tests/Component/Plugin/Attribute/Fixtures/Plugins/PluginNamespace/AttributeDiscoveryTest1.php b/core/tests/fixtures/plugins/Plugin/PluginNamespace/AttributeDiscoveryTest1.php similarity index 100% rename from core/tests/Drupal/Tests/Component/Plugin/Attribute/Fixtures/Plugins/PluginNamespace/AttributeDiscoveryTest1.php rename to core/tests/fixtures/plugins/Plugin/PluginNamespace/AttributeDiscoveryTest1.php diff --git a/core/tests/Drupal/Tests/Component/Plugin/Attribute/Fixtures/Plugins/PluginNamespace/AttributeDiscoveryTest2.php b/core/tests/fixtures/plugins/Plugin/PluginNamespace/AttributeDiscoveryTest2.php similarity index 100% rename from core/tests/Drupal/Tests/Component/Plugin/Attribute/Fixtures/Plugins/PluginNamespace/AttributeDiscoveryTest2.php rename to core/tests/fixtures/plugins/Plugin/PluginNamespace/AttributeDiscoveryTest2.php