class WebformHtmlHelperTest in Webform 8.5
Same name and namespace in other branches
- 6.x tests/src/Unit/Utility/WebformHtmlHelperTest.php \Drupal\Tests\webform\Unit\Utility\WebformHtmlHelperTest
Tests webform HTML helper.
@group webform
@coversDefaultClass \Drupal\webform\Utility\WebformHtmlHelper
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\webform\Unit\Utility\WebformHtmlHelperTest
Expanded class hierarchy of WebformHtmlHelperTest
File
- tests/
src/ Unit/ Utility/ WebformHtmlHelperTest.php, line 16
Namespace
Drupal\Tests\webform\Unit\UtilityView source
class WebformHtmlHelperTest extends UnitTestCase {
/**
* Tests WebformHtmlHelper::containsToPlainText().
*
* @param string $text
* Text to run through WebformHtmlHelper::toPlainText().
* @param string $expected
* The expected result from calling the function.
*
* @see \Drupal\webform\Utility\WebformHtmlHelper::toPlainText()
*
* @dataProvider providerToPlainText
*/
public function testToPlainText($text, $expected) {
$config_factory = $this
->getConfigFactoryStub([
'webform.settings' => [
'element' => [
'allowed_tags' => 'b',
],
],
]);
$container = new ContainerBuilder();
$container
->set('config.factory', $config_factory);
\Drupal::setContainer($container);
/**************************************************************************/
$result = WebformHtmlHelper::toPlainText($text);
$this
->assertEquals((string) $expected, (string) $result, $text);
}
/**
* Data provider for testToPlainText().
*
* @see testToPlainText()
*/
public function providerToPlainText() {
$tests = [];
$tests[] = [
'some text',
'some text',
];
$tests[] = [
'some & text',
'some & text',
];
$tests[] = [
'<b>some text</b>',
'some text',
];
$tests[] = [
'<script>alert(\'message\');</script><b>some text</b>',
'alert(\'message\');some text',
];
return $tests;
}
/**
* Tests WebformHtmlHelper::containsToHtmlMarkup().
*
* @param string $text
* Text to run through WebformHtmlHelper::toHtmlMarkup().
* @param string $expected
* The expected result from calling the function.
*
* @see \Drupal\webform\Utility\WebformHtmlHelper::toHtmlMarkup()
*
* @dataProvider providerToHtmlMarkup
*/
public function testToHtmlMarkup($text, $expected) {
$config_factory = $this
->getConfigFactoryStub([
'webform.settings' => [
'element' => [
'allowed_tags' => 'b',
],
],
]);
$container = new ContainerBuilder();
$container
->set('config.factory', $config_factory);
\Drupal::setContainer($container);
/**************************************************************************/
$result = WebformHtmlHelper::toHtmlMarkup($text);
$this
->assertEquals((string) $expected, (string) $result, $text);
}
/**
* Data provider for testToHtmlMarkup().
*
* @see testToHtmlMarkup()
*/
public function providerToHtmlMarkup() {
$tests = [];
$tests[] = [
'some text',
'some text',
];
$tests[] = [
'some & text',
'some & text',
];
$tests[] = [
'<b>some text</b>',
'<b>some text</b>',
];
$tests[] = [
'<script>alert(\'message\');</script><b>some text</b>',
'alert(\'message\');<b>some text</b>',
];
return $tests;
}
/**
* Tests WebformHtmlHelper::containsHtml().
*
* @param string $text
* Text to run through WebformHtmlHelper::containsHtml().
* @param string $expected
* The expected result from calling the function.
*
* @see \Drupal\webform\Utility\WebformHtmlHelper::containsHtml()
*
* @dataProvider providerContainsHtml
*/
public function testContainsHtml($text, $expected) {
$result = WebformHtmlHelper::containsHtml($text);
$this
->assertEquals($expected, $result, $text);
}
/**
* Data provider for testContainsHtml().
*
* @see testContainsHtml()
*/
public function providerContainsHtml() {
$tests = [];
$tests[] = [
'some text',
FALSE,
];
$tests[] = [
'<b>some text</b>',
TRUE,
];
return $tests;
}
/**
* Tests WebformHtmlHelper::hasBlockTags().
*
* @param string $text
* Text to run through WebformHtmlHelper::hasBlockTags().
* @param string $expected
* The expected result from calling the function.
*
* @see \Drupal\webform\Utility\WebformHtmlHelper::hasBlockTags()
*
* @dataProvider providerHasBlockTags
*/
public function testHasBlockTags($text, $expected) {
$result = WebformHtmlHelper::hasBlockTags($text);
$this
->assertEquals($expected, $result, $text);
}
/**
* Data provider for testHasBlockTags().
*
* @see testHasBlockTags()
*/
public function providerHasBlockTags() {
$tests = [];
$tests[] = [
'some text',
FALSE,
];
$tests[] = [
'<b>some text</b>',
FALSE,
];
$tests[] = [
'<p>some text</p>',
TRUE,
];
$tests[] = [
'some text<br />',
TRUE,
];
return $tests;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 340 | |
WebformHtmlHelperTest:: |
public | function | Data provider for testContainsHtml(). | |
WebformHtmlHelperTest:: |
public | function | Data provider for testHasBlockTags(). | |
WebformHtmlHelperTest:: |
public | function | Data provider for testToHtmlMarkup(). | |
WebformHtmlHelperTest:: |
public | function | Data provider for testToPlainText(). | |
WebformHtmlHelperTest:: |
public | function | Tests WebformHtmlHelper::containsHtml(). | |
WebformHtmlHelperTest:: |
public | function | Tests WebformHtmlHelper::hasBlockTags(). | |
WebformHtmlHelperTest:: |
public | function | Tests WebformHtmlHelper::containsToHtmlMarkup(). | |
WebformHtmlHelperTest:: |
public | function | Tests WebformHtmlHelper::containsToPlainText(). |