| 1 | ### Eclipse Workspace Patch 1.0 |
|---|
| 2 | #P wordpress-trunk |
|---|
| 3 | Index: wp-content/plugins/debug-11608-tests.php |
|---|
| 4 | =================================================================== |
|---|
| 5 | --- wp-content/plugins/debug-11608-tests.php (revision 0) |
|---|
| 6 | +++ wp-content/plugins/debug-11608-tests.php (revision 0) |
|---|
| 7 | @@ -0,0 +1,163 @@ |
|---|
| 8 | +<?php |
|---|
| 9 | +/** |
|---|
| 10 | + * @link http://core.trac.wordpress.org/ticket/11608 |
|---|
| 11 | + */ |
|---|
| 12 | + |
|---|
| 13 | +print "<h2>Data Integrity Tests:</h2><br />\n"; |
|---|
| 14 | + |
|---|
| 15 | +/* |
|---|
| 16 | + * test: number of arguments parsing |
|---|
| 17 | + * |
|---|
| 18 | + * must all return a string when correct, false if it does not match |
|---|
| 19 | + */ |
|---|
| 20 | +$testdata = array( |
|---|
| 21 | + array( false, '% wrong query' , 0 ), # invalid |
|---|
| 22 | + array( true, '%d%d' , 2 ), |
|---|
| 23 | + array( true, '%s %d' , 2 ), |
|---|
| 24 | + array( true, '%s %d ' , 2 ), |
|---|
| 25 | + array( true, '%s%s%s' , 3 ), |
|---|
| 26 | + array( true, '%s%s%s%s' , 4 ), |
|---|
| 27 | + array( true, '%d%%%%%%%%%s%s%s' , 4 ), |
|---|
| 28 | + array( false, 'false%d%d' , 0 ), |
|---|
| 29 | + array( false, 'false%s %d' , 0 ), |
|---|
| 30 | + array( false, 'false%s %d ' , 0 ), |
|---|
| 31 | + array( false, 'false%s%s%s' , 0 ), |
|---|
| 32 | + array( false, 'false%s%s%s%s' , 0 ), |
|---|
| 33 | + array( false, 'false%d%%%%%%%%%s%s%s' , 0, ) |
|---|
| 34 | +); |
|---|
| 35 | + |
|---|
| 36 | +foreach ( $testdata as $test ) { |
|---|
| 37 | + list( $valid, $query, $args ) = $test; |
|---|
| 38 | + $return = $wpdb->_prepare_quote_lits( $query, true, $args ); |
|---|
| 39 | + $result = ( $return === false ) ? false : true; |
|---|
| 40 | + printf("TEST: %s _prepare_quote_lits( '%s', true, %d)<br />\n", $result == $valid ? 'PASS' : 'FAIL', $query, $args); |
|---|
| 41 | +} |
|---|
| 42 | + |
|---|
| 43 | + |
|---|
| 44 | +/* |
|---|
| 45 | + * test: substitution tests |
|---|
| 46 | + * |
|---|
| 47 | + * must return the predefined value, strict checking |
|---|
| 48 | + */ |
|---|
| 49 | +$testdata = array( |
|---|
| 50 | + array( '', '' ), |
|---|
| 51 | + array( '%', false ), |
|---|
| 52 | + array( '%%', '%%' ), |
|---|
| 53 | + array( '%s', '\'%s\'' ), |
|---|
| 54 | + array( '%%s', '%%s' ), |
|---|
| 55 | + array( '%%%s', '%%\'%s\'' ), |
|---|
| 56 | + array( '%d', '%d' ), |
|---|
| 57 | + array( '%-' , false), |
|---|
| 58 | + array( '%d%s%%', '%d\'%s\'%%' ), |
|---|
| 59 | + array( '%%d%s%%', '%%d\'%s\'%%'), |
|---|
| 60 | + array( '%%%d%s%%', '%%%d\'%s\'%%' ), |
|---|
| 61 | + array( '%%%d%%s%%','%%%d%%s%%' ), |
|---|
| 62 | + array( '%%d%%s%', false ), |
|---|
| 63 | + array( 'percentage stupid or %stupid is the question', 'percentage stupid or \'%s\'tupid is the question' ), |
|---|
| 64 | + array( 'percentage stupid or %%stupid is the question', 'percentage stupid or %%stupid is the question' ), |
|---|
| 65 | + array( 'SELECT FROM t1 WHERE a LIKE (%s)', 'SELECT FROM t1 WHERE a LIKE (\'%s\')' ), |
|---|
| 66 | + array( 'SELECT FROM t1 WHERE a = %s', 'SELECT FROM t1 WHERE a = \'%s\'' ), |
|---|
| 67 | + array( "SELECT 1 WHERE table.row LIKE '%stupid' AND othertable.row = %s", "SELECT 1 WHERE table.row LIKE ''%s'tupid' AND othertable.row = '%s'" ), |
|---|
| 68 | + array( "SELECT 1 WHERE table.row LIKE '%%stupid' AND othertable.row = %s", "SELECT 1 WHERE table.row LIKE '%%stupid' AND othertable.row = '%s'" ), |
|---|
| 69 | + 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'" ), |
|---|
| 70 | +); |
|---|
| 71 | + |
|---|
| 72 | +foreach ( $testdata as $test ) { |
|---|
| 73 | + list( $query, $valid ) = $test; |
|---|
| 74 | + $return = $wpdb->_prepare_quote_lits( $query, true ); |
|---|
| 75 | + printf("TEST: %s _prepare_quote_lits( '%s', true)<br />\n", $return !== $valid ? 'FAIL' : 'PASS', $query); |
|---|
| 76 | +} |
|---|
| 77 | + |
|---|
| 78 | +print "<h2>Functional Tests on wpdb->prepare():</h2><br />\n"; |
|---|
| 79 | + |
|---|
| 80 | +/* |
|---|
| 81 | + * test: run prepare() |
|---|
| 82 | + * |
|---|
| 83 | + * should return false and throw an error on debug in case a |
|---|
| 84 | + * query is invalid (fixed behaviour) |
|---|
| 85 | + * |
|---|
| 86 | + * should return null|false in case a query is invalid (broken, |
|---|
| 87 | + * old behaviour) |
|---|
| 88 | + */ |
|---|
| 89 | +$tests = array( |
|---|
| 90 | + '% wrong query' => array( '' ), # invalid |
|---|
| 91 | + 'SELECT * FROM `table` WHERE `column` = %s AND `field` = %d' => array( 'foo', 1337 ), # valid |
|---|
| 92 | + "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s" => array( 'foo' ), # valid |
|---|
| 93 | + "SELECT DATE_FORMAT(`field`, '%c') FROM `table` WHERE `column` = %s" => array( 'foo' ), # invalid |
|---|
| 94 | +); |
|---|
| 95 | + |
|---|
| 96 | +foreach ( $tests as $query => $args) { |
|---|
| 97 | + printf("TEST: \$wpdb->prepare( '%s', %s )<br />\n", $query, print_r($args, true) ); |
|---|
| 98 | + var_dump( $wpdb->prepare( $query, $args ) ); |
|---|
| 99 | +} |
|---|
| 100 | + |
|---|
| 101 | +print "<h2>Functional Tests on wpdb->_prepare_quote_lits():</h2><br />\n"; |
|---|
| 102 | + |
|---|
| 103 | +/* |
|---|
| 104 | + * test: quote lits |
|---|
| 105 | + * |
|---|
| 106 | + * test the concrete quote routine for query parameter |
|---|
| 107 | + */ |
|---|
| 108 | +$tests = array( |
|---|
| 109 | + '', |
|---|
| 110 | + '%', |
|---|
| 111 | + '%%', |
|---|
| 112 | + '%s', |
|---|
| 113 | + '%%s', |
|---|
| 114 | + '%%%s', |
|---|
| 115 | + '%d', |
|---|
| 116 | + '%-', |
|---|
| 117 | + '%d%s%%', |
|---|
| 118 | + '%%d%s%%', |
|---|
| 119 | + '%%%d%s%%', |
|---|
| 120 | + '%%%d%%s%%', |
|---|
| 121 | + '%%d%%s%', |
|---|
| 122 | + 'percentage stupid or %stupid is the question', |
|---|
| 123 | + 'percentage stupid or %%stupid is the question', |
|---|
| 124 | + 'SELECT FROM t1 WHERE a LIKE (%s)', |
|---|
| 125 | + 'SELECT FROM t1 WHERE a = %s', |
|---|
| 126 | + "SELECT 1 WHERE table.row LIKE '%stupid' AND othertable.row = %s", |
|---|
| 127 | + "SELECT 1 WHERE table.row LIKE '%stupid' AND othertable.row = %s", |
|---|
| 128 | + "SELECT 1 WHERE table.row LIKE '%%stupid' AND othertable.row = %s", |
|---|
| 129 | + "SELECT 1 WHERE table.row LIKE '%%stupidisas%%stupiddoes' AND othertable.row = %s", |
|---|
| 130 | +); |
|---|
| 131 | + |
|---|
| 132 | +foreach( $tests as $query ) { |
|---|
| 133 | + printf(" '%s' -> '%s' (Syntax: %s)<br />\n", $query, $wpdb->_prepare_quote_lits( $query ), ( false === $wpdb->_prepare_quote_lits( $query, true) ? 'Error' : 'Ok' ) ) ; |
|---|
| 134 | +} |
|---|
| 135 | + |
|---|
| 136 | + |
|---|
| 137 | +print "<h2>Test Error Message:</h2><br />\n"; |
|---|
| 138 | + |
|---|
| 139 | +/* |
|---|
| 140 | + * test: error message |
|---|
| 141 | + * |
|---|
| 142 | + * wpdb should report an error on a wrong query in prepare |
|---|
| 143 | + */ |
|---|
| 144 | +$wpdb->suppress_errors(false); |
|---|
| 145 | +$wpdb->show_errors(true); |
|---|
| 146 | +$wpdb->prepare( $query = '% wrong query' ); |
|---|
| 147 | + |
|---|
| 148 | +print "<h2>Regression Test:</h2><br />\n"; |
|---|
| 149 | + |
|---|
| 150 | +/* |
|---|
| 151 | + * test: show what's wrong with the function |
|---|
| 152 | + * |
|---|
| 153 | + * NOTE: only for old, broken routine |
|---|
| 154 | + */ |
|---|
| 155 | +print "TEST: \$wpdb->prepare( $query = '% wrong query', '' ) <i>Must not return string</i><br />\n"; |
|---|
| 156 | +var_dump( $wpdb->prepare( $query = '% wrong query', '' ) ); |
|---|
| 157 | + |
|---|
| 158 | + |
|---|
| 159 | +print "<h2>Simple Run Test:</h2><br />\n"; |
|---|
| 160 | + |
|---|
| 161 | +/* |
|---|
| 162 | + * test: just run |
|---|
| 163 | + * |
|---|
| 164 | + * should return null on broken implementation, false on fixed one |
|---|
| 165 | + */ |
|---|
| 166 | +print "TEST: \$wpdb->prepare() <i>Should return false</i><br />\n"; |
|---|
| 167 | +var_dump( $wpdb->prepare() ); |
|---|
| 168 | + |
|---|
| 169 | + |
|---|
| 170 | +die(); |
|---|
| 171 | \ No newline at end of file |
|---|