diff --git a/core/modules/config/src/Tests/ConfigEntityTest.php b/core/modules/config/src/Tests/ConfigEntityTest.php index 7cf16e6..1de0264 100644 --- a/core/modules/config/src/Tests/ConfigEntityTest.php +++ b/core/modules/config/src/Tests/ConfigEntityTest.php @@ -319,6 +319,44 @@ function testCRUDUI() { $this->drupalPostForm('admin/structure/config_test/manage/0/delete', array(), 'Delete'); $this->assertFalse(entity_load('config_test', '0'), 'Test entity deleted'); + // Create a configuration entity with a property that uses AJAX to show + // extra form elements. + $this->drupalGet('admin/structure/config_test/add'); + + // Test that the dependent element is not shown initially. + $this->assertFieldByName('size'); + $this->assertNoFieldByName('size_value'); + + $id = strtolower($this->randomMachineName()); + $edit = array( + 'id' => $id, + 'label' => $this->randomString(), + 'size' => 'custom', + ); + $this->drupalPostAjaxForm(NULL, $edit, 'size'); + + // Check that the dependent element is shown after selecting a 'size' value. + $this->assertFieldByName('size'); + $this->assertFieldByName('size_value'); + + // Test the same scenario but it in a non-js case by using a js-hidden + // submit button. + $this->drupalGet('admin/structure/config_test/add'); + $this->assertFieldByName('size'); + $this->assertNoFieldByName('size_value'); + + $this->drupalPostForm(NULL, $edit, 'Change size'); + $this->assertFieldByName('size'); + $this->assertFieldByName('size_value'); + + // Submit the form with the regular 'Save' button and check that the entity + // values are correct. + $edit += array('size_value' => 'medium'); + $this->drupalPostForm(NULL, $edit, 'Save'); + + $entity = entity_load('config_test', $id); + $this->assertEqual($entity->get('size'), 'custom'); + $this->assertEqual($entity->get('size_value'), 'medium'); } } diff --git a/core/modules/config/src/Tests/ConfigImportUITest.php b/core/modules/config/src/Tests/ConfigImportUITest.php index 9bc30e3..4f3aea5 100644 --- a/core/modules/config/src/Tests/ConfigImportUITest.php +++ b/core/modules/config/src/Tests/ConfigImportUITest.php @@ -68,6 +68,8 @@ function testImport() { 'label' => 'New', 'weight' => 0, 'style' => '', + 'size' => '', + 'size_value' => '', 'protected_property' => '', ); $staging->write($dynamic_name, $original_dynamic_data); @@ -373,7 +375,9 @@ function testImportErrorLog() { 'label' => 'Primary', 'weight' => 0, 'style' => NULL, - 'protected_property' => null, + 'size' => NULL, + 'size_value' => NULL, + 'protected_property' => NULL, ); $staging->write($name_primary, $values_primary); $values_secondary = array( @@ -388,7 +392,9 @@ function testImportErrorLog() { 'label' => 'Secondary Sync', 'weight' => 0, 'style' => NULL, - 'protected_property' => null, + 'size' => NULL, + 'size_value' => NULL, + 'protected_property' => NULL, ); $staging->write($name_secondary, $values_secondary); // Verify that there are configuration differences to import. diff --git a/core/modules/config/src/Tests/ConfigImporterTest.php b/core/modules/config/src/Tests/ConfigImporterTest.php index 581b5c4..a6b2312 100644 --- a/core/modules/config/src/Tests/ConfigImporterTest.php +++ b/core/modules/config/src/Tests/ConfigImporterTest.php @@ -173,6 +173,8 @@ function testNew() { 'label' => 'New', 'weight' => 0, 'style' => '', + 'size' => '', + 'size_value' => '', 'protected_property' => '', ); $staging->write($dynamic_name, $original_dynamic_data); diff --git a/core/modules/config/tests/config_test/config/schema/config_test.schema.yml b/core/modules/config/tests/config_test/config/schema/config_test.schema.yml index 9240d4b..f68acde 100644 --- a/core/modules/config/tests/config_test/config/schema/config_test.schema.yml +++ b/core/modules/config/tests/config_test/config/schema/config_test.schema.yml @@ -15,6 +15,12 @@ config_test_dynamic: style: type: string label: 'style' + size: + type: string + label: 'Size' + size_value: + type: string + label: 'Size value' protected_property: type: string label: 'Protected property' diff --git a/core/modules/config/tests/config_test/src/ConfigTestForm.php b/core/modules/config/tests/config_test/src/ConfigTestForm.php index e77ddb0..975e27b 100644 --- a/core/modules/config/tests/config_test/src/ConfigTestForm.php +++ b/core/modules/config/tests/config_test/src/ConfigTestForm.php @@ -54,6 +54,50 @@ public function form(array $form, FormStateInterface $form_state) { $form['style']['#options'] = image_style_options(); } + // The main premise of entity forms is that we get to work with an entity + // object at all times instead of checking submitted values from the form + // state. + $size = $entity->get('size'); + + $form['size_wrapper'] = array( + '#type' => 'container', + '#attributes' => array( + 'id' => 'size-wrapper', + ), + ); + $form['size_wrapper']['size'] = array( + '#type' => 'select', + '#title' => 'Size', + '#options' => array( + 'custom' => 'Custom', + ), + '#empty_option' => '- None -', + '#default_value' => $size, + '#ajax' => array( + 'callback' => '::updateSize', + 'wrapper' => 'size-wrapper', + ), + ); + $form['size_wrapper']['size_submit'] = array( + '#type' => 'submit', + '#value' => t('Change size'), + '#attributes' => array( + 'class' => array('js-hide'), + ), + '#submit' => array(array(get_class($this), 'changeSize')), + ); + $form['size_wrapper']['size_value'] = array( + '#type' => 'select', + '#title' => 'Custom size value', + '#options' => array( + 'small' => 'Small', + 'medium' => 'Medium', + 'large' => 'Large', + ), + '#default_value' => $entity->get('size_value'), + '#access' => !empty($size), + ); + $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array( '#type' => 'submit', @@ -68,6 +112,20 @@ public function form(array $form, FormStateInterface $form_state) { } /** + * Ajax callback for the size selection element. + */ + public static function updateSize(array $form, FormStateInterface $form_state) { + return $form['size_wrapper']; + } + + /** + * Element submit handler for non-js testing. + */ + public static function changeSize(array $form, FormStateInterface $form_state) { + $form_state->setRebuild(); + } + + /** * {@inheritdoc} */ public function save(array $form, FormStateInterface $form_state) {