Proyectos de Subversion Moodle

Rev

| 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
declare(strict_types=1);
18
 
19
namespace core\content\export\exportable_items;
20
 
21
use advanced_testcase;
22
use context;
23
use context_system;
24
use core\content\export\zipwriter;
25
use stdClass;
26
use stored_file;
27
 
28
/**
29
 * Unit tests for the `exportable_stored_file` export item class.
30
 *
31
 * @package     core
32
 * @category    test
33
 * @copyright   2020 Andrew Nicols <andrew@nicols.co.uk>
34
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 * @covers      \core\content\exportable_items\exportable_stored_file
36
 */
37
class exportable_stored_file_test extends advanced_testcase {
38
 
39
    /**
40
     * Ensure that the create_from_area_params function returns an array.
41
     */
42
    public function test_create_from_area_params_no_files(): void {
43
        $exportables = exportable_stored_file::create_from_area_params(
44
            context_system::instance(),
45
            'fake',
46
            'filearea',
47
            null
48
        );
49
 
50
        $this->assertIsArray($exportables);
51
        $this->assertCount(0, $exportables);
52
    }
53
 
54
    /**
55
     * Ensure that the create_from_area_params function returns a set of exportable_stored_file items, for all itemids.
56
     */
57
    public function test_create_from_area_params_no_itemid(): void {
58
        $this->resetAfterTest(true);
59
 
60
        // Setup for test.
61
        $user = $this->getDataGenerator()->create_user();
62
        $context = context_system::instance();
63
        $component = 'fake';
64
        $filearea = 'myfirstfilearea';
65
 
66
        $files1 = $this->create_files(context_system::instance(), $component, $filearea, 1);
67
        $files2 = $this->create_files(context_system::instance(), $component, $filearea, 2);
68
        $files3 = $this->create_files(context_system::instance(), $component, $filearea, 3);
69
        $files = array_values(array_merge($files1, $files2, $files3));
70
 
71
        $exportables = exportable_stored_file::create_from_area_params($context, $component, $filearea, null);
72
 
73
        $this->assertIsArray($exportables);
74
        $this->assertCount(3, $exportables);
75
 
76
        // There should be three exportables. These are listed in order of itemid.
77
        for ($i = 0; $i < 3; $i++) {
78
            $exportable = $exportables[$i];
79
            $file = $files[$i];
80
 
81
            $this->assertInstanceOf(exportable_stored_file::class, $exportable);
82
            $this->assert_exportable_matches_file($component, $user, $context, $filearea, '', $file, $exportable);
83
        }
84
 
85
    }
86
 
87
    /**
88
     * Ensure that the create_from_area_params function returns a set of exportable_stored_file items, for the requested
89
     * itemid
90
     */
91
    public function test_create_from_area_params_specified_itemid(): void {
92
        $this->resetAfterTest(true);
93
 
94
        // Setup for test.
95
        $user = $this->getDataGenerator()->create_user();
96
        $context = context_system::instance();
97
        $component = 'fake';
98
        $filearea = 'myfirstfilearea';
99
 
100
        $files1 = $this->create_files(context_system::instance(), $component, $filearea, 1);
101
        $files2 = $this->create_files(context_system::instance(), $component, $filearea, 2);
102
        $files3 = $this->create_files(context_system::instance(), $component, $filearea, 3);
103
 
104
        $exportables = exportable_stored_file::create_from_area_params($context, $component, $filearea, 2);
105
 
106
        $this->assertIsArray($exportables);
107
        $this->assertCount(1, $exportables);
108
 
109
        // There is only one exportable.
110
        $exportable = array_shift($exportables);
111
        $this->assertInstanceOf(exportable_stored_file::class, $exportable);
112
 
113
        $file2 = reset($files2);
114
        $this->assert_exportable_matches_file($component, $user, $context, $filearea, '', $file2, $exportable);
115
    }
116
 
117
    /**
118
     * Ensure that the create_from_area_params function returns a set of exportable_stored_file items, for the requested
119
     * itemid
120
     */
121
    public function test_create_from_area_params_in_subdir(): void {
122
        $this->resetAfterTest(true);
123
 
124
        // Setup for test.
125
        $user = $this->getDataGenerator()->create_user();
126
        $context = context_system::instance();
127
        $component = 'fake';
128
        $filearea = 'myfirstfilearea';
129
        $subdir = 'a/path/to/my/subdir';
130
 
131
        $files1 = $this->create_files(context_system::instance(), $component, $filearea, 1);
132
        $files2 = $this->create_files(context_system::instance(), $component, $filearea, 2);
133
        $files3 = $this->create_files(context_system::instance(), $component, $filearea, 3);
134
 
135
        $exportables = exportable_stored_file::create_from_area_params($context, $component, $filearea, 2, 2, $subdir);
136
 
137
        $this->assertIsArray($exportables);
138
        $this->assertCount(1, $exportables);
139
 
140
        // There is only one exportable.
141
        $exportable = array_shift($exportables);
142
        $this->assertInstanceOf(exportable_stored_file::class, $exportable);
143
 
144
        $file2 = reset($files2);
145
        $this->assert_exportable_matches_file($component, $user, $context, $filearea, $subdir, $file2, $exportable);
146
    }
147
 
148
    /**
149
     * Create files for use in testing.
150
     *
151
     * @param   context $context
152
     * @param   string $component
153
     * @param   string $filearea
154
     * @param   int $itemid
155
     * @param   int $count
156
     * @return  stored_file[]
157
     */
158
    protected function create_files(context $context, string $component, string $filearea, int $itemid, int $count = 1): array {
159
        $fs = get_file_storage();
160
 
161
        $files = [];
162
        for ($i = 0; $i < $count; $i++) {
163
 
164
            $filepath = '/';
165
            for ($j = 0; $j < $i; $j++) {
166
                $filepath .= "{$j}/";
167
            }
168
 
169
            $files[] = $fs->create_file_from_string(
170
                (object) [
171
                    'contextid' => $context->id,
172
                    'component' => $component,
173
                    'filearea' => $filearea,
174
                    'filepath' => $filepath,
175
                    'filename' => "file.txt",
176
                    'itemid' => $itemid,
177
                ],
178
                "File content: {$i}"
179
            );
180
        }
181
 
182
        return $files;
183
    }
184
 
185
    /**
186
     * Assert that the supplied expotable matches the supplied file.
187
     *
188
     * @param   string $component
189
     * @param   stdClass $user
190
     * @param   context $context
191
     * @param   string $filearea
192
     * @param   string $subdir
193
     * @param   stored_file $file
194
     * @param   exportable_stored_file $exportable
195
     */
196
    protected function assert_exportable_matches_file(
197
        string $component,
198
        stdClass $user,
199
        context $context,
200
        string $filearea,
201
        string $subdir,
202
        stored_file $file,
203
        exportable_stored_file $exportable
204
    ): void {
205
        $archive = $this->getMockBuilder(zipwriter::class)
206
            ->setConstructorArgs([$this->getMockBuilder(\ZipStream\ZipStream::class)->getmock()])
207
            ->onlyMethods([
208
                'add_file_from_stored_file',
209
            ])
210
            ->getMock();
211
 
212
        $this->assertEquals($file->get_filepath() . $file->get_filename(), $exportable->get_user_visible_name());
213
 
214
        $expectedfilepath = implode('/', array_filter([$subdir, $filearea, $file->get_filepath(), $file->get_filename()]));
215
        $expectedfilepath = preg_replace('#/+#', '/', $expectedfilepath);
216
 
217
        $archive->expects($this->once())
218
            ->method('add_file_from_stored_file')
219
            ->with(
220
                $this->equalTo($context),
221
                $this->equalTo($expectedfilepath),
222
                $this->equalTo($file)
223
            );
224
 
225
        $exportable->add_to_archive($archive);
226
    }
227
}