Verified Commit 5fe48edb authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3302833 by akhil babu, b_sharpe, alexpott, oily, smustgrave: Improve...

Issue #3302833 by akhil babu, b_sharpe, alexpott, oily, smustgrave: Improve PluginNotFound exception to include possible shorthand action IDs

(cherry picked from commit e573df5f)
parent 97d929b6
Loading
Loading
Loading
Loading
Loading
+15 −2
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@

namespace Drupal\Core\Config\Action;

use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\Action\Attribute\ConfigAction;
@@ -137,8 +138,20 @@ public function applyAction(string $action_id, string $configName, mixed $data):
        $action_id = $this->getShorthandActionIdsForEntityType($entity_type)[$action_id] ?? $action_id;
      }
    }
    try {
      /** @var \Drupal\Core\Config\Action\ConfigActionPluginInterface $action */
      $action = $this->createInstance($action_id);
    }
    catch (PluginNotFoundException $e) {
      $entity_type = $this->configManager->getEntityTypeIdByName($configName);
      if ($entity_type) {
        $action_ids = $this->getShorthandActionIdsForEntityType($entity_type);
        $valid_ids = implode(', ', array_keys($action_ids));
        throw new PluginNotFoundException($action_id, sprintf('The "%s" entity does not support the "%s" config action. Valid config actions for %s are: %s', $entity_type, $action_id, $entity_type, $valid_ids));
      }
      throw $e;
    }

    foreach ($this->getConfigNamesMatchingExpression($configName) as $name) {
      $action->apply($name, $data);
      $typed_config = $this->typedConfig->createFromNameAndData($name, $this->configFactory->get($name)->getRawData());
+1 −1
Original line number Diff line number Diff line
@@ -76,7 +76,7 @@ public function testWorkflowMustBeContentModeration(): void {
  public function testActionOnlyTargetsWorkflows(): void {
    $recipe = $this->createRecipe('user.role.anonymous');
    $this->expectException(PluginNotFoundException::class);
    $this->expectExceptionMessage('The "addNodeTypes" plugin does not exist.');
    $this->expectExceptionMessage('The "user_role" entity does not support the "addNodeTypes" config action.');
    RecipeRunner::processRecipe($recipe);
  }

+1 −1
Original line number Diff line number Diff line
@@ -74,7 +74,7 @@ public function testInstantiateNewFieldOnAllBundles(): void {
    // Expect an error when the 'addToAllBundles' action is invoked on anything
    // other than a field storage config entity.
    $this->expectException(PluginNotFoundException::class);
    $this->expectExceptionMessage('The "addToAllBundles" plugin does not exist.');
    $this->expectExceptionMessage('The "user_role" entity does not support the "addToAllBundles" config action.');
    $this->applyAction('user.role.anonymous');
  }

+1 −1
Original line number Diff line number Diff line
@@ -124,7 +124,7 @@ public function testActionIsOnlyAvailableToUserRoles(): void {
YAML;

    $this->expectException(PluginNotFoundException::class);
    $this->expectExceptionMessage('The "grantPermissionsForEachNodeType" plugin does not exist.');
    $this->expectExceptionMessage('The "field_storage_config" entity does not support the "grantPermissionsForEachNodeType" config action.');
    $this->applyRecipeFromString($recipe_data);
  }

+18 −1
Original line number Diff line number Diff line
@@ -203,6 +203,23 @@ public function testConfigActionsPreExistingConfig() :void {
  public function testInvalidConfigAction() :void {
    $recipe_data = <<<YAML
name: Invalid config action
install:
  - config_test
config:
  actions:
    config_test.system:
      setFoo: 'Bar'
YAML;

    $recipe = $this->createRecipe($recipe_data);
    $this->expectException(PluginNotFoundException::class);
    $this->expectExceptionMessage('The "setFoo" plugin does not exist.');
    RecipeRunner::processRecipe($recipe);
  }

  public function testInvalidConfigActionAppliedOnConfigEntity() :void {
    $recipe_data = <<<YAML
name: Invalid config action
install:
  - config_test
config:
@@ -215,7 +232,7 @@ public function testInvalidConfigAction() :void {

    $recipe = $this->createRecipe($recipe_data);
    $this->expectException(PluginNotFoundException::class);
    $this->expectExceptionMessage('The "setBody" plugin does not exist.');
    $this->expectExceptionMessage('The "config_test" entity does not support the "setBody" config action.');
    RecipeRunner::processRecipe($recipe);
  }