Skip to content
Snippets Groups Projects
Verified Commit f562df44 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3436164 by omkar.podey, kunal.sachdev, pradhumanjain2311, smustgrave,...

Issue #3436164 by omkar.podey, kunal.sachdev, pradhumanjain2311, smustgrave, Wim Leers, narendraR, alexpott, larowlan, borisson_: Add validation constraints to user.settings
parent 87bf1eef
Branches
Tags
24 merge requests!11131[10.4.x-only-DO-NOT-MERGE]: Issue ##2842525 Ajax attached to Views exposed filter form does not trigger callbacks,!9470[10.3.x-only-DO-NOT-MERGE]: #3331771 Fix file_get_contents(): Passing null to parameter,!8736Update the Documention As per the Function uses.,!8528Issue #3456871 by Tim Bozeman: Support NULL services,!8513Issue #3453786: DefaultSelection should document why values for target_bundles NULL and [] behave as they do,!8126Added escape fucntionality on admintoolbar close icon,!3878Removed unused condition head title for views,!3818Issue #2140179: $entity->original gets stale between updates,!3742Issue #3328429: Create item list field formatter for displaying ordered and unordered lists,!3731Claro: role=button on status report items,!3651Issue #3347736: Create new SDC component for Olivero (header-search),!3355Issue #3209129: Scrolling problems when adding a block via layout builder,!3154Fixes #2987987 - CSRF token validation broken on routes with optional parameters.,!3133core/modules/system/css/components/hidden.module.css,!2964Issue #2865710 : Dependencies from only one instance of a widget are used in display modes,!2812Issue #3312049: [Followup] Fix Drupal.Commenting.FunctionComment.MissingReturnType returns for NULL,!2378Issue #2875033: Optimize joins and table selection in SQL entity query implementation,!2062Issue #3246454: Add weekly granularity to views date sort,!10223132456: Fix issue where views instances are emptied before an ajax request is complete,!877Issue #2708101: Default value for link text is not saved,!617Issue #3043725: Provide a Entity Handler for user cancelation,!579Issue #2230909: Simple decimals fail to pass validation,!560Move callback classRemove outside of the loop,!555Issue #3202493
Pipeline #210588 canceled
Pipeline: drupal

