|
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 require_once($CFG->dirroot . '/backup/util/structure/base_attribute.class.php'); 00034 require_once($CFG->dirroot . '/backup/util/structure/base_final_element.class.php'); 00035 require_once($CFG->dirroot . '/backup/util/structure/base_nested_element.class.php'); 00036 require_once($CFG->dirroot . '/backup/util/structure/base_optigroup.class.php'); 00037 require_once($CFG->dirroot . '/backup/util/structure/simpletest/fixtures/structuremocks.php'); 00038 00042 class base_optigroup_test extends UnitTestCase { 00043 00044 public static $includecoverage = array( 00045 'backup/util/structure/base_optigroup.class.php', 00046 ); 00047 00051 function test_creation() { 00052 $instance = new mock_base_optigroup('optigroup', null, true); 00053 $this->assertIsA($instance, 'base_optigroup'); 00054 $this->assertEqual($instance->get_name(), 'optigroup'); 00055 $this->assertNull($instance->get_parent()); 00056 $this->assertEqual($instance->get_children(), array()); 00057 $this->assertEqual($instance->get_level(), 1); 00058 $this->assertTrue($instance->is_multiple()); 00059 00060 // Get to_string() results (with values) 00061 $child1 = new mock_base_nested_element('child1', null, new mock_base_final_element('four')); 00062 $child2 = new mock_base_nested_element('child2', null, new mock_base_final_element('five')); 00063 $instance->add_child($child1); 00064 $instance->add_child($child2); 00065 $children = $instance->get_children(); 00066 $final_elements = $children['child1']->get_final_elements(); 00067 $final_elements['four']->set_value('final4value'); 00068 $final_elements['four']->add_attributes('attr4'); 00069 $grandchild = new mock_base_nested_element('grandchild', new mock_base_attribute('attr5')); 00070 $child2->add_child($grandchild); 00071 $attrs = $grandchild->get_attributes(); 00072 $attrs['attr5']->set_value('attr5value'); 00073 $tostring = $instance->to_string(true); 00074 $this->assertTrue(strpos($tostring, '!optigroup (level: 1)') !== false); 00075 $this->assertTrue(strpos($tostring, '?child2 (level: 2) =>') !== false); 00076 $this->assertTrue(strpos($tostring, ' => ') !== false); 00077 $this->assertTrue(strpos($tostring, '#four (level: 3) => final4value') !== false); 00078 $this->assertTrue(strpos($tostring, '@attr5 => attr5value') !== false); 00079 $this->assertTrue(strpos($tostring, '#five (level: 3) => not set') !== false); 00080 } 00081 00085 function itest_wrong_creation() { 00086 00087 // Create instance with invalid name 00088 try { 00089 $instance = new mock_base_nested_element(''); 00090 $this->fail("Expecting base_atom_struct_exception exception, none occurred"); 00091 } catch (Exception $e) { 00092 $this->assertTrue($e instanceof base_atom_struct_exception); 00093 } 00094 00095 // Create instance with incorrect (object) final element 00096 try { 00097 $obj = new stdClass; 00098 $obj->name = 'test_attr'; 00099 $instance = new mock_base_nested_element('TEST', null, $obj); 00100 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 00101 } catch (Exception $e) { 00102 $this->assertTrue($e instanceof base_element_struct_exception); 00103 } 00104 00105 // Create instance with array containing incorrect (object) final element 00106 try { 00107 $obj = new stdClass; 00108 $obj->name = 'test_attr'; 00109 $instance = new mock_base_nested_element('TEST', null, array($obj)); 00110 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 00111 } catch (Exception $e) { 00112 $this->assertTrue($e instanceof base_element_struct_exception); 00113 } 00114 00115 // Create instance with array containing duplicate final elements 00116 try { 00117 $instance = new mock_base_nested_element('TEST', null, array('VAL1', 'VAL2', 'VAL1')); 00118 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 00119 } catch (Exception $e) { 00120 $this->assertTrue($e instanceof base_element_struct_exception); 00121 } 00122 00123 // Try to get value of base_nested_element 00124 $instance = new mock_base_nested_element('TEST'); 00125 try { 00126 $instance->get_value(); 00127 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 00128 } catch (Exception $e) { 00129 $this->assertTrue($e instanceof base_element_struct_exception); 00130 } 00131 00132 // Try to set value of base_nested_element 00133 $instance = new mock_base_nested_element('TEST'); 00134 try { 00135 $instance->set_value('some_value'); 00136 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 00137 } catch (Exception $e) { 00138 $this->assertTrue($e instanceof base_element_struct_exception); 00139 } 00140 00141 // Try to clean one value of base_nested_element 00142 $instance = new mock_base_nested_element('TEST'); 00143 try { 00144 $instance->clean_value('some_value'); 00145 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 00146 } catch (Exception $e) { 00147 $this->assertTrue($e instanceof base_element_struct_exception); 00148 } 00149 } 00150 }