1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-03 13:17:24 +02:00

PHP 5.6 fixes for e_db_mysql

- FIX: Don't redefine MYSQL_* constants in e_db_pdo_class.php
- FIX: e_db_mysql::rowCount() could try to use mysql_num_rows() to count rows of a non-resource
- FIX: e_db_mysql::delete() stores the number of deleted rows in e_db_mysql::$mySQLrows
- FIX: e_db_abstractTest::testDb_Query() was fetching in PDO mode but shouldn't have been
- FIX: Typos in e_db_abstractTest::testDelete()
- MOD: Moved PDO-exclusive testBackup() from e_db_abstractTest to e_db_pdoTest
- FIX: e_db_mysqlTest now works in PHP 5.6 if the main e_db instance was in PDO mode but the test
       class initializes in legacy mode
- MOD: e_db_mysqlTest now asserts that PDO mode is not in use
- FIX: e_db_mysqlTest::testGetServerInfo() should now actually get a version number
- FIX: e_db_mysqlTest::testGetLastErrorNumber() has a different error code compared to PDO
- FIX: e_db_mysqlTest::testEscape() should actually get something from mysql_real_escape_string()
This commit is contained in:
Nick Liu
2020-01-19 12:30:23 +01:00
parent 970f65b705
commit 72d3f07410
5 changed files with 108 additions and 80 deletions

View File

@@ -6,9 +6,9 @@
*/
// Legacy Fix.
define('MYSQL_ASSOC', 1);
define('MYSQL_NUM', 2);
define('MYSQL_BOTH', 3);
defined('MYSQL_ASSOC') or define('MYSQL_ASSOC', 1);
defined('MYSQL_NUM') or define('MYSQL_NUM', 2);
defined('MYSQL_BOTH') or define('MYSQL_BOTH', 3);
require_once('e_db_interface.php');
require_once('e_db_legacy_trait.php');

View File

