Skip to content
Snippets Groups Projects
Unverified Commit 25e67bc3 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3482783 by phenaproxima, alexpott: Consider making the cloneAs config...

Issue #3482783 by phenaproxima, alexpott: Consider making the cloneAs config action optionally fail if the clone already exists

(cherry picked from commit 3bc3652d)
parent a64662a3
Branches
Tags
2 merge requests!11769Issue #3517987: Add option to contextual filters to encode slashes in query parameter.,!11185Issue #3477324 by andypost, alexpott: Fix usage of str_getcsv() and fgetcsv() for PHP 8.4
Pipeline #322005 passed with warnings
Pipeline: drupal

#322033

    Pipeline: drupal

    #322032

      Pipeline: drupal

      #322031

        +7
        ......@@ -42,8 +42,14 @@ public static function create(ContainerInterface $container, array $configuratio
        /**
        * {@inheritdoc}
        */
        public function apply(string $configName, mixed $duplicate_id): void {
        assert(is_string($duplicate_id));
        public function apply(string $configName, mixed $value): void {
        if (!is_array($value)) {
        $value = ['id' => $value];
        }
        assert(is_string($value['id']));
        $value += ['fail_if_exists' => FALSE];
        assert(is_bool($value['fail_if_exists']));
        // If the original doesn't exist, there's nothing to clone.
        $original = $this->configManager->loadConfigEntityByName($configName);
        ......@@ -51,11 +57,12 @@ public function apply(string $configName, mixed $duplicate_id): void {
        throw new ConfigActionException("Cannot clone '$configName' because it does not exist.");
        }
        $clone = $original->createDuplicate();
        $clone->set($original->getEntityType()->getKey('id'), $duplicate_id);
        $clone->set($original->getEntityType()->getKey('id'), $value['id']);
        // Use the config action manager to invoke the `entity_create` action on
        // the clone, so that it will be validated.
        $this->configActionManager->applyAction('entity_create:createIfNotExists', $clone->getConfigDependencyName(), $clone->toArray());
        $create_action = 'entity_create:' . ($value['fail_if_exists'] ? 'create' : 'createIfNotExists');
        // Use the config action manager to invoke the create action on the clone,
        // so that it will be validated.
        $this->configActionManager->applyAction($create_action, $clone->getConfigDependencyName(), $clone->toArray());
        }
        }
        ......@@ -73,6 +73,25 @@ public function testNoErrorWithExistingEntity(): void {
        $this->assertFalse($clone->hasPermission('access user profiles'));
        }
        /**
        * Tests that the action can be configured to fail if the clone exists.
        */
        public function testFailIfEntityExists(): void {
        $this->container->get('plugin.manager.config_action')
        ->applyAction('cloneAs', 'user.role.test', [
        'id' => 'cloned',
        'fail_if_exists' => TRUE,
        ]);
        $this->expectException(ConfigActionException::class);
        $this->expectExceptionMessage('Entity user.role.cloned exists');
        $this->container->get('plugin.manager.config_action')
        ->applyAction('cloneAs', 'user.role.test', [
        'id' => 'cloned',
        'fail_if_exists' => TRUE,
        ]);
        }
        /**
        * Tests cloning entity displays, which have specialized logic for that.
        */
        ......
        0% Loading or .
        You are about to add 0 people to the discussion. Proceed with caution.
        Please register or to comment