Verified Commit 23141ce2 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3401186 by Wim Leers, alexpott: Follow-up for #3382510: Throw...

Issue #3401186 by Wim Leers, alexpott: Follow-up for #3382510: Throw \LogicException when >1 #config_target in the same form targets the same property path

(cherry picked from commit 40084a52b0329dd9ce70e9d992a90cb64e4924d2)
parent 1221e7e5
Loading
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -136,6 +136,14 @@ public function storeConfigKeyToFormElementMap(array $element, FormStateInterfac
        $target = ConfigTarget::fromString($target);
      }
      foreach ($target->propertyPaths as $property_path) {
        if (isset($map[$target->configName][$property_path])) {
          throw new \LogicException(sprintf('Two #config_targets both target "%s" in the "%s" config: `%s` and `%s`.',
            $property_path,
            $target->configName,
            '$form[\'' . implode("']['", $map[$target->configName][$property_path]) . '\']',
            '$form[\'' . implode("']['", $element['#array_parents']) . '\']',
          ));
        }
        $map[$target->configName][$property_path] = $element['#array_parents'];
      }
      $form_state->set(static::CONFIG_KEY_TO_FORM_ELEMENT_MAP, $map);
+44 −0
Original line number Diff line number Diff line
@@ -3,9 +3,14 @@
namespace Drupal\Tests\Core\Form;

use Drupal\Core\Config\Config;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\ConfigTarget;
use Drupal\Core\Form\ToConfig;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\RedundantEditableConfigNamesTrait;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;

@@ -15,6 +20,45 @@
 */
class ConfigTargetTest extends UnitTestCase {

  /**
   * @covers \Drupal\Core\Form\ConfigFormBase::storeConfigKeyToFormElementMap
   */
  public function testDuplicateTargetsNotAllowed(): void {
    $form = [
      'test' => [
        '#type' => 'text',
        '#default_value' => 'A test',
        '#config_target' => new ConfigTarget('system.site', 'admin_compact_mode', 'intval', 'boolval'),
        '#name' => 'test',
        '#array_parents' => ['test'],
      ],
      'duplicate' => [
        '#type' => 'text',
        '#config_target' => new ConfigTarget('system.site', 'admin_compact_mode', 'intval', 'boolval'),
        '#name' => 'duplicate',
        '#array_parents' => ['duplicate'],
      ],
    ];

    $test_form = new class(
      $this->prophesize(ConfigFactoryInterface::class)->reveal(),
      $this->prophesize(TypedConfigManagerInterface::class)->reveal(),
    ) extends ConfigFormBase {
      use RedundantEditableConfigNamesTrait;

      public function getFormId() {
        return 'test';
      }

    };
    $form_state = new FormState();
    $test_form->storeConfigKeyToFormElementMap($form['test'], $form_state);

    $this->expectException(\LogicException::class);
    $this->expectExceptionMessage('Two #config_targets both target "admin_compact_mode" in the "system.site" config: `$form[\'test\']` and `$form[\'duplicate\']`.');
    $test_form->storeConfigKeyToFormElementMap($form['duplicate'], $form_state);
  }

  /**
   * @covers ::fromForm
   * @covers ::fromString