1 | <?php |
---|
2 | |
---|
3 | add_action('template_redirect', function() { |
---|
4 | global $wpdb; |
---|
5 | |
---|
6 | assert_options(ASSERT_ACTIVE, 1); |
---|
7 | assert_options(ASSERT_QUIET_EVAL, 1); |
---|
8 | assert_options(ASSERT_WARNING, 0); |
---|
9 | assert_options(ASSERT_CALLBACK, function ($file, $line, $code) use ( &$failed ) { |
---|
10 | echo "Failed: $file (line $line)<hr />"; |
---|
11 | }); |
---|
12 | |
---|
13 | $mq = new WP_Meta_Query; |
---|
14 | |
---|
15 | // empty query |
---|
16 | $qv = array(); |
---|
17 | |
---|
18 | $expected = array(); |
---|
19 | |
---|
20 | $mq->parse_query_vars( $qv ); |
---|
21 | |
---|
22 | assert( $mq->queries == $expected ); |
---|
23 | |
---|
24 | $expected = array( 'where' => '', 'join' => '' ); |
---|
25 | |
---|
26 | $clauses = $mq->get_sql( 'post', $wpdb->posts, 'ID' ); |
---|
27 | |
---|
28 | assert( $clauses == $expected ); |
---|
29 | |
---|
30 | // invalid query |
---|
31 | $queries = array( |
---|
32 | array( |
---|
33 | 'compare' => 'like', |
---|
34 | ) |
---|
35 | ); |
---|
36 | |
---|
37 | $clauses = get_meta_sql( $queries, 'post', $wpdb->posts, 'ID' ); |
---|
38 | |
---|
39 | $expected = array( 'where' => '', 'join' => '' ); |
---|
40 | |
---|
41 | assert( $clauses == $expected ); |
---|
42 | |
---|
43 | // 'value' only |
---|
44 | $queries = array( |
---|
45 | array( |
---|
46 | 'value' => 'foo', |
---|
47 | 'compare' => 'like', |
---|
48 | ) |
---|
49 | ); |
---|
50 | |
---|
51 | $clauses = get_meta_sql( $queries, 'post', $wpdb->posts, 'ID' ); |
---|
52 | |
---|
53 | $expected = array( |
---|
54 | 'join' => " INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)", |
---|
55 | 'where' => " AND ( (CAST(wp_postmeta.meta_value AS CHAR) LIKE '%foo%') )" |
---|
56 | ); |
---|
57 | |
---|
58 | assert( $clauses == $expected ); |
---|
59 | |
---|
60 | // 'meta_key' & 'meta_value' = 0 |
---|
61 | $qv = array( |
---|
62 | 'meta_key' => 'foo', |
---|
63 | 'meta_value' => 0, |
---|
64 | ); |
---|
65 | |
---|
66 | $expected = array( |
---|
67 | array( |
---|
68 | 'key' => 'foo', |
---|
69 | 'value' => '0' |
---|
70 | ) |
---|
71 | ); |
---|
72 | |
---|
73 | $mq->parse_query_vars( $qv ); |
---|
74 | |
---|
75 | assert( $mq->queries == $expected ); |
---|
76 | |
---|
77 | |
---|
78 | $expected = array( |
---|
79 | 'join' => " INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)", |
---|
80 | 'where' => " AND ( (wp_postmeta.meta_key = 'foo' AND CAST(wp_postmeta.meta_value AS CHAR) = '0') )" |
---|
81 | ); |
---|
82 | |
---|
83 | $clauses = $mq->get_sql( 'post', $wpdb->posts, 'ID' ); |
---|
84 | |
---|
85 | assert( $clauses == $expected ); |
---|
86 | |
---|
87 | // Only 'meta_key' (meta_value should be ignored) |
---|
88 | $qv = array( |
---|
89 | 'meta_key' => 'foo', |
---|
90 | 'meta_value' => '', |
---|
91 | ); |
---|
92 | |
---|
93 | $expected = array( |
---|
94 | array( |
---|
95 | 'key' => 'foo', |
---|
96 | ) |
---|
97 | ); |
---|
98 | |
---|
99 | $mq->parse_query_vars( $qv ); |
---|
100 | assert( $mq->queries == $expected ); |
---|
101 | |
---|
102 | $expected = array( |
---|
103 | 'join' => " INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)", |
---|
104 | 'where' => " AND (wp_postmeta.meta_key = 'foo' )" |
---|
105 | ); |
---|
106 | |
---|
107 | $clauses = $mq->get_sql( 'post', $wpdb->posts, 'ID' ); |
---|
108 | |
---|
109 | assert( $clauses == $expected ); |
---|
110 | |
---|
111 | // 'key' and 'value' (value should _not_ be ignored) |
---|
112 | $meta_query = array( |
---|
113 | array( |
---|
114 | 'key' => 'foo', |
---|
115 | 'value' => '', |
---|
116 | 'compare' => '!=' |
---|
117 | ) |
---|
118 | ); |
---|
119 | |
---|
120 | $expected = array( |
---|
121 | 'join' => " INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)", |
---|
122 | 'where' => " AND ( (wp_postmeta.meta_key = 'foo' AND CAST(wp_postmeta.meta_value AS CHAR) != '') )" |
---|
123 | ); |
---|
124 | |
---|
125 | $clauses = get_meta_sql( $meta_query, 'post', $wpdb->posts, 'ID' ); |
---|
126 | |
---|
127 | assert( $clauses == $expected ); |
---|
128 | |
---|
129 | // empty 'value' with 'IN' comparison ('value' should not be ignored) |
---|
130 | $meta_query = array( |
---|
131 | array( |
---|
132 | 'key' => 'foo', |
---|
133 | 'value' => '0', |
---|
134 | 'compare' => 'NOT IN', |
---|
135 | ) |
---|
136 | ); |
---|
137 | |
---|
138 | $expected = array( |
---|
139 | 'join' => " INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)", |
---|
140 | 'where' => " AND ( (wp_postmeta.meta_key = 'foo' AND CAST(wp_postmeta.meta_value AS CHAR) NOT IN ('0')) )" |
---|
141 | ); |
---|
142 | |
---|
143 | $clauses = get_meta_sql( $meta_query, 'post', $wpdb->posts, 'ID' ); |
---|
144 | |
---|
145 | assert( $clauses == $expected ); |
---|
146 | |
---|
147 | // 'relation' => 'AND' (default) |
---|
148 | $meta_query = array( |
---|
149 | array( |
---|
150 | 'key' => 'foo', |
---|
151 | 'value' => '0', |
---|
152 | 'compare' => 'NOT IN', |
---|
153 | ), |
---|
154 | |
---|
155 | array( |
---|
156 | 'key' => 'bar', |
---|
157 | ), |
---|
158 | ); |
---|
159 | |
---|
160 | $expected = array( |
---|
161 | 'join' => " INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) |
---|
162 | INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id)", |
---|
163 | 'where' => " AND ( (wp_postmeta.meta_key = 'foo' AND CAST(wp_postmeta.meta_value AS CHAR) NOT IN ('0')) |
---|
164 | AND mt1.meta_key = 'bar' )" |
---|
165 | ); |
---|
166 | |
---|
167 | $clauses = get_meta_sql( $meta_query, 'post', $wpdb->posts, 'ID' ); |
---|
168 | |
---|
169 | assert( $clauses == $expected ); |
---|
170 | |
---|
171 | // 'relation' => 'OR' |
---|
172 | $meta_query = array( |
---|
173 | 'relation' => 'or', |
---|
174 | array( |
---|
175 | 'key' => 'foo', |
---|
176 | 'value' => '0', |
---|
177 | 'compare' => 'NOT IN', |
---|
178 | ), |
---|
179 | |
---|
180 | array( |
---|
181 | 'key' => 'bar', |
---|
182 | ), |
---|
183 | |
---|
184 | array( |
---|
185 | 'value' => array(5, 6), |
---|
186 | 'compare' => 'BETWEEN', |
---|
187 | 'type' => 'NUMERIC' |
---|
188 | ), |
---|
189 | ); |
---|
190 | |
---|
191 | $expected = array( |
---|
192 | 'join' => " INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) |
---|
193 | INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id) |
---|
194 | INNER JOIN wp_postmeta AS mt2 ON (wp_posts.ID = mt2.post_id)", |
---|
195 | 'where' => " AND ( (wp_postmeta.meta_key = 'foo' AND CAST(wp_postmeta.meta_value AS CHAR) NOT IN ('0')) |
---|
196 | OR mt1.meta_key = 'bar' |
---|
197 | OR (CAST(mt2.meta_value AS SIGNED) BETWEEN '5' AND '6') )" |
---|
198 | ); |
---|
199 | |
---|
200 | $clauses = get_meta_sql( $meta_query, 'post', $wpdb->posts, 'ID' ); |
---|
201 | |
---|
202 | assert( $clauses == $expected ); |
---|
203 | }); |
---|
204 | |
---|