Proyectos de Subversion Moodle

Rev

Rev 11 | | 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
/**
18
 * Unit test for recent repository
19
 *
20
 * @package repository_recent
21
 *
22
 * @author  Nathan Nguyen <nathannguyen@catalyst-au.net>
23
 * @copyright  Catalyst IT
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
26
namespace repository_recent;
27
 
28
use repository;
29
 
30
defined('MOODLE_INTERNAL') || die();
31
 
32
global $CFG;
33
require_once($CFG->dirroot . '/repository/lib.php');
34
require_once($CFG->dirroot . '/files/externallib.php');
35
/**
36
 * Unit test for recent repository
37
 *
38
 * @package repository_recent
39
 *
40
 * @author  Nathan Nguyen <nathannguyen@catalyst-au.net>
41
 * @copyright  Catalyst IT
42
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43
 */
1441 ariadna 44
final class lib_test extends \advanced_testcase {
1 efrain 45
 
46
    /** @var repository Recent repository */
47
    private $repo;
48
 
49
    /** @var \context repository */
50
    private $usercontext;
51
 
52
    /**
53
     * SetUp to create an repository instance.
54
     */
55
    protected function setUp(): void {
56
        global $USER;
1441 ariadna 57
        parent::setUp();
1 efrain 58
        $this->setAdminUser();
59
        $this->usercontext = \context_user::instance($USER->id);
60
        $repoid = $this->getDataGenerator()->create_repository('recent')->id;
61
        $this->repo = repository::get_repository_by_id($repoid, $this->usercontext);
62
    }
63
 
64
    /**
65
     * Test get listing
66
     */
11 efrain 67
    public function test_get_listing_with_duplicate_file(): void {
1 efrain 68
        global $itemid;
69
        $this->resetAfterTest(true);
70
 
71
        // Set global itemid for draft file (file manager mockup).
72
        $itemid = file_get_unused_draft_itemid();
73
 
74
        // No recent file.
75
        $filelist = $this->repo->get_listing()['list'];
76
        $this->assertCount(0, $filelist);
77
 
78
        // Create test file 1.
79
        $this->create_test_file('TestFile1', 'draft', $itemid);
80
        $filelist = $this->repo->get_listing()['list'];
81
        $this->assertCount(1, $filelist);
82
 
83
        // Due to create_test_file function, same filename means same content as the content is the filename hash.
84
        $this->create_test_file('TestFile1', 'private');
85
        $filelist = $this->repo->get_listing()['list'];
86
        $this->assertCount(1, $filelist);
87
 
88
        // Create test file 2, different area.
89
        $this->create_test_file('TestFile2', 'private');
90
        $filelist = $this->repo->get_listing()['list'];
91
        $this->assertCount(2, $filelist);
92
    }
93
 
94
    /**
95
     * Test get listing reference file
96
     */
11 efrain 97
    public function test_get_listing_with_reference_file(): void {
1 efrain 98
        $this->resetAfterTest(true);
99
        // Create test file 1.
100
        $file1 = $this->create_test_file('TestFile1', 'private');
101
        $filelist = $this->repo->get_listing()['list'];
102
        $this->assertCount(1, $filelist);
103
 
104
        // Create reference file.
105
        $file2 = $this->create_reference_file($file1, 'TestFile2', 'private');
106
        $filelist = $this->repo->get_listing()['list'];
107
        $this->assertCount(1, $filelist);
108
 
109
        // Delete reference.
110
        $file2->delete_reference();
111
        $filelist = $this->repo->get_listing()['list'];
112
        $this->assertCount(2, $filelist);
113
    }
114
 
115
    /**
116
     * Test number limit
117
     */
11 efrain 118
    public function test_get_listing_number_limit(): void {
1 efrain 119
        $this->resetAfterTest(true);
120
        $this->create_multiple_test_files('private', 75);
121
        $filelist = $this->repo->get_listing()['list'];
122
        $this->assertCount(50, $filelist);
123
 
124
        // The number limit is set as property of the repo, so we need to create new repo instance.
125
        set_config('recentfilesnumber', 100, 'recent');
126
        $repoid = $this->getDataGenerator()->create_repository('recent')->id;
127
        $repo = repository::get_repository_by_id($repoid, $this->usercontext);
128
        $filelist = $repo->get_listing()['list'];
129
        $this->assertCount(75, $filelist);
130
    }
131
 
132
    /**
133
     * Test time limit
134
     */
11 efrain 135
    public function test_get_listing_time_limit(): void {
1 efrain 136
        $this->resetAfterTest(true);
137
        $this->create_multiple_test_files('private', 25);
138
        $file1 = $this->create_test_file('TestFileTimeLimit', 'private');
139
        // Set time modified back to a year ago.
140
        $file1->set_timemodified(time() - YEARSECS);
141
 
142
        // There is no time limit by default.
143
        $filelist = $this->repo->get_listing()['list'];
144
        $this->assertCount(26, $filelist);
145
 
146
        // The time limit is set as property of the repo, so we need to create new repo instance.
147
        set_config('recentfilestimelimit', 3600, 'recent');
148
        $repoid = $this->getDataGenerator()->create_repository('recent')->id;
149
        $repo = repository::get_repository_by_id($repoid, $this->usercontext);
150
        $filelist = $repo->get_listing()['list'];
151
        // Only get the recent files in the last hour.
152
        $this->assertCount(25, $filelist);
153
    }
154
 
155
    /**
156
     * Create multiple test file
157
     *
158
     * @param string $filearea file area
159
     * @param int $numberoffiles number of files to be created
160
     */
161
    private function create_multiple_test_files($filearea, $numberoffiles) {
162
        for ($i = 0; $i < $numberoffiles; ++$i) {
163
            $filename = "TestFile$i" . time();
164
            $this->create_test_file($filename, $filearea);
165
        }
166
    }
167
 
168
    /**
169
     * Create test file
170
     *
171
     * @param string $filename file name
172
     * @param string $filearea file area
173
     * @param int $itemid item id
174
     * @return \stored_file the newly created file
175
     */
176
    private function create_test_file($filename, $filearea, $itemid = 0) {
177
        global $USER;
178
 
179
        $filerecord = array();
180
        $filerecord['contextid'] = $this->usercontext->id;
181
        $filerecord['component'] = 'user';
182
        $filerecord['filearea'] = $filearea;
183
        $filerecord['itemid'] = $itemid;
184
        $filerecord['filepath'] = '/';
185
        $filerecord['filename'] = $filename;
186
        $filerecord['userid'] = $USER->id;
187
 
188
        $fs = get_file_storage();
189
        $content = hash("md5", $filename);
190
        return $fs->create_file_from_string($filerecord, $content);
191
    }
192
 
193
    /**
194
     * Create reference file
195
     *
196
     * @param \stored_file $file source file
197
     * @param string $filename file name
198
     * @param string $filearea file area
199
     * @param int $itemid item id
200
     * @return \stored_file the newly created file
201
     */
202
    private function create_reference_file($file, $filename, $filearea, $itemid = 0) {
203
        global $USER, $DB;
204
 
205
        $newfilerecord = array();
206
        $newfilerecord['contextid'] = $this->usercontext->id;
207
        $newfilerecord['component'] = 'user';
208
        $newfilerecord['filearea'] = $filearea;
209
        $newfilerecord['itemid'] = $itemid;
210
        $newfilerecord['filepath'] = '/';
211
        $newfilerecord['filename'] = $filename;
212
        $newfilerecord['userid'] = $USER->id;
213
 
214
        $fs = get_file_storage();
215
        $oldfilerecord = $DB->get_record('files', ['id' => $file->get_id()]);
216
        $ref = $fs->pack_reference($oldfilerecord);
217
        return $fs->create_file_from_reference($newfilerecord, $this->repo->id, $ref);
218
    }
219
}