diff --git a/backup/controller/tests/controller_test.php b/backup/controller/tests/controller_test.php new file mode 100644 index 00000000000..41fd99f4fe8 --- /dev/null +++ b/backup/controller/tests/controller_test.php @@ -0,0 +1,96 @@ +. + +/** + * @package core_backup + * @category phpunit + * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +// Include all the needed stuff +global $CFG; +require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php'); + +/* + * controller tests (all) + */ +class backup_controller_test extends advanced_testcase { + + protected $moduleid; // course_modules id used for testing + protected $sectionid; // course_sections id used for testing + protected $courseid; // course id used for testing + protected $userid; // user used if for testing + + public function setUp() { + global $DB, $CFG; + + $this->resetAfterTest(true); + + $course = $this->getDataGenerator()->create_course(); + $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id), array('section'=>3)); + $coursemodule = $DB->get_record('course_modules', array('id'=>$page->cmid)); + + $this->moduleid = $coursemodule->id; + $this->sectionid = $DB->get_field("course_sections", 'id', array("section"=>$coursemodule->section, "course"=>$course->id)); + $this->courseid = $coursemodule->course; + $this->userid = 2; // admin + + // Disable all loggers + $CFG->backup_error_log_logger_level = backup::LOG_NONE; + $CFG->backup_output_indented_logger_level = backup::LOG_NONE; + $CFG->backup_file_logger_level = backup::LOG_NONE; + $CFG->backup_database_logger_level = backup::LOG_NONE; + $CFG->backup_file_logger_level_extra = backup::LOG_NONE; + } + + /* + * test base_setting class + */ + public function test_backup_controller() { + // Instantiate non interactive backup_controller + $bc = new mock_backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE, + backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid); + $this->assertTrue($bc instanceof backup_controller); + $this->assertEquals($bc->get_status(), backup::STATUS_AWAITING); + // Instantiate interactive backup_controller + $bc = new mock_backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE, + backup::INTERACTIVE_YES, backup::MODE_GENERAL, $this->userid); + $this->assertTrue($bc instanceof backup_controller); + $this->assertEquals($bc->get_status(), backup::STATUS_SETTING_UI); + $this->assertEquals(strlen($bc->get_backupid()), 32); // is one md5 + + // Save and load one backup controller to check everything is in place + $bc = new mock_backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE, + backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid); + $recid = $bc->save_controller(); + $newbc = mock_backup_controller::load_controller($bc->get_backupid()); + $this->assertTrue($newbc instanceof backup_controller); // This means checksum and load worked ok + } +} + + +/* + * helper extended @backup_controller class that makes some methods public for testing + */ +class mock_backup_controller extends backup_controller { + + public function save_controller() { + parent::save_controller(); + } +} diff --git a/backup/converter/moodle1/tests/lib_test.php b/backup/converter/moodle1/tests/lib_test.php new file mode 100644 index 00000000000..6bc3d15af06 --- /dev/null +++ b/backup/converter/moodle1/tests/lib_test.php @@ -0,0 +1,475 @@ +. + +/** + * Unit tests for the moodle1 converter + * + * @package core_backup + * @subpackage backup-convert + * @category phpunit + * @copyright 2011 Mark Nielsen + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once($CFG->dirroot . '/backup/converter/moodle1/lib.php'); + + +class moodle1_converter_test extends advanced_testcase { + + /** @var string the name of the directory containing the unpacked Moodle 1.9 backup */ + protected $tempdir; + + public function setUp() { + global $CFG; + + $this->tempdir = convert_helper::generate_id('simpletest'); + check_dir_exists("$CFG->tempdir/backup/$this->tempdir/course_files/sub1"); + check_dir_exists("$CFG->tempdir/backup/$this->tempdir/moddata/unittest/4/7"); + copy( + "$CFG->dirroot/backup/converter/moodle1/simpletest/files/moodle.xml", + "$CFG->tempdir/backup/$this->tempdir/moodle.xml" + ); + copy( + "$CFG->dirroot/backup/converter/moodle1/simpletest/files/icon.gif", + "$CFG->tempdir/backup/$this->tempdir/course_files/file1.gif" + ); + copy( + "$CFG->dirroot/backup/converter/moodle1/simpletest/files/icon.gif", + "$CFG->tempdir/backup/$this->tempdir/course_files/sub1/file2.gif" + ); + copy( + "$CFG->dirroot/backup/converter/moodle1/simpletest/files/icon.gif", + "$CFG->tempdir/backup/$this->tempdir/moddata/unittest/4/file1.gif" + ); + copy( + "$CFG->dirroot/backup/converter/moodle1/simpletest/files/icon.gif", + "$CFG->tempdir/backup/$this->tempdir/moddata/unittest/4/icon.gif" + ); + copy( + "$CFG->dirroot/backup/converter/moodle1/simpletest/files/icon.gif", + "$CFG->tempdir/backup/$this->tempdir/moddata/unittest/4/7/icon.gif" + ); + } + + public function tearDown() { + global $CFG; + if (empty($CFG->keeptempdirectoriesonbackup)) { + fulldelete("$CFG->tempdir/backup/$this->tempdir"); + } + } + + public function test_detect_format() { + $detected = moodle1_converter::detect_format($this->tempdir); + $this->assertEquals(backup::FORMAT_MOODLE1, $detected); + } + + public function test_convert_factory() { + $converter = convert_factory::get_converter('moodle1', $this->tempdir); + $this->assertInstanceOf('moodle1_converter', $converter); + } + + public function test_stash_storage_not_created() { + $converter = convert_factory::get_converter('moodle1', $this->tempdir); + $this->setExpectedException('moodle1_convert_storage_exception'); + $converter->set_stash('tempinfo', 12); + } + + public function test_stash_requiring_empty_stash() { + $this->resetAfterTest(true); + $converter = convert_factory::get_converter('moodle1', $this->tempdir); + $converter->create_stash_storage(); + $converter->set_stash('tempinfo', 12); + $this->setExpectedException('moodle1_convert_empty_storage_exception'); + try { + $converter->get_stash('anothertempinfo'); + + } catch (moodle1_convert_empty_storage_exception $e) { + // we must drop the storage here so we are able to re-create it in the next test + $converter->drop_stash_storage(); + throw new moodle1_convert_empty_storage_exception('rethrowing'); + } + } + + public function test_stash_storage() { + $this->resetAfterTest(true); + $converter = convert_factory::get_converter('moodle1', $this->tempdir); + $converter->create_stash_storage(); + + // no implicit stashes + $stashes = $converter->get_stash_names(); + $this->assertEquals(gettype($stashes), 'array'); + $this->assertTrue(empty($stashes)); + + // test stashes without itemid + $converter->set_stash('tempinfo1', 12); + $converter->set_stash('tempinfo2', array('a' => 2, 'b' => 3)); + $stashes = $converter->get_stash_names(); + $this->assertEquals('array', gettype($stashes)); + $this->assertEquals(2, count($stashes)); + $this->assertTrue(in_array('tempinfo1', $stashes)); + $this->assertTrue(in_array('tempinfo2', $stashes)); + $this->assertEquals(12, $converter->get_stash('tempinfo1')); + $this->assertEquals(array('a' => 2, 'b' => 3), $converter->get_stash('tempinfo2')); + + // overwriting a stashed value is allowed + $converter->set_stash('tempinfo1', '13'); + $this->assertNotSame(13, $converter->get_stash('tempinfo1')); + $this->assertSame('13', $converter->get_stash('tempinfo1')); + + // repeated reading is allowed + $this->assertEquals('13', $converter->get_stash('tempinfo1')); + + // storing empty array + $converter->set_stash('empty_array_stash', array()); + $restored = $converter->get_stash('empty_array_stash'); + //$this->assertEquals(gettype($restored), 'array'); // todo return null now, this needs MDL-27713 to be fixed, then uncomment + $this->assertTrue(empty($restored)); + + // test stashes with itemid + $converter->set_stash('tempinfo', 'Hello', 1); + $converter->set_stash('tempinfo', 'World', 2); + $this->assertSame('Hello', $converter->get_stash('tempinfo', 1)); + $this->assertSame('World', $converter->get_stash('tempinfo', 2)); + + // test get_stash_itemids() + $ids = $converter->get_stash_itemids('course_fileref'); + $this->assertEquals(gettype($ids), 'array'); + $this->assertTrue(empty($ids)); + + $converter->set_stash('course_fileref', null, 34); + $converter->set_stash('course_fileref', null, 52); + $ids = $converter->get_stash_itemids('course_fileref'); + $this->assertEquals(2, count($ids)); + $this->assertTrue(in_array(34, $ids)); + $this->assertTrue(in_array(52, $ids)); + + $converter->drop_stash_storage(); + } + + public function test_get_stash_or_default() { + $this->resetAfterTest(true); + $converter = convert_factory::get_converter('moodle1', $this->tempdir); + $converter->create_stash_storage(); + + $this->assertTrue(is_null($converter->get_stash_or_default('stashname'))); + $this->assertTrue(is_null($converter->get_stash_or_default('stashname', 7))); + $this->assertTrue('default' === $converter->get_stash_or_default('stashname', 0, 'default')); + $this->assertTrue(array('foo', 'bar') === $converter->get_stash_or_default('stashname', 42, array('foo', 'bar'))); + + //$converter->set_stash('stashname', 0); + //$this->assertFalse(is_null($converter->get_stash_or_default('stashname'))); // todo returns true now, this needs MDL-27713 to be fixed + + //$converter->set_stash('stashname', ''); + //$this->assertFalse(is_null($converter->get_stash_or_default('stashname'))); // todo returns true now, this needs MDL-27713 to be fixed + + //$converter->set_stash('stashname', array()); + //$this->assertFalse(is_null($converter->get_stash_or_default('stashname'))); // todo returns true now, this needs MDL-27713 to be fixed + + $converter->set_stash('stashname', 42); + $this->assertTrue(42 === $converter->get_stash_or_default('stashname')); + $this->assertTrue(is_null($converter->get_stash_or_default('stashname', 1))); + $this->assertTrue(42 === $converter->get_stash_or_default('stashname', 0, 61)); + + $converter->set_stash('stashname', array(42 => (object)array('id' => 42)), 18); + $stashed = $converter->get_stash_or_default('stashname', 18, 1984); + $this->assertEquals(gettype($stashed), 'array'); + $this->assertTrue(is_object($stashed[42])); + $this->assertTrue($stashed[42]->id === 42); + + $converter->drop_stash_storage(); + } + + public function test_get_contextid() { + $this->resetAfterTest(true); + + $converter = convert_factory::get_converter('moodle1', $this->tempdir); + + // stash storage must be created in advance + $converter->create_stash_storage(); + + // ids are generated on the first call + $id1 = $converter->get_contextid(CONTEXT_BLOCK, 10); + $id2 = $converter->get_contextid(CONTEXT_BLOCK, 11); + $id3 = $converter->get_contextid(CONTEXT_MODULE, 10); + + $this->assertNotEquals($id1, $id2); + $this->assertNotEquals($id1, $id3); + $this->assertNotEquals($id2, $id3); + + // and then re-used if called with the same params + $this->assertEquals($id1, $converter->get_contextid(CONTEXT_BLOCK, 10)); + $this->assertEquals($id2, $converter->get_contextid(CONTEXT_BLOCK, 11)); + $this->assertEquals($id3, $converter->get_contextid(CONTEXT_MODULE, 10)); + + // for system and course level, the instance is irrelevant + // as we need only one system and one course + $id1 = $converter->get_contextid(CONTEXT_COURSE); + $id2 = $converter->get_contextid(CONTEXT_COURSE, 10); + $id3 = $converter->get_contextid(CONTEXT_COURSE, 14); + + $this->assertEquals($id1, $id2); + $this->assertEquals($id1, $id3); + + $id1 = $converter->get_contextid(CONTEXT_SYSTEM); + $id2 = $converter->get_contextid(CONTEXT_SYSTEM, 11); + $id3 = $converter->get_contextid(CONTEXT_SYSTEM, 15); + + $this->assertEquals($id1, $id2); + $this->assertEquals($id1, $id3); + + $converter->drop_stash_storage(); + } + + public function test_get_nextid() { + $this->resetAfterTest(true); + + $converter = convert_factory::get_converter('moodle1', $this->tempdir); + + $id1 = $converter->get_nextid(); + $id2 = $converter->get_nextid(); + $id3 = $converter->get_nextid(); + + $this->assertTrue(0 < $id1); + $this->assertTrue($id1 < $id2); + $this->assertTrue($id2 < $id3); + } + + public function test_migrate_file() { + $this->resetAfterTest(true); + + // set-up the file manager + $converter = convert_factory::get_converter('moodle1', $this->tempdir); + $converter->create_stash_storage(); + $contextid = $converter->get_contextid(CONTEXT_MODULE, 32); + $fileman = $converter->get_file_manager($contextid, 'mod_unittest', 'testarea'); + // this fileman has not converted anything yet + $fileids = $fileman->get_fileids(); + $this->assertEquals(gettype($fileids), 'array'); + $this->assertEquals(0, count($fileids)); + // try to migrate a non-existing directory + $returned = $fileman->migrate_directory('not/existing/directory'); + $this->assertEquals(gettype($returned), 'array'); + $this->assertEquals(0, count($returned)); + $fileids = $fileman->get_fileids(); + $this->assertEquals(gettype($fileids), 'array'); + $this->assertEquals(0, count($fileids)); + // migrate a single file + $fileman->itemid = 4; + $fileman->migrate_file('moddata/unittest/4/icon.gif'); + $this->assertTrue(is_file($converter->get_workdir_path().'/files/4e/4ea114b0558f53e3af8dd9afc0e0810a95c2a724')); + // get the file id + $fileids = $fileman->get_fileids(); + $this->assertEquals(gettype($fileids), 'array'); + $this->assertEquals(1, count($fileids)); + // migrate another single file into another file area + $fileman->filearea = 'anotherarea'; + $fileman->itemid = 7; + $fileman->migrate_file('moddata/unittest/4/7/icon.gif', '/', 'renamed.gif'); + // get the file records + $filerecordids = $converter->get_stash_itemids('files'); + foreach ($filerecordids as $filerecordid) { + $filerecord = $converter->get_stash('files', $filerecordid); + $this->assertEquals('4ea114b0558f53e3af8dd9afc0e0810a95c2a724', $filerecord['contenthash']); + $this->assertEquals($contextid, $filerecord['contextid']); + $this->assertEquals('mod_unittest', $filerecord['component']); + if ($filerecord['filearea'] === 'testarea') { + $this->assertEquals(4, $filerecord['itemid']); + $this->assertEquals('icon.gif', $filerecord['filename']); + } + } + // explicitly clear the list of migrated files + $this->assertTrue(count($fileman->get_fileids()) > 0); + $fileman->reset_fileids(); + $this->assertTrue(count($fileman->get_fileids()) == 0); + $converter->drop_stash_storage(); + } + + public function test_convert_path() { + $path = new convert_path('foo_bar', '/ROOT/THINGS/FOO/BAR'); + $this->assertEquals('foo_bar', $path->get_name()); + $this->assertEquals('/ROOT/THINGS/FOO/BAR', $path->get_path()); + $this->assertEquals('process_foo_bar', $path->get_processing_method()); + $this->assertEquals('on_foo_bar_start', $path->get_start_method()); + $this->assertEquals('on_foo_bar_end', $path->get_end_method()); + } + + public function test_convert_path_implicit_recipes() { + $path = new convert_path('foo_bar', '/ROOT/THINGS/FOO/BAR'); + $data = array( + 'ID' => 76, + 'ELOY' => 'stronk7', + 'MARTIN' => 'moodler', + 'EMPTY' => null, + ); + // apply default recipes (converting keys to lowercase) + $data = $path->apply_recipes($data); + $this->assertEquals(4, count($data)); + $this->assertEquals(76, $data['id']); + $this->assertEquals('stronk7', $data['eloy']); + $this->assertEquals('moodler', $data['martin']); + $this->assertSame(null, $data['empty']); + } + + public function test_convert_path_explicit_recipes() { + $path = new convert_path( + 'foo_bar', '/ROOT/THINGS/FOO/BAR', + array( + 'newfields' => array( + 'david' => 'mudrd8mz', + 'petr' => 'skodak', + ), + 'renamefields' => array( + 'empty' => 'nothing', + ), + 'dropfields' => array( + 'id' + ), + ) + ); + $data = array( + 'ID' => 76, + 'ELOY' => 'stronk7', + 'MARTIN' => 'moodler', + 'EMPTY' => null, + ); + $data = $path->apply_recipes($data); + + $this->assertEquals(5, count($data)); + $this->assertFalse(array_key_exists('id', $data)); + $this->assertEquals('stronk7', $data['eloy']); + $this->assertEquals('moodler', $data['martin']); + $this->assertEquals('mudrd8mz', $data['david']); + $this->assertEquals('skodak', $data['petr']); + $this->assertSame(null, $data['nothing']); + } + + public function test_grouped_data_on_nongrouped_convert_path() { + // prepare some grouped data + $data = array( + 'ID' => 77, + 'NAME' => 'Pale lagers', + 'BEERS' => array( + array( + 'BEER' => array( + 'ID' => 67, + 'NAME' => 'Pilsner Urquell', + ) + ), + array( + 'BEER' => array( + 'ID' => 34, + 'NAME' => 'Heineken', + ) + ), + ) + ); + + // declare a non-grouped path + $path = new convert_path('beer_style', '/ROOT/BEER_STYLES/BEER_STYLE'); + + // an attempt to apply recipes throws exception because we do not expect grouped data + $this->setExpectedException('convert_path_exception'); + $data = $path->apply_recipes($data); + } + + public function test_grouped_convert_path_with_recipes() { + // prepare some grouped data + $data = array( + 'ID' => 77, + 'NAME' => 'Pale lagers', + 'BEERS' => array( + array( + 'BEER' => array( + 'ID' => 67, + 'NAME' => 'Pilsner Urquell', + ) + ), + array( + 'BEER' => array( + 'ID' => 34, + 'NAME' => 'Heineken', + ) + ), + ) + ); + + // implict recipes work for grouped data if the path is declared as grouped + $path = new convert_path('beer_style', '/ROOT/BEER_STYLES/BEER_STYLE', array(), true); + $data = $path->apply_recipes($data); + $this->assertEquals('Heineken', $data['beers'][1]['beer']['name']); + + // an attempt to provide explicit recipes on grouped elements throws exception + $this->setExpectedException('convert_path_exception'); + $path = new convert_path( + 'beer_style', '/ROOT/BEER_STYLES/BEER_STYLE', + array( + 'renamefields' => array( + 'name' => 'beername', // note this is confusing recipe because the 'name' is used for both + // beer-style name ('Pale lagers') and beer name ('Pilsner Urquell') + ) + ), true); + } + + public function test_referenced_course_files() { + + $text = 'This is a text containing links to file.php +as it is parsed from the backup file.

Newsdownload image
+
download manual
'; + + $files = moodle1_converter::find_referenced_files($text); + $this->assertEquals(gettype($files), 'array'); + $this->assertEquals(2, count($files)); + $this->assertTrue(in_array('/pics/news.gif', $files)); + $this->assertTrue(in_array('/MANUAL.DOC', $files)); + + $text = moodle1_converter::rewrite_filephp_usage($text, array('/pics/news.gif', '/another/file/notused.txt'), $files); + $this->assertEquals($text, 'This is a text containing links to file.php +as it is parsed from the backup file.

Newsdownload image
+
download manual
'); + } + + public function test_question_bank_conversion() { + global $CFG; + + $this->resetAfterTest(true); + + copy( + "$CFG->dirroot/backup/converter/moodle1/simpletest/files/questions.xml", + "$CFG->tempdir/backup/$this->tempdir/moodle.xml" + ); + $converter = convert_factory::get_converter('moodle1', $this->tempdir); + $converter->convert(); + } + + public function test_convert_run_convert() { + $this->resetAfterTest(true); + $converter = convert_factory::get_converter('moodle1', $this->tempdir); + $converter->convert(); + } + + public function test_inforef_manager() { + $converter = convert_factory::get_converter('moodle1', $this->tempdir); + $inforef = $converter->get_inforef_manager('unittest'); + $inforef->add_ref('file', 45); + $inforef->add_refs('file', array(46, 47)); + // todo test the write_refs() via some dummy xml_writer + $this->setExpectedException('coding_exception'); + $inforef->add_ref('unknown_referenced_item_name', 76); + } +} diff --git a/backup/util/checks/tests/checks_test.php b/backup/util/checks/tests/checks_test.php new file mode 100644 index 00000000000..15d76e4d6f6 --- /dev/null +++ b/backup/util/checks/tests/checks_test.php @@ -0,0 +1,136 @@ +. + +/** + * @package core_backup + * @category phpunit + * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +// Include all the needed stuff +global $CFG; +require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php'); + + +/* + * check tests (all) + */ +class backup_check_test extends advanced_testcase { + + protected $moduleid; // course_modules id used for testing + protected $sectionid; // course_sections id used for testing + protected $courseid; // course id used for testing + protected $userid; // user record id + + public function setUp() { + global $DB, $CFG; + parent::setUp(); + + $this->resetAfterTest(true); + + $course = $this->getDataGenerator()->create_course(); + $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id), array('section'=>3)); + $coursemodule = $DB->get_record('course_modules', array('id'=>$page->cmid)); + + $this->moduleid = $coursemodule->id; + $this->sectionid = $DB->get_field("course_sections", 'id', array("section"=>$coursemodule->section, "course"=>$course->id)); + $this->courseid = $coursemodule->course; + $this->userid = 2; // admin + + $CFG->backup_error_log_logger_level = backup::LOG_NONE; + $CFG->backup_output_indented_logger_level = backup::LOG_NONE; + $CFG->backup_file_logger_level = backup::LOG_NONE; + $CFG->backup_database_logger_level = backup::LOG_NONE; + unset($CFG->backup_file_logger_extra); + $CFG->backup_file_logger_level_extra = backup::LOG_NONE; + } + + /* + * test backup_check class + */ + public function test_backup_check() { + + // Check against existing course module/section course or fail + $this->assertTrue(backup_check::check_id(backup::TYPE_1ACTIVITY, $this->moduleid)); + $this->assertTrue(backup_check::check_id(backup::TYPE_1SECTION, $this->sectionid)); + $this->assertTrue(backup_check::check_id(backup::TYPE_1COURSE, $this->courseid)); + $this->assertTrue(backup_check::check_user($this->userid)); + + // Check against non-existing course module/section/course (0) + try { + backup_check::check_id(backup::TYPE_1ACTIVITY, 0); + $this->assertTrue(false, 'backup_controller_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof backup_controller_exception); + $this->assertEquals($e->errorcode, 'backup_check_module_not_exists'); + } + try { + backup_check::check_id(backup::TYPE_1SECTION, 0); + $this->assertTrue(false, 'backup_controller_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof backup_controller_exception); + $this->assertEquals($e->errorcode, 'backup_check_section_not_exists'); + } + try { + backup_check::check_id(backup::TYPE_1COURSE, 0); + $this->assertTrue(false, 'backup_controller_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof backup_controller_exception); + $this->assertEquals($e->errorcode, 'backup_check_course_not_exists'); + } + + // Try wrong type + try { + backup_check::check_id(12345678,0); + $this->assertTrue(false, 'backup_controller_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof backup_controller_exception); + $this->assertEquals($e->errorcode, 'backup_check_incorrect_type'); + } + + // Test non-existing user + $userid = 0; + try { + backup_check::check_user($userid); + $this->assertTrue(false, 'backup_controller_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof backup_controller_exception); + $this->assertEquals($e->errorcode, 'backup_check_user_not_exists'); + } + + // Security check tests + // Try to pass wrong controller + try { + backup_check::check_security(new stdclass(), true); + $this->assertTrue(false, 'backup_controller_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof backup_controller_exception); + $this->assertEquals($e->errorcode, 'backup_check_security_requires_backup_controller'); + } + + // Pass correct controller, check must return true in any case with $apply enabled + // and $bc must continue being mock_backup_controller + $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE, + backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid); + $this->assertTrue(backup_check::check_security($bc, true)); + $this->assertTrue($bc instanceof backup_controller); + + } +} diff --git a/backup/util/dbops/tests/dbops_test.php b/backup/util/dbops/tests/dbops_test.php new file mode 100644 index 00000000000..a97aa13fa95 --- /dev/null +++ b/backup/util/dbops/tests/dbops_test.php @@ -0,0 +1,163 @@ +. + +/** + * @package core_backup + * @category phpunit + * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +// Include all the needed stuff +global $CFG; +require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php'); + + +/* + * dbops tests (all) + */ +class backup_dbops_test extends advanced_testcase { + + protected $moduleid; // course_modules id used for testing + protected $sectionid; // course_sections id used for testing + protected $courseid; // course id used for testing + protected $userid; // user record used for testing + + public function setUp() { + global $DB, $CFG; + parent::setUp(); + + $this->resetAfterTest(true); + + $course = $this->getDataGenerator()->create_course(); + $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id), array('section'=>3)); + $coursemodule = $DB->get_record('course_modules', array('id'=>$page->cmid)); + + $this->moduleid = $coursemodule->id; + $this->sectionid = $DB->get_field("course_sections", 'id', array("section"=>$coursemodule->section, "course"=>$course->id)); + $this->courseid = $coursemodule->course; + $this->userid = 2; // admin + + $CFG->backup_error_log_logger_level = backup::LOG_NONE; + $CFG->backup_output_indented_logger_level = backup::LOG_NONE; + $CFG->backup_file_logger_level = backup::LOG_NONE; + $CFG->backup_database_logger_level = backup::LOG_NONE; + unset($CFG->backup_file_logger_extra); + $CFG->backup_file_logger_level_extra = backup::LOG_NONE; + } + + /* + * test backup_ops class + */ + function test_backup_dbops() { + // Nothing to do here, abstract class + exception, will be tested by the rest + } + + /* + * test backup_controller_dbops class + */ + function test_backup_controller_dbops() { + global $DB; + + $dbman = $DB->get_manager(); // Going to use some database_manager services for testing + + // Instantiate non interactive backup_controller + $bc = new mock_backup_controller4dbops(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE, + backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid); + $this->assertTrue($bc instanceof backup_controller); + // Calculate checksum + $checksum = $bc->calculate_checksum(); + $this->assertEquals(strlen($checksum), 32); // is one md5 + + // save controller + $recid = backup_controller_dbops::save_controller($bc, $checksum); + $this->assertNotEmpty($recid); + // save it again (should cause update to happen) + $recid2 = backup_controller_dbops::save_controller($bc, $checksum); + $this->assertNotEmpty($recid2); + $this->assertEquals($recid, $recid2); // Same record in both save operations + + // Try incorrect checksum + $bc = new mock_backup_controller4dbops(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE, + backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid); + $checksum = $bc->calculate_checksum(); + try { + $recid = backup_controller_dbops::save_controller($bc, 'lalala'); + $this->assertTrue(false, 'backup_dbops_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof backup_dbops_exception); + $this->assertEquals($e->errorcode, 'backup_controller_dbops_saving_checksum_mismatch'); + } + + // Try to save non backup_controller object + $bc = new stdclass(); + try { + $recid = backup_controller_dbops::save_controller($bc, 'lalala'); + $this->assertTrue(false, 'backup_controller_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof backup_controller_exception); + $this->assertEquals($e->errorcode, 'backup_controller_expected'); + } + + // save and load controller (by backupid). Then compare + $bc = new mock_backup_controller4dbops(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE, + backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid); + $checksum = $bc->calculate_checksum(); // Calculate checksum + $backupid = $bc->get_backupid(); + $this->assertEquals(strlen($backupid), 32); // is one md5 + $recid = backup_controller_dbops::save_controller($bc, $checksum); // save controller + $newbc = backup_controller_dbops::load_controller($backupid); // load controller + $this->assertTrue($newbc instanceof backup_controller); + $newchecksum = $newbc->calculate_checksum(); + $this->assertEquals($newchecksum, $checksum); + + // try to load non-existing controller + try { + $bc = backup_controller_dbops::load_controller('1234567890'); + $this->assertTrue(false, 'backup_dbops_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof backup_dbops_exception); + $this->assertEquals($e->errorcode, 'backup_controller_dbops_nonexisting'); + } + + // backup_ids_temp table tests + // If, for any reason table exists, drop it + if ($dbman->table_exists('backup_ids_temp')) { + $dbman->drop_temp_table(new xmldb_table('backup_ids_temp')); + } + // Check backup_ids_temp table doesn't exist + $this->assertFalse($dbman->table_exists('backup_ids_temp')); + // Create and check it exists + backup_controller_dbops::create_backup_ids_temp_table('testingid'); + $this->assertTrue($dbman->table_exists('backup_ids_temp')); + // Drop and check it doesn't exists anymore + backup_controller_dbops::drop_backup_ids_temp_table('testingid'); + $this->assertFalse($dbman->table_exists('backup_ids_temp')); + } +} + +class mock_backup_controller4dbops extends backup_controller { + + /** + * Change standard behavior so the checksum is also stored and not onlt calculated + */ + public function calculate_checksum() { + $this->checksum = parent::calculate_checksum(); + return $this->checksum; + } +} diff --git a/backup/util/destinations/tests/destinations_test.php b/backup/util/destinations/tests/destinations_test.php new file mode 100644 index 00000000000..d0f88ecd856 --- /dev/null +++ b/backup/util/destinations/tests/destinations_test.php @@ -0,0 +1,47 @@ +. + +/** + * @package core_backup + * @category phpunit + * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +// Include all the needed stuff +global $CFG; +//require_once($CFG->dirroot . '/backup/util/helper/backup_helper.class.php'); + +/** + * dbops tests (all) + */ +class backup_destinations_test extends basic_testcase { + + /** + * test backup_destination class + */ + function test_backup_destination() { + } + + /** + * test backup_destination_osfs class + */ + function test_backup_destination_osfs() { + } +} + diff --git a/backup/util/factories/tests/factories_test.php b/backup/util/factories/tests/factories_test.php new file mode 100644 index 00000000000..f730ef2eda3 --- /dev/null +++ b/backup/util/factories/tests/factories_test.php @@ -0,0 +1,126 @@ +. + +/** + * @package core_backup + * @category phpunit + * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +// Include all the needed stuff +global $CFG; +require_once($CFG->dirroot . '/backup/util/interfaces/checksumable.class.php'); +require_once($CFG->dirroot . '/backup/backup.class.php'); +require_once($CFG->dirroot . '/backup/util/loggers/base_logger.class.php'); +require_once($CFG->dirroot . '/backup/util/loggers/error_log_logger.class.php'); +require_once($CFG->dirroot . '/backup/util/loggers/output_indented_logger.class.php'); +require_once($CFG->dirroot . '/backup/util/loggers/database_logger.class.php'); +require_once($CFG->dirroot . '/backup/util/loggers/file_logger.class.php'); +require_once($CFG->dirroot . '/backup/util/factories/backup_factory.class.php'); + + +/** + * backup_factory tests (all) + */ +class backup_factories_test extends advanced_testcase { + + function setUp() { + global $CFG; + parent::setUp(); + + $this->resetAfterTest(true); + + $CFG->backup_error_log_logger_level = backup::LOG_NONE; + $CFG->backup_output_indented_logger_level = backup::LOG_NONE; + $CFG->backup_file_logger_level = backup::LOG_NONE; + $CFG->backup_database_logger_level = backup::LOG_NONE; + unset($CFG->backup_file_logger_extra); + $CFG->backup_file_logger_level_extra = backup::LOG_NONE; + } + + /** + * test get_logger_chain() method + */ + function test_backup_factory() { + global $CFG; + + // Default instantiate, all levels = backup::LOG_NONE + // With debugdisplay enabled + $CFG->debugdisplay = true; + $logger1 = backup_factory::get_logger_chain(backup::INTERACTIVE_YES, backup::EXECUTION_INMEDIATE, 'test'); + $this->assertTrue($logger1 instanceof error_log_logger); // 1st logger is error_log_logger + $this->assertEquals($logger1->get_level(), backup::LOG_NONE); + $logger2 = $logger1->get_next(); + $this->assertTrue($logger2 instanceof output_indented_logger); // 2nd logger is output_indented_logger + $this->assertEquals($logger2->get_level(), backup::LOG_NONE); + $logger3 = $logger2->get_next(); + $this->assertTrue($logger3 instanceof file_logger); // 3rd logger is file_logger + $this->assertEquals($logger3->get_level(), backup::LOG_NONE); + $logger4 = $logger3->get_next(); + $this->assertTrue($logger4 instanceof database_logger); // 4th logger is database_logger + $this->assertEquals($logger4->get_level(), backup::LOG_NONE); + $logger5 = $logger4->get_next(); + $this->assertTrue($logger5 === null); + + // With debugdisplay disabled + $CFG->debugdisplay = false; + $logger1 = backup_factory::get_logger_chain(backup::INTERACTIVE_YES, backup::EXECUTION_INMEDIATE, 'test'); + $this->assertTrue($logger1 instanceof error_log_logger); // 1st logger is error_log_logger + $this->assertEquals($logger1->get_level(), backup::LOG_NONE); + $logger2 = $logger1->get_next(); + $this->assertTrue($logger2 instanceof file_logger); // 2nd logger is file_logger + $this->assertEquals($logger2->get_level(), backup::LOG_NONE); + $logger3 = $logger2->get_next(); + $this->assertTrue($logger3 instanceof database_logger); // 3rd logger is database_logger + $this->assertEquals($logger3->get_level(), backup::LOG_NONE); + $logger4 = $logger3->get_next(); + $this->assertTrue($logger4 === null); + + // Instantiate with debugging enabled and $CFG->backup_error_log_logger_level not set + $CFG->debugdisplay = true; + $CFG->debug = DEBUG_DEVELOPER; + unset($CFG->backup_error_log_logger_level); + $logger1 = backup_factory::get_logger_chain(backup::INTERACTIVE_YES, backup::EXECUTION_INMEDIATE, 'test'); + $this->assertTrue($logger1 instanceof error_log_logger); // 1st logger is error_log_logger + $this->assertEquals($logger1->get_level(), backup::LOG_DEBUG); // and must have backup::LOG_DEBUG level + // Set $CFG->backup_error_log_logger_level to backup::LOG_WARNING and test again + $CFG->backup_error_log_logger_level = backup::LOG_WARNING; + $logger1 = backup_factory::get_logger_chain(backup::INTERACTIVE_YES, backup::EXECUTION_INMEDIATE, 'test'); + $this->assertTrue($logger1 instanceof error_log_logger); // 1st logger is error_log_logger + $this->assertEquals($logger1->get_level(), backup::LOG_WARNING); // and must have backup::LOG_WARNING level + + // Instantiate in non-interactive mode, output_indented_logger must be out + $logger1 = backup_factory::get_logger_chain(backup::INTERACTIVE_NO, backup::EXECUTION_INMEDIATE, 'test'); + $logger2 = $logger1->get_next(); + $this->assertTrue($logger2 instanceof file_logger); // 2nd logger is file_logger (output_indented_logger skiped) + + // Define extra file logger and instantiate, should be 5th and last logger + $CFG->backup_file_logger_extra = '/tmp/test.html'; + $CFG->backup_file_logger_level_extra = backup::LOG_NONE; + $logger1 = backup_factory::get_logger_chain(backup::INTERACTIVE_YES, backup::EXECUTION_INMEDIATE, 'test'); + $logger2 = $logger1->get_next(); + $logger3 = $logger2->get_next(); + $logger4 = $logger3->get_next(); + $logger5 = $logger4->get_next(); + $this->assertTrue($logger5 instanceof file_logger); // 5rd logger is file_logger (extra) + $this->assertEquals($logger3->get_level(), backup::LOG_NONE); + $logger6 = $logger5->get_next(); + $this->assertTrue($logger6 === null); + } +} diff --git a/backup/util/helper/tests/converterhelper_test.php b/backup/util/helper/tests/converterhelper_test.php new file mode 100644 index 00000000000..d03431588c4 --- /dev/null +++ b/backup/util/helper/tests/converterhelper_test.php @@ -0,0 +1,145 @@ +. + +/** + * @package core_backup + * @category phpunit + * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +// Include all the needed stuff +global $CFG; +require_once($CFG->dirroot . '/backup/util/helper/convert_helper.class.php'); + + +/** + * Provides access to the protected methods we need to test + */ +class testable_convert_helper extends convert_helper { + + public static function choose_conversion_path($format, array $descriptions) { + return parent::choose_conversion_path($format, $descriptions); + } +} + + +/** + * Defines the test methods + */ +class convert_helper_test extends basic_testcase { + + public function test_choose_conversion_path() { + + // no converters available + $descriptions = array(); + $path = testable_convert_helper::choose_conversion_path(backup::FORMAT_MOODLE1, $descriptions); + $this->assertEquals($path, array()); + + // missing source and/or targets + $descriptions = array( + // some custom converter + 'exporter' => array( + 'from' => backup::FORMAT_MOODLE1, + 'to' => 'some_custom_format', + 'cost' => 10, + ), + // another custom converter + 'converter' => array( + 'from' => 'yet_another_crazy_custom_format', + 'to' => backup::FORMAT_MOODLE, + 'cost' => 10, + ), + ); + $path = testable_convert_helper::choose_conversion_path(backup::FORMAT_MOODLE1, $descriptions); + $this->assertEquals($path, array()); + + $path = testable_convert_helper::choose_conversion_path('some_other_custom_format', $descriptions); + $this->assertEquals($path, array()); + + // single step conversion + $path = testable_convert_helper::choose_conversion_path('yet_another_crazy_custom_format', $descriptions); + $this->assertEquals($path, array('converter')); + + // no conversion needed - this is supposed to be detected by the caller + $path = testable_convert_helper::choose_conversion_path(backup::FORMAT_MOODLE, $descriptions); + $this->assertEquals($path, array()); + + // two alternatives + $descriptions = array( + // standard moodle 1.9 -> 2.x converter + 'moodle1' => array( + 'from' => backup::FORMAT_MOODLE1, + 'to' => backup::FORMAT_MOODLE, + 'cost' => 10, + ), + // alternative moodle 1.9 -> 2.x converter + 'alternative' => array( + 'from' => backup::FORMAT_MOODLE1, + 'to' => backup::FORMAT_MOODLE, + 'cost' => 8, + ) + ); + $path = testable_convert_helper::choose_conversion_path(backup::FORMAT_MOODLE1, $descriptions); + $this->assertEquals($path, array('alternative')); + + // complex case + $descriptions = array( + // standard moodle 1.9 -> 2.x converter + 'moodle1' => array( + 'from' => backup::FORMAT_MOODLE1, + 'to' => backup::FORMAT_MOODLE, + 'cost' => 10, + ), + // alternative moodle 1.9 -> 2.x converter + 'alternative' => array( + 'from' => backup::FORMAT_MOODLE1, + 'to' => backup::FORMAT_MOODLE, + 'cost' => 8, + ), + // custom converter from 1.9 -> custom 'CFv1' format + 'cc1' => array( + 'from' => backup::FORMAT_MOODLE1, + 'to' => 'CFv1', + 'cost' => 2, + ), + // custom converter from custom 'CFv1' format -> moodle 2.0 format + 'cc2' => array( + 'from' => 'CFv1', + 'to' => backup::FORMAT_MOODLE, + 'cost' => 5, + ), + // custom converter from CFv1 -> CFv2 format + 'cc3' => array( + 'from' => 'CFv1', + 'to' => 'CFv2', + 'cost' => 2, + ), + // custom converter from CFv2 -> moodle 2.0 format + 'cc4' => array( + 'from' => 'CFv2', + 'to' => backup::FORMAT_MOODLE, + 'cost' => 2, + ), + ); + + // ask the helper to find the most effective way + $path = testable_convert_helper::choose_conversion_path(backup::FORMAT_MOODLE1, $descriptions); + $this->assertEquals($path, array('cc1', 'cc3', 'cc4')); + } +} diff --git a/backup/util/helper/tests/decode_test.php b/backup/util/helper/tests/decode_test.php new file mode 100644 index 00000000000..75c913085d2 --- /dev/null +++ b/backup/util/helper/tests/decode_test.php @@ -0,0 +1,190 @@ +. + +/** + * @package core_backup + * @category phpunit + * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +// Include all the needed stuff +global $CFG; +require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php'); + + +/** + * restore_decode tests (both rule and content) + */ +class restore_decode_test extends basic_testcase { + + /** + * test restore_decode_rule class + */ + function test_restore_decode_rule() { + + // Test various incorrect constructors + try { + $dr = new restore_decode_rule('28 HJH', '/index.php', array()); + $this->assertTrue(false, 'restore_decode_rule_exception exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof restore_decode_rule_exception); + $this->assertEquals($e->errorcode, 'decode_rule_incorrect_name'); + $this->assertEquals($e->a, '28 HJH'); + } + + try { + $dr = new restore_decode_rule('HJHJhH', '/index.php', array()); + $this->assertTrue(false, 'restore_decode_rule_exception exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof restore_decode_rule_exception); + $this->assertEquals($e->errorcode, 'decode_rule_incorrect_name'); + $this->assertEquals($e->a, 'HJHJhH'); + } + + try { + $dr = new restore_decode_rule('', '/index.php', array()); + $this->assertTrue(false, 'restore_decode_rule_exception exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof restore_decode_rule_exception); + $this->assertEquals($e->errorcode, 'decode_rule_incorrect_name'); + $this->assertEquals($e->a, ''); + } + + try { + $dr = new restore_decode_rule('TESTRULE', 'index.php', array()); + $this->assertTrue(false, 'restore_decode_rule_exception exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof restore_decode_rule_exception); + $this->assertEquals($e->errorcode, 'decode_rule_incorrect_urltemplate'); + $this->assertEquals($e->a, 'index.php'); + } + + try { + $dr = new restore_decode_rule('TESTRULE', '', array()); + $this->assertTrue(false, 'restore_decode_rule_exception exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof restore_decode_rule_exception); + $this->assertEquals($e->errorcode, 'decode_rule_incorrect_urltemplate'); + $this->assertEquals($e->a, ''); + } + + try { + $dr = new restore_decode_rule('TESTRULE', '/course/view.php?id=$1&c=$2$3', array('test1', 'test2')); + $this->assertTrue(false, 'restore_decode_rule_exception exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof restore_decode_rule_exception); + $this->assertEquals($e->errorcode, 'decode_rule_mappings_incorrect_count'); + $this->assertEquals($e->a->placeholders, 3); + $this->assertEquals($e->a->mappings, 2); + } + + try { + $dr = new restore_decode_rule('TESTRULE', '/course/view.php?id=$5&c=$4$1', array('test1', 'test2', 'test3')); + $this->assertTrue(false, 'restore_decode_rule_exception exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof restore_decode_rule_exception); + $this->assertEquals($e->errorcode, 'decode_rule_nonconsecutive_placeholders'); + $this->assertEquals($e->a, '1, 4, 5'); + } + + try { + $dr = new restore_decode_rule('TESTRULE', '/course/view.php?id=$0&c=$3$2', array('test1', 'test2', 'test3')); + $this->assertTrue(false, 'restore_decode_rule_exception exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof restore_decode_rule_exception); + $this->assertEquals($e->errorcode, 'decode_rule_nonconsecutive_placeholders'); + $this->assertEquals($e->a, '0, 2, 3'); + } + + try { + $dr = new restore_decode_rule('TESTRULE', '/course/view.php?id=$1&c=$3$3', array('test1', 'test2', 'test3')); + $this->assertTrue(false, 'restore_decode_rule_exception exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof restore_decode_rule_exception); + $this->assertEquals($e->errorcode, 'decode_rule_duplicate_placeholders'); + $this->assertEquals($e->a, '1, 3, 3'); + } + + // Provide some example content and test the regexp is calculated ok + $content = '$@TESTRULE*22*33*44@$'; + $linkname = 'TESTRULE'; + $urltemplate= '/course/view.php?id=$1&c=$3$2'; + $mappings = array('test1', 'test2', 'test3'); + $result = '1/course/view.php?id=44&c=8866'; + $dr = new mock_restore_decode_rule($linkname, $urltemplate, $mappings); + $this->assertEquals($dr->decode($content), $result); + + $content = '$@TESTRULE*22*33*44@$ñ$@TESTRULE*22*33*44@$'; + $linkname = 'TESTRULE'; + $urltemplate= '/course/view.php?id=$1&c=$3$2'; + $mappings = array('test1', 'test2', 'test3'); + $result = '1/course/view.php?id=44&c=8866ñ1/course/view.php?id=44&c=8866'; + $dr = new mock_restore_decode_rule($linkname, $urltemplate, $mappings); + $this->assertEquals($dr->decode($content), $result); + + $content = 'ñ$@TESTRULE*22*0*44@$ñ$@TESTRULE*22*33*44@$ñ'; + $linkname = 'TESTRULE'; + $urltemplate= '/course/view.php?id=$1&c=$3$2'; + $mappings = array('test1', 'test2', 'test3'); + $result = 'ñ0/course/view.php?id=22&c=440ñ1/course/view.php?id=44&c=8866ñ'; + $dr = new mock_restore_decode_rule($linkname, $urltemplate, $mappings); + $this->assertEquals($dr->decode($content), $result); + } + + /** + * test restore_decode_content class + */ + function test_restore_decode_content() { + // TODO: restore_decode_content tests + } + + /** + * test restore_decode_processor class + */ + function test_restore_decode_processor() { + // TODO: restore_decode_processor tests + } +} + +/** + * Mockup restore_decode_rule for testing purposes + */ +class mock_restore_decode_rule extends restore_decode_rule { + + /** + * Originally protected, make it public + */ + public function get_calculated_regexp() { + return parent::get_calculated_regexp(); + } + + /** + * Simply map each itemid by its double + */ + protected function get_mapping($itemname, $itemid) { + return $itemid * 2; + } + + /** + * Simply prefix with '0' non-mapped results and with '1' mapped ones + */ + protected function apply_modifications($toreplace, $mappingsok) { + return ($mappingsok ? '1' : '0') . $toreplace; + } +} diff --git a/backup/util/helper/tests/helper_test.php b/backup/util/helper/tests/helper_test.php new file mode 100644 index 00000000000..fdac0875fd8 --- /dev/null +++ b/backup/util/helper/tests/helper_test.php @@ -0,0 +1,49 @@ +. + +/** + * @package core_backup + * @category phpunit + * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +// Include all the needed stuff +global $CFG; +require_once($CFG->dirroot . '/backup/util/interfaces/checksumable.class.php'); +require_once($CFG->dirroot . '/backup/backup.class.php'); +require_once($CFG->dirroot . '/backup/util/helper/backup_helper.class.php'); +require_once($CFG->dirroot . '/backup/util/helper/backup_general_helper.class.php'); + +/* + * backup_helper tests (all) + */ +class backup_helper_test extends basic_testcase { + + /* + * test backup_helper class + */ + function test_backup_helper() { + } + + /* + * test backup_general_helper class + */ + function test_backup_general_helper() { + } +} diff --git a/backup/util/loggers/error_log_logger.class.php b/backup/util/loggers/error_log_logger.class.php index a303cd11087..899e9513ad1 100644 --- a/backup/util/loggers/error_log_logger.class.php +++ b/backup/util/loggers/error_log_logger.class.php @@ -32,6 +32,10 @@ class error_log_logger extends base_logger { // Protected API starts here protected function action($message, $level, $options = null) { + if (PHPUNIT_TEST) { + // no logging from PHPUnit, it is admins fault if it does not work!!! + return true; + } return error_log($message); } } diff --git a/backup/util/loggers/tests/logger_test.php b/backup/util/loggers/tests/logger_test.php new file mode 100644 index 00000000000..0907b9e424e --- /dev/null +++ b/backup/util/loggers/tests/logger_test.php @@ -0,0 +1,388 @@ +. + +/** + * @package core_backup + * @category phpunit + * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +// Include all the needed stuff +global $CFG; +require_once($CFG->dirroot . '/backup/util/interfaces/checksumable.class.php'); +require_once($CFG->dirroot . '/backup/backup.class.php'); +require_once($CFG->dirroot . '/backup/util/loggers/base_logger.class.php'); +require_once($CFG->dirroot . '/backup/util/loggers/error_log_logger.class.php'); +require_once($CFG->dirroot . '/backup/util/loggers/output_text_logger.class.php'); +require_once($CFG->dirroot . '/backup/util/loggers/output_indented_logger.class.php'); +require_once($CFG->dirroot . '/backup/util/loggers/database_logger.class.php'); +require_once($CFG->dirroot . '/backup/util/loggers/file_logger.class.php'); + +/** + * logger tests (all) + */ +class logger_test extends basic_testcase { + + /** + * test base_logger class + */ + function test_base_logger() { + // Test logger with simple action (message * level) + $lo = new mock_base_logger1(backup::LOG_ERROR); + $msg = 13; + $this->assertEquals($lo->process($msg, backup::LOG_ERROR), $msg * backup::LOG_ERROR); + // With lowest level must return true + $lo = new mock_base_logger1(backup::LOG_ERROR); + $msg = 13; + $this->assertTrue($lo->process($msg, backup::LOG_DEBUG)); + + // Chain 2 loggers, we must get as result the result of the inner one + $lo1 = new mock_base_logger1(backup::LOG_ERROR); + $lo2 = new mock_base_logger2(backup::LOG_ERROR); + $lo1->set_next($lo2); + $msg = 13; + $this->assertEquals($lo1->process($msg, backup::LOG_ERROR), $msg + backup::LOG_ERROR); + + // Try circular reference + $lo1 = new mock_base_logger1(backup::LOG_ERROR); + try { + $lo1->set_next($lo1); //self + $this->assertTrue(false, 'base_logger_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof base_logger_exception); + $this->assertEquals($e->errorcode, 'logger_circular_reference'); + $this->assertTrue($e->a instanceof stdclass); + $this->assertEquals($e->a->main, get_class($lo1)); + $this->assertEquals($e->a->alreadyinchain, get_class($lo1)); + } + + $lo1 = new mock_base_logger1(backup::LOG_ERROR); + $lo2 = new mock_base_logger2(backup::LOG_ERROR); + $lo3 = new mock_base_logger3(backup::LOG_ERROR); + $lo1->set_next($lo2); + $lo2->set_next($lo3); + try { + $lo3->set_next($lo1); + $this->assertTrue(false, 'base_logger_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof base_logger_exception); + $this->assertEquals($e->errorcode, 'logger_circular_reference'); + $this->assertTrue($e->a instanceof stdclass); + $this->assertEquals($e->a->main, get_class($lo1)); + $this->assertEquals($e->a->alreadyinchain, get_class($lo3)); + } + + // Test stopper logger + $lo1 = new mock_base_logger1(backup::LOG_ERROR); + $lo2 = new mock_base_logger2(backup::LOG_ERROR); + $lo3 = new mock_base_logger3(backup::LOG_ERROR); + $lo1->set_next($lo2); + $lo2->set_next($lo3); + $this->assertFalse($lo1->process('test', backup::LOG_ERROR)); + + // Test checksum correct + $lo1 = new mock_base_logger1(backup::LOG_ERROR); + $lo1->is_checksum_correct(get_class($lo1) . '-' . backup::LOG_ERROR); + + // Test get_levelstr() + $lo1 = new mock_base_logger1(backup::LOG_ERROR); + $this->assertEquals($lo1->get_levelstr(backup::LOG_NONE), 'undefined'); + $this->assertEquals($lo1->get_levelstr(backup::LOG_ERROR), 'error'); + $this->assertEquals($lo1->get_levelstr(backup::LOG_WARNING), 'warn'); + $this->assertEquals($lo1->get_levelstr(backup::LOG_INFO), 'info'); + $this->assertEquals($lo1->get_levelstr(backup::LOG_DEBUG), 'debug'); + } + + /** + * test error_log_logger class + */ + function test_error_log_logger() { + // Not much really to test, just instantiate and execute, should return true + $lo = new error_log_logger(backup::LOG_ERROR); + $this->assertTrue($lo instanceof error_log_logger); + $message = 'This log exists because you have run Moodle unit tests: Ignore it'; + $result = $lo->process($message, backup::LOG_ERROR); + $this->assertTrue($result); + } + + /** + * test output_text_logger class + */ + function test_output_text_logger() { + // Instantiate without date nor level output + $lo = new output_text_logger(backup::LOG_ERROR); + $this->assertTrue($lo instanceof output_text_logger); + $message = 'testing output_text_logger'; + ob_start(); // Capture output + $result = $lo->process($message, backup::LOG_ERROR); + $contents = ob_get_contents(); + ob_end_clean(); // End capture and discard + $this->assertTrue($result); + $this->assertTrue(strpos($contents, $message) !== false); + + // Instantiate with date and level output + $lo = new output_text_logger(backup::LOG_ERROR, true, true); + $this->assertTrue($lo instanceof output_text_logger); + $message = 'testing output_text_logger'; + ob_start(); // Capture output + $result = $lo->process($message, backup::LOG_ERROR); + $contents = ob_get_contents(); + ob_end_clean(); // End capture and discard + $this->assertTrue($result); + $this->assertTrue(strpos($contents,'[') === 0); + $this->assertTrue(strpos($contents,'[error]') !== false); + $this->assertTrue(strpos($contents, $message) !== false); + $this->assertTrue(substr_count($contents , '] ') >= 2); + } + + /** + * test output_indented_logger class + */ + function test_output_indented_logger() { + // Instantiate without date nor level output + $options = array('depth' => 2); + $lo = new output_indented_logger(backup::LOG_ERROR); + $this->assertTrue($lo instanceof output_indented_logger); + $message = 'testing output_indented_logger'; + ob_start(); // Capture output + $result = $lo->process($message, backup::LOG_ERROR, $options); + $contents = ob_get_contents(); + ob_end_clean(); // End capture and discard + $this->assertTrue($result); + if (defined('STDOUT')) { + $check = ' '; + } else { + $check = '  '; + } + $this->assertTrue(strpos($contents, str_repeat($check, $options['depth']) . $message) !== false); + + // Instantiate with date and level output + $options = array('depth' => 3); + $lo = new output_indented_logger(backup::LOG_ERROR, true, true); + $this->assertTrue($lo instanceof output_indented_logger); + $message = 'testing output_indented_logger'; + ob_start(); // Capture output + $result = $lo->process($message, backup::LOG_ERROR, $options); + $contents = ob_get_contents(); + ob_end_clean(); // End capture and discard + $this->assertTrue($result); + $this->assertTrue(strpos($contents,'[') === 0); + $this->assertTrue(strpos($contents,'[error]') !== false); + $this->assertTrue(strpos($contents, $message) !== false); + $this->assertTrue(substr_count($contents , '] ') >= 2); + if (defined('STDOUT')) { + $check = ' '; + } else { + $check = '  '; + } + $this->assertTrue(strpos($contents, str_repeat($check, $options['depth']) . $message) !== false); + } + + /** + * test database_logger class + */ + function test_database_logger() { + // Instantiate with date and level output (and with specs from the global moodle "log" table so checks will pass + $now = time(); + $datecol = 'time'; + $levelcol = 'action'; + $messagecol = 'info'; + $logtable = 'log'; + $columns = array('url' => 'http://127.0.0.1'); + $loglevel = backup::LOG_ERROR; + $lo = new mock_database_logger(backup::LOG_ERROR, $datecol, $levelcol, $messagecol, $logtable, $columns); + $this->assertTrue($lo instanceof database_logger); + $message = 'testing database_logger'; + $result = $lo->process($message, $loglevel); + // Check everything is ready to be inserted to DB + $this->assertEquals($result['table'], $logtable); + $this->assertTrue($result['columns'][$datecol] >= $now); + $this->assertEquals($result['columns'][$levelcol], $loglevel); + $this->assertEquals($result['columns'][$messagecol], $message); + $this->assertEquals($result['columns']['url'], $columns['url']); + } + + /** + * test file_logger class + */ + function test_file_logger() { + global $CFG; + + $file = $CFG->tempdir . '/test/test_file_logger.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)); + } + + // Instantiate with date and level output, and also use the depth option + $options = array('depth' => 3); + $lo1 = new file_logger(backup::LOG_ERROR, true, true, $file); + $this->assertTrue($lo1 instanceof file_logger); + $message1 = 'testing file_logger'; + $result = $lo1->process($message1, backup::LOG_ERROR, $options); + $this->assertTrue($result); + + // Another file_logger is going towrite there too without closing + $options = array(); + $lo2 = new file_logger(backup::LOG_WARNING, true, true, $file); + $this->assertTrue($lo2 instanceof file_logger); + $message2 = 'testing file_logger2'; + $result = $lo2->process($message2, backup::LOG_WARNING, $options); + $this->assertTrue($result); + + // Destruct loggers + $lo1 = null; + $lo2 = null; + + // Load file results to analyze them + $fcontents = file_get_contents($file); + $acontents = explode(PHP_EOL, $fcontents); // Split by line + $this->assertTrue(strpos($acontents[0], $message1) !== false); + $this->assertTrue(strpos($acontents[0], '[error]') !== false); + $this->assertTrue(strpos($acontents[0], ' ') !== false); + $this->assertTrue(substr_count($acontents[0] , '] ') >= 2); + $this->assertTrue(strpos($acontents[1], $message2) !== false); + $this->assertTrue(strpos($acontents[1], '[warn]') !== false); + $this->assertTrue(strpos($acontents[1], ' ') === false); + $this->assertTrue(substr_count($acontents[1] , '] ') >= 2); + unlink($file); // delete file + + // Try one html file + $file = $CFG->tempdir . '/test/test_file_logger.html'; + $options = array('depth' => 1); + $lo = new file_logger(backup::LOG_ERROR, true, true, $file); + $this->assertTrue($lo instanceof file_logger); + $this->assertTrue(file_exists($file)); + $message = 'testing file_logger'; + $result = $lo->process($message, backup::LOG_ERROR, $options); + // Get file contents and inspect them + $fcontents = file_get_contents($file); + $this->assertTrue($result); + $this->assertTrue(strpos($fcontents, $message) !== false); + $this->assertTrue(strpos($fcontents, '[error]') !== false); + $this->assertTrue(strpos($fcontents, '  ') !== false); + $this->assertTrue(substr_count($fcontents , '] ') >= 2); + unlink($file); // delete file + + // Instantiate, write something, force deletion, try to write again + $file = $CFG->tempdir . '/test/test_file_logger.html'; + $lo = new mock_file_logger(backup::LOG_ERROR, true, true, $file); + $this->assertTrue(file_exists($file)); + $message = 'testing file_logger'; + $result = $lo->process($message, backup::LOG_ERROR); + fclose($lo->get_fhandle()); // close file + try { + $result = @$lo->process($message, backup::LOG_ERROR); // Try to write again + $this->assertTrue(false, 'base_logger_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof base_logger_exception); + $this->assertEquals($e->errorcode, 'error_writing_file'); + } + + // Instantiate without file + try { + $lo = new file_logger(backup::LOG_WARNING, true, true, ''); + $this->assertTrue(false, 'base_logger_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof base_logger_exception); + $this->assertEquals($e->errorcode, 'missing_fullpath_parameter'); + } + + // Instantiate in (near) impossible path + $file = $CFG->tempdir . '/test_azby/test_file_logger.txt'; + try { + $lo = new file_logger(backup::LOG_WARNING, true, true, $file); + $this->assertTrue(false, 'base_logger_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof base_logger_exception); + $this->assertEquals($e->errorcode, 'file_not_writable'); + $this->assertEquals($e->a, $file); + } + + // Instantiate one file logger with level = backup::LOG_NONE + $file = $CFG->tempdir . '/test/test_file_logger.txt'; + $lo = new file_logger(backup::LOG_NONE, true, true, $file); + $this->assertTrue($lo instanceof file_logger); + $this->assertFalse(file_exists($file)); + + // Remove the test dir and any content + @remove_dir(dirname($file)); + } +} + + +/** + * helper extended base_logger class that implements some methods for testing + * Simply return the product of message and level + */ +class mock_base_logger1 extends base_logger { + + protected function action($message, $level, $options = null) { + return $message * $level; // Simply return that, for testing + } + public function get_levelstr($level) { + return parent::get_levelstr($level); + } +} + +/** + * helper extended base_logger class that implements some methods for testing + * Simply return the sum of message and level + */ +class mock_base_logger2 extends base_logger { + + protected function action($message, $level, $options = null) { + return $message + $level; // Simply return that, for testing + } +} + +/** + * helper extended base_logger class that implements some methods for testing + * Simply return 8 + */ +class mock_base_logger3 extends base_logger { + + protected function action($message, $level, $options = null) { + return false; // Simply return false, for testing stopper + } +} + +/** + * helper extended database_logger class that implements some methods for testing + * Returns the complete info that normally will be used by insert record calls + */ +class mock_database_logger extends database_logger { + + protected function insert_log_record($table, $columns) { + return array('table' => $table, 'columns' => $columns); + } +} + +/** + * helper extended file_logger class that implements some methods for testing + * Returns the, usually protected, handle + */ +class mock_file_logger extends file_logger { + + function get_fhandle() { + return $this->fhandle; + } +} diff --git a/backup/util/plan/tests/plan_test.php b/backup/util/plan/tests/plan_test.php new file mode 100644 index 00000000000..0683530fd86 --- /dev/null +++ b/backup/util/plan/tests/plan_test.php @@ -0,0 +1,158 @@ +. + +/** + * @package core_backup + * @category phpunit + * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +// Include all the needed stuff +global $CFG; +require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php'); + + +/** + * plan tests (all) + */ +class backup_plan_test extends advanced_testcase { + + protected $moduleid; // course_modules id used for testing + protected $sectionid; // course_sections id used for testing + protected $courseid; // course id used for testing + protected $userid; // user record used for testing + + public function setUp() { + global $DB, $CFG; + parent::setUp(); + + $this->resetAfterTest(true); + + $course = $this->getDataGenerator()->create_course(); + $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id), array('section'=>3)); + $coursemodule = $DB->get_record('course_modules', array('id'=>$page->cmid)); + + $this->moduleid = $coursemodule->id; + $this->sectionid = $DB->get_field("course_sections", 'id', array("section"=>$coursemodule->section, "course"=>$course->id)); + $this->courseid = $coursemodule->course; + $this->userid = 2; // admin + + // Disable all loggers + $CFG->backup_error_log_logger_level = backup::LOG_NONE; + $CFG->backup_file_logger_level = backup::LOG_NONE; + $CFG->backup_database_logger_level = backup::LOG_NONE; + $CFG->backup_file_logger_level_extra = backup::LOG_NONE; + } + + /** + * test base_plan class + */ + function test_base_plan() { + + // Instantiate + $bp = new mock_base_plan('name'); + $this->assertTrue($bp instanceof base_plan); + $this->assertEquals($bp->get_name(), 'name'); + $this->assertTrue(is_array($bp->get_settings())); + $this->assertEquals(count($bp->get_settings()), 0); + $this->assertTrue(is_array($bp->get_tasks())); + $this->assertEquals(count($bp->get_tasks()), 0); + } + + /** + * test backup_plan class + */ + function test_backup_plan() { + + // We need one (non interactive) controller for instantiating plan + $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE, + backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid); + // Instantiate one backup plan + $bp = new backup_plan($bc); + $this->assertTrue($bp instanceof backup_plan); + $this->assertEquals($bp->get_name(), 'backup_plan'); + + // Calculate checksum and check it + $checksum = $bp->calculate_checksum(); + $this->assertTrue($bp->is_checksum_correct($checksum)); + } + + /** + * wrong base_plan class tests + */ + function test_base_plan_wrong() { + + // We need one (non interactive) controller for instantiating plan + $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE, + backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid); + // Instantiate one backup plan + $bp = new backup_plan($bc); + // Add wrong task + try { + $bp->add_task(new stdclass()); + $this->assertTrue(false, 'base_plan_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof base_plan_exception); + $this->assertEquals($e->errorcode, 'wrong_base_task_specified'); + } + } + + /** + * wrong backup_plan class tests + */ + function test_backup_plan_wrong() { + + // Try to pass one wrong controller + try { + $bp = new backup_plan(new stdclass()); + $this->assertTrue(false, 'backup_plan_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof backup_plan_exception); + $this->assertEquals($e->errorcode, 'wrong_backup_controller_specified'); + } + try { + $bp = new backup_plan(null); + $this->assertTrue(false, 'backup_plan_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof backup_plan_exception); + $this->assertEquals($e->errorcode, 'wrong_backup_controller_specified'); + } + + // Try to build one non-existent format plan (when creating the controller) + // We need one (non interactive) controller for instatiating plan + try { + $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, 'non_existing_format', + backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid); + $this->assertTrue(false, 'backup_controller_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof backup_controller_exception); + $this->assertEquals($e->errorcode, 'backup_check_unsupported_format'); + $this->assertEquals($e->a, 'non_existing_format'); + } + } +} + + +/** + * Instantiable class extending base_plan in order to be able to perform tests + */ +class mock_base_plan extends base_plan { + public function build() { + } +} diff --git a/backup/util/plan/tests/step_test.php b/backup/util/plan/tests/step_test.php new file mode 100644 index 00000000000..c3c3678eb12 --- /dev/null +++ b/backup/util/plan/tests/step_test.php @@ -0,0 +1,325 @@ +. + +/** + * @package core_backup + * @category phpunit + * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +// Include all the needed stuff +global $CFG; +require_once($CFG->dirroot . '/backup/util/interfaces/checksumable.class.php'); +require_once($CFG->dirroot . '/backup/util/interfaces/processable.class.php'); +require_once($CFG->dirroot . '/backup/util/interfaces/annotable.class.php'); +require_once($CFG->dirroot . '/backup/util/interfaces/executable.class.php'); +require_once($CFG->dirroot . '/backup/util/interfaces/loggable.class.php'); +require_once($CFG->dirroot . '/backup/backup.class.php'); +require_once($CFG->dirroot . '/backup/util/factories/backup_factory.class.php'); +require_once($CFG->dirroot . '/backup/util/helper/backup_array_iterator.class.php'); +require_once($CFG->dirroot . '/backup/util/dbops/backup_dbops.class.php'); +require_once($CFG->dirroot . '/backup/util/dbops/backup_controller_dbops.class.php'); +require_once($CFG->dirroot . '/backup/util/dbops/backup_structure_dbops.class.php'); +require_once($CFG->dirroot . '/backup/util/helper/backup_helper.class.php'); +require_once($CFG->dirroot . '/backup/util/helper/backup_general_helper.class.php'); +require_once($CFG->dirroot . '/backup/util/checks/backup_check.class.php'); +require_once($CFG->dirroot . '/backup/util/loggers/base_logger.class.php'); +require_once($CFG->dirroot . '/backup/util/loggers/error_log_logger.class.php'); +require_once($CFG->dirroot . '/backup/util/loggers/file_logger.class.php'); +require_once($CFG->dirroot . '/backup/util/loggers/database_logger.class.php'); +require_once($CFG->dirroot . '/backup/util/loggers/output_indented_logger.class.php'); +require_once($CFG->dirroot . '/backup/util/xml/contenttransformer/xml_contenttransformer.class.php'); +require_once($CFG->dirroot . '/backup/util/xml/output/xml_output.class.php'); +require_once($CFG->dirroot . '/backup/util/xml/output/file_xml_output.class.php'); +require_once($CFG->dirroot . '/backup/util/xml/xml_writer.class.php'); +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/base_processor.class.php'); +require_once($CFG->dirroot . '/backup/util/structure/backup_attribute.class.php'); +require_once($CFG->dirroot . '/backup/util/structure/backup_final_element.class.php'); +require_once($CFG->dirroot . '/backup/util/structure/backup_nested_element.class.php'); +require_once($CFG->dirroot . '/backup/util/structure/backup_optigroup.class.php'); +require_once($CFG->dirroot . '/backup/util/structure/backup_optigroup_element.class.php'); +require_once($CFG->dirroot . '/backup/util/structure/backup_structure_processor.class.php'); +require_once($CFG->dirroot . '/backup/controller/backup_controller.class.php'); +require_once($CFG->dirroot . '/backup/util/settings/base_setting.class.php'); +require_once($CFG->dirroot . '/backup/util/settings/backup_setting.class.php'); +require_once($CFG->dirroot . '/backup/util/settings/root/root_backup_setting.class.php'); +require_once($CFG->dirroot . '/backup/util/settings/section/section_backup_setting.class.php'); +require_once($CFG->dirroot . '/backup/util/settings/activity/activity_backup_setting.class.php'); +require_once($CFG->dirroot . '/backup/moodle2/backup_settingslib.php'); +require_once($CFG->dirroot . '/backup/util/settings/base_setting.class.php'); +require_once($CFG->dirroot . '/backup/util/settings/backup_setting.class.php'); +require_once($CFG->dirroot . '/backup/util/settings/activity/activity_backup_setting.class.php'); +require_once($CFG->dirroot . '/backup/util/plan/base_plan.class.php'); +require_once($CFG->dirroot . '/backup/util/plan/backup_plan.class.php'); +require_once($CFG->dirroot . '/backup/util/plan/base_task.class.php'); +require_once($CFG->dirroot . '/backup/util/plan/backup_task.class.php'); +require_once($CFG->dirroot . '/backup/util/plan/base_step.class.php'); +require_once($CFG->dirroot . '/backup/util/plan/backup_step.class.php'); +require_once($CFG->dirroot . '/backup/util/plan/backup_execution_step.class.php'); +require_once($CFG->dirroot . '/backup/util/plan/backup_structure_step.class.php'); +require_once($CFG->dirroot . '/backup/util/output/output_controller.class.php'); +require_once($CFG->dirroot . '/backup/util/ui/backup_ui_setting.class.php'); +require_once($CFG->dirroot . '/backup/util/settings/setting_dependency.class.php'); +require_once($CFG->dirroot . '/backup/util/dbops/backup_plan_dbops.class.php'); + + +/* + * step tests (all) + */ +class backup_step_test extends advanced_testcase { + + protected $moduleid; // course_modules id used for testing + protected $sectionid; // course_sections id used for testing + protected $courseid; // course id used for testing + protected $userid; // user record used for testing + + public function setUp() { + global $DB, $CFG; + parent::setUp(); + + $this->resetAfterTest(true); + + $course = $this->getDataGenerator()->create_course(); + $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id), array('section'=>3)); + $coursemodule = $DB->get_record('course_modules', array('id'=>$page->cmid)); + + $this->moduleid = $coursemodule->id; + $this->sectionid = $DB->get_field("course_sections", 'id', array("section"=>$coursemodule->section, "course"=>$course->id)); + $this->courseid = $coursemodule->course; + $this->userid = 2; // admin + + // Disable all loggers + $CFG->backup_error_log_logger_level = backup::LOG_NONE; + $CFG->backup_file_logger_level = backup::LOG_NONE; + $CFG->backup_database_logger_level = backup::LOG_NONE; + $CFG->backup_file_logger_level_extra = backup::LOG_NONE; + } + + /** + * test base_step class + */ + function test_base_step() { + + $bp = new mock_base_plan2('planname'); // We need one plan + $bt = new mock_base_task2('taskname', $bp); // We need one task + // Instantiate + $bs = new mock_base_step('stepname', $bt); + $this->assertTrue($bs instanceof base_step); + $this->assertEquals($bs->get_name(), 'stepname'); + } + + /** + * test backup_step class + */ + function test_backup_step() { + + // 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 + $bt = new mock_backup_task2('taskname', $bp); + // Instantiate step + $bs = new mock_backup_step('stepname', $bt); + $this->assertTrue($bs instanceof backup_step); + $this->assertEquals($bs->get_name(), 'stepname'); + + } + + /** + * 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, 'assertTrue(strpos($contents, '') !== false); + $this->assertTrue(strpos($contents, 'value1') !== false); + $this->assertTrue(strpos($contents, 'value2') !== false); + $this->assertTrue(strpos($contents, '') !== false); + + unlink($file); // delete file + + // Remove the test dir and any content + @remove_dir(dirname($file)); + } + + /** + * wrong base_step class tests + */ + function test_base_step_wrong() { + + // Try to pass one wrong task + try { + $bt = new mock_base_step('teststep', new stdclass()); + $this->assertTrue(false, 'base_step_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof base_step_exception); + $this->assertEquals($e->errorcode, 'wrong_base_task_specified'); + } + } + + /** + * wrong backup_step class tests + */ + function test_backup_test_wrong() { + + // Try to pass one wrong task + try { + $bt = new mock_backup_step('teststep', new stdclass()); + $this->assertTrue(false, 'backup_step_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof backup_step_exception); + $this->assertEquals($e->errorcode, 'wrong_backup_task_specified'); + } + } +} + +/** + * Instantiable class extending base_step in order to be able to perform tests + */ +class mock_base_step extends base_step { + public function execute() { + } +} + +/** + * Instantiable class extending backup_step in order to be able to perform tests + */ +class mock_backup_step extends backup_step { + public function execute() { + } +} + +/** + * Instantiable class extending backup_task in order to mockup get_taskbasepath() + */ +class mock_backup_task_basepath extends backup_task { + + public function build() { + // Nothing to do + } + + public function define_settings() { + // Nothing to do + } + + public function get_taskbasepath() { + global $CFG; + return $CFG->tempdir . '/test'; + } +} + +/** + * Instantiable class extending backup_structure_step in order to be able to perform tests + */ +class mock_backup_structure_step extends backup_structure_step { + + protected function define_structure() { + + // Create really simple structure (1 nested with 1 attr and 2 fields) + $test = new backup_nested_element('test', + array('id'), + array('field1', 'field2') + ); + $test->set_source_array(array(array('id' => 1, 'field1' => 'value1', 'field2' => 'value2'))); + + return $test; + } +} + +/** + * Instantiable class extending activity_backup_setting to be added to task and perform tests + */ +class mock_fullpath_activity_setting extends activity_backup_setting { + public function process_change($setting, $ctype, $oldv) { + // Nothing to do + } +} + +/** + * Instantiable class extending activity_backup_setting to be added to task and perform tests + */ +class mock_backupid_activity_setting extends activity_backup_setting { + public function process_change($setting, $ctype, $oldv) { + // Nothing to do + } +} + +/** + * Instantiable class extending base_plan in order to be able to perform tests + */ +class mock_base_plan2 extends base_plan { + public function build() { + } +} + +/** + * Instantiable class extending base_task in order to be able to perform tests + */ +class mock_base_task2 extends base_task { + public function build() { + } + + public function define_settings() { + } +} + +/** + * Instantiable class extending backup_task in order to be able to perform tests + */ +class mock_backup_task2 extends backup_task { + public function build() { + } + + public function define_settings() { + } +} diff --git a/backup/util/plan/tests/task_test.php b/backup/util/plan/tests/task_test.php new file mode 100644 index 00000000000..c89da7057ec --- /dev/null +++ b/backup/util/plan/tests/task_test.php @@ -0,0 +1,228 @@ +. + +/** + * @package core_backup + * @category phpunit + * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +// Include all the needed stuff +global $CFG; +require_once($CFG->dirroot . '/backup/util/interfaces/checksumable.class.php'); +require_once($CFG->dirroot . '/backup/util/interfaces/executable.class.php'); +require_once($CFG->dirroot . '/backup/util/interfaces/loggable.class.php'); +require_once($CFG->dirroot . '/backup/util/interfaces/processable.class.php'); +require_once($CFG->dirroot . '/backup/util/interfaces/annotable.class.php'); +require_once($CFG->dirroot . '/backup/backup.class.php'); +require_once($CFG->dirroot . '/backup/util/factories/backup_factory.class.php'); +require_once($CFG->dirroot . '/backup/util/dbops/backup_dbops.class.php'); +require_once($CFG->dirroot . '/backup/util/dbops/backup_controller_dbops.class.php'); +require_once($CFG->dirroot . '/backup/util/helper/backup_helper.class.php'); +require_once($CFG->dirroot . '/backup/util/helper/backup_general_helper.class.php'); +require_once($CFG->dirroot . '/backup/util/checks/backup_check.class.php'); +require_once($CFG->dirroot . '/backup/util/loggers/base_logger.class.php'); +require_once($CFG->dirroot . '/backup/util/loggers/error_log_logger.class.php'); +require_once($CFG->dirroot . '/backup/util/loggers/file_logger.class.php'); +require_once($CFG->dirroot . '/backup/util/loggers/database_logger.class.php'); +require_once($CFG->dirroot . '/backup/util/loggers/output_indented_logger.class.php'); +require_once($CFG->dirroot . '/backup/controller/backup_controller.class.php'); +require_once($CFG->dirroot . '/backup/util/plan/base_plan.class.php'); +require_once($CFG->dirroot . '/backup/util/plan/backup_plan.class.php'); +require_once($CFG->dirroot . '/backup/util/plan/base_task.class.php'); +require_once($CFG->dirroot . '/backup/util/plan/backup_task.class.php'); +require_once($CFG->dirroot . '/backup/util/xml/contenttransformer/xml_contenttransformer.class.php'); +require_once($CFG->dirroot . '/backup/util/settings/base_setting.class.php'); +require_once($CFG->dirroot . '/backup/util/settings/backup_setting.class.php'); +require_once($CFG->dirroot . '/backup/util/settings/root/root_backup_setting.class.php'); +require_once($CFG->dirroot . '/backup/util/settings/section/section_backup_setting.class.php'); +require_once($CFG->dirroot . '/backup/util/settings/activity/activity_backup_setting.class.php'); +require_once($CFG->dirroot . '/backup/moodle2/backup_settingslib.php'); +require_once($CFG->dirroot . '/backup/util/settings/base_setting.class.php'); +require_once($CFG->dirroot . '/backup/util/settings/backup_setting.class.php'); +require_once($CFG->dirroot . '/backup/util/settings/activity/activity_backup_setting.class.php'); +require_once($CFG->dirroot . '/backup/util/plan/base_plan.class.php'); +require_once($CFG->dirroot . '/backup/util/plan/backup_plan.class.php'); +require_once($CFG->dirroot . '/backup/util/plan/base_task.class.php'); +require_once($CFG->dirroot . '/backup/util/plan/backup_task.class.php'); +require_once($CFG->dirroot . '/backup/util/plan/base_step.class.php'); +require_once($CFG->dirroot . '/backup/util/plan/backup_step.class.php'); +require_once($CFG->dirroot . '/backup/util/plan/backup_execution_step.class.php'); +require_once($CFG->dirroot . '/backup/util/plan/backup_structure_step.class.php'); +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/base_processor.class.php'); +require_once($CFG->dirroot . '/backup/util/structure/backup_attribute.class.php'); +require_once($CFG->dirroot . '/backup/util/structure/backup_final_element.class.php'); +require_once($CFG->dirroot . '/backup/util/structure/backup_nested_element.class.php'); +require_once($CFG->dirroot . '/backup/util/structure/backup_optigroup.class.php'); +require_once($CFG->dirroot . '/backup/util/structure/backup_optigroup_element.class.php'); +require_once($CFG->dirroot . '/backup/util/structure/backup_structure_processor.class.php'); +require_once($CFG->dirroot . '/backup/util/output/output_controller.class.php'); +require_once($CFG->dirroot . '/backup/util/ui/backup_ui_setting.class.php'); +require_once($CFG->dirroot . '/backup/util/settings/setting_dependency.class.php'); +require_once($CFG->dirroot . '/backup/util/dbops/backup_plan_dbops.class.php'); + + +/** + * task tests (all) + */ +class backup_task_test extends advanced_testcase { + + protected $moduleid; // course_modules id used for testing + protected $sectionid; // course_sections id used for testing + protected $courseid; // course id used for testing + protected $userid; // user record used for testing + + public function setUp() { + global $DB, $CFG; + parent::setUp(); + + $this->resetAfterTest(true); + + $course = $this->getDataGenerator()->create_course(); + $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id), array('section'=>3)); + $coursemodule = $DB->get_record('course_modules', array('id'=>$page->cmid)); + + $this->moduleid = $coursemodule->id; + $this->sectionid = $DB->get_field("course_sections", 'id', array("section"=>$coursemodule->section, "course"=>$course->id)); + $this->courseid = $coursemodule->course; + $this->userid = 2; // admin + + // Disable all loggers + $CFG->backup_error_log_logger_level = backup::LOG_NONE; + $CFG->backup_file_logger_level = backup::LOG_NONE; + $CFG->backup_database_logger_level = backup::LOG_NONE; + $CFG->backup_file_logger_level_extra = backup::LOG_NONE; + } + + /** + * test base_task class + */ + function test_base_task() { + + $bp = new mock_base_plan3('planname'); // We need one plan + // Instantiate + $bt = new mock_base_task('taskname', $bp); + $this->assertTrue($bt instanceof base_task); + $this->assertEquals($bt->get_name(), 'taskname'); + $this->assertTrue(is_array($bt->get_settings())); + $this->assertEquals(count($bt->get_settings()), 0); + $this->assertTrue(is_array($bt->get_steps())); + $this->assertEquals(count($bt->get_steps()), 0); + } + + /** + * test backup_task class + */ + function test_backup_task() { + + // 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); + // Instantiate task + $bt = new mock_backup_task('taskname', $bp); + $this->assertTrue($bt instanceof backup_task); + $this->assertEquals($bt->get_name(), 'taskname'); + + // Calculate checksum and check it + $checksum = $bt->calculate_checksum(); + $this->assertTrue($bt->is_checksum_correct($checksum)); + + } + + /** + * wrong base_task class tests + */ + function test_base_task_wrong() { + + // Try to pass one wrong plan + try { + $bt = new mock_base_task('tasktest', new stdclass()); + $this->assertTrue(false, 'base_task_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof base_task_exception); + $this->assertEquals($e->errorcode, 'wrong_base_plan_specified'); + } + + // Add wrong step to task + $bp = new mock_base_plan3('planname'); // We need one plan + // Instantiate + $bt = new mock_base_task('taskname', $bp); + try { + $bt->add_step(new stdclass()); + $this->assertTrue(false, 'base_task_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof base_task_exception); + $this->assertEquals($e->errorcode, 'wrong_base_step_specified'); + } + + } + + /** + * wrong backup_task class tests + */ + function test_backup_task_wrong() { + + // Try to pass one wrong plan + try { + $bt = new mock_backup_task('tasktest', new stdclass()); + $this->assertTrue(false, 'backup_task_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof backup_task_exception); + $this->assertEquals($e->errorcode, 'wrong_backup_plan_specified'); + } + } +} + +/** + * Instantiable class extending base_task in order to be able to perform tests + */ +class mock_base_task extends base_task { + public function build() { + } + + public function define_settings() { + } +} + +/** + * Instantiable class extending backup_task in order to be able to perform tests + */ +class mock_backup_task extends backup_task { + public function build() { + } + + public function define_settings() { + } +} + +/** + * Instantiable class extending base_plan in order to be able to perform tests + */ +class mock_base_plan3 extends base_plan { + public function build() { + } +} + diff --git a/backup/util/settings/tests/settings_test.php b/backup/util/settings/tests/settings_test.php new file mode 100644 index 00000000000..05ce246b504 --- /dev/null +++ b/backup/util/settings/tests/settings_test.php @@ -0,0 +1,463 @@ +. + +/** + * @package core_backup + * @category phpunit + * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +// Include all the needed stuff +global $CFG; +require_once($CFG->dirroot . '/backup/util/interfaces/checksumable.class.php'); +require_once($CFG->dirroot . '/backup/backup.class.php'); +require_once($CFG->dirroot . '/backup/util/settings/base_setting.class.php'); +require_once($CFG->dirroot . '/backup/util/settings/backup_setting.class.php'); +require_once($CFG->dirroot . '/backup/util/settings/setting_dependency.class.php'); +require_once($CFG->dirroot . '/backup/util/settings/root/root_backup_setting.class.php'); +require_once($CFG->dirroot . '/backup/util/settings/activity/activity_backup_setting.class.php'); +require_once($CFG->dirroot . '/backup/util/settings/section/section_backup_setting.class.php'); +require_once($CFG->dirroot . '/backup/util/settings/course/course_backup_setting.class.php'); +require_once($CFG->dirroot . '/backup/util/ui/backup_ui_setting.class.php'); + + +/** + * setting tests (all) + */ +class setting_test extends basic_testcase { + + /** + * test base_setting class + */ + function test_base_setting() { + // Instantiate base_setting and check everything + $bs = new mock_base_setting('test', base_setting::IS_BOOLEAN); + $this->assertTrue($bs instanceof base_setting); + $this->assertEquals($bs->get_name(), 'test'); + $this->assertEquals($bs->get_vtype(), base_setting::IS_BOOLEAN); + $this->assertTrue(is_null($bs->get_value())); + $this->assertEquals($bs->get_visibility(), base_setting::VISIBLE); + $this->assertEquals($bs->get_status(), base_setting::NOT_LOCKED); + + // Instantiate base_setting with explicit nulls + $bs = new mock_base_setting('test', base_setting::IS_FILENAME, 'filename.txt', null, null); + $this->assertEquals($bs->get_value() , 'filename.txt'); + $this->assertEquals($bs->get_visibility(), base_setting::VISIBLE); + $this->assertEquals($bs->get_status(), base_setting::NOT_LOCKED); + + // Instantiate base_setting and set value, visibility and status + $bs = new mock_base_setting('test', base_setting::IS_BOOLEAN); + $bs->set_value(true); + $this->assertNotEmpty($bs->get_value()); + $bs->set_visibility(base_setting::HIDDEN); + $this->assertEquals($bs->get_visibility(), base_setting::HIDDEN); + $bs->set_status(base_setting::LOCKED_BY_HIERARCHY); + $this->assertEquals($bs->get_status(), base_setting::LOCKED_BY_HIERARCHY); + + // Instantiate with wrong vtype + try { + $bs = new mock_base_setting('test', 'one_wrong_type'); + $this->assertTrue(false, 'base_setting_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof base_setting_exception); + $this->assertEquals($e->errorcode, 'setting_invalid_type'); + } + + // Instantiate with wrong integer value + try { + $bs = new mock_base_setting('test', base_setting::IS_INTEGER, 99.99); + $this->assertTrue(false, 'base_setting_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof base_setting_exception); + $this->assertEquals($e->errorcode, 'setting_invalid_integer'); + } + + // Instantiate with wrong filename value + try { + $bs = new mock_base_setting('test', base_setting::IS_FILENAME, '../../filename.txt'); + $this->assertTrue(false, 'base_setting_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof base_setting_exception); + $this->assertEquals($e->errorcode, 'setting_invalid_filename'); + } + + // Instantiate with wrong visibility + try { + $bs = new mock_base_setting('test', base_setting::IS_BOOLEAN, null, 'one_wrong_visibility'); + $this->assertTrue(false, 'base_setting_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof base_setting_exception); + $this->assertEquals($e->errorcode, 'setting_invalid_visibility'); + } + + // Instantiate with wrong status + try { + $bs = new mock_base_setting('test', base_setting::IS_BOOLEAN, null, null, 'one_wrong_status'); + $this->assertTrue(false, 'base_setting_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof base_setting_exception); + $this->assertEquals($e->errorcode, 'setting_invalid_status'); + } + + // Instantiate base_setting and try to set wrong ui_type + // We need a custom error handler to catch the type hinting error + // that should return incorrect_object_passed + $bs = new mock_base_setting('test', base_setting::IS_BOOLEAN); + set_error_handler('backup_setting_error_handler', E_RECOVERABLE_ERROR); + try { + $bs->set_ui('one_wrong_ui_type', 'label', array(), array()); + $this->assertTrue(false, 'base_setting_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof base_setting_exception); + $this->assertEquals($e->errorcode, 'incorrect_object_passed'); + } + restore_error_handler(); + + // Instantiate base_setting and try to set wrong ui_label + // We need a custom error handler to catch the type hinting error + // that should return incorrect_object_passed + $bs = new mock_base_setting('test', base_setting::IS_BOOLEAN); + set_error_handler('backup_setting_error_handler', E_RECOVERABLE_ERROR); + try { + $bs->set_ui(base_setting::UI_HTML_CHECKBOX, 'one/wrong/label', array(), array()); + $this->assertTrue(false, 'base_setting_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof base_setting_exception); + $this->assertEquals($e->errorcode, 'incorrect_object_passed'); + } + restore_error_handler(); + + // Try to change value of locked setting by permission + $bs = new mock_base_setting('test', base_setting::IS_BOOLEAN, null, null, base_setting::LOCKED_BY_PERMISSION); + try { + $bs->set_value(true); + $this->assertTrue(false, 'base_setting_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof base_setting_exception); + $this->assertEquals($e->errorcode, 'setting_locked_by_permission'); + } + + // Try to change value of locked setting by config + $bs = new mock_base_setting('test', base_setting::IS_BOOLEAN, null, null, base_setting::LOCKED_BY_CONFIG); + try { + $bs->set_value(true); + $this->assertTrue(false, 'base_setting_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof base_setting_exception); + $this->assertEquals($e->errorcode, 'setting_locked_by_config'); + } + + // Try to add same setting twice + $bs1 = new mock_base_setting('test1', base_setting::IS_INTEGER, null); + $bs2 = new mock_base_setting('test2', base_setting::IS_INTEGER, null); + $bs1->add_dependency($bs2, null, array('value'=>0)); + try { + $bs1->add_dependency($bs2); + $this->assertTrue(false, 'base_setting_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof base_setting_exception); + $this->assertEquals($e->errorcode, 'setting_already_added'); + } + + // Try to create one circular reference + $bs1 = new mock_base_setting('test1', base_setting::IS_INTEGER, null); + try { + $bs1->add_dependency($bs1); // self + $this->assertTrue(false, 'base_setting_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof base_setting_exception); + $this->assertEquals($e->errorcode, 'setting_circular_reference'); + $this->assertTrue($e->a instanceof stdclass); + $this->assertEquals($e->a->main, 'test1'); + $this->assertEquals($e->a->alreadydependent, 'test1'); + } + + $bs1 = new mock_base_setting('test1', base_setting::IS_INTEGER, null); + $bs2 = new mock_base_setting('test2', base_setting::IS_INTEGER, null); + $bs3 = new mock_base_setting('test3', base_setting::IS_INTEGER, null); + $bs4 = new mock_base_setting('test4', base_setting::IS_INTEGER, null); + $bs1->add_dependency($bs2, null, array('value'=>0)); + $bs2->add_dependency($bs3, null, array('value'=>0)); + $bs3->add_dependency($bs4, null, array('value'=>0)); + try { + $bs4->add_dependency($bs1, null, array('value'=>0)); + $this->assertTrue(false, 'base_setting_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof base_setting_exception); + $this->assertEquals($e->errorcode, 'setting_circular_reference'); + $this->assertTrue($e->a instanceof stdclass); + $this->assertEquals($e->a->main, 'test1'); + $this->assertEquals($e->a->alreadydependent, 'test4'); + } + + $bs1 = new mock_base_setting('test1', base_setting::IS_INTEGER, null); + $bs2 = new mock_base_setting('test2', base_setting::IS_INTEGER, null); + $bs1->register_dependency(new setting_dependency_disabledif_empty($bs1, $bs2)); + try { + // $bs1 is already dependent on $bs2 so this should fail. + $bs2->register_dependency(new setting_dependency_disabledif_empty($bs2, $bs1)); + $this->assertTrue(false, 'base_setting_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof base_setting_exception); + $this->assertEquals($e->errorcode, 'setting_circular_reference'); + $this->assertTrue($e->a instanceof stdclass); + $this->assertEquals($e->a->main, 'test1'); + $this->assertEquals($e->a->alreadydependent, 'test2'); + } + + // Create 3 settings and observe between them, last one must + // automatically inherit all the settings defined in the main one + $bs1 = new mock_base_setting('test1', base_setting::IS_INTEGER, null); + $bs2 = new mock_base_setting('test2', base_setting::IS_INTEGER, null); + $bs3 = new mock_base_setting('test3', base_setting::IS_INTEGER, null); + $bs1->add_dependency($bs2, setting_dependency::DISABLED_NOT_EMPTY); + $bs2->add_dependency($bs3, setting_dependency::DISABLED_NOT_EMPTY); + // Check values are spreaded ok + $bs1->set_value(123); + $this->assertEquals($bs1->get_value(), 123); + $this->assertEquals($bs2->get_value(), $bs1->get_value()); + $this->assertEquals($bs3->get_value(), $bs1->get_value()); + + // Add one more setting and set value again + $bs4 = new mock_base_setting('test4', base_setting::IS_INTEGER, null); + $bs2->add_dependency($bs4, setting_dependency::DISABLED_NOT_EMPTY); + $bs2->set_value(321); + // The above change should change + $this->assertEquals($bs1->get_value(), 123); + $this->assertEquals($bs2->get_value(), 321); + $this->assertEquals($bs3->get_value(), 321); + $this->assertEquals($bs4->get_value(), 321); + + // Check visibility is spreaded ok + $bs1->set_visibility(base_setting::HIDDEN); + $this->assertEquals($bs2->get_visibility(), $bs1->get_visibility()); + $this->assertEquals($bs3->get_visibility(), $bs1->get_visibility()); + // Check status is spreaded ok + $bs1->set_status(base_setting::LOCKED_BY_HIERARCHY); + $this->assertEquals($bs2->get_status(), $bs1->get_status()); + $this->assertEquals($bs3->get_status(), $bs1->get_status()); + + // Create 3 settings and observe between them, put them in one array, + // force serialize/deserialize to check the observable pattern continues + // working after that + $bs1 = new mock_base_setting('test1', base_setting::IS_INTEGER, null); + $bs2 = new mock_base_setting('test2', base_setting::IS_INTEGER, null); + $bs3 = new mock_base_setting('test3', base_setting::IS_INTEGER, null); + $bs1->add_dependency($bs2, null, array('value'=>0)); + $bs2->add_dependency($bs3, null, array('value'=>0)); + // Serialize + $arr = array($bs1, $bs2, $bs3); + $ser = base64_encode(serialize($arr)); + // Unserialize and copy to new objects + $newarr = unserialize(base64_decode($ser)); + $ubs1 = $newarr[0]; + $ubs2 = $newarr[1]; + $ubs3 = $newarr[2]; + // Must continue being base settings + $this->assertTrue($ubs1 instanceof base_setting); + $this->assertTrue($ubs2 instanceof base_setting); + $this->assertTrue($ubs3 instanceof base_setting); + // Set parent setting + $ubs1->set_value(1234); + $ubs1->set_visibility(base_setting::HIDDEN); + $ubs1->set_status(base_setting::LOCKED_BY_HIERARCHY); + // Check changes have been spreaded + $this->assertEquals($ubs2->get_visibility(), $ubs1->get_visibility()); + $this->assertEquals($ubs3->get_visibility(), $ubs1->get_visibility()); + $this->assertEquals($ubs2->get_status(), $ubs1->get_status()); + $this->assertEquals($ubs3->get_status(), $ubs1->get_status()); + } + + /** + * test backup_setting class + */ + function test_backup_setting() { + // Instantiate backup_setting class and set level + $bs = new mock_backup_setting('test', base_setting::IS_INTEGER, null); + $bs->set_level(1); + $this->assertEquals($bs->get_level(), 1); + + // Instantiate backup setting class and try to add one non backup_setting dependency + set_error_handler('backup_setting_error_handler', E_RECOVERABLE_ERROR); + $bs = new mock_backup_setting('test', base_setting::IS_INTEGER, null); + try { + $bs->add_dependency(new stdclass()); + $this->assertTrue(false, 'backup_setting_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof backup_setting_exception); + $this->assertEquals($e->errorcode, 'incorrect_object_passed'); + } + restore_error_handler(); + + // Try to assing upper level dependency + $bs1 = new mock_backup_setting('test1', base_setting::IS_INTEGER, null); + $bs1->set_level(1); + $bs2 = new mock_backup_setting('test2', base_setting::IS_INTEGER, null); + $bs2->set_level(2); + try { + $bs2->add_dependency($bs1); + $this->assertTrue(false, 'backup_setting_exception expected'); + } catch (exception $e) { + $this->assertTrue($e instanceof backup_setting_exception); + $this->assertEquals($e->errorcode, 'cannot_add_upper_level_dependency'); + } + + // Check dependencies are working ok + $bs1 = new mock_backup_setting('test1', base_setting::IS_INTEGER, null); + $bs1->set_level(1); + $bs2 = new mock_backup_setting('test2', base_setting::IS_INTEGER, null); + $bs2->set_level(1); // Same level *must* work + $bs1->add_dependency($bs2, setting_dependency::DISABLED_NOT_EMPTY); + $bs1->set_value(123456); + $this->assertEquals($bs2->get_value(), $bs1->get_value()); + } + + /** + * test activity_backup_setting class + */ + function test_activity_backup_setting() { + $bs = new mock_activity_backup_setting('test', base_setting::IS_INTEGER, null); + $this->assertEquals($bs->get_level(), backup_setting::ACTIVITY_LEVEL); + + // Check checksum implementation is working + $bs1 = new mock_activity_backup_setting('test', base_setting::IS_INTEGER, null); + $bs1->set_value(123); + $checksum = $bs1->calculate_checksum(); + $this->assertNotEmpty($checksum); + $this->assertTrue($bs1->is_checksum_correct($checksum)); + } + + /** + * test section_backup_setting class + */ + function test_section_backup_setting() { + $bs = new mock_section_backup_setting('test', base_setting::IS_INTEGER, null); + $this->assertEquals($bs->get_level(), backup_setting::SECTION_LEVEL); + + // Check checksum implementation is working + $bs1 = new mock_section_backup_setting('test', base_setting::IS_INTEGER, null); + $bs1->set_value(123); + $checksum = $bs1->calculate_checksum(); + $this->assertNotEmpty($checksum); + $this->assertTrue($bs1->is_checksum_correct($checksum)); + } + + /** + * test course_backup_setting class + */ + function test_course_backup_setting() { + $bs = new mock_course_backup_setting('test', base_setting::IS_INTEGER, null); + $this->assertEquals($bs->get_level(), backup_setting::COURSE_LEVEL); + + // Check checksum implementation is working + $bs1 = new mock_course_backup_setting('test', base_setting::IS_INTEGER, null); + $bs1->set_value(123); + $checksum = $bs1->calculate_checksum(); + $this->assertNotEmpty($checksum); + $this->assertTrue($bs1->is_checksum_correct($checksum)); + } +} + +/** + * helper extended base_setting class that makes some methods public for testing + */ +class mock_base_setting extends base_setting { + public function get_vtype() { + return $this->vtype; + } + + public function process_change($setting, $ctype, $oldv) { + // Simply, inherit from the main object + $this->set_value($setting->get_value()); + $this->set_visibility($setting->get_visibility()); + $this->set_status($setting->get_status()); + } + + public function get_ui_info() { + // Return an array with all the ui info to be tested + return array($this->ui_type, $this->ui_label, $this->ui_values, $this->ui_options); + } +} + +/** + * helper extended backup_setting class that makes some methods public for testing + */ +class mock_backup_setting extends backup_setting { + public function set_level($level) { + $this->level = $level; + } + + public function process_change($setting, $ctype, $oldv) { + // Simply, inherit from the main object + $this->set_value($setting->get_value()); + $this->set_visibility($setting->get_visibility()); + $this->set_status($setting->get_status()); + } +} + +/** + * helper extended activity_backup_setting class that makes some methods public for testing + */ +class mock_activity_backup_setting extends activity_backup_setting { + public function process_change($setting, $ctype, $oldv) { + // Do nothing + } +} + +/** + * helper extended section_backup_setting class that makes some methods public for testing + */ +class mock_section_backup_setting extends section_backup_setting { + public function process_change($setting, $ctype, $oldv) { + // Do nothing + } +} + +/** + * helper extended course_backup_setting class that makes some methods public for testing + */ +class mock_course_backup_setting extends course_backup_setting { + public function process_change($setting, $ctype, $oldv) { + // Do nothing + } +} + +/** + * This error handler is used to convert errors to excpetions so that simepltest can + * catch them. + * + * This is required in order to catch type hint mismatches that result in a error + * being thrown. It should only ever be used to catch E_RECOVERABLE_ERROR's. + * + * It throws a backup_setting_exception with 'incorrect_object_passed' + * + * @param int $errno E_RECOVERABLE_ERROR + * @param string $errstr + * @param string $errfile + * @param int $errline + * @param array $errcontext + * @return null + */ +function backup_setting_error_handler($errno, $errstr, $errfile, $errline, $errcontext) { + if ($errno !== E_RECOVERABLE_ERROR) { + // Currently we only want to deal with type hinting errors + return false; + } + throw new backup_setting_exception('incorrect_object_passed'); +} diff --git a/lib/phpunit/lib.php b/lib/phpunit/lib.php index e40be9938d5..b923d7e0adc 100644 --- a/lib/phpunit/lib.php +++ b/lib/phpunit/lib.php @@ -142,6 +142,12 @@ class phpunit_util { } $trans->allow_commit(); $dbreset = true; + // remove extra tables + foreach ($tables as $tablename) { + if (!isset($data[$tablename])) { + $DB->get_manager()->drop_table(new xmldb_table($tablename)); + } + } } } diff --git a/lib/tests/phpunit_test.php b/lib/tests/phpunit_test.php index 63954b6756a..95876f9747c 100644 --- a/lib/tests/phpunit_test.php +++ b/lib/tests/phpunit_test.php @@ -37,7 +37,7 @@ defined('MOODLE_INTERNAL') || die(); class core_phpunit_basic_testcase extends basic_testcase { /** - * Tests that bootstraping has occurred correctly + * Tests that bootstrapping has occurred correctly * @return void */ public function test_bootstrap() { @@ -99,4 +99,4 @@ class core_phpunit_advanced_testcase extends advanced_testcase { $this->assertEquals(0, $USER->id); $this->assertSame($_SESSION['USER'], $USER); } -} \ No newline at end of file +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist index e369a986ccc..a7bed3fa176 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -33,6 +33,11 @@ lib/form/tests + + backup/controller/tests + backup/converter/moodle1/tests + backup/util +