Add phpunit for DDLUpdateField

This commit is contained in:
Laurent Destailleur 2023-03-22 15:22:23 +01:00
parent e32ab31697
commit a618bed16d
4 changed files with 175 additions and 3 deletions

View File

@ -60,6 +60,7 @@ class FileUpload
$this->element = $element;
$pathname = $filename = $element;
$regs = array();
if (preg_match('/^([^_]+)_([^_]+)/i', $element, $regs)) {
$pathname = $regs[1];
$filename = $regs[2];

View File

@ -1225,7 +1225,7 @@ class DoliDBPgsql extends DoliDB
{
// phpcs:enable
$sql = "ALTER TABLE ".$table;
$sql .= ' ALTER COLUMN "'.$field_name.'" TYPE '.$field_desc['type'];
$sql .= " ALTER COLUMN '".$this->escape($field_name)."' TYPE ".$field_desc['type'];
if (preg_match("/^[^\s]/i", $field_desc['value'])) {
if (!in_array($field_desc['type'], array('smallint', 'int', 'date', 'datetime')) && $field_desc['value']) {
$sql .= "(".$field_desc['value'].")";
@ -1235,10 +1235,10 @@ class DoliDBPgsql extends DoliDB
if ($field_desc['null'] == 'not null' || $field_desc['null'] == 'NOT NULL') {
// We will try to change format of column to NOT NULL. To be sure the ALTER works, we try to update fields that are NULL
if ($field_desc['type'] == 'varchar' || $field_desc['type'] == 'text') {
$sqlbis = "UPDATE ".$table." SET ".$field_name." = '".$this->escape($field_desc['default'] ? $field_desc['default'] : '')."' WHERE ".$field_name." IS NULL";
$sqlbis = "UPDATE ".$table." SET ".$this->escape($field_name)." = '".$this->escape($field_desc['default'] ? $field_desc['default'] : '')."' WHERE ".$this->escape($field_name)." IS NULL";
$this->query($sqlbis);
} elseif ($field_desc['type'] == 'tinyint' || $field_desc['type'] == 'int') {
$sqlbis = "UPDATE ".$table." SET ".$field_name." = ".((int) $this->escape($field_desc['default'] ? $field_desc['default'] : 0))." WHERE ".$field_name." IS NULL";
$sqlbis = "UPDATE ".$table." SET ".$this->escape($field_name)." = ".((int) $this->escape($field_desc['default'] ? $field_desc['default'] : 0))." WHERE ".$this->escape($field_name)." IS NULL";
$this->query($sqlbis);
}
}

View File

@ -112,6 +112,8 @@ class AllTests
$suite->addTestSuite('CodingSqlTest');
require_once dirname(__FILE__).'/CodingPhpTest.php';
$suite->addTestSuite('CodingPhpTest');
require_once dirname(__FILE__).'/DoliDBTest.php';
$suite->addTestSuite('DoliDBTest');
require_once dirname(__FILE__).'/SecurityTest.php';
$suite->addTestSuite('SecurityTest');

169
test/phpunit/DoliDBTest.php Normal file
View File

@ -0,0 +1,169 @@
<?php
/* Copyright (C) 2010 Laurent Destailleur <eldy@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* or see https://www.gnu.org/
*/
/**
* \file test/phpunit/DoliDBTest.php
* \ingroup test
* \brief PHPUnit test
* \remarks To run this script as CLI: phpunit filename.php
*/
global $conf,$user,$langs,$db;
//define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
//require_once 'PHPUnit/Autoload.php';
require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
require_once dirname(__FILE__).'/../../htdocs/core/class/discount.class.php';
if (empty($user->id)) {
print "Load permissions for admin user nb 1\n";
$user->fetch(1);
$user->getrights();
}
$conf->global->MAIN_DISABLE_ALL_MAILS=1;
/**
* Class for PHPUnit tests
*
* @backupGlobals disabled
* @backupStaticAttributes enabled
* @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
*/
class DoliDBTest extends PHPUnit\Framework\TestCase
{
protected $savconf;
protected $savuser;
protected $savlangs;
protected $savdb;
/**
* Constructor
* We save global variables into local variables
*
* @return DiscountTest
*/
public function __construct()
{
parent::__construct();
//$this->sharedFixture
global $conf,$user,$langs,$db;
$this->savconf=$conf;
$this->savuser=$user;
$this->savlangs=$langs;
$this->savdb=$db;
print __METHOD__." db->type=".$db->type." user->id=".$user->id;
//print " - db ".$db->db;
print "\n";
}
/**
* setUpBeforeClass
*
* @return void
*/
public static function setUpBeforeClass(): void
{
global $conf,$user,$langs,$db;
$db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
print __METHOD__."\n";
}
/**
* tearDownAfterClass
*
* @return void
*/
public static function tearDownAfterClass(): void
{
global $conf,$user,$langs,$db;
$db->rollback();
print __METHOD__."\n";
}
/**
* Init phpunit tests
*
* @return void
*/
protected function setUp(): void
{
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
print __METHOD__."\n";
//print $db->getVersion()."\n";
}
/**
* End phpunit tests
*
* @return void
*/
protected function tearDown(): void
{
print __METHOD__."\n";
}
/**
* testDDLUpdateField
*
* @return int
*/
public function testDDLUpdateField()
{
global $conf,$user,$langs,$db;
$conf=$this->savconf;
$user=$this->savuser;
$langs=$this->savlangs;
$db=$this->savdb;
print __METHOD__.' db->type = '.$db->type."\n";
$savtype = '';
$savnull = '';
$resql = $db->DDLDescTable($db->prefix().'c_paper_format', 'code');
while ($obj = $resql->fetch_object()) {
if ($obj->Field == 'code') {
$savtype = $obj->Type;
$savnull = $obj->Null;
}
}
// Set new field
$field_desc = array('type'=>'varchar', 'value'=>'17', 'null'=>'NOT NULL');
$result = $db->DDLUpdateField($db->prefix().'c_paper_format', 'code', $field_desc);
$this->assertEquals(1, $result);
print __METHOD__." result=".$result."\n";
// TODO Use $savtype and $savnull instead of hard coded
$field_desc = array('type'=>'varchar', 'value'=>'16', 'null'=>'NOT NULL');
$result = $db->DDLUpdateField($db->prefix().'c_paper_format', 'code', $field_desc);
$this->assertEquals(1, $result);
print __METHOD__." result=".$result."\n";
return $result;
}
}