1
0
mirror of https://github.com/e107inc/e107.git synced 2025-03-14 17:39:46 +01:00

Fixes #4268 - missing field in table definition. Tests added.

This commit is contained in:
Cameron 2020-11-29 14:05:14 -08:00
parent 35ad3e7284
commit 072c1b3a90
4 changed files with 480 additions and 118 deletions

View File

@ -22,12 +22,13 @@
*/ */
// DEPRECATED - USE db_verify_class where possible.
class db_table_admin class db_table_admin
{ {
var $file_buffer = ''; // Contents of a file protected $file_buffer = ''; // Contents of a file
var $last_file = ''; protected $last_file = '';
protected $errors = array();
// Get list of fields and keys for a table - return FALSE if unsuccessful // Get list of fields and keys for a table - return FALSE if unsuccessful
// Return as for get_table_def // Return as for get_table_def
@ -61,6 +62,12 @@ class db_table_admin
{ {
return "No matches"; return "No matches";
} }
if(isset($matches[0][2]) && is_string($matches[0][2]))
{
$matches[0][2] = trim($matches[0][2]);
}
return $matches; return $matches;
} }
@ -73,6 +80,7 @@ class db_table_admin
* *
* @param string $table_name - If specified, returns only that table's info; otherwise returns a list of all tables * @param string $table_name - If specified, returns only that table's info; otherwise returns a list of all tables
* The table name must include a prefix where appropriate (although not required with standard E107 table definition files) * The table name must include a prefix where appropriate (although not required with standard E107 table definition files)
* @param string $file_name
* @return string|array * @return string|array
* - if error, returns a brief text message * - if error, returns a brief text message
* - if successful, returns an array of table definitions, each of which is itself an array: * - if successful, returns an array of table definitions, each of which is itself an array:
@ -124,6 +132,8 @@ class db_table_admin
); );
$text = str_replace("\r", "\n", $text); $text = str_replace("\r", "\n", $text);
$field_lines = explode("\n", $text); $field_lines = explode("\n", $text);
$defs = array();
foreach ($field_lines as $fv) foreach ($field_lines as $fv)
{ {
unset($defs); unset($defs);
@ -148,7 +158,7 @@ class db_table_admin
case 'UNIQUE': case 'UNIQUE':
if (count($fd) < 3) if (count($fd) < 3)
{ {
echo "Truncated definition after UNIQUE {$i}: ".$fd[1]."<br />"; $this->errors[] = "Truncated definition after UNIQUE {$fv}: ".$fd[1]."<br />";
} }
elseif (strtoupper($fd[1]) == 'KEY') elseif (strtoupper($fd[1]) == 'KEY')
{ {
@ -159,14 +169,14 @@ class db_table_admin
} }
else else
{ {
echo "Unrecognised word after UNIQUE in definition {$i}: ".$fd[1]."<br />"; $this->errors[] = "Unrecognised word after UNIQUE in definition {$fv}: ".$fd[1]."<br />";
} }
break; break;
case 'FULLTEXT': case 'FULLTEXT':
if (count($fd) < 3) if (count($fd) < 3)
{ {
echo "Truncated definition after FULLTEXT {$i}: ".$fd[1]."<br />"; $this->errors[] = "Truncated definition after FULLTEXT {$fv}: ".$fd[1]."<br />";
} }
elseif (strtoupper($fd[1]) == 'KEY') elseif (strtoupper($fd[1]) == 'KEY')
{ {
@ -177,7 +187,7 @@ class db_table_admin
} }
else else
{ {
echo "Unrecognised word after FULLTEXT in definition {$i}: ".$fd[1]."<br />"; $this->errors[] = "Unrecognised word after FULLTEXT in definition {$fv}: ".$fd[1]."<br />";
} }
break; break;
@ -218,7 +228,7 @@ class db_table_admin
} }
else else
{ // Syntax error { // Syntax error
echo "Unrecognised word in definition {$i} after 'NOT': ".$fd[$i + 1]."<br />"; $this->errors[] = "Unrecognised word in definition {$i} after 'NOT': ".$fd[$i + 1]."<br />";
} }
break; break;
case 'DEFAULT': case 'DEFAULT':
@ -250,7 +260,7 @@ class db_table_admin
} }
else else
{ {
echo "Partial definition<br />"; $this->errors[] = "Partial definition<br />";
} }
} }
} }
@ -678,12 +688,12 @@ class db_table_admin
} }
return TRUE; // Success even if no changes required return TRUE; // Success even if no changes required
} }
return FALSE;
} }
function createTable($pathToSqlFile = '', $tableName = '', $addPrefix = true, $renameTable = '') function createTable($pathToSqlFile = '', $tableName = '', $addPrefix = true, $renameTable = '')
{ {
$e107 = e107::getInstance(); // $e107 = e107::getInstance();
$tmp = $this->get_table_def($tableName, $pathToSqlFile); $tmp = $this->get_table_def($tableName, $pathToSqlFile);
$createText = $tmp[0][0]; $createText = $tmp[0][0];
$newTableName = ($renameTable ? $renameTable : $tableName); $newTableName = ($renameTable ? $renameTable : $tableName);
@ -697,7 +707,77 @@ class db_table_admin
} }
return e107::getDb()->gen($createText); return e107::getDb()->gen($createText);
} }
/**
* Used by $sql->makeTableDef() to create an e_CACHE_DB.$tableName.'.php' file.
* @param array $fieldDefs
* @return array as returned by parse_field_defs()
*/
public function make_field_types($fieldDefs=array())
{
$outDefs = array();
foreach ($fieldDefs as $k => $v)
{
switch ($v['type'])
{
case 'field' :
// if (!empty($v['autoinc']))
// {
//break; Probably include autoinc fields in array
// }
$baseType = preg_replace('#\(.*?\)#', '', $v['fieldtype']); // Should strip any length
switch ($baseType)
{
case 'int' :
case 'integer':
case 'smallint':
case 'shortint' :
case 'tinyint' :
case 'mediumint':
case 'bigint':
$outDefs['_FIELD_TYPES'][$v['name']] = 'int';
break;
case 'char' :
case 'text' :
case 'varchar' :
case 'tinytext' :
case 'mediumtext' :
case 'longtext' :
case 'enum' :
$outDefs['_FIELD_TYPES'][$v['name']] = 'escape'; //XXX toDB() causes serious BC issues.
break;
}
// if($v['name'])
if (isset($v['nulltype']) && !isset($v['default']))
{
$outDefs['_NOTNULL'][$v['name']] = '';
}
break;
case 'pkey' :
case 'ukey' :
case 'key' :
case 'ftkey' :
break; // Do nothing with keys for now
default :
$this->errors[] = "Unexpected field type: {$k} => {$v['type']}<br />";
}
}
return $outDefs;
}
} }

