WebformElementAddressTest.php in Webform 8.5
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;
class WebformElementAddressTest extends WebformElementBrowserTestBase {
public static $modules = [
'webform',
'address',
'node',
];
protected static $testWebforms = [
'test_element_address',
];
public function testAddress() {
$this
->drupalLogin($this->rootUser);
$webform = Webform::load('test_element_address');
$this
->drupalGet('/webform/test_element_address');
$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>');
$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="<div class="webform-element-help--title">address_advanced</div><div class="webform-element-help--content">This is help text</div>"><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>');
}
$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'");
$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');
$field_storage = FieldStorageConfig::create([
'entity_type' => 'node',
'field_name' => 'address',
'type' => 'address',
]);
$schema = $field_storage
->getSchema();
$element_manager = \Drupal::service('plugin.manager.webform.element');
$element_plugin = $element_manager
->getElementInstance([
'#type' => 'address',
]);
$element = [];
$element_plugin
->initializeCompositeElements($element);
$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);
foreach ($schema['columns'] as $column_name => $column) {
$this
->assertEqual($composite_elements[$column_name]['#maxlength'], $column['length']);
}
}
}