You are here

public function FieldWebTest::testFieldClasses in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/views/src/Tests/Handler/FieldWebTest.php \Drupal\views\Tests\Handler\FieldWebTest::testFieldClasses()

Tests the field/label/wrapper classes.

File

core/modules/views/src/Tests/Handler/FieldWebTest.php, line 389
Contains \Drupal\views\Tests\Handler\FieldWebTest.

Class

FieldWebTest
Tests fields from within a UI.

Namespace

Drupal\views\Tests\Handler

Code

public function testFieldClasses() {

  /** @var \Drupal\Core\Render\RendererInterface $renderer */
  $renderer = $this->container
    ->get('renderer');
  $view = Views::getView('test_field_classes');
  $view
    ->initHandlers();

  // Tests whether the default field classes are added.
  $id_field = $view->field['id'];
  $id_field->options['element_default_classes'] = FALSE;

  // Setup some kind of label by default.
  $id_field->options['label'] = $this
    ->randomMachineName();
  $output = $view
    ->preview();
  $output = $renderer
    ->renderRoot($output);
  $this
    ->assertFalse($this
    ->xpathContent($output, '//div[contains(@class, :class)]', array(
    ':class' => 'field-content',
  )));
  $this
    ->assertFalse($this
    ->xpathContent($output, '//div[contains(@class, :class)]', array(
    ':class' => 'field__label',
  )));
  $id_field->options['element_default_classes'] = TRUE;
  $output = $view
    ->preview();
  $output = $renderer
    ->renderRoot($output);

  // Per default the label and the element of the field are spans.
  $this
    ->assertTrue($this
    ->xpathContent($output, '//span[contains(@class, :class)]', array(
    ':class' => 'field-content',
  )));
  $this
    ->assertTrue($this
    ->xpathContent($output, '//span[contains(@class, :class)]', array(
    ':class' => 'views-label',
  )));
  $this
    ->assertTrue($this
    ->xpathContent($output, '//div[contains(@class, :class)]', array(
    ':class' => 'views-field',
  )));

  // Tests the element wrapper classes/element.
  $random_class = $this
    ->randomMachineName();

  // Set some common wrapper element types and see whether they appear with and without a custom class set.
  foreach (array(
    'h1',
    'span',
    'p',
    'div',
  ) as $element_type) {
    $id_field->options['element_wrapper_type'] = $element_type;

    // Set a custom wrapper element css class.
    $id_field->options['element_wrapper_class'] = $random_class;
    $output = $view
      ->preview();
    $output = $renderer
      ->renderRoot($output);
    $this
      ->assertTrue($this
      ->xpathContent($output, "//{$element_type}[contains(@class, :class)]", array(
      ':class' => $random_class,
    )));

    // Set no custom css class.
    $id_field->options['element_wrapper_class'] = '';
    $output = $view
      ->preview();
    $output = $renderer
      ->renderRoot($output);
    $this
      ->assertFalse($this
      ->xpathContent($output, "//{$element_type}[contains(@class, :class)]", array(
      ':class' => $random_class,
    )));
    $this
      ->assertTrue($this
      ->xpathContent($output, "//li[contains(@class, views-row)]/{$element_type}"));
  }

  // Tests the label class/element.
  // Set some common label element types and see whether they appear with and without a custom class set.
  foreach (array(
    'h1',
    'span',
    'p',
    'div',
  ) as $element_type) {
    $id_field->options['element_label_type'] = $element_type;

    // Set a custom label element css class.
    $id_field->options['element_label_class'] = $random_class;
    $output = $view
      ->preview();
    $output = $renderer
      ->renderRoot($output);
    $this
      ->assertTrue($this
      ->xpathContent($output, "//li[contains(@class, views-row)]//{$element_type}[contains(@class, :class)]", array(
      ':class' => $random_class,
    )));

    // Set no custom css class.
    $id_field->options['element_label_class'] = '';
    $output = $view
      ->preview();
    $output = $renderer
      ->renderRoot($output);
    $this
      ->assertFalse($this
      ->xpathContent($output, "//li[contains(@class, views-row)]//{$element_type}[contains(@class, :class)]", array(
      ':class' => $random_class,
    )));
    $this
      ->assertTrue($this
      ->xpathContent($output, "//li[contains(@class, views-row)]//{$element_type}"));
  }

  // Tests the element classes/element.
  // Set some common element element types and see whether they appear with and without a custom class set.
  foreach (array(
    'h1',
    'span',
    'p',
    'div',
  ) as $element_type) {
    $id_field->options['element_type'] = $element_type;

    // Set a custom label element css class.
    $id_field->options['element_class'] = $random_class;
    $output = $view
      ->preview();
    $output = $renderer
      ->renderRoot($output);
    $this
      ->assertTrue($this
      ->xpathContent($output, "//li[contains(@class, views-row)]//div[contains(@class, views-field)]//{$element_type}[contains(@class, :class)]", array(
      ':class' => $random_class,
    )));

    // Set no custom css class.
    $id_field->options['element_class'] = '';
    $output = $view
      ->preview();
    $output = $renderer
      ->renderRoot($output);
    $this
      ->assertFalse($this
      ->xpathContent($output, "//li[contains(@class, views-row)]//div[contains(@class, views-field)]//{$element_type}[contains(@class, :class)]", array(
      ':class' => $random_class,
    )));
    $this
      ->assertTrue($this
      ->xpathContent($output, "//li[contains(@class, views-row)]//div[contains(@class, views-field)]//{$element_type}"));
  }

  // Tests the available html elements.
  $element_types = $id_field
    ->getElements();
  $expected_elements = array(
    '',
    0,
    'div',
    'span',
    'h1',
    'h2',
    'h3',
    'h4',
    'h5',
    'h6',
    'p',
    'strong',
    'em',
    'marquee',
  );
  $this
    ->assertEqual(array_keys($element_types), $expected_elements);
}