Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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 assignsubmission_file;
18
 
19
use mod_assign_test_generator;
20
 
21
defined('MOODLE_INTERNAL') || die();
22
 
23
global $CFG;
24
require_once($CFG->dirroot . '/mod/assign/tests/generator.php');
25
 
26
/**
27
 * Unit tests for mod/assign/submission/file/locallib.php
28
 *
11 efrain 29
 * @package    assignsubmission_file
1 efrain 30
 * @copyright  2016 Cameron Ball
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
11 efrain 33
final class locallib_test extends \advanced_testcase {
1 efrain 34
 
35
    // Use the generator helper.
36
    use mod_assign_test_generator;
37
 
38
    /**
39
     * Test submission_is_empty
40
     *
41
     * @dataProvider submission_is_empty_testcases
42
     * @param string $data The file submission data
43
     * @param bool $expected The expected return value
44
     */
11 efrain 45
    public function test_submission_is_empty($data, $expected): void {
1 efrain 46
        $this->resetAfterTest();
47
 
48
        $course = $this->getDataGenerator()->create_course();
49
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
50
        $assign = $this->create_instance($course, [
51
                'assignsubmission_file_enabled' => 1,
52
                'assignsubmission_file_maxfiles' => 12,
53
                'assignsubmission_file_maxsizebytes' => 10,
54
            ]);
55
 
56
        $this->setUser($student->id);
57
 
58
        $itemid = file_get_unused_draft_itemid();
59
        $submission = (object)['files_filemanager' => $itemid];
60
        $plugin = $assign->get_submission_plugin_by_type('file');
61
 
62
        if ($data) {
63
            $data += ['contextid' => \context_user::instance($student->id)->id, 'itemid' => $itemid];
64
            $fs = get_file_storage();
65
            $fs->create_file_from_string((object)$data, 'Content of ' . $data['filename']);
66
        }
67
 
68
        $result = $plugin->submission_is_empty($submission);
69
        $this->assertTrue($result === $expected);
70
    }
71
 
72
    /**
73
     * Test that an empty directory is is not detected as a valid submission by submission_is_empty.
74
     */
11 efrain 75
    public function test_submission_is_empty_directory_only(): void {
1 efrain 76
        $this->resetAfterTest();
77
        $course = $this->getDataGenerator()->create_course();
78
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
79
        $assign = $this->create_instance($course, [
80
                'assignsubmission_file_enabled' => 1,
81
                'assignsubmission_file_maxfiles' => 12,
82
                'assignsubmission_file_maxsizebytes' => 10,
83
            ]);
84
        $this->setUser($student->id);
85
        $itemid = file_get_unused_draft_itemid();
86
        $submission = (object)['files_filemanager' => $itemid];
87
        $plugin = $assign->get_submission_plugin_by_type('file');
88
        $fs = get_file_storage();
89
        $fs->create_directory(
90
                \context_user::instance($student->id)->id,
91
                'user',
92
                'draft',
93
                $itemid,
94
                '/subdirectory/'
95
        );
96
 
97
        $this->assertTrue($plugin->submission_is_empty($submission));
98
    }
99
 
100
    /**
101
     * Test new_submission_empty
102
     *
103
     * @dataProvider submission_is_empty_testcases
104
     * @param string $data The file submission data
105
     * @param bool $expected The expected return value
106
     */
11 efrain 107
    public function test_new_submission_empty($data, $expected): void {
1 efrain 108
        $this->resetAfterTest();
109
 
110
        $course = $this->getDataGenerator()->create_course();
111
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
112
        $assign = $this->create_instance($course, [
113
                'assignsubmission_file_enabled' => 1,
114
                'assignsubmission_file_maxfiles' => 12,
115
                'assignsubmission_file_maxsizebytes' => 10,
116
            ]);
117
 
118
        $this->setUser($student);
119
 
120
        $itemid = file_get_unused_draft_itemid();
121
        $submission = (object) ['files_filemanager' => $itemid];
122
 
123
        if ($data) {
124
            $data += ['contextid' => \context_user::instance($student->id)->id, 'itemid' => $itemid];
125
            $fs = get_file_storage();
126
            $fs->create_file_from_string((object)$data, 'Content of ' . $data['filename']);
127
        }
128
 
129
        $result = $assign->new_submission_empty($submission);
130
        $this->assertTrue($result === $expected);
131
    }
