|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests the Fieldmanager Checkboxes Field |
| 5 | + * |
| 6 | + * @group field |
| 7 | + * @group checkboxes |
| 8 | + */ |
| 9 | +class Test_Fieldmanager_Checkboxes_Field extends WP_UnitTestCase { |
| 10 | + public $post_id; |
| 11 | + public $post; |
| 12 | + public $custom_datasource; |
| 13 | + |
| 14 | + public function setUp() { |
| 15 | + parent::setUp(); |
| 16 | + Fieldmanager_Field::$debug = true; |
| 17 | + |
| 18 | + $this->post_id = $this->factory->post->create( array( 'post_title' => rand_str(), 'post_date' => '2009-07-01 00:00:00' ) ); |
| 19 | + $this->post = get_post( $this->post_id ); |
| 20 | + |
| 21 | + $this->months = array( 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ); |
| 22 | + |
| 23 | + $this->custom_datasource = new Fieldmanager_Datasource( array( 'options' => $this->months ) ); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Helper which returns the post meta box HTML for a given field; |
| 28 | + * |
| 29 | + * @param object $field Some Fieldmanager_Field object. |
| 30 | + * @param array $test_data Data to save (and use when rendering) |
| 31 | + * @return string Rendered HTML |
| 32 | + */ |
| 33 | + private function _get_html_for( $field, $test_data = null ) { |
| 34 | + ob_start(); |
| 35 | + $context = $field->add_meta_box( 'test meta box', $this->post ); |
| 36 | + if ( $test_data ) { |
| 37 | + $context->save_to_post_meta( $this->post_id, $test_data ); |
| 38 | + } |
| 39 | + $context->render_meta_box( $this->post ); |
| 40 | + return ob_get_clean(); |
| 41 | + } |
| 42 | + |
| 43 | + private function _get_input_field_regex( $name, $value, $checked = false ) { |
| 44 | + if ( is_array( $value ) ) { |
| 45 | + $v = array_keys( $value ); |
| 46 | + $v = $v[0]; |
| 47 | + $label = $value[ $v ]; |
| 48 | + $value = $v; |
| 49 | + } else { |
| 50 | + $label = $value; |
| 51 | + } |
| 52 | + |
| 53 | + return sprintf( '#<input\s*class="fm-element"\s*type="checkbox"\s*value="%2$s"\s*name="%1$s\[\]"\s*id="fm-%1$s-\d+-%2$s"' |
| 54 | + . ( $checked ? '\s*checked' : '' ) . '\s*/>' |
| 55 | + . '\s*<label\s*for="fm-%1$s-\d+-%2$s"\s*class="fm-option-label">\s*%3$s\s*</label>#si', |
| 56 | + $name, $value, $label ); |
| 57 | + } |
| 58 | + |
| 59 | + public function test_basic_render() { |
| 60 | + $fm = new Fieldmanager_Checkboxes( array( |
| 61 | + 'name' => 'base_field', |
| 62 | + 'options' => array( 'one', 'two', 'three' ), |
| 63 | + ) ); |
| 64 | + |
| 65 | + $html = $this->_get_html_for( $fm ); |
| 66 | + |
| 67 | + $this->assertRegExp( $this->_get_input_field_regex( 'base_field', 'one' ), $html ); |
| 68 | + $this->assertRegExp( $this->_get_input_field_regex( 'base_field', 'two' ), $html ); |
| 69 | + $this->assertRegExp( $this->_get_input_field_regex( 'base_field', 'three' ), $html ); |
| 70 | + } |
| 71 | + |
| 72 | + public function test_basic_save() { |
| 73 | + $fm = new Fieldmanager_Checkboxes( array( |
| 74 | + 'name' => 'base_field', |
| 75 | + 'options' => array( 'one', 'two', 'three' ), |
| 76 | + ) ); |
| 77 | + |
| 78 | + $html = $this->_get_html_for( $fm, array( 'two' ) ); |
| 79 | + |
| 80 | + $this->assertRegExp( $this->_get_input_field_regex( 'base_field', 'one' ), $html ); |
| 81 | + $this->assertRegExp( $this->_get_input_field_regex( 'base_field', 'two', true ), $html ); |
| 82 | + $this->assertRegExp( $this->_get_input_field_regex( 'base_field', 'three' ), $html ); |
| 83 | + } |
| 84 | + |
| 85 | + public function test_associative_render() { |
| 86 | + $fm = new Fieldmanager_Checkboxes( array( |
| 87 | + 'name' => 'base_field', |
| 88 | + 'options' => array( 1 => 'one', 2 => 'two', 3 => 'three' ), |
| 89 | + ) ); |
| 90 | + |
| 91 | + $html = $this->_get_html_for( $fm ); |
| 92 | + |
| 93 | + $this->assertRegExp( $this->_get_input_field_regex( 'base_field', array( 1 => 'one' ) ), $html ); |
| 94 | + $this->assertRegExp( $this->_get_input_field_regex( 'base_field', array( 2 => 'two' ) ), $html ); |
| 95 | + $this->assertRegExp( $this->_get_input_field_regex( 'base_field', array( 3 => 'three' ) ), $html ); |
| 96 | + |
| 97 | + $html = $this->_get_html_for( $fm, array( 2 ) ); |
| 98 | + |
| 99 | + $this->assertRegExp( $this->_get_input_field_regex( 'base_field', array( 1 => 'one' ) ), $html ); |
| 100 | + $this->assertRegExp( $this->_get_input_field_regex( 'base_field', array( 2 => 'two' ), true ), $html ); |
| 101 | + $this->assertRegExp( $this->_get_input_field_regex( 'base_field', array( 3 => 'three' ) ), $html ); |
| 102 | + } |
| 103 | + |
| 104 | + public function test_default_value() { |
| 105 | + $fm = new Fieldmanager_Checkboxes( array( |
| 106 | + 'name' => 'base_field', |
| 107 | + 'options' => array( 'one', 'two', 'three' ), |
| 108 | + 'default_value' => 'two', |
| 109 | + ) ); |
| 110 | + |
| 111 | + $html = $this->_get_html_for( $fm ); |
| 112 | + |
| 113 | + $this->assertRegExp( $this->_get_input_field_regex( 'base_field', 'one' ), $html ); |
| 114 | + $this->assertRegExp( $this->_get_input_field_regex( 'base_field', 'two', true ), $html ); |
| 115 | + $this->assertRegExp( $this->_get_input_field_regex( 'base_field', 'three' ), $html ); |
| 116 | + } |
| 117 | + |
| 118 | + public function test_datasource() { |
| 119 | + $fm = new Fieldmanager_Checkboxes( array( |
| 120 | + 'name' => 'base_field', |
| 121 | + 'datasource' => $this->custom_datasource, |
| 122 | + ) ); |
| 123 | + |
| 124 | + $html = $this->_get_html_for( $fm ); |
| 125 | + |
| 126 | + foreach ( $this->months as $month ) { |
| 127 | + $this->assertRegExp( $this->_get_input_field_regex( 'base_field', $month ), $html ); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + public function test_datasource_default_value() { |
| 132 | + $fm = new Fieldmanager_Checkboxes( array( |
| 133 | + 'name' => 'base_field', |
| 134 | + 'default_value' => 'February', |
| 135 | + 'datasource' => $this->custom_datasource, |
| 136 | + ) ); |
| 137 | + |
| 138 | + $html = $this->_get_html_for( $fm ); |
| 139 | + |
| 140 | + foreach ( $this->months as $month ) { |
| 141 | + $this->assertRegExp( $this->_get_input_field_regex( 'base_field', $month, ( 'February' === $month ) ), $html ); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + public function test_datasource_save() { |
| 146 | + $fm = new Fieldmanager_Checkboxes( array( |
| 147 | + 'name' => 'base_field', |
| 148 | + 'datasource' => $this->custom_datasource, |
| 149 | + ) ); |
| 150 | + |
| 151 | + $html = $this->_get_html_for( $fm, array( 'February' ) ); |
| 152 | + |
| 153 | + foreach ( $this->months as $month ) { |
| 154 | + $this->assertRegExp( $this->_get_input_field_regex( 'base_field', $month, ( 'February' === $month ) ), $html ); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + public function test_datasource_default_value_all() { |
| 159 | + $fm = new Fieldmanager_Checkboxes( array( |
| 160 | + 'name' => 'base_field', |
| 161 | + 'default_value' => 'checked', |
| 162 | + 'datasource' => $this->custom_datasource, |
| 163 | + ) ); |
| 164 | + |
| 165 | + $html = $this->_get_html_for( $fm ); |
| 166 | + |
| 167 | + foreach ( $this->months as $month ) { |
| 168 | + $this->assertRegExp( $this->_get_input_field_regex( 'base_field', $month, true ), $html ); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | +} |
0 commit comments