Issue #2614066 by webflo: Machine name element leaks to much data to drupalSetting

8.1.x
Nathaniel Catchpole 2015-11-25 13:07:45 +00:00
parent 7ec6ad0ea4
commit 30471f550d
2 changed files with 87 additions and 3 deletions

View File

@ -190,7 +190,17 @@ class MachineName extends Textfield {
}
$element['#attached']['library'][] = 'core/drupal.machine-name';
$element['#attached']['drupalSettings']['machineName']['#' . $source['#id']] = $element['#machine_name'];
$options = [
'replace_pattern',
'replace',
'maxlength',
'target',
'label',
'field_prefix',
'field_suffix',
'suffix',
];
$element['#attached']['drupalSettings']['machineName']['#' . $source['#id']] = array_intersect_key($element['#machine_name'], array_flip($options));
$element['#attached']['drupalSettings']['langcode'] = $language->getId();
return $element;

View File

@ -5,11 +5,15 @@
* Contains \Drupal\Tests\Core\Render\Element\MachineNameTest.
*/
namespace Drupal\Tests\Core\Render\Element;
namespace Drupal\Tests\Core\Render\Element {
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Render\Element\MachineName;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* @coversDefaultClass \Drupal\Core\Render\Element\MachineName
@ -42,4 +46,74 @@ class MachineNameTest extends UnitTestCase {
return $data;
}
/**
* @covers ::processMachineName
*/
public function testProcessMachineName() {
$form_state = new FormState();
$element = [
'#id' => 'test',
'#field_suffix' => 'test_suffix',
'#field_prefix' => 'test_prefix',
'#machine_name' => [
'source' => [
'test_source',
],
'maxlength' => 32,
'additional_property' => TRUE,
'#additional_property_with_hash' => TRUE,
]
];
$complete_form = [
'test_source' => [
'#type' => 'textfield',
'#id' => 'source',
],
'test_machine_name' => $element
];
$form_state->setCompleteForm($complete_form);
$language = $this->prophesize(LanguageInterface::class);
$language->getId()->willReturn('xx-lolspeak');
$language_manager = $this->prophesize(LanguageManagerInterface::class);
$language_manager->getCurrentLanguage()->willReturn($language);
$container = $this->prophesize(ContainerInterface::class);
$container->get('language_manager')->willReturn($language_manager->reveal());
\Drupal::setContainer($container->reveal());
$element = MachineName::processMachineName($element, $form_state, $complete_form);
$settings = $element['#attached']['drupalSettings']['machineName']['#source'];
$allowed_options = [
'replace_pattern',
'replace',
'maxlength',
'target',
'label',
'field_prefix',
'field_suffix',
'suffix'
];
$this->assertEmpty(array_diff_key($settings, array_flip($allowed_options)));
foreach ($allowed_options as $key) {
$this->assertArrayHasKey($key, $settings);
}
}
}
}
namespace {
if (!function_exists('t')) {
function t($string, array $args = []) {
return strtr($string, $args);
}
}
}