Skip to content
Snippets Groups Projects
Select Git revision
  • 8.9.x
  • 11.x default protected
  • 11.2.x protected
  • 10.5.x protected
  • 10.6.x protected
  • 11.1.x protected
  • 10.4.x protected
  • 11.0.x protected
  • 10.3.x protected
  • 7.x protected
  • 10.2.x protected
  • 10.1.x protected
  • 9.5.x protected
  • 10.0.x protected
  • 9.4.x protected
  • 9.3.x protected
  • 9.2.x protected
  • 9.1.x protected
  • 9.0.x protected
  • 8.8.x protected
  • 10.5.2 protected
  • 11.2.3 protected
  • 10.5.1 protected
  • 11.2.2 protected
  • 11.2.1 protected
  • 11.2.0 protected
  • 10.5.0 protected
  • 11.2.0-rc2 protected
  • 10.5.0-rc1 protected
  • 11.2.0-rc1 protected
  • 10.4.8 protected
  • 11.1.8 protected
  • 10.5.0-beta1 protected
  • 11.2.0-beta1 protected
  • 11.2.0-alpha1 protected
  • 10.4.7 protected
  • 11.1.7 protected
  • 10.4.6 protected
  • 11.1.6 protected
  • 10.3.14 protected
40 results

ConfigFormTestBase.php

Blame
  • Alex Pott's avatar
    Issue #2228741 by rpayanm, mikelutz, voleger, epari.siva, malotor, Rolf van de...
    Alex Pott authored
    Issue #2228741 by rpayanm, mikelutz, voleger, epari.siva, malotor, Rolf van de Krol, quietone, Mile23, andypost, ianthomas_uk, Lendude, alexpott, dawehner, xjm, Gábor Hojtsy, Berdir, catch: Replace calls to format_string() with Drupal\Component\Render\FormattableMarkup
    e39262bf
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    ConfigFormTestBase.php 1.84 KiB
    <?php
    
    namespace Drupal\KernelTests;
    
    use Drupal\Component\Render\FormattableMarkup;
    use Drupal\Core\Form\FormState;
    
    /**
     * Full generic test suite for any form that data with the configuration system.
     *
     * @see UserAdminSettingsFormTest
     *   For a full working implementation.
     */
    abstract class ConfigFormTestBase extends KernelTestBase {
      /**
       * Form ID to use for testing.
       *
       * @var \Drupal\Core\Form\FormInterface
       */
      protected $form;
    
      /**
       * Values to use for testing.
       *
       * Contains details for form key, configuration object name, and config key.
       * Example:
       * @code
       *   array(
       *     'user_mail_cancel_confirm_body' => 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, new FormattableMarkup('Input values: %values<br/>Validation handler errors: %errors', $args));
    
        foreach ($this->values as $data) {
          $this->assertEqual($data['#value'], $this->config($data['#config_name'])->get($data['#config_key']));
        }
      }
    
    }