From befb8a21377e42a6d7325c978ea064baf53f4ceb Mon Sep 17 00:00:00 2001 From: michelangelo Date: Tue, 17 Jun 2014 20:16:25 +0200 Subject: [PATCH 1/2] Unit test for ZF-1.12.7 (issue #378 & #381) regarding select statements with ordering direction or condition --- tests/Zend/Db/Select/Pdo/PgsqlTest.php | 57 +++++++++++++++++++ tests/Zend/Db/Select/Pdo/SqliteTest.php | 57 +++++++++++++++++++ tests/Zend/Db/Select/StaticTest.php | 57 +++++++++++++++++++ tests/Zend/Db/Select/TestCommon.php | 57 +++++++++++++++++++ tests/Zend/Db/Table/Select/Pdo/PgsqlTest.php | 57 +++++++++++++++++++ tests/Zend/Db/Table/Select/Pdo/SqliteTest.php | 57 +++++++++++++++++++ tests/Zend/Db/Table/Select/StaticTest.php | 57 +++++++++++++++++++ 7 files changed, 399 insertions(+) diff --git a/tests/Zend/Db/Select/Pdo/PgsqlTest.php b/tests/Zend/Db/Select/Pdo/PgsqlTest.php index 3a93a61ea6..d27fb871e0 100644 --- a/tests/Zend/Db/Select/Pdo/PgsqlTest.php +++ b/tests/Zend/Db/Select/Pdo/PgsqlTest.php @@ -137,4 +137,61 @@ public function testSqlInjectionWithOrder() $select->from(array('p' => 'products'))->order('name;select;MD5(1)'); $this->assertEquals('SELECT "p".* FROM "products" AS "p" ORDER BY "name;select;MD5(1)" ASC', $select->assemble()); } + + /** + * @group ZF-378 + */ + public function testOrderOfSingleFieldWithDirection() + { + $select = $this->_db->select(); + $select->from(array ('p' => 'product')) + ->order('productId DESC'); + + $expected = 'SELECT "p".* FROM "product" AS "p" ORDER BY "productId" DESC'; + $this->assertEquals($expected, $select->assemble(), + 'Order direction of field failed'); + } + + /** + * @group ZF-378 + */ + public function testOrderOfMultiFieldWithDirection() + { + $select = $this->_db->select(); + $select->from(array ('p' => 'product')) + ->order(array ('productId DESC', 'userId ASC')); + + $expected = 'SELECT "p".* FROM "product" AS "p" ORDER BY "productId" DESC, "userId" ASC'; + $this->assertEquals($expected, $select->assemble(), + 'Order direction of field failed'); + } + + /** + * @group ZF-378 + */ + public function testOrderOfMultiFieldButOnlyOneWithDirection() + { + $select = $this->_db->select(); + $select->from(array ('p' => 'product')) + ->order(array ('productId', 'userId DESC')); + + $expected = 'SELECT "p".* FROM "product" AS "p" ORDER BY "productId" ASC, "userId" DESC'; + $this->assertEquals($expected, $select->assemble(), + 'Order direction of field failed'); + } + + /** + * @group ZF-378 + * @group ZF-381 + */ + public function testOrderOfConditionalFieldWithDirection($adapter) + { + $select = $this->_db->select(); + $select->from(array ('p' => 'product')) + ->order('IF("productId" > 5,1,0) ASC'); + + $expected = 'SELECT "p".* FROM "product" AS "p" ORDER BY IF("productId" > 5,1,0) ASC'; + $this->assertEquals($expected, $select->assemble(), + 'Order direction of field failed'); + } } diff --git a/tests/Zend/Db/Select/Pdo/SqliteTest.php b/tests/Zend/Db/Select/Pdo/SqliteTest.php index 803a966fda..2794f8f078 100644 --- a/tests/Zend/Db/Select/Pdo/SqliteTest.php +++ b/tests/Zend/Db/Select/Pdo/SqliteTest.php @@ -185,4 +185,61 @@ public function testSqlInjectionWithOrder() $this->assertEquals('SELECT "p".* FROM "products" AS "p" ORDER BY "name;select;MD5(1)" ASC', $select->assemble()); } + /** + * @group ZF-378 + */ + public function testOrderOfSingleFieldWithDirection() + { + $select = $this->_db->select(); + $select->from(array ('p' => 'product')) + ->order('productId DESC'); + + $expected = 'SELECT "p".* FROM "product" AS "p" ORDER BY "productId" DESC'; + $this->assertEquals($expected, $select->assemble(), + 'Order direction of field failed'); + } + + /** + * @group ZF-378 + */ + public function testOrderOfMultiFieldWithDirection() + { + $select = $this->_db->select(); + $select->from(array ('p' => 'product')) + ->order(array ('productId DESC', 'userId ASC')); + + $expected = 'SELECT "p".* FROM "product" AS "p" ORDER BY "productId" DESC, "userId" ASC'; + $this->assertEquals($expected, $select->assemble(), + 'Order direction of field failed'); + } + + /** + * @group ZF-378 + */ + public function testOrderOfMultiFieldButOnlyOneWithDirection() + { + $select = $this->_db->select(); + $select->from(array ('p' => 'product')) + ->order(array ('productId', 'userId DESC')); + + $expected = 'SELECT "p".* FROM "product" AS "p" ORDER BY "productId" ASC, "userId" DESC'; + $this->assertEquals($expected, $select->assemble(), + 'Order direction of field failed'); + } + + /** + * @group ZF-378 + * @group ZF-381 + */ + public function testOrderOfConditionalFieldWithDirection($adapter) + { + $select = $this->_db->select(); + $select->from(array ('p' => 'product')) + ->order('IF("productId" > 5,1,0) ASC'); + + $expected = 'SELECT "p".* FROM "product" AS "p" ORDER BY IF("productId" > 5,1,0) ASC'; + $this->assertEquals($expected, $select->assemble(), + 'Order direction of field failed'); + } + } diff --git a/tests/Zend/Db/Select/StaticTest.php b/tests/Zend/Db/Select/StaticTest.php index f349c6cc27..f6cd44b967 100644 --- a/tests/Zend/Db/Select/StaticTest.php +++ b/tests/Zend/Db/Select/StaticTest.php @@ -831,4 +831,61 @@ public function testSqlInjectionWithOrder() $select->from(array('p' => 'products'))->order('name;select;MD5(1)'); $this->assertEquals('SELECT "p".* FROM "products" AS "p" ORDER BY "name;select;MD5(1)" ASC', $select->assemble()); } + + /** + * @group ZF-378 + */ + public function testOrderOfSingleFieldWithDirection() + { + $select = $this->_db->select(); + $select->from(array ('p' => 'product')) + ->order('productId DESC'); + + $expected = 'SELECT "p".* FROM "product" AS "p" ORDER BY "productId" DESC'; + $this->assertEquals($expected, $select->assemble(), + 'Order direction of field failed'); + } + + /** + * @group ZF-378 + */ + public function testOrderOfMultiFieldWithDirection() + { + $select = $this->_db->select(); + $select->from(array ('p' => 'product')) + ->order(array ('productId DESC', 'userId ASC')); + + $expected = 'SELECT "p".* FROM "product" AS "p" ORDER BY "productId" DESC, "userId" ASC'; + $this->assertEquals($expected, $select->assemble(), + 'Order direction of field failed'); + } + + /** + * @group ZF-378 + */ + public function testOrderOfMultiFieldButOnlyOneWithDirection() + { + $select = $this->_db->select(); + $select->from(array ('p' => 'product')) + ->order(array ('productId', 'userId DESC')); + + $expected = 'SELECT "p".* FROM "product" AS "p" ORDER BY "productId" ASC, "userId" DESC'; + $this->assertEquals($expected, $select->assemble(), + 'Order direction of field failed'); + } + + /** + * @group ZF-378 + * @group ZF-381 + */ + public function testOrderOfConditionalFieldWithDirection($adapter) + { + $select = $this->_db->select(); + $select->from(array ('p' => 'product')) + ->order('IF("productId" > 5,1,0) ASC'); + + $expected = 'SELECT "p".* FROM "product" AS "p" ORDER BY IF("productId" > 5,1,0) ASC'; + $this->assertEquals($expected, $select->assemble(), + 'Order direction of field failed'); + } } diff --git a/tests/Zend/Db/Select/TestCommon.php b/tests/Zend/Db/Select/TestCommon.php index 1a91f2bb11..47a42792cd 100644 --- a/tests/Zend/Db/Select/TestCommon.php +++ b/tests/Zend/Db/Select/TestCommon.php @@ -1767,4 +1767,61 @@ public function testSqlInjectionWithOrder() $select->from(array('p' => 'products'))->order('name;select;MD5(1)'); $this->assertEquals('SELECT `p`.* FROM `products` AS `p` ORDER BY `name;select;MD5(1)` ASC', $select->assemble()); } + + /** + * @group ZF-378 + */ + public function testOrderOfSingleFieldWithDirection() + { + $select = $this->_db->select(); + $select->from(array ('p' => 'product')) + ->order('productId DESC'); + + $expected = 'SELECT `p`.* FROM `product` AS `p` ORDER BY `productId` DESC'; + $this->assertEquals($expected, $select->assemble(), + 'Order direction of field failed'); + } + + /** + * @group ZF-378 + */ + public function testOrderOfMultiFieldWithDirection() + { + $select = $this->_db->select(); + $select->from(array ('p' => 'product')) + ->order(array ('productId DESC', 'userId ASC')); + + $expected = 'SELECT `p`.* FROM `product` AS `p` ORDER BY `productId` DESC, `userId` ASC'; + $this->assertEquals($expected, $select->assemble(), + 'Order direction of field failed'); + } + + /** + * @group ZF-378 + */ + public function testOrderOfMultiFieldButOnlyOneWithDirection() + { + $select = $this->_db->select(); + $select->from(array ('p' => 'product')) + ->order(array ('productId', 'userId DESC')); + + $expected = 'SELECT `p`.* FROM `product` AS `p` ORDER BY `productId` ASC, `userId` DESC'; + $this->assertEquals($expected, $select->assemble(), + 'Order direction of field failed'); + } + + /** + * @group ZF-378 + * @group ZF-381 + */ + public function testOrderOfConditionalFieldWithDirection($adapter) + { + $select = $this->_db->select(); + $select->from(array ('p' => 'product')) + ->order('IF(`productId` > 5,1,0) ASC'); + + $expected = 'SELECT `p`.* FROM `product` AS `p` ORDER BY IF(`productId` > 5,1,0) ASC'; + $this->assertEquals($expected, $select->assemble(), + 'Order direction of field failed'); + } } diff --git a/tests/Zend/Db/Table/Select/Pdo/PgsqlTest.php b/tests/Zend/Db/Table/Select/Pdo/PgsqlTest.php index 91e356a415..e48b4fb25c 100644 --- a/tests/Zend/Db/Table/Select/Pdo/PgsqlTest.php +++ b/tests/Zend/Db/Table/Select/Pdo/PgsqlTest.php @@ -141,4 +141,61 @@ public function testSqlInjectionWithOrder() $select->from(array('p' => 'products'))->order('name;select;MD5(1)'); $this->assertEquals('SELECT "p".* FROM "products" AS "p" ORDER BY "name;select;MD5(1)" ASC', $select->assemble()); } + + /** + * @group ZF-378 + */ + public function testOrderOfSingleFieldWithDirection() + { + $select = $this->_db->select(); + $select->from(array ('p' => 'product')) + ->order('productId DESC'); + + $expected = 'SELECT "p".* FROM "product" AS "p" ORDER BY "productId" DESC'; + $this->assertEquals($expected, $select->assemble(), + 'Order direction of field failed'); + } + + /** + * @group ZF-378 + */ + public function testOrderOfMultiFieldWithDirection() + { + $select = $this->_db->select(); + $select->from(array ('p' => 'product')) + ->order(array ('productId DESC', 'userId ASC')); + + $expected = 'SELECT "p".* FROM "product" AS "p" ORDER BY "productId" DESC, "userId" ASC'; + $this->assertEquals($expected, $select->assemble(), + 'Order direction of field failed'); + } + + /** + * @group ZF-378 + */ + public function testOrderOfMultiFieldButOnlyOneWithDirection() + { + $select = $this->_db->select(); + $select->from(array ('p' => 'product')) + ->order(array ('productId', 'userId DESC')); + + $expected = 'SELECT "p".* FROM "product" AS "p" ORDER BY "productId" ASC, "userId" DESC'; + $this->assertEquals($expected, $select->assemble(), + 'Order direction of field failed'); + } + + /** + * @group ZF-378 + * @group ZF-381 + */ + public function testOrderOfConditionalFieldWithDirection($adapter) + { + $select = $this->_db->select(); + $select->from(array ('p' => 'product')) + ->order('IF("productId" > 5,1,0) ASC'); + + $expected = 'SELECT "p".* FROM "product" AS "p" ORDER BY IF("productId" > 5,1,0) ASC'; + $this->assertEquals($expected, $select->assemble(), + 'Order direction of field failed'); + } } diff --git a/tests/Zend/Db/Table/Select/Pdo/SqliteTest.php b/tests/Zend/Db/Table/Select/Pdo/SqliteTest.php index 9f6c14b5e1..8d949c2a1e 100644 --- a/tests/Zend/Db/Table/Select/Pdo/SqliteTest.php +++ b/tests/Zend/Db/Table/Select/Pdo/SqliteTest.php @@ -189,4 +189,61 @@ public function testSqlInjectionWithOrder() $this->assertEquals('SELECT "p".* FROM "products" AS "p" ORDER BY "name;select;MD5(1)" ASC', $select->assemble()); } + /** + * @group ZF-378 + */ + public function testOrderOfSingleFieldWithDirection() + { + $select = $this->_db->select(); + $select->from(array ('p' => 'product')) + ->order('productId DESC'); + + $expected = 'SELECT "p".* FROM "product" AS "p" ORDER BY "productId" DESC'; + $this->assertEquals($expected, $select->assemble(), + 'Order direction of field failed'); + } + + /** + * @group ZF-378 + */ + public function testOrderOfMultiFieldWithDirection() + { + $select = $this->_db->select(); + $select->from(array ('p' => 'product')) + ->order(array ('productId DESC', 'userId ASC')); + + $expected = 'SELECT "p".* FROM "product" AS "p" ORDER BY "productId" DESC, "userId" ASC'; + $this->assertEquals($expected, $select->assemble(), + 'Order direction of field failed'); + } + + /** + * @group ZF-378 + */ + public function testOrderOfMultiFieldButOnlyOneWithDirection() + { + $select = $this->_db->select(); + $select->from(array ('p' => 'product')) + ->order(array ('productId', 'userId DESC')); + + $expected = 'SELECT "p".* FROM "product" AS "p" ORDER BY "productId" ASC, "userId" DESC'; + $this->assertEquals($expected, $select->assemble(), + 'Order direction of field failed'); + } + + /** + * @group ZF-378 + * @group ZF-381 + */ + public function testOrderOfConditionalFieldWithDirection($adapter) + { + $select = $this->_db->select(); + $select->from(array ('p' => 'product')) + ->order('IF("productId" > 5,1,0) ASC'); + + $expected = 'SELECT "p".* FROM "product" AS "p" ORDER BY IF("productId" > 5,1,0) ASC'; + $this->assertEquals($expected, $select->assemble(), + 'Order direction of field failed'); + } + } diff --git a/tests/Zend/Db/Table/Select/StaticTest.php b/tests/Zend/Db/Table/Select/StaticTest.php index 93629efa08..c4c3152e1f 100644 --- a/tests/Zend/Db/Table/Select/StaticTest.php +++ b/tests/Zend/Db/Table/Select/StaticTest.php @@ -708,4 +708,61 @@ public function testSqlInjectionWithOrder() $this->assertEquals('SELECT "p".* FROM "products" AS "p" ORDER BY "name;select;MD5(1)" ASC', $select->assemble()); } + /** + * @group ZF-378 + */ + public function testOrderOfSingleFieldWithDirection() + { + $select = $this->_db->select(); + $select->from(array ('p' => 'product')) + ->order('productId DESC'); + + $expected = 'SELECT "p".* FROM "product" AS "p" ORDER BY "productId" DESC'; + $this->assertEquals($expected, $select->assemble(), + 'Order direction of field failed'); + } + + /** + * @group ZF-378 + */ + public function testOrderOfMultiFieldWithDirection() + { + $select = $this->_db->select(); + $select->from(array ('p' => 'product')) + ->order(array ('productId DESC', 'userId ASC')); + + $expected = 'SELECT "p".* FROM "product" AS "p" ORDER BY "productId" DESC, "userId" ASC'; + $this->assertEquals($expected, $select->assemble(), + 'Order direction of field failed'); + } + + /** + * @group ZF-378 + */ + public function testOrderOfMultiFieldButOnlyOneWithDirection() + { + $select = $this->_db->select(); + $select->from(array ('p' => 'product')) + ->order(array ('productId', 'userId DESC')); + + $expected = 'SELECT "p".* FROM "product" AS "p" ORDER BY "productId" ASC, "userId" DESC'; + $this->assertEquals($expected, $select->assemble(), + 'Order direction of field failed'); + } + + /** + * @group ZF-378 + * @group ZF-381 + */ + public function testOrderOfConditionalFieldWithDirection($adapter) + { + $select = $this->_db->select(); + $select->from(array ('p' => 'product')) + ->order('IF("productId" > 5,1,0) ASC'); + + $expected = 'SELECT "p".* FROM "product" AS "p" ORDER BY IF("productId" > 5,1,0) ASC'; + $this->assertEquals($expected, $select->assemble(), + 'Order direction of field failed'); + } + } From 279b682419ad4a80e502db0914335faebbbe68d0 Mon Sep 17 00:00:00 2001 From: Enrico Zimuel Date: Wed, 18 Jun 2014 16:25:31 +0200 Subject: [PATCH 2/2] Fixed the unit tests for issue #378 --- tests/Zend/Db/Select/Pdo/PgsqlTest.php | 2 +- tests/Zend/Db/Select/Pdo/SqliteTest.php | 2 +- tests/Zend/Db/Select/StaticTest.php | 2 +- tests/Zend/Db/Select/TestCommon.php | 2 +- tests/Zend/Db/Table/Select/Pdo/PgsqlTest.php | 2 +- tests/Zend/Db/Table/Select/Pdo/SqliteTest.php | 2 +- tests/Zend/Db/Table/Select/StaticTest.php | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/Zend/Db/Select/Pdo/PgsqlTest.php b/tests/Zend/Db/Select/Pdo/PgsqlTest.php index d27fb871e0..596f2b7e88 100644 --- a/tests/Zend/Db/Select/Pdo/PgsqlTest.php +++ b/tests/Zend/Db/Select/Pdo/PgsqlTest.php @@ -184,7 +184,7 @@ public function testOrderOfMultiFieldButOnlyOneWithDirection() * @group ZF-378 * @group ZF-381 */ - public function testOrderOfConditionalFieldWithDirection($adapter) + public function testOrderOfConditionalFieldWithDirection() { $select = $this->_db->select(); $select->from(array ('p' => 'product')) diff --git a/tests/Zend/Db/Select/Pdo/SqliteTest.php b/tests/Zend/Db/Select/Pdo/SqliteTest.php index 2794f8f078..6d02f2fc1a 100644 --- a/tests/Zend/Db/Select/Pdo/SqliteTest.php +++ b/tests/Zend/Db/Select/Pdo/SqliteTest.php @@ -231,7 +231,7 @@ public function testOrderOfMultiFieldButOnlyOneWithDirection() * @group ZF-378 * @group ZF-381 */ - public function testOrderOfConditionalFieldWithDirection($adapter) + public function testOrderOfConditionalFieldWithDirection() { $select = $this->_db->select(); $select->from(array ('p' => 'product')) diff --git a/tests/Zend/Db/Select/StaticTest.php b/tests/Zend/Db/Select/StaticTest.php index f6cd44b967..2d69534936 100644 --- a/tests/Zend/Db/Select/StaticTest.php +++ b/tests/Zend/Db/Select/StaticTest.php @@ -878,7 +878,7 @@ public function testOrderOfMultiFieldButOnlyOneWithDirection() * @group ZF-378 * @group ZF-381 */ - public function testOrderOfConditionalFieldWithDirection($adapter) + public function testOrderOfConditionalFieldWithDirection() { $select = $this->_db->select(); $select->from(array ('p' => 'product')) diff --git a/tests/Zend/Db/Select/TestCommon.php b/tests/Zend/Db/Select/TestCommon.php index 47a42792cd..1d8139c964 100644 --- a/tests/Zend/Db/Select/TestCommon.php +++ b/tests/Zend/Db/Select/TestCommon.php @@ -1814,7 +1814,7 @@ public function testOrderOfMultiFieldButOnlyOneWithDirection() * @group ZF-378 * @group ZF-381 */ - public function testOrderOfConditionalFieldWithDirection($adapter) + public function testOrderOfConditionalFieldWithDirection() { $select = $this->_db->select(); $select->from(array ('p' => 'product')) diff --git a/tests/Zend/Db/Table/Select/Pdo/PgsqlTest.php b/tests/Zend/Db/Table/Select/Pdo/PgsqlTest.php index e48b4fb25c..42017bd609 100644 --- a/tests/Zend/Db/Table/Select/Pdo/PgsqlTest.php +++ b/tests/Zend/Db/Table/Select/Pdo/PgsqlTest.php @@ -188,7 +188,7 @@ public function testOrderOfMultiFieldButOnlyOneWithDirection() * @group ZF-378 * @group ZF-381 */ - public function testOrderOfConditionalFieldWithDirection($adapter) + public function testOrderOfConditionalFieldWithDirection() { $select = $this->_db->select(); $select->from(array ('p' => 'product')) diff --git a/tests/Zend/Db/Table/Select/Pdo/SqliteTest.php b/tests/Zend/Db/Table/Select/Pdo/SqliteTest.php index 8d949c2a1e..ba5b1f4133 100644 --- a/tests/Zend/Db/Table/Select/Pdo/SqliteTest.php +++ b/tests/Zend/Db/Table/Select/Pdo/SqliteTest.php @@ -235,7 +235,7 @@ public function testOrderOfMultiFieldButOnlyOneWithDirection() * @group ZF-378 * @group ZF-381 */ - public function testOrderOfConditionalFieldWithDirection($adapter) + public function testOrderOfConditionalFieldWithDirection() { $select = $this->_db->select(); $select->from(array ('p' => 'product')) diff --git a/tests/Zend/Db/Table/Select/StaticTest.php b/tests/Zend/Db/Table/Select/StaticTest.php index c4c3152e1f..e23ac251ee 100644 --- a/tests/Zend/Db/Table/Select/StaticTest.php +++ b/tests/Zend/Db/Table/Select/StaticTest.php @@ -754,7 +754,7 @@ public function testOrderOfMultiFieldButOnlyOneWithDirection() * @group ZF-378 * @group ZF-381 */ - public function testOrderOfConditionalFieldWithDirection($adapter) + public function testOrderOfConditionalFieldWithDirection() { $select = $this->_db->select(); $select->from(array ('p' => 'product'))