. /** * Unit tests for backup/backup_nested_element.class.php * * @package moodlecore * @subpackage backup-tests * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ // Prevent direct access to this file if (!defined('MOODLE_INTERNAL')) { die('Direct access to this script is forbidden.'); } require_once($CFG->dirroot . '/backup/util/structure/base_atom.class.php'); require_once($CFG->dirroot . '/backup/util/structure/base_attribute.class.php'); require_once($CFG->dirroot . '/backup/util/structure/base_final_element.class.php'); require_once($CFG->dirroot . '/backup/util/structure/base_nested_element.class.php'); require_once($CFG->dirroot . '/backup/util/structure/base_optigroup.class.php'); require_once($CFG->dirroot . '/backup/util/structure/simpletest/fixtures/structuremocks.php'); /** * Unit test case the base_optigroup class. Note: highly imbricated with nested/final base elements */ class base_optigroup_test extends UnitTestCase { public static $includecoverage = array( 'backup/util/structure/base_optigroup.class.php', ); /** * Correct creation tests (s) */ function test_creation() { $instance = new mock_base_optigroup('optigroup', null, true); $this->assertIsA($instance, 'base_optigroup'); $this->assertEqual($instance->get_name(), 'optigroup'); $this->assertNull($instance->get_parent()); $this->assertEqual($instance->get_children(), array()); $this->assertEqual($instance->get_level(), 1); $this->assertTrue($instance->is_multiple()); // Get to_string() results (with values) $child1 = new mock_base_nested_element('child1', null, new mock_base_final_element('four')); $child2 = new mock_base_nested_element('child2', null, new mock_base_final_element('five')); $instance->add_child($child1); $instance->add_child($child2); $children = $instance->get_children(); $final_elements = $children['child1']->get_final_elements(); $final_elements['four']->set_value('final4value'); $final_elements['four']->add_attributes('attr4'); $grandchild = new mock_base_nested_element('grandchild', new mock_base_attribute('attr5')); $child2->add_child($grandchild); $attrs = $grandchild->get_attributes(); $attrs['attr5']->set_value('attr5value'); $tostring = $instance->to_string(true); $this->assertTrue(strpos($tostring, '!optigroup (level: 1)') !== false); $this->assertTrue(strpos($tostring, '?child2 (level: 2) =>') !== false); $this->assertTrue(strpos($tostring, ' => ') !== false); $this->assertTrue(strpos($tostring, '#four (level: 3) => final4value') !== false); $this->assertTrue(strpos($tostring, '@attr5 => attr5value') !== false); $this->assertTrue(strpos($tostring, '#five (level: 3) => not set') !== false); } /** * Incorrect creation tests (attributes and final elements) */ function itest_wrong_creation() { // Create instance with invalid name try { $instance = new mock_base_nested_element(''); $this->fail("Expecting base_atom_struct_exception exception, none occurred"); } catch (Exception $e) { $this->assertTrue($e instanceof base_atom_struct_exception); } // Create instance with incorrect (object) final element try { $obj = new stdClass; $obj->name = 'test_attr'; $instance = new mock_base_nested_element('TEST', null, $obj); $this->fail("Expecting base_element_struct_exception exception, none occurred"); } catch (Exception $e) { $this->assertTrue($e instanceof base_element_struct_exception); } // Create instance with array containing incorrect (object) final element try { $obj = new stdClass; $obj->name = 'test_attr'; $instance = new mock_base_nested_element('TEST', null, array($obj)); $this->fail("Expecting base_element_struct_exception exception, none occurred"); } catch (Exception $e) { $this->assertTrue($e instanceof base_element_struct_exception); } // Create instance with array containing duplicate final elements try { $instance = new mock_base_nested_element('TEST', null, array('VAL1', 'VAL2', 'VAL1')); $this->fail("Expecting base_element_struct_exception exception, none occurred"); } catch (Exception $e) { $this->assertTrue($e instanceof base_element_struct_exception); } // Try to get value of base_nested_element $instance = new mock_base_nested_element('TEST'); try { $instance->get_value(); $this->fail("Expecting base_element_struct_exception exception, none occurred"); } catch (Exception $e) { $this->assertTrue($e instanceof base_element_struct_exception); } // Try to set value of base_nested_element $instance = new mock_base_nested_element('TEST'); try { $instance->set_value('some_value'); $this->fail("Expecting base_element_struct_exception exception, none occurred"); } catch (Exception $e) { $this->assertTrue($e instanceof base_element_struct_exception); } // Try to clean one value of base_nested_element $instance = new mock_base_nested_element('TEST'); try { $instance->clean_value('some_value'); $this->fail("Expecting base_element_struct_exception exception, none occurred"); } catch (Exception $e) { $this->assertTrue($e instanceof base_element_struct_exception); } } }