Issue #3422872 by kunal.sachdev, Wim Leers: Add validation constraints to contact.settings

(cherry picked from commit c41bea2d63)
merge-requests/8190/head
Lee Rowlands 2024-04-12 07:51:51 +10:00
parent 43e3efe009
commit 55849506c4
No known key found for this signature in database
GPG Key ID: 2B829A3DF9204DC4
8 changed files with 90 additions and 6 deletions

View File

@ -52,6 +52,13 @@ trait SchemaCheckTrait {
'This value should not be blank.',
],
],
'contact.settings' => [
// @todo Simple config cannot have dependencies on any other config.
// Remove this in https://www.drupal.org/project/drupal/issues/3425992.
'default_form' => [
"The 'contact.form.feedback' config does not exist.",
],
],
'editor.editor.*' => [
// @todo Fix stream wrappers not being available early enough in
// https://www.drupal.org/project/drupal/issues/3416735

View File

@ -37,20 +37,35 @@ contact.form.*:
contact.settings:
type: config_object
label: 'Contact settings'
constraints:
FullyValidatable: ~
mapping:
default_form:
type: string
label: 'Default form identifier'
# It is possible to not configure a default form.
# @see \Drupal\contact\ContactFormEditForm::save()
nullable: true
constraints:
ConfigExists:
prefix: contact.form.
flood:
# @see \Drupal\Core\Flood\FloodInterface::isAllowed()
type: mapping
label: 'Flood control'
mapping:
limit:
type: integer
label: 'Limit'
label: 'Limit (messages per interval)'
constraints:
Range:
min: 1
interval:
type: integer
label: 'Interval'
label: 'Interval (seconds)'
constraints:
Range:
min: 1
user_default_enabled:
type: boolean
label: 'Personal contact form enabled by default'

View File

@ -13,3 +13,15 @@ function contact_removed_post_updates() {
'contact_post_update_add_message_redirect_field_to_contact_form' => '9.0.0',
];
}
/**
* Converts empty `default_form` in settings to NULL.
*/
function contact_post_update_set_empty_default_form_to_null(): void {
$config = \Drupal::configFactory()->getEditable('contact.settings');
// 'default_form' in 'contact.settings' config must be stored as NULL if it
// is empty.
if ($config->get('default_form') === '') {
$config->set('default_form', NULL)->save();
}
}

View File

@ -11,6 +11,11 @@ source:
process:
user_default_enabled: contact_default_status
'flood/limit': contact_hourly_threshold
'flood/interval':
plugin: default_value
# It was defaulted to 3600 in D6.
# @see https://api.drupal.org/api/drupal/includes%21common.inc/function/flood_is_allowed/6.x
default_value: 3600
default_form:
plugin: migration_lookup
migration: contact_category

View File

@ -11,6 +11,11 @@ source:
process:
user_default_enabled: contact_default_status
'flood/limit': contact_threshold_limit
'flood/interval':
plugin: default_value
# It was defaulted to 3600 in D7.
# @see https://api.drupal.org/api/drupal/includes%21common.inc/function/flood_is_allowed/7.x
default_value: 3600
default_form:
plugin: migration_lookup
migration: contact_category

View File

@ -50,9 +50,13 @@ class ContactController extends ControllerBase {
// Use the default form if no form has been passed.
if (empty($contact_form)) {
$contact_form = $this->entityTypeManager()
->getStorage('contact_form')
->load($config->get('default_form'));
$default_form = $config->get('default_form');
// Load the default form, if configured.
if (!is_null($default_form)) {
$contact_form = $this->entityTypeManager()
->getStorage('contact_form')
->load($default_form);
}
// If there are no forms, do not display the form.
if (empty($contact_form)) {
if ($this->currentUser()->hasPermission('administer contact forms')) {

View File

@ -256,7 +256,7 @@ class ContactSitewideTest extends BrowserTestBase {
// Test contact form with no default form selected.
$this->config('contact.settings')
->set('default_form', '')
->set('default_form', NULL)
->save();
$this->drupalGet('contact');
$this->assertSession()->statusCodeEquals(404);

View File

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\contact\Functional\Update;
use Drupal\FunctionalTests\Update\UpdatePathTestBase;
/**
* Tests the upgrade path for making 'default_form' in 'contact.settings' config to NULL.
*
* @group contact
* @see contact_post_update_set_empty_default_form_to_null()
*/
class NullDefaultFormTest extends UpdatePathTestBase {
/**
* {@inheritdoc}
*/
protected function setDatabaseDumpFiles() {
$this->databaseDumpFiles = [
__DIR__ . '/../../../../../system/tests/fixtures/update/drupal-9.4.0.filled.standard.php.gz',
];
}
/**
* Tests the upgrade path for updating empty 'default_form' to NULL.
*/
public function testRunUpdates(): void {
$this->config('contact.settings')->set('default_form', '')->save();
$this->assertSame('', $this->config('contact.settings')->get('default_form'));
$this->runUpdates();
$this->assertNull($this->config('contact.settings')->get('default_form'));
}
}