-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-cmb-types-base.php
185 lines (162 loc) · 4.82 KB
/
test-cmb-types-base.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
<?php
/**
* CMB2_Types tests
*
* @package Tests_CMB2
* @author WebDevStudios
* @license GPL-2.0+
* @link http://webdevstudios.com
*/
require_once( 'cmb-tests-base.php' );
abstract class Test_CMB2_Types_Base extends Test_CMB2 {
/**
* Set up the test fixture
*/
public function setUp() {
parent::setUp();
$this->cmb_id = 'test';
$this->text_type_field = array(
'name' => 'Name',
'desc' => 'This is a description',
'id' => 'field_test_field',
'type' => 'text',
);
$this->field_test = array(
'id' => 'field_test',
'fields' => array(
$this->text_type_field,
),
);
$this->attributes_test = array(
'id' => 'attributes_test',
'fields' => array(
array(
'name' => 'Name',
'desc' => 'This is a description',
'id' => 'attributes_test_field',
'type' => 'text',
'attributes' => array(
'type' => 'number',
'disabled' => 'disabled',
'id' => 'arbitrary-id',
'data-test' => json_encode( array(
'one' => 'One',
'two' => 'Two',
'true' => true,
'false' => false,
'array' => array(
'nested_data' => true,
),
) ),
),
),
),
);
$this->options_test = array(
'id' => 'options_test',
'fields' => array(
array(
'name' => 'Name',
'description' => 'This is a description',
'id' => 'options_test_field',
'type' => 'select',
'options' => array(
'one' => 'One',
'two' => 'Two',
'true' => true,
'false' => false,
),
),
),
);
$this->options_cb_test = array(
'id' => 'options_cb_test',
'fields' => array(
array(
'name' => 'Name',
'description' => 'This is a description',
'id' => 'options_cb_test_field',
'type' => 'select',
'options_cb' => array( $this, 'options_cb' ),
),
),
);
$this->options_cb_and_array_test = array(
'id' => 'options_cb_and_array_test',
'fields' => array(
array(
'name' => 'Name',
'description' => 'This is a description',
'id' => 'options_cb_and_array_test_field',
'type' => 'select',
'options' => array(
'one' => 'One',
'two' => 'Two',
'true' => true,
'false' => false,
),
'options_cb' => array( $this, 'options_cb' ),
),
),
);
$this->post_id = $this->factory->post->create();
$this->term = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'test_category' ) );
$this->term2 = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'number_2' ) );
wp_set_object_terms( $this->post_id, 'test_category', 'category' );
$this->img_name = 'image.jpg';
$this->attachment_id = $this->factory->attachment->create_object( $this->img_name, $this->post_id, array(
'post_mime_type' => 'image/jpeg',
'post_type' => 'attachment'
) );
$this->attachment_id2 = $this->factory->attachment->create_object( '2nd-'.$this->img_name, $this->post_id, array(
'post_mime_type' => 'image/jpeg',
'post_type' => 'attachment'
) );
}
public function tearDown() {
parent::tearDown();
}
protected function check_box_assertion( $output, $checked = false ) {
$checked = $checked ? ' checked="checked"' : '';
$this->assertHTMLstringsAreEqual(
'<input type="checkbox" class="cmb2-option cmb2-list" name="field_test_field" id="field_test_field" value="on"'. $checked .'/><label for="field_test_field"><span class="cmb2-metabox-description">This is a description</span></label>',
is_string( $output ) ? $output : $this->capture_render( $output )
);
}
protected function file_sprintf( $args ) {
return sprintf( '<li class="file-status"><span>' . __( 'File:', 'cmb2' ) . ' <strong>%1$s</strong></span> (<a href="%3$s" target="_blank" rel="external">' . __( 'Download','cmb2' ) . '</a> / <a href="#" class="cmb2-remove-file-button">' . __( 'Remove', 'cmb2' ) . '</a>)<input type="hidden" name="field_test_field[%2$d]" id="filelist-%2$d" value="%3$s" data-id=\'%2$d\'/></li>',
$args['file_name'],
$args['attachment_id'],
$args['url']
);
}
/**
* Test_CMB2_Types helper methods
*/
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 );
}
}
return new CMB2_Field( array(
'field_args' => $args,
'object_id' => $this->post_id,
) );
}
protected function get_field_type_object( $args = '' ) {
$field = is_a( $args, 'CMB2_Field' ) ? $args : $this->get_field_object( $args );
return new CMB2_Types( $field );
}
}
/**
* Simply allows access to the dependencies protected property (for testing)
*/
class Test_CMB2_JS extends CMB2_JS {
public static function dependencies() {
return parent::$dependencies;
}
}