From acfd5e83294bd2cb223edbb6f1a4852f232dedfa Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Wed, 20 Mar 2019 15:28:51 +0100 Subject: [PATCH] MDL-65019 core: Return repository type for main file --- course/externallib.php | 6 ++++++ course/tests/externallib_test.php | 32 ++++++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/course/externallib.php b/course/externallib.php index 3e8744cc99d..83a789bb83b 100644 --- a/course/externallib.php +++ b/course/externallib.php @@ -326,6 +326,10 @@ class core_course_external extends external_api { 'mimetypes' => array(), ); foreach ($contents as $content) { + // Check repository file (only main file). + if (!isset($module['contentsinfo']['repositorytype'])) { + $module['contentsinfo']['repositorytype'] = isset($content['repositorytype']) ? $content['repositorytype'] : ''; + } if (isset($content['filesize'])) { $module['contentsinfo']['filessize'] += $content['filesize']; } @@ -501,6 +505,8 @@ class core_course_external extends external_api { new external_value(PARAM_RAW, 'File mime type.'), 'Files mime types.' ), + 'repositorytype' => new external_value(PARAM_PLUGIN, 'The repository type for + the main file.', VALUE_OPTIONAL), ), 'Contents summary information.', VALUE_OPTIONAL ), ) diff --git a/course/tests/externallib_test.php b/course/tests/externallib_test.php index ad77f90a026..02818cd0372 100644 --- a/course/tests/externallib_test.php +++ b/course/tests/externallib_test.php @@ -1285,15 +1285,40 @@ class core_course_externallib_testcase extends externallib_advanced_testcase { $record->files = file_get_unused_draft_itemid(); $usercontext = context_user::instance($USER->id); $extensions = array('txt', 'png', 'pdf'); + $fs = get_file_storage(); foreach ($extensions as $key => $extension) { // Add actual file there. $filerecord = array('component' => 'user', 'filearea' => 'draft', 'contextid' => $usercontext->id, 'itemid' => $record->files, 'filename' => 'resource' . $key . '.' . $extension, 'filepath' => '/'); - $fs = get_file_storage(); $fs->create_file_from_string($filerecord, 'Test resource ' . $key . ' file'); } + // Create file reference. + $repos = repository::get_instances(array('type' => 'user')); + $userrepository = reset($repos); + + // Create a user private file. + $userfilerecord = new stdClass; + $userfilerecord->contextid = $usercontext->id; + $userfilerecord->component = 'user'; + $userfilerecord->filearea = 'private'; + $userfilerecord->itemid = 0; + $userfilerecord->filepath = '/'; + $userfilerecord->filename = 'userfile.txt'; + $userfilerecord->source = 'test'; + $userfile = $fs->create_file_from_string($userfilerecord, 'User file content'); + $userfileref = $fs->pack_reference($userfilerecord); + + // Clone latest "normal" file. + $filerefrecord = clone (object) $filerecord; + $filerefrecord->filename = 'testref.txt'; + $fileref = $fs->create_file_from_reference($filerefrecord, $userrepository->id, $userfileref); + // Set main file pointing to the file reference. + file_set_sortorder($usercontext->id, 'user', 'draft', $record->files, $filerefrecord->filepath, + $filerefrecord->filename, 1); + + // Once the reference has been created, create the file resource. $resource2 = self::getDataGenerator()->create_module('resource', $record); $result = core_course_external::get_course_contents($course->id); @@ -1306,10 +1331,11 @@ class core_course_externallib_testcase extends externallib_advanced_testcase { $this->assertEquals($module['contents'][0]['filesize'], $module['contentsinfo']['filessize']); $this->assertEquals(array('text/plain'), $module['contentsinfo']['mimetypes']); } else { - $this->assertEquals(count($extensions), $module['contentsinfo']['filescount']); + $this->assertEquals(count($extensions) + 1, $module['contentsinfo']['filescount']); $filessize = $module['contents'][0]['filesize'] + $module['contents'][1]['filesize'] + - $module['contents'][2]['filesize']; + $module['contents'][2]['filesize'] + $module['contents'][3]['filesize']; $this->assertEquals($filessize, $module['contentsinfo']['filessize']); + $this->assertEquals('user', $module['contentsinfo']['repositorytype']); $this->assertGreaterThanOrEqual($timenow, $module['contentsinfo']['lastmodified']); $this->assertEquals(array('text/plain', 'image/png', 'application/pdf'), $module['contentsinfo']['mimetypes']); }