Remove unused code, format code.

remotes/angel/1.11.x
jmontoyaa 8 years ago
parent 8fe553c01b
commit ea549fce47
  1. 2
      main/document/document.php
  2. 6
      main/exercise/exercise.php
  3. 2
      main/exercise/hotpotatoes.php
  4. 44
      main/inc/lib/document.lib.php
  5. 203
      main/inc/lib/fileManage.lib.php
  6. 110
      main/inc/lib/fileUpload.lib.php
  7. 8
      main/lp/aicc.class.php
  8. 1
      main/work/work.lib.php
  9. 39
      tests/main/inc/lib/fileManage.lib.test.php
  10. 40
      tests/main/inc/lib/fileManager.lib.test.php
  11. 772
      tests/main/inc/lib/fileUpload.lib.test.php

@ -1051,7 +1051,7 @@ if ($is_allowed_to_edit ||
$fileExist = true;
}
if (move($base_work_dir.$document_to_move['path'], $base_work_dir.$moveTo)) {
update_db_info(
DocumentManager::updateDbInfo(
'update',
$document_to_move['path'],
$moveTo . '/' . basename($document_to_move['path'])

@ -311,15 +311,15 @@ if ($is_allowedToEdit) {
for ($i = 0; $i < $imgcount; $i++) {
my_delete($documentPath.$uploadPath."/".$fld."/".$imgparams[$i]);
update_db_info("delete", $uploadPath."/".$fld."/".$imgparams[$i]);
DocumentManager::updateDbInfo("delete", $uploadPath."/".$fld."/".$imgparams[$i]);
}
if (!is_dir($documentPath.$uploadPath."/".$fld."/")) {
my_delete($documentPath.$file);
update_db_info("delete", $file);
DocumentManager::updateDbInfo("delete", $file);
} else {
if (my_delete($documentPath.$file)) {
update_db_info("delete", $file);
DocumentManager::updateDbInfo("delete", $file);
}
}

@ -169,7 +169,7 @@ if ((api_is_allowed_to_edit(null, true)) && (($finish == 0) || ($finish == 2)))
} else {
$dialogBox .= $filename.' '.get_lang('NameNotEqual');
my_delete($document_sys_path.$uploadPath.'/'.$fld.'/'.$filename);
update_db_info('delete', $uploadPath.'/'.$fld.'/'.$filename);
DocumentManager::updateDbInfo('delete', $uploadPath.'/'.$fld.'/'.$filename);
}
if ($imgcount == 0) { // all image uploaded
$finish = 1;

@ -5927,7 +5927,7 @@ class DocumentManager
$tmp_folders_titles[$tmp_path] = $tmp_title;
}
}
return $path_displayed;
}
@ -6297,4 +6297,46 @@ class DocumentManager
WHERE c_id = $courseId AND session_id = $sessionId AND tool = '".TOOL_DOCUMENT."'";
Database::query($sql);
}
/**
* Update the file or directory path in the document db document table
*
* @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
* @param - action (string) - action type require : 'delete' or 'update'
* @param - old_path (string) - old path info stored to change
* @param - new_path (string) - new path info to substitute
* @desc Update the file or directory path in the document db document table
*
*/
public static function updateDbInfo($action, $old_path, $new_path = '')
{
$dbTable = Database::get_course_table(TABLE_DOCUMENT);
$course_id = api_get_course_int_id();
switch ($action) {
case 'delete':
$old_path = Database::escape_string($old_path);
$query = "DELETE FROM $dbTable
WHERE
c_id = $course_id AND
(
path LIKE BINARY '".$old_path."' OR
path LIKE BINARY '".$old_path."/%'
)";
Database::query($query);
break;
case 'update':
if ($new_path[0] == '.') {
$new_path = substr($new_path, 1);
}
$new_path = str_replace('//', '/', $new_path);
// Attempt to update - tested & working for root dir
$new_path = Database::escape_string($new_path);
$query = "UPDATE $dbTable SET
path = CONCAT('".$new_path."', SUBSTRING(path, LENGTH('".$old_path."')+1) )
WHERE c_id = $course_id AND (path LIKE BINARY '".$old_path."' OR path LIKE BINARY '".$old_path."/%')";
Database::query($query);
break;
}
}
}

