069163ad8bdfb3bd83530a432e5659426076733b
[moodle.git] / backup / util / helper / tests / restore_structure_parser_processor_test.php
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/>.
17 /**
18  * Tests for restore_structure_parser_processor class.
19  *
20  * @package     core_backup
21  * @category    test
22  * @copyright   2017 Dmitrii Metelkin (dmitriim@catalyst-au.net)
23  * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
30 require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
31 require_once($CFG->dirroot . '/backup/util/helper/restore_structure_parser_processor.class.php');
33 /**
34  * Tests for restore_structure_parser_processor class.
35  *
36  * @package core_backup
37  * @copyright 2017 Dmitrii Metelkin (dmitriim@catalyst-au.net)
38  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39  */
40 class restore_structure_parser_processor_test extends advanced_testcase {
42     /**
43      * Initial set up.
44      */
45     public function setUp() {
46         parent::setUp();
48         $this->resetAfterTest(true);
49     }
51     /**
52      * Data provider for ::test_process_cdata.
53      *
54      * @return array
55      */
56     public function process_cdata_data_provider() {
57         return array(
58             array(null, null, true),
59             array("$@NULL@$", null, true),
60             array("$@NULL@$ ", "$@NULL@$ ", true),
61             array(1, 1, true),
62             array(" ", " ", true),
63             array("1", "1", true),
64             array("$@FILEPHP@$1.jpg", "$@FILEPHP@$1.jpg", true),
65             array(
66                 "http://test.test/$@SLASH@$",
67                 "http://test.test/$@SLASH@$",
68                 true
69             ),
70             array(
71                 "<a href='$@FILEPHP@$1.jpg'>Image</a>",
72                 "<a href='http://test.test/file.php/11.jpg'>Image</a>",
73                 true
74             ),
75             array(
76                 "<a href='$@FILEPHP@$$@SLASH@$1.jpg'>Image</a>",
77                 "<a href='http://test.test/file.php/1/1.jpg'>Image</a>",
78                 true
79             ),
80             array(
81                 "<a href='$@FILEPHP@$$@SLASH@$$@SLASH@$1.jpg'>Image</a>",
82                 "<a href='http://test.test/file.php/1//1.jpg'>Image</a>",
83                 true
84             ),
85             array(
86                 "<a href='$@FILEPHP@$1.jpg'>Image</a>",
87                 "<a href='http://test.test/file.php?file=%2F11.jpg'>Image</a>",
88                 false
89             ),
90             array(
91                 "<a href='$@FILEPHP@$$@SLASH@$1.jpg'>Image</a>",
92                 "<a href='http://test.test/file.php?file=%2F1%2F1.jpg'>Image</a>",
93                 false
94             ),
95             array(
96                 "<a href='$@FILEPHP@$$@SLASH@$$@SLASH@$1.jpg'>Image</a>",
97                 "<a href='http://test.test/file.php?file=%2F1%2F%2F1.jpg'>Image</a>",
98                 false
99             ),
100             array(
101                 "<a href='$@FILEPHP@$$@SLASH@$1.jpg$@FORCEDOWNLOAD@$'>Image</a>",
102                 "<a href='http://test.test/file.php/1/1.jpg?forcedownload=1'>Image</a>",
103                 true
104             ),
105             array(
106                 "<a href='$@FILEPHP@$$@SLASH@$1.jpg$@FORCEDOWNLOAD@$'>Image</a>",
107                 "<a href='http://test.test/file.php?file=%2F1%2F1.jpg&amp;forcedownload=1'>Image</a>",
108                 false
109             ),
110         );
111     }
113     /**
114      * Test that restore_structure_parser_processor replaces $@FILEPHP@$ to correct file php links.
115      *
116      * @dataProvider process_cdata_data_provider
117      * @param string $content Testing content.
118      * @param string $expected Expected result.
119      * @param bool $slasharguments A value for $CFG->slasharguments setting.
120      */
121     public function test_process_cdata($content, $expected, $slasharguments) {
122         global $CFG;
124         $CFG->slasharguments = $slasharguments;
125         $CFG->wwwroot = 'http://test.test';
127         $processor = new restore_structure_parser_processor(1, 1);
129         $this->assertEquals($expected, $processor->process_cdata($content));
130     }