Commit 7f1624f4 authored by git's avatar git Committed by Attila Fekete
Browse files

Issue #2830527 by beutlerk, attila.fekete: Validation of URLs

parent 7648ca4c
# Schema for the configuration files of the soundcloudfield module.
soundcloudfield.settings:
type: mapping
label: 'SoundCloud'
mapping:
url:
type: varchar
label: 'URL'
field.soundcloud.settings:
type: mapping
......@@ -7,3 +14,60 @@ field.soundcloud.settings:
field.soundcloud.instance_settings:
type: mapping
label: 'SoundCloud field settings'
field.storage_settings.soundcloud:
type: mapping
label: 'SoundCloud'
mapping:
url:
type: varchar
label: 'URL'
field.widget.settings.soundcloud_url:
type: mapping
label: 'SoundCloud URL'
mapping:
placeholder_url:
type: string
label: 'Placeholder for URL'
url:
type: string
label: 'SoundCloud URL'
field.formatter.settings.soundcloud_default:
type: mapping
label: 'Default (HTML5 player)'
mapping:
soundcloud_player_type:
type: string
label: 'HTML5 player type'
soundcloud_player_width:
type: integer
label: 'Width'
soundcloud_player_height:
type: integer
label: 'Height'
soundcloud_player_height_sets:
type: integer
label: 'Height for sets'
soundcloud_player_visual_height:
type: integer
label: 'Height of the visual player'
soundcloud_player_autoplay:
type: boolean
label: 'Play audio automatically when loaded (autoplay).'
soundcloud_player_color:
type: string
label: 'Player color.'
soundcloud_player_hiderelated:
type: boolean
label: 'Hide raleted tracks.'
soundcloud_player_showartwork:
type: boolean
label: 'Show artwork.'
soundcloud_player_showcomments:
type: boolean
label: 'Show comments.'
soundcloud_player_showplaycount:
type: boolean
label: 'how play count.'
......@@ -31,12 +31,12 @@ class SoundCloudWidget extends WidgetBase {
*/
public static function defaultSettings() {
// todo: investigate
$settings = parent::defaultSettings();
// $settings = parent::defaultSettings();
return array(
return [
'url' => '',
'placeholder_url' => '',
) + parent::defaultSettings();
] + parent::defaultSettings();
}
/**
......@@ -51,11 +51,15 @@ class SoundCloudWidget extends WidgetBase {
'#title' => $this->t('SoundCloud URL'),
'#placeholder' => $this->getSetting('placeholder_url'), // investigate
'#default_value' => isset($item->url) ? $item->url : NULL,
'#element_validate' => array(array(get_called_class(), 'validateSoundCloudUriElement')),
'#element_validate' => [[get_called_class(), 'validateSoundCloudUriElement']],
'#maxlength' => 2048,
'#required' => $element['#required'],
);
if (empty($element['url']['#description'])) {
$element['url']['#description'] = $this->t('Enter the SoundCloud URL. A valid example: https://soundcloud.com/archives-5/purl-form-is-emptiness.');
}
if ($this->fieldDefinition->getFieldStorageDefinition()->getCardinality() == 1) {
$element += array(
'#type' => 'fieldset',
......@@ -101,11 +105,14 @@ class SoundCloudWidget extends WidgetBase {
}
/**
* Form element validation handler for ...().
* Form element validation handler for the 'url' element.
*/
function validateSoundCloudUriElement(&$element, FormStateInterface $form_state, $form) {
public static function validateSoundCloudUriElement($element, FormStateInterface $form_state, $form) {
$input = $element['#value'];
if (!empty($input) && !preg_match('@^https?://soundcloud\.com/([^"\&]+)@i', $input, $matches)) {
$form_state->setError($element, t('Please provide a valid SoundCloud URL.'));
}
}
}
\ No newline at end of file
}
<?php
namespace Drupal\soundcloudfield\Tests;
use Drupal\simpletest\WebTestBase;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
use Drupal\Component\Utility\Unicode;
/**
* Test case for custom time field
*
* @group soundcloudfield
*/
class SoundCloudWidgetValidationTest extends WebTestBase {
/**
* Modules to install
*
* @var array
*/
public static $modules = ['entity_test', 'node', 'soundcloudfield'];
/**
* Random fieldName
*/
protected $fieldName;
/**
* A field to use in this test class.
*
* @var \Drupal\field\Entity\FieldStorageConfig
*/
protected $fieldStorage;
/**
* The instance used in this test class.
*
* @var \Drupal\field\Entity\FieldConfig
*/
protected $field;
protected function setUp() {
parent::setUp();
$this->drupalLogin($this->drupalCreateUser([
'view test entity',
'administer entity_test content',
'link to any page',
]));
}
/**
* Test an a valid SoundCloud URL
*/
function testSoundCloudUrlValid() {
$field = $this->getField();
// Display creation form.
$this->drupalGet('entity_test/add');
$this->assertFieldByName("{$this->fieldName}[0][url]",
'',
'SoundCloud URL');
$this->assertRaw('value=""');
// Create a path alias.
\Drupal::service('path.alias_storage')->save('/admin', '/a/path/alias');
// Create a node to test the link widget.
$node = $this->drupalCreateNode();
$validEntries = ['https://soundcloud.com/dioxidmusic/soncha-gona-the-ground-20-tribute-to-the-100'];
$this->assertValidEntries($validEntries);
}
/**
* Test an invalid URLs or strings
*/
function testSoundCloudUrlInvalid() {
$field = $this->getField();
// Display creation form.
$this->drupalGet('entity_test/add');
$this->assertFieldByName("{$this->fieldName}[0][url]",
'',
'SoundCloud URL');
$this->assertRaw('value=""');
// Create a path alias.
\Drupal::service('path.alias_storage')->save('/admin', '/a/path/alias');
// Create a node to test the link widget.
$node = $this->drupalCreateNode();
$validationError = 'Please insert a valid SoundCloud url.';
$invalidEntries = [
'only a string' => $validationError,
'https://lmgtfy.com/' => $validationError,
];
$this->assertInvalidEntries($invalidEntries);
}
/**
* Asserts that valid URLs can be submitted.
*
* @param array $validEntries
* An array of valid URL entries.
*/
protected function assertValidEntries(array $validEntries) {
foreach ($validEntries as $key => $validEntry) {
$edit = [
"{$this->fieldName}[0][url]" => $validEntry,
];
$this->drupalPostForm(NULL, $edit, t('Save'));
preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
$id = $match[1];
$this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
$this->assertRaw($validEntry);
}
}
/**
* Asserts that invalid URLs cannot be submitted.
*
* @param array $invalidEntries
* An array of invalid URL entries.
*/
protected function assertInvalidEntries(array $invalidEntries) {
foreach ($invalidEntries as $invalidValue => $errorMessage) {
$edit = [
"{$this->fieldName}[0][url]" => $invalidValue,
];
$this->drupalPostForm('entity_test/add', $edit, t('Save'));
$this->assertText(t($errorMessage, ['@link_path' => $invalidValue]));
}
}
/**
* Get the field for the test with default settings.
*/
private function getField() {
$this->fieldName = Unicode::strtolower($this->randomMachineName());
//db field
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => $this->fieldName,
'entity_type' => 'entity_test',
'type' => 'soundcloud',
'cardinality' => 1,
]);
$this->fieldStorage->save();
//field type
$this->field = FieldConfig::create([
'field_storage' => $this->fieldStorage,
'bundle' => 'entity_test',
'settings' => [
'title' => DRUPAL_DISABLED,
],
]);
$this->field->save();
//form display
entity_get_form_display('entity_test', 'entity_test', 'default')
->setComponent($this->fieldName,
[
'type' => 'soundcloud_url',
])
->save();
//display
entity_get_display('entity_test', 'entity_test', 'full')
->setComponent($this->fieldName,
[
'type' => 'soundcloud_default',
'settings' => [
'soundcloud_player_type' => 'classic',
'soundcloud_player_width' => 100,
'soundcloud_player_height' => 166,
'soundcloud_player_height_sets' => 450,
'soundcloud_player_visual_height' => 450,
'soundcloud_player_autoplay' => '',
'soundcloud_player_color' => 'ff7700',
'soundcloud_player_hiderelated' => '',
'soundcloud_player_showartwork' => '',
'soundcloud_player_showcomments' => TRUE,
'soundcloud_player_showplaycount' => '',
],
])
->save();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment