From fbd82f094be87367bdb5688af43899b4047fcbf4 Mon Sep 17 00:00:00 2001
From: Jon Duell <jon@jonduell.com>
Date: Wed, 21 Mar 2012 17:37:30 -0600
Subject: [PATCH] Enables pictures to be deleted when an account is deleted

---
 core/modules/file/file.module                      | 11 +++++++
 .../user/lib/Drupal/user/Tests/UserPictureTest.php | 38 ++++++++++++++++++++++
 2 files changed, 49 insertions(+)

diff --git a/core/modules/file/file.module b/core/modules/file/file.module
index 4e6c8eb..96e114b 100644
--- a/core/modules/file/file.module
+++ b/core/modules/file/file.module
@@ -908,6 +908,17 @@ function file_file_predelete(File $file) {
 }
 
 /**
+ * Implements hook_user_predelete()
+ */
+function file_user_predelete($account) {
+  // Delete user picture.
+  if (!empty($account->picture)) {
+    file_usage()->delete($account->picture, 'user', 'user', $account->uid);
+    file_delete($account->picture->fid);
+  }
+}
+
+/**
  * Render API callback: Expands the managed_file element type.
  *
  * Expands the file type to include Upload and Remove buttons, as well as
diff --git a/core/modules/user/lib/Drupal/user/Tests/UserPictureTest.php b/core/modules/user/lib/Drupal/user/Tests/UserPictureTest.php
index 0413b3b..380a811 100644
--- a/core/modules/user/lib/Drupal/user/Tests/UserPictureTest.php
+++ b/core/modules/user/lib/Drupal/user/Tests/UserPictureTest.php
@@ -255,6 +255,44 @@ function testExternalPicture() {
   }
 
   /**
+   * Tests that picture is deleted when user account is deleted.
+   */
+  function testUserCancelDeletePicture() {
+    if ($this->_directory_test) {
+      $this->drupalLogin($this->user);
+
+      $image = current($this->drupalGetTestFiles('image'));
+      $info = image_get_info($image->uri);
+
+      // Set new variables: valid dimensions, valid filesize (0 = no limit).
+      $test_dim = ($info['width'] + 10) . 'x' . ($info['height'] + 10);
+      variable_set('user_picture_dimensions', $test_dim);
+      variable_set('user_picture_file_size', 0);
+
+      $pic_path = $this->saveUserPicture($image);
+
+      // Load user object.
+      $account = user_load($this->user->uid, TRUE);
+
+      // Create administrative user to delete account.
+      $admin_user = $this->drupalCreateUser(array('administer users'));
+      $this->drupalLogin($admin_user);
+
+      // Delete regular user.
+      variable_set('user_cancel_method', 'user_cancel_reassign');
+      $this->drupalPost('user/' . $this->user->uid . '/edit', NULL, t('Cancel account'));
+
+      // Confirm deletion.
+      $this->drupalPost(NULL, NULL, t('Cancel account'));
+
+      $this->assertFalse(file_load($account->picture->fid), 'File is removed from database after account is cancelled');
+      // Clear out PHP's file stat cache so we see the current value.
+      clearstatcache();
+      $this->assertFalse(is_file($pic_path), 'File is removed from file system');
+    }
+  }
+
+  /**
    * Tests deletion of user pictures.
    */
   function testDeletePicture() {
-- 
1.8.0

