You are here

WebformElementAddressTest.php in Webform 8.5

Same filename and directory in other branches
  1. 6.x tests/src/Functional/Element/WebformElementAddressTest.php

File

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

namespace Drupal\Tests\webform\Functional\Element;

use Drupal\field\Entity\FieldStorageConfig;
use Drupal\webform\Entity\Webform;

/**
 * Tests for webform address element.
 *
 * @group webform
 */
class WebformElementAddressTest extends WebformElementBrowserTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = [
    'webform',
    'address',
    'node',
  ];

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

  /**
   * Tests address element.
   */
  public function testAddress() {
    $this
      ->drupalLogin($this->rootUser);
    $webform = Webform::load('test_element_address');

    /**************************************************************************/

    // Rendering.

    /**************************************************************************/
    $this
      ->drupalGet('/webform/test_element_address');

    // Check basic fieldset wrapper.
    $this
      ->assertRaw('<fieldset data-drupal-selector="edit-address" id="edit-address--wrapper" class="address--wrapper fieldgroup form-composite webform-composite-hidden-title js-webform-type-address webform-type-address js-form-item form-item js-form-wrapper form-wrapper">');
    $this
      ->assertRaw('<span class="visually-hidden fieldset-legend">address_basic</span>');

    // Check advanced fieldset, legend, help, and description.
    $this
      ->assertRaw('<fieldset data-drupal-selector="edit-address-advanced" aria-describedby="edit-address-advanced--wrapper--description" id="edit-address-advanced--wrapper" class="address--wrapper fieldgroup form-composite webform-composite-visible-title webform-element-help-container--title webform-element-help-container--title-after js-webform-type-address webform-type-address js-form-item form-item js-form-wrapper form-wrapper">');
    $this
      ->assertRaw('<span class="fieldset-legend">address_advanced<span class="webform-element-help js-webform-element-help" role="tooltip" tabindex="0" aria-label="address_advanced" data-webform-help="&lt;div class=&quot;webform-element-help--title&quot;&gt;address_advanced&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>');
    if (floatval(\Drupal::VERSION) >= 9) {
      $this
        ->assertRaw('<div class="description"><div id="edit-address-advanced--wrapper--description" data-drupal-field-elements="description" class="webform-element-description">This is a description</div>');
    }
    else {
      $this
        ->assertRaw('<div class="description"><div id="edit-address-advanced--wrapper--description" class="webform-element-description">This is a description</div>');
    }

    /**************************************************************************/

    // Processing.

    /**************************************************************************/

    // Check submitted value.
    $sid = $this
      ->postSubmission($webform);
    $this
      ->assertRaw("address:\n  country_code: US\n  langcode: en\n  given_name: John\n  family_name: Smith\n  organization: 'Google Inc.'\n  address_line1: '1098 Alta Ave'\n  address_line2: ''\n  locality: 'Mountain View'\n  administrative_area: CA\n  postal_code: '94043'\n  additional_name: null\n  sorting_code: null\n  dependent_locality: null\naddress_advanced:\n  country_code: US\n  langcode: en\n  address_line1: '1098 Alta Ave'\n  address_line2: ''\n  locality: 'Mountain View'\n  administrative_area: CA\n  postal_code: '94043'\n  given_name: null\n  additional_name: null\n  family_name: null\n  organization: null\n  sorting_code: null\n  dependent_locality: null\naddress_none: null\naddress_multiple:\n  - country_code: US\n    langcode: en\n    given_name: John\n    family_name: Smith\n    organization: 'Google Inc.'\n    address_line1: '1098 Alta Ave'\n    address_line2: ''\n    locality: 'Mountain View'\n    administrative_area: CA\n    postal_code: '94043'");

    // Check text formatting.
    $this
      ->drupalGet("/admin/structure/webform/manage/test_element_address/submission/{$sid}/text");
    $this
      ->assertRaw('address_basic:
John Smith
Google Inc.
1098 Alta Ave
Mountain View, CA 94043
United States

address_advanced:
1098 Alta Ave
Mountain View, CA 94043
United States

address_none:
{Empty}

address_multiple:
- John Smith
  Google Inc.
  1098 Alta Ave
  Mountain View, CA 94043
  United States');

    /**************************************************************************/

    // Schema.

    /**************************************************************************/
    $field_storage = FieldStorageConfig::create([
      'entity_type' => 'node',
      'field_name' => 'address',
      'type' => 'address',
    ]);
    $schema = $field_storage
      ->getSchema();

    /** @var \Drupal\webform\Plugin\WebformElementManagerInterface $element_manager */
    $element_manager = \Drupal::service('plugin.manager.webform.element');

    /** @var \Drupal\webform\Plugin\WebformElement\Address $element_plugin */
    $element_plugin = $element_manager
      ->getElementInstance([
      '#type' => 'address',
    ]);

    // Get webform address element plugin.
    $element = [];
    $element_plugin
      ->initializeCompositeElements($element);

    // Check composite elements against address schema.
    $composite_elements = $element['#webform_composite_elements'];
    $diff_composite_elements = array_diff_key($composite_elements, $schema['columns']);
    $this
      ->debug($diff_composite_elements);
    $this
      ->assertEmpty($diff_composite_elements);

    // Check composite elements maxlength against address schema.
    foreach ($schema['columns'] as $column_name => $column) {
      $this
        ->assertEqual($composite_elements[$column_name]['#maxlength'], $column['length']);
    }
  }

}

Classes

Namesort descending Description
WebformElementAddressTest Tests for webform address element.