1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-04 13:47:31 +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. // Legacy Fix.
define('MYSQL_ASSOC', 1); defined('MYSQL_ASSOC') or define('MYSQL_ASSOC', 1);
define('MYSQL_NUM', 2); defined('MYSQL_NUM') or define('MYSQL_NUM', 2);
define('MYSQL_BOTH', 3); defined('MYSQL_BOTH') or define('MYSQL_BOTH', 3);
require_once('e_db_interface.php'); require_once('e_db_interface.php');
require_once('e_db_legacy_trait.php'); require_once('e_db_legacy_trait.php');

View File

@@ -1083,9 +1083,16 @@ class e_db_mysql implements e_db
/** @var PDOStatement $resource */ /** @var PDOStatement $resource */
$resource = $this->mySQLresult; $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'); $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)) 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 // 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'); $this->dbError('db_Delete');
return $tmp; return $this->mySQLrows;
} }
else 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)) 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'); $this->dbError('db_Delete');
return $tmp; return $this->mySQLrows;
} }
else 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().", ''"; $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})" ); $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"); $this->db->db_Query("SELECT user_email FROM ".MPREFIX."user WHERE user_id = 3");
$result = $res->fetch(); $result = $this->db->fetch();
$this->assertEquals('email@address.com', $result['user_email']); $this->assertEquals('email@address.com', $result['user_email']);
// duplicate unique field 'media_cat_category', should return false/error. // 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 // Check if the returned value is equal to the number of affected records
$expected = $actual; $expected = $actual;
$actual = $this->db->rowCount(); $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 // Insert some records
$this->db->insert('tmp', array('tmp_ip' => '127.0.0.1', 'tmp_time' => time(), 'tmp_info' => 'Delete test 1')); $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 // Count records
$expected = 3; $expected = 3;
$actual = $this->db->count('tmp'); $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 // Delete 1 record
$expected = 1; $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 // Check if the returned value is equal to the number of affected records
$expected = $actual; $expected = $actual;
$actual = $this->db->rowCount(); $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 // Delete all remaining (2) records
$expected = 2; $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 // Check if the returned value is equal to the number of affected records
$expected = $actual; $expected = $actual;
$actual = $this->db->rowCount(); $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 // Delete from an table that doesn't exist
$actual = $this->db->delete('tmp_unknown_table'); $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); $result = $this->db->escape("Can't", true);
$this->assertEquals("Can't", $result); $this->assertEquals("Can't", $result);
} }
public function testDb_Table_exists() public function testDb_Table_exists()
{ {
$result = $this->db->db_Table_exists('plugin'); $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() public function testGetLanguage()
{ {
@@ -982,7 +942,6 @@ abstract class e_db_abstractTest extends \Codeception\Test\Unit
$this->db->select('doesnt_exists'); $this->db->select('doesnt_exists');
$result = $this->db->getLastErrorNumber(); $result = $this->db->getLastErrorNumber();
$this->assertEquals("42S02", $result); $this->assertEquals("42S02", $result);
} }
public function testGetLastErrorText() public function testGetLastErrorText()

View File

@@ -36,12 +36,39 @@ class e_db_mysqlTest extends e_db_abstractTest
$this->db->__construct(); $this->db->__construct();
$this->loadConfig(); $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() public function testGetServerInfo()
{ {
$result = $this->db->getServerInfo(); $result = $this->db->getServerInfo();
// This implementation always returns "?". $this->assertRegExp('/[0-9]+\./', $result);
$this->assertEquals('?',$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

@@ -43,6 +43,42 @@ class e_db_pdoTest extends e_db_abstractTest
$this->assertEquals('utf8', $result); $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 * PDO-exclusive feature: Select with argument bindings
* @see e_db_abstractTest::testSelect() * @see e_db_abstractTest::testSelect()
@@ -86,7 +122,6 @@ class e_db_pdoTest extends e_db_abstractTest
$this->assertGreaterThan(0, $result); $this->assertGreaterThan(0, $result);
$query = array( $query = array(
'PREPARE' => 'SELECT * FROM ' . MPREFIX . 'user WHERE user_id=:user_id AND user_name=:user_name', 'PREPARE' => 'SELECT * FROM ' . MPREFIX . 'user WHERE user_id=:user_id AND user_name=:user_name',
'EXECUTE' => array( 'EXECUTE' => array(