diff --git a/backup/moodle2/tests/backup_xml_transformer_test.php b/backup/moodle2/tests/backup_xml_transformer_test.php
new file mode 100644
index 00000000000..55af1763597
--- /dev/null
+++ b/backup/moodle2/tests/backup_xml_transformer_test.php
@@ -0,0 +1,99 @@
+.
+
+/**
+ * Tests for backup_xml_transformer class.
+ *
+ * @package core_backup
+ * @subpackage moodle2
+ * @category backup
+ * @copyright 2017 Dmitrii Metelkin (dmitriim@catalyst-au.net)
+ * @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_plan_builder.class.php');
+
+/**
+ * Tests for backup_xml_transformer.
+ *
+ * @package core_backup
+ * @copyright 2017 Dmitrii Metelkin (dmitriim@catalyst-au.net)
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class backup_xml_transformer_testcase extends advanced_testcase {
+
+ /**
+ * Initial set up.
+ */
+ public function setUp() {
+ parent::setUp();
+
+ $this->resetAfterTest(true);
+ }
+
+ /**
+ * Data provider for ::test_filephp_links_replace.
+ *
+ * @return array
+ */
+ public function filephp_links_replace_data_provider() {
+ return array(
+ array('http://test.test/', 'http://test.test/'),
+ array('http://test.test/file.php/1', 'http://test.test/file.php/1'),
+ array('http://test.test/file.php/2/1.jpg', 'http://test.test/file.php/2/1.jpg'),
+ array('http://test.test/file.php/2', 'http://test.test/file.php/2'),
+ array('http://test.test/file.php/1/1.jpg', '$@FILEPHP@$$@SLASH@$1.jpg'),
+ array('http://test.test/file.php/1//1.jpg', '$@FILEPHP@$$@SLASH@$$@SLASH@$1.jpg'),
+ array('http://test.test/file.php?file=/1', '$@FILEPHP@$'),
+ array('http://test.test/file.php?file=/2/1.jpg', 'http://test.test/file.php?file=/2/1.jpg'),
+ array('http://test.test/file.php?file=/2', 'http://test.test/file.php?file=/2'),
+ array('http://test.test/file.php?file=/1/1.jpg', '$@FILEPHP@$$@SLASH@$1.jpg'),
+ array('http://test.test/file.php?file=/1//1.jpg', '$@FILEPHP@$$@SLASH@$$@SLASH@$1.jpg'),
+ array('http://test.test/file.php?file=%2f1', '$@FILEPHP@$'),
+ array('http://test.test/file.php?file=%2f2%2f1.jpg', 'http://test.test/file.php?file=%2f2%2f1.jpg'),
+ array('http://test.test/file.php?file=%2f2', 'http://test.test/file.php?file=%2f2'),
+ array('http://test.test/file.php?file=%2f1%2f1.jpg', '$@FILEPHP@$$@SLASH@$1.jpg'),
+ array('http://test.test/file.php?file=%2f1%2f%2f1.jpg', '$@FILEPHP@$$@SLASH@$$@SLASH@$1.jpg'),
+ array('http://test.test/file.php?file=%2F1', '$@FILEPHP@$'),
+ array('http://test.test/file.php?file=%2F2%2F1.jpg', 'http://test.test/file.php?file=%2F2%2F1.jpg'),
+ array('http://test.test/file.php?file=%2F2', 'http://test.test/file.php?file=%2F2'),
+ array('http://test.test/file.php?file=%2F1%2F1.jpg', '$@FILEPHP@$$@SLASH@$1.jpg'),
+ array('http://test.test/file.php?file=%2F1%2F%2F1.jpg', '$@FILEPHP@$$@SLASH@$$@SLASH@$1.jpg'),
+ );
+ }
+
+ /**
+ * Test that backup_xml_transformer replaces file php links to $@FILEPHP@$.
+ *
+ * @dataProvider filephp_links_replace_data_provider
+ * @param string $content Testing content.
+ * @param string $expected Expected result.
+ */
+ public function test_filephp_links_replace($content, $expected) {
+ global $CFG;
+
+ $CFG->wwwroot = 'http://test.test';
+
+ $transformer = new backup_xml_transformer(1);
+
+ $this->assertEquals($expected, $transformer->process($content));
+ }
+
+}
diff --git a/backup/util/helper/restore_structure_parser_processor.class.php b/backup/util/helper/restore_structure_parser_processor.class.php
index 500fa0c2612..add8d855053 100644
--- a/backup/util/helper/restore_structure_parser_processor.class.php
+++ b/backup/util/helper/restore_structure_parser_processor.class.php
@@ -60,17 +60,26 @@ class restore_structure_parser_processor extends grouped_parser_processor {
} else if (strpos($cdata, '$@FILEPHP@$') === false) { // No $@FILEPHP@$, nothing to convert
return $cdata;
}
+
+ if ($CFG->slasharguments) {
+ $slash = '/';
+ $forcedownload = '?forcedownload=1';
+ } else {
+ $slash = '%2F';
+ $forcedownload = '&forcedownload=1';
+ }
+
+ // We have to remove trailing slashes, otherwise file URLs will be restored with an extra slash.
+ $basefileurl = rtrim(moodle_url::make_legacyfile_url($this->courseid, null)->out(true), $slash);
// Decode file.php calls
$search = array ("$@FILEPHP@$");
- $replace = array(moodle_url::make_legacyfile_url($this->courseid, null));
+ $replace = array($basefileurl);
$result = str_replace($search, $replace, $cdata);
+
// Now $@SLASH@$ and $@FORCEDOWNLOAD@$ MDL-18799
$search = array('$@SLASH@$', '$@FORCEDOWNLOAD@$');
- if ($CFG->slasharguments) {
- $replace = array('/', '?forcedownload=1');
- } else {
- $replace = array('%2F', '&forcedownload=1');
- }
+ $replace = array($slash, $forcedownload);
+
return str_replace($search, $replace, $result);
}
diff --git a/backup/util/helper/tests/restore_structure_parser_processor_test.php b/backup/util/helper/tests/restore_structure_parser_processor_test.php
new file mode 100644
index 00000000000..069163ad8bd
--- /dev/null
+++ b/backup/util/helper/tests/restore_structure_parser_processor_test.php
@@ -0,0 +1,132 @@
+.
+
+/**
+ * Tests for restore_structure_parser_processor class.
+ *
+ * @package core_backup
+ * @category test
+ * @copyright 2017 Dmitrii Metelkin (dmitriim@catalyst-au.net)
+ * @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/util/helper/restore_structure_parser_processor.class.php');
+
+/**
+ * Tests for restore_structure_parser_processor class.
+ *
+ * @package core_backup
+ * @copyright 2017 Dmitrii Metelkin (dmitriim@catalyst-au.net)
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class restore_structure_parser_processor_test extends advanced_testcase {
+
+ /**
+ * Initial set up.
+ */
+ public function setUp() {
+ parent::setUp();
+
+ $this->resetAfterTest(true);
+ }
+
+ /**
+ * Data provider for ::test_process_cdata.
+ *
+ * @return array
+ */
+ public function process_cdata_data_provider() {
+ return array(
+ array(null, null, true),
+ array("$@NULL@$", null, true),
+ array("$@NULL@$ ", "$@NULL@$ ", true),
+ array(1, 1, true),
+ array(" ", " ", true),
+ array("1", "1", true),
+ array("$@FILEPHP@$1.jpg", "$@FILEPHP@$1.jpg", true),
+ array(
+ "http://test.test/$@SLASH@$",
+ "http://test.test/$@SLASH@$",
+ true
+ ),
+ array(
+ "Image",
+ "Image",
+ true
+ ),
+ array(
+ "Image",
+ "Image",
+ true
+ ),
+ array(
+ "Image",
+ "Image",
+ true
+ ),
+ array(
+ "Image",
+ "Image",
+ false
+ ),
+ array(
+ "Image",
+ "Image",
+ false
+ ),
+ array(
+ "Image",
+ "Image",
+ false
+ ),
+ array(
+ "Image",
+ "Image",
+ true
+ ),
+ array(
+ "Image",
+ "Image",
+ false
+ ),
+ );
+ }
+
+ /**
+ * Test that restore_structure_parser_processor replaces $@FILEPHP@$ to correct file php links.
+ *
+ * @dataProvider process_cdata_data_provider
+ * @param string $content Testing content.
+ * @param string $expected Expected result.
+ * @param bool $slasharguments A value for $CFG->slasharguments setting.
+ */
+ public function test_process_cdata($content, $expected, $slasharguments) {
+ global $CFG;
+
+ $CFG->slasharguments = $slasharguments;
+ $CFG->wwwroot = 'http://test.test';
+
+ $processor = new restore_structure_parser_processor(1, 1);
+
+ $this->assertEquals($expected, $processor->process_cdata($content));
+ }
+
+}