|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests the Fieldmanager Radio Field |
| 5 | + * |
| 6 | + * @group field |
| 7 | + * @group radio |
| 8 | + */ |
| 9 | +class Test_Fieldmanager_Radios_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->custom_datasource = new Fieldmanager_Datasource( array( |
| 22 | + 'options' => array( 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ) |
| 23 | + ) ); |
| 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 | + public function test_repeatable_save() { |
| 44 | + $fm = new Fieldmanager_Radios( array( |
| 45 | + 'name' => 'base_field', |
| 46 | + 'limit' => 0, |
| 47 | + 'options' => array( 'one', 'two', 'three' ), |
| 48 | + ) ); |
| 49 | + |
| 50 | + $fm->add_meta_box( 'base_field', $this->post->post_type )->save_to_post_meta( $this->post->ID, array( 'two' ) ); |
| 51 | + $saved_value = get_post_meta( $this->post->ID, 'base_field', true ); |
| 52 | + $this->assertSame( array( 'two' ), $saved_value ); |
| 53 | + |
| 54 | + $fm->add_meta_box( 'base_field', $this->post->post_type )->save_to_post_meta( $this->post->ID, array( 'two', 'three' ) ); |
| 55 | + $saved_value = get_post_meta( $this->post->ID, 'base_field', true ); |
| 56 | + $this->assertSame( array( 'two', 'three' ), $saved_value ); |
| 57 | + |
| 58 | + $fm->add_meta_box( 'base_field', $this->post->post_type )->save_to_post_meta( $this->post->ID, '' ); |
| 59 | + $saved_value = get_post_meta( $this->post->ID, 'base_field', true ); |
| 60 | + $this->assertEquals( null, $saved_value ); |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | +} |
0 commit comments