MachineNameTest.php

Same filename in this branch
  1. 11.x core/modules/migrate/tests/src/Unit/process/MachineNameTest.php
  2. 11.x core/tests/Drupal/FunctionalJavascriptTests/Core/MachineNameTest.php
  3. 11.x core/tests/Drupal/Tests/Core/Render/Element/MachineNameTest.php
Same filename and directory in other branches
  1. 9 core/modules/migrate/tests/src/Unit/process/MachineNameTest.php
  2. 9 core/tests/Drupal/KernelTests/Core/Render/Element/MachineNameTest.php
  3. 9 core/tests/Drupal/FunctionalJavascriptTests/Core/MachineNameTest.php
  4. 9 core/tests/Drupal/Tests/Core/Render/Element/MachineNameTest.php
  5. 8.9.x core/modules/migrate/tests/src/Unit/process/MachineNameTest.php
  6. 8.9.x core/tests/Drupal/FunctionalJavascriptTests/Core/MachineNameTest.php
  7. 8.9.x core/tests/Drupal/Tests/Core/Render/Element/MachineNameTest.php
  8. 10 core/modules/migrate/tests/src/Unit/process/MachineNameTest.php
  9. 10 core/tests/Drupal/KernelTests/Core/Render/Element/MachineNameTest.php
  10. 10 core/tests/Drupal/FunctionalJavascriptTests/Core/MachineNameTest.php
  11. 10 core/tests/Drupal/Tests/Core/Render/Element/MachineNameTest.php

Namespace

Drupal\KernelTests\Core\Render\Element

File

core/tests/Drupal/KernelTests/Core/Render/Element/MachineNameTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\KernelTests\Core\Render\Element;

use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\KernelTests\KernelTestBase;

/**
 * @coversDefaultClass \Drupal\Core\Render\Element\MachineName
 * @group Render
 */
class MachineNameTest extends KernelTestBase implements FormInterface {
    
    /**
     * {@inheritdoc}
     */
    protected static $modules = [
        'system',
    ];
    
    /**
     * {@inheritdoc}
     */
    public function getFormId() {
        return __CLASS__;
    }
    
    /**
     * {@inheritdoc}
     */
    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;
    }
    
    /**
     * {@inheritdoc}
     */
    public function validateForm(array &$form, FormStateInterface $form_state) {
    }
    
    /**
     * {@inheritdoc}
     */
    public function submitForm(array &$form, FormStateInterface $form_state) {
    }
    
    /**
     * Tests the order of the machine name field and the source.
     */
    public function testMachineNameOrderException() : void {
        $this->expectException(\LogicException::class);
        $this->expectExceptionMessage('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);
    }

}

Classes

Title Deprecated Summary
MachineNameTest @coversDefaultClass \Drupal\Core\Render\Element\MachineName @group Render

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.