-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide text_format render element in JSON Form Widget (#4371)
Co-authored-by: Stefan Korn <[email protected]>
- Loading branch information
1 parent
fd97b42
commit cf63eea
Showing
6 changed files
with
147 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ | |
use Drupal\Component\Utility\EmailValidator; | ||
use Drupal\Core\Form\FormState; | ||
use Drupal\Core\StringTranslation\TranslationManager; | ||
use Drupal\field\Plugin\migrate\source\d6\Field; | ||
use Drupal\json_form_widget\FieldTypeRouter; | ||
use Drupal\json_form_widget\StringHelper; | ||
use MockChain\Options; | ||
|
||
|
@@ -73,4 +75,35 @@ public function testEmailValidate() { | |
$this->assertEmpty($form_state->getErrors()); | ||
} | ||
|
||
/** | ||
* Test that StringHelper::handleStringElement correctly reemoves mailto:. | ||
*/ | ||
public function testHandleStringElement() { | ||
$definition = [ | ||
'schema' => (object) [ | ||
'title' => 'Email', | ||
'description' => 'Email address for the contact name.', | ||
'type' => 'string', | ||
"pattern" => "^mailto:[\\w\\_\\~\\!\\$\\&\\'\\(\\)\\*\\+\\,\\;\\=\\:.-]+@[\\w.-]+\\.[\\w.-]+?$|[\\w\\_\\~\\!\\$\\&\\'\\(\\)\\*\\+\\,\\;\\=\\:.-]+@[\\w.-]+\\.[\\w.-]+?$", | ||
], | ||
'name' => 'hasEmail', | ||
]; | ||
|
||
$string_helper = new StringHelper(new EmailValidator()); | ||
$field_type_router = $this->createMock(FieldTypeRouter::class); | ||
$field_type_router->method('getSchema')->willReturn((object) [ | ||
'type' => 'object', | ||
'properties' => [ | ||
'hasEmail' => $definition['schema'], | ||
], | ||
]); | ||
$string_helper->setBuilder($field_type_router); | ||
|
||
$data = 'mailto:[email protected]'; | ||
|
||
$result = $string_helper->handleStringElement($definition, $data); | ||
print_r($result['#default_value']); | ||
$this->assertEquals('[email protected]', $result['#default_value']); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -203,6 +203,20 @@ public function testFlattenValuesEmptyDistribution() { | |
$this->assertEquals([], $result); | ||
} | ||
|
||
/** | ||
* Test mailto: fix. | ||
*/ | ||
public function testFlattenValuesMailto() { | ||
$value_handler = new ValueHandler(); | ||
|
||
$schema = json_decode('{"type":"string"}'); | ||
$formValues = [ | ||
'hasEmail' => '[email protected]', | ||
]; | ||
$result = $value_handler->flattenValues($formValues, "hasEmail", $schema); | ||
$this->assertEquals('mailto:[email protected]', $result); | ||
} | ||
|
||
/** | ||
* Test values for datetime elements. | ||
*/ | ||
|
@@ -226,7 +240,7 @@ public function testDatetimeValues() { | |
]; | ||
$expected = $date->format('c', ['timezone' => 'UTC']); | ||
$result = $value_handler->handleStringValues($formValues, 'modified'); | ||
$this->assertEquals($result, $expected); | ||
$this->assertEquals($expected, $result); | ||
|
||
// Test date_range. | ||
$date = new DrupalDateTime('2020-05-11T15:06:39.000Z'); | ||
|
@@ -240,7 +254,38 @@ public function testDatetimeValues() { | |
]; | ||
$expected = '2020-05-11T15:06:39.000Z/2020-05-15T15:00:00.000Z'; | ||
$result = $value_handler->handleStringValues($formValues, 'temporal'); | ||
$this->assertEquals($result, $expected); | ||
$this->assertEquals($expected, $result); | ||
} | ||
|
||
/** | ||
* Test textFormat handling. | ||
*/ | ||
public function testTextFormat() { | ||
$value_handler = new ValueHandler(); | ||
|
||
// Test textFormat. | ||
$formValues = [ | ||
'description' => [ | ||
'value' => 'This is a description', | ||
'format' => 'basic_html', | ||
], | ||
]; | ||
$expected = 'This is a description'; | ||
$result = $value_handler->handleStringValues($formValues, 'description'); | ||
$this->assertEquals($expected, $result); | ||
} | ||
|
||
public function testIntegerValues() { | ||
$value_handler = new ValueHandler(); | ||
|
||
// Test integer. | ||
$formValues = [ | ||
'integer' => "1", | ||
]; | ||
$expected = 1; | ||
$result = $value_handler->handleIntegerValues($formValues, 'integer'); | ||
$this->assertIsInt($result); | ||
$this->assertEquals($expected, $result); | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters