diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 1da7bc7eb97..c048c968545 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -10059,7 +10059,7 @@ abstract class CommonObject public function isInt($info) { if (is_array($info)) { - if (isset($info['type']) && (preg_match('/(^int|int$)/i', $info['type']))) { + if (isset($info['type']) && (preg_match('/^(?:tiny|small|medium|big)?int|int$/i', $info['type']))) { return true; } else { return false; diff --git a/test/phpunit/CommonObjectTest.php b/test/phpunit/CommonObjectTest.php index 49f63efddc7..2e8e4da47d7 100644 --- a/test/phpunit/CommonObjectTest.php +++ b/test/phpunit/CommonObjectTest.php @@ -117,4 +117,38 @@ class CommonObjectTest extends CommonClassTest $this->assertLessThanOrEqual($result, 0); return $result; } + + /** + * testIsInt + * + * @return void + */ + public function testIsInt() + { + global $conf,$user,$langs,$db; + $conf = $this->savconf; + $user = $this->savuser; + $langs = $this->savlangs; + $db = $this->savdb; + + $localobject = new Commande($db); + + // Integer types, with or without a size/unsigned suffix, must be detected. + $this->assertTrue($localobject->isInt(array('type' => 'int')), 'int'); + $this->assertTrue($localobject->isInt(array('type' => 'int(11)')), 'int(11)'); + $this->assertTrue($localobject->isInt(array('type' => 'integer')), 'integer'); + $this->assertTrue($localobject->isInt(array('type' => 'tinyint(4)')), 'tinyint(4)'); + $this->assertTrue($localobject->isInt(array('type' => 'smallint(6)')), 'smallint(6)'); + $this->assertTrue($localobject->isInt(array('type' => 'bigint(20)')), 'bigint(20)'); + // Dolibarr foreign-key column syntax "integer:Class:path" must keep being detected. + $this->assertTrue($localobject->isInt(array('type' => 'integer:User:user/class/user.class.php')), 'integer:User:...'); + + // Non-integer types must not be detected, including strings that merely contain "int". + $this->assertFalse($localobject->isInt(array('type' => 'varchar(255)')), 'varchar(255)'); + $this->assertFalse($localobject->isInt(array('type' => 'double(24,8)')), 'double(24,8)'); + $this->assertFalse($localobject->isInt(array('type' => 'date')), 'date'); + $this->assertFalse($localobject->isInt(array('type' => 'sellist:llx_c_typent:libelle:id')), 'sellist:...'); + + print __METHOD__." OK\n"; + } }