Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]
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/xml/output/xml_output.class.php'); 29 require_once($CFG->dirroot . '/backup/util/xml/output/memory_xml_output.class.php'); 30 require_once($CFG->dirroot . '/backup/util/xml/output/file_xml_output.class.php'); 31 32 /* 33 * xml_output tests (base, memory and file) 34 */ 35 class xml_output_test extends advanced_testcase { 36 37 /* 38 * test memory_xml_output 39 */ 40 function test_memory_xml_output() { 41 // Instantiate xml_output 42 $xo = new memory_xml_output(); 43 $this->assertTrue($xo instanceof xml_output); 44 45 // Try to write some contents before starting it 46 $xo = new memory_xml_output(); 47 try { 48 $xo->write('test'); 49 $this->assertTrue(false, 'xml_output_exception expected'); 50 } catch (exception $e) { 51 $this->assertTrue($e instanceof xml_output_exception); 52 $this->assertEquals($e->errorcode, 'xml_output_not_started'); 53 } 54 55 // Try to set buffer size if unsupported 56 $xo = new memory_xml_output(); 57 try { 58 $xo->set_buffersize(8192); 59 $this->assertTrue(false, 'xml_output_exception expected'); 60 } catch (exception $e) { 61 $this->assertTrue($e instanceof xml_output_exception); 62 $this->assertEquals($e->errorcode, 'xml_output_buffer_nosupport'); 63 } 64 65 // Try to set buffer after start 66 $xo = new memory_xml_output(); 67 $xo->start(); 68 try { 69 $xo->set_buffersize(8192); 70 $this->assertTrue(false, 'xml_output_exception expected'); 71 } catch (exception $e) { 72 $this->assertTrue($e instanceof xml_output_exception); 73 $this->assertEquals($e->errorcode, 'xml_output_already_started'); 74 } 75 76 // Try to stop output before starting it 77 $xo = new memory_xml_output(); 78 try { 79 $xo->stop(); 80 $this->assertTrue(false, 'xml_output_exception expected'); 81 } catch (exception $e) { 82 $this->assertTrue($e instanceof xml_output_exception); 83 $this->assertEquals($e->errorcode, 'xml_output_not_started'); 84 } 85 86 // Try to debug_info() before starting 87 $xo = new memory_xml_output(); 88 try { 89 $xo->debug_info(); 90 $this->assertTrue(false, 'xml_output_exception expected'); 91 } catch (exception $e) { 92 $this->assertTrue($e instanceof xml_output_exception); 93 $this->assertEquals($e->errorcode, 'xml_output_not_stopped'); 94 } 95 96 // Start output twice 97 $xo = new memory_xml_output(); 98 $xo->start(); 99 try { 100 $xo->start(); 101 $this->assertTrue(false, 'xml_output_exception expected'); 102 } catch (exception $e) { 103 $this->assertTrue($e instanceof xml_output_exception); 104 $this->assertEquals($e->errorcode, 'xml_output_already_started'); 105 } 106 107 // Try to debug_info() before stoping 108 $xo = new memory_xml_output(); 109 $xo->start(); 110 try { 111 $xo->debug_info(); 112 $this->assertTrue(false, 'xml_output_exception expected'); 113 } catch (exception $e) { 114 $this->assertTrue($e instanceof xml_output_exception); 115 $this->assertEquals($e->errorcode, 'xml_output_not_stopped'); 116 } 117 118 // Stop output twice 119 $xo = new memory_xml_output(); 120 $xo->start(); 121 $xo->stop(); 122 try { 123 $xo->stop(); 124 $this->assertTrue(false, 'xml_output_exception expected'); 125 } catch (exception $e) { 126 $this->assertTrue($e instanceof xml_output_exception); 127 $this->assertEquals($e->errorcode, 'xml_output_not_started'); 128 } 129 130 // Try to re-start after stop 131 $xo = new memory_xml_output(); 132 $xo->start(); 133 $xo->stop(); 134 try { 135 $xo->start(); 136 $this->assertTrue(false, 'xml_output_exception expected'); 137 } catch (exception $e) { 138 $this->assertTrue($e instanceof xml_output_exception); 139 $this->assertEquals($e->errorcode, 'xml_output_already_stopped'); 140 } 141 142 // Try to get contents before stopping 143 $xo = new memory_xml_output(); 144 $xo->start(); 145 try { 146 $xo->get_allcontents(); 147 $this->assertTrue(false, 'xml_output_exception expected'); 148 } catch (exception $e) { 149 $this->assertTrue($e instanceof xml_output_exception); 150 $this->assertEquals($e->errorcode, 'xml_output_not_stopped'); 151 } 152 153 // Write some contents and check them 154 $xo = new memory_xml_output(); 155 $xo->start(); 156 $xo->write('first test'); 157 $xo->stop(); 158 $this->assertEquals('first test', $xo->get_allcontents()); 159 160 // Write 3 times and check them 161 $xo = new memory_xml_output(); 162 $xo->start(); 163 $xo->write('first test'); 164 $xo->write(', sencond test'); 165 $xo->write(', third test'); 166 $xo->stop(); 167 $this->assertEquals('first test, sencond test, third test', $xo->get_allcontents()); 168 169 // Write some line feeds, tabs and friends 170 $string = "\n\r\tcrazy test\n\r\t"; 171 $xo = new memory_xml_output(); 172 $xo->start(); 173 $xo->write($string); 174 $xo->stop(); 175 $this->assertEquals($string, $xo->get_allcontents()); 176 177 // Write some UTF-8 chars 178 $string = 'áéíóú'; 179 $xo = new memory_xml_output(); 180 $xo->start(); 181 $xo->write($string); 182 $xo->stop(); 183 $this->assertEquals($string, $xo->get_allcontents()); 184 185 // Write some empty content 186 $xo = new memory_xml_output(); 187 $xo->start(); 188 $xo->write('Hello '); 189 $xo->write(null); 190 $xo->write(false); 191 $xo->write(''); 192 $xo->write('World'); 193 $xo->write(null); 194 $xo->stop(); 195 $this->assertEquals('Hello World', $xo->get_allcontents()); 196 197 // Get debug info 198 $xo = new memory_xml_output(); 199 $xo->start(); 200 $xo->write('01234'); 201 $xo->write('56789'); 202 $xo->stop(); 203 $this->assertEquals('0123456789', $xo->get_allcontents()); 204 $debug = $xo->debug_info(); 205 $this->assertTrue(is_array($debug)); 206 $this->assertTrue(array_key_exists('sent', $debug)); 207 $this->assertEquals($debug['sent'], 10); 208 } 209 210 /* 211 * test file_xml_output 212 */ 213 function test_file_xml_output() { 214 global $CFG; 215 216 $this->resetAfterTest(); 217 218 $file = $CFG->tempdir . '/test/test_file_xml_output.txt'; 219 // Remove the test dir and any content 220 @remove_dir(dirname($file)); 221 // Recreate test dir 222 if (!check_dir_exists(dirname($file), true, true)) { 223 throw new moodle_exception('error_creating_temp_dir', 'error', dirname($file)); 224 } 225 226 // Instantiate xml_output 227 $xo = new file_xml_output($file); 228 $this->assertTrue($xo instanceof xml_output); 229 230 // Try to init file in (near) impossible path 231 $file = $CFG->tempdir . '/test_azby/test_file_xml_output.txt'; 232 $xo = new file_xml_output($file); 233 try { 234 $xo->start(); 235 $this->assertTrue(false, 'xml_output_exception expected'); 236 } catch (exception $e) { 237 $this->assertTrue($e instanceof xml_output_exception); 238 $this->assertEquals($e->errorcode, 'directory_not_exists'); 239 } 240 241 // Try to init file already existing 242 $file = $CFG->tempdir . '/test/test_file_xml_output.txt'; 243 file_put_contents($file, 'createdtobedeleted'); // create file manually 244 $xo = new file_xml_output($file); 245 try { 246 $xo->start(); 247 $this->assertTrue(false, 'xml_output_exception expected'); 248 } catch (exception $e) { 249 $this->assertTrue($e instanceof xml_output_exception); 250 $this->assertEquals($e->errorcode, 'file_already_exists'); 251 } 252 unlink($file); // delete file 253 254 // Send some output and check 255 $file = $CFG->tempdir . '/test/test_file_xml_output.txt'; 256 $xo = new file_xml_output($file); 257 $xo->start(); 258 $xo->write('first text'); 259 $xo->stop(); 260 $this->assertEquals('first text', file_get_contents($file)); 261 unlink($file); // delete file 262 263 // With buffer of 4 bytes, send 3 contents of 3 bytes each 264 // so we force both buffering and last write on stop 265 $file = $CFG->tempdir . '/test/test_file_xml_output.txt'; 266 $xo = new file_xml_output($file); 267 $xo->set_buffersize(5); 268 $xo->start(); 269 $xo->write('123'); 270 $xo->write('456'); 271 $xo->write('789'); 272 $xo->stop(); 273 $this->assertEquals('123456789', file_get_contents($file)); 274 unlink($file); // delete file 275 276 // Write some line feeds, tabs and friends 277 $file = $CFG->tempdir . '/test/test_file_xml_output.txt'; 278 $string = "\n\r\tcrazy test\n\r\t"; 279 $xo = new file_xml_output($file); 280 $xo->start(); 281 $xo->write($string); 282 $xo->stop(); 283 $this->assertEquals($string, file_get_contents($file)); 284 unlink($file); // delete file 285 286 // Write some UTF-8 chars 287 $file = $CFG->tempdir . '/test/test_file_xml_output.txt'; 288 $string = 'áéíóú'; 289 $xo = new file_xml_output($file); 290 $xo->start(); 291 $xo->write($string); 292 $xo->stop(); 293 $this->assertEquals($string, file_get_contents($file)); 294 unlink($file); // delete file 295 296 // Remove the test dir and any content 297 @remove_dir(dirname($file)); 298 } 299 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body