Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / system / src / Tests / System / SystemConfigFormTestBase.php
1 <?php
2
3 namespace Drupal\system\Tests\System;
4
5 @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);
6
7 use Drupal\Core\Form\FormState;
8 use Drupal\simpletest\WebTestBase;
9
10 /**
11  * Full generic test suite for any form that data with the configuration system.
12  *
13  * @see UserAdminSettingsFormTest
14  *   For a full working implementation.
15  *
16  * @deprecated in Drupal 8.6.x and will be removed before Drupal 9.0.0. Use
17  *   \Drupal\KernelTests\ConfigFormTestBase instead.
18  *
19  * @see https://www.drupal.org/node/2941907
20  */
21 abstract class SystemConfigFormTestBase extends WebTestBase {
22   /**
23    * Form ID to use for testing.
24    *
25    * @var \Drupal\Core\Form\FormInterface
26    */
27   protected $form;
28
29   /**
30    * Values to use for testing.
31    *
32    * Contains details for form key, configuration object name, and config key.
33    * Example:
34    * @code
35    *   array(
36    *     'user_mail_cancel_confirm_body' => array(
37    *       '#value' => $this->randomString(),
38    *       '#config_name' => 'user.mail',
39    *       '#config_key' => 'cancel_confirm.body',
40    *     ),
41    *   );
42    * @endcode
43    *
44    * @var array
45    */
46   protected $values;
47
48   /**
49    * Submit the system_config_form ensure the configuration has expected values.
50    */
51   public function testConfigForm() {
52     // Programmatically submit the given values.
53     $values = [];
54     foreach ($this->values as $form_key => $data) {
55       $values[$form_key] = $data['#value'];
56     }
57     $form_state = (new FormState())->setValues($values);
58     \Drupal::formBuilder()->submitForm($this->form, $form_state);
59
60     // Check that the form returns an error when expected, and vice versa.
61     $errors = $form_state->getErrors();
62     $valid_form = empty($errors);
63     $args = [
64       '%values' => print_r($values, TRUE),
65       '%errors' => $valid_form ? t('None') : implode(' ', $errors),
66     ];
67     $this->assertTrue($valid_form, format_string('Input values: %values<br/>Validation handler errors: %errors', $args));
68
69     foreach ($this->values as $data) {
70       $this->assertEqual($data['#value'], $this->config($data['#config_name'])->get($data['#config_key']));
71     }
72   }
73
74 }