132
 
133
    /**
134
     * Test that an empty directory is is not detected as a valid submission by new_submission_is_empty.
135
     */
11 efrain 136
    public function test_new_submission_empty_directory_only(): void {
1 efrain 137
        $this->resetAfterTest();
138
        $course = $this->getDataGenerator()->create_course();
139
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
140
        $assign = $this->create_instance($course, [
141
                'assignsubmission_file_enabled' => 1,
142
                'assignsubmission_file_maxfiles' => 12,
143
                'assignsubmission_file_maxsizebytes' => 10,
144
            ]);
145
        $this->setUser($student->id);
146
        $itemid = file_get_unused_draft_itemid();
147
        $submission = (object)['files_filemanager' => $itemid];
148
        $plugin = $assign->get_submission_plugin_by_type('file');
149
        $fs = get_file_storage();
150
        $fs->create_directory(
151
                \context_user::instance($student->id)->id,
152
                'user',
153
                'draft',
154
                $itemid,
155
                '/subdirectory/'
156
        );
157
 
158
        $this->assertTrue($assign->new_submission_empty($submission));
159
    }
160
 
161
    /**
162
     * Dataprovider for the test_submission_is_empty testcase
163
     *
164
     * @return array of testcases
165
     */
11 efrain 166
    public static function submission_is_empty_testcases(): array {
1 efrain 167
        return [
168
            'With file' => [
169
                [
170
                    'component' => 'user',
171
                    'filearea' => 'draft',
172
                    'filepath' => '/',
173
                    'filename' => 'not_a_virus.exe'
174
                ],
175
                false
176
            ],
177
            'With file in directory' => [
178
                [
179
                    'component' => 'user',
180
                    'filearea' => 'draft',
181
                    'filepath' => '/subdir/',
182
                    'filename' => 'not_a_virus.exe'
183
                ],
184
                false
185
            ],
186
            'Without file' => [null, true]
187
        ];
188
    }
11 efrain 189
 
190
    /**
191
     * Test getting files from plugin submission
192
     */
193
    public function test_get_files(): void {
194
        $this->resetAfterTest();
195
 
196
        $course = $this->getDataGenerator()->create_course();
197
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
198
        $assign = $this->create_instance($course, [
199
            'assignsubmission_file_enabled' => 1,
200
            'assignsubmission_file_maxfiles' => 2,
201
            'assignsubmission_file_maxsizebytes' => 512,
202
        ]);
203
 
204
        // Switch to student, create some dummy files, and submit data to plugin.
205
        $this->setUser($student);
206
        $submission = $assign->get_user_submission($student->id, true);
207
 
208
        $filerecord = [
209
            'contextid' => $assign->get_context()->id,
210
            'component' => 'assignsubmission_file',
211
            'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
212
            'itemid' => $submission->id,
213
            'filepath' => '/',
214
        ];
215
 
216
        get_file_storage()->create_file_from_string($filerecord + ['filename' => 'File 1.txt'], 'File One');
217
        get_file_storage()->create_file_from_string($filerecord + ['filename' => 'File 2.txt'], 'File Two');
218
 
219
        /** @var \assign_submission_file $plugin */
220
        $plugin = $assign->get_submission_plugin_by_type('file');
221
        $plugin->save($submission, (object) []);
222
 
223
        // Ensure we retrieve back list of file submissions, deterministically ordered.
224
        $files = $plugin->get_files($submission, $student);
225
        $this->assertSame([
226
            '/File 1.txt' => 'File 1.txt',
227
            '/File 2.txt' => 'File 2.txt',
228
        ], array_map(fn(\stored_file $f) => $f->get_filename(), $files));
229
    }
1 efrain 230
}