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
<?php
require_once( 'cmb-tests-base.php' );
class Test_CMB2_Options extends Test_CMB2 {
public function set_up() {
parent::set_up();
$this->option_metabox_array = array(
'id' => 'options_page',
'title' => 'Theme Options Metabox',
'show_on' => array(
'options-page' => array( 'theme_options' ),
),
'fields' => array(
'bg_color' => array(
'name' => 'Site Background Color',
'desc' => 'field description (optional)',
'id' => 'bg_color',
'type' => 'colorpicker',
'default' => '#ffffff',
),
),
);
$this->options_cmb = new CMB2( $this->option_metabox_array );
$this->opt_set = array(
'bg_color' => '#ffffff',
'my_name' => 'Justin',
);
add_option( $this->options_cmb->cmb_id, $this->opt_set );
}
public function test_cmb2_options_function() {
$opts = cmb2_options( $this->options_cmb->cmb_id );
$this->assertSame( $opts->get_options(), $this->opt_set );
}
public function test_cmb2_get_option() {
$get = get_option( $this->options_cmb->cmb_id );
$val = cmb2_get_option( $this->options_cmb->cmb_id, 'my_name' );
$this->assertSame( $this->opt_set['my_name'], $get['my_name'] );
$this->assertSame( $val, $get['my_name'] );
$this->assertSame( $val, $this->opt_set['my_name'] );
}
public function test_cmb2_get_option_bad_value() {
$opts = cmb2_options( $this->options_cmb->cmb_id );
$opts->set( '1' );
$get = get_option( $this->options_cmb->cmb_id );
$val = $opts->get_options();
$this->assertSame( '1', $get );
$this->assertSame( array( '1' ), $val );
$opts->delete_option();
$get = get_option( $this->options_cmb->cmb_id );
$val = $opts->get_options();
$this->assertSame( false, $get );
$this->assertSame( array(), $val );
$opts->set( $this->opt_set );
}
public function test_cmb2_remove_option_bad_value() {
$opts = cmb2_options( $this->options_cmb->cmb_id );
$opts->delete_option();
$val = $opts->remove( 'my_name' );
$this->assertSame( array(), $val );
$opts->set( $this->opt_set );
}
public function test_cmb2_update_option() {
$new_value = 'James';
cmb2_update_option( $this->options_cmb->cmb_id, 'my_name', $new_value );
$get = get_option( $this->options_cmb->cmb_id );
$val = cmb2_get_option( $this->options_cmb->cmb_id, 'my_name' );
$this->assertSame( $new_value, $get['my_name'] );
$this->assertSame( $val, $get['my_name'] );
$this->assertSame( $val, $new_value );
}
public function test_cmb2_with_empty_options() {
$opts = cmb2_options( 'cmb_empty_option' );
$this->assertInternalType( 'array', $opts->get_options() );
$this->assertSame( array(), $opts->get_options() );
}
public function test_cmb2_get_option_with_empty_options() {
$opts = cmb2_options( 'cmb_empty_option' );
$this->assertFalse( $opts->get( 'nothing' ) );
}
public function test_cmb2_update_option_with_empty_options() {
$new_value = 'Van Anh';
cmb2_update_option( 'cmb_empty_option', 'my_name', $new_value );
$get = get_option( 'cmb_empty_option' );
$val = cmb2_get_option( 'cmb_empty_option', 'my_name' );
$this->assertSame( $new_value, $get['my_name'] );
$this->assertSame( $val, $get['my_name'] );
$this->assertSame( $val, $new_value );
}
}