You are here

function RenderWebTest::testDrupalRenderFormElements in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Common/RenderWebTest.php \Drupal\system\Tests\Common\RenderWebTest::testDrupalRenderFormElements()

Tests rendering form elements without passing through \Drupal::formBuilder()->doBuildForm().

File

core/modules/system/src/Tests/Common/RenderWebTest.php, line 51
Contains \Drupal\system\Tests\Common\RenderWebTest.

Class

RenderWebTest
Performs integration tests on drupal_render().

Namespace

Drupal\system\Tests\Common

Code

function testDrupalRenderFormElements() {

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