diff --git a/backup/moodle2/backup_course_task.class.php b/backup/moodle2/backup_course_task.class.php index 5d4cd9b4f45..eb1b3b84677 100644 --- a/backup/moodle2/backup_course_task.class.php +++ b/backup/moodle2/backup_course_task.class.php @@ -129,20 +129,38 @@ class backup_course_task extends backup_task { /** * Code the transformations to perform in the course in * order to get transportable (encoded) links + * @param string $content content in which to encode links. + * @return string content with links encoded. */ static public function encode_content_links($content) { - global $CFG; - - $base = preg_quote($CFG->wwwroot, '/'); // Link to the course main page (it also covers "&topic=xx" and "&week=xx" - // because they don't become transformed (section number) in backup/restore - $search = '/(' . $base . '\/course\/view.php\?id\=)([0-9]+)/'; - $content= preg_replace($search, '$@COURSEVIEWBYID*$2@$', $content); + // because they don't become transformed (section number) in backup/restore. + $content = self::encode_links_helper($content, 'COURSEVIEWBYID', '/course/view.php?id='); + + // A few other key course links. + $content = self::encode_links_helper($content, 'GRADEINDEXBYID', '/grade/index.php?id='); + $content = self::encode_links_helper($content, 'GRADEREPORTINDEXBYID', '/grade/report/index.php?id='); + $content = self::encode_links_helper($content, 'BADGESVIEWBYID', '/badges/view.php?type=2&id='); + $content = self::encode_links_helper($content, 'USERINDEXVIEWBYID', '/user/index.php?id='); return $content; } + /** + * Helper method, used by encode_content_links. + * @param string $content content in which to encode links. + * @param unknown_type $name the name of this type of encoded link. + * @param unknown_type $path the path that identifies this type of link, up + * to the ?paramname= bit. + * @return string content with one type of link encoded. + */ + static private function encode_links_helper($content, $name, $path) { + global $CFG; + $base = preg_quote($CFG->wwwroot . $path, '/'); + return preg_replace('/(' . $base . ')([0-9]+)/', '$@' . $name . '*$2@$', $content); + } + // Protected API starts here /** diff --git a/backup/moodle2/restore_course_task.class.php b/backup/moodle2/restore_course_task.class.php index 74bfef42a2e..e9058b8c61c 100644 --- a/backup/moodle2/restore_course_task.class.php +++ b/backup/moodle2/restore_course_task.class.php @@ -135,10 +135,17 @@ class restore_course_task extends restore_task { static public function define_decode_rules() { $rules = array(); - $rules[] = new restore_decode_rule('COURSEVIEWBYID', '/course/view.php?id=$1', 'course'); + // Link to the course main page (it also covers "&topic=xx" and "&week=xx" + // because they don't become transformed (section number) in backup/restore. + $rules[] = new restore_decode_rule('COURSEVIEWBYID', '/course/view.php?id=$1', 'course'); + + // A few other key course links. + $rules[] = new restore_decode_rule('GRADEINDEXBYID', '/grade/index.php?id=$1', 'course'); + $rules[] = new restore_decode_rule('GRADEREPORTINDEXBYID', '/grade/report/index.php?id=$1', 'course'); + $rules[] = new restore_decode_rule('BADGESVIEWBYID', '/badges/view.php?type=2&id=$1', 'course'); + $rules[] = new restore_decode_rule('USERINDEXVIEWBYID', '/user/index.php?id=$1', 'course'); return $rules; - } // Protected API starts here diff --git a/backup/util/helper/tests/backup_encode_content_test.php b/backup/util/helper/tests/backup_encode_content_test.php new file mode 100644 index 00000000000..26299d95be3 --- /dev/null +++ b/backup/util/helper/tests/backup_encode_content_test.php @@ -0,0 +1,56 @@ +. + +/** + * @package core_backup + * @category phpunit + * @copyright 2013 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + + +global $CFG; +require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php'); +require_once($CFG->dirroot . '/backup/moodle2/backup_course_task.class.php'); + + + +/** + * Tests for encoding content links in backup_course_task. + * + * The code that this tests is acutally in backup/moodle2/backup_course_task.class.php, + * but there is no place for unit tests near there, and perhaps one day it will + * be refactored so it becomes more generic. + */ +class backup_course_task_testcase extends basic_testcase { + + /** + * Test the encode_content_links method for course. + */ + public function test_course_encode_content_links() { + global $CFG; + $encoded = backup_course_task::encode_content_links( + $CFG->wwwroot . '/course/view.php?id=123, ' . + $CFG->wwwroot . '/grade/index.php?id=123, ' . + $CFG->wwwroot . '/grade/report/index.php?id=123, ' . + $CFG->wwwroot . '/badges/view.php?type=2&id=123 and ' . + $CFG->wwwroot . '/user/index.php?id=123.'); + $this->assertEquals('$@COURSEVIEWBYID*123@$, $@GRADEINDEXBYID*123@$, ' . + '$@GRADEREPORTINDEXBYID*123@$, $@BADGESVIEWBYID*123@$ and $@USERINDEXVIEWBYID*123@$.', $encoded); + } +}