Skip to content

Commit 2eff7b5

Browse files
authored
fix: use correct context id for package retrieval
1 parent bfbab3a commit 2eff7b5

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

classes/static_file_service.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,17 @@ public function __construct(api $api, package_file_service $packagefileservice)
5858
* @param string $namespace
5959
* @param string $shortname
6060
* @param string $path
61+
* @param int $contextid
6162
* @return array{ 0: string, 1: string }|null array of temporary file path and mime type or null of the file wasn't
6263
* found
6364
* @throws coding_exception
6465
* @throws dml_exception
6566
* @throws invalid_dataroot_permissions
6667
*/
67-
public function download_public_static_file(string $packagehash, string $namespace, string $shortname, string $path): ?array {
68+
public function download_public_static_file(string $packagehash, string $namespace, string $shortname, string $path,
69+
int $contextid): ?array {
6870
$path = ltrim($path, '/');
69-
$packagefileiflocal = $this->packagefileservice->get_file_by_package_hash($packagehash, context_system::instance()->id);
71+
$packagefileiflocal = $this->packagefileservice->get_file_by_package_hash($packagehash, $contextid);
7072

7173
$temppath = make_request_directory() . "/$packagehash/$namespace/$shortname/$path";
7274
make_writable_directory(dirname($temppath));

lib.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@ function qtype_questionpy_pluginfile($course, $cm, $context, $filearea, $args, $
5252
[$packagehash, $namespace, $shortname] = $args;
5353
$path = implode('/', array_slice($args, 3));
5454

55-
[$filepath, $mimetype] = $staticfileservice->download_public_static_file($packagehash, $namespace, $shortname, $path);
55+
[$filepath, $mimetype] = $staticfileservice->download_public_static_file(
56+
$packagehash,
57+
$namespace,
58+
$shortname,
59+
$path,
60+
$context->id,
61+
);
5662
if (is_null($filepath)) {
5763
send_file_not_found();
5864
}

tests/static_file_service_test.php

+10-3
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ protected function setUp(): void {
9393
* @throws invalid_dataroot_permissions
9494
*/
9595
public function test_should_download_public_static_file(): void {
96+
global $PAGE;
97+
9698
$hash = random_string(64);
9799
$this->mockhandler->append(new Response(200, [
98100
'Content-Type' => 'text/markdown',
@@ -102,7 +104,8 @@ public function test_should_download_public_static_file(): void {
102104
$hash,
103105
'local',
104106
'example',
105-
'/path/to/file.txt'
107+
'/path/to/file.txt',
108+
$PAGE->context->id,
106109
);
107110

108111
$this->assertStringEqualsFile($path, 'Static file content');
@@ -125,13 +128,15 @@ public function test_should_download_public_static_file(): void {
125128
* @throws invalid_dataroot_permissions
126129
*/
127130
public function test_should_fall_back_and_warn_when_no_content_type(): void {
131+
global $PAGE;
128132
$this->mockhandler->append(new Response(200, [], 'Static file content'));
129133

130134
[, $mimetype] = $this->staticfileservice->download_public_static_file(
131135
random_string(64),
132136
'local',
133137
'example',
134-
'/path/to/file.txt'
138+
'/path/to/file.txt',
139+
$PAGE->context->id,
135140
);
136141

137142
$this->assertEquals('application/octet-stream', $mimetype);
@@ -146,13 +151,15 @@ public function test_should_fall_back_and_warn_when_no_content_type(): void {
146151
* @throws moodle_exception
147152
*/
148153
public function test_should_return_null_when_file_doesnt_exist(): void {
154+
global $PAGE;
149155
$this->mockhandler->append(new Response(404, []));
150156

151157
$result = $this->staticfileservice->download_public_static_file(
152158
random_string(64),
153159
'local',
154160
'example',
155-
'/path/to/file.txt'
161+
'/path/to/file.txt',
162+
$PAGE->context->id,
156163
);
157164

158165
$this->assertNull($result);

0 commit comments

Comments
 (0)