RenameAdminPathsSettingsFormTest.php in Rename Admin Paths 8.2
File
tests/src/Unit/Form/RenameAdminPathsSettingsFormTest.php
View source
<?php
namespace Drupal\Tests\rename_admin_paths\Unit\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteBuilderInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\rename_admin_paths\Config;
use Drupal\rename_admin_paths\Form\RenameAdminPathsSettingsForm;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
class RenameAdminPathsSettingsFormTest extends UnitTestCase {
public function testValidatePathWithoutValue() {
$element = [];
$this
->getForm()
->validate($element, $this
->getInvalidFormState());
}
public function testWithValidValue(string $value) {
$element = [
'#value' => $value,
];
$this
->getForm()
->validate($element, $this
->getValidFormState());
}
public function testWithInvalidValue(string $value) {
$element = [
'#value' => $value,
];
$this
->getForm()
->validate($element, $this
->getInvalidFormState());
}
public function validValues() {
(yield [
'backend',
]);
(yield [
'back-end',
]);
(yield [
'Backend',
]);
(yield [
'Back-End',
]);
(yield [
'Back_End',
]);
(yield [
'Back-End_123',
]);
(yield [
'admin2',
]);
(yield [
'user2',
]);
}
public function invalidValues() {
(yield [
'backend!',
]);
(yield [
'back@end',
]);
(yield [
'(Backend)',
]);
(yield [
'Back~End',
]);
(yield [
'Back=End',
]);
(yield [
'Back-End+123',
]);
(yield [
'admin',
]);
(yield [
'user',
]);
(yield [
'Admin',
]);
}
private function getForm() {
$config = $this
->createMock(Config::class);
$routeBuilder = $this
->createMock(RouteBuilderInterface::class);
$translator = $this
->createMock(TranslationInterface::class);
$translator
->method('translateString')
->willReturn('Error');
return new RenameAdminPathsSettingsForm($config, $routeBuilder, $translator);
}
private function getValidFormState() {
$formState = $this
->prophesize(FormStateInterface::class);
$formState
->setError()
->shouldNotBeCalled();
return $formState
->reveal();
}
private function getInvalidFormState() {
$formState = $this
->prophesize(FormStateInterface::class);
$formState
->setError(Argument::any(), Argument::any())
->shouldBeCalled();
return $formState
->reveal();
}
}