Proyectos de Subversion Moodle

Rev

Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
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.
8
//
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.
13
//
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/>.
16
 
17
namespace core_course;
18
 
19
use context_user;
20
use context_course;
21
use ReflectionMethod;
22
use cache_definition;
23
use core_course\cache\course_image;
24
 
25
/**
26
 * Functional test for class course_image
27
 *
28
 * @package    core
29
 * @subpackage course
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
33
 */
34
class course_image_cache_test extends \advanced_testcase {
35
 
36
    /**
37
     * Initial setup.
38
     */
39
    protected function setUp(): void {
40
        global $CFG;
41
 
42
        parent::setUp();
43
        $this->resetAfterTest();
44
        $this->setAdminUser();
45
 
46
        // Allow multiple overview files.
47
        $CFG->courseoverviewfileslimit = 3;
48
    }
49
 
50
    /**
51
     * Helper method to create a draft area for current user and fills it with fake files
52
     *
53
     * @param array $files array of files that need to be added to filearea, filename => filecontents
54
     * @return int draftid for the filearea
55
     */
56
    protected function fill_draft_area(array $files): int {
57
        global $USER;
58
 
59
        $draftid = file_get_unused_draft_itemid();
60
        foreach ($files as $filename => $filecontents) {
61
            // Add actual file there.
62
            $filerecord = [
63
                'component' => 'user',
64
                'filearea' => 'draft',
65
                'contextid' => context_user::instance($USER->id)->id, 'itemid' => $draftid,
66
                'filename' => $filename, 'filepath' => '/'
67
            ];
68
            $fs = get_file_storage();
69
            $fs->create_file_from_string($filerecord, $filecontents);
70
        }
71
        return $draftid;
72
    }
73
 
74
    /**
75
     * A helper method to generate expected file URL.
76
     *
77
     * @param \stdClass $course Course object.
78
     * @param string $filename File name.
79
     * @return string
80
     */
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;
84
    }
85
 
86
    /**
87
     * Test exception if try to get an image for non existing course.
88
     */
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));
93
    }
94
 
95
    /**
96
     * Test get_image_url_from_overview_files when no summary files in the course.
97
     */
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());
101
 
102
        // Create course without files.
103
        $course = $this->getDataGenerator()->create_course();
104
        $this->assertNull($method->invokeArgs($cache, [$course]));
105
    }
106
 
107
    /**
108
     * Test get_image_url_from_overview_files when no summary images in the course.
109
     */
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());
113
 
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]));
118
    }
119
 
120
    /**
121
     * Test get_image_url_from_overview_files when no summary images in the course.
122
     */
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());
126
 
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]));
132
    }
133
 
134
    /**
135
     * Test get_image_url_from_overview_files when several summary images in the course.
136
     */
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());
140
 
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'),
145
        ]);
146
        $course1 = $this->getDataGenerator()->create_course(['overviewfiles_filemanager' => $draftid1]);
147
 
148
        $expected = $this->build_expected_course_image_url($course1, 'filename1.jpg');
149
        $this->assertEquals($expected, $method->invokeArgs($cache, [$course1]));
150
    }
151
 
152
    /**
153
     * Test get_image_url_from_overview_files when several summary files in the course.
154
     */
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());
158
 
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'),
164
        ]);
165
        $course1 = $this->getDataGenerator()->create_course(['overviewfiles_filemanager' => $draftid1]);
166
 
167
        $expected = $this->build_expected_course_image_url($course1, 'filename2.jpg');
168
        $this->assertEquals($expected, $method->invokeArgs($cache, [$course1]));
169
    }
170
 
171
}