View source
<?php
namespace Drupal\Tests\webform\Functional\Element;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Entity\WebformSubmission;
class WebformElementComputedTest extends WebformElementBrowserTestBase {
public static $modules = [
'filter',
'webform',
];
protected static $testWebforms = [
'test_element_computed_token',
'test_element_computed_twig',
'test_element_computed_ajax',
];
protected function setUp() {
parent::setUp();
$this
->createFilters();
}
public function testComputedElement() {
$this
->drupalLogin($this->rootUser);
$token_webform = Webform::load('test_element_computed_token');
$this
->drupalGet('/webform/test_element_computed_token');
$this
->assertRaw('<b class="webform_computed_token_auto">simple string:</b> This is a string<br />');
$this
->drupalPostForm('/webform/test_element_computed_token', [], 'Preview');
$this
->assertRaw('<b class="webform_computed_token_auto">simple string:</b> This is a string<br />');
$this
->assertRaw('<b class="webform_computed_token_auto">complex string :</b> This is a <strong>complex</strong> string, which contains "double" and 'single' quotes with special characters like >, <, ><, and <>.<br />');
$this
->assertRaw('<b class="webform_computed_token_auto">text_format:</b> <p>This is a <strong>text format</strong> string.</p>');
$this
->assertRaw('<p>It contains "double" and \'single\' quotes with special characters like <, >, <>, and ><.</p><br />');
$this
->assertRaw('<b class="webform_computed_token_auto">xss:</b> <script>alert("XSS");</script><br />');
$this
->assertRaw('<b class="webform_computed_token_html">simple string:</b> This is a string<br />');
$this
->assertRaw('<b class="webform_computed_token_html">complex string :</b> This is a <strong>complex</strong> string, which contains "double" and 'single' quotes with special characters like >, <, ><, and <>.<br />');
$this
->assertRaw('<b class="webform_computed_token_html">text_format:</b> <p>This is a <strong>text format</strong> string.</p>');
$this
->assertRaw('<p>It contains "double" and \'single\' quotes with special characters like <, >, <>, and ><.</p><br />');
$this
->assertRaw('<b class="webform_computed_token_html">xss:</b> <script>alert("XSS");</script><br />');
$this
->assertRaw(' <div class="webform-element webform-element-type-webform-computed-token js-form-item form-item js-form-type-item form-item-webform-computed-token-text js-form-item-webform-computed-token-text" id="test_element_computed_token--webform_computed_token_text">');
$this
->assertRaw('<label>webform_computed_token_text</label>');
$this
->assertRaw('simple string: This is a string<br />');
$this
->assertRaw('complex string : This is a <strong>complex</strong> string, which contains "double" and 'single' quotes with special characters like >, <, ><, and <>.<br />');
$this
->assertRaw('xss: <script>alert("XSS");</script><br />');
$sid = $this
->postSubmission($token_webform);
$webform_submission = WebformSubmission::load($sid);
$data = $webform_submission
->getData();
$this
->debug($data['webform_computed_token_store']);
$this
->assertEqual($data['webform_computed_token_store'], "sid: {$sid}");
$result = \Drupal::database()
->select('webform_submission_data')
->fields('webform_submission_data', [
'value',
])
->condition('webform_id', 'test_element_computed_token')
->condition('name', [
'webform_computed_token_auto',
'webform_computed_token_html',
'webform_computed_token_text',
], 'IN')
->execute()
->fetchAll();
$this
->assertEmpty($result);
$this
->drupalGet('/webform/test_element_computed_twig');
$this
->assertRaw('<b class="webform_computed_twig_auto">number:</b> 2 * 2 = 4<br />');
$this
->assertFieldByName('webform_computed_twig_trim', '<em>This is trimmed</em> <br/>');
$this
->assertFieldByName('webform_computed_twig_spaceless', '<em>This is spaceless</em><br/>');
$this
->drupalPostForm('/webform/test_element_computed_twig', [], 'Preview');
$this
->assertRaw('<b class="webform_computed_twig_auto">number:</b> 2 * 2 = 4<br />');
$this
->assertRaw('<b class="webform_computed_twig_auto">simple string:</b> This is a string<br />');
$this
->assertRaw('<b class="webform_computed_twig_auto">complex string:</b> This is a <strong>complex</strong> string, which contains "double" and 'single' quotes with special characters like >, <, ><, and <>.<br />');
$this
->assertRaw('<b class="webform_computed_twig_auto">text_format:</b> <p>This is a <strong>text format</strong> string.</p>');
$this
->assertRaw('<p>It contains "double" and \'single\' quotes with special characters like <, >, <>, and ><.</p><br />');
$this
->assertRaw('<b class="webform_computed_twig_auto">xss:</b> <script>alert("XSS");</script><br />');
$this
->assertRaw('<b class="webform_computed_twig_html">number:</b> 2 * 2 = 4<br />');
$this
->assertRaw('<b class="webform_computed_twig_html">simple string:</b> This is a string<br />');
$this
->assertRaw('<b class="webform_computed_twig_html">complex string:</b> This is a <strong>complex</strong> string, which contains "double" and 'single' quotes with special characters like >, <, ><, and <>.<br />');
$this
->assertRaw('<b class="webform_computed_twig_html">xss:</b> <script>alert("XSS");</script><br />');
$this
->assertRaw('number: 2 * 2 = 4<br />');
$this
->assertRaw('simple string: This is a string<br />');
$this
->assertRaw('complex string: This is a <strong>complex</strong> string, which contains "double" and 'single' quotes with special characters like >, <, ><, and <>.<br />');
$this
->assertRaw('text_format: This is a *text format* string.<br />');
$this
->assertRaw('<b class="webform_computed_twig_data">number:</b> 2 * 2 = 4<br />');
$this
->assertRaw('<b class="webform_computed_twig_data">simple string:</b> This is a string<br />');
$this
->assertRaw('<b class="webform_computed_twig_data">complex string:</b> This is a <strong>complex</strong> string, which contains "double" and 'single' quotes with special characters like >, <, ><, and <>.<br />');
$this
->assertRaw('<b class="webform_computed_twig_data">text_format:</b> <p>This is a <strong>text format</strong> string.</p>');
$this
->assertRaw('<b class="webform_computed_twig_data">xss:</b> <script>alert("XSS");</script><br />');
}
}