Make the "Do not delete users who posted comments" option safer when the `comment` module is not installed

Created on 17 February 2025, 14 days ago

Problem/Motivation

Currently, the module allows enabling the "Do not delete users who posted comments." option even when the `comment` module is not installed.
If this option is enabled and the `comment` module is not installed, the cron process fails with an exception because it attempts to query the `comment` table, which does not exist.

This is risky because:
- `user_prune` does not declare `comment` as a dependency, so there is no guarantee that the `comment` module is installed.
- Users can enable the option without knowing it depends on `comment`, causing cron errors later.

Steps to reproduce

1. Install and enable `user_prune` on a site without the `comment` module installed.
2. Go to /admin/structure/user-prune/settings.
3. Enable the option "Do not delete users who posted comments.".
4. Run cron.
5. An error occurs because the module attempts to query the `comment` table, which does not exist.

Proposed resolution

Make the comment-related option safer by implementing two changes:

1. In the settings form (`UserPruneSettingsForm`):
Hide the field:

   $form['user_with_comment'] = [
     '#type' => 'checkbox',
     '#title' => $this->t('Do not delete users who posted comments.'),
     '#default_value' => $config->get('user_with_comment'),
   ];
   
   // If the `comment` module is not installed:
   
   if (!\Drupal::moduleHandler()->moduleExists('comment')) {
     $form['user_with_comment']['#access'] = FALSE;
   }

2. In the user deletion logic (where the `comment` table is queried):
- Check if the `comment` module is installed before running the comment-related logic.
- Ignore the `user_with_comment` setting if `comment` is not installed, even if it is set to `TRUE` in the configuration.

Example in `.module` file:

   if ($config->get('user_with_comment') && \Drupal::moduleHandler()->moduleExists('comment')) {
     // Logic to exclude users who posted comments.
   }

Remaining tasks

- Implement the two proposed changes.
- Verify that cron works correctly with and without the `comment` module installed.
- Test the settings form with and without the `comment` module.

User interface changes

The "Do not delete users who posted comments." field will be hidden in the settings form if the `comment` module is not installed.

API changes

None.

Data model changes

None.

πŸ› Bug report
Status

Active

Version

1.5

Component

Code

Created by

πŸ‡ΊπŸ‡ΎUruguay gonzalo2683

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @gonzalo2683
  • Pipeline finished with Failed
    11 days ago
    Total: 153s
    #430103
  • Pipeline finished with Failed
    11 days ago
    Total: 173s
    #430106
  • Pipeline finished with Failed
    11 days ago
    Total: 159s
    #430113
  • Pipeline finished with Failed
    11 days ago
    Total: 156s
    #430116
  • Pipeline finished with Success
    11 days ago
    Total: 158s
    #430117
  • Pipeline finished with Success
    11 days ago
    Total: 164s
    #430155
  • πŸ‡ΊπŸ‡ΎUruguay gonzalo2683

    Here are the key changes and improvements made:

    1. Fixed dependencies to handle cases when the Comment module is disabled but referenced in configuration
    2. Added functional tests to verify module behavior and core functionality
    3. Implemented Kernel tests to validate filtering logic, with room for future test expansion
    4. Ensured Drupal 11 compatibility by removing deprecated function calls
    5. Configured GitLab CI pipeline for automated test execution

    The code is now ready for review and integration into the main codebase.

  • Pipeline finished with Success
    7 days ago
    Total: 159s
    #432931
Production build 0.71.5 2024