diff --git a/e107_handlers/e_db_pdo_class.php b/e107_handlers/e_db_pdo_class.php index d5a66ec89..7535f3c49 100644 --- a/e107_handlers/e_db_pdo_class.php +++ b/e107_handlers/e_db_pdo_class.php @@ -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'); diff --git a/e107_handlers/mysql_class.php b/e107_handlers/mysql_class.php index 5c5fbae27..75c5723df 100644 --- a/e107_handlers/mysql_class.php +++ b/e107_handlers/mysql_class.php @@ -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 { diff --git a/e107_tests/tests/unit/e_db_abstractTest.php b/e107_tests/tests/unit/e_db_abstractTest.php index d46f13ce6..b3d395dd5 100644 --- a/e107_tests/tests/unit/e_db_abstractTest.php +++ b/e107_tests/tests/unit/e_db_abstractTest.php @@ -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() diff --git a/e107_tests/tests/unit/e_db_mysqlTest.php b/e107_tests/tests/unit/e_db_mysqlTest.php index c06f49a91..ab1b65465 100644 --- a/e107_tests/tests/unit/e_db_mysqlTest.php +++ b/e107_tests/tests/unit/e_db_mysqlTest.php @@ -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); } } diff --git a/e107_tests/tests/unit/e_db_pdoTest.php b/e107_tests/tests/unit/e_db_pdoTest.php index 630d707a6..e09c566af 100644 --- a/e107_tests/tests/unit/e_db_pdoTest.php +++ b/e107_tests/tests/unit/e_db_pdoTest.php @@ -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'; }