MDL-39087 Delete all component files in uninstall_plugin()
authorDavid Mudrák <david@moodle.com>
Wed, 10 Apr 2013 11:20:02 +0000 (13:20 +0200)
committerDavid Mudrák <david@moodle.com>
Thu, 11 Apr 2013 23:42:58 +0000 (01:42 +0200)
lib/adminlib.php
lib/filestorage/file_storage.php
lib/filestorage/tests/file_storage_test.php

index 89fa8e8..cf5af72 100644 (file)
@@ -321,6 +321,10 @@ function uninstall_plugin($type, $name) {
     // remove event handlers and dequeue pending events
     events_uninstall($component);
 
+    // Delete all remaining files in the filepool owned by the component.
+    $fs = get_file_storage();
+    $fs->delete_component_files($component);
+
     // Finally purge all caches.
     purge_all_caches();
 
index de8be24..f22635f 100644 (file)
@@ -743,6 +743,21 @@ class file_storage {
         $filerecords->close();
     }
 
+    /**
+     * Delete all files associated with the given component.
+     *
+     * @param string $component the component owning the file
+     */
+    public function delete_component_files($component) {
+        global $DB;
+
+        $filerecords = $DB->get_recordset('files', array('component' => $component));
+        foreach ($filerecords as $filerecord) {
+            $this->get_file_instance($filerecord)->delete();
+        }
+        $filerecords->close();
+    }
+
     /**
      * Move all the files in a file area from one context to another.
      *
index 38e0eb1..b895e57 100644 (file)
@@ -688,6 +688,17 @@ class filestoragelib_testcase extends advanced_testcase {
         $this->assertEquals(0, count($areafiles));
     }
 
+    public function test_delete_component_files() {
+        $user = $this->setup_three_private_files();
+        $fs = get_file_storage();
+
+        $areafiles = $fs->get_area_files($user->ctxid, 'user', 'private');
+        $this->assertEquals(4, count($areafiles));
+        $fs->delete_component_files('user');
+        $areafiles = $fs->get_area_files($user->ctxid, 'user', 'private');
+        $this->assertEquals(0, count($areafiles));
+    }
+
     public function test_create_file_from_url() {
         $this->resetAfterTest(true);