"MDL-8642, setting of auto scrolling"
[moodle-linuxchix.git] / lib / simpletest / testbackuplib.php
blob572c4250bf50ce57e85b540a331df9d28843a53d
1 <?php // $Id$
3 ///////////////////////////////////////////////////////////////////////////
4 // //
5 // NOTICE OF COPYRIGHT //
6 // //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.org //
9 // //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
11 // //
12 // This program is free software; you can redistribute it and/or modify //
13 // it under the terms of the GNU General Public License as published by //
14 // the Free Software Foundation; either version 2 of the License, or //
15 // (at your option) any later version. //
16 // //
17 // This program is distributed in the hope that it will be useful, //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20 // GNU General Public License for more details: //
21 // //
22 // http://www.gnu.org/copyleft/gpl.html //
23 // //
24 ///////////////////////////////////////////////////////////////////////////
26 /**
27 * Unit tests for (some of) ../../backup/backuplib.php.
29 * @author nicolasconnault@gmail.com
30 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
31 * @package moodlecore
34 if (!defined('MOODLE_INTERNAL')) {
35 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
38 require_once($CFG->dirroot . '/backup/backuplib.php');
39 Mock::generate('ADODB_mysql');
40 Mock::generate('ADORecordSet_mysql');
42 class backuplib_test extends UnitTestCase {
43 var $real_db;
44 var $real_dataroot;
45 var $rs;
46 var $firstcolumn;
47 var $db;
48 var $testfiles = array();
49 var $userbasedir;
51 /* The mocking of the $rs and $db objects is problematic. Somehow it's affecting other unit test suites.
52 I'm commenting this off until this is resolved -- nicolasconnault@gmail.com --
54 function setUp() {
55 global $db, $CFG;
56 $this->real_db = fullclone($db);
57 $db = new MockADODB_mysql();
58 $this->rs = new MockADORecordSet_mysql();
59 $this->rs->EOF = false;
60 $this->firstcolumn = new stdClass();
61 $this->firstcolumn->name = 'id';
63 // Override dataroot: we don't want to test with live data
64 $this->real_dataroot = fullclone($CFG->dataroot);
65 $CFG->dataroot .= '/unittests';
66 $this->userbasedir = $CFG->dataroot.'/user';
68 // Create some sample files in this temporary directory
69 mkdir($CFG->dataroot);
70 mkdir($this->userbasedir);
72 $this->testfiles = array('0/1','0/3','1000/1043','457498000/457498167');
73 foreach ($this->testfiles as $file) {
74 $parts = explode('/', $file);
76 if (!file_exists("$this->userbasedir/{$parts[0]}")) {
77 mkdir("$this->userbasedir/{$parts[0]}");
79 mkdir("$this->userbasedir/$file");
80 $handle = fopen("$this->userbasedir/$file/f1.gif", 'w+b');
81 fclose($handle);
85 function tearDown() {
86 global $CFG, $db;
88 if (!is_null($this->real_dataroot) && $this->real_dataroot != $CFG->dataroot) {
89 remove_dir($CFG->dataroot);
91 $db = $this->real_db;
92 $CFG->dataroot = $this->real_dataroot;
95 function test_backup_copy_user_files() {
96 global $CFG, $db;
97 $preferences = new stdClass();
98 $preferences->backup_unique_code = time();
100 $db->setReturnValue('Execute', $this->rs);
101 $this->rs->setReturnValue('RecordCount', 1);
102 $this->rs->fields = array(1);
104 // Perform the backup
105 backup_copy_user_files($preferences);
107 // Check for the existence of the backup file
108 $backupfile = "$CFG->dataroot/temp/backup/$preferences->backup_unique_code/user_files";
109 $this->assertTrue(file_exists($backupfile));
111 // Check for the existence of the user files in the backup file
112 foreach ($this->testfiles as $file) {
113 $parts = explode('/', $file);
114 $section = $parts[0];
115 $userid = $parts[1];
116 $userimage = "$CFG->dataroot/temp/backup/$preferences->backup_unique_code/user_files/$section/$userid/f1.gif";
117 $this->assertTrue(file_exists($userimage));
121 // This is a moodlelib method but it is used in backuplib, so it is tested here in that context, with typical backup data.
122 function test_get_user_directories() {
123 global $CFG;
124 $dirlist = get_user_directories();
125 $this->assertEqual(4, count($dirlist));
127 foreach ($this->testfiles as $file) {
128 $parts = explode('/', $file);
129 $section = $parts[0];
130 $userid = $parts[1];
132 $this->assertEqual($file, $dirlist[$userid]['userfolder']);
133 $this->assertEqual($this->userbasedir, $dirlist[$userid]['basedir']);