Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 11
Línea 12... Línea 12...
12
// GNU General Public License for more details.
12
// GNU General Public License for more details.
13
//
13
//
14
// You should have received a copy of the GNU General Public License
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/>.
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
Línea 16... Línea -...
16
 
-
 
17
/**
-
 
18
 * Tests for mod/assign/submission/file/locallib.php
-
 
19
 *
-
 
20
 * @package   assignsubmission_file
-
 
21
 * @copyright 2016 Cameron Ball
-
 
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
23
 */
16
 
Línea 24... Línea 17...
24
namespace assignsubmission_file;
17
namespace assignsubmission_file;
Línea 25... Línea 18...
25
 
18
 
Línea 31... Línea 24...
31
require_once($CFG->dirroot . '/mod/assign/tests/generator.php');
24
require_once($CFG->dirroot . '/mod/assign/tests/generator.php');
Línea 32... Línea 25...
32
 
25
 
33
/**
26
/**
34
 * Unit tests for mod/assign/submission/file/locallib.php
27
 * Unit tests for mod/assign/submission/file/locallib.php
-
 
28
 *
35
 *
29
 * @package    assignsubmission_file
36
 * @copyright  2016 Cameron Ball
30
 * @copyright  2016 Cameron Ball
37
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
32
 */
Línea 39... Línea 33...
39
class locallib_test extends \advanced_testcase {
33
final class locallib_test extends \advanced_testcase {
40
 
34
 
Línea 41... Línea 35...
41
    // Use the generator helper.
35
    // Use the generator helper.
Línea 46... Línea 40...
46
     *
40
     *
47
     * @dataProvider submission_is_empty_testcases
41
     * @dataProvider submission_is_empty_testcases
48
     * @param string $data The file submission data
42
     * @param string $data The file submission data
49
     * @param bool $expected The expected return value
43
     * @param bool $expected The expected return value
50
     */
44
     */
51
    public function test_submission_is_empty($data, $expected) {
45
    public function test_submission_is_empty($data, $expected): void {
52
        $this->resetAfterTest();
46
        $this->resetAfterTest();
Línea 53... Línea 47...
53
 
47
 
54
        $course = $this->getDataGenerator()->create_course();
48
        $course = $this->getDataGenerator()->create_course();
55
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
49
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
Línea 76... Línea 70...
76
    }
70
    }
Línea 77... Línea 71...
77
 
71
 
78
    /**
72
    /**
79
     * Test that an empty directory is is not detected as a valid submission by submission_is_empty.
73
     * Test that an empty directory is is not detected as a valid submission by submission_is_empty.
80
     */
74
     */
81
    public function test_submission_is_empty_directory_only() {
75
    public function test_submission_is_empty_directory_only(): void {
82
        $this->resetAfterTest();
76
        $this->resetAfterTest();
83
        $course = $this->getDataGenerator()->create_course();
77
        $course = $this->getDataGenerator()->create_course();
84
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
78
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
85
        $assign = $this->create_instance($course, [
79
        $assign = $this->create_instance($course, [
Línea 108... Línea 102...
108
     *
102
     *
109
     * @dataProvider submission_is_empty_testcases
103
     * @dataProvider submission_is_empty_testcases
110
     * @param string $data The file submission data
104
     * @param string $data The file submission data
111
     * @param bool $expected The expected return value
105
     * @param bool $expected The expected return value
112
     */
106
     */
113
    public function test_new_submission_empty($data, $expected) {
107
    public function test_new_submission_empty($data, $expected): void {
114
        $this->resetAfterTest();
108
        $this->resetAfterTest();
Línea 115... Línea 109...
115
 
109
 
116
        $course = $this->getDataGenerator()->create_course();
110
        $course = $this->getDataGenerator()->create_course();
117
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
111
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
Línea 137... Línea 131...
137
    }
131
    }
Línea 138... Línea 132...
138
 
132
 
139
    /**
133
    /**
140
     * Test that an empty directory is is not detected as a valid submission by new_submission_is_empty.
134
     * Test that an empty directory is is not detected as a valid submission by new_submission_is_empty.
141
     */
135
     */
142
    public function test_new_submission_empty_directory_only() {
136
    public function test_new_submission_empty_directory_only(): void {
143
        $this->resetAfterTest();
137
        $this->resetAfterTest();
144
        $course = $this->getDataGenerator()->create_course();
138
        $course = $this->getDataGenerator()->create_course();
145
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
139
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
146
        $assign = $this->create_instance($course, [
140
        $assign = $this->create_instance($course, [
Línea 167... Línea 161...
167
    /**
161
    /**
168
     * Dataprovider for the test_submission_is_empty testcase
162
     * Dataprovider for the test_submission_is_empty testcase
169
     *
163
     *
170
     * @return array of testcases
164
     * @return array of testcases
171
     */
165
     */
172
    public function submission_is_empty_testcases() {
166
    public static function submission_is_empty_testcases(): array {
173
        return [
167
        return [
174
            'With file' => [
168
            'With file' => [
175
                [
169
                [
176
                    'component' => 'user',
170
                    'component' => 'user',
177
                    'filearea' => 'draft',
171
                    'filearea' => 'draft',
Línea 190... Línea 184...
190
                false
184
                false
191
            ],
185
            ],
192
            'Without file' => [null, true]
186
            'Without file' => [null, true]
193
        ];
187
        ];
194
    }
188
    }
-
 
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
    }
195
}
230
}