Skip to content

Commit

Permalink
Test field rendering and normalize field values
Browse files Browse the repository at this point in the history
  • Loading branch information
mboynes committed Feb 20, 2016
1 parent 5f9722e commit 6478c69
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
15 changes: 12 additions & 3 deletions php/datasource/class-fieldmanager-datasource-post.php
Expand Up @@ -67,8 +67,14 @@ class Fieldmanager_Datasource_Post extends Fieldmanager_Datasource {
*/
public $only_save_to_post_parent = False;

// constructor not required for this datasource; options are just set to keys,
// which Fieldmanager_Datasource does.
public function __construct( $options = array() ) {
parent::__construct( $options );

// Infer $save_to_post_parent if $only_save_to_post_parent
if ( $this->only_save_to_post_parent ) {
$this->save_to_post_parent = true;
}
}

/**
* Get a post title by post ID
Expand Down Expand Up @@ -263,7 +269,10 @@ public function presave_status_transition( Fieldmanager_Field $field, $value ) {
*/
public function preload_alter_values( Fieldmanager_Field $field, $values ) {
if ( $this->only_save_to_post_parent ) {
return array( wp_get_post_parent_id( $field->data_id ) );
$post_parent = wp_get_post_parent_id( $field->data_id );
if ( $post_parent ) {
return ( 1 == $field->limit && empty( $field->multiple ) ) ? $post_parent : array( $post_parent );
}
}
return $values;
}
Expand Down
61 changes: 61 additions & 0 deletions tests/php/test-fieldmanager-datasource-post.php
Expand Up @@ -325,4 +325,65 @@ public function test_alter_child_invalid_reciprocal() {

$this->save_values( $children, $this->parent_post, $test_data );
}

public function test_post_parent_render() {
$fm = new Fieldmanager_Autocomplete( array(
'name' => 'test_parent',
'datasource' => new Fieldmanager_Datasource_Post( array(
'only_save_to_post_parent' => true,
'query_args' => array(
'post_type' => 'post'
),
) ),
) );

ob_start();
$fm->add_meta_box( 'Test Autocomplete', 'post' )->render_meta_box( $this->child_post_a, array() );
$html = ob_get_clean();
$this->assertRegExp( '/<input[^>]+type=[\'"]hidden[\'"][^>]+value=[\'"]{2}/', $html );

$this->save_values( $fm, $this->child_post_a, $this->parent_post->ID );
$child = get_post( $this->child_post_a->ID );
$this->assertEquals( $this->parent_post->ID, $child->post_parent );
$this->assertEquals( '', get_post_meta( $this->child_post_a->ID, 'test_parent', true ) );

ob_start();
$fm->add_meta_box( 'Test Autocomplete', 'post' )->render_meta_box( $this->child_post_a, array() );
$html = ob_get_clean();
$this->assertRegExp( "/<input[^>]+type=['\"]hidden['\"][^>]+value=['\"]{$this->parent_post->ID}['\"]/", $html );
}

public function test_options_post_parent_render() {
$fm = new Fieldmanager_Select( array(
'name' => 'test_parent',
'datasource' => new Fieldmanager_Datasource_Post( array(
'only_save_to_post_parent' => true,
'query_args' => array(
'post_type' => 'post',
'post_status' => 'draft',
),
) ),
) );

ob_start();
$fm->add_meta_box( 'Test Select', 'post' )->render_meta_box( $this->child_post_a, array() );
$html = ob_get_clean();
$this->assertRegExp(
sprintf( '#<option\s*value="%s"\s*>%s</option>#i', $this->parent_post->ID, $this->parent_post->post_title ),
$html
);

$this->save_values( $fm, $this->child_post_a, $this->parent_post->ID );
$child = get_post( $this->child_post_a->ID );
$this->assertEquals( $this->parent_post->ID, $child->post_parent );
$this->assertEquals( '', get_post_meta( $this->child_post_a->ID, 'test_parent', true ) );

ob_start();
$fm->add_meta_box( 'Test Select', 'post' )->render_meta_box( $this->child_post_a, array() );
$html = ob_get_clean();
$this->assertRegExp(
sprintf( '#<option\s*value="%s"\s*selected(?:\s*=\s*"selected")?\s*>%s</option>#i', $this->parent_post->ID, $this->parent_post->post_title ),
$html
);
}
}

0 comments on commit 6478c69

Please sign in to comment.