### Eclipse Workspace Patch 1.0
#P wordpress-trunk
Index: wp-content/plugins/debug-11608-tests.php
===================================================================
--- wp-content/plugins/debug-11608-tests.php	(revision 0)
+++ wp-content/plugins/debug-11608-tests.php	(revision 0)
@@ -0,0 +1,163 @@
+<?php
+/**
+ * @link http://core.trac.wordpress.org/ticket/11608
+ */
+
+print "<h2>Data Integrity Tests:</h2><br />\n";
+
+/*
+ * test: number of arguments parsing
+ * 
+ * must all return a string when correct, false if it does not match
+ */
+$testdata = array(
+	array( false,  '% wrong query' , 0 ), # invalid
+	array( true,  '%d%d'          , 2 ),
+	array( true,  '%s %d'         , 2 ),
+	array( true,  '%s %d '        , 2 ),
+	array( true,  '%s%s%s'        , 3 ), 
+	array( true,  '%s%s%s%s'      , 4 ),
+	array( true,  '%d%%%%%%%%%s%s%s' , 4 ),
+	array( false, 'false%d%d'     , 0 ),
+	array( false, 'false%s %d'    , 0 ),
+	array( false, 'false%s %d '   , 0 ),
+	array( false, 'false%s%s%s'   , 0 ), 
+	array( false, 'false%s%s%s%s' , 0 ),
+	array( false, 'false%d%%%%%%%%%s%s%s' , 0, )
+);
+
+foreach ( $testdata as $test ) {
+	list( $valid, $query, $args ) = $test;	
+	$return = $wpdb->_prepare_quote_lits( $query, true, $args );
+	$result = ( $return === false ) ? false : true;	
+	printf("TEST: %s _prepare_quote_lits( '%s', true, %d)<br />\n", $result == $valid ? 'PASS' : 'FAIL', $query, $args);	
+}
+
+
+/*
+ * test: substitution tests
+ * 
+ * must return the predefined value, strict checking
+ */
+$testdata = array(
+	array( '',         '' ),
+	array( '%',        false ),
+	array( '%%',       '%%' ),
+	array( '%s',       '\'%s\'' ),
+	array( '%%s',      '%%s' ),
+	array( '%%%s',     '%%\'%s\'' ),
+	array( '%d',       '%d' ),
+	array( '%-' ,      false),
+	array( '%d%s%%',   '%d\'%s\'%%' ),
+	array( '%%d%s%%',  '%%d\'%s\'%%'),
+	array( '%%%d%s%%', '%%%d\'%s\'%%' ),	
+	array( '%%%d%%s%%','%%%d%%s%%' ),
+	array( '%%d%%s%',  false ),
+	array( 'percentage stupid or %stupid is the question',  'percentage stupid or \'%s\'tupid is the question' ),
+	array( 'percentage stupid or %%stupid is the question', 'percentage stupid or %%stupid is the question' ),
+	array( 'SELECT FROM t1 WHERE a LIKE (%s)', 'SELECT FROM t1 WHERE a LIKE (\'%s\')' ),
+	array( 'SELECT FROM t1 WHERE a = %s', 'SELECT FROM t1 WHERE a = \'%s\'' ),
+	array( "SELECT 1 WHERE table.row LIKE '%stupid' AND othertable.row = %s",  "SELECT 1 WHERE table.row LIKE ''%s'tupid' AND othertable.row = '%s'" ),
+	array( "SELECT 1 WHERE table.row LIKE '%%stupid' AND othertable.row = %s", "SELECT 1 WHERE table.row LIKE '%%stupid' AND othertable.row = '%s'" ),
+	array( "SELECT 1 WHERE table.row LIKE '%%stupidisas%%stupiddoes' AND othertable.row = %s", "SELECT 1 WHERE table.row LIKE '%%stupidisas%%stupiddoes' AND othertable.row = '%s'" ),
+);
+
+foreach ( $testdata as $test ) {
+	list( $query, $valid ) = $test;	
+	$return = $wpdb->_prepare_quote_lits( $query, true );
+	printf("TEST: %s _prepare_quote_lits( '%s', true)<br />\n", $return !== $valid ? 'FAIL' : 'PASS', $query);	
+}
+
+print "<h2>Functional Tests on wpdb->prepare():</h2><br />\n";
+
+/*
+ * test: run prepare()
+ * 
+ * should return false and throw an error on debug in case a
+ * query is invalid (fixed behaviour)
+ * 
+ * should return null|false in case a query is invalid (broken,
+ * old behaviour)
+ */
+$tests = array(
+	'% wrong query' => array( '' ), # invalid
+	'SELECT * FROM `table` WHERE `column` = %s AND `field` = %d' => array( 'foo', 1337 ), # valid 
+	"SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s" => array( 'foo' ), # valid 
+	"SELECT DATE_FORMAT(`field`, '%c') FROM `table` WHERE `column` = %s" => array( 'foo' ), # invalid
+);
+
+foreach ( $tests as $query => $args) {
+	printf("TEST: \$wpdb->prepare( '%s', %s )<br />\n", $query, print_r($args, true) );	
+	var_dump( $wpdb->prepare( $query, $args ) );
+}
+
+print "<h2>Functional Tests on wpdb->_prepare_quote_lits():</h2><br />\n";
+
+/*
+ * test: quote lits 
+ * 
+ * test the concrete quote routine for query parameter
+ */
+$tests = array(
+	'',
+	'%',
+	'%%',
+	'%s',
+	'%%s',
+	'%%%s',
+	'%d',
+	'%-',
+	'%d%s%%',
+	'%%d%s%%',
+	'%%%d%s%%',	
+	'%%%d%%s%%',
+	'%%d%%s%',
+	'percentage stupid or %stupid is the question',
+	'percentage stupid or %%stupid is the question',	
+	'SELECT FROM t1 WHERE a LIKE (%s)',
+	'SELECT FROM t1 WHERE a = %s',
+	"SELECT 1 WHERE table.row LIKE '%stupid' AND othertable.row = %s",
+	"SELECT 1 WHERE table.row LIKE '%stupid' AND othertable.row = %s",
+	"SELECT 1 WHERE table.row LIKE '%%stupid' AND othertable.row = %s",
+	"SELECT 1 WHERE table.row LIKE '%%stupidisas%%stupiddoes' AND othertable.row = %s",
+);
+
+foreach( $tests as $query ) {
+	printf("   '%s' -> '%s' (Syntax: %s)<br />\n", $query, $wpdb->_prepare_quote_lits( $query ), ( false === $wpdb->_prepare_quote_lits( $query, true) ? 'Error' : 'Ok' ) ) ;		
+}
+
+
+print "<h2>Test Error Message:</h2><br />\n";
+
+/*
+ * test: error message 
+ * 
+ * wpdb should report an error on a wrong query in prepare
+ */
+$wpdb->suppress_errors(false);
+$wpdb->show_errors(true);
+$wpdb->prepare( $query = '% wrong query' );
+
+print "<h2>Regression Test:</h2><br />\n";
+
+/*
+ * test: show what's wrong with the function
+ * 
+ * NOTE: only for old, broken routine
+ */
+print "TEST: \$wpdb->prepare( $query = '% wrong query', '' ) <i>Must not return string</i><br />\n";
+var_dump( $wpdb->prepare( $query = '% wrong query', '' ) );
+
+
+print "<h2>Simple Run Test:</h2><br />\n";
+
+/*
+ * test: just run
+ * 
+ * should return null on broken implementation, false on fixed one
+ */
+print "TEST: \$wpdb->prepare() <i>Should return false</i><br />\n";
+var_dump( $wpdb->prepare() );
+
+
+die();
\ No newline at end of file
