]> BookStack Code Mirror - bookstack/commitdiff
Updated user profile image delete to delete all uploads
authorDan Brown <redacted>
Sun, 28 Jan 2018 14:08:14 +0000 (14:08 +0000)
committerDan Brown <redacted>
Sun, 28 Jan 2018 14:08:14 +0000 (14:08 +0000)
Also moved test and made more comprehensive

app/Repos/UserRepo.php
app/Services/ImageService.php
tests/ImageTest.php
tests/UserProfileTest.php

index a159606da74ca48ba41c0559839fcf1dd50f05f7..e21568d63efb4823c528289f4fac3a87a01ef2f9 100644 (file)
@@ -1,9 +1,11 @@
 <?php namespace BookStack\Repos;
 
 <?php namespace BookStack\Repos;
 
+use Activity;
+use BookStack\Image;
 use BookStack\Role;
 use BookStack\User;
 use Exception;
 use BookStack\Role;
 use BookStack\User;
 use Exception;
-use BookStack\Services\ImageService;
+use Images;
 
 class UserRepo
 {
 
 class UserRepo
 {
@@ -11,7 +13,6 @@ class UserRepo
     protected $user;
     protected $role;
     protected $entityRepo;
     protected $user;
     protected $role;
     protected $entityRepo;
-    protected $imageService;
 
     /**
      * UserRepo constructor.
 
     /**
      * UserRepo constructor.
@@ -19,12 +20,11 @@ class UserRepo
      * @param Role $role
      * @param EntityRepo $entityRepo
      */
      * @param Role $role
      * @param EntityRepo $entityRepo
      */
-    public function __construct(User $user, Role $role, EntityRepo $entityRepo, ImageService $imageService)
+    public function __construct(User $user, Role $role, EntityRepo $entityRepo)
     {
         $this->user = $user;
         $this->role = $role;
         $this->entityRepo = $entityRepo;
     {
         $this->user = $user;
         $this->role = $role;
         $this->entityRepo = $entityRepo;
-        $this->imageService = $imageService;
     }
 
     /**
     }
 
     /**
@@ -88,7 +88,7 @@ class UserRepo
         // Get avatar from gravatar and save
         if (!config('services.disable_services')) {
             try {
         // Get avatar from gravatar and save
         if (!config('services.disable_services')) {
             try {
-                $avatar = \Images::saveUserGravatar($user);
+                $avatar = Images::saveUserGravatar($user);
                 $user->avatar()->associate($avatar);
                 $user->save();
             } catch (Exception $e) {
                 $user->avatar()->associate($avatar);
                 $user->save();
             } catch (Exception $e) {
@@ -143,16 +143,17 @@ class UserRepo
     /**
      * Remove the given user from storage, Delete all related content.
      * @param User $user
     /**
      * Remove the given user from storage, Delete all related content.
      * @param User $user
+     * @throws Exception
      */
     public function destroy(User $user)
     {
         $user->socialAccounts()->delete();
         $user->delete();
         
      */
     public function destroy(User $user)
     {
         $user->socialAccounts()->delete();
         $user->delete();
         
-        // Deleting User profile pics
-        $profilePic = $user->image_id ? $user->avatar->findOrFail($user->image_id) : FALSE;
-        if ($profilePic) {
-            $this->imageService->destroyImage($profilePic);
+        // Delete user profile images
+        $profileImages = $images = Image::where('type', '=', 'user')->where('created_by', '=', $user->id)->get();
+        foreach ($profileImages as $image) {
+            Images::destroyImage($image);
         }
     }
 
         }
     }
 
@@ -165,7 +166,7 @@ class UserRepo
      */
     public function getActivity(User $user, $count = 20, $page = 0)
     {
      */
     public function getActivity(User $user, $count = 20, $page = 0)
     {
-        return \Activity::userActivity($user, $count, $page);
+        return Activity::userActivity($user, $count, $page);
     }
 
     /**
     }
 
     /**
index 5eea285e5fda62ab74e6afb683277cf4a5d798e6..f87e980dfec03419bee55d82dbdb238efb6bf467 100644 (file)
@@ -236,6 +236,7 @@ class ImageService extends UploadService
      * Destroys an Image object along with its files and thumbnails.
      * @param Image $image
      * @return bool
      * Destroys an Image object along with its files and thumbnails.
      * @param Image $image
      * @return bool
+     * @throws Exception
      */
     public function destroyImage(Image $image)
     {
      */
     public function destroyImage(Image $image)
     {
index c75617c0e1578e0e5f8cbd6e0837cd10251b45dd..881f73b552a93e2b4298774533bbc3672d44914f 100644 (file)
@@ -184,4 +184,28 @@ class ImageTest extends TestCase
         $this->assertTrue($testImageData === $uploadedImageData, "Uploaded image file data does not match our test image as expected");
     }
 
         $this->assertTrue($testImageData === $uploadedImageData, "Uploaded image file data does not match our test image as expected");
     }
 
+    public function test_user_images_deleted_on_user_deletion()
+    {
+        $editor = $this->getEditor();
+        $this->actingAs($editor);
+
+        $imageName = 'profile.png';
+        $relPath = $this->getTestImagePath('gallery', $imageName);
+        $this->deleteImage($relPath);
+
+        $file = $this->getTestImage($imageName);
+        $this->call('POST', '/images/user/upload', [], [], ['file' => $file], []);
+        $this->call('POST', '/images/user/upload', [], [], ['file' => $file], []);
+
+        $profileImages = Image::where('type', '=', 'user')->where('created_by', '=', $editor->id)->get();
+        $this->assertTrue($profileImages->count() === 2, "Found profile images does not match upload count");
+
+        $userDelete = $this->asAdmin()->delete("/settings/users/{$editor->id}");
+        $userDelete->assertStatus(302);
+        $this->assertDatabaseMissing('images', [
+            'type' => 'user',
+            'created_by' => $editor->id
+        ]);
+    }
+
 }
\ No newline at end of file
 }
\ No newline at end of file
index e6b0b4f5f4250ae4ba7ad0ae637753aced50ab95..c1e254090f27ac1d336001fd7e5630a6813bcc27 100644 (file)
@@ -115,19 +115,5 @@ class UserProfileTest extends BrowserKitTest
             ->visit('/books')
             ->pageHasElement('.featured-image-container');
     }
             ->visit('/books')
             ->pageHasElement('.featured-image-container');
     }
-    
-    public function test_user_delete() 
-    {
-        $newUser = $this->getNewBlankUser();
-        $this->actingAs($newUser);
-        $this->asAdmin()->visit('/settings/users/' . $newUser->id . '/delete')
-               ->see('Delete User')
-               ->press('Confirm')
-               ->seePageIs('/settings/users/')
-               ->see('USERS')->see('ADD NEW USER');
-        
-        $this->dontSeeInDatabase('images', [
-               'id' => $newUser->image_id
-            ]);
-    }
+
 }
 }