/** * test backup_structure_step class */ function test_backup_structure_step() { global $CFG; $file = $CFG->tempdir . '/test/test_backup_structure_step.txt'; // Remove the test dir and any content @remove_dir(dirname($file)); // Recreate test dir if (!check_dir_exists(dirname($file), true, true)) { throw new moodle_exception('error_creating_temp_dir', 'error', dirname($file)); } // We need one (non interactive) controller for instatiating plan $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid); // We need one plan $bp = new backup_plan($bc); // We need one task with mocked basepath $bt = new mock_backup_task_basepath('taskname'); $bp->add_task($bt); // Instantiate backup_structure_step (and add it to task) $bs = new mock_backup_structure_step('steptest', basename($file), $bt); // Execute backup_structure_step $bs->execute(); // Test file has been created $this->assertTrue(file_exists($file)); // Some simple tests with contents $contents = file_get_contents($file); $this->assertTrue(strpos($contents, '<?xml version="1.0"') !== false); $this->assertTrue(strpos($contents, '<test id="1">') !== false); $this->assertTrue(strpos($contents, '<field1>value1</field1>') !== false); $this->assertTrue(strpos($contents, '<field2>value2</field2>') !== false); $this->assertTrue(strpos($contents, '</test>') !== false); unlink($file); // delete file // Remove the test dir and any content @remove_dir(dirname($file)); }
/** * Verify the add_subplugin_structure() backup method behavior and created structures. */ public function test_backup_structure_step_add_subplugin_structure() { // Create mocked task, step and element. $bt = new mock_backup_task_basepath('taskname'); $bs = new mock_backup_structure_step('steptest', null, $bt); $el = new backup_nested_element('workshop', array('id'), array('one', 'two', 'qtype')); // Wrong plugin type. try { $bs->add_subplugin_structure('fakesubplugin', $el, true, 'fakeplugintype', 'fakepluginname'); $this->assertTrue(false, 'base_step_exception expected'); } catch (exception $e) { $this->assertTrue($e instanceof backup_step_exception); $this->assertEquals('incorrect_plugin_type', $e->errorcode); } // Wrong plugin type. try { $bs->add_subplugin_structure('fakesubplugin', $el, true, 'mod', 'fakepluginname'); $this->assertTrue(false, 'base_step_exception expected'); } catch (exception $e) { $this->assertTrue($e instanceof backup_step_exception); $this->assertEquals('incorrect_plugin_name', $e->errorcode); } // Wrong plugin not having subplugins. try { $bs->add_subplugin_structure('fakesubplugin', $el, true, 'mod', 'page'); $this->assertTrue(false, 'base_step_exception expected'); } catch (exception $e) { $this->assertTrue($e instanceof backup_step_exception); $this->assertEquals('plugin_missing_subplugins_php_file', $e->errorcode); } // Wrong BC (defaulting to mod and modulename) use not having subplugins. try { $bt->set_modulename('page'); $bs->add_subplugin_structure('fakesubplugin', $el, true); $this->assertTrue(false, 'base_step_exception expected'); } catch (exception $e) { $this->assertTrue($e instanceof backup_step_exception); $this->assertEquals('plugin_missing_subplugins_php_file', $e->errorcode); } // Wrong subplugin type. try { $bs->add_subplugin_structure('fakesubplugin', $el, true, 'mod', 'workshop'); $this->assertTrue(false, 'base_step_exception expected'); } catch (exception $e) { $this->assertTrue($e instanceof backup_step_exception); $this->assertEquals('incorrect_subplugin_type', $e->errorcode); } // Wrong BC subplugin type. try { $bt->set_modulename('workshop'); $bs->add_subplugin_structure('fakesubplugin', $el, true); $this->assertTrue(false, 'base_step_exception expected'); } catch (exception $e) { $this->assertTrue($e instanceof backup_step_exception); $this->assertEquals('incorrect_subplugin_type', $e->errorcode); } // Correct call to workshopform subplugin (@ 'workshop' level). $bs->add_subplugin_structure('workshopform', $el, true, 'mod', 'workshop'); $ch = $el->get_children(); $this->assertEquals(1, count($ch)); $og = reset($ch); $this->assertTrue($og instanceof backup_optigroup); $ch = $og->get_children(); $this->assertTrue(array_key_exists('optigroup_workshopform_accumulative_workshop', $ch)); $this->assertTrue($ch['optigroup_workshopform_accumulative_workshop'] instanceof backup_subplugin_element); // Correct BC call to workshopform subplugin (@ 'assessment' level). $el = new backup_nested_element('assessment', array('id'), array('one', 'two', 'qtype')); $bt->set_modulename('workshop'); $bs->add_subplugin_structure('workshopform', $el, true); $ch = $el->get_children(); $this->assertEquals(1, count($ch)); $og = reset($ch); $this->assertTrue($og instanceof backup_optigroup); $ch = $og->get_children(); $this->assertTrue(array_key_exists('optigroup_workshopform_accumulative_assessment', $ch)); $this->assertTrue($ch['optigroup_workshopform_accumulative_assessment'] instanceof backup_subplugin_element); // TODO: Add some test covering a non-mod subplugin once we have some implemented in core. }