diff --git a/comment_notify.module b/comment_notify.module index 7ed8c07d3f576c180f8634120a31bd9283399b24..7359efb104444e131c85a5bb58d3dc37901a027e 100644 --- a/comment_notify.module +++ b/comment_notify.module @@ -265,9 +265,30 @@ function _comment_notify_submit_user_form(array &$form, FormStateInterface $form } } +/** + * Implements hook_user_delete(). + */ +function comment_notify_user_predelete(\Drupal\Core\Entity\EntityInterface $entity) { + // This hook is invoked when the account is deleted. + comment_notify_remove_user_settings($entity->id()); +} +/** + * Implements hook_user_cancel(). + */ function comment_notify_user_cancel($edit, $account, $method) { + // This hook is invoked when the account is disabled. + comment_notify_remove_user_settings($account->id()); +} + +/** + * Remove the user settings of of the user with the given $uid + * + * @param int $uid + * The user id. + */ +function comment_notify_remove_user_settings($uid) { module_load_include('inc', 'comment_notify', 'comment_notify'); - comment_notify_delete_user_notification_setting($account->uid); + comment_notify_delete_user_notification_setting($uid); } /** diff --git a/tests/src/Functional/CommentNotifyUserPreferencesTest.php b/tests/src/Functional/CommentNotifyUserPreferencesTest.php index 245e1c6e1df019600e228a9b06b29183fc49fdd1..5206031e4e0ac9ed2be2feaf74c297d160c32ad3 100644 --- a/tests/src/Functional/CommentNotifyUserPreferencesTest.php +++ b/tests/src/Functional/CommentNotifyUserPreferencesTest.php @@ -242,4 +242,29 @@ class CommentNotifyUserPreferencesTest extends CommentNotifyTestBase { } + /** + * Test that when the user account is canceled or deleted all the settings + * related with the CommentNotify module are deleted. + */ + public function testUserCancelAccount() { + $cancel_method_options = [ + 'user_cancel_block', + 'user_cancel_block_unpublish', + 'user_cancel_reassign', + ]; + + foreach ($cancel_method_options as $cancel_method_option) { + $user = $this->drupalCreateUser($this->permissions); + comment_notify_set_user_notification_setting($user->id(), COMMENT_NOTIFY_NODE, COMMENT_NOTIFY_COMMENT); + user_cancel([], $user->id(), $cancel_method_option); + $this->assertTrue(is_null(comment_notify_get_user_notification_setting($user->id()))); + } + + // Delete Account. + $user = $this->drupalCreateUser($this->permissions); + comment_notify_set_user_notification_setting($user->id(), COMMENT_NOTIFY_NODE, COMMENT_NOTIFY_COMMENT); + $user->delete(); + $this->assertTrue(is_null(comment_notify_get_user_notification_setting($user->id()))); + } + }