Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/backup/util/structure/simpletest/testbasefinalelement.php
Go to the documentation of this file.
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/simpletest/fixtures/structuremocks.php');
00037 
00041 class base_final_element_test extends UnitTestCase {
00042 
00043     public static $includecoverage = array(
00044         'backup/util/structure/base_final_element.class.php',
00045         'backup/util/structure/base_nested_element.class.php'
00046     );
00047 
00051     function test_base_final_element() {
00052 
00053         // Create instance with name
00054         $instance = new mock_base_final_element('TEST');
00055         $this->assertIsA($instance, 'base_final_element');
00056         $this->assertEqual($instance->get_name(), 'TEST');
00057         $this->assertNull($instance->get_value());
00058         $this->assertEqual($instance->get_attributes(), array());
00059         $this->assertNull($instance->get_parent());
00060         $this->assertEqual($instance->get_level(), 1);
00061 
00062         // Set value
00063         $instance->set_value('value');
00064         $this->assertEqual($instance->get_value(), 'value');
00065 
00066         // Create instance with name and one object attribute
00067         $instance = new mock_base_final_element('TEST', new mock_base_attribute('ATTR1'));
00068         $attrs = $instance->get_attributes();
00069         $this->assertTrue(is_array($attrs));
00070         $this->assertEqual(count($attrs), 1);
00071         $this->assertTrue($attrs['ATTR1'] instanceof base_attribute);
00072         $this->assertEqual($attrs['ATTR1']->get_name(), 'ATTR1');
00073         $this->assertNull($attrs['ATTR1']->get_value());
00074 
00075         // Create instance with name and various object attributes
00076         $attr1 = new mock_base_attribute('ATTR1');
00077         $attr1->set_value('attr1_value');
00078         $attr2 = new mock_base_attribute('ATTR2');
00079         $instance = new mock_base_final_element('TEST', array($attr1, $attr2));
00080         $attrs = $instance->get_attributes();
00081         $this->assertTrue(is_array($attrs));
00082         $this->assertEqual(count($attrs), 2);
00083         $this->assertTrue($attrs['ATTR1'] instanceof base_attribute);
00084         $this->assertEqual($attrs['ATTR1']->get_name(), 'ATTR1');
00085         $this->assertEqual($attrs['ATTR1']->get_value(), 'attr1_value');
00086         $this->assertTrue($attrs['ATTR2'] instanceof base_attribute);
00087         $this->assertEqual($attrs['ATTR2']->get_name(), 'ATTR2');
00088         $this->assertNull($attrs['ATTR2']->get_value());
00089 
00090         // Create instance with name and one string attribute
00091         $instance = new mock_base_final_element('TEST', 'ATTR1');
00092         $attrs = $instance->get_attributes();
00093         $this->assertTrue(is_array($attrs));
00094         $this->assertEqual(count($attrs), 1);
00095         $this->assertTrue($attrs['ATTR1'] instanceof base_attribute);
00096         $this->assertEqual($attrs['ATTR1']->get_name(), 'ATTR1');
00097         $this->assertNull($attrs['ATTR1']->get_value());
00098 
00099         // Create instance with name and various object attributes
00100         $instance = new mock_base_final_element('TEST', array('ATTR1', 'ATTR2'));
00101         $attrs = $instance->get_attributes();
00102         $attrs['ATTR1']->set_value('attr1_value');
00103         $this->assertTrue(is_array($attrs));
00104         $this->assertEqual(count($attrs), 2);
00105         $this->assertTrue($attrs['ATTR1'] instanceof base_attribute);
00106         $this->assertEqual($attrs['ATTR1']->get_name(), 'ATTR1');
00107         $this->assertEqual($attrs['ATTR1']->get_value(), 'attr1_value');
00108         $this->assertTrue($attrs['ATTR2'] instanceof base_attribute);
00109         $this->assertEqual($attrs['ATTR2']->get_name(), 'ATTR2');
00110         $this->assertNull($attrs['ATTR2']->get_value());
00111 
00112         // Clean values
00113         $instance = new mock_base_final_element('TEST', array('ATTR1', 'ATTR2'));
00114         $instance->set_value('instance_value');
00115         $attrs = $instance->get_attributes();
00116         $attrs['ATTR1']->set_value('attr1_value');
00117         $this->assertEqual($instance->get_value(), 'instance_value');
00118         $this->assertEqual($attrs['ATTR1']->get_value(), 'attr1_value');
00119         $instance->clean_values();
00120         $this->assertNull($instance->get_value());
00121         $this->assertNull($attrs['ATTR1']->get_value());
00122 
00123         // Get to_string() results (with values)
00124         $instance = new mock_base_final_element('TEST', array('ATTR1', 'ATTR2'));
00125         $instance->set_value('final element value');
00126         $attrs = $instance->get_attributes();
00127         $attrs['ATTR1']->set_value('attr1 value');
00128         $tostring = $instance->to_string(true);
00129         $this->assertTrue(strpos($tostring, '#TEST (level: 1)') !== false);
00130         $this->assertTrue(strpos($tostring, ' => ') !== false);
00131         $this->assertTrue(strpos($tostring, 'final element value') !== false);
00132         $this->assertTrue(strpos($tostring, 'attr1 value') !== false);
00133     }
00134 
00138     function test_base_final_element_exceptions() {
00139 
00140         // Create instance with invalid name
00141         try {
00142             $instance = new mock_base_final_element('');
00143             $this->fail("Expecting base_atom_struct_exception exception, none occurred");
00144         } catch (Exception $e) {
00145             $this->assertTrue($e instanceof base_atom_struct_exception);
00146         }
00147 
00148         // Create instance with incorrect (object) attribute
00149         try {
00150             $obj = new stdClass;
00151             $obj->name = 'test_attr';
00152             $instance = new mock_base_final_element('TEST', $obj);
00153             $this->fail("Expecting base_element_attribute_exception exception, none occurred");
00154         } catch (Exception $e) {
00155             $this->assertTrue($e instanceof base_element_attribute_exception);
00156         }
00157 
00158         // Create instance with array containing incorrect (object) attribute
00159         try {
00160             $obj = new stdClass;
00161             $obj->name = 'test_attr';
00162             $instance = new mock_base_final_element('TEST', array($obj));
00163             $this->fail("Expecting base_element_attribute_exception exception, none occurred");
00164         } catch (Exception $e) {
00165             $this->assertTrue($e instanceof base_element_attribute_exception);
00166         }
00167 
00168         // Create instance with array containing duplicate attributes
00169         try {
00170             $instance = new mock_base_final_element('TEST', array('ATTR1', 'ATTR2', 'ATTR1'));
00171             $this->fail("Expecting base_element_attribute_exception exception, none occurred");
00172         } catch (Exception $e) {
00173             $this->assertTrue($e instanceof base_element_attribute_exception);
00174         }
00175     }
00176 }
 All Data Structures Namespaces Files Functions Variables Enumerations