2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 namespace core_course
;
23 use core_course\cache\course_image
;
26 * Functional test for class course_image
30 * @author Dmitrii Metelkin <dmitriim@catalyst-au.net>
31 * @copyright 2021 Catalyst IT
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class course_image_cache_test
extends \advanced_testcase
{
39 protected function setUp(): void
{
43 $this->resetAfterTest();
44 $this->setAdminUser();
46 // Allow multiple overview files.
47 $CFG->courseoverviewfileslimit
= 3;
51 * Helper method to create a draft area for current user and fills it with fake files
53 * @param array $files array of files that need to be added to filearea, filename => filecontents
54 * @return int draftid for the filearea
56 protected function fill_draft_area(array $files): int {
59 $draftid = file_get_unused_draft_itemid();
60 foreach ($files as $filename => $filecontents) {
61 // Add actual file there.
63 'component' => 'user',
64 'filearea' => 'draft',
65 'contextid' => context_user
::instance($USER->id
)->id
, 'itemid' => $draftid,
66 'filename' => $filename, 'filepath' => '/'
68 $fs = get_file_storage();
69 $fs->create_file_from_string($filerecord, $filecontents);
75 * A helper method to generate expected file URL.
77 * @param \stdClass $course Course object.
78 * @param string $filename File name.
81 protected function build_expected_course_image_url(\stdClass
$course, string $filename): string {
82 $contextid = context_course
::instance($course->id
)->id
;
83 return 'https://www.example.com/moodle/pluginfile.php/' . $contextid. '/course/overviewfiles/' . $filename;
87 * Test exception if try to get an image for non existing course.
89 public function test_getting_data_if_course_is_not_exist() {
90 $this->expectException('dml_missing_record_exception');
91 $this->expectExceptionMessageMatches("/Can't find data record in database table course./");
92 $this->assertFalse(\cache
::make('core', 'course_image')->get(999));
96 * Test get_image_url_from_overview_files when no summary files in the course.
98 public function test_get_image_url_from_overview_files_return_null_if_no_summary_files_in_the_course() {
99 $method = new ReflectionMethod(course_image
::class, 'get_image_url_from_overview_files');
100 $cache = course_image
::get_instance_for_cache(new cache_definition());
102 // Create course without files.
103 $course = $this->getDataGenerator()->create_course();
104 $this->assertNull($method->invokeArgs($cache, [$course]));
108 * Test get_image_url_from_overview_files when no summary images in the course.
110 public function test_get_image_url_from_overview_files_returns_null_if_no_summary_images_in_the_course() {
111 $method = new ReflectionMethod(course_image
::class, 'get_image_url_from_overview_files');
112 $cache = course_image
::get_instance_for_cache(new cache_definition());
114 // Create course without image files.
115 $draftid2 = $this->fill_draft_area(['filename2.zip' => 'Test file contents2']);
116 $course2 = $this->getDataGenerator()->create_course(['overviewfiles_filemanager' => $draftid2]);
117 $this->assertNull($method->invokeArgs($cache, [$course2]));
121 * Test get_image_url_from_overview_files when no summary images in the course.
123 public function test_get_image_url_from_overview_files_returns_url_if_there_is_a_summary_image() {
124 $method = new ReflectionMethod(course_image
::class, 'get_image_url_from_overview_files');
125 $cache = course_image
::get_instance_for_cache(new cache_definition());
127 // Create course without one image.
128 $draftid1 = $this->fill_draft_area(['filename1.jpg' => file_get_contents(__DIR__
. '/fixtures/image.jpg')]);
129 $course1 = $this->getDataGenerator()->create_course(['overviewfiles_filemanager' => $draftid1]);
130 $expected = $this->build_expected_course_image_url($course1, 'filename1.jpg');
131 $this->assertEquals($expected, $method->invokeArgs($cache, [$course1]));
135 * Test get_image_url_from_overview_files when several summary images in the course.
137 public function test_get_image_url_from_overview_files_returns_url_of_the_first_image_if_there_are_many_summary_images() {
138 $method = new ReflectionMethod(course_image
::class, 'get_image_url_from_overview_files');
139 $cache = course_image
::get_instance_for_cache(new cache_definition());
141 // Create course with two image files.
142 $draftid1 = $this->fill_draft_area([
143 'filename1.jpg' => file_get_contents(__DIR__
. '/fixtures/image.jpg'),
144 'filename2.jpg' => file_get_contents(__DIR__
. '/fixtures/image.jpg'),
146 $course1 = $this->getDataGenerator()->create_course(['overviewfiles_filemanager' => $draftid1]);
148 $expected = $this->build_expected_course_image_url($course1, 'filename1.jpg');
149 $this->assertEquals($expected, $method->invokeArgs($cache, [$course1]));
153 * Test get_image_url_from_overview_files when several summary files in the course.
155 public function test_get_image_url_from_overview_files_returns_url_of_the_first_image_if_there_are_many_summary_files() {
156 $method = new ReflectionMethod(course_image
::class, 'get_image_url_from_overview_files');
157 $cache = course_image
::get_instance_for_cache(new cache_definition());
159 // Create course with two image files and one zip file.
160 $draftid1 = $this->fill_draft_area([
161 'filename1.zip' => 'Test file contents2',
162 'filename2.jpg' => file_get_contents(__DIR__
. '/fixtures/image.jpg'),
163 'filename3.jpg' => file_get_contents(__DIR__
. '/fixtures/image.jpg'),
165 $course1 = $this->getDataGenerator()->create_course(['overviewfiles_filemanager' => $draftid1]);
167 $expected = $this->build_expected_course_image_url($course1, 'filename2.jpg');
168 $this->assertEquals($expected, $method->invokeArgs($cache, [$course1]));