MachineNameTest.php in Drupal 10
File
core/tests/Drupal/KernelTests/Core/Render/Element/MachineNameTest.php
View source
<?php
namespace Drupal\KernelTests\Core\Render\Element;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\KernelTests\KernelTestBase;
class MachineNameTest extends KernelTestBase implements FormInterface {
protected static $modules = [
'system',
];
public function getFormId() {
return __CLASS__;
}
public function buildForm(array $form, FormStateInterface $form_state) {
$element = [
'#id' => 'test',
'#type' => 'machine_name',
'#machine_name' => [
'source' => [
'test_source',
],
],
'#name' => 'test_machine_name',
'#default_value' => NULL,
];
$complete_form = [
'test_machine_name' => $element,
'test_source' => [
'#type' => 'textfield',
],
];
return $complete_form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
public function testMachineNameOrderException() {
$this
->expectException(\LogicException::class);
$this
->expectErrorMessage('The machine name element "test_machine_name" is defined before the source element "test_source", it must be defined after or the source element must specify an id.');
$form = \Drupal::formBuilder()
->getForm($this);
$this
->render($form);
}
}