@@ -1083,9 +1083,16 @@ class e_db_mysql implements e_db
/** @var PDOStatement $resource */
$resource = $this->mySQLresult;
$rows = $this->mySQLrows = ($this->pdo) ? $resource->rowCount() : mysql_num_rows($this->mySQLresult);
if ($this->pdo)
{
$this->mySQLrows = $resource->rowCount();
}
elseif (is_resource($this->mySQLresult))
{
$this->mySQLrows = mysql_num_rows($this->mySQLresult);
}
$this->dbError('db_Rows');
return $rows;
return $this->mySQLrows;
}
@@ -1723,9 +1730,9 @@ class e_db_mysql implements e_db
if ($result = $this->mySQLresult = $this->db_Query('DELETE FROM '.$this->mySQLPrefix.$table, NULL, 'db_Delete', $debug, $log_type, $log_remark))
{
// return the number of records deleted instead of an object
$tmp = ($this->pdo) ? $this->mySQLresult->rowCount() : mysql_affected_rows($this->mySQLaccess);
$this->mySQLrows = ($this->pdo) ? $this->mySQLresult->rowCount() : mysql_affected_rows($this->mySQLaccess);
$this->dbError('db_Delete');
return $tmp;
return $this->mySQLrows;
}
else
{
@@ -1737,9 +1744,9 @@ class e_db_mysql implements e_db
{
if ($result = $this->mySQLresult = $this->db_Query('DELETE FROM '.$this->mySQLPrefix.$table.' WHERE '.$arg, NULL, 'db_Delete', $debug, $log_type, $log_remark))
{
$tmp = ($this->pdo) ? $this->mySQLresult->rowCount() : mysql_affected_rows($this->mySQLaccess);
$this->mySQLrows = ($this->pdo) ? $this->mySQLresult->rowCount() : mysql_affected_rows($this->mySQLaccess);
$this->dbError('db_Delete');
return $tmp;
return $this->mySQLrows;
}
else
{

View File

@@ -200,8 +200,8 @@ abstract class e_db_abstractTest extends \Codeception\Test\Unit
$userp = "3, 'Display Name', 'Username', '', 'password-hash', '', 'email@address.com', '', '', 0, ".time().", 0, 0, 0, 0, 0, '127.0.0.1', 0, '', 0, 1, '', '', '0', '', ".time().", ''";
$this->db->db_Query("REPLACE INTO ".MPREFIX."user VALUES ({$userp})" );
$res = $this->db->db_Query("SELECT user_email FROM ".MPREFIX."user WHERE user_id = 3");
$result = $res->fetch();
$this->db->db_Query("SELECT user_email FROM ".MPREFIX."user WHERE user_id = 3");
$result = $this->db->fetch();
$this->assertEquals('email@address.com', $result['user_email']);
// duplicate unique field 'media_cat_category', should return false/error.
@@ -656,7 +656,7 @@ abstract class e_db_abstractTest extends \Codeception\Test\Unit
// Check if the returned value is equal to the number of affected records
$expected = $actual;
$actual = $this->db->rowCount();
$this->assertEquals($expected, $actual, "Number of deleted records is wrong ({$expected} != {$actual}");
$this->assertEquals($expected, $actual, "Number of deleted records is wrong ({$expected} != {$actual})");
// Insert some records
$this->db->insert('tmp', array('tmp_ip' => '127.0.0.1', 'tmp_time' => time(), 'tmp_info' => 'Delete test 1'));
@@ -666,7 +666,7 @@ abstract class e_db_abstractTest extends \Codeception\Test\Unit
// Count records
$expected = 3;
$actual = $this->db->count('tmp');
$this->assertEquals($expected, $actual, "Number of inserted records is wrong ({$expected} != {$actual}");
$this->assertEquals($expected, $actual, "Number of inserted records is wrong ({$expected} != {$actual})");
// Delete 1 record
$expected = 1;
@@ -676,7 +676,7 @@ abstract class e_db_abstractTest extends \Codeception\Test\Unit
// Check if the returned value is equal to the number of affected records
$expected = $actual;
$actual = $this->db->rowCount();
$this->assertEquals($expected, $actual, "Number of deleted records is wrong ({$expected} != {$actual}");
$this->assertEquals($expected, $actual, "Number of deleted records is wrong ({$expected} != {$actual})");
// Delete all remaining (2) records
$expected = 2;
@@ -686,7 +686,7 @@ abstract class e_db_abstractTest extends \Codeception\Test\Unit
// Check if the returned value is equal to the number of affected records
$expected = $actual;
$actual = $this->db->rowCount();
$this->assertEquals($expected, $actual, "Number of deleted records is wrong ({$expected} != {$actual}");
$this->assertEquals($expected, $actual, "Number of deleted records is wrong ({$expected} != {$actual})");
// Delete from an table that doesn't exist
$actual = $this->db->delete('tmp_unknown_table');
@@ -819,11 +819,8 @@ abstract class e_db_abstractTest extends \Codeception\Test\Unit
$result = $this->db->escape("Can't", true);
$this->assertEquals("Can't", $result);
}
public function testDb_Table_exists()
{
$result = $this->db->db_Table_exists('plugin');
@@ -923,43 +920,6 @@ abstract class e_db_abstractTest extends \Codeception\Test\Unit
}
public function testBackup()
{
$opts = array(
'gzip' => false,
'nologs' => false,
'droptable' => false,
);
$result = $this->db->backup('user,core_media_cat', null, $opts);
$uncompressedSize = filesize($result);
$tmp = file_get_contents($result);
$this->assertStringNotContainsString("DROP TABLE IF EXISTS `e107_user`;", $tmp);
$this->assertStringContainsString("CREATE TABLE `e107_user` (", $tmp);
$this->assertStringContainsString("INSERT INTO `e107_user` VALUES (1", $tmp);
$this->assertStringContainsString("CREATE TABLE `e107_core_media_cat`", $tmp);
$result = $this->db->backup('*', null, $opts);
$size = filesize($result);
$this->assertGreaterThan(100000,$size);
$opts = array(
'gzip' => true,
'nologs' => false,
'droptable' => false,
);
$result = $this->db->backup('user,core_media_cat', null, $opts);
$compressedSize = filesize($result);
$this->assertLessThan($uncompressedSize, $compressedSize);
$result = $this->db->backup('missing_table', null, $opts);
$this->assertFalse($result);
}
public function testGetLanguage()
{
@@ -982,7 +942,6 @@ abstract class e_db_abstractTest extends \Codeception\Test\Unit
$this->db->select('doesnt_exists');
$result = $this->db->getLastErrorNumber();
$this->assertEquals("42S02", $result);
}
public function testGetLastErrorText()

View File

@@ -36,12 +36,39 @@ class e_db_mysqlTest extends e_db_abstractTest
$this->db->__construct();
$this->loadConfig();
$this->db->db_Connect(
$this->dbConfig['mySQLserver'],
$this->dbConfig['mySQLuser'],
$this->dbConfig['mySQLpassword'],
$this->dbConfig['mySQLdefaultdb']
);
}
public function testGetPDO()
{
$result = $this->db->getPDO();
$this->assertFalse($result);
}
public function testGetServerInfo()
{
$result = $this->db->getServerInfo();
// This implementation always returns "?".
$this->assertEquals('?',$result);
$this->assertRegExp('/[0-9]+\./', $result);
}
public function testGetLastErrorNumber()
{
$this->db->select('doesnt_exists');
$result = $this->db->getLastErrorNumber();
$this->assertEquals("1146", $result);
}
public function testEscape()
{
$result = $this->db->escape(123);
$this->assertEquals(123,$result);
$result = $this->db->escape("Can't", true);
$this->assertEquals("Can\'t", $result);
}
}

