Make WordPress Core

Ticket #11608: 11608-tests.php

File 11608-tests.php, 2.5 KB (added by hakre, 15 years ago)

Testcases

Line 
1### Eclipse Workspace Patch 1.0
2#P wordpress-trunk
3Index: 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,84 @@
8+<?php
9+/**
10+ * @link http://core.trac.wordpress.org/ticket/11608
11+ */
12+
13+/*
14+ * test: just run
15+ * 
16+ * should return null on broken implementation, false on fixed one
17+ */
18+var_dump( $wpdb->prepare( ) );
19+
20+/*
21+ * test: run prepare()
22+ * 
23+ * should return false and throw an error on debug in case a
24+ * query is invalid (fixed behaviour)
25+ * 
26+ * should return null|false in case a query is invalid (broken,
27+ * old behaviour)
28+ */
29+$tests = array(
30+       '% wrong query' => array( '' ), # invalid
31+       'SELECT * FROM `table` WHERE `column` = %s AND `field` = %d' => array( 'foo', 1337 ), # valid
32+       "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s" => array( 'foo' ), # valid
33+       "SELECT DATE_FORMAT(`field`, '%c') FROM `table` WHERE `column` = %s" => array( 'foo' ), # invalid
34+);
35+
36+foreach ( $tests as $query => $args) {
37+       var_dump( $wpdb->prepare( $query, $args ) );
38+}
39+
40+
41+/*
42+ * test: quote lits 
43+ * 
44+ * test the concrete quote routine for query parameter
45+ */
46+$tests = array(
47+       '',
48+       '%',
49+       '%%',
50+       '%s',
51+       '%%s',
52+       '%%%s',
53+       '%d',
54+       '%-',
55+       '%d%s%%',
56+       '%%d%s%%',
57+       '%%%d%s%%',     
58+       '%%%d%%s%%',
59+       '%%d%%s%',
60+       'percentage stupid or %stupid is the question',
61+       'percentage stupid or %%stupid is the question',       
62+       'SELECT FROM t1 WHERE a LIKE (%s)',
63+       'SELECT FROM t1 WHERE a = %s',
64+       "SELECT 1 WHERE table.row LIKE '%stupid' AND othertable.row = %s",
65+       "SELECT 1 WHERE table.row LIKE '%stupid' AND othertable.row = %s",
66+       "SELECT 1 WHERE table.row LIKE '%%stupid' AND othertable.row = %s",
67+       "SELECT 1 WHERE table.row LIKE '%%stupidisas%%stupiddoes' AND othertable.row = %s",
68+);
69+
70+print '<pre>';
71+foreach( $tests as $query ) {
72+       printf("   %-9s -> %-10s (Syntax: %s)\n", $query, $wpdb->_prepare_quote_lits( $query ), ( false === $wpdb->_prepare_quote_lits( $query, true) ? 'Error' : 'Ok' ) ) ;           
73+}
74+
75+/*
76+ * test: error message 
77+ * 
78+ * wpdb should report an error on a wrong query in prepare
79+ */
80+$wpdb->suppress_errors(false);
81+$wpdb->show_errors(true);
82+$wpdb->prepare( $query = '% wrong query' );
83+
84+/*
85+ * test: show what's wrong with the function
86+ *
87+ * NOTE: only for old, broken routine
88+ */
89+$wpdb->prepare( $query = '% wrong query', '' );
90+
91+die();
92\ No newline at end of file