-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-cmb-types-display.php
305 lines (240 loc) · 10.4 KB
/
test-cmb-types-display.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<?php
/**
* CMB2_Types tests
*
* @package Tests_CMB2
* @author CMB2 team
* @license GPL-2.0+
* @link https://cmb2.io
*/
require_once( 'test-cmb-types-base.php' );
class Test_CMB2_Types_Display extends Test_CMB2_Types_Base {
/**
* Set up the test fixture
*/
public function setUp() {
parent::setUp();
}
public function tearDown() {
parent::tearDown();
}
// public function test_repeatable_field() {}
public function test_text() {
$this->assertDisplayFieldMatches( 'text', __FUNCTION__ );
}
public function test_hidden() {
$this->assertDisplayFieldMatches( 'hidden', __FUNCTION__ );
}
public function test_text_small() {
$this->assertDisplayFieldMatches( 'text_small', __FUNCTION__ );
}
public function test_text_medium() {
$this->assertDisplayFieldMatches( 'text_medium', __FUNCTION__ );
}
public function test_text_email() {
$this->assertDisplayFieldMatches( 'text_email', __FUNCTION__ );
}
public function test_text_url() {
$value = __FUNCTION__;
$this->assertDisplayFieldMatches( 'text_url', $value, '<a href="http://' . $value . '" rel="nofollow">http://' . $value . '</a>' );
}
public function test_text_money() {
$value = __FUNCTION__;
$this->assertDisplayFieldMatches( 'text_money', $value, '$' . $value );
}
public function test_textarea() {
$value = __FUNCTION__;
$this->assertDisplayFieldMatches( 'textarea_small', $value, '<p>' . $value . '</p>' );
}
public function test_textarea_small() {
$value = __FUNCTION__;
$this->assertDisplayFieldMatches( 'textarea_small', $value, '<p>' . $value . '</p>' );
}
public function test_textarea_code() {
$value = __FUNCTION__;
$this->assertDisplayFieldMatches( 'textarea_code', $value, '<xmp class="cmb2-code">' . $value . '</xmp>' );
}
public function test_wysiwyg() {
$value = __FUNCTION__;
$this->assertDisplayFieldMatches( 'textarea_small', $value, '<p>' . $value . '</p>' );
}
public function test_text_date() {
$value = time();
$this->assertDisplayFieldMatches( 'text_date', $value, date( 'm/d/Y', $value ) );
}
public function test_text_date_timestamp() {
$value = time();
$this->assertDisplayFieldMatches( 'text_date_timestamp', $value, date( 'm/d/Y', $value ) );
}
public function test_text_time() {
$value = time();
$this->assertDisplayFieldMatches( 'text_time', $value, date( 'h:i A', $value ) );
}
public function test_text_datetime_timestamp() {
$value = time();
$this->assertDisplayFieldMatches( 'text_datetime_timestamp', $value, date( 'm/d/Y', $value ) );
}
public function test_text_datetime_timestamp_timezone() {
if ( version_compare( phpversion(), '5.3', '<' ) ) {
$this->assertTrue( true );
return;
}
$time = time();
$sanitizer = new CMB2_Sanitize( $this->get_field_object( 'text_datetime_timestamp_timezone' ), array(
'date' => date( 'm/d/Y', $time ),
'time' => date( 'h:i A', $time ),
'timezone' => date( 'e', $time ),
) );
$saved_value = $sanitizer->text_datetime_timestamp_timezone();
$datetime = unserialize( $saved_value );
$tz = $datetime->getTimezone();
$tzstring = $tz->getName();
$expected = date( 'm/d/Y h:i A', $time ) . ', ' . $tzstring;
$this->assertDisplayFieldMatches( 'text_datetime_timestamp_timezone', $saved_value, $expected );
}
public function test_select_timezone() {
$value = __FUNCTION__;
$this->assertDisplayFieldMatches( 'select_timezone', $value );
}
public function test_colorpicker() {
$value = __FUNCTION__;
$this->assertDisplayFieldMatches( 'colorpicker', $value, '<span class="cmb2-colorpicker-swatch"><span style="background-color:' . $value . '"></span> ' . $value . '</span>' );
}
public function test_title() {
$this->assertDisplayFieldMatches( 'title', '' );
}
public function test_select() {
$this->assertOptionDisplayFieldMatches( 'select', 'two' );
}
public function test_taxonomy_select() {
$this->text_type_field['taxonomy'] = 'category';
$this->assertDisplayFieldMatches( 'taxonomy_select', '', '<a href="">test_category</a>' );
}
public function test_radio() {
$this->assertOptionDisplayFieldMatches( 'radio', 'two' );
}
public function test_radio_inline() {
$this->assertOptionDisplayFieldMatches( 'radio_inline', 'two', false, '<div class="cmb-column cmb-type-radio-inline cmb2-id-options-test-field cmb-inline" data-fieldtype="radio_inline">Two</div>' );
}
public function test_multicheck() {
$this->assertOptionDisplayFieldMatches( 'multicheck', array( 'one', 'two' ), 'One, Two' );
}
public function test_multicheck_inline() {
$this->assertOptionDisplayFieldMatches( 'multicheck_inline', array( 'one', 'two' ), true, '<div class="cmb-column cmb-type-multicheck-inline cmb2-id-options-test-field cmb-inline" data-fieldtype="multicheck_inline">One, Two</div>' );
}
public function test_checkbox() {
$value = 'on';
$this->assertDisplayFieldMatches( 'checkbox', $value );
}
public function test_taxonomy_radio() {
$this->text_type_field['taxonomy'] = 'category';
$this->assertDisplayFieldMatches( 'taxonomy_radio', '', '<a href="">test_category</a>' );
}
public function test_taxonomy_radio_inline() {
$this->text_type_field['taxonomy'] = 'category';
$this->assertDisplayFieldMatches( 'taxonomy_radio_inline', '', true, '<div class="cmb-column cmb-type-taxonomy-radio-inline cmb2-id-field-test-field cmb-inline" data-fieldtype="taxonomy_radio_inline"><a href="">test_category</a></div>' );
}
public function test_taxonomy_multicheck() {
$this->text_type_field['taxonomy'] = 'category';
$this->assertDisplayFieldMatches( 'taxonomy_multicheck', '', '<div class="cmb2-taxonomy-terms-category"><a href="">test_category</a></div>' );
}
public function test_taxonomy_multicheck_inline() {
$this->text_type_field['taxonomy'] = 'category';
$this->assertDisplayFieldMatches( 'taxonomy_multicheck_inline', '', true, '<div class="cmb-column cmb-type-taxonomy-multicheck-inline cmb2-id-field-test-field cmb-inline" data-fieldtype="taxonomy_multicheck_inline"><div class="cmb2-taxonomy-terms-category"><a href="">test_category</a></div></div>' );
}
/**
* @group cmb2-ajax-embed
*/
public function test_oembed() {
$vid = 'EOfy5LDpEHo';
$args = array(
'src' => 'http://www.youtube.com/embed/' . $vid . '?feature=oembed',
'url' => 'https://www.youtube.com/watch?v=' . $vid,
'field_id' => 'field_test_field',
'object_id' => $this->post_id,
);
update_post_meta( $this->post_id, $this->text_type_field['id'], $args['url'] );
$result_verifiers = array(
'connected' => array(
'<div class="cmb-column cmb-type-oembed cmb2-id-field-test-field" data-fieldtype="oembed"><div class="cmb2-oembed"><iframe ',
'src="www.youtube.com/embed/' . $vid . '?feature=oembed"',
'</iframe></div></div>',
),
'no_connection' => array(
'<div class="cmb-column cmb-type-oembed cmb2-id-field-test-field" data-fieldtype="oembed"><p class="ui-state-error-text">' . sprintf( esc_html__( 'No oEmbed Results Found for %1$s. View more info at %2$s.', 'cmb2' ), '<a href="www.youtube.com/watch?v=' . $vid . '">www.youtube.com/watch?v=' . $vid . '</a>', '<a href="codex.wordpress.org/Embeds" target="_blank">codex.wordpress.org/Embeds</a>' ) . '</p></div>',
),
);
$cb = array( $this->get_field_object( 'oembed' ), 'render_column' );
$actual_field = $this->capture_render( $cb );
$this->assertVerifiersMatch( $result_verifiers, $actual_field );
delete_post_meta( $this->post_id, $this->text_type_field['id'] );
}
public function test_file_list() {
$images = get_attached_media( 'image', $this->post_id );
$attach_1_url = get_permalink( $this->attachment_id );
$attach_2_url = get_permalink( $this->attachment_id2 );
$this->assertDisplayFieldMatches(
'file_list',
array(
$this->attachment_id => $attach_1_url,
$this->attachment_id2 => $attach_2_url,
),
'<ul class="cmb2-display-file-list"><li><div class="file-status"><span>File: <strong><a href="' . $attach_1_url . '">' . CMB2_Utils::get_file_name_from_path( $attach_1_url ) . '</a></strong></span></div></li><li><div class="file-status"><span>File: <strong><a href="' . $attach_2_url . '">' . CMB2_Utils::get_file_name_from_path( $attach_2_url ) . '</a></strong></span></div></li></ul>'
);
}
public function test_file() {
$images = get_attached_media( 'image', $this->post_id );
$attach_1_url = get_permalink( $this->attachment_id );
$field_id = $this->text_type_field['id'];
update_post_meta( $this->post_id, $field_id . '_id', $this->attachment_id );
$this->assertDisplayFieldMatches(
'file',
$attach_1_url,
'<div class="file-status"><span>File: <strong><a href="' . $attach_1_url . '">' . CMB2_Utils::get_file_name_from_path( $attach_1_url ) . '</a></strong></span></div>'
);
}
protected function assertOptionDisplayFieldMatches( $type, $value, $value_display = false, $string = false ) {
$args = $this->options_test['fields'][0];
$field_id = $args['id'];
update_post_meta( $this->post_id, $field_id, $value );
$value_display = $value_display ? $value_display : $args['options'][ $value ];
$args['type'] = $type;
$args['column'] = true;
$field = new CMB2_Field( array(
'field_args' => $args,
'object_id' => $this->post_id,
) );
$string = $string ? $string : '<div class="cmb-column cmb-type-' . str_replace( '_', '-', $type ) . ' cmb2-id-' . str_replace( '_', '-', $field_id ) . '" data-fieldtype="' . $type . '">' . $value_display . '</div>';
$this->assertHTMLstringsAreEqual(
$string,
$this->capture_render( array( $field, 'render_column' ) )
);
}
protected function assertDisplayFieldMatches( $type, $value, $value_display = false, $string = false ) {
$field_id = $this->text_type_field['id'];
if ( false !== $value ) {
update_post_meta( $this->post_id, $field_id, $value );
}
$value_display = $value_display ? $value_display : $value;
$string = $string ? $string : '<div class="cmb-column cmb-type-' . str_replace( '_', '-', $type ) . ' cmb2-id-' . str_replace( '_', '-', $field_id ) . '" data-fieldtype="' . $type . '">' . $value_display . '</div>';
$this->assertHTMLstringsAreEqual(
$string,
$this->capture_render( array( $this->get_field_object( $type ), 'render_column' ) )
);
}
protected function get_field_object( $type = '' ) {
$args = $this->text_type_field;
if ( $type ) {
if ( is_string( $type ) ) {
$args['type'] = $type;
} elseif ( is_array( $type ) ) {
$args = wp_parse_args( $type, $args );
}
}
$args['column'] = true;
return new CMB2_Field( array(
'field_args' => $args,
'object_id' => $this->post_id,
) );
}
}