View File

@ -2728,60 +2728,8 @@ class e_db_pdo implements e_db
$fieldDefs = $dbAdm->parse_field_defs($baseStruct); // Required definitions $fieldDefs = $dbAdm->parse_field_defs($baseStruct); // Required definitions
if (!$fieldDefs) return false; if (!$fieldDefs) return false;
$outDefs = array(); $outDefs = $dbAdm->make_field_types($fieldDefs);
foreach ($fieldDefs as $k => $v)
{
switch ($v['type'])
{
case 'field' :
if (vartrue($v['autoinc']))
{
//break; Probably include autoinc fields in array
}
$baseType = preg_replace('#\(\d+?\)#', '', $v['fieldtype']); // Should strip any length
switch ($baseType)
{
case 'int' :
case 'integer':
case 'smallint':
case 'shortint' :
case 'tinyint' :
case 'mediumint':
$outDefs['_FIELD_TYPES'][$v['name']] = 'int';
break;
case 'char' :
case 'text' :
case 'varchar' :
case 'tinytext' :
case 'mediumtext' :
case 'longtext' :
$outDefs['_FIELD_TYPES'][$v['name']] = 'escape'; //XXX toDB() causes serious BC issues.
break;
}
// if($v['name'])
if (isset($v['nulltype']) && !isset($v['default']))
{
$outDefs['_NOTNULL'][$v['name']] = '';
}
break;
case 'pkey' :
case 'ukey' :
case 'key' :
case 'ftkey' :
break; // Do nothing with keys for now
default :
echo "Unexpected field type: {$k} => {$v['type']}<br />";
}
}
// $array = e107::getArrayStorage();
$this->dbFieldDefs[$tableName] = $outDefs; $this->dbFieldDefs[$tableName] = $outDefs;
$toSave = e107::serialize($outDefs, false); // 2nd parameter to TRUE if needs to be written to DB $toSave = e107::serialize($outDefs, false); // 2nd parameter to TRUE if needs to be written to DB

