Same filename and directory in other branches
- 8.9.x core/tests/Drupal/Tests/Core/Form/FormTestBase.php
- 9 core/tests/Drupal/Tests/Core/Form/FormTestBase.php
Namespace
Drupal\Tests\Core\Form
File
core/tests/Drupal/Tests/Core/Form/FormTestBase.php
View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\Core\Form;
use Drupal\Component\Utility\Html;
use Drupal\Core\EventSubscriber\RedirectResponseSubscriber;
use Drupal\Core\Form\FormBuilder;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\FormValidator;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
abstract class FormTestBase extends UnitTestCase {
protected $formBuilder;
protected $formValidator;
protected $formSubmitter;
protected $urlGenerator;
protected $moduleHandler;
protected $formCache;
protected $cache;
protected $account;
protected $controllerResolver;
protected $csrfToken;
protected $request;
protected $requestStack;
protected $classResolver;
protected $elementInfo;
protected $eventDispatcher;
protected $translationManager;
protected $kernel;
protected $logger;
protected $redirectResponseSubscriber;
protected $themeManager;
protected function setUp() : void {
parent::setUp();
require_once __DIR__ . '/fixtures/form_base_test.inc';
$this->moduleHandler = $this
->createMock('Drupal\\Core\\Extension\\ModuleHandlerInterface');
$this->formCache = $this
->createMock('Drupal\\Core\\Form\\FormCacheInterface');
$this->cache = $this
->createMock('Drupal\\Core\\Cache\\CacheBackendInterface');
$this->urlGenerator = $this
->createMock('Drupal\\Core\\Routing\\UrlGeneratorInterface');
$this->redirectResponseSubscriber = $this
->createMock(RedirectResponseSubscriber::class);
$this->classResolver = $this
->getClassResolverStub();
$this->elementInfo = $this
->getMockBuilder('\\Drupal\\Core\\Render\\ElementInfoManagerInterface')
->disableOriginalConstructor()
->getMock();
$this->elementInfo
->expects($this
->any())
->method('getInfo')
->willReturnCallback([
$this,
'getInfo',
]);
$this->csrfToken = $this
->getMockBuilder('Drupal\\Core\\Access\\CsrfTokenGenerator')
->disableOriginalConstructor()
->getMock();
$this->kernel = $this
->getMockBuilder('\\Drupal\\Core\\DrupalKernel')
->disableOriginalConstructor()
->getMock();
$this->account = $this
->createMock('Drupal\\Core\\Session\\AccountInterface');
$this->themeManager = $this
->createMock('Drupal\\Core\\Theme\\ThemeManagerInterface');
$this->request = Request::createFromGlobals();
$this->request
->setSession(new Session(new MockArraySessionStorage()));
$this->eventDispatcher = $this
->createMock('Symfony\\Contracts\\EventDispatcher\\EventDispatcherInterface');
$this->requestStack = new RequestStack();
$this->requestStack
->push($this->request);
$this->logger = $this
->createMock('Drupal\\Core\\Logger\\LoggerChannelInterface');
$form_error_handler = $this
->createMock('Drupal\\Core\\Form\\FormErrorHandlerInterface');
$this->formValidator = new FormValidator($this->requestStack, $this
->getStringTranslationStub(), $this->csrfToken, $this->logger, $form_error_handler);
$this->formSubmitter = $this
->getMockBuilder('Drupal\\Core\\Form\\FormSubmitter')
->setConstructorArgs([
$this->requestStack,
$this->urlGenerator,
$this->redirectResponseSubscriber,
])
->onlyMethods([
'batchGet',
])
->getMock();
$this->root = dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__)), 2);
$this->formBuilder = new FormBuilder($this->formValidator, $this->formSubmitter, $this->formCache, $this->moduleHandler, $this->eventDispatcher, $this->requestStack, $this->classResolver, $this->elementInfo, $this->themeManager, $this->csrfToken);
}
protected function tearDown() : void {
Html::resetSeenIds();
(new FormState())
->clearErrors();
}
protected function getMockForm($form_id, $expected_form = NULL, $count = 1) {
$form = $this
->createMock('Drupal\\Core\\Form\\FormInterface');
$form
->expects($this
->once())
->method('getFormId')
->willReturn($form_id);
if ($expected_form) {
$form
->expects($this
->exactly($count))
->method('buildForm')
->willReturn($expected_form);
}
return $form;
}
protected function simulateFormSubmission($form_id, FormInterface $form_arg, FormStateInterface $form_state, $programmed = TRUE) {
$input = $form_state
->getUserInput();
$input['op'] = 'Submit';
$form_state
->setUserInput($input)
->setProgrammed($programmed)
->setSubmitted();
return $this->formBuilder
->buildForm($form_arg, $form_state);
}
protected function assertFormElement(array $expected_form, array $actual_form, $form_key = NULL) {
$expected_element = $form_key ? $expected_form[$form_key] : $expected_form;
$actual_element = $form_key ? $actual_form[$form_key] : $actual_form;
$this
->assertSame(array_intersect_key($expected_element, $actual_element), $expected_element);
}
public function getInfo($type) {
$types['hidden'] = [
'#input' => TRUE,
];
$types['token'] = [
'#input' => TRUE,
];
$types['value'] = [
'#input' => TRUE,
];
$types['radios'] = [
'#input' => TRUE,
];
$types['textfield'] = [
'#input' => TRUE,
];
$types['submit'] = [
'#input' => TRUE,
'#name' => 'op',
'#is_button' => TRUE,
];
if (!isset($types[$type])) {
$types[$type] = [];
}
return $types[$type];
}
}
Classes
Name |
Description |
FormTestBase |
Provides a base class for testing form functionality. |