From 40d534a04ffd142ae1926d9d827d5a185e36216a Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Thu, 8 Mar 2018 23:21:59 +0000 Subject: [PATCH] Issue #2941494 by jhedstrom, Lendude: Deprecate SystemConfigFormTestBase and create kernel test version --- .../Tests/System/SystemConfigFormTestBase.php | 7 ++ .../src/Functional/Form/FormObjectTest.php | 18 +---- .../tests/src/Kernel/Form/FormObjectTest.php | 35 ++++++++++ .../src/Tests/UserAdminSettingsFormTest.php | 12 +++- .../Drupal/KernelTests/ConfigFormTestBase.php | 66 +++++++++++++++++++ 5 files changed, 120 insertions(+), 18 deletions(-) create mode 100644 core/modules/system/tests/src/Kernel/Form/FormObjectTest.php create mode 100644 core/tests/Drupal/KernelTests/ConfigFormTestBase.php diff --git a/core/modules/system/src/Tests/System/SystemConfigFormTestBase.php b/core/modules/system/src/Tests/System/SystemConfigFormTestBase.php index a14fd3107e0..6cf9079d5a3 100644 --- a/core/modules/system/src/Tests/System/SystemConfigFormTestBase.php +++ b/core/modules/system/src/Tests/System/SystemConfigFormTestBase.php @@ -2,6 +2,8 @@ namespace Drupal\system\Tests\System; +@trigger_error('\Drupal\system\Tests\System\SystemConfigFormTestBase is deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0. Use \Drupal\KernelTests\ConfigFormTestBase instead.', E_USER_DEPRECATED); + use Drupal\Core\Form\FormState; use Drupal\simpletest\WebTestBase; @@ -10,6 +12,11 @@ use Drupal\simpletest\WebTestBase; * * @see UserAdminSettingsFormTest * For a full working implementation. + * + * @deprecated in Drupal 8.6.x and will be removed before Drupal 9.0.0. Use + * \Drupal\KernelTests\ConfigFormTestBase instead. + * + * @see https://www.drupal.org/node/2941907 */ abstract class SystemConfigFormTestBase extends WebTestBase { /** diff --git a/core/modules/system/tests/src/Functional/Form/FormObjectTest.php b/core/modules/system/tests/src/Functional/Form/FormObjectTest.php index 34ebb2e79ad..e2c00f5d17a 100644 --- a/core/modules/system/tests/src/Functional/Form/FormObjectTest.php +++ b/core/modules/system/tests/src/Functional/Form/FormObjectTest.php @@ -2,15 +2,14 @@ namespace Drupal\Tests\system\Functional\Form; -use Drupal\system\Tests\System\SystemConfigFormTestBase; -use Drupal\form_test\FormTestObject; +use Drupal\Tests\BrowserTestBase; /** * Tests building a form from an object. * * @group Form */ -class FormObjectTest extends SystemConfigFormTestBase { +class FormObjectTest extends BrowserTestBase { /** * Modules to enable. @@ -19,19 +18,6 @@ class FormObjectTest extends SystemConfigFormTestBase { */ public static $modules = ['form_test']; - protected function setUp() { - parent::setUp(); - - $this->form = new FormTestObject($this->container->get('config.factory')); - $this->values = [ - 'bananas' => [ - '#value' => $this->randomString(10), - '#config_name' => 'form_test.object', - '#config_key' => 'bananas', - ], - ]; - } - /** * Tests using an object as the form callback. * diff --git a/core/modules/system/tests/src/Kernel/Form/FormObjectTest.php b/core/modules/system/tests/src/Kernel/Form/FormObjectTest.php new file mode 100644 index 00000000000..36cbb50ca77 --- /dev/null +++ b/core/modules/system/tests/src/Kernel/Form/FormObjectTest.php @@ -0,0 +1,35 @@ +form = new FormTestObject($this->container->get('config.factory')); + $this->values = [ + 'bananas' => [ + '#value' => $this->randomString(10), + '#config_name' => 'form_test.object', + '#config_key' => 'bananas', + ], + ]; + } + +} diff --git a/core/modules/user/src/Tests/UserAdminSettingsFormTest.php b/core/modules/user/src/Tests/UserAdminSettingsFormTest.php index b77411154c6..784d5e1e62e 100644 --- a/core/modules/user/src/Tests/UserAdminSettingsFormTest.php +++ b/core/modules/user/src/Tests/UserAdminSettingsFormTest.php @@ -2,7 +2,7 @@ namespace Drupal\user\Tests; -use Drupal\system\Tests\System\SystemConfigFormTestBase; +use Drupal\KernelTests\ConfigFormTestBase; use Drupal\user\AccountSettingsForm; /** @@ -10,8 +10,16 @@ use Drupal\user\AccountSettingsForm; * * @group user */ -class UserAdminSettingsFormTest extends SystemConfigFormTestBase { +class UserAdminSettingsFormTest extends ConfigFormTestBase { + /** + * {@inheritdoc} + */ + public static $modules = ['user', 'system']; + + /** + * {@inheritdoc} + */ protected function setUp() { parent::setUp(); diff --git a/core/tests/Drupal/KernelTests/ConfigFormTestBase.php b/core/tests/Drupal/KernelTests/ConfigFormTestBase.php new file mode 100644 index 00000000000..9997dc54ba8 --- /dev/null +++ b/core/tests/Drupal/KernelTests/ConfigFormTestBase.php @@ -0,0 +1,66 @@ + array( + * '#value' => $this->randomString(), + * '#config_name' => 'user.mail', + * '#config_key' => 'cancel_confirm.body', + * ), + * ); + * @endcode + * + * @var array + */ + protected $values; + + /** + * Submit the system_config_form ensure the configuration has expected values. + */ + public function testConfigForm() { + // Programmatically submit the given values. + $values = []; + foreach ($this->values as $form_key => $data) { + $values[$form_key] = $data['#value']; + } + $form_state = (new FormState())->setValues($values); + \Drupal::formBuilder()->submitForm($this->form, $form_state); + + // Check that the form returns an error when expected, and vice versa. + $errors = $form_state->getErrors(); + $valid_form = empty($errors); + $args = [ + '%values' => print_r($values, TRUE), + '%errors' => $valid_form ? t('None') : implode(' ', $errors), + ]; + $this->assertTrue($valid_form, format_string('Input values: %values
Validation handler errors: %errors', $args)); + + foreach ($this->values as $data) { + $this->assertEqual($data['#value'], $this->config($data['#config_name'])->get($data['#config_key'])); + } + } + +}