",
290 | "got" => array(4.5,7.8,91.2),
291 | "valid" => false
292 | ),
293 | );
294 |
295 | foreach ($tests as $test) {
296 | $validation = new Validator(
297 | array('arg' => $test['expect']),
298 | array('arg' => $test['got'])
299 | );
300 |
301 | $got = (is_array($test['got'])) ? implode(",",$test['got'])
302 | : $test['got'];
303 |
304 | $msg = sprintf("Testing %s with value=[%s], valid=%b. Error: %s",
305 | $test['expect'], $got,
306 | $test['valid'], implode(" ",$validation->getErrors()));
307 |
308 | $this->assertEquals($test['valid'], $validation->passes(), $msg);
309 | }
310 |
311 |
312 | }
313 |
314 | }
--------------------------------------------------------------------------------
/src/InputMarkup.php:
--------------------------------------------------------------------------------
1 | 1,
12 | 'color' => 1,
13 | 'date' => 1,
14 | 'datetime' => 1,
15 | 'datetime-local' => 1,
16 | 'email' => 1,
17 | 'month' => 1,
18 | 'number' => 1,
19 | 'range' => 1,
20 | 'search' => 1,
21 | 'tel' => 1,
22 | 'time' => 1,
23 | 'url' => 1,
24 | 'week' => 1
25 | );
26 |
27 | public function __construct(Input $input) {
28 | $this->input = $input;
29 | }
30 |
31 | public function generate() {
32 | $output = '';
33 | $format = str_replace('-','_',$this->input->getFormat());
34 |
35 | if ($format != 'hidden') {
36 | $output .= $this->input->getPreHtml();
37 | $output .= $this->openWrapper();
38 | $output .= $this->inputLabel();
39 | }
40 |
41 | $output .= $this->generateInnerMarkup($format);
42 |
43 | if ($format != 'hidden') {
44 | $output .= $this->closeWrapper();
45 | $output .= $this->input->getPostHtml();
46 | }
47 |
48 | return $output;
49 | }
50 |
51 | private function generateInnerMarkup($format) {
52 | if (isset(self::$input_types[$format])) {
53 | return $this->input($format);
54 | }
55 | return $this->$format();
56 | }
57 |
58 | private function openWrapper() {
59 | $output = '';
60 | $id = $this->input->getId();
61 | if ($this->input->wrappersDisabled() === false) {
62 | $output .= '';
64 | }
65 | return $output;
66 | }
67 |
68 | private function closeWrapper() {
69 | $output = '';
70 | if ($this->input->wrappersDisabled() === false) {
71 | $output .= '
';
72 | }
73 | return $output;
74 | }
75 |
76 | private function inputLabel() {
77 | $output = '';
78 | $label = $this->input->getLabel();
79 | if ($label) {
80 | $output .= '' .
81 | '
';
82 | }
83 | return $output;
84 | }
85 |
86 | /**
87 | * Generates a select field
88 | */
89 | private function select($multi = false) {
90 |
91 | $output = '';
114 | return $output;
115 | }
116 |
117 | /**
118 | * Generates a checkbox field
119 | *
120 | */
121 | private function checkbox() {
122 | return $this->listField(true);
123 | }
124 |
125 | /**
126 | * Generates a radio field
127 | *
128 | */
129 | private function radio() {
130 | return $this->listField(false);
131 | }
132 |
133 | /**
134 | * Generates a list-style field (either checkboxes or radio buttons)
135 | *
136 | * @param bool $is_checkbox
137 | * @return string
138 | */
139 | private function listField($is_checkbox = true) {
140 | $group_label = ($is_checkbox) ? 'checkboxes' : 'radio-buttons';
141 | $option_func = ($is_checkbox) ? 'checkboxOption' : 'radioOption';
142 |
143 | $output = '';
144 |
145 | if ($this->input->isNested()) {
146 | $output .= $this->buildOptionsList($this->input->getValues(), array($this, $option_func), 0, true);
147 | return $output;
148 | }
149 |
150 | foreach ($this->input->getValues() as $value => $label) {
151 | $output .= call_user_func(array($this,$option_func), $value, $label);
152 | }
153 |
154 | $output .= '
';
155 | return $output;
156 | }
157 |
158 | /**
159 | * Builds and returns list of field options
160 | *
161 | * Used for select, checkbox, and radio fields. Supports nested
162 | * hierarchies of elements.
163 | *
164 | * @param array $elements
165 | * @param $field_func
166 | * @param int $level
167 | * @param bool $list_el
168 | * @return string
169 | */
170 | private function buildOptionsList($elements = array(), $field_func, $level = 0,
171 | $list_el = false) {
172 | if (empty($elements)) return "";
173 |
174 | $output = "";
175 | $output .= ($list_el) ? '' : '';
176 |
177 | foreach($elements as $element) {
178 | $output .= call_user_func($field_func, $element['value'], $element['label'], $level);
179 | $output .= $this->buildOptionsList($element['children'], $field_func, $level+1, $list_el);
180 | }
181 |
182 | $output .= ($list_el) ? '
' : '';
183 |
184 | return $output;
185 | }
186 |
187 | /**
188 | * Generates a text input field
189 | *
190 | * Also used to generate other HTML5 field types through use of $input_type
191 | * argument.
192 | *
193 | */
194 | private function input( $input_type = 'text' ) {
195 | $value = $this->getInputValue();
196 | $placeholder = '';
197 | if ($this->input->getPlaceholder())
198 | $placeholder = ' placeholder="'.$this->input->getPlaceholder().'"';
199 | $output = 'attributesString().'>';
200 | return $output;
201 | }
202 |
203 | /**
204 | * Generates a textarea field
205 | *
206 | */
207 | private function textarea() {
208 | $value = $this->getInputValue();
209 | $placeholder = '';
210 | if ($this->input->getPlaceholder())
211 | $placeholder = ' placeholder="'.$this->input->getPlaceholder().'"';
212 | $output = '';
213 | return $output;
214 | }
215 |
216 | /**
217 | * Generates a submit button
218 | *
219 | *
220 | */
221 | private function submit() {
222 | return $this->buttonInput('submit');
223 | }
224 |
225 | /**
226 | * Generates a reset button
227 | *
228 | * @since 1.4
229 | */
230 | private function reset() {
231 | return $this->buttonInput('reset');
232 | }
233 |
234 | private function buttonInput($type) {
235 | $values = $this->input->getValues();
236 | $value = reset($values);
237 | $output = 'attributesString().'>';
238 | return $output;
239 | }
240 |
241 | /**
242 | * Generates a clear button
243 | *
244 | * @since 1.4
245 | */
246 | private function clear() {
247 | $values = $this->input->getValues();
248 | $value = reset($values);
249 | $output = '';
250 | return $output;
251 | }
252 |
253 | /**
254 | * Generates an html field
255 | */
256 | private function html() {
257 | $values = $this->input->getValues();
258 | return reset($values);
259 | }
260 |
261 | /**
262 | * Generates a hidden field
263 | */
264 | private function hidden() {
265 | $values = $this->input->getValues();
266 | $value = reset($values);
267 | $output = 'attributesString().'>';
268 | return $output;
269 | }
270 |
271 | /**
272 | * Creates a string of HTML element attributes for the input
273 | */
274 | private function attributesString() {
275 | $output = "";
276 | if ($this->input->getAttributes()) {
277 | foreach($this->input->getAttributes() as $k => $v) {
278 | $output .= $k . '="'.$v.'" ';
279 | }
280 | }
281 | return $output;
282 | }
283 |
284 | /**
285 | * Obtains the value to use in the field.
286 | *
287 | * Used only for text & textarea inputs
288 | *
289 | * @since 1.3
290 | */
291 | private function getInputValue() {
292 | $value = '';
293 | $selected = $this->input->getSelected();
294 | $values = $this->input->getValues();
295 |
296 | if (!empty($selected)) {
297 | $value = reset($selected);
298 | } else if (!empty($values)) {
299 | $value = reset($values);
300 | }
301 | return $value;
302 | }
303 |
304 | /**
305 | * Generates a single option for a select field
306 | *
307 | * @since 1.3
308 | */
309 | private function selectOption($value, $label, $level = 0) {
310 | $indent = '';
311 | if ($level > 0) {
312 | for($i=0; $i<$level; $i++) {
313 | $indent .= "—";
314 | }
315 | $indent .= ' ';
316 | }
317 | $output = '';
322 | return $output;
323 | }
324 |
325 | /**
326 | * Generates a single option for a checkbox field
327 | *
328 | * @since 1.3
329 | */
330 | private function checkboxOption($value, $label, $level = 0) {
331 | return $this->listOption($value, $label, 'checkbox');
332 | }
333 |
334 | /**
335 | * Generates a single option for a radio field
336 | *
337 | * @since 1.3
338 | */
339 | private function radioOption($value, $label, $level = 0) {
340 | return $this->listOption($value, $label, 'radio');
341 | }
342 |
343 | /**
344 | * Generates a single field option for a checkbox or radio field
345 | *
346 | * @param $value
347 | * @param $label
348 | * @param string $type "checkbox" or "radio"
349 | * @return string
350 | */
351 | private function listOption($value, $label, $type = 'checkbox') {
352 | $ctr = $this->ctr;
353 | $element = ($this->input->isNested()) ? 'li' : 'div';
354 | $id = $this->input->getId();
355 | $name = $this->input->getInputName();
356 | $name .= ($type == 'checkbox') ? '[]' : '';
357 | $output = '<'.$element.' class="wpas-'.$id.'-'.$type.'-'.$ctr.'-container wpas-'.$id.'-'.$type.'-container wpas-'.$type.'-container">'
358 | . 'input->getSelected(), true)) {
360 | $output .= ' checked="checked"';
361 | }
362 | $output .= '>';
363 | $output .= ''.$element.'>';
364 | $this->ctr++;
365 | return $output;
366 | }
367 |
368 | private function multi_select() {
369 | return $this->select(true);
370 | }
371 |
372 | private function text() {
373 | return $this->input('text');
374 | }
375 |
376 | private function number() {
377 | return $this->input('number');
378 | }
379 |
380 | private function color() {
381 | return $this->input('color');
382 | }
383 |
384 | private function url() {
385 | return $this->input('url');
386 | }
387 |
388 | private function email() {
389 | return $this->input('email');
390 | }
391 |
392 | private function tel() {
393 | return $this->input('tel');
394 | }
395 |
396 | private function date() {
397 | return $this->input('date');
398 | }
399 |
400 | private function datetime() {
401 | return $this->input('datetime');
402 | }
403 |
404 | private function datetime_local() {
405 | return $this->input('datetime-local');
406 | }
407 |
408 | private function time() {
409 | return $this->input('time');
410 | }
411 |
412 | private function week() {
413 | return $this->input('week');
414 | }
415 |
416 | }
--------------------------------------------------------------------------------
/tests/TestInputBuilder.php:
--------------------------------------------------------------------------------
1 | 'search',
17 | 'label' => 'Search',
18 | 'id' => 'search-id',
19 | 'format' => 'text',
20 | 'class' => array('testclass'),
21 | 'name' => 'my_search',
22 | 'attributes' => array('data-src' => 12345,
23 | 'data-color' => 'red',
24 | 'min' => 0,
25 | 'max' => 100),
26 | 'default' => 'something');
27 |
28 | $input = InputBuilder::make('search_query', FieldType::search, $args);
29 | $this->assertTrue($input instanceof Input);
30 |
31 | $args = array('field_type' => 'search');
32 | $input = InputBuilder::make('search_query', FieldType::search, $args);
33 | $this->assertTrue($input instanceof Input);
34 | $this->assertTrue($input->getFormat() == InputFormat::text);
35 | }
36 |
37 | public function testCanBuildSubmit() {
38 | $args = array('field_type' => 'submit');
39 | $input = InputBuilder::make('submit', FieldType::submit, $args);
40 | $this->assertTrue($input instanceof Input);
41 | $this->assertTrue($input->getFormat() == InputFormat::submit);
42 |
43 | $args = array('field_type' => 'submit', 'values' => array("Go!"));
44 | $input = InputBuilder::make('submit', FieldType::submit, $args);
45 | $this->assertTrue($input instanceof Input);
46 | $values = $input->getValues();
47 | $this->assertTrue(is_array($values) && $values[0] == "Go!");
48 | }
49 |
50 | public function testCanBuildTaxonomy() {
51 | $args = array(
52 | 'field_type' => 'taxonomy',
53 | 'taxonomy' => 'category',
54 | 'format' => 'select'
55 | );
56 | $input = InputBuilder::make('tax_category', FieldType::taxonomy, $args);
57 | $this->assertTrue($input instanceof Input);
58 | $this->assertFalse($input->isNested());
59 |
60 | $args = array(
61 | 'field_type' => 'taxonomy',
62 | 'taxonomy' => 'category',
63 | 'format' => 'select',
64 | 'nested' => true
65 | );
66 | $input = InputBuilder::make('tax_category', FieldType::taxonomy, $args);
67 | $this->assertTrue($input->isNested());
68 | }
69 |
70 | public function testTaxonomyAutoGenTerms() {
71 |
72 | $t = array();
73 | $t[] = $this->factory->category->create_and_get(array('name' => "Category One"));
74 | $t[] = $this->factory->category->create_and_get(array('name' => "Category Two"));
75 | $t[] = $this->factory->category->create_and_get(array('name' => "Z Category"));
76 |
77 | $args = array(
78 | 'field_type' => 'taxonomy',
79 | 'taxonomy' => 'category',
80 | 'format' => 'select',
81 | 'term_format' => 'slug'
82 | );
83 | $input = InputBuilder::make('tax_category', FieldType::taxonomy, $args);
84 |
85 | $values = $input->getValues();
86 | $this->assertTrue(count($values) == 4);
87 |
88 | // Test term_format = 'slug'
89 | $first_label = reset($values);
90 | $first_value = key($values);
91 | $this->assertTrue($first_label == $t[0]->name);
92 | $this->assertTrue($first_value == $t[0]->slug);
93 |
94 | // Test term_format = 'id'
95 | $args['term_format'] = 'id';
96 | $input = InputBuilder::make('tax_category', FieldType::taxonomy, $args);
97 | $values = $input->getValues();
98 | $first_value = key($values);
99 | $this->assertTrue(key($input->getValues()) == $t[0]->term_id);
100 |
101 | // Test term_format = 'name'
102 | $args['term_format'] = 'name';
103 | $input = InputBuilder::make('tax_category', FieldType::taxonomy, $args);
104 | $values = $input->getValues();
105 | $first_value = key($values);
106 | $this->assertTrue(key($input->getValues()) == $t[0]->name);
107 |
108 | // Test term_args
109 | $args['term_args'] = array('orderby' => 'name', 'order' => 'DESC');
110 | $input = InputBuilder::make('tax_category', FieldType::taxonomy, $args);
111 | $values = $input->getValues();
112 | $this->assertTrue(key($input->getValues()) == $t[2]->name);
113 |
114 | // Test exclude
115 | $args['exclude'] = array('category-one');
116 | $args['term_format'] = 'slug';
117 | $input = InputBuilder::make('tax_category', FieldType::taxonomy, $args);
118 | $values = $input->getValues();
119 | $this->assertTrue(count($values) == 3);
120 | $this->assertFalse(isset($values[$args['exclude'][0]]));
121 | }
122 |
123 | public function testTaxonomyNestedTerms() {
124 | $t = array();
125 | $t[] = $this->factory->category->create_and_get(array('name' => "Category One"));
126 | $t[] = $this->factory->category->create_and_get(array('name' => "Category Two", 'parent' => $t[0]->term_id));
127 | $t[] = $this->factory->category->create_and_get(array('name' => "Z Category", 'parent' => $t[0]->term_id));
128 |
129 | $args = array(
130 | 'field_type' => 'taxonomy',
131 | 'taxonomy' => 'category',
132 | 'format' => 'select',
133 | 'nested' => 'true',
134 | 'term_format' => 'slug'
135 | );
136 |
137 | $input = InputBuilder::make('tax_category', FieldType::taxonomy, $args);
138 | $values = $input->getValues();
139 | $value = reset($values);
140 | $this->assertTrue(count($value['children']) == 2);
141 |
142 | $args['format'] = 'multi-select';
143 | $input = InputBuilder::make('tax_category', FieldType::taxonomy, $args);
144 | $values = $input->getValues();
145 | $value = reset($values);
146 | $this->assertTrue(count($value['children']) == 2);
147 | $input->toHTML();
148 |
149 | $args['format'] = 'radio';
150 | $input = InputBuilder::make('tax_category', FieldType::taxonomy, $args);
151 | $values = $input->getValues();
152 | $value = reset($values);
153 | $this->assertTrue(count($value['children']) == 2);
154 | $input->toHTML();
155 |
156 | $args['format'] = 'checkbox';
157 | $input = InputBuilder::make('tax_category', FieldType::taxonomy, $args);
158 | $values = $input->getValues();
159 | $value = reset($values);
160 | $this->assertTrue(count($value['children']) == 2);
161 | $input->toHTML();
162 |
163 | }
164 |
165 | public function testExcludeWithNestedTaxonomy() {
166 |
167 | $t = array();
168 | $t[] = $this->factory->category->create_and_get(array('name' => "Category One"));
169 | $t[] = $this->factory->category->create_and_get(array('name' => "Category Two", 'parent' => $t[0]->term_id));
170 | $t[] = $this->factory->category->create_and_get(array('name' => "Z Category", 'parent' => $t[0]->term_id));
171 |
172 | $args = array(
173 | 'field_type' => 'taxonomy',
174 | 'taxonomy' => 'category',
175 | 'format' => 'select',
176 | 'nested' => 'true',
177 | 'exclude' => array('category-one'), // By excluding category-one, its two
178 | // children should also be excluded
179 | 'term_format' => 'slug'
180 | );
181 |
182 | $input = InputBuilder::make('tax_category', FieldType::taxonomy, $args);
183 | $values = $input->getValues();
184 | $this->assertTrue(count($values) == 1); // count = 1 due to "Uncategorized"
185 | }
186 |
187 | public function testTaxonomyWithManualTermsList() {
188 | $t = array();
189 | $t[] = $this->factory->category->create_and_get(array('name' => "Category One"));
190 | $t[] = $this->factory->category->create_and_get(array('name' => "Category Two", 'parent' => $t[0]->term_id));
191 | $t[] = $this->factory->category->create_and_get(array('name' => "Z Category", 'parent' => $t[0]->term_id));
192 | $t[] = $this->factory->category->create_and_get(array('name' => "Y Category", 'parent' => $t[0]->term_id));
193 |
194 | $args = array(
195 | 'field_type' => 'taxonomy',
196 | 'taxonomy' => 'category',
197 | 'format' => 'select',
198 | 'terms' => array($t[0]->term_id, $t[1]->term_id, $t[2]->term_id),
199 | 'term_format' => 'ID'
200 | );
201 |
202 | $input = InputBuilder::make('tax_category', FieldType::taxonomy, $args);
203 | $values = $input->getValues();
204 |
205 | $this->assertTrue(count($values) == 3);
206 | }
207 |
208 |
209 | public function testSetSelectedValues() {
210 | $t = array();
211 | $t[] = $this->factory->category->create_and_get(array('name' => "Category One"));
212 | $t[] = $this->factory->category->create_and_get(array('name' => "Category Two"));
213 | $t[] = $this->factory->category->create_and_get(array('name' => "Z Category"));
214 |
215 | $request = new HttpRequest(array('tax_category' => array('category-two')));
216 | $args = array(
217 | 'field_type' => 'taxonomy',
218 | 'taxonomy' => 'category',
219 | 'format' => 'select',
220 | 'term_format' => 'slug'
221 | );
222 | $input = InputBuilder::make('tax_category', FieldType::taxonomy, $args, $request);
223 |
224 | // Selected value is present
225 | $selected = $input->getSelected();
226 | $this->assertTrue(count($selected) == 1);
227 | $this->assertTrue($selected[0] == 'category-two');
228 |
229 | // Default should be overridden by request value
230 | $args['default'] = 'category-one';
231 | $input = InputBuilder::make('tax_category', FieldType::taxonomy, $args,
232 | $request);
233 | $selected = $input->getSelected();
234 | $this->assertTrue(count($selected) == 1);
235 | $this->assertTrue($selected[0] == 'category-two');
236 |
237 | // Default should be selected with empty request
238 | $request = array();
239 | $args['default'] = 'category-one';
240 | $input = InputBuilder::make('tax_category', FieldType::taxonomy, $args,
241 | $request);
242 | $selected = $input->getSelected();
243 | $this->assertTrue(count($selected) == 1);
244 | $this->assertTrue($selected[0] == 'category-one');
245 |
246 |
247 | // Check default_all
248 | $args['default_all'] = true;
249 | $args['format'] = 'multi-select';
250 | $input = InputBuilder::make('tax_category', FieldType::taxonomy, $args,
251 | $request);
252 | $selected = $input->getSelected();
253 | $this->assertTrue(count($selected) == 4);
254 | }
255 |
256 | public function testGenericFieldSelected() {
257 | $inputname = 'myfield';
258 | $request = new HttpRequest(array($inputname => array('blue','green')));
259 |
260 | $args = array(
261 | 'field_type' => 'generic',
262 | 'format' => 'checkbox',
263 | 'values' => array('red','blue','green')
264 | );
265 | $input = InputBuilder::make($inputname, FieldType::generic, $args,
266 | $request);
267 | $this->assertTrue(count($input->getSelected()) == 2);
268 | }
269 |
270 | public function testAuthor() {
271 | $queried = array(2,4);
272 | $request = new HttpRequest(array('a' => array(2,4)));
273 |
274 | $users = array();
275 | $users[] = $this->factory->user->create_and_get(array('display_name'=>'Sean', 'role' => 'administrator')); // id=2
276 | $users[] = $this->factory->user->create_and_get(array('display_name'=>'Dave', 'role' => 'editor')); // id=3
277 | $users[] = $this->factory->user->create_and_get(array('display_name'=>'Alex', 'role' => 'author')); // id=4
278 | $users[] = $this->factory->user->create_and_get(array('display_name'=>'Jim', 'role' => 'subscriber')); // id=5
279 | $users[] = $this->factory->user->create_and_get(array('display_name'=>'Rob', 'role' => 'contributor')); // id=6
280 |
281 | $values = array();
282 | foreach($users as $user) {
283 | $values[] = $user->user_id;
284 | }
285 |
286 | $args = array(
287 | 'field_type' => 'author',
288 | 'format' => 'checkbox',
289 | 'values' => $values
290 | );
291 |
292 | $input = InputBuilder::make('a', FieldType::author, $args, $request);
293 | $values = $input->getValues();
294 | $selected = $input->getSelected();
295 |
296 | $this->assertTrue(count($values) == 5); // 5 authors including "admin"
297 | $this->assertFalse(isset($values[5])); // User 'Jim' should not be included
298 |
299 | $this->assertTrue(count($selected) == 2);
300 | $this->assertContains(2, $selected);
301 | $this->assertContains(4,$selected);
302 | }
303 |
304 |
305 |
306 |
307 | }
--------------------------------------------------------------------------------
/tests/TestFactory.php:
--------------------------------------------------------------------------------
1 | assertFalse($f->hasErrors());
15 |
16 | }
17 |
18 | public function testFactory() {
19 | $args = array();
20 |
21 | $args['fields'][] = array('type' => 'search',
22 | 'label' => 'Search',
23 | 'id' => 'hhh',
24 | 'format' => 'text',
25 | 'class' => 'testclass',
26 | 'name' => 'my_search',
27 | 'attributes' => array('data-src' => 12345, 'data-color' => 'red', 'min' => 0, 'max' => 100),
28 | 'default' => 'something'
29 | );
30 |
31 | $args['fields'][] = array('type' => 'meta_key',
32 | 'label' => 'Color',
33 | 'format' => 'checkbox',
34 | 'meta_key' => 'color',
35 | 'compare' => 'LIKE',
36 | 'data_type' => 'CHAR',
37 | 'relation' => 'OR',
38 | 'values' => array(
39 | 'red' => 'Red',
40 | 'blue' => 'Blue',
41 | 'green' => 'Green',
42 | 'orange' => 'Orange',
43 | 'purple' => 'Purple',
44 | 'yellow' => 'Yellow'
45 | ));
46 |
47 | $args['fields'][] = array('type' => 'meta_key',
48 | 'label' => 'Animal',
49 | 'format' => 'checkbox',
50 | 'meta_key' => 'custom1',
51 | 'compare' => 'IN',
52 | 'data_type' => 'CHAR',
53 | 'relation' => 'OR',
54 | 'values' => array(
55 | 'Dog' => 'Dog',
56 | 'Cat' => 'Cat',
57 | 'Giraffe' => 'Giraffe',
58 | ));
59 |
60 | $args['fields'][] = array('type' => 'post_type',
61 | 'label' => 'Post Type',
62 | 'values' => array('post' => 'Post', 'page' => 'Page', 'event' => 'Event'),
63 | 'format' => 'checkbox',
64 | 'default_all' => true);
65 |
66 | $args['fields'][] = array('type' => 'post_type',
67 | 'label' => 'Post Type',
68 | 'values' => array('post' => 'Post', 'page' => 'Page', 'event' => 'Event'),
69 | 'format' => 'checkbox',
70 | 'default_all' => true);
71 |
72 | $args['fields'][] = array('type' => 'order',
73 | 'label' => 'Order',
74 | 'class' => array('orderfield', 'zzz'),
75 | 'id' => 'checkoutmyid',
76 | 'values' => array('' => '', 'ASC' => 'ASC', 'DESC' => 'DESC'),
77 | 'format' => 'select');
78 |
79 | $args['fields'][] = array('type' => 'orderby',
80 | 'label' => 'Order By',
81 | //'exclude' => 'post_modified',
82 | 'id' => 'myorder',
83 | 'class' => 'orderclass anotherone',
84 | 'allow_null' => false,
85 | 'format' => 'radio');
86 |
87 | $args['fields'][] = array('type' => 'submit',
88 | 'value' => 'Search',
89 | 'class' => 'submitclasss',
90 | 'attributes' => array('one' => 'five'),
91 | );
92 |
93 | $f = new Factory($args);
94 | $this->assertFalse($f->hasErrors());
95 | $inputs = $f->getInputs();
96 | $this->assertTrue(count($inputs) == 8);
97 |
98 | $q = $f->buildQueryObject()->query;
99 | }
100 |
101 | public function testMetaQueryBetween() {
102 | $args = array();
103 | $prefix = RequestVar::meta_key;
104 | $meta_key = 'price';
105 | $compare = 'BETWEEN';
106 | $type = 'NUMERIC';
107 |
108 | $request = array($prefix.$meta_key.'1' => 10, $prefix.$meta_key.'2' => 25,
109 | $prefix.$meta_key.'3' => 30);
110 |
111 | $args['fields'][] = array(
112 | 'type' => 'meta_key',
113 | 'meta_key' => $meta_key,
114 | 'compare' => $compare,
115 | 'data_type' => $type,
116 | 'inputs' => array(
117 | array(
118 | 'format' => 'text',
119 | ),
120 | array(
121 | 'format' => 'text'
122 | )
123 | )
124 | );
125 |
126 | $f = new Factory($args, $request);
127 | $this->assertFalse($f->hasErrors());
128 | $q = $f->buildQueryObject()->query;
129 | $this->assertTrue(!empty($q));
130 | $this->assertTrue(!empty($q['meta_query']));
131 | $expected_query = '[{"key":"price","type":"NUMERIC","value":["10","25"],"compare":"BETWEEN"}]';
132 | $this->assertTrue(json_encode($q['meta_query']) == $expected_query);
133 |
134 | }
135 |
136 | public function testMetaQueryBetweenSingleInput() {
137 | $args = array();
138 | $prefix = RequestVar::meta_key;
139 | $meta_key = 'price';
140 | $compare = 'BETWEEN';
141 | $type = 'NUMERIC';
142 |
143 | $request = array($prefix.$meta_key => '0:10');
144 |
145 | $args['fields'][] = array(
146 | 'type' => 'meta_key',
147 | 'meta_key' => $meta_key,
148 | 'compare' => $compare,
149 | 'data_type' => $type,
150 | 'inputs' => array(
151 | array(
152 | 'format' => 'select',
153 | 'values' => array(
154 | '0:10' => '0 to 10',
155 | '11:25' => '11 to 25',
156 | '26:' => '26+'
157 | )
158 | ),
159 |
160 | )
161 | );
162 |
163 | $f = new Factory($args, $request);
164 | $this->assertFalse($f->hasErrors());
165 | $q = $f->buildQueryObject()->query;
166 | $this->assertTrue(!empty($q));
167 | $this->assertTrue(!empty($q['meta_query']));
168 |
169 | $expected_query = '[{"key":"price","type":"NUMERIC","value":["0","10"],"compare":"BETWEEN"}]';
170 | $this->assertTrue(json_encode($q['meta_query']) == $expected_query);
171 |
172 | $request = array($prefix.$meta_key => '26:');
173 | $f = new Factory($args, $request);
174 | $this->assertFalse($f->hasErrors());
175 | $q = $f->buildQueryObject()->query;
176 | $this->assertTrue(!empty($q));
177 | $this->assertTrue(!empty($q['meta_query']));
178 | $expected_query = '[{"key":"price","type":"NUMERIC","value":"26","compare":">="}]';
179 | $this->assertTrue(json_encode($q['meta_query']) == $expected_query);
180 |
181 | }
182 |
183 | public function testMetaQueryWithOneKey()
184 | {
185 | $args = array();
186 | $meta_key = 'color';
187 | $args['fields'][] = array(
188 | 'type' => 'meta_key',
189 | 'meta_key' => $meta_key,
190 | 'compare' => 'IN',
191 | 'relation' => 'AND',
192 | 'data_type' => 'CHAR',
193 | 'format' => 'checkbox'
194 | );
195 |
196 | $request = array('meta_'.$meta_key => array('red','blue'));
197 | $f = new Factory($args, $request);
198 | $q = $f->buildQueryObject()->query;
199 |
200 | $this->assertFalse(empty($q['meta_query']));
201 | $this->assertTrue(count($q['meta_query']) == 1);
202 |
203 | }
204 |
205 | public function testMetaQueryWithMultiKey()
206 | {
207 | $args = array();
208 | $meta_key = 'color';
209 | $meta_key2 = 'shape';
210 |
211 | $args['fields'][] = array(
212 | 'type' => 'meta_key',
213 | 'meta_key' => $meta_key,
214 | 'compare' => 'LIKE',
215 | 'relation' => 'OR',
216 | 'data_type' => 'ARRAY',
217 | 'format' => 'checkbox'
218 | );
219 | $args['fields'][] = array(
220 | 'type' => 'meta_key',
221 | 'meta_key' => $meta_key2,
222 | 'compare' => 'LIKE',
223 | 'relation' => 'OR',
224 | 'data_type' => 'ARRAY',
225 | 'format' => 'checkbox'
226 | );
227 |
228 | $request = array('meta_'.$meta_key => array('red','blue'), 'meta_'.$meta_key2 => array('square','circle'));
229 | $f = new Factory($args, $request);
230 | $q = $f->buildQueryObject()->query;
231 |
232 | $this->assertFalse(empty($q['meta_query']));
233 | $this->assertTrue(count($q['meta_query']) == 3);
234 | $this->assertTrue(count($q['meta_query'][0]) == 3);
235 | $this->assertTrue(count($q['meta_query'][1]) == 3);
236 |
237 | }
238 |
239 | public function testBadMetaKeyRelation()
240 | {
241 | $args = array();
242 | $meta_key = 'color';
243 | $meta_key2 = 'shape';
244 |
245 | $args['meta_key_relation'] = "BAD";
246 |
247 | $args['fields'][] = array(
248 | 'type' => 'meta_key',
249 | 'meta_key' => $meta_key,
250 | 'compare' => 'LIKE',
251 | 'relation' => 'OR',
252 | 'data_type' => 'ARRAY',
253 | 'format' => 'checkbox'
254 | );
255 | $args['fields'][] = array(
256 | 'type' => 'meta_key',
257 | 'meta_key' => $meta_key2,
258 | 'compare' => 'LIKE',
259 | 'relation' => 'OR',
260 | 'data_type' => 'ARRAY',
261 | 'format' => 'checkbox'
262 | );
263 |
264 | $request = array('meta_'.$meta_key => array('red','blue'), 'meta_'.$meta_key2 => array('square','circle'));
265 | $f = new Factory($args, $request);
266 | $q = $f->buildQueryObject()->query;
267 |
268 | $this->assertTrue($q['meta_query']['relation'] == Relation::_default);
269 |
270 | }
271 |
272 |
273 | public function testTaxQuery()
274 | {
275 | $args = array();
276 | $tax = 'category';
277 | $args['fields'][] = array(
278 | 'type' => 'taxonomy',
279 | 'taxonomy' => $tax,
280 | 'operator' => 'IN',
281 | 'format' => 'checkbox'
282 | );
283 |
284 | $expected_query = '[{"taxonomy":"category","operator":"IN","field":"slug","terms":["red","blue"]}]';
285 |
286 | $request = array(RequestVar::taxonomy . $tax => array('red','blue'));
287 | $f = new Factory($args, $request);
288 | $this->assertFalse($f->hasErrors());
289 |
290 | $q = $f->buildQueryObject()->query;
291 | $this->assertTrue(json_encode($q['tax_query']) == $expected_query);
292 |
293 | }
294 |
295 | public function testTaxQueryMultiTax() {
296 | $args = array();
297 | $tax = 'category';
298 | $tax2 = 'post_tag';
299 | $args['fields'][] = array(
300 | 'type' => 'taxonomy',
301 | 'taxonomy' => $tax,
302 | 'operator' => 'AND',
303 | 'format' => 'checkbox'
304 | );
305 | $args['fields'][] = array(
306 | 'type' => 'taxonomy',
307 | 'taxonomy' => $tax2,
308 | 'operator' => 'IN',
309 | 'format' => 'checkbox'
310 | );
311 |
312 | $expected_query = '{"0":{"taxonomy":"category","operator":"AND","field":"slug","terms":["red","blue"]},"1":{"taxonomy":"post_tag","operator":"IN","field":"slug","terms":["square","circle"]},"relation":"AND"}';
313 |
314 | $request = array(RequestVar::taxonomy . $tax => array('red','blue'),
315 | RequestVar::taxonomy . $tax2 => array('square','circle'));
316 | $f = new Factory($args, $request);
317 | $this->assertFalse($f->hasErrors());
318 |
319 | $q = $f->buildQueryObject()->query;
320 | $this->assertTrue(json_encode($q['tax_query']) == $expected_query);
321 |
322 | }
323 |
324 | public function testTaxQueryMultiInput() {
325 | $args = array();
326 | $tax = 'category';
327 | $args['fields'][] = array(
328 | 'type' => 'taxonomy',
329 | 'taxonomy' => $tax,
330 | 'relation' => 'AND',
331 | 'inputs' => array(
332 | array(
333 | 'format' => 'select',
334 | 'operator' => 'IN'
335 | ),
336 | array(
337 | 'format' => 'select',
338 | 'operator' => 'NOT IN'
339 | )
340 | )
341 |
342 | );
343 |
344 | $expected_query = '[{"0":{"taxonomy":"category","operator":"IN","field":"slug","terms":"red"},"1":{"taxonomy":"category","operator":"NOT IN","field":"slug","terms":["blue","green"]},"relation":"AND"}]';
345 |
346 | $request = array(RequestVar::taxonomy . $tax . '1' => 'red',
347 | RequestVar::taxonomy . $tax . '2' => array('blue','green'));
348 | $f = new Factory($args, $request);
349 | $this->assertFalse($f->hasErrors());
350 |
351 | $q = $f->buildQueryObject()->query;
352 |
353 | $this->assertTrue(json_encode($q['tax_query']) == $expected_query);
354 | }
355 |
356 | public function testTaxQueryMultiInputEmptyInput() {
357 | $args = array();
358 | $tax = 'category';
359 | $args['fields'][] = array(
360 | 'type' => 'taxonomy',
361 | 'taxonomy' => $tax,
362 | 'relation' => 'AND',
363 | 'inputs' => array(
364 | array(
365 | 'format' => 'select',
366 | 'operator' => 'IN'
367 | ),
368 | array(
369 | 'format' => 'select',
370 | 'operator' => 'NOT IN'
371 | )
372 | )
373 | );
374 |
375 | $expected_query = '[{"taxonomy":"category","operator":"IN","field":"slug","terms":"red"}]';
376 |
377 | $request = array(RequestVar::taxonomy . $tax . '1' => 'red',
378 | RequestVar::taxonomy . $tax . '2' => array());
379 | $f = new Factory($args, $request);
380 | $this->assertFalse($f->hasErrors());
381 |
382 | $q = $f->buildQueryObject()->query;
383 | $this->assertTrue(json_encode($q['tax_query']) == $expected_query);
384 | }
385 |
386 | public function testFactoryComplexRequest() {
387 | $args = array();
388 |
389 | $args['fields'][] = array('type' => 'search',
390 | 'label' => 'Search',
391 | //'id' => 'hhh',
392 | 'format' => 'text',
393 | 'class' => 'testclass',
394 | 'name' => 'my_search',
395 | 'attributes' => array('data-src' => 12345, 'data-color' => 'red', 'min' => 0, 'max' => 100),
396 | 'default' => 'something'
397 | );
398 |
399 | $args['fields'][] = array('type' => 'post_type',
400 | 'label' => 'Post Type',
401 | 'values' => array('post' => 'Post', 'page' => 'Page', 'event' => 'Event'),
402 | 'format' => 'checkbox',
403 | 'default_all' => true);
404 |
405 | $args['fields'][] = array('type' => 'order',
406 | 'label' => 'Order',
407 | 'id' => 'checkoutmyid',
408 | 'values' => array('' => '', 'ASC' => 'ASC', 'DESC' => 'DESC'),
409 | 'format' => 'select');
410 |
411 | $args['fields'][] = array('type' => 'orderby',
412 | 'label' => 'Order By',
413 | 'id' => 'myorder',
414 | 'allow_null' => false,
415 | 'format' => 'radio');
416 |
417 | $args['fields'][] = array('type' => 'posts_per_page',
418 | 'label' => 'Order By',
419 | 'id' => 'myorder',
420 | 'allow_null' => false,
421 | 'format' => 'radio');
422 |
423 | $args['fields'][] = array('type' => 'submit',
424 | 'value' => 'Search',
425 | 'attributes' => array('one' => 'five'),
426 | );
427 |
428 | $request = array('order'=>'ASC', 'ptype'=>array('post','page'), 'search_query' => 'testing', 'posts_per_page' => 12);
429 | $expected_query = '{"post_type":["post","page"],"order":"ASC","posts_per_page":"12","s":"testing","paged":1}';
430 | $e = '{"post_type":["post","page"],"order":"ASC","posts_per_page":12,"s":"testing","paged":1}';
431 |
432 | $f = new Factory($args, $request);
433 | $this->assertFalse($f->hasErrors());
434 | $q = $f->buildQueryObject()->query;
435 | $this->assertTrue(json_encode($q) == $expected_query);
436 |
437 | }
438 |
439 | public function testFactoryDateQuery() {
440 | $args = array();
441 | $args['fields'][] = array(
442 | 'type' => 'date',
443 | 'format' => 'select',
444 | 'date_type' => 'month'
445 | );
446 |
447 | $expected_query = '{"year":"2014","month":"01"}';
448 |
449 | $request = array(RequestVar::date . 'm' => '2014-01');
450 | $f = new Factory($args, $request);
451 | $this->assertFalse($f->hasErrors());
452 |
453 | $q = $f->buildQueryObject()->query;
454 | $this->assertFalse(empty($q['date_query']));
455 | $this->assertTrue(json_encode($q['date_query']) == $expected_query);
456 |
457 | }
458 |
459 | public function testOrderbyValues() {
460 | $args = array();
461 | $args['fields'][] = array(
462 | 'type' => 'orderby',
463 | 'format' => 'radio',
464 | 'default' => 'event_date',
465 | 'title' => 'Order by',
466 | 'orderby_values' =>
467 | array(
468 | 'event_date' => array('label' => 'Date',
469 | 'meta_key' => true,
470 | 'orderby' => 'meta_value'),
471 | 'event_price' => array('label' => 'Date',
472 | 'meta_key' => true,
473 | 'orderby' => 'meta_value_num'),
474 | 'title' => array('label' => 'Title'),
475 | ));
476 |
477 | // Test meta_value_num
478 | $request = array(RequestVar::orderby => 'event_price');
479 | $f = new Factory($args, $request);
480 | $this->assertFalse($f->hasErrors());
481 | $q = $f->buildQueryObject()->query;
482 | $this->assertTrue($q['orderby'] == 'meta_value_num');
483 | $this->assertTrue($q['meta_key'] == 'event_price');
484 |
485 | // Test meta_value
486 | $request = array(RequestVar::orderby => 'event_date');
487 | $f = new Factory($args, $request);
488 | $this->assertFalse($f->hasErrors());
489 | $q = $f->buildQueryObject()->query;
490 | $this->assertTrue($q['orderby'] == 'meta_value');
491 | $this->assertTrue($q['meta_key'] == 'event_date');
492 |
493 | // Test regular orderby
494 | $request = array(RequestVar::orderby => 'title');
495 | $f = new Factory($args, $request);
496 | $this->assertFalse($f->hasErrors());
497 | $q = $f->buildQueryObject()->query;
498 | $this->assertTrue($q['orderby'] == 'title');
499 | $this->assertTrue(!isset($q['meta_key']));
500 |
501 | }
502 |
503 | public function testGenericFieldIdApplies() {
504 | $args = array();
505 | $args['fields'][] = array(
506 | 'type' => 'generic',
507 | 'format' => 'select',
508 | 'id' => 'bleep'
509 | );
510 | $f = new Factory($args);
511 | $inputs = $f->getForm()->getInputs();
512 | $input = $inputs[0];
513 | $this->assertTrue($input->getId() == 'bleep');
514 | }
515 |
516 |
517 |
518 | }
--------------------------------------------------------------------------------