Skip to content

Commit

Permalink
Enhance phpunit on sql code testing
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Mar 7, 2022
1 parent d6852c3 commit b339e1a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
27 changes: 14 additions & 13 deletions htdocs/core/class/html.formmail.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1250,7 +1250,7 @@ public function getHtmlForTopic($arraydefaultmessage, $helpforsubstitution)
* Return templates of email with type = $type_template or type = 'all'.
* 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 User $user Get template public or limited to this user
* @param Translate $outputlangs Output lang object
Expand All @@ -1259,7 +1259,7 @@ public function getHtmlForTopic($arraydefaultmessage, $helpforsubstitution)
* @param string $label Label of template
* @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;

Expand All @@ -1279,18 +1279,18 @@ public function getEMailTemplate($db, $type_template, $user, $outputlangs, $id =
}

$sql = "SELECT rowid, module, label, type_template, topic, joinfiles, content, content_lines, lang";
$sql .= " FROM ".$db->prefix().'c_email_templates';
$sql .= " WHERE (type_template='".$db->escape($type_template)."' OR type_template='all')";
$sql .= " FROM ".$dbs->prefix().'c_email_templates';
$sql .= " WHERE (type_template='".$dbs->escape($type_template)."' OR type_template='all')";
$sql .= " AND entity IN (".getEntity('c_email_templates').")";
$sql .= " AND (private = 0 OR fk_user = ".((int) $user->id).")"; // Get all public or private owned
if ($active >= 0) {
$sql .= " AND active = ".((int) $active);
}
if ($label) {
$sql .= " AND label = '".$db->escape($label)."'";
$sql .= " AND label = '".$dbs->escape($label)."'";
}
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) {
$sql .= " AND rowid=".(int) $id;
Expand All @@ -1299,22 +1299,22 @@ public function getEMailTemplate($db, $type_template, $user, $outputlangs, $id =
$sql .= " AND position=0";
}
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 {
$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;

$resql = $db->query($sql);
$resql = $dbs->query($sql);
if (!$resql) {
dol_print_error($db);
dol_print_error($dbs);
return -1;
}

// Get first found
while (1) {
$obj = $db->fetch_object($resql);
$obj = $dbs->fetch_object($resql);

if ($obj) {
// If template is for a module, check module is enabled; if not, take next template
Expand Down Expand Up @@ -1386,7 +1386,8 @@ public function getEMailTemplate($db, $type_template, $user, $outputlangs, $id =
}
}

$db->free($resql);
$dbs->free($resql);

return $ret;
}

Expand Down
3 changes: 1 addition & 2 deletions test/phpunit/CodingPhpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ public function testPHP()
'commonobject.class.php',
'conf.class.php',
'html.form.class.php',
'html.formmail.class.php',
'translate.class.php',
'utils.class.php',
'TraceableDB.php',
Expand Down Expand Up @@ -362,7 +361,7 @@ public function testPHP()
$matches=array();
preg_match_all('/(sql|SET|WHERE|INSERT|VALUES|LIKE).+\s*\'"\s*\.\s*\$(.......)/', $filecontent, $matches, PREG_SET_ORDER);
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
break;
}
Expand Down

0 comments on commit b339e1a

Please sign in to comment.