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