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

merge-requests/5398/head
Alex Pott 2023-11-14 22:46:13 +00:00
parent 8aa03123ec
commit b7540532dc
No known key found for this signature in database
GPG Key ID: BDA67E7EE836E5CE
2 changed files with 52 additions and 0 deletions

View File

@ -136,6 +136,14 @@ abstract class ConfigFormBase extends FormBase {
$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);

View File

@ -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 @@ use Prophecy\Argument;
*/
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