FormTestBase.php in Purge 8.3
File
modules/purge_ui/tests/src/Functional/Form/FormTestBase.php
View source
<?php
namespace Drupal\Tests\purge_ui\Functional\Form;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Tests\purge\Functional\BrowserTestBase;
use Symfony\Component\HttpFoundation\Request;
abstract class FormTestBase extends BrowserTestBase {
public static $modules = [
'purge_ui',
];
protected $adminUser;
protected $formBuilder = NULL;
protected $formClass = '';
protected $formId = NULL;
protected $formInstance = NULL;
protected $route = '';
protected $routeParameters = [];
protected $routeParametersInvalid = NULL;
protected $routeTitle = 'Untitled';
public function setUp($switch_to_memory_queue = TRUE) : void {
parent::setUp($switch_to_memory_queue);
$this
->assertTestProperties();
$this->adminUser = $this
->drupalCreateUser([
'administer site configuration',
]);
if (is_null($this->formId)) {
$this->formId = $this->route;
}
}
protected function assertTestProperties() : void {
$this
->assertNotSame('', $this->route, '$route not set!');
$this
->assertNotSame('', $this->formClass, '$formClass not set!');
}
protected function formInstance() : FormInterface {
if (is_null($this->formInstance)) {
$this->formInstance = $this
->getFormInstance();
}
return $this->formInstance;
}
protected function formBuilder() : FormBuilderInterface {
if (is_null($this->formBuilder)) {
$this->formBuilder = $this->container
->get('form_builder');
}
return $this->formBuilder;
}
protected function getBuiltForm(array $route_parameters = [], array $options = []) : array {
$path = $this
->getPath($route_parameters, $options);
$request = Request::create($path);
$match = $this->container
->get('router')
->matchRequest($request);
$controller = $this->container
->get('controller_resolver')
->getControllerFromDefinition($match['_controller']);
$arguments = $this->container
->get('http_kernel.controller.argument_resolver')
->getArguments($request, $controller);
return call_user_func_array($controller, $arguments);
}
protected function getFormInstance() : FormInterface {
return $this->formClass::create($this->container);
}
protected function getFormStateInstance() : FormStateInterface {
return new FormState();
}
protected function getPath(array $route_parameters = [], array $options = []) : string {
$this
->propagateRouteParameters($route_parameters);
return Url::fromRoute($this->route, $route_parameters, $options)
->toString();
}
protected function propagateRouteParameters(array &$route_parameters) {
if (empty($route_parameters)) {
$route_parameters = $this->routeParameters;
}
}
public function testFormCodeContract() : void {
$form = $this
->getFormInstance();
$this
->assertInstanceOf(FormInterface::class, $form);
}
public function testFormBuilds() : void {
$this
->drupalLogin($this->adminUser);
$form = $this
->getBuiltForm();
$this
->assertSame(TRUE, isset($form['#type']));
$this
->assertSame(TRUE, isset($form['form_id']['#value']));
$this
->assertSame(TRUE, isset($form['#form_id']));
$this
->assertSame(TRUE, isset($form['#cache']['contexts'][0]));
$this
->assertSame('form', $form['#type']);
$this
->assertSame($this->formId, $form['form_id']['#value']);
$this
->assertSame($this->formId, $form['#form_id']);
$this
->assertSame([
'user.roles:authenticated',
], $form['#cache']['contexts']);
}
public function testRouteAccess() : void {
$this
->drupalGet($this
->getPath());
$this
->assertSession()
->statusCodeEquals(403);
if (!is_null($this->routeParametersInvalid)) {
$path = $this
->getPath($this->routeParametersInvalid);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet($path);
$this
->assertSession()
->statusCodeEquals(404);
}
}
public function testRouteAccessGranted() : void {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet($this
->getPath());
$this
->assertSession()
->statusCodeEquals(200);
}
public function testRouteTitle() : void {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet($this
->getPath());
$this
->assertSame($this->routeTitle . ' | Drupal', $this
->getSession()
->getPage()
->find('css', 'title')
->getText());
}
}