Enhance phpunit on sql code testing

This commit is contained in:
Laurent Destailleur 2022-03-07 19:37:53 +01:00
parent d6852c31b8
commit b339e1a883
2 changed files with 15 additions and 15 deletions

View File

@ -1250,7 +1250,7 @@ class FormMail extends Form
* Return templates of email with type = $type_template or type = 'all'. * Return templates of email with type = $type_template or type = 'all'.
* This search into table c_email_templates. Used by the get_form function. * This search into table c_email_templates. Used by the get_form function.
* *
* @param DoliDB $db Database handler * @param DoliDB $dbs Database handler
* @param string $type_template Get message for model/type=$type_template, type='all' also included. * @param string $type_template Get message for model/type=$type_template, type='all' also included.
* @param User $user Get template public or limited to this user * @param User $user Get template public or limited to this user
* @param Translate $outputlangs Output lang object * @param Translate $outputlangs Output lang object
@ -1259,7 +1259,7 @@ class FormMail extends Form
* @param string $label Label of template * @param string $label Label of template
* @return ModelMail|integer One instance of ModelMail or -1 if error * @return ModelMail|integer One instance of ModelMail or -1 if error
*/ */
public function getEMailTemplate($db, $type_template, $user, $outputlangs, $id = 0, $active = 1, $label = '') public function getEMailTemplate($dbs, $type_template, $user, $outputlangs, $id = 0, $active = 1, $label = '')
{ {
global $conf, $langs; global $conf, $langs;
@ -1279,18 +1279,18 @@ class FormMail extends Form
} }
$sql = "SELECT rowid, module, label, type_template, topic, joinfiles, content, content_lines, lang"; $sql = "SELECT rowid, module, label, type_template, topic, joinfiles, content, content_lines, lang";
$sql .= " FROM ".$db->prefix().'c_email_templates'; $sql .= " FROM ".$dbs->prefix().'c_email_templates';
$sql .= " WHERE (type_template='".$db->escape($type_template)."' OR type_template='all')"; $sql .= " WHERE (type_template='".$dbs->escape($type_template)."' OR type_template='all')";
$sql .= " AND entity IN (".getEntity('c_email_templates').")"; $sql .= " AND entity IN (".getEntity('c_email_templates').")";
$sql .= " AND (private = 0 OR fk_user = ".((int) $user->id).")"; // Get all public or private owned $sql .= " AND (private = 0 OR fk_user = ".((int) $user->id).")"; // Get all public or private owned
if ($active >= 0) { if ($active >= 0) {
$sql .= " AND active = ".((int) $active); $sql .= " AND active = ".((int) $active);
} }
if ($label) { if ($label) {
$sql .= " AND label = '".$db->escape($label)."'"; $sql .= " AND label = '".$dbs->escape($label)."'";
} }
if (!($id > 0) && $languagetosearch) { if (!($id > 0) && $languagetosearch) {
$sql .= " AND (lang = '".$db->escape($languagetosearch)."'".($languagetosearchmain ? " OR lang = '".$db->escape($languagetosearchmain)."'" : "")." OR lang IS NULL OR lang = '')"; $sql .= " AND (lang = '".$dbs->escape($languagetosearch)."'".($languagetosearchmain ? " OR lang = '".$dbs->escape($languagetosearchmain)."'" : "")." OR lang IS NULL OR lang = '')";
} }
if ($id > 0) { if ($id > 0) {
$sql .= " AND rowid=".(int) $id; $sql .= " AND rowid=".(int) $id;
@ -1299,22 +1299,22 @@ class FormMail extends Form
$sql .= " AND position=0"; $sql .= " AND position=0";
} }
if ($languagetosearch) { if ($languagetosearch) {
$sql .= $db->order("position,lang,label", "ASC,DESC,ASC"); // We want line with lang set first, then with lang null or '' $sql .= $dbs->order("position,lang,label", "ASC,DESC,ASC"); // We want line with lang set first, then with lang null or ''
} else { } else {
$sql .= $db->order("position,lang,label", "ASC,ASC,ASC"); // If no language provided, we give priority to lang not defined $sql .= $dbs->order("position,lang,label", "ASC,ASC,ASC"); // If no language provided, we give priority to lang not defined
} }
//$sql .= $db->plimit(1); //$sql .= $dbs->plimit(1);
//print $sql; //print $sql;
$resql = $db->query($sql); $resql = $dbs->query($sql);
if (!$resql) { if (!$resql) {
dol_print_error($db); dol_print_error($dbs);
return -1; return -1;
} }
// Get first found // Get first found
while (1) { while (1) {
$obj = $db->fetch_object($resql); $obj = $dbs->fetch_object($resql);
if ($obj) { if ($obj) {
// If template is for a module, check module is enabled; if not, take next template // If template is for a module, check module is enabled; if not, take next template
@ -1386,7 +1386,8 @@ class FormMail extends Form
} }
} }
$db->free($resql); $dbs->free($resql);
return $ret; return $ret;
} }

View File

@ -204,7 +204,6 @@ class CodingPhpTest extends PHPUnit\Framework\TestCase
'commonobject.class.php', 'commonobject.class.php',
'conf.class.php', 'conf.class.php',
'html.form.class.php', 'html.form.class.php',
'html.formmail.class.php',
'translate.class.php', 'translate.class.php',
'utils.class.php', 'utils.class.php',
'TraceableDB.php', 'TraceableDB.php',
@ -362,7 +361,7 @@ class CodingPhpTest extends PHPUnit\Framework\TestCase
$matches=array(); $matches=array();
preg_match_all('/(sql|SET|WHERE|INSERT|VALUES|LIKE).+\s*\'"\s*\.\s*\$(.......)/', $filecontent, $matches, PREG_SET_ORDER); preg_match_all('/(sql|SET|WHERE|INSERT|VALUES|LIKE).+\s*\'"\s*\.\s*\$(.......)/', $filecontent, $matches, PREG_SET_ORDER);
foreach ($matches as $key => $val) { foreach ($matches as $key => $val) {
if (! in_array($val[2], array('this->d', 'this->e', 'db->esc', 'dbs->es', 'mydb->e', 'dbsessi', 'db->ida', 'escaped', 'exclude', 'include'))) { if (! in_array($val[2], array('this->d', 'this->e', 'db->esc', 'dbs->es', 'dbs->id', 'mydb->e', 'dbsessi', 'db->ida', 'escaped', 'exclude', 'include'))) {
$ok=false; // This will generate error $ok=false; // This will generate error
break; break;
} }