View File

@ -1093,7 +1093,7 @@ class e_db_mysql implements e_db
case 'array': case 'array':
if(is_array($fieldValue)) if(is_array($fieldValue))
{ {
return "'".e107::getArrayStorage()->writeArray($fieldValue, true)."'"; return "'".e107::getArrayStorage()->WriteArray($fieldValue, true)."'";
} }
return "'". (string) $fieldValue."'"; return "'". (string) $fieldValue."'";
break; break;
@ -2565,59 +2565,8 @@ class e_db_mysql implements e_db
$fieldDefs = $dbAdm->parse_field_defs($baseStruct); // Required definitions $fieldDefs = $dbAdm->parse_field_defs($baseStruct); // Required definitions
if (!$fieldDefs) return false; if (!$fieldDefs) return false;
$outDefs = array(); $outDefs = $dbAdm->make_field_types($fieldDefs);
foreach ($fieldDefs as $k => $v)
{
switch ($v['type'])
{
case 'field' :
if (vartrue($v['autoinc']))
{
//break; Probably include autoinc fields in array
}
$baseType = preg_replace('#\(\d+?\)#', '', $v['fieldtype']); // Should strip any length
switch ($baseType)
{
case 'int' :
case 'integer':
case 'shortint' :
case 'tinyint' :
case 'mediumint':
$outDefs['_FIELD_TYPES'][$v['name']] = 'int';
break;
case 'char' :
case 'text' :
case 'varchar' :
case 'tinytext' :
case 'mediumtext' :
case 'longtext' :
$outDefs['_FIELD_TYPES'][$v['name']] = 'escape'; //XXX toDB() causes serious BC issues.
break;
}
// if($v['name'])
if (isset($v['nulltype']) && !isset($v['default']))
{
$outDefs['_NOTNULL'][$v['name']] = '';
}
break;
case 'pkey' :
case 'ukey' :
case 'key' :
case 'ftkey' :
break; // Do nothing with keys for now
default :
echo "Unexpected field type: {$k} => {$v['type']}<br />";
}
}
// $array = e107::getArrayStorage();
$this->dbFieldDefs[$tableName] = $outDefs; $this->dbFieldDefs[$tableName] = $outDefs;
$toSave = e107::serialize($outDefs, false); // 2nd parameter to TRUE if needs to be written to DB $toSave = e107::serialize($outDefs, false); // 2nd parameter to TRUE if needs to be written to DB

View File

