~ubuntu-branches/ubuntu/trusty/moodle/trusty-proposed

« back to all changes in this revision

Viewing changes to backup/util/helper/tests/decode_test.php

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2013-07-19 08:52:46 UTC
  • mfrom: (1.1.10)
  • Revision ID: package-import@ubuntu.com-20130719085246-yebwditc2exoap2r
Tags: 2.5.1-1
* New upstream version: 2.5.1.
  - Fixes security issues:
    CVE-2013-2242 CVE-2013-2243 CVE-2013-2244 CVE-2013-2245
    CVE-2013-2246
* Depend on apache2 instead of obsolete apache2-mpm-prefork.
* Use packaged libphp-phpmailer (closes: #429339), adodb,
  HTMLPurifier, PclZip.
* Update debconf translations, thanks Salvatore Merone, Pietro Tollot,
  Joe Hansen, Yuri Kozlov, Holger Wansing, Américo Monteiro,
  Adriano Rafael Gomes, victory, Michał Kułach.
  (closes: #716972, #716986, #717080, #717108, #717278)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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/>.
 
16
 
 
17
/**
 
18
 * @package    core_backup
 
19
 * @category   phpunit
 
20
 * @copyright  2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
 
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 
22
 */
 
23
 
 
24
defined('MOODLE_INTERNAL') || die();
 
25
 
 
26
// Include all the needed stuff
 
27
global $CFG;
 
28
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
 
29
 
 
30
 
 
31
/**
 
32
 * restore_decode tests (both rule and content)
 
33
 */
 
34
class backup_restore_decode_testcase extends basic_testcase {
 
35
 
 
36
    /**
 
37
     * test restore_decode_rule class
 
38
     */
 
39
    function test_restore_decode_rule() {
 
40
 
 
41
        // Test various incorrect constructors
 
42
        try {
 
43
            $dr = new restore_decode_rule('28 HJH', '/index.php', array());
 
44
            $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
 
45
        } catch (exception $e) {
 
46
            $this->assertTrue($e instanceof restore_decode_rule_exception);
 
47
            $this->assertEquals($e->errorcode, 'decode_rule_incorrect_name');
 
48
            $this->assertEquals($e->a, '28 HJH');
 
49
        }
 
50
 
 
51
        try {
 
52
            $dr = new restore_decode_rule('HJHJhH', '/index.php', array());
 
53
            $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
 
54
        } catch (exception $e) {
 
55
            $this->assertTrue($e instanceof restore_decode_rule_exception);
 
56
            $this->assertEquals($e->errorcode, 'decode_rule_incorrect_name');
 
57
            $this->assertEquals($e->a, 'HJHJhH');
 
58
        }
 
59
 
 
60
        try {
 
61
            $dr = new restore_decode_rule('', '/index.php', array());
 
62
            $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
 
63
        } catch (exception $e) {
 
64
            $this->assertTrue($e instanceof restore_decode_rule_exception);
 
65
            $this->assertEquals($e->errorcode, 'decode_rule_incorrect_name');
 
66
            $this->assertEquals($e->a, '');
 
67
        }
 
68
 
 
69
        try {
 
70
            $dr = new restore_decode_rule('TESTRULE', 'index.php', array());
 
71
            $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
 
72
        } catch (exception $e) {
 
73
            $this->assertTrue($e instanceof restore_decode_rule_exception);
 
74
            $this->assertEquals($e->errorcode, 'decode_rule_incorrect_urltemplate');
 
75
            $this->assertEquals($e->a, 'index.php');
 
76
        }
 
77
 
 
78
        try {
 
79
            $dr = new restore_decode_rule('TESTRULE', '', array());
 
80
            $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
 
81
        } catch (exception $e) {
 
82
            $this->assertTrue($e instanceof restore_decode_rule_exception);
 
83
            $this->assertEquals($e->errorcode, 'decode_rule_incorrect_urltemplate');
 
84
            $this->assertEquals($e->a, '');
 
85
        }
 
86
 
 
87
        try {
 
88
            $dr = new restore_decode_rule('TESTRULE', '/course/view.php?id=$1&c=$2$3', array('test1', 'test2'));
 
89
            $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
 
90
        } catch (exception $e) {
 
91
            $this->assertTrue($e instanceof restore_decode_rule_exception);
 
92
            $this->assertEquals($e->errorcode, 'decode_rule_mappings_incorrect_count');
 
93
            $this->assertEquals($e->a->placeholders, 3);
 
94
            $this->assertEquals($e->a->mappings, 2);
 
95
        }
 
96
 
 
97
        try {
 
98
            $dr = new restore_decode_rule('TESTRULE', '/course/view.php?id=$5&c=$4$1', array('test1', 'test2', 'test3'));
 
99
            $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
 
100
        } catch (exception $e) {
 
101
            $this->assertTrue($e instanceof restore_decode_rule_exception);
 
102
            $this->assertEquals($e->errorcode, 'decode_rule_nonconsecutive_placeholders');
 
103
            $this->assertEquals($e->a, '1, 4, 5');
 
104
        }
 
105
 
 
106
        try {
 
107
            $dr = new restore_decode_rule('TESTRULE', '/course/view.php?id=$0&c=$3$2', array('test1', 'test2', 'test3'));
 
108
            $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
 
109
        } catch (exception $e) {
 
110
            $this->assertTrue($e instanceof restore_decode_rule_exception);
 
111
            $this->assertEquals($e->errorcode, 'decode_rule_nonconsecutive_placeholders');
 
112
            $this->assertEquals($e->a, '0, 2, 3');
 
113
        }
 
114
 
 
115
        try {
 
116
            $dr = new restore_decode_rule('TESTRULE', '/course/view.php?id=$1&c=$3$3', array('test1', 'test2', 'test3'));
 
117
            $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
 
118
        } catch (exception $e) {
 
119
            $this->assertTrue($e instanceof restore_decode_rule_exception);
 
120
            $this->assertEquals($e->errorcode, 'decode_rule_duplicate_placeholders');
 
121
            $this->assertEquals($e->a, '1, 3, 3');
 
122
        }
 
123
 
 
124
        // Provide some example content and test the regexp is calculated ok
 
125
        $content    = '$@TESTRULE*22*33*44@$';
 
126
        $linkname   = 'TESTRULE';
 
127
        $urltemplate= '/course/view.php?id=$1&c=$3$2';
 
128
        $mappings   = array('test1', 'test2', 'test3');
 
129
        $result     = '1/course/view.php?id=44&c=8866';
 
130
        $dr = new mock_restore_decode_rule($linkname, $urltemplate, $mappings);
 
131
        $this->assertEquals($dr->decode($content), $result);
 
132
 
 
133
        $content    = '$@TESTRULE*22*33*44@$ñ$@TESTRULE*22*33*44@$';
 
134
        $linkname   = 'TESTRULE';
 
135
        $urltemplate= '/course/view.php?id=$1&c=$3$2';
 
136
        $mappings   = array('test1', 'test2', 'test3');
 
137
        $result     = '1/course/view.php?id=44&c=8866ñ1/course/view.php?id=44&c=8866';
 
138
        $dr = new mock_restore_decode_rule($linkname, $urltemplate, $mappings);
 
139
        $this->assertEquals($dr->decode($content), $result);
 
140
 
 
141
        $content    = 'ñ$@TESTRULE*22*0*44@$ñ$@TESTRULE*22*33*44@$ñ';
 
142
        $linkname   = 'TESTRULE';
 
143
        $urltemplate= '/course/view.php?id=$1&c=$3$2';
 
144
        $mappings   = array('test1', 'test2', 'test3');
 
145
        $result     = 'ñ0/course/view.php?id=22&c=440ñ1/course/view.php?id=44&c=8866ñ';
 
146
        $dr = new mock_restore_decode_rule($linkname, $urltemplate, $mappings);
 
147
        $this->assertEquals($dr->decode($content), $result);
 
148
    }
 
149
 
 
150
    /**
 
151
     * test restore_decode_content class
 
152
     */
 
153
    function test_restore_decode_content() {
 
154
        // TODO: restore_decode_content tests
 
155
    }
 
156
 
 
157
    /**
 
158
     * test restore_decode_processor class
 
159
     */
 
160
    function test_restore_decode_processor() {
 
161
        // TODO: restore_decode_processor tests
 
162
    }
 
163
}
 
164
 
 
165
/**
 
166
 * Mockup restore_decode_rule for testing purposes
 
167
 */
 
168
class mock_restore_decode_rule extends restore_decode_rule {
 
169
 
 
170
    /**
 
171
     * Originally protected, make it public
 
172
     */
 
173
    public function get_calculated_regexp() {
 
174
        return parent::get_calculated_regexp();
 
175
    }
 
176
 
 
177
    /**
 
178
     * Simply map each itemid by its double
 
179
     */
 
180
    protected function get_mapping($itemname, $itemid) {
 
181
        return $itemid * 2;
 
182
    }
 
183
 
 
184
    /**
 
185
     * Simply prefix with '0' non-mapped results and with '1' mapped ones
 
186
     */
 
187
    protected function apply_modifications($toreplace, $mappingsok) {
 
188
        return ($mappingsok ? '1' : '0') . $toreplace;
 
189
    }
 
190
}