function FormElementsRenderTest::testDrupalRenderFormElements

Same name in other branches
  1. 8.9.x core/modules/system/tests/src/Kernel/Common/FormElementsRenderTest.php \Drupal\Tests\system\Kernel\Common\FormElementsRenderTest::testDrupalRenderFormElements()
  2. 10 core/modules/system/tests/src/Kernel/Common/FormElementsRenderTest.php \Drupal\Tests\system\Kernel\Common\FormElementsRenderTest::testDrupalRenderFormElements()
  3. 11.x core/modules/system/tests/src/Kernel/Common/FormElementsRenderTest.php \Drupal\Tests\system\Kernel\Common\FormElementsRenderTest::testDrupalRenderFormElements()

Tests rendering form elements without using doBuildForm().

See also

\Drupal\Core\Form\FormBuilderInterface::doBuildForm()

File

core/modules/system/tests/src/Kernel/Common/FormElementsRenderTest.php, line 26

Class

FormElementsRenderTest
Performs integration tests on \Drupal::service('renderer')->render().

Namespace

Drupal\Tests\system\Kernel\Common

Code

public function testDrupalRenderFormElements() {
    // Define a series of form elements.
    $element = [
        '#type' => 'button',
        '#value' => $this->randomMachineName(),
    ];
    $this->assertRenderedElement($element, '//input[@type=:type]', [
        ':type' => 'submit',
    ]);
    $element = [
        '#type' => 'textfield',
        '#title' => $this->randomMachineName(),
        '#value' => $this->randomMachineName(),
    ];
    $this->assertRenderedElement($element, '//input[@type=:type]', [
        ':type' => 'text',
    ]);
    $element = [
        '#type' => 'password',
        '#title' => $this->randomMachineName(),
    ];
    $this->assertRenderedElement($element, '//input[@type=:type]', [
        ':type' => 'password',
    ]);
    $element = [
        '#type' => 'textarea',
        '#title' => $this->randomMachineName(),
        '#value' => $this->randomMachineName(),
    ];
    $this->assertRenderedElement($element, '//textarea');
    $element = [
        '#type' => 'radio',
        '#title' => $this->randomMachineName(),
        '#value' => FALSE,
    ];
    $this->assertRenderedElement($element, '//input[@type=:type]', [
        ':type' => 'radio',
    ]);
    $element = [
        '#type' => 'checkbox',
        '#title' => $this->randomMachineName(),
    ];
    $this->assertRenderedElement($element, '//input[@type=:type]', [
        ':type' => 'checkbox',
    ]);
    $element = [
        '#type' => 'select',
        '#title' => $this->randomMachineName(),
        '#options' => [
            0 => $this->randomMachineName(),
            1 => $this->randomMachineName(),
        ],
    ];
    $this->assertRenderedElement($element, '//select');
    $element = [
        '#type' => 'file',
        '#title' => $this->randomMachineName(),
    ];
    $this->assertRenderedElement($element, '//input[@type=:type]', [
        ':type' => 'file',
    ]);
    $element = [
        '#type' => 'item',
        '#title' => $this->randomMachineName(),
        '#markup' => $this->randomMachineName(),
    ];
    $this->assertRenderedElement($element, '//div[contains(@class, :class) and contains(., :markup)]/label[contains(., :label)]', [
        ':class' => 'js-form-type-item',
        ':markup' => $element['#markup'],
        ':label' => $element['#title'],
    ]);
    $element = [
        '#type' => 'hidden',
        '#title' => $this->randomMachineName(),
        '#value' => $this->randomMachineName(),
    ];
    $this->assertRenderedElement($element, '//input[@type=:type]', [
        ':type' => 'hidden',
    ]);
    $element = [
        '#type' => 'link',
        '#title' => $this->randomMachineName(),
        '#url' => Url::fromRoute('common_test.destination'),
        '#options' => [
            'absolute' => TRUE,
        ],
    ];
    $this->assertRenderedElement($element, '//a[@href=:href and contains(., :title)]', [
        ':href' => URL::fromRoute('common_test.destination')->setAbsolute()
            ->toString(),
        ':title' => $element['#title'],
    ]);
    $element = [
        '#type' => 'details',
        '#open' => TRUE,
        '#title' => $this->randomMachineName(),
    ];
    $this->assertRenderedElement($element, '//details/summary[contains(., :title)]', [
        ':title' => $element['#title'],
    ]);
    $element = [
        '#type' => 'details',
        '#open' => TRUE,
        '#title' => $this->randomMachineName(),
    ];
    $this->assertRenderedElement($element, '//details');
    $element['item'] = [
        '#type' => 'item',
        '#title' => $this->randomMachineName(),
        '#markup' => $this->randomMachineName(),
    ];
    $this->assertRenderedElement($element, '//details/div[contains(@class, :class) and contains(., :markup)]', [
        ':class' => 'js-form-type-item',
        ':markup' => $element['item']['#markup'],
    ]);
}

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