comparison core/tests/Drupal/KernelTests/ConfigFormTestBase.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
1 <?php
2
3 namespace Drupal\KernelTests;
4
5 use Drupal\Core\Form\FormState;
6
7 /**
8 * Full generic test suite for any form that data with the configuration system.
9 *
10 * @see UserAdminSettingsFormTest
11 * For a full working implementation.
12 */
13 abstract class ConfigFormTestBase extends KernelTestBase {
14 /**
15 * Form ID to use for testing.
16 *
17 * @var \Drupal\Core\Form\FormInterface.
18 */
19 protected $form;
20
21 /**
22 * Values to use for testing.
23 *
24 * Contains details for form key, configuration object name, and config key.
25 * Example:
26 * @code
27 * array(
28 * 'user_mail_cancel_confirm_body' => array(
29 * '#value' => $this->randomString(),
30 * '#config_name' => 'user.mail',
31 * '#config_key' => 'cancel_confirm.body',
32 * ),
33 * );
34 * @endcode
35 *
36 * @var array
37 */
38 protected $values;
39
40 /**
41 * Submit the system_config_form ensure the configuration has expected values.
42 */
43 public function testConfigForm() {
44 // Programmatically submit the given values.
45 $values = [];
46 foreach ($this->values as $form_key => $data) {
47 $values[$form_key] = $data['#value'];
48 }
49 $form_state = (new FormState())->setValues($values);
50 \Drupal::formBuilder()->submitForm($this->form, $form_state);
51
52 // Check that the form returns an error when expected, and vice versa.
53 $errors = $form_state->getErrors();
54 $valid_form = empty($errors);
55 $args = [
56 '%values' => print_r($values, TRUE),
57 '%errors' => $valid_form ? t('None') : implode(' ', $errors),
58 ];
59 $this->assertTrue($valid_form, format_string('Input values: %values<br/>Validation handler errors: %errors', $args));
60
61 foreach ($this->values as $data) {
62 $this->assertEqual($data['#value'], $this->config($data['#config_name'])->get($data['#config_key']));
63 }
64 }
65
66 }