|
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 00027 // Prevent direct access to this file 00028 if (!defined('MOODLE_INTERNAL')) { 00029 die('Direct access to this script is forbidden.'); 00030 } 00031 00032 require_once($CFG->dirroot . '/backup/util/structure/base_atom.class.php'); 00033 00038 class base_atom_test extends UnitTestCase { 00039 00040 public static $includecoverage = array('/backup/util/structure/base_atom.class.php'); 00041 00045 function test_base_atom() { 00046 $name_with_all_chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'; 00047 $value_to_test = 'Some <value> to test'; 00048 00049 // Create instance with correct names 00050 $instance = new mock_base_atom($name_with_all_chars); 00051 $this->assertIsA($instance, 'base_atom'); 00052 $this->assertEqual($instance->get_name(), $name_with_all_chars); 00053 $this->assertFalse($instance->is_set()); 00054 $this->assertNull($instance->get_value()); 00055 00056 // Set value 00057 $instance->set_value($value_to_test); 00058 $this->assertEqual($instance->get_value(), $value_to_test); 00059 $this->assertTrue($instance->is_set()); 00060 00061 // Clean value 00062 $instance->clean_value(); 00063 $this->assertFalse($instance->is_set()); 00064 $this->assertNull($instance->get_value()); 00065 00066 // Get to_string() results (with values) 00067 $instance = new mock_base_atom($name_with_all_chars); 00068 $instance->set_value($value_to_test); 00069 $tostring = $instance->to_string(true); 00070 $this->assertTrue(strpos($tostring, $name_with_all_chars) !== false); 00071 $this->assertTrue(strpos($tostring, ' => ') !== false); 00072 $this->assertTrue(strpos($tostring, $value_to_test) !== false); 00073 00074 // Get to_string() results (without values) 00075 $tostring = $instance->to_string(false); 00076 $this->assertTrue(strpos($tostring, $name_with_all_chars) !== false); 00077 $this->assertFalse(strpos($tostring, ' => ')); 00078 $this->assertFalse(strpos($tostring, $value_to_test)); 00079 } 00080 00084 function test_base_atom_exceptions() { 00085 // empty names 00086 try { 00087 $instance = new mock_base_atom(''); 00088 $this->fail("Expecting base_atom_struct_exception exception, none occurred"); 00089 } catch (Exception $e) { 00090 $this->assertTrue($e instanceof base_atom_struct_exception); 00091 } 00092 00093 // whitespace names 00094 try { 00095 $instance = new mock_base_atom('TESTING ATOM'); 00096 $this->fail("Expecting base_atom_struct_exception exception, none occurred"); 00097 } catch (Exception $e) { 00098 $this->assertTrue($e instanceof base_atom_struct_exception); 00099 } 00100 00101 // ascii names 00102 try { 00103 $instance = new mock_base_atom('TESTING-ATOM'); 00104 $this->fail("Expecting base_atom_struct_exception exception, none occurred"); 00105 } catch (Exception $e) { 00106 $this->assertTrue($e instanceof base_atom_struct_exception); 00107 } 00108 try { 00109 $instance = new mock_base_atom('TESTING_ATOM_Á'); 00110 $this->fail("Expecting base_atom_struct_exception exception, none occurred"); 00111 } catch (Exception $e) { 00112 $this->assertTrue($e instanceof base_atom_struct_exception); 00113 } 00114 00115 // setting already set value 00116 $instance = new mock_base_atom('TEST'); 00117 $instance->set_value('test'); 00118 try { 00119 $instance->set_value('test'); 00120 $this->fail("Expecting base_atom_content_exception exception, none occurred"); 00121 } catch (Exception $e) { 00122 $this->assertTrue($e instanceof base_atom_content_exception); 00123 } 00124 } 00125 } 00126 00130 class mock_base_atom extends base_atom { 00131 // Nothing new in this class, just an instantiable base_atom class 00132 // with the is_set() method public for testing purposes 00133 public function is_set() { 00134 return parent::is_set(); 00135 } 00136 }