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 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
<?php
require_once( 'cmb-tests-base.php' );
class Test_CMB2_Utils extends Test_CMB2 {
protected $test_empty = array(
array(
'val' => null,
'empty' => true,
),
array(
'val' => false,
'empty' => true,
),
array(
'val' => '',
'empty' => true,
),
array(
'val' => 0,
'empty' => false,
),
array(
'val' => 0.0,
'empty' => false,
),
array(
'val' => '0',
'empty' => false,
),
array(
'val' => '0.0',
'empty' => false,
),
array(
'val' => 1,
'empty' => false,
),
array(
'val' => ' ',
'empty' => false,
),
array(
'val' => "\n",
'empty' => false,
),
array(
'val' => ' ',
'empty' => false,
),
array(
'val' => array(),
'empty' => true,
),
array(
'val' => array( 0 ),
'empty' => false,
),
);
public function set_up() {
parent::set_up();
$this->post_id = $this->factory->post->create();
$this->img_name = 'test-image.jpg';
$filename = ( CMB2_TESTDATA . '/images/test-image.jpg' );
$contents = file_get_contents( $filename );
$upload = wp_upload_bits( basename( $filename ), null, $contents );
$this->attachment_id = $this->_make_attachment( $upload, $this->post_id );
}
public function tear_down() {
parent::tear_down();
}
public function test_image_id_from_url() {
$_id_value = CMB2_Utils::image_id_from_url( esc_url_raw( wp_get_attachment_url( $this->attachment_id ) ) );
if ( get_bloginfo( 'version' ) > 3.9 ) {
$this->assertEquals( $_id_value, $this->attachment_id );
} else {
$this->assertGreaterThan( 0, $this->attachment_id );
}
}
function _make_attachment( $upload, $parent_post_id = -1 ) {
$type = '';
if ( ! empty( $upload['type'] ) ) {
$type = $upload['type'];
} else {
$mime = wp_check_filetype( $upload['file'] );
if ( $mime ) {
$type = $mime['type'];
}
}
$attachment = array(
'post_title' => basename( $upload['file'] ),
'post_content' => '',
'post_type' => 'attachment',
'post_parent' => $parent_post_id,
'post_mime_type' => $type,
'guid' => $upload['url'],
);
$id = wp_insert_attachment( $attachment, $upload['file'], $parent_post_id );
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
return $id;
}
public function test_get_url_from_dir() {
$this->assertEquals(
trailingslashit( site_url() ),
CMB2_Utils::get_url_from_dir( ABSPATH )
);
foreach ( array(
'cmb2',
'wp-content/cmb2',
'vendor/cmb2/',
'wp-content/themes/cmb2/',
'wp-content/themes/twentysixteen/cmb2/',
'wp-content/plugins/cmb2/',
'wp-content/plugins/some-plugin/cmb2/',
'wp-content/mu-plugins/cmb2/',
'wp-content/mu-plugins/some-mu-plugin/cmb2/',
) as $located ) {
$this->assertEquals(
site_url( $located ),
CMB2_Utils::get_url_from_dir( ABSPATH . $located )
);
add_filter( 'theme_root', array( 'Test_CMB2_Utils_WIN', '_change_to_wamp_theme_root' ) );
$this->assertEquals(
site_url( $located ),
Test_CMB2_Utils_WIN::get_url_from_dir( ABSPATH . $located )
);
remove_filter( 'theme_root', array( 'Test_CMB2_Utils_WIN', '_change_to_wamp_theme_root' ) );
}
}
public function test_isempty() {
foreach ( $this->test_empty as $test ) {
$this->assertEquals( $test['empty'], CMB2_Utils::isempty( $test['val'] ) );
}
}
public function test_notempty() {
foreach ( $this->test_empty as $test ) {
$this->assertEquals( ! $test['empty'], CMB2_Utils::notempty( $test['val'] ) );
}
}
public function test_filter_empty() {
$vals = wp_list_pluck( $this->test_empty, 'val' );
$non_empties = array(
3 => 0,
4 => 0.0,
5 => '0',
6 => '0.0',
7 => 1,
8 => ' ',
9 => "\n",
10 => ' ',
12 => array( 0 ),
);
$this->assertEquals( $non_empties, CMB2_Utils::filter_empty( $vals ) );
}
public function test_concat_attrs() {
$data = array(
'att3' => 'att3 value',
'att5' => 'att5 value',
'att7' => 'att7 value',
'att10' => 'att10 value',
);
$data2 = array(
'val1' => 1,
'val2' => 'two',
'val3' => array(
'val1' => 1,
'val2' => 'two',
'val3' => 'two',
),
);
$attributes = array(
'rendered' => 'rendered',
'false' => false,
'value' => false,
'att3' => 'att3 value',
'att5' => 'att5 value',
'att7' => 'att7 value',
'att10' => 'att10 value',
'data-blah' => function_exists( 'wp_json_encode' ) ? wp_json_encode( $data ) : json_encode( $data ),
'data-pizza' => function_exists( 'wp_json_encode' ) ? wp_json_encode( $data2 ) : json_encode( $data2 ),
);
$to_exclude = array(
'att5',
'att7',
);
$this->assertHTMLstringsAreEqual(
'value="" att3="att3 value" att10="att10 value" data-blah=\'{"att3":"att3 value","att5":"att5 value","att7":"att7 value","att10":"att10 value"}\' data-pizza=\'{"val1":1,"val2":"two","val3":{"val1":1,"val2":"two","val3":"two"}}\'',
CMB2_Utils::concat_attrs( $attributes, $to_exclude )
);
}
public function test_ensure_array() {
$this->assertEquals( array( 'test' ), CMB2_Utils::ensure_array( '', array( 'test' ) ) );
$this->assertEquals( array( 'test' ), CMB2_Utils::ensure_array( false, array( 'test' ) ) );
$this->assertEquals( array( 'test' ), CMB2_Utils::ensure_array( 0, array( 'test' ) ) );
$this->assertEquals( array( 'test' ), CMB2_Utils::ensure_array( 'test' ) );
$this->assertEquals( array( 'test' ), CMB2_Utils::ensure_array( array( 'test' ) ) );
$this->assertEquals( array(), CMB2_Utils::ensure_array( new CMB2_Utils ) );
}
public function test_url_set() {
$cmb2_url = str_replace(
array( WP_CONTENT_DIR, WP_PLUGIN_DIR ),
array( WP_CONTENT_URL, WP_PLUGIN_URL ),
cmb2_dir()
);
$this->assertEquals( CMB2_Utils::url(), $cmb2_url );
}
public function test_array_insert() {
$array = array(
'one' => array( 1,2,3 ),
'two' => array( 1,2,3 ),
'three' => array( 1,2,3 ),
);
$new = array(
'new' => array( 4, 5, 6 ),
);
CMB2_Utils::array_insert( $array, $new, 2 );
$this->assertEquals( array(
'one' => array( 1,2,3 ),
'new' => array( 4,5,6 ),
'two' => array( 1,2,3 ),
'three' => array( 1,2,3 ),
), $array );
}
public function test_normalize_if_numeric() {
$tests = array(
array( '0.1', 0.10000000000000001 ),
array( 0.1, 0.1 ),
array( .1, .1 ),
array( 1, 1 ),
array( '1', 1 ),
array( 'one', 'one' ),
array( '0.0', 0.0 ),
array( 0, 0 ),
array( '', '' ),
);
foreach ( $tests as $index => $test ) {
$this->assertSame(
$test[1],
CMB2_Utils::normalize_if_numeric( $test[0] ),
"Test index: $index"
);
}
}
public function test_php_to_js_dateformat() {
$tests = array(
array( 'l F j, Y', 'DD MM d, yy' ),
array( 'Y-m-d', 'yy-mm-dd' ),
array( 'm-d-Y', 'mm-dd-yy' ),
array( 'F', 'MM' ),
array( 'l', 'DD' ),
array( 'F', 'MM' ),
array( 'l jS \of F Y h:i:s A', 'DD dS 'o'f MM yy hh:mm:ss TT' ),
array( 'm.d.y', 'mm.dd.y' ),
array( 'j, n, Y', 'd, m, yy' ),
array( 'Ymd', 'yymmdd' ),
array( 'j-m-y, \it \is w Day', 'd-mm-y, 'i't 'i'ss w Dtty' ),
array( '\i\t \i\s \t\h\e jS \d\a\y.', ''it' 'is' 'the' dS 'day'.' ),
);
foreach ( $tests as $index => $test ) {
$this->assertSame(
$test[1],
CMB2_Utils::php_to_js_dateformat( $test[0] ),
"Test index: $index"
);
}
}
public function test_make_valid_time_stamp() {
date_default_timezone_set( 'America/New_York' );
$this->assertSame(
1658548800,
CMB2_Utils::make_valid_time_stamp( '07/23/2022' )
);
$this->assertSame(
1658548800,
CMB2_Utils::make_valid_time_stamp( '1658548800' )
);
$this->assertSame(
1658548800,
CMB2_Utils::make_valid_time_stamp( 1658548800 )
);
$this->assertSame(
22,
CMB2_Utils::make_valid_time_stamp( 22 )
);
}
}
class Test_CMB2_Utils_WIN extends CMB2_Utils {
protected static function get_normalized_abspath() {
return self::normalize_path( 'C:\xampp\htdocs\the-site-dir' );
}
public static function _change_to_wamp_theme_root() {
return self::get_normalized_abspath() . '/wp-content/themes/';
}
}