@ -9,46 +9,6 @@ use Symfony\Component\Filesystem\Filesystem;
* @package chamilo.library
*/
/**
* Update the file or directory path in the document db document table
*
* @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
* @param - action (string) - action type require : 'delete' or 'update'
* @param - old_path (string) - old path info stored to change
* @param - new_path (string) - new path info to substitute
* @desc Update the file or directory path in the document db document table
*
*/
function update_db_info($action, $old_path, $new_path = '')
{
$dbTable = Database::get_course_table(TABLE_DOCUMENT);
$course_id = api_get_course_int_id();
switch ($action) {
case 'delete':
$old_path = Database::escape_string($old_path);
$query = "DELETE FROM $dbTable
WHERE
c_id = $course_id AND
(
path LIKE BINARY '".$old_path."' OR
path LIKE BINARY '".$old_path."/%'
)";
Database::query($query);
break;
case 'update':
if ($new_path[0] == '.') $new_path = substr($new_path, 1);
$new_path = str_replace('//', '/', $new_path);
// Attempt to update - tested & working for root dir
$new_path = Database::escape_string($new_path);
$query = "UPDATE $dbTable SET
path = CONCAT('".$new_path."', SUBSTRING(path, LENGTH('".$old_path."')+1) )
WHERE c_id = $course_id AND (path LIKE BINARY '".$old_path."' OR path LIKE BINARY '".$old_path."/%')";
Database::query($query);
break;
}
}
/**
* Cheks a file or a directory actually exist at this location
*
@ -56,7 +16,8 @@ function update_db_info($action, $old_path, $new_path = '')
* @param string $file_path Path of the presume existing file or dir
* @return boolean TRUE if the file or the directory exists or FALSE otherwise.
*/
function check_name_exist($file_path) {
function check_name_exist($file_path)
{
clearstatcache();
$save_dir = getcwd();
if (!is_dir(dirname($file_path))) {
@ -201,6 +162,7 @@ function my_rename($file_path, $new_file_name) {
chdir($path);
$res = rename($old_file_name, $new_file_name) ? $new_file_name : false;
chdir($save_dir);
return $res;
}
@ -271,14 +233,14 @@ function copyDirTo($orig_dir_path, $destination, $move = true)
}
}
/**
* Extracting extention of a filename
* Extracting extension of a filename
*
* @returns array
* @param string $filename filename
*/
function getextension($filename) {
function getextension($filename)
{
$bouts = explode('.', $filename);
return array(array_pop($bouts), implode('.', $bouts));
}
@ -305,156 +267,3 @@ function dirsize($root, $recursive = true) {
@closedir($dir);
return $size;
}
/* CLASS FileManager */
/**
This class contains functions that you can access statically.
FileManager::list_all_directories($path)
FileManager::list_all_files($dir_array)
FileManager::compat_load_file($file_name)
FileManager::set_default_settings($upload_path, $filename, $filetype="file", $glued_table, $default_visibility='v')
@author Roan Embrechts
@version 1.1, July 2004
* @package chamilo.library
*/
class FileManager
{
/**
Returns a list of all directories, except the base dir,
of the current course. This function uses recursion.
Convention: the parameter $path does not end with a slash.
@author Roan Embrechts
@version 1.0.1
*/
function list_all_directories($path)
{
$result_array = array();
if (is_dir($path)) {
$save_dir = getcwd();
chdir($path);
$handle = opendir($path);
while ($element = readdir($handle)) {
if ($element == '.' || $element == '..') continue;
// Skip the current and parent directories
if (is_dir($element)) {
$dir_array[] = $path.'/'.$element;
}
}
closedir($handle);
// Recursive operation if subdirectories exist
$dir_number = sizeof($dir_array);
if ($dir_number > 0) {
for ($i = 0 ; $i < $dir_number ; $i++) {
$sub_dir_array = FileManager::list_all_directories($dir_array[$i]); // Function recursivity
if (is_array($dir_array) && is_array($sub_dir_array)) {
$dir_array = array_merge($dir_array, $sub_dir_array); // Data merge
}
}
}
$result_array = $dir_array;
chdir($save_dir) ;
}
return $result_array ;
}
/**
This function receives a list of directories.
It returns a list of all files in these directories
@author Roan Embrechts
@version 1.0
*/
function list_all_files($dir_array)
{
$element_array = array();
if (is_dir($dir_array)) {
$save_dir = getcwd();
foreach ($dir_array as $directory) {
chdir($directory);
$handle = opendir($directory);
while ($element = readdir($handle)) {
if ($element == '.' || $element == '..' || $element == '.htaccess') continue;
// Skip the current and parent directories
if (!is_dir($element)) {
$element_array[] = $directory.'/'.$element;
}
}
closedir($handle);
chdir('..');
chdir($save_dir);
}
}
return $element_array;
}
/**
Loads contents of file $filename into memory and returns them as a string.
Function kept for compatibility with older PHP versions.
Function is binary safe (is needed on Windows)
*/
function compat_load_file($file_name)
{
$buffer = '';
if (file_exists($file_name)) {
$fp = fopen($file_name, 'rb');
$buffer = fread ($fp, filesize($file_name));
fclose ($fp);
}
return $buffer;
}
/**
* Adds file/folder to document table in database
* improvement from set_default_settings (see below):
* take all info from function parameters
* no global variables needed
*
* NOTE $glued_table should already have backticks around it
* (get it from the database library, and it is done automatically)
*
* @param path, filename, filetype,
$glued_table, default_visibility
* action: Adds an entry to the document table with the default settings.
* @author Olivier Cauberghe <olivier.cauberghe@ugent.be>
* @author Roan Embrechts
* @version 1.2
*/
function set_default_settings($upload_path, $filename, $filetype = 'file', $glued_table, $default_visibility = 'v')
{
if (!$default_visibility) $default_visibility = 'v';
// Make sure path is not wrongly formed
$upload_path = !empty($upload_path) ? "/$upload_path" : '';
$endchar = substr($filename, strlen($filename) - 1, 1);
if ($endchar == "\\" || $endchar == '/') {
$filename = substr($filename, 0, strlen($filename) - 1);
}
$full_file_name = $upload_path.'/'.$filename;
$full_file_name = str_replace("//", '/', $full_file_name);
$sql_query = "SELECT count(*) as number_existing FROM $glued_table WHERE path='$full_file_name'";
$sql_result = Database::query($sql_query);
$result = Database::fetch_array($sql_result);
// Determine which query to execute
if ($result['number_existing'] > 0) {
// Entry exists, update
$query = "UPDATE $glued_table SET path='$full_file_name',visibility='$default_visibility', filetype='$filetype'
WHERE path='$full_file_name'";
} else {
// No entry exists, create new one
$query = "INSERT INTO $glued_table (path,visibility,filetype)
VALUES ('$full_file_name','$default_visibility','$filetype')";
}
Database::query($query);
}
}

@ -19,7 +19,8 @@
* @param string $file_name Name of a file
* @return string the filename phps'ized
*/
function php2phps($file_name) {
function php2phps($file_name)
{
return preg_replace('/\.(php.?|phtml.?)(\.){0,1}.*$/i', '.phps', $file_name);
}
@ -29,7 +30,8 @@ function php2phps($file_name) {
* @param string $filename
* @return string
*/
function htaccess2txt($filename) {
function htaccess2txt($filename)
{
return str_replace(array('.htaccess', '.HTACCESS'), array('htaccess.txt', 'htaccess.txt'), $filename);
}
@ -47,36 +49,14 @@ function disable_dangerous_file($filename)
return htaccess2txt(php2phps($filename));
}
/**
* This function generates a unique name for a file on a given location
* file names are changed to name_#.ext
*
* @param string $path
* @param string $name
*
* @deprecated
*
* @return string new unique name
*/
function unique_name($path, $name)
{
$ext = substr(strrchr($name, '.'), 0);
$name_no_ext = substr($name, 0, strlen($name) - strlen(strstr($name, $ext)));
$n = 0;
$unique = '';
while (file_exists($path . $name_no_ext . $unique . $ext)) {
$unique = '_' . ++$n;
}
return $name_no_ext . $unique . $ext;
}
/**
* Returns the name without extension, used for the title
*
* @param string $name
* @return name without the extension
*/
function get_document_title($name) {
function get_document_title($name)
{
// If they upload .htaccess...
$name = disable_dangerous_file($name);
$ext = substr(strrchr($name, '.'), 0);
@ -97,13 +77,16 @@ function process_uploaded_file($uploaded_file, $show_output = true)
case 1:
// The uploaded file exceeds the upload_max_filesize directive in php.ini.
if ($show_output) {
Display::display_error_message(get_lang('UplExceedMaxServerUpload').ini_get('upload_max_filesize'));
Display::display_error_message(
get_lang('UplExceedMaxServerUpload').ini_get('upload_max_filesize')
);
}
return false;
case 2:
// The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
// Not used at the moment, but could be handy if we want to limit the size of an upload (e.g. image upload in html editor).
// Not used at the moment, but could be handy if we want to limit the size of an upload
// (e.g. image upload in html editor).
$max_file_size = intval($_POST['MAX_FILE_SIZE']);
if ($show_output) {
Display::display_error_message(get_lang('UplExceedMaxPostSize'). format_file_size($max_file_size));
@ -420,7 +403,8 @@ function handle_uploaded_document(
$courseInfo
);
} else {
// There might be cases where the file exists on disk but there is no registration of that in the database
// There might be cases where the file exists on disk but there is no registration of
// that in the database
// In this case, and if we are in overwrite mode, overwrite and create the db record
$documentId = add_document(
$courseInfo,
@ -504,7 +488,10 @@ function handle_uploaded_document(
item_property_update_on_folder($courseInfo, $uploadPath, $userId);
// Display success message to user
if ($output) {
Display::display_confirmation_message(get_lang('UplUploadSucceeded').'<br /> '.$documentTitle, false);
Display::display_confirmation_message(
get_lang('UplUploadSucceeded').'<br /> '.$documentTitle,
false
);
}
return $filePath;
@ -517,9 +504,8 @@ function handle_uploaded_document(
return false;
}
break;
// Rename the file if it exists
case 'rename':
// Rename the file if it exists
// Always rename.
$cleanName = DocumentManager::getUniqueFileName(
$uploadPath,
@ -886,54 +872,6 @@ function add_ext_on_mime($file_name, $file_type)
return $file_name;
}
/**
*
* @author Hugues Peeters <hugues.peeters@claroline.net>
*
* @param array $uploaded_file - follows the $_FILES Structure
* @param string $base_work_dir - base working directory of the module
* @param string $upload_path - destination of the upload.
* This path is to append to $base_work_dir
* @param int $max_filled_space - amount of bytes to not exceed in the base
* working directory
*
* @return boolean true if it succeds, false otherwise
*/
function treat_uploaded_file($uploaded_file, $base_work_dir, $upload_path, $max_filled_space, $uncompress = '')
{
$uploaded_file['name'] = stripslashes($uploaded_file['name']);
if (!enough_size($uploaded_file['size'], $base_work_dir, $max_filled_space)) {
Display::addFlash(
Display::return_message(get_lang('NoSpace'))
);
return false;
}
if ($uncompress == 'unzip' && preg_match('/.zip$/', strtolower($uploaded_file['name']))) {
return unzip_uploaded_file($uploaded_file, $upload_path, $base_work_dir, $max_filled_space);
} else {
$file_name = trim($uploaded_file['name']);
// CHECK FOR NO DESIRED CHARACTERS
$file_name = api_replace_dangerous_char($file_name);
// TRY TO ADD AN EXTENSION TO FILES WITOUT EXTENSION
$file_name = add_ext_on_mime($file_name, $uploaded_file['type']);
// HANDLE PHP FILES
$file_name = ($file_name);
// COPY THE FILE TO THE DESIRED DESTINATION
if (move_uploaded_file($uploaded_file['tmp_name'], $base_work_dir.$upload_path.'/'.$file_name)) {
set_default_settings($upload_path, $file_name);
}
return true;
}
}
/**
* Manages all the unzipping process of an uploaded file
*
@ -1311,10 +1249,10 @@ function add_document(
function update_existing_document($_course, $documentId, $filesize, $readonly = 0)
{
$document_table = Database::get_course_table(TABLE_DOCUMENT);
$documentId = intval($documentId);
$filesize = intval($filesize);
$readonly = intval($readonly);
$course_id = $_course['real_id'];
$documentId = intval($documentId);
$filesize = intval($filesize);
$readonly = intval($readonly);
$course_id = $_course['real_id'];
$sql = "UPDATE $document_table SET
size = '$filesize',
@ -1397,7 +1335,6 @@ function get_levels($filename) {
/**
* Adds file to document table in database
* deprecated: use file_set_default_settings instead
*
* @author Olivier Cauberghe <olivier.cauberghe@ugent.be>
* @param path,filename
@ -1497,7 +1434,6 @@ function search_img_from_html($html_file) {
/**
* Creates a new directory trying to find a directory name
* that doesn't already exist
* (we could use unique_name() here...)
*
* @author Hugues Peeters <hugues.peeters@claroline.net>
* @author Bert Vanderkimpen
@ -2075,6 +2011,7 @@ function add_all_documents_in_folder_to_database(
$parent = array()
) {
if (empty($userInfo) || empty($courseInfo)) {
return false;
}
@ -2082,7 +2019,6 @@ function add_all_documents_in_folder_to_database(
// Open dir
$handle = opendir($folderPath);
$files = array();
if (is_dir($folderPath)) {
// Run trough

@ -546,9 +546,13 @@ class aicc extends learnpath
}
}
@rename($course_sys_dir.$new_dir.$file, $course_sys_dir.$new_dir.$safe_file);
if ($this->debug == 1) { error_log('New LP - Renaming '.$course_sys_dir.$new_dir.$file.' to '.$course_sys_dir.$new_dir.$safe_file, 0); }
if ($this->debug == 1) {
error_log(
'New LP - Renaming '.$course_sys_dir.$new_dir.$file.' to '.$course_sys_dir.$new_dir.$safe_file,
0
);
}
}
//set_default_settings($course_sys_dir, $safe_file, $filetype);
}
}

@ -690,7 +690,6 @@ function build_work_move_to_selector($folders, $curdirpath, $move_file, $group_d
/**
* creates a new directory trying to find a directory name
* that doesn't already exist
* (we could use unique_name() here...)
*
* @author Hugues Peeters <hugues.peeters@claroline.net>
* @author Bert Vanderkimpen

@ -36,7 +36,7 @@ class TestFileManager extends UnitTestCase {
public function testUpdatedbInfo(){
$action ='';
$oldPath ='';
$res = update_db_info($action, $oldPath, $newPath="");
$res = DocumentManager::updateDbInfo($action, $oldPath, $newPath="");
$this->assertNull($res);
//var_dump($res);
}
@ -120,41 +120,4 @@ class TestFileManager extends UnitTestCase {
$this->assertTrue($res ===0);
//var_dump($res);
}
public function testListAllDirectories(){
$path=api_get_path(SYS_COURSE_PATH).'document/';
$res = $this->fmanager->list_all_directories($path);
if(!is_null($res)) {
$this->assertTrue($res);
$this->assertTrue(is_array($res));
}
//var_dump($res);
}
public function testListAllFiles(){
$dirArray = array('COURSETEST, document, images');
$res = $this->fmanager->list_all_files($dirArray);
$this->assertFalse($res);
$this->assertTrue(is_array($res));
$this->assertTrue($res === array());
//var_dump($res);
}
public function testCompatLoadFile(){
$file_name='README.txt';
$res = $this->fmanager->compat_load_file($file_name);
$this->assertTrue(is_string($res));
//var_dump($res);
}
public function testSetDefaultSettings(){
global $_course, $_configuration;
$upload_path=api_get_path(SYS_COURSE_PATH);
$filename='index.html';
$glue_table = $_course['dbName'].'.document';
$res = $this->fmanager->set_default_settings($upload_path, $filename, $filetype="file", $glue_table, $default_visibility='v');
$this->assertNull($res);
//var_dump($res);
}
}
?>

@ -42,7 +42,7 @@ class TestFileManager extends UnitTestCase {
public function testUpdatedbInfo(){
$action ='';
$oldPath ='';
$res = update_db_info($action, $oldPath, $newPath="");
$res = DocumentManager::updateDbInfo($action, $oldPath, $newPath="");
$this->assertTrue(is_null($res));
//var_dump($res);
}
@ -126,42 +126,4 @@ class TestFileManager extends UnitTestCase {
$this->assertTrue($res ===0);
//var_dump($res);
}
public function testListAllDirectories(){
$path='/var/www/path/';
$res = $this->fmanager->list_all_directories($path);
$this->assertFalse($res);
$this->assertTrue(is_null($res));
//var_dump($res);
}
public function testListAllFiles(){
$dirArray='documents';
$res = $this->fmanager->list_all_files($dirArray);
$this->assertFalse($res);
$this->assertTrue(is_null($res));
$this->assertTrue($res ===null);
//var_dump($res);
}
public function testCompatLoadFile(){
$file_name='images';
$res = $this->fmanager->compat_load_file($file_name);
$this->assertFalse($res);
$this->assertTrue(is_bool($res));
//var_dump($res);
}
public function testSetDefaultSettings(){
$upload_path='/var/www/path/ruta';
$filename='doc';
$glued_table='xxx';
$res = $this->fmanager->set_default_settings($upload_path, $filename, $filetype="file", $glued_table, $default_visibility='v');
$this->assertTrue(is_null($res));
//var_dump($res);
}
}
?>

@ -3,379 +3,405 @@ require_once(api_get_path(LIBRARY_PATH).'fileUpload.lib.php');
require_once(api_get_path(LIBRARY_PATH).'document.lib.php');
require_once(api_get_path(LIBRARY_PATH).'pclzip/pclzip.lib.php');
class TestFileUpload extends UnitTestCase {
class TestFileUpload extends UnitTestCase
{
public function __construct(){
public function __construct()
{
$this->UnitTestCase('File upload library - main/inc/lib/fileUpload.lib.test.php');
}
/*
//Deprecated
//settings
function testset_default_settings() {
global $dbTable,$_configuration;
global $default_visibility;
$upload_path = api_get_path(SYS_PATH).'courses';
$filename = 'test.txt';
$res= set_default_settings($upload_path,$filename,$filetype="file");
$this->assertTrue(is_null($res));
//var_dump($res);
}
*/
//Creating
function testCreateUnexistingDirectory() {
global $_course, $user_id;
$to_group_id = 1;
$to_user_id = 1;
$base_work_dir = api_get_path(SYS_COURSE_PATH).'document/';
$desired_dir_name = 'images';
$res= create_unexisting_directory($_course,$user_id,$to_group_id,$to_user_id,$base_work_dir,$desired_dir_name);
if(!is_null($res)) {
$this->assertTrue(is_bool($res));
$this->assertFalse($res);
}
}
/**
* Creates a file containing an html redirection to a given url
* @param string $filePath
* @param string $url
* @return void
*/
function testCreateLinkFile() {
$base_work_dir=api_get_path(SYS_COURSE_PATH);
$filePath = $base_work_dir.'upload/blog';
$dirurl = api_get_path(WEB_COURSE_PATH);
$url = $dirurl.'doc.php';
$res= create_link_file($filePath, $url);
if (!is_numeric($res)) {
$this->assertFalse($res);
} else {
$this->assertTrue($res);
}
}
/**
* This recursive function can be used during the upgrade process form older versions of Dokeos
* It crawls the given directory, checks if the file is in the DB and adds it if it's not
*
* @param string $base_work_dir
* @param string $current_path, needed for recursivity
*/
function testAddAllDocumentsInFolderToDatabase() {
global $_course, $user_id;
$base_work_dir= api_get_path(SYS_PATH).'';
$current_path = 'courses/';
$to_group_id =0;
$res=add_all_documents_in_folder_to_database($_course,$user_id,$base_work_dir,$current_path,$to_group_id);
$this->assertTrue(is_null($res));
//var_dump($res);
}
function testAddDocument() {
global $_course;
$path='';
$filetype='';
$filesize='';
$title='';
$res=add_document($_course,$path,$filetype,$filesize,$title);
if(!is_numeric($res)) :
$this->assertTrue(is_bool($res));
endif;
//var_dump($res);
}
function testAddExtOnMime() {
$fileName='';
$fileType='';
$res=add_ext_on_mime($fileName,$fileType);
$this->assertTrue(is_string($res));
//var_dump($res);
}
//build
function testBuildMissingFilesForm() {
global $_course;
$missing_files = array();
$upload_path=api_get_path(SYS_CODE_PATH).'default_course_document/images';
$file_name = 'board.jpg';
$res=build_missing_files_form($missing_files,$upload_path,$file_name);
$this->assertTrue(is_string($res));
//var_dump($res);
}
//check
function testCheckForMissingFiles() {
$file='';
$res=check_for_missing_files($file);
$this->assertTrue(is_bool($res));
}
//space
function testDirTotalSpace() {
$dirPath= api_get_path(SYS_CODE_PATH).'default_course_document/images';
$res= dir_total_space($dirPath);
$this->assertTrue($res>0,'The default_course_document/images dir should be larger than 0 bytes');
}
//filter
function testfilter_extension() {
$filename='index.php';
$res= filter_extension($filename);
$this->assertTrue(is_numeric($res));
}
//get
function testget_document_title() {
$name='';
$res= get_document_title($name);
$this->assertTrue(is_string($res));
//var_dump($res);
}
function testget_levels() {
$filename='readme.txt';
$res= get_levels($filename);
$this->assertTrue(is_numeric($res));
//var_dump($res);
}
//access
function testhtaccess2txt() {
$filename = 'readme.txt';
$res= htaccess2txt($filename);
$this->assertTrue(is_string($res));
//var_dump($res);
}
//moving
function testmove_uploaded_file_collection_into_directory() {
global $_course;
$uploaded_file_collection=array();
$base_work_dir=api_get_path(SYS_COURSE_PATH).'upload/';
$missing_files_dir='';
$user_id=1;
$to_group_id='';
$to_user_id='';
$max_filled_space='';
$res= move_uploaded_file_collection_into_directory($_course, $uploaded_file_collection, $base_work_dir, $missing_files_dir,$user_id,$to_group_id,$to_user_id,$max_filled_space);
$this->assertTrue(is_null($res));
//var_dump($res);
}
//php?
function testphp2phps () {
$fileName = 'index.php';
$res= php2phps($fileName);
$this->assertTrue(is_string($res));
//var_dump($res);
}
//searching
function testsearch_img_from_html() {
$imgFilePath = array();
$htmlFile= 'file:///var/www/chamilotest/documentation/credits.html';
$res= search_img_from_html($htmlFile);
if(is_array($res)){
$this->assertTrue(is_array($res));
} else {
$this->assertTrue(is_null($res));
}
//var_dump($res);
}
//uploading
function testprocess_uploaded_file() {
$uploaded_file='';
ob_start();
$res= process_uploaded_file($uploaded_file);
ob_end_clean();
$this->assertTrue(is_bool($res));
}
function testtreat_uploaded_file() {
$uploadedFile['name']=stripslashes($uploadedFile['name']);
$uploadedFile='';
$baseWorkDir='';
$uploadPath='';
$maxFilledSpace='';
$uncompress= '';
$res= treat_uploaded_file($uploadedFile, $baseWorkDir, $uploadPath, $maxFilledSpace, $uncompress= '');
$this->assertTrue(is_bool($res));
//var_dump($res);
}
function testhandle_uploaded_document() {
global $_course;
$uploaded_file='';
$base_work_dir='';
$upload_path='';
$user_id=1;
$to_group_id=0;
$to_user_id=NULL;
$unzip=0;
$what_if_file_exists='';
$output=true;
$clean_name = disable_dangerous_file($clean_name);
$where_to_save = $base_work_dir.$upload_path;
$new_name = unique_name($where_to_save, $clean_name);
$new_file_path = $upload_path.$new_name;
$clean_name = disable_dangerous_file($clean_name);
$file_path = $upload_path.$clean_name;
ob_start();
$res= handle_uploaded_document($_course,$uploaded_file,$base_work_dir,$upload_path,$user_id,$to_group_id=0,$to_user_id=NULL, $unzip=0,$what_if_file_exists='',$output=true);
$this->assertTrue(is_null($res));
ob_end_clean();
//var_dump($res);
}
//updating
function testupdate_existing_document() {
$_course='';
$document_id='';
$filesize='';
$res= update_existing_document($_course,$document_id,$filesize,$readonly=0);
$this->assertTrue(is_bool($res));
//var_dump($res);
}
function testitem_property_update_on_folder() {
global $_course, $user_id;
$path=api_get_path(SYS_COURSE_PATH).'document/license.txt';
$res= item_property_update_on_folder($_course,$path,$user_id);
$this->assertTrue(is_null($res));
//var_dump($res);
}
//unique name
function testunique_name() {
$path='';
$name='';
$res= unique_name($path,$name);
$this->assertTrue(is_string($res));
//var_dump($res);
}
//unzipping
function testunzip_uploaded_document() {
//require_once(api_get_path(LIBRARY_PATH).'pclzip/pclzip.lib.php');
global $_course;
global $_user;
global $to_user_id;
global $to_group_id;
$uploaded_file='';
$upload_path='';
$base_work_dir='';
$max_filled_space='';
ob_start();
$res= unzip_uploaded_document($uploaded_file, $upload_path, $base_work_dir, $max_filled_space, $output = true, $to_group_id=0);
ob_end_clean();
$this->assertTrue(is_bool($res));
//var_dump($res);
}
function testunzip_uploaded_file() {
global $_course;
$uploadedFile = 'README.txt';
$uploadPath = api_get_path(SYS_PATH).$_course.'/document';
$baseWorkDir = api_get_path(SYS_PATH);
$maxFilledSpace = 1000;
$res= unzip_uploaded_file($uploadedFile, $uploadPath, $baseWorkDir, $maxFilledSpace);
$this->assertTrue(is_bool($res));
//var_dump($res);
}
//disable
function testDisableDangerousFile() {
$filename = 'index.php';
$res= disable_dangerous_file($filename);
$this->assertTrue(is_string($res));
//var_dump($res);
}
//replace
function testreplace_img_path_in_html_file() {
global $_course;
$originalImgPath='';
$newImgPath='';
$htmlFile='file:///var/www/chamilotest/documentation/credits.html';
$res= replace_img_path_in_html_file($originalImgPath, $newImgPath, $htmlFile);
$this->assertTrue(is_null($res));
//var_dump($res);
}
function testApiReplaceLinksInHtml() {
$base_work_dir=api_get_path(SYS_COURSE_PATH);
$upload_path=$base_work_dir.'upload/blog';
$full_file_name = 'doc.php';
$res=api_replace_links_in_html($upload_path,$full_file_name);
$this->assertTrue(is_null($res));
//var_dump($res);
}
function testApiReplaceLinksInString() {
$base_work_dir=api_get_path(SYS_COURSE_PATH);
$upload_path=$base_work_dir.'upload/blog';
$buffer=ob_get_contents();
$res=api_replace_links_in_string($upload_path,$buffer);
$this->assertTrue(is_string($res));
//var_dump($res);
}
function testApiReplaceParameter() {
$count = 0;
$matches = array();
$href_list=array();
$file_path_list[] = $matches[1];
$base_work_dir=api_get_path(SYS_COURSE_PATH);
$upload_path=$base_work_dir.'upload/blog';
$replaceWhat[$count] = $href_list[$count];
/** To can test this function you need to comment "die ('can not create file')"
* $res return void
*/
$replaceBy[$count] = " $param_name=\"" . $file_path_list[$count] . "\" target =\"_top\"";
$replaceBy[$count] = $replaceWhat[$count];
$buffer = str_replace($replaceWhat, $replaceBy, $buffer);
$param_name="src";
$res=api_replace_parameter($upload_path, $buffer, $param_name="src");
$this->assertTrue(is_string($res));
//var_dump($res);
}
//clenaning
function testCleanUpFilesInZip() {
$p_event='';
$p_header['filename']='';
$res=clean_up_files_in_zip($p_event, &$p_header);
$this->assertTrue(is_numeric($res));
//var_dump($res);
}
function testCleanUpPath() {
$base_work_dir=api_get_path(SYS_COURSE_PATH);
$path = $base_work_dir.'upload/blog';
$res = clean_up_path($path);
$this->assertTrue(is_numeric($res));
}
}
?>
//Creating
function testCreateUnexistingDirectory()
{
global $_course, $user_id;
$to_group_id = 1;
$to_user_id = 1;
$base_work_dir = api_get_path(SYS_COURSE_PATH).'document/';
$desired_dir_name = 'images';
$res = create_unexisting_directory(
$_course,
$user_id,
$to_group_id,
$to_user_id,
$base_work_dir,
$desired_dir_name
);
if (!is_null($res)) {
$this->assertTrue(is_bool($res));
$this->assertFalse($res);
}
}
/**
* Creates a file containing an html redirection to a given url
* @param string $filePath
* @param string $url
* @return void
*/
function testCreateLinkFile()
{
$base_work_dir = api_get_path(SYS_COURSE_PATH);
$filePath = $base_work_dir.'upload/blog';
$dirurl = api_get_path(WEB_COURSE_PATH);
$url = $dirurl.'doc.php';
$res = create_link_file($filePath, $url);
if (!is_numeric($res)) {
$this->assertFalse($res);
} else {
$this->assertTrue($res);
}
}
/**
* This recursive function can be used during the upgrade process form older versions of Dokeos
* It crawls the given directory, checks if the file is in the DB and adds it if it's not
*
* @param string $base_work_dir
* @param string $current_path , needed for recursivity
*/
function testAddAllDocumentsInFolderToDatabase()
{
global $_course, $user_id;
$base_work_dir = api_get_path(SYS_PATH).'';
$current_path = 'courses/';
$to_group_id = 0;
$res = add_all_documents_in_folder_to_database($_course, $user_id, $base_work_dir, $current_path, $to_group_id);
$this->assertTrue(is_null($res));
//var_dump($res);
}
function testAddDocument()
{
global $_course;
$path = '';
$filetype = '';
$filesize = '';
$title = '';
$res = add_document($_course, $path, $filetype, $filesize, $title);
if (!is_numeric($res)) :
$this->assertTrue(is_bool($res));
endif;
//var_dump($res);
}
function testAddExtOnMime()
{
$fileName = '';
$fileType = '';
$res = add_ext_on_mime($fileName, $fileType);
$this->assertTrue(is_string($res));
//var_dump($res);
}
//build
function testBuildMissingFilesForm()
{
global $_course;
$missing_files = array();
$upload_path = api_get_path(SYS_CODE_PATH).'default_course_document/images';
$file_name = 'board.jpg';
$res = build_missing_files_form($missing_files, $upload_path, $file_name);
$this->assertTrue(is_string($res));
//var_dump($res);
}
//check
function testCheckForMissingFiles()
{
$file = '';
$res = check_for_missing_files($file);
$this->assertTrue(is_bool($res));
}
//space
function testDirTotalSpace()
{
$dirPath = api_get_path(SYS_CODE_PATH).'default_course_document/images';
$res = dir_total_space($dirPath);
$this->assertTrue($res > 0, 'The default_course_document/images dir should be larger than 0 bytes');
}
//filter
function testfilter_extension()
{
$filename = 'index.php';
$res = filter_extension($filename);
$this->assertTrue(is_numeric($res));
}
//get
function testget_document_title()
{
$name = '';
$res = get_document_title($name);
$this->assertTrue(is_string($res));
//var_dump($res);
}
function testget_levels()
{
$filename = 'readme.txt';
$res = get_levels($filename);
$this->assertTrue(is_numeric($res));
//var_dump($res);
}
//access
function testhtaccess2txt()
{
$filename = 'readme.txt';
$res = htaccess2txt($filename);
$this->assertTrue(is_string($res));
//var_dump($res);
}
//moving
function testmove_uploaded_file_collection_into_directory()
{
global $_course;
$uploaded_file_collection = array();
$base_work_dir = api_get_path(SYS_COURSE_PATH).'upload/';
$missing_files_dir = '';
$user_id = 1;
$to_group_id = '';
$to_user_id = '';
$max_filled_space = '';
$res = move_uploaded_file_collection_into_directory(
$_course,
$uploaded_file_collection,
$base_work_dir,
$missing_files_dir,
$user_id,
$to_group_id,
$to_user_id,
$max_filled_space
);
$this->assertTrue(is_null($res));
//var_dump($res);
}
//php?
function testphp2phps()
{
$fileName = 'index.php';
$res = php2phps($fileName);
$this->assertTrue(is_string($res));
//var_dump($res);
}
//searching
function testsearch_img_from_html()
{
$imgFilePath = array();
$htmlFile = 'file:///var/www/chamilotest/documentation/credits.html';
$res = search_img_from_html($htmlFile);
if (is_array($res)) {
$this->assertTrue(is_array($res));
} else {
$this->assertTrue(is_null($res));
}
//var_dump($res);
}
//uploading
function testprocess_uploaded_file()
{
$uploaded_file = '';
ob_start();
$res = process_uploaded_file($uploaded_file);
ob_end_clean();
$this->assertTrue(is_bool($res));
}
function testhandle_uploaded_document()
{
global $_course;
$uploaded_file = '';
$base_work_dir = '';
$upload_path = '';
$user_id = 1;
$to_group_id = 0;
$to_user_id = null;
$unzip = 0;
$what_if_file_exists = '';
$output = true;
$clean_name = disable_dangerous_file($clean_name);
$where_to_save = $base_work_dir.$upload_path;
$new_file_path = $upload_path.$clean_name;
$clean_name = disable_dangerous_file($clean_name);
$file_path = $upload_path.$clean_name;
ob_start();
$res = handle_uploaded_document(
$_course,
$uploaded_file,
$base_work_dir,
$upload_path,
$user_id,
$to_group_id = 0,
$to_user_id = null,
$unzip = 0,
$what_if_file_exists = '',
$output = true
);
$this->assertTrue(is_null($res));
ob_end_clean();
//var_dump($res);
}
//updating
function testupdate_existing_document()
{
$_course = '';
$document_id = '';
$filesize = '';
$res = update_existing_document($_course, $document_id, $filesize, $readonly = 0);
$this->assertTrue(is_bool($res));
//var_dump($res);
}
function testitem_property_update_on_folder()
{
global $_course, $user_id;
$path = api_get_path(SYS_COURSE_PATH).'document/license.txt';
$res = item_property_update_on_folder($_course, $path, $user_id);
$this->assertTrue(is_null($res));
//var_dump($res);
}
//unzipping
function testunzip_uploaded_document()
{
//require_once(api_get_path(LIBRARY_PATH).'pclzip/pclzip.lib.php');
global $_course;
global $_user;
global $to_user_id;
global $to_group_id;
$uploaded_file = '';
$upload_path = '';
$base_work_dir = '';
$max_filled_space = '';
ob_start();
$res = unzip_uploaded_document(
$uploaded_file,
$upload_path,
$base_work_dir,
$max_filled_space,
$output = true,
$to_group_id = 0
);
ob_end_clean();
$this->assertTrue(is_bool($res));
//var_dump($res);
}
function testunzip_uploaded_file()
{
global $_course;
$uploadedFile = 'README.txt';
$uploadPath = api_get_path(SYS_PATH).$_course.'/document';
$baseWorkDir = api_get_path(SYS_PATH);
$maxFilledSpace = 1000;
$res = unzip_uploaded_file($uploadedFile, $uploadPath, $baseWorkDir, $maxFilledSpace);
$this->assertTrue(is_bool($res));
//var_dump($res);
}
//disable
function testDisableDangerousFile()
{
$filename = 'index.php';
$res = disable_dangerous_file($filename);
$this->assertTrue(is_string($res));
//var_dump($res);
}
//replace
function testreplace_img_path_in_html_file()
{
global $_course;
$originalImgPath = '';
$newImgPath = '';
$htmlFile = 'file:///var/www/chamilotest/documentation/credits.html';
$res = replace_img_path_in_html_file($originalImgPath, $newImgPath, $htmlFile);
$this->assertTrue(is_null($res));
//var_dump($res);
}
function testApiReplaceLinksInHtml()
{
$base_work_dir = api_get_path(SYS_COURSE_PATH);
$upload_path = $base_work_dir.'upload/blog';
$full_file_name = 'doc.php';
$res = api_replace_links_in_html($upload_path, $full_file_name);
$this->assertTrue(is_null($res));
//var_dump($res);
}
function testApiReplaceLinksInString()
{
$base_work_dir = api_get_path(SYS_COURSE_PATH);
$upload_path = $base_work_dir.'upload/blog';
$buffer = ob_get_contents();
$res = api_replace_links_in_string($upload_path, $buffer);
$this->assertTrue(is_string($res));
//var_dump($res);
}
function testApiReplaceParameter()
{
$count = 0;
$matches = array();
$href_list = array();
$file_path_list[] = $matches[1];
$base_work_dir = api_get_path(SYS_COURSE_PATH);
$upload_path = $base_work_dir.'upload/blog';
$replaceWhat[$count] = $href_list[$count];
/** To can test this function you need to comment "die ('can not create file')"
* $res return void
*/
$replaceBy[$count] = " $param_name=\"".$file_path_list[$count]."\" target =\"_top\"";
$replaceBy[$count] = $replaceWhat[$count];
$buffer = str_replace($replaceWhat, $replaceBy, $buffer);
$param_name = "src";
$res = api_replace_parameter($upload_path, $buffer, $param_name = "src");
$this->assertTrue(is_string($res));
//var_dump($res);
}
//clenaning
function testCleanUpFilesInZip()
{
$p_event = '';
$p_header['filename'] = '';
$res = clean_up_files_in_zip($p_event, &$p_header);
$this->assertTrue(is_numeric($res));
//var_dump($res);
}
function testCleanUpPath()
{
$base_work_dir = api_get_path(SYS_COURSE_PATH);
$path = $base_work_dir.'upload/blog';
$res = clean_up_path($path);
$this->assertTrue(is_numeric($res));
}
}
Loading…
Cancel
Save