You are here

public function FieldTest::testFieldClasses in Views (for Drupal 7) 8.3

Tests the field/label/wrapper classes.

File

lib/Drupal/views/Tests/Handler/FieldTest.php, line 380
Definition of Drupal\views\Tests\Handler\FieldTest.

Class

FieldTest
Tests the generic field handler.

Namespace

Drupal\views\Tests\Handler

Code

public function testFieldClasses() {
  $view = views_get_view('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;
  $output = $view
    ->preview();
  $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();

  // 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
    ->randomName();

  // 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();
    $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();
    $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();
    $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();
    $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();
    $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();
    $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
    ->get_elements();
  $expected_elements = array(
    '',
    0,
    'div',
    'span',
    'h1',
    'h2',
    'h3',
    'h4',
    'h5',
    'h6',
    'p',
    'strong',
    'em',
    'marquee',
  );
  $this
    ->assertEqual(array_keys($element_types), $expected_elements);
}