MDL-41578 Fixed bug in coursecat::has_course_overviewfiles
authorMarina Glancy <marina@moodle.com>
Fri, 6 Sep 2013 00:50:59 +0000 (10:50 +1000)
committerMarina Glancy <marina@moodle.com>
Fri, 6 Sep 2013 00:50:59 +0000 (10:50 +1000)
lib/coursecatlib.php
lib/tests/coursecatlib_test.php

index 9cd0f6c..c433408 100644 (file)
@@ -2203,12 +2203,11 @@ class course_in_list implements IteratorAggregate {
     public function has_course_overviewfiles() {
         global $CFG;
         if (empty($CFG->courseoverviewfileslimit)) {
-            return 0;
+            return false;
         }
-        require_once($CFG->libdir. '/filestorage/file_storage.php');
         $fs = get_file_storage();
         $context = context_course::instance($this->id);
-        return $fs->is_area_empty($context->id, 'course', 'overviewfiles');
+        return !$fs->is_area_empty($context->id, 'course', 'overviewfiles');
     }
 
     /**
index be7c288..a71c799 100644 (file)
@@ -538,4 +538,116 @@ class core_coursecatlib_testcase extends advanced_testcase {
 
         $CFG->coursecontact = $oldcoursecontact;
     }
+
+    public function test_overview_files() {
+        global $CFG;
+        $this->setAdminUser();
+        $cat1 = coursecat::create(array('name' => 'Cat1'));
+
+        // Create course c1 with one image file.
+        $dratid1 = $this->fill_draft_area(array('filename.jpg' => 'Test file contents1'));
+        $c1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id,
+            'fullname' => 'Test 1', 'overviewfiles_filemanager' => $dratid1));
+        // Create course c2 with two image files (only one file will be added because of settings).
+        $dratid2 = $this->fill_draft_area(array('filename21.jpg' => 'Test file contents21', 'filename22.jpg' => 'Test file contents22'));
+        $c2 = $this->getDataGenerator()->create_course(array('category' => $cat1->id,
+            'fullname' => 'Test 2', 'overviewfiles_filemanager' => $dratid2));
+        // Create course c3 without files.
+        $c3 = $this->getDataGenerator()->create_course(array('category' => $cat1->id, 'fullname' => 'Test 3'));
+
+        // Change the settings to allow multiple files of any types.
+        $CFG->courseoverviewfileslimit = 3;
+        $CFG->courseoverviewfilesext = '*';
+        // Create course c5 with two image files.
+        $dratid4 = $this->fill_draft_area(array('filename41.jpg' => 'Test file contents41', 'filename42.jpg' => 'Test file contents42'));
+        $c4 = $this->getDataGenerator()->create_course(array('category' => $cat1->id,
+            'fullname' => 'Test 4', 'overviewfiles_filemanager' => $dratid4));
+        // Create course c6 with non-image file.
+        $dratid5 = $this->fill_draft_area(array('filename51.zip' => 'Test file contents51'));
+        $c5 = $this->getDataGenerator()->create_course(array('category' => $cat1->id,
+            'fullname' => 'Test 5', 'overviewfiles_filemanager' => $dratid5));
+
+        // Reset default settings.
+        $CFG->courseoverviewfileslimit = 1;
+        $CFG->courseoverviewfilesext = '.jpg,.gif,.png';
+
+        $courses = $cat1->get_courses();
+        $this->assertTrue($courses[$c1->id]->has_course_overviewfiles());
+        $this->assertTrue($courses[$c2->id]->has_course_overviewfiles());
+        $this->assertFalse($courses[$c3->id]->has_course_overviewfiles());
+        $this->assertTrue($courses[$c4->id]->has_course_overviewfiles());
+        $this->assertTrue($courses[$c5->id]->has_course_overviewfiles()); // Does not validate the filetypes.
+
+        $this->assertEquals(1, count($courses[$c1->id]->get_course_overviewfiles()));
+        $this->assertEquals(1, count($courses[$c2->id]->get_course_overviewfiles()));
+        $this->assertEquals(0, count($courses[$c3->id]->get_course_overviewfiles()));
+        $this->assertEquals(1, count($courses[$c4->id]->get_course_overviewfiles()));
+        $this->assertEquals(0, count($courses[$c5->id]->get_course_overviewfiles())); // Validate the filetypes.
+
+        // Overview files are not allowed, all functions return empty values.
+        $CFG->courseoverviewfileslimit = 0;
+
+        $this->assertFalse($courses[$c1->id]->has_course_overviewfiles());
+        $this->assertFalse($courses[$c2->id]->has_course_overviewfiles());
+        $this->assertFalse($courses[$c3->id]->has_course_overviewfiles());
+        $this->assertFalse($courses[$c4->id]->has_course_overviewfiles());
+        $this->assertFalse($courses[$c5->id]->has_course_overviewfiles());
+
+        $this->assertEquals(0, count($courses[$c1->id]->get_course_overviewfiles()));
+        $this->assertEquals(0, count($courses[$c2->id]->get_course_overviewfiles()));
+        $this->assertEquals(0, count($courses[$c3->id]->get_course_overviewfiles()));
+        $this->assertEquals(0, count($courses[$c4->id]->get_course_overviewfiles()));
+        $this->assertEquals(0, count($courses[$c5->id]->get_course_overviewfiles()));
+
+        // Multiple overview files are allowed but still limited to images.
+        $CFG->courseoverviewfileslimit = 3;
+
+        $this->assertTrue($courses[$c1->id]->has_course_overviewfiles());
+        $this->assertTrue($courses[$c2->id]->has_course_overviewfiles());
+        $this->assertFalse($courses[$c3->id]->has_course_overviewfiles());
+        $this->assertTrue($courses[$c4->id]->has_course_overviewfiles());
+        $this->assertTrue($courses[$c5->id]->has_course_overviewfiles()); // Still does not validate the filetypes.
+
+        $this->assertEquals(1, count($courses[$c1->id]->get_course_overviewfiles()));
+        $this->assertEquals(1, count($courses[$c2->id]->get_course_overviewfiles())); // Only 1 file was actually added.
+        $this->assertEquals(0, count($courses[$c3->id]->get_course_overviewfiles()));
+        $this->assertEquals(2, count($courses[$c4->id]->get_course_overviewfiles()));
+        $this->assertEquals(0, count($courses[$c5->id]->get_course_overviewfiles()));
+
+        // Multiple overview files of any type are allowed.
+        $CFG->courseoverviewfilesext = '*';
+
+        $this->assertTrue($courses[$c1->id]->has_course_overviewfiles());
+        $this->assertTrue($courses[$c2->id]->has_course_overviewfiles());
+        $this->assertFalse($courses[$c3->id]->has_course_overviewfiles());
+        $this->assertTrue($courses[$c4->id]->has_course_overviewfiles());
+        $this->assertTrue($courses[$c5->id]->has_course_overviewfiles());
+
+        $this->assertEquals(1, count($courses[$c1->id]->get_course_overviewfiles()));
+        $this->assertEquals(1, count($courses[$c2->id]->get_course_overviewfiles()));
+        $this->assertEquals(0, count($courses[$c3->id]->get_course_overviewfiles()));
+        $this->assertEquals(2, count($courses[$c4->id]->get_course_overviewfiles()));
+        $this->assertEquals(1, count($courses[$c5->id]->get_course_overviewfiles()));
+    }
+
+    /**
+     * Creates a draft area for current user and fills it with fake files
+     *
+     * @param array $files array of files that need to be added to filearea, filename => filecontents
+     * @return int draftid for the filearea
+     */
+    protected function fill_draft_area(array $files) {
+        global $USER;
+        $usercontext = context_user::instance($USER->id);
+        $draftid = file_get_unused_draft_itemid();
+        foreach ($files as $filename => $filecontents) {
+            // Add actual file there.
+            $filerecord = array('component' => 'user', 'filearea' => 'draft',
+                    'contextid' => $usercontext->id, 'itemid' => $draftid,
+                    'filename' => $filename, 'filepath' => '/');
+            $fs = get_file_storage();
+            $fs->create_file_from_string($filerecord, $filecontents);
+        }
+        return $draftid;
+    }
 }