You are here

WebformElementLikertTest.php in Webform 6.x

Same filename and directory in other branches
  1. 8.5 tests/src/Functional/Element/WebformElementLikertTest.php

File

tests/src/Functional/Element/WebformElementLikertTest.php
View source
<?php

namespace Drupal\Tests\webform\Functional\Element;


/**
 * Tests for likert element.
 *
 * @group webform
 */
class WebformElementLikertTest extends WebformElementBrowserTestBase {

  /**
   * Webforms to load.
   *
   * @var array
   */
  protected static $testWebforms = [
    'test_element_likert',
  ];

  /**
   * Test likert element.
   */
  public function testLikertElement() {
    $this
      ->drupalGet('/webform/test_element_likert');

    // Check default likert element.
    $this
      ->assertRaw('<table class="webform-likert-table sticky-enabled responsive-enabled" data-likert-answers-count="3" data-drupal-selector="edit-likert-default-table" id="edit-likert-default-table" data-striping="1">');
    $this
      ->assertPattern('#<tr>\\s+<th><span class="visually-hidden">Questions</span></th>\\s+<th>Option 1</th>\\s+<th>Option 2</th>\\s+<th>Option 3</th>\\s+</tr>#');
    $this
      ->assertRaw('<label>Question 1</label>');
    $this
      ->assertRaw('<td><div class="js-form-item form-item js-form-type-radio form-item-likert-default-q1 js-form-item-likert-default-q1">');
    $this
      ->assertRaw('<input aria-labelledby="edit-likert-default-table-q1-likert-question" data-drupal-selector="edit-likert-default-q1" type="radio" id="edit-likert-default-q1" name="likert_default[q1]" value="1" class="form-radio" />');
    $this
      ->assertRaw('<label for="edit-likert-default-q1" class="option"><span class="webform-likert-label visually-hidden">Option 1</span></label>');

    // Check advanced likert element with N/A.
    $this
      ->assertPattern('#<tr>\\s+<th><span class="visually-hidden">Questions</span></th>\\s+<th>Option 1</th>\\s+<th>Option 2</th>\\s+<th>Option 3</th>\\s+<th>Not applicable</th>\\s+</tr>#');
    $this
      ->assertRaw('<td><div class="js-form-item form-item js-form-type-radio form-item-likert-advanced-q1 js-form-item-likert-advanced-q1">');
    $this
      ->assertRaw('<input aria-labelledby="edit-likert-advanced-table-q1-likert-question" required="required" aria-required="true" data-drupal-selector="edit-likert-advanced-q1" type="radio" id="edit-likert-advanced-q1--4" name="likert_advanced[q1]" value="N/A" class="form-radio" />');
    $this
      ->assertRaw('<label for="edit-likert-advanced-q1--4" class="option"><span class="webform-likert-label visually-hidden">Not applicable</span></label>');

    // Check likert with description.
    $this
      ->assertRaw('<th>Option 1<div class="description">This is a description</div>');
    $this
      ->assertRaw('<label>Question 1</label>');
    $this
      ->assertRaw('<div id="edit-likert-description-table-q1-likert-question--description" class="webform-element-description">');
    $this
      ->assertRaw('<label for="edit-likert-description-q1" class="option"><span class="webform-likert-label visually-hidden">Option 1</span></label>');
    $this
      ->assertRaw('<span class="webform-likert-description hidden">This is a description</span>');

    // Check likert with help.
    $this
      ->assertRaw('<th>Option 1<span class="webform-element-help js-webform-element-help" role="tooltip" tabindex="0" aria-label="Option 1" data-webform-help="&lt;div class=&quot;webform-element-help--title&quot;&gt;Option 1&lt;/div&gt;&lt;div class=&quot;webform-element-help--content&quot;&gt;This is help text&lt;/div&gt;"><span aria-hidden="true">?</span></span>');
    $this
      ->assertRaw('<label>Question 1<span class="webform-element-help js-webform-element-help" role="tooltip" tabindex="0" aria-label="Question 1" data-webform-help="&lt;div class=&quot;webform-element-help--title&quot;&gt;Question 1&lt;/div&gt;&lt;div class=&quot;webform-element-help--content&quot;&gt;This is help text&lt;/div&gt;"><span aria-hidden="true">?</span></span>');
    $this
      ->assertRaw('<label for="edit-likert-help-q1--2" class="option"><span class="webform-likert-label visually-hidden">Option 2<span class="webform-likert-help hidden"><span class="webform-element-help js-webform-element-help" role="tooltip" tabindex="0" aria-label="Option 2" data-webform-help="&lt;div class=&quot;webform-element-help--title&quot;&gt;Option 2&lt;/div&gt;&lt;div class=&quot;webform-element-help--content&quot;&gt;This is help text&lt;/div&gt;"><span aria-hidden="true">?</span></span>');

    // Check likert with custom required error.
    $this
      ->drupalPostForm('/webform/test_element_likert', [], 'Submit');
    $this
      ->assertNoRaw('Question 1 field is required.');
    $this
      ->assertNoRaw('Question 2 field is required.');
    $this
      ->assertNoRaw('Question 3 field is required.');
    $this
      ->assertRaw('{custom error for Question 1}');
    $this
      ->assertRaw('{custom error for Question 2}');
    $this
      ->assertRaw('{custom error for Question 3}');

    // Check likert processing.
    $edit = [
      'likert_advanced[q1]' => '1',
      'likert_advanced[q2]' => '2',
      'likert_advanced[q3]' => 'N/A',
      'likert_values[0]' => '0',
      'likert_values[1]' => '1',
      'likert_values[2]' => 'N/A',
    ];
    $this
      ->drupalPostForm('/webform/test_element_likert', $edit, 'Submit');
    $this
      ->assertRaw("likert_default:\n  q1: null\n  q2: null\n  q3: null\nlikert_advanced:\n  q1: '1'\n  q2: '2'\n  q3: N/A\nlikert_description:\n  q1: null\n  q2: null\n  q3: null\nlikert_help:\n  q1: null\n  q2: null\n  q3: null\nlikert_values:\n  - '0'\n  - '1'\n  - N/A");
  }

}

Classes

Namesort descending Description
WebformElementLikertTest Tests for likert element.