View File

@@ -18,9 +18,9 @@ class e_db_pdoTest extends e_db_abstractTest
protected function _before()
{
require_once(e_HANDLER."e_db_interface.php");
require_once(e_HANDLER."e_db_legacy_trait.php");
require_once(e_HANDLER."e_db_pdo_class.php");
require_once(e_HANDLER . "e_db_interface.php");
require_once(e_HANDLER . "e_db_legacy_trait.php");
require_once(e_HANDLER . "e_db_pdo_class.php");
try
{
$this->db = $this->makeDb();
@@ -43,13 +43,49 @@ class e_db_pdoTest extends e_db_abstractTest
$this->assertEquals('utf8', $result);
}
public function testBackup()
{
$opts = array(
'gzip' => false,
'nologs' => false,
'droptable' => false,
);
$result = $this->db->backup('user,core_media_cat', null, $opts);
$uncompressedSize = filesize($result);
$tmp = file_get_contents($result);
$this->assertStringNotContainsString("DROP TABLE IF EXISTS `e107_user`;", $tmp);
$this->assertStringContainsString("CREATE TABLE `e107_user` (", $tmp);
$this->assertStringContainsString("INSERT INTO `e107_user` VALUES (1", $tmp);
$this->assertStringContainsString("CREATE TABLE `e107_core_media_cat`", $tmp);
$result = $this->db->backup('*', null, $opts);
$size = filesize($result);
$this->assertGreaterThan(100000, $size);
$opts = array(
'gzip' => true,
'nologs' => false,
'droptable' => false,
);
$result = $this->db->backup('user,core_media_cat', null, $opts);
$compressedSize = filesize($result);
$this->assertLessThan($uncompressedSize, $compressedSize);
$result = $this->db->backup('missing_table', null, $opts);
$this->assertFalse($result);
}
/**
* PDO-exclusive feature: Select with argument bindings
* @see e_db_abstractTest::testSelect()
*/
public function testSelectBind()
{
$result = $this->db->select('user', 'user_id, user_name', 'user_id=:id OR user_name=:name ORDER BY user_name', array('id' => 999, 'name'=>'e107')); // bind support.
$result = $this->db->select('user', 'user_id, user_name', 'user_id=:id OR user_name=:name ORDER BY user_name', array('id' => 999, 'name' => 'e107')); // bind support.
$this->assertEquals(1, $result);
}
@@ -59,38 +95,37 @@ class e_db_pdoTest extends e_db_abstractTest
*/
public function testDb_QueryBind()
{
$query = array (
'PREPARE' => 'INSERT INTO '.MPREFIX.'tmp (`tmp_ip`,`tmp_time`,`tmp_info`) VALUES (:tmp_ip, :tmp_time, :tmp_info)',
$query = array(
'PREPARE' => 'INSERT INTO ' . MPREFIX . 'tmp (`tmp_ip`,`tmp_time`,`tmp_info`) VALUES (:tmp_ip, :tmp_time, :tmp_info)',
'BIND' =>
array(
'tmp_ip' =>
'tmp_ip' =>
array(
'value' => '127.0.0.1',
'type' => PDO::PARAM_STR,
'type' => PDO::PARAM_STR,
),
'tmp_time' =>
array(
'value' => 12345435,
'type' => PDO::PARAM_INT,
'type' => PDO::PARAM_INT,
),
'tmp_info' =>
array(
'value' => 'Insert test',
'type' => PDO::PARAM_STR,
'type' => PDO::PARAM_STR,
),
),
);
$result = $this->db->db_Query($query, null, 'db_Insert');
$this->assertGreaterThan(0,$result);
$this->assertGreaterThan(0, $result);
$query = array(
'PREPARE' => 'SELECT * FROM '.MPREFIX.'user WHERE user_id=:user_id AND user_name=:user_name',
'EXECUTE' => array(
'user_id' => 1,
'PREPARE' => 'SELECT * FROM ' . MPREFIX . 'user WHERE user_id=:user_id AND user_name=:user_name',
'EXECUTE' => array(
'user_id' => 1,
'user_name' => 'e107'
)
);
@@ -111,24 +146,24 @@ class e_db_pdoTest extends e_db_abstractTest
// test with table that has unique keys.
$result = $this->db->db_CopyRow('core_media_cat', '*', "media_cat_id = 1");
$qry = $this->db->getLastErrorText();
$this->assertGreaterThan(1,$result, $qry);
$this->assertGreaterThan(1, $result, $qry);
// test with table that has unique keys. (same row again) - make sure copyRow duplicates it regardless.
$result = $this->db->db_CopyRow('core_media_cat', '*', "media_cat_id = 1");
$qry = $this->db->getLastErrorText();
$this->assertGreaterThan(1,$result, $qry);
$this->assertGreaterThan(1, $result, $qry);
}
public function test_Db_CopyRowRNGRetry()
{
$original_user_handler = e107::getRegistry('core/e107/singleton/UserHandler');
$evil_user_handler = $this->make('UserHandler', [
'generateRandomString' => function($pattern = '', $seed = '')
'generateRandomString' => function ($pattern = '', $seed = '')
{
static $index = 0;
$mock_values = ['same0000000', 'same0000000', 'different00'];
return $mock_values[$index ++];
return $mock_values[$index++];
}
]);
e107::setRegistry('core/e107/singleton/UserHandler', $evil_user_handler);
@@ -136,12 +171,12 @@ class e_db_pdoTest extends e_db_abstractTest
// test with table that has unique keys.
$result = $this->db->db_CopyRow('core_media_cat', '*', "media_cat_id = 1");
$qry = $this->db->getLastErrorText();
$this->assertGreaterThan(1,$result, $qry);
$this->assertGreaterThan(1, $result, $qry);
// test with table that has unique keys. (same row again) - make sure copyRow duplicates it regardless.
$result = $this->db->db_CopyRow('core_media_cat', '*', "media_cat_id = 1");
$qry = $this->db->getLastErrorText();
$this->assertGreaterThan(1,$result, $qry);
$this->assertGreaterThan(1, $result, $qry);
e107::setRegistry('core/e107/singleton/UserHandler', $original_user_handler);
}
@@ -150,7 +185,7 @@ class e_db_pdoTest extends e_db_abstractTest
{
$original_user_handler = e107::getRegistry('core/e107/singleton/UserHandler');
$evil_user_handler = $this->make('UserHandler', [
'generateRandomString' => function($pattern = '', $seed = '')
'generateRandomString' => function ($pattern = '', $seed = '')
{
return 'neverchange';
}