#210590

    ......@@ -3,6 +3,8 @@
    user.settings:
    type: config_object
    label: 'User settings'
    constraints:
    FullyValidatable: ~
    mapping:
    anonymous:
    type: required_label
    ......@@ -41,12 +43,27 @@ user.settings:
    register:
    type: string
    label: 'Who can register accounts?'
    # Choices are derived from the constants.
    # @see \Drupal\user\UserInterface::REGISTER_*
    # @todo Convert to use Enum in https://www.drupal.org/project/drupal/issues/3450782
    constraints:
    Choice:
    choices:
    - 'visitors'
    - 'admin_only'
    - 'visitors_admin_approval'
    cancel_method:
    type: string
    label: 'When cancelling a user account'
    constraints:
    UserCancelMethod: []
    password_reset_timeout:
    type: integer
    label: 'Password reset timeout'
    # @todo Increase min in https://www.drupal.org/i/3441772
    constraints:
    Range:
    min: 1
    password_strength:
    type: boolean
    label: 'Enable password strength indicator'
    ......
    ......@@ -16,6 +16,35 @@ process:
    'notify/status_blocked': user_mail_status_blocked_notify
    'notify/status_activated': user_mail_status_activated_notify
    verify_mail: user_email_verification
    # @see core/modules/user/config/install/user.settings.yml
    # Default values
    cancel_method:
    plugin: default_value
    default_value: user_cancel_block
    password_reset_timeout:
    plugin: default_value
    default_value: 86400
    password_strength:
    plugin: default_value
    default_value: true
    'notify/cancel_confirm':
    plugin: default_value
    default_value: true
    'notify/password_reset':
    plugin: default_value
    default_value: true
    'notify/status_canceled':
    plugin: default_value
    default_value: false
    'notify/register_admin_created':
    plugin: default_value
    default_value: true
    'notify/register_no_approval_required':
    plugin: default_value
    default_value: true
    'notify/register_pending_approval':
    plugin: default_value
    default_value: true
    register:
    plugin: static_map
    source: user_register
    ......
    ......@@ -16,6 +16,35 @@ process:
    'notify/status_blocked': user_mail_status_blocked_notify
    'notify/status_activated': user_mail_status_activated_notify
    verify_mail: user_email_verification
    # @see core/modules/user/config/install/user.settings.yml
    # Default values
    cancel_method:
    plugin: default_value
    default_value: user_cancel_block
    password_reset_timeout:
    plugin: default_value
    default_value: 86400
    password_strength:
    plugin: default_value
    default_value: true
    'notify/cancel_confirm':
    plugin: default_value
    default_value: true
    'notify/password_reset':
    plugin: default_value
    default_value: true
    'notify/status_canceled':
    plugin: default_value
    default_value: false
    'notify/register_admin_created':
    plugin: default_value
    default_value: true
    'notify/register_no_approval_required':
    plugin: default_value
    default_value: true
    'notify/register_pending_approval':
    plugin: default_value
    default_value: true
    register:
    plugin: static_map
    source: user_register
    ......
    <?php
    declare(strict_types=1);
    namespace Drupal\user\Plugin\Validation\Constraint;
    use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
    use Drupal\Core\StringTranslation\TranslatableMarkup;
    use Drupal\Core\Validation\Attribute\Constraint;
    use Symfony\Component\DependencyInjection\ContainerInterface;
    use Symfony\Component\Validator\Constraints\Choice;
    #[Constraint(
    id: 'UserCancelMethod',
    label: new TranslatableMarkup('UserCancelMethod', [], ['context' => 'Validation']),
    )]
    class UserCancelMethodsConstraint implements ContainerFactoryPluginInterface {
    /**
    * {@inheritdoc}
    */
    public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): Choice {
    $configuration['choices'] = array_keys(user_cancel_methods()['#options']);
    return new Choice($configuration);
    }
    }
    <?php
    declare(strict_types=1);
    namespace Drupal\Tests\user\Kernel;
    use Drupal\KernelTests\KernelTestBase;
    /**
    * Tests validating user modules' configuration.
    *
    * @group user
    */
    class UserConfigValidationTest extends KernelTestBase {
    /**
    * {@inheritdoc}
    */
    protected static $modules = ['system', 'user'];
    /**
    * {@inheritdoc}
    */
    protected function setUp(): void {
    parent::setUp();
    $this->installConfig('user');
    }
    /**
    * Data provider for testUserSettings().
    *
    * @return array
    */
    public static function providerTestUserSettings(): array {
    return [
    "Invalid register" => [
    'register',
    'somebody',
    'The value you selected is not a valid choice.',
    ],
    "Invalid cancel_method" => [
    'cancel_method',
    'somebody',
    'The value you selected is not a valid choice.',
    ],
    "Invalid password_reset_timeout" => [
    'password_reset_timeout',
    '0',
    'This value should be <em class="placeholder">1</em> or more.',
    ],
    ];
    }
    /**
    * Tests invalid values in 'user.settings' config properties.
    *
    * @dataProvider providerTestUserSettings
    */
    public function testUserSettings($property, $property_value, $expected_message): void {
    $config_name = 'user.settings';
    $config = $this->config($config_name);
    $violations = $this->container->get('config.typed')
    ->createFromNameAndData($config_name, $config->set($property, $property_value)->get())
    ->validate();
    $this->assertCount(1, $violations);
    $this->assertSame($property, $violations[0]->getPropertyPath());
    $this->assertSame($expected_message, (string) $violations[0]->getMessage());
    }
    }
    ......@@ -103,6 +103,7 @@ public function testUserMailsSent($op, array $mail_keys): void {
    * @dataProvider userMailsProvider
    */
    public function testUserMailsNotSent($op): void {
    $this->installConfig('user');
    $this->config('user.settings')->set('notify.' . $op, FALSE)->save();
    $return = _user_mail_notify($op, $this->createUser());
    $this->assertNull($return);
    ......
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment