[BUGFIX] "stripOrderBy, stripGroupBy" should remove double occurrence 30/17630/3
authorLaurent Cherpit <lcherpit@ttree.ch>
Sat, 6 Oct 2012 16:16:43 +0000 (18:16 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Sat, 13 Apr 2013 08:44:56 +0000 (10:44 +0200)
Sometimes the string handed over to stripOrderBy includes the ORDER BY
twice.
Tweak the regular expression to detect and fix that kind of errors.

The same applies to stripGroupBy.

Fixes: #41641
Releases: 6.0, 4.7, 4.6, 4.5
Change-Id: Ib1829fbf57ec281a54b5f940211a86c52723cfbe
Reviewed-on: https://review.typo3.org/17630
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
t3lib/class.t3lib_db.php
tests/t3lib/t3lib_dbTest.php

index 513cbf7..d8c9e58 100644 (file)
@@ -867,7 +867,7 @@ class t3lib_DB {
         * @see exec_SELECTquery(), stripGroupBy()
         */
        function stripOrderBy($str) {
-               return preg_replace('/^ORDER[[:space:]]+BY[[:space:]]+/i', '', trim($str));
+               return preg_replace('/^(?:ORDER[[:space:]]*BY[[:space:]]*)+/i', '', trim($str));
        }
 
        /**
@@ -881,7 +881,7 @@ class t3lib_DB {
         * @see exec_SELECTquery(), stripOrderBy()
         */
        function stripGroupBy($str) {
-               return preg_replace('/^GROUP[[:space:]]+BY[[:space:]]+/i', '', trim($str));
+               return preg_replace('/^(?:GROUP[[:space:]]*BY[[:space:]]*)+/i', '', trim($str));
        }
 
        /**
index 7c2271f..e27d7d5 100644 (file)
@@ -72,7 +72,7 @@ class t3lib_dbTest extends tx_phpunit_testcase {
                for ($i = 0; $i < 256; $i ++) {
                        $binaryString .= chr($i);
                }
-               
+
                $this->fixture->exec_INSERTquery(
                        $this->testTable,
                        array('fieldblob' => $binaryString)
@@ -149,5 +149,78 @@ class t3lib_dbTest extends tx_phpunit_testcase {
                        $this->fixture->escapeStrForLike('foo_bar%', 'table')
                );
        }
+
+       /////////////////////////////////////////////////
+       // Tests concerning stripOrderByForOrderByKeyword
+       /////////////////////////////////////////////////
+
+
+       /**
+        * Data Provider for stripGroupByForGroupByKeyword()
+        *
+        * @see stripOrderByForOrderByKeyword()
+        * @return array
+        */
+       public function stripOrderByForOrderByKeywordDataProvider() {
+               return array(
+                       'single ORDER BY' => array('ORDER BY name, tstamp', 'name, tstamp'),
+                       'single ORDER BY in lower case' => array('order by name, tstamp', 'name, tstamp'),
+                       'ORDER BY with additional space behind' => array('ORDER BY  name, tstamp', 'name, tstamp'),
+                       'ORDER BY without space between the words' => array('ORDERBY name, tstamp', 'name, tstamp'),
+                       'ORDER BY added twice' => array('ORDER BY ORDER BY name, tstamp', 'name, tstamp'),
+                       'ORDER BY added twice without spaces in the first occurrence' => array('ORDERBY ORDER BY  name, tstamp', 'name, tstamp'),
+                       'ORDER BY added twice without spaces in the second occurrence' => array('ORDER BYORDERBY name, tstamp', 'name, tstamp'),
+                       'ORDER BY added twice without spaces' => array('ORDERBYORDERBY name, tstamp', 'name, tstamp'),
+                       'ORDER BY added twice without spaces afterwards' => array('ORDERBYORDERBYname, tstamp', 'name, tstamp'),
+               );
+       }
+
+       /**
+        * @test
+        * @dataProvider stripOrderByForOrderByKeywordDataProvider
+        * @param string $orderByClause The clause to test
+        * @param string $expectedResult The expected result
+        * @return void
+        */
+       public function stripOrderByForOrderByKeyword($orderByClause, $expectedResult) {
+               $strippedQuery = $this->fixture->stripOrderBy($orderByClause);
+               $this->assertEquals($expectedResult, $strippedQuery);
+       }
+
+       /////////////////////////////////////////////////
+       // Tests concerning stripGroupByForGroupByKeyword
+       /////////////////////////////////////////////////
+
+       /**
+        * Data Provider for stripGroupByForGroupByKeyword()
+        *
+        * @see stripGroupByForGroupByKeyword()
+        * @return array
+        */
+       public function stripGroupByForGroupByKeywordDataProvider() {
+               return array(
+                       'single GROUP BY' => array('GROUP BY name, tstamp', 'name, tstamp'),
+                       'single GROUP BY in lower case' => array('group by name, tstamp', 'name, tstamp'),
+                       'GROUP BY with additional space behind' => array('GROUP BY  name, tstamp', 'name, tstamp'),
+                       'GROUP BY without space between the words' => array('GROUPBY name, tstamp', 'name, tstamp'),
+                       'GROUP BY added twice' => array('GROUP BY GROUP BY name, tstamp', 'name, tstamp'),
+                       'GROUP BY added twice without spaces in the first occurrence' => array('GROUPBY GROUP BY  name, tstamp', 'name, tstamp'),
+                       'GROUP BY added twice without spaces in the second occurrence' => array('GROUP BYGROUPBY name, tstamp', 'name, tstamp'),
+                       'GROUP BY added twice without spaces' => array('GROUPBYGROUPBY name, tstamp', 'name, tstamp'),
+                       'GROUP BY added twice without spaces afterwards' => array('GROUPBYGROUPBYname, tstamp', 'name, tstamp'),
+               );
+       }
+
+       /**
+        * @test
+        * @dataProvider stripGroupByForGroupByKeywordDataProvider
+        * @param string $groupByClause The clause to test
+        * @param string $expectedResult The expected result
+        * @return void
+        */
+       public function stripGroupByForGroupByKeyword($groupByClause, $expectedResult) {
+               $strippedQuery = $this->fixture->stripGroupBy($groupByClause);
+               $this->assertEquals($expectedResult, $strippedQuery);
+       }
 }
 ?>
\ No newline at end of file