You are here

public function FieldWebTest::testFieldClasses in Drupal 8

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

Tests the field/label/wrapper classes.

File

core/modules/views/tests/src/Functional/Handler/FieldWebTest.php, line 396

Class

FieldWebTest
Tests fields from within a UI.

Namespace

Drupal\Tests\views\Functional\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
    ->assertEmpty($this
    ->xpathContent($output, '//div[contains(@class, :class)]', [
    ':class' => 'field-content',
  ]));
  $this
    ->assertEmpty($this
    ->xpathContent($output, '//div[contains(@class, :class)]', [
    ':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
    ->assertNotEmpty($this
    ->xpathContent($output, '//span[contains(@class, :class)]', [
    ':class' => 'field-content',
  ]));
  $this
    ->assertNotEmpty($this
    ->xpathContent($output, '//span[contains(@class, :class)]', [
    ':class' => 'views-label',
  ]));
  $this
    ->assertNotEmpty($this
    ->xpathContent($output, '//div[contains(@class, :class)]', [
    ':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 ([
    '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
      ->assertNotEmpty($this
      ->xpathContent($output, "//{$element_type}[contains(@class, :class)]", [
      ':class' => $random_class,
    ]));

    // Set no custom css class.
    $id_field->options['element_wrapper_class'] = '';
    $output = $view
      ->preview();
    $output = $renderer
      ->renderRoot($output);
    $this
      ->assertEmpty($this
      ->xpathContent($output, "//{$element_type}[contains(@class, :class)]", [
      ':class' => $random_class,
    ]));
    $this
      ->assertNotEmpty($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 ([
    '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
      ->assertNotEmpty($this
      ->xpathContent($output, "//li[contains(@class, views-row)]//{$element_type}[contains(@class, :class)]", [
      ':class' => $random_class,
    ]));

    // Set no custom css class.
    $id_field->options['element_label_class'] = '';
    $output = $view
      ->preview();
    $output = $renderer
      ->renderRoot($output);
    $this
      ->assertEmpty($this
      ->xpathContent($output, "//li[contains(@class, views-row)]//{$element_type}[contains(@class, :class)]", [
      ':class' => $random_class,
    ]));
    $this
      ->assertNotEmpty($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 ([
    '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
      ->assertNotEmpty($this
      ->xpathContent($output, "//li[contains(@class, views-row)]//div[contains(@class, views-field)]//{$element_type}[contains(@class, :class)]", [
      ':class' => $random_class,
    ]));

    // Set no custom css class.
    $id_field->options['element_class'] = '';
    $output = $view
      ->preview();
    $output = $renderer
      ->renderRoot($output);
    $this
      ->assertEmpty($this
      ->xpathContent($output, "//li[contains(@class, views-row)]//div[contains(@class, views-field)]//{$element_type}[contains(@class, :class)]", [
      ':class' => $random_class,
    ]));
    $this
      ->assertNotEmpty($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 = [
    '',
    0,
    'div',
    'span',
    'h1',
    'h2',
    'h3',
    'h4',
    'h5',
    'h6',
    'p',
    'strong',
    'em',
    'marquee',
  ];
  $this
    ->assertEqual(array_keys($element_types), $expected_elements);
}