@ -0,0 +1,385 @@
<?php
/**
* e107 website system
*
* Copyright (C) 2008-2020 e107 Inc (e107.org)
* Released under the terms and conditions of the
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
*
*/
class db_table_adminTest extends \Codeception\Test\Unit
{
/** @var db_table_admin */
protected $dta;
protected function _before()
{
try
{
$this->dta = $this->make('db_table_admin');
}
catch(Exception $e)
{
$this->assertTrue(false, "Couldn't load db_table_admin object");
}
}
/*
public function testCompare_field_lists()
{
}*/
public function testParse_field_defs()
{
$baseStruct = $this->dta->get_current_table('core');
$baseStruct = isset($baseStruct[0][2]) ? $baseStruct[0][2] : null;
$result = $this->dta->parse_field_defs($baseStruct);
$expected = array (
0 => array (
'type' => 'field',
'name' => 'e107_name',
'fieldtype' => 'varchar(100)',
'nulltype' => 'NOT NULL',
'default' => '\'\'',
),
1 => array (
'type' => 'field',
'name' => 'e107_value',
'fieldtype' => 'text',
'nulltype' => 'NOT NULL',
),
2 => array (
'type' => 'pkey',
'name' => '(e107_name)',
),
);
$this->assertSame($expected,$result);
$test2 = "userjournals_id int(10) unsigned NOT NULL auto_increment,
userjournals_userid int(10) unsigned NOT NULL default '0',
userjournals_subject varchar(64) NOT NULL default '',
userjournals_categories varchar(100) NOT NULL default '',
userjournals_playing varchar(50) NOT NULL default '',
userjournals_mood enum('','happy','sad','alienated','beat_up','angry','annoyed') NOT NULL default 'happy',
userjournals_entry longtext NOT NULL,
userjournals_date varchar(64) NOT NULL default '',
userjournals_timestamp varchar(32) NOT NULL default '',
userjournals_is_comment int(1) NOT NULL default '0',
PRIMARY KEY (userjournals_id)";
$result2 = $this->dta->parse_field_defs($test2);
$expected = array (
0 =>
array (
'type' => 'field',
'name' => 'userjournals_id',
'fieldtype' => 'int(10)',
'vartype' => 'unsigned',
'nulltype' => 'NOT NULL',
'autoinc' => true,
),
1 =>
array (
'type' => 'field',
'name' => 'userjournals_userid',
'fieldtype' => 'int(10)',
'vartype' => 'unsigned',
'nulltype' => 'NOT NULL',
'default' => '\'0\'',
),
2 =>
array (
'type' => 'field',
'name' => 'userjournals_subject',
'fieldtype' => 'varchar(64)',
'nulltype' => 'NOT NULL',
'default' => '\'\'',
),
3 =>
array (
'type' => 'field',
'name' => 'userjournals_categories',
'fieldtype' => 'varchar(100)',
'nulltype' => 'NOT NULL',
'default' => '\'\'',
),
4 =>
array (
'type' => 'field',
'name' => 'userjournals_playing',
'fieldtype' => 'varchar(50)',
'nulltype' => 'NOT NULL',
'default' => '\'\'',
),
5 =>
array (
'type' => 'field',
'name' => 'userjournals_mood',
'fieldtype' => 'enum(\'\',\'happy\',\'sad\',\'alienated\',\'beat_up\',\'angry\',\'annoyed\')',
'nulltype' => 'NOT NULL',
'default' => '\'happy\'',
),
6 =>
array (
'type' => 'field',
'name' => 'userjournals_entry',
'fieldtype' => 'longtext',
'nulltype' => 'NOT NULL',
),
7 =>
array (
'type' => 'field',
'name' => 'userjournals_date',
'fieldtype' => 'varchar(64)',
'nulltype' => 'NOT NULL',
'default' => '\'\'',
),
8 =>
array (
'type' => 'field',
'name' => 'userjournals_timestamp',
'fieldtype' => 'varchar(32)',
'nulltype' => 'NOT NULL',
'default' => '\'\'',
),
9 =>
array (
'type' => 'field',
'name' => 'userjournals_is_comment',
'fieldtype' => 'int(1)',
'nulltype' => 'NOT NULL',
'default' => '\'0\'',
),
10 =>
array (
'type' => 'pkey',
'name' => '(userjournals_id)',
),
);
$this->assertSame($expected,$result2);
}
/* public function testUpdate_table_structure()
{
}*/
public function testMake_field_types()
{
$fieldDefs = array (
0 =>
array (
'type' => 'field',
'name' => 'userjournals_id',
'fieldtype' => 'int(10)',
'vartype' => 'unsigned',
'nulltype' => 'NOT NULL',
'autoinc' => true,
),
1 =>
array (
'type' => 'field',
'name' => 'userjournals_userid',
'fieldtype' => 'int(10)',
'vartype' => 'unsigned',
'nulltype' => 'NOT NULL',
'default' => '\'0\'',
),
2 =>
array (
'type' => 'field',
'name' => 'userjournals_subject',
'fieldtype' => 'varchar(64)',
'nulltype' => 'NOT NULL',
'default' => '\'\'',
),
3 =>
array (
'type' => 'field',
'name' => 'userjournals_categories',
'fieldtype' => 'varchar(100)',
'nulltype' => 'NOT NULL',
'default' => '\'\'',
),
4 =>
array (
'type' => 'field',
'name' => 'userjournals_playing',
'fieldtype' => 'varchar(50)',
'nulltype' => 'NOT NULL',
'default' => '\'\'',
),
5 =>
array (
'type' => 'field',
'name' => 'userjournals_mood',
'fieldtype' => 'enum(\'\',\'happy\',\'sad\',\'alienated\',\'beat_up\',\'angry\',\'annoyed\')',
'nulltype' => 'NOT NULL',
'default' => '\'happy\'',
),
6 =>
array (
'type' => 'field',
'name' => 'userjournals_entry',
'fieldtype' => 'longtext',
'nulltype' => 'NOT NULL',
),
7 =>
array (
'type' => 'field',
'name' => 'userjournals_date',
'fieldtype' => 'varchar(64)',
'nulltype' => 'NOT NULL',
'default' => '\'\'',
),
8 =>
array (
'type' => 'field',
'name' => 'userjournals_timestamp',
'fieldtype' => 'varchar(32)',
'nulltype' => 'NOT NULL',
'default' => '\'\'',
),
9 =>
array (
'type' => 'field',
'name' => 'userjournals_is_comment',
'fieldtype' => 'int(1)',
'nulltype' => 'NOT NULL',
'default' => '\'0\'',
),
10 =>
array (
'type' => 'field',
'name' => 'userjournals_comment_parent',
'fieldtype' => 'int(1)',
'default' => 'NULL',
),
11 =>
array (
'type' => 'field',
'name' => 'userjournals_is_blog_desc',
'fieldtype' => 'int(1)',
'nulltype' => 'NOT NULL',
'default' => '\'0\'',
),
12 =>
array (
'type' => 'field',
'name' => 'userjournals_is_published',
'fieldtype' => 'int(1)',
'nulltype' => 'NOT NULL',
'default' => '\'0\'',
),
13 =>
array (
'type' => 'pkey',
'name' => '(userjournals_id)',
),
);
$result = $this->dta->make_field_types($fieldDefs);
$expected = array (
'_FIELD_TYPES' =>
array (
'userjournals_id' => 'int',
'userjournals_userid' => 'int',
'userjournals_subject' => 'escape',
'userjournals_categories' => 'escape',
'userjournals_playing' => 'escape',
'userjournals_mood' => 'escape',
'userjournals_entry' => 'escape',
'userjournals_date' => 'escape',
'userjournals_timestamp' => 'escape',
'userjournals_is_comment' => 'int',
'userjournals_comment_parent' => 'int',
'userjournals_is_blog_desc' => 'int',
'userjournals_is_published' => 'int',
),
'_NOTNULL' =>
array (
'userjournals_id' => '',
'userjournals_entry' => '',
),
);
$this->assertSame($expected, $result);
}
/*
public function testCreateTable()
{
}*/
public function testGet_current_table()
{
$expected = array (
0 =>
array (
0 => 'CREATE TABLE e107_core (
e107_name varchar(100) NOT NULL DEFAULT \'\',
e107_value text NOT NULL,
PRIMARY KEY (e107_name)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;',
1 => 'e107_core',
2 => 'e107_name varchar(100) NOT NULL DEFAULT \'\',
e107_value text NOT NULL,
PRIMARY KEY (e107_name)',
3 => 'MyISAM DEFAULT CHARSET=utf8',
),
);
$result = $this->dta->get_current_table('core');
$this->assertSame($result,$expected);
}
/*
public function testMake_changes_list()
{
}
public function testMake_field_list()
{
}
public function testGet_table_def()
{
}
public function testMake_table_list()
{
}
public function testMake_def()
{
}
*/
}