As you'd be aware by now - Drupal 8 features lots of refactoring of form procedural code to object-oriented.
One such refactoring was the way forms are build, validated and executed.
One cool side-effect of this is that you can now build and test a form with a single class.
Yep that's right, the form and the test are one and the same - read on to find out more.
Firstly kudos here to Tim Plunkett who pointed this out to me, and to all of those who championed much of the refactoring that made this even possible.
Testing forms and elements in Drupal 7
In Drupal 7 to test a form, or an element you need the following:
- A test module with:
- A hook_menu entry
- A form callback
- (optional) A validate callback
- (optional) A submit callback
- A web-test (a test that extends from DrupalWebTestCase)
Drupal 8 forms
As you're probably aware from all the example code and posts out there, forms in Drupal 8 are objects that implement Drupal\Core\Form\FormInterface.
Luckily, you can write a test that both extends from KernelTestBase (Drupal\KernelTests\KernelTestBase) that also implements FormInterface. This means you don't need all of the additional routing plumbing you needed in Drupal 7.
Let's look at an example in Drupal - PathElementFormTest (\Drupal\KernelTests\Core\Element\PathElementFormTest). This test is to test core's PathElement (\Drupal\Core\Render\Element\PathElement) - a plugin that provides a form element where users can enter a path that can be optionally validated and stored as either a \Drupal\Core\Url value object or a array containing a route name and route parameters pair. So in terms of testing, the bulk of the logic in the element plugin is contained in the #element_validate callback - PathElement::validateMatchedPath.
There are several different combinations of configuration for the path element as follows:
- Required and validated with no conversion
- Required and non validated with no conversion
- Optional and validated with no conversion
- Optional, validated and converted into a route name/parameter pair
- Required, validated and converted into a route name/parameter pair
- Required, validated and converted into a Url object
So we need to set up several instance of the element on a test form.
So because our test extends from KernelTestBase, but also implements FormInterface, we just build a normal form array with all of these configurations in our implementation of FormInterface's ::buildForm method - see \Drupal\KernelTests\Core\Element\PathElementFormTest::buildForm to see how this is done. We're not interested in doing any additional validation or submission, so our implementation of FormInterface's ::submitForm and ::validateForm can be blank.
Testing the element behaviour
So to test the element validate works as expected for each of the fields, we need to trigger submission of the form. Now in a web-test, we'd use the internal test browser to visit the form on a route and then use a method like BrowserTestBase::submitForm to actually submit the form. But as we're using a kernel test here, there is no internal browser - so instead we can submit directly through the form_builder service (\Drupal\Core\Form\FormBuilderInterface). The code in PathElementFormTest looks something like this:
$form_state = (new FormState()) ->setValues([ 'required_validate' => 'user/' . $this->testUser->id(), 'required_non_validate' => 'magic-ponies', 'required_validate_route' => 'user/' . $this->testUser->id(), 'required_validate_url' => 'user/' . $this->testUser->id(), ]); $form_builder = $this->container->get('form_builder'); $form_builder->submitForm($this, $form_state);
So firstly we're building a new FormState object and setting the submitted values on it - this is just a key-value pair of form values. Then we're getting the form builder service from the container and submitting the form.
From here, if there were any errors, they'll be present on the form state object. So we can do things likes check for expected errors, or check for expected values.
For example, to check that there were no errors.
Or to check that the conversion occurred (i.e. the input path was upcast to a route name/parameter pair or Url object).
$this->assertEquals($form_state->getValue('required_validate_route'), array( 'route_name' => 'entity.user.canonical', 'route_parameters' => array( 'user' => $this->testUser->id(), ), ));
Or to check for a particular error.
$this->assertEquals($errors, array('required_validate' => t('@name field is required.', array('@name' => 'required_validate'))));
So why would you want to use this approach?
Well for one, the test is damn fast. Kernel tests don't do a full site install, and because there is no HTTP to fetch and submit the form, you get fast feedback. And when you get fast feedback, you're more likely to practice good test driven development.
So if you're building an element plugin for a contrib or client project, I encourage you to start with a test, or rather a form, or rather both. Specify the various configurations of your element and test the expected behaviour.
I'm sure you agree, this is another clear case of Drupal 8 for the win.
Tiny nitpick: I think you've got your "expected" and actual" params reversed in your asserts.
Yep, you're right - these examples are taken from core - do you want to file an issue to fix them? Thanks