|
Moodle
2.2.1
http://www.collinsharper.com
|
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/xml/output/xml_output.class.php'); 00032 require_once($CFG->dirroot . '/backup/util/xml/output/memory_xml_output.class.php'); 00033 require_once($CFG->dirroot . '/backup/util/xml/output/file_xml_output.class.php'); 00034 00035 /* 00036 * xml_output tests (base, memory and file) 00037 */ 00038 class xml_output_test extends UnitTestCase { 00039 00040 public static $includecoverage = array('backup/util/xml/output'); 00041 public static $excludecoverage = array('backup/util/xml/output/simpletest'); 00042 00043 /* 00044 * test memory_xml_output 00045 */ 00046 function test_memory_xml_output() { 00047 // Instantiate xml_output 00048 $xo = new memory_xml_output(); 00049 $this->assertTrue($xo instanceof xml_output); 00050 00051 // Try to write some contents before starting it 00052 $xo = new memory_xml_output(); 00053 try { 00054 $xo->write('test'); 00055 $this->assertTrue(false, 'xml_output_exception expected'); 00056 } catch (exception $e) { 00057 $this->assertTrue($e instanceof xml_output_exception); 00058 $this->assertEqual($e->errorcode, 'xml_output_not_started'); 00059 } 00060 00061 // Try to set buffer size if unsupported 00062 $xo = new memory_xml_output(); 00063 try { 00064 $xo->set_buffersize(8192); 00065 $this->assertTrue(false, 'xml_output_exception expected'); 00066 } catch (exception $e) { 00067 $this->assertTrue($e instanceof xml_output_exception); 00068 $this->assertEqual($e->errorcode, 'xml_output_buffer_nosupport'); 00069 } 00070 00071 // Try to set buffer after start 00072 $xo = new memory_xml_output(); 00073 $xo->start(); 00074 try { 00075 $xo->set_buffersize(8192); 00076 $this->assertTrue(false, 'xml_output_exception expected'); 00077 } catch (exception $e) { 00078 $this->assertTrue($e instanceof xml_output_exception); 00079 $this->assertEqual($e->errorcode, 'xml_output_already_started'); 00080 } 00081 00082 // Try to stop output before starting it 00083 $xo = new memory_xml_output(); 00084 try { 00085 $xo->stop(); 00086 $this->assertTrue(false, 'xml_output_exception expected'); 00087 } catch (exception $e) { 00088 $this->assertTrue($e instanceof xml_output_exception); 00089 $this->assertEqual($e->errorcode, 'xml_output_not_started'); 00090 } 00091 00092 // Try to debug_info() before starting 00093 $xo = new memory_xml_output(); 00094 try { 00095 $xo->debug_info(); 00096 $this->assertTrue(false, 'xml_output_exception expected'); 00097 } catch (exception $e) { 00098 $this->assertTrue($e instanceof xml_output_exception); 00099 $this->assertEqual($e->errorcode, 'xml_output_not_stopped'); 00100 } 00101 00102 // Start output twice 00103 $xo = new memory_xml_output(); 00104 $xo->start(); 00105 try { 00106 $xo->start(); 00107 $this->assertTrue(false, 'xml_output_exception expected'); 00108 } catch (exception $e) { 00109 $this->assertTrue($e instanceof xml_output_exception); 00110 $this->assertEqual($e->errorcode, 'xml_output_already_started'); 00111 } 00112 00113 // Try to debug_info() before stoping 00114 $xo = new memory_xml_output(); 00115 $xo->start(); 00116 try { 00117 $xo->debug_info(); 00118 $this->assertTrue(false, 'xml_output_exception expected'); 00119 } catch (exception $e) { 00120 $this->assertTrue($e instanceof xml_output_exception); 00121 $this->assertEqual($e->errorcode, 'xml_output_not_stopped'); 00122 } 00123 00124 // Stop output twice 00125 $xo = new memory_xml_output(); 00126 $xo->start(); 00127 $xo->stop(); 00128 try { 00129 $xo->stop(); 00130 $this->assertTrue(false, 'xml_output_exception expected'); 00131 } catch (exception $e) { 00132 $this->assertTrue($e instanceof xml_output_exception); 00133 $this->assertEqual($e->errorcode, 'xml_output_not_started'); 00134 } 00135 00136 // Try to re-start after stop 00137 $xo = new memory_xml_output(); 00138 $xo->start(); 00139 $xo->stop(); 00140 try { 00141 $xo->start(); 00142 $this->assertTrue(false, 'xml_output_exception expected'); 00143 } catch (exception $e) { 00144 $this->assertTrue($e instanceof xml_output_exception); 00145 $this->assertEqual($e->errorcode, 'xml_output_already_stopped'); 00146 } 00147 00148 // Try to get contents before stopping 00149 $xo = new memory_xml_output(); 00150 $xo->start(); 00151 try { 00152 $xo->get_allcontents(); 00153 $this->assertTrue(false, 'xml_output_exception expected'); 00154 } catch (exception $e) { 00155 $this->assertTrue($e instanceof xml_output_exception); 00156 $this->assertEqual($e->errorcode, 'xml_output_not_stopped'); 00157 } 00158 00159 // Write some contents and check them 00160 $xo = new memory_xml_output(); 00161 $xo->start(); 00162 $xo->write('first test'); 00163 $xo->stop(); 00164 $this->assertEqual('first test', $xo->get_allcontents()); 00165 00166 // Write 3 times and check them 00167 $xo = new memory_xml_output(); 00168 $xo->start(); 00169 $xo->write('first test'); 00170 $xo->write(', sencond test'); 00171 $xo->write(', third test'); 00172 $xo->stop(); 00173 $this->assertEqual('first test, sencond test, third test', $xo->get_allcontents()); 00174 00175 // Write some line feeds, tabs and friends 00176 $string = "\n\r\tcrazy test\n\r\t"; 00177 $xo = new memory_xml_output(); 00178 $xo->start(); 00179 $xo->write($string); 00180 $xo->stop(); 00181 $this->assertEqual($string, $xo->get_allcontents()); 00182 00183 // Write some UTF-8 chars 00184 $string = 'áéíóú'; 00185 $xo = new memory_xml_output(); 00186 $xo->start(); 00187 $xo->write($string); 00188 $xo->stop(); 00189 $this->assertEqual($string, $xo->get_allcontents()); 00190 00191 // Write some empty content 00192 $xo = new memory_xml_output(); 00193 $xo->start(); 00194 $xo->write('Hello '); 00195 $xo->write(null); 00196 $xo->write(false); 00197 $xo->write(''); 00198 $xo->write('World'); 00199 $xo->write(null); 00200 $xo->stop(); 00201 $this->assertEqual('Hello World', $xo->get_allcontents()); 00202 00203 // Get debug info 00204 $xo = new memory_xml_output(); 00205 $xo->start(); 00206 $xo->write('01234'); 00207 $xo->write('56789'); 00208 $xo->stop(); 00209 $this->assertEqual('0123456789', $xo->get_allcontents()); 00210 $debug = $xo->debug_info(); 00211 $this->assertTrue(is_array($debug)); 00212 $this->assertTrue(array_key_exists('sent', $debug)); 00213 $this->assertEqual($debug['sent'], 10); 00214 } 00215 00216 /* 00217 * test file_xml_output 00218 */ 00219 function test_file_xml_output() { 00220 global $CFG; 00221 00222 $file = $CFG->tempdir . '/test/test_file_xml_output.txt'; 00223 // Remove the test dir and any content 00224 @remove_dir(dirname($file)); 00225 // Recreate test dir 00226 if (!check_dir_exists(dirname($file), true, true)) { 00227 throw new moodle_exception('error_creating_temp_dir', 'error', dirname($file)); 00228 } 00229 00230 // Instantiate xml_output 00231 $xo = new file_xml_output($file); 00232 $this->assertTrue($xo instanceof xml_output); 00233 00234 // Try to init file in (near) impossible path 00235 $file = $CFG->tempdir . '/test_azby/test_file_xml_output.txt'; 00236 $xo = new file_xml_output($file); 00237 try { 00238 $xo->start(); 00239 $this->assertTrue(false, 'xml_output_exception expected'); 00240 } catch (exception $e) { 00241 $this->assertTrue($e instanceof xml_output_exception); 00242 $this->assertEqual($e->errorcode, 'directory_not_exists'); 00243 } 00244 00245 // Try to init file already existing 00246 $file = $CFG->tempdir . '/test/test_file_xml_output.txt'; 00247 file_put_contents($file, 'createdtobedeleted'); // create file manually 00248 $xo = new file_xml_output($file); 00249 try { 00250 $xo->start(); 00251 $this->assertTrue(false, 'xml_output_exception expected'); 00252 } catch (exception $e) { 00253 $this->assertTrue($e instanceof xml_output_exception); 00254 $this->assertEqual($e->errorcode, 'file_already_exists'); 00255 } 00256 unlink($file); // delete file 00257 00258 // Send some output and check 00259 $file = $CFG->tempdir . '/test/test_file_xml_output.txt'; 00260 $xo = new file_xml_output($file); 00261 $xo->start(); 00262 $xo->write('first text'); 00263 $xo->stop(); 00264 $this->assertEqual('first text', file_get_contents($file)); 00265 unlink($file); // delete file 00266 00267 // With buffer of 4 bytes, send 3 contents of 3 bytes each 00268 // so we force both buffering and last write on stop 00269 $file = $CFG->tempdir . '/test/test_file_xml_output.txt'; 00270 $xo = new file_xml_output($file); 00271 $xo->set_buffersize(5); 00272 $xo->start(); 00273 $xo->write('123'); 00274 $xo->write('456'); 00275 $xo->write('789'); 00276 $xo->stop(); 00277 $this->assertEqual('123456789', file_get_contents($file)); 00278 unlink($file); // delete file 00279 00280 // Write some line feeds, tabs and friends 00281 $file = $CFG->tempdir . '/test/test_file_xml_output.txt'; 00282 $string = "\n\r\tcrazy test\n\r\t"; 00283 $xo = new file_xml_output($file); 00284 $xo->start(); 00285 $xo->write($string); 00286 $xo->stop(); 00287 $this->assertEqual($string, file_get_contents($file)); 00288 unlink($file); // delete file 00289 00290 // Write some UTF-8 chars 00291 $file = $CFG->tempdir . '/test/test_file_xml_output.txt'; 00292 $string = 'áéíóú'; 00293 $xo = new file_xml_output($file); 00294 $xo->start(); 00295 $xo->write($string); 00296 $xo->stop(); 00297 $this->assertEqual($string, file_get_contents($file)); 00298 unlink($file); // delete file 00299 00300 // Remove the test dir and any content 00301 @remove_dir(dirname($file)); 00302 } 00303 }