Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/backup/util/helper/simpletest/testdecode.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00025 // Prevent direct access to this file
00026 if (!defined('MOODLE_INTERNAL')) {
00027     die('Direct access to this script is forbidden.');
00028 }
00029 
00030 // Include all the needed stuff
00031 require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
00032 
00033 /*
00034  * restore_decode tests (both rule and content)
00035  */
00036 class restore_decode_test extends UnitTestCase {
00037 
00038     public static $includecoverage = array(
00039         'backup/util/helper/restore_decode_rule.class.php',
00040         'backup/util/helper/restore_decode_content.class.php'
00041     );
00042 
00043     /*
00044      * test restore_decode_rule class
00045      */
00046     function test_restore_decode_rule() {
00047 
00048         // Test various incorrect constructors
00049         try {
00050             $dr = new restore_decode_rule('28 HJH', '/index.php', array());
00051             $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
00052         } catch (exception $e) {
00053             $this->assertTrue($e instanceof restore_decode_rule_exception);
00054             $this->assertEqual($e->errorcode, 'decode_rule_incorrect_name');
00055             $this->assertEqual($e->a, '28 HJH');
00056         }
00057 
00058         try {
00059             $dr = new restore_decode_rule('HJHJhH', '/index.php', array());
00060             $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
00061         } catch (exception $e) {
00062             $this->assertTrue($e instanceof restore_decode_rule_exception);
00063             $this->assertEqual($e->errorcode, 'decode_rule_incorrect_name');
00064             $this->assertEqual($e->a, 'HJHJhH');
00065         }
00066 
00067         try {
00068             $dr = new restore_decode_rule('', '/index.php', array());
00069             $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
00070         } catch (exception $e) {
00071             $this->assertTrue($e instanceof restore_decode_rule_exception);
00072             $this->assertEqual($e->errorcode, 'decode_rule_incorrect_name');
00073             $this->assertEqual($e->a, '');
00074         }
00075 
00076         try {
00077             $dr = new restore_decode_rule('TESTRULE', 'index.php', array());
00078             $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
00079         } catch (exception $e) {
00080             $this->assertTrue($e instanceof restore_decode_rule_exception);
00081             $this->assertEqual($e->errorcode, 'decode_rule_incorrect_urltemplate');
00082             $this->assertEqual($e->a, 'index.php');
00083         }
00084 
00085         try {
00086             $dr = new restore_decode_rule('TESTRULE', '', array());
00087             $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
00088         } catch (exception $e) {
00089             $this->assertTrue($e instanceof restore_decode_rule_exception);
00090             $this->assertEqual($e->errorcode, 'decode_rule_incorrect_urltemplate');
00091             $this->assertEqual($e->a, '');
00092         }
00093 
00094         try {
00095             $dr = new restore_decode_rule('TESTRULE', '/course/view.php?id=$1&c=$2$3', array('test1', 'test2'));
00096             $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
00097         } catch (exception $e) {
00098             $this->assertTrue($e instanceof restore_decode_rule_exception);
00099             $this->assertEqual($e->errorcode, 'decode_rule_mappings_incorrect_count');
00100             $this->assertEqual($e->a->placeholders, 3);
00101             $this->assertEqual($e->a->mappings, 2);
00102         }
00103 
00104         try {
00105             $dr = new restore_decode_rule('TESTRULE', '/course/view.php?id=$5&c=$4$1', array('test1', 'test2', 'test3'));
00106             $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
00107         } catch (exception $e) {
00108             $this->assertTrue($e instanceof restore_decode_rule_exception);
00109             $this->assertEqual($e->errorcode, 'decode_rule_nonconsecutive_placeholders');
00110             $this->assertEqual($e->a, '1, 4, 5');
00111         }
00112 
00113         try {
00114             $dr = new restore_decode_rule('TESTRULE', '/course/view.php?id=$0&c=$3$2', array('test1', 'test2', 'test3'));
00115             $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
00116         } catch (exception $e) {
00117             $this->assertTrue($e instanceof restore_decode_rule_exception);
00118             $this->assertEqual($e->errorcode, 'decode_rule_nonconsecutive_placeholders');
00119             $this->assertEqual($e->a, '0, 2, 3');
00120         }
00121 
00122         try {
00123             $dr = new restore_decode_rule('TESTRULE', '/course/view.php?id=$1&c=$3$3', array('test1', 'test2', 'test3'));
00124             $this->assertTrue(false, 'restore_decode_rule_exception exception expected');
00125         } catch (exception $e) {
00126             $this->assertTrue($e instanceof restore_decode_rule_exception);
00127             $this->assertEqual($e->errorcode, 'decode_rule_duplicate_placeholders');
00128             $this->assertEqual($e->a, '1, 3, 3');
00129         }
00130 
00131         // Provide some example content and test the regexp is calculated ok
00132         $content    = '$@TESTRULE*22*33*44@$';
00133         $linkname   = 'TESTRULE';
00134         $urltemplate= '/course/view.php?id=$1&c=$3$2';
00135         $mappings   = array('test1', 'test2', 'test3');
00136         $result     = '1/course/view.php?id=44&c=8866';
00137         $dr = new mock_restore_decode_rule($linkname, $urltemplate, $mappings);
00138         $this->assertEqual($dr->decode($content), $result);
00139 
00140         $content    = '$@TESTRULE*22*33*44@$ñ$@TESTRULE*22*33*44@$';
00141         $linkname   = 'TESTRULE';
00142         $urltemplate= '/course/view.php?id=$1&c=$3$2';
00143         $mappings   = array('test1', 'test2', 'test3');
00144         $result     = '1/course/view.php?id=44&c=8866ñ1/course/view.php?id=44&c=8866';
00145         $dr = new mock_restore_decode_rule($linkname, $urltemplate, $mappings);
00146         $this->assertEqual($dr->decode($content), $result);
00147 
00148         $content    = 'ñ$@TESTRULE*22*0*44@$ñ$@TESTRULE*22*33*44@$ñ';
00149         $linkname   = 'TESTRULE';
00150         $urltemplate= '/course/view.php?id=$1&c=$3$2';
00151         $mappings   = array('test1', 'test2', 'test3');
00152         $result     = 'ñ0/course/view.php?id=22&c=440ñ1/course/view.php?id=44&c=8866ñ';
00153         $dr = new mock_restore_decode_rule($linkname, $urltemplate, $mappings);
00154         $this->assertEqual($dr->decode($content), $result);
00155     }
00156 
00157     /*
00158      * test restore_decode_content class
00159      */
00160     function test_restore_decode_content() {
00161         // TODO: restore_decode_content tests
00162     }
00163 
00164     /*
00165      * test restore_decode_processor class
00166      */
00167     function test_restore_decode_processor() {
00168         // TODO: restore_decode_processor tests
00169     }
00170 }
00171 
00175 class mock_restore_decode_rule extends restore_decode_rule {
00176 
00180     public function get_calculated_regexp() {
00181         return parent::get_calculated_regexp();
00182     }
00183 
00187     protected function get_mapping($itemname, $itemid) {
00188         return $itemid * 2;
00189     }
00190 
00194     protected function apply_modifications($toreplace, $mappingsok) {
00195         return ($mappingsok ? '1' : '0') . $toreplace;
00196     }
00197 }
 All Data Structures Namespaces Files Functions Variables Enumerations