diff --git a/php/class-fieldmanager-checkboxes.php b/php/class-fieldmanager-checkboxes.php
index a91bcd5847..36a3705519 100644
--- a/php/class-fieldmanager-checkboxes.php
+++ b/php/class-fieldmanager-checkboxes.php
@@ -28,4 +28,19 @@ public function form_element( $value = array() ) {
);
}
+ /**
+ * Override function to allow all boxes to be checked by default.
+ * @param string $current_option this option
+ * @param array $options all valid options
+ * @param string $attribute
+ * @return string $attribute on match, empty on failure.
+ */
+ public function option_selected( $current_option, $options, $attribute ) {
+ if ( ( ( $options != null && !empty( $options ) ) && in_array( $current_option, $options ) ) || ( 'checked' == $this->default_value && in_array( $this->default_value, $options ) ) ) {
+ return $attribute;
+ } else {
+ return '';
+ }
+ }
+
}
\ No newline at end of file
diff --git a/php/datasource/class-fieldmanager-datasource-term.php b/php/datasource/class-fieldmanager-datasource-term.php
index 44ebb62953..5a8555a11e 100644
--- a/php/datasource/class-fieldmanager-datasource-term.php
+++ b/php/datasource/class-fieldmanager-datasource-term.php
@@ -190,7 +190,12 @@ public function presave_alter_values( Fieldmanager_Field $field, $values, $curre
}
$this->save_taxonomy( $tax_values, $field->data_id );
}
- if ( $this->only_save_to_taxonomy ) return array();
+ if ( $this->only_save_to_taxonomy ) {
+ if ( empty( $values ) && ! ( $this->append_taxonomy ) ) {
+ $this->save_taxonomy( array(), $field->data_id );
+ }
+ return array();
+ }
return $values;
}
diff --git a/php/datasource/class-fieldmanager-datasource-user.php b/php/datasource/class-fieldmanager-datasource-user.php
index f14e5f7dd3..ffacb71f31 100644
--- a/php/datasource/class-fieldmanager-datasource-user.php
+++ b/php/datasource/class-fieldmanager-datasource-user.php
@@ -4,7 +4,7 @@
*/
/**
- * Data source for WordPress Posts, for autocomplete and option types.
+ * Data source for WordPress Users, for autocomplete and option types.
* @package Fieldmanager_Datasource
*/
class Fieldmanager_Datasource_User extends Fieldmanager_Datasource {
@@ -35,6 +35,25 @@ class Fieldmanager_Datasource_User extends Fieldmanager_Datasource {
*/
public $display_property = 'display_name';
+ /**
+ * @var array
+ * Allowed display properties for validation.
+ */
+ protected $allowed_display_properties = array( 'display_name', 'user_login', 'user_email', 'user_nicename' );
+
+ /**
+ * @var string
+ * Store property. Defaults to ID, but can also be 'user_login', 'user_email',
+ * or 'user_nicename'.
+ */
+ public $store_property = 'ID';
+
+ /**
+ * @var array
+ * Allowed store properties for validation.
+ */
+ protected $allowed_store_properties = array( 'ID', 'user_login', 'user_email', 'user_nicename' );
+
/**
* @var string
* Capability required to refer to a user via this datasource.
@@ -44,23 +63,66 @@ class Fieldmanager_Datasource_User extends Fieldmanager_Datasource {
/**
* @var string|Null
- * If not empty, set this post's ID as a value on the user. This is used to
+ * If not empty, set this object's ID as a value on the user. This is used to
* establish two-way relationships.
*/
public $reciprocal = Null;
- // constructor not required for this datasource; options are just set to keys,
- // which Fieldmanager_Datasource does.
+ /**
+ * Constructor. Used for validation.
+ */
+ public function __construct( $options = array() ) {
+ parent::__construct( $options );
+
+ // Validate improper usage of store property
+ if ( ! in_array( $this->store_property, $this->allowed_store_properties ) ) {
+ throw new FM_Developer_Exception( sprintf(
+ __( 'Store property %s is invalid. Must be one of %s.', 'fieldmanager' ),
+ $this->store_property,
+ implode( ', ', $this->allowed_store_properties )
+ ) );
+ }
+
+ if ( ! empty( $this->reciprocal ) && 'ID' != $this->store_property ) {
+ throw new FM_Developer_Exception( __( 'You cannot use reciprocal relationships with FM_Datasource_User if store_property is not set to ID.', 'fieldmanager' ) );
+ }
+
+ // Validate improper usage of display property
+ if ( ! in_array( $this->display_property, $this->allowed_display_properties ) ) {
+ throw new FM_Developer_Exception( sprintf(
+ __( 'Display property %s is invalid. Must be one of %s.', 'fieldmanager' ),
+ $this->display_property,
+ implode( ', ', $this->allowed_display_properties )
+ ) );
+ }
+ }
/**
- * Get a post title by post ID
+ * Get a user by the specified field.
* @param int $value post_id
- * @return string post title
+ * @return int|string
*/
public function get_value( $value ) {
- $id = intval( $value );
- $user = get_userdata( $id );
- return $user ? $user->{$this->display_property} : '';
+ switch ( $this->store_property ) {
+ case 'ID':
+ $field = 'id';
+ break;
+ case 'user_nicename':
+ $field = 'slug';
+ break;
+ case 'user_email':
+ $field = 'email';
+ break;
+ case 'user_login':
+ $field = 'login';
+ break;
+ }
+
+ // Sanitize the value
+ $value = $this->sanitize_value( $value );
+
+ $user = get_user_by( $field, $value );
+ return $user ? $user->{$this->display_property} : '';
}
/**
@@ -73,14 +135,20 @@ public function get_items( $fragment = Null ) {
if ( is_callable( $this->query_callback ) ) {
return call_user_func( $this->query_callback, $fragment );
}
+
$default_args = array();
$user_args = array_merge( $default_args, $this->query_args );
$ret = array();
- if ( $fragment ) $user_args['search'] = $fragment;
+
+ if ( $fragment ) {
+ $user_args['search'] = $fragment;
+ }
+
$users = get_users( $user_args );
foreach ( $users as $u ) {
- $ret[$u->ID] = $u->{$this->display_property};
+ $ret[ $u->{$this->store_property} ] = $u->{$this->display_property};
}
+
return $ret;
}
@@ -98,44 +166,57 @@ public function get_ajax_action() {
}
/**
- * For post relationships, delete reciprocal post metadata prior to saving (presave will re-add)
+ * Delete reciprocal user metadata prior to saving (presave will re-add).
+ * Reciprocal relationships are not possible if we are not storing by ID.
* @param array $values new post values
* @param array $current_values existing post values
*/
public function presave_alter_values( Fieldmanager_Field $field, $values, $current_values ) {
- if ( $field->data_type != 'post' || !$this->reciprocal ) return $values;
- foreach ( $current_values as $user_id ) {
- delete_user_meta( $user_id, $this->reciprocal, $field->data_id );
- }
+ if ( $field->data_type != 'post' || ! $this->reciprocal || 'ID' != $this->store_property ) {
+ return $values;
+ }
+
+ if ( ! empty( $current_values ) ) {
+ foreach ( $current_values as $user_id ) {
+ delete_user_meta( $user_id, $this->reciprocal, $field->data_id );
+ }
+ }
+
return $values;
}
/**
- * Handle reciprocal postmeta
+ * Handle reciprocal usermeta.
+ * Reciprocal relationships are not possible if we are not storing by ID.
* @param int $value
* @return string
*/
public function presave( Fieldmanager_Field $field, $value, $current_value ) {
- if ( empty( $value ) ) return;
+ if ( empty( $value ) ) {
+ return;
+ }
+
$return_single = False;
if ( !is_array( $value ) ) {
$return_single = True;
$value = array( $value );
}
+
foreach ( $value as $i => $v ) {
- $value[$i] = intval( $v );
- if( !current_user_can( $this->capability, $v ) ) {
+ $value[$i] = $this->sanitize_value( $v );
+ if( ! current_user_can( $this->capability, $v ) ) {
wp_die( esc_html( sprintf( __( 'Tried to refer to user "%s" which current user cannot edit.', 'fieldmanager' ), $v ) ) );
}
- if ( $this->reciprocal ) {
+ if ( $this->reciprocal && 'ID' == $this->store_property ) {
add_user_meta( $v, $this->reciprocal, $field->data_id );
}
}
+
return $return_single ? $value[0] : $value;
}
/**
- * Get view link for a user
+ * Get view link for a user.
* @param int $value
* @return string
*/
@@ -144,7 +225,7 @@ public function get_view_link( $value ) {
}
/**
- * Get edit link for a user
+ * Get edit link for a user.
* @param int $value
* @return string
*/
@@ -156,4 +237,25 @@ public function get_edit_link( $value ) {
esc_html__( 'Edit', 'fieldmanager' )
);
}
-}
\ No newline at end of file
+
+ /**
+ * Sanitize the value based on store_property.
+ * @param int|string $value
+ * @return int|string
+ */
+ protected function sanitize_value( $value ) {
+ switch ( $this->store_property ) {
+ case 'ID':
+ $value = intval( $value );
+ break;
+ case 'user_email':
+ $value = sanitize_email( $value );
+ break;
+ default:
+ $value = sanitize_text_field( $value );
+ break;
+ }
+
+ return $value;
+ }
+}
diff --git a/tests/php/test-fieldmanager-checkboxes-field.php b/tests/php/test-fieldmanager-checkboxes-field.php
new file mode 100644
index 0000000000..15ac1290f1
--- /dev/null
+++ b/tests/php/test-fieldmanager-checkboxes-field.php
@@ -0,0 +1,172 @@
+post_id = $this->factory->post->create( array( 'post_title' => rand_str(), 'post_date' => '2009-07-01 00:00:00' ) );
+ $this->post = get_post( $this->post_id );
+
+ $this->months = array( 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' );
+
+ $this->custom_datasource = new Fieldmanager_Datasource( array( 'options' => $this->months ) );
+ }
+
+ /**
+ * Helper which returns the post meta box HTML for a given field;
+ *
+ * @param object $field Some Fieldmanager_Field object.
+ * @param array $test_data Data to save (and use when rendering)
+ * @return string Rendered HTML
+ */
+ private function _get_html_for( $field, $test_data = null ) {
+ ob_start();
+ $context = $field->add_meta_box( 'test meta box', $this->post );
+ if ( $test_data ) {
+ $context->save_to_post_meta( $this->post_id, $test_data );
+ }
+ $context->render_meta_box( $this->post );
+ return ob_get_clean();
+ }
+
+ private function _get_input_field_regex( $name, $value, $checked = false ) {
+ if ( is_array( $value ) ) {
+ $v = array_keys( $value );
+ $v = $v[0];
+ $label = $value[ $v ];
+ $value = $v;
+ } else {
+ $label = $value;
+ }
+
+ return sprintf( '#'
+ . '\s*#si',
+ $name, $value, $label );
+ }
+
+ public function test_basic_render() {
+ $fm = new Fieldmanager_Checkboxes( array(
+ 'name' => 'base_field',
+ 'options' => array( 'one', 'two', 'three' ),
+ ) );
+
+ $html = $this->_get_html_for( $fm );
+
+ $this->assertRegExp( $this->_get_input_field_regex( 'base_field', 'one' ), $html );
+ $this->assertRegExp( $this->_get_input_field_regex( 'base_field', 'two' ), $html );
+ $this->assertRegExp( $this->_get_input_field_regex( 'base_field', 'three' ), $html );
+ }
+
+ public function test_basic_save() {
+ $fm = new Fieldmanager_Checkboxes( array(
+ 'name' => 'base_field',
+ 'options' => array( 'one', 'two', 'three' ),
+ ) );
+
+ $html = $this->_get_html_for( $fm, array( 'two' ) );
+
+ $this->assertRegExp( $this->_get_input_field_regex( 'base_field', 'one' ), $html );
+ $this->assertRegExp( $this->_get_input_field_regex( 'base_field', 'two', true ), $html );
+ $this->assertRegExp( $this->_get_input_field_regex( 'base_field', 'three' ), $html );
+ }
+
+ public function test_associative_render() {
+ $fm = new Fieldmanager_Checkboxes( array(
+ 'name' => 'base_field',
+ 'options' => array( 1 => 'one', 2 => 'two', 3 => 'three' ),
+ ) );
+
+ $html = $this->_get_html_for( $fm );
+
+ $this->assertRegExp( $this->_get_input_field_regex( 'base_field', array( 1 => 'one' ) ), $html );
+ $this->assertRegExp( $this->_get_input_field_regex( 'base_field', array( 2 => 'two' ) ), $html );
+ $this->assertRegExp( $this->_get_input_field_regex( 'base_field', array( 3 => 'three' ) ), $html );
+
+ $html = $this->_get_html_for( $fm, array( 2 ) );
+
+ $this->assertRegExp( $this->_get_input_field_regex( 'base_field', array( 1 => 'one' ) ), $html );
+ $this->assertRegExp( $this->_get_input_field_regex( 'base_field', array( 2 => 'two' ), true ), $html );
+ $this->assertRegExp( $this->_get_input_field_regex( 'base_field', array( 3 => 'three' ) ), $html );
+ }
+
+ public function test_default_value() {
+ $fm = new Fieldmanager_Checkboxes( array(
+ 'name' => 'base_field',
+ 'options' => array( 'one', 'two', 'three' ),
+ 'default_value' => 'two',
+ ) );
+
+ $html = $this->_get_html_for( $fm );
+
+ $this->assertRegExp( $this->_get_input_field_regex( 'base_field', 'one' ), $html );
+ $this->assertRegExp( $this->_get_input_field_regex( 'base_field', 'two', true ), $html );
+ $this->assertRegExp( $this->_get_input_field_regex( 'base_field', 'three' ), $html );
+ }
+
+ public function test_datasource() {
+ $fm = new Fieldmanager_Checkboxes( array(
+ 'name' => 'base_field',
+ 'datasource' => $this->custom_datasource,
+ ) );
+
+ $html = $this->_get_html_for( $fm );
+
+ foreach ( $this->months as $month ) {
+ $this->assertRegExp( $this->_get_input_field_regex( 'base_field', $month ), $html );
+ }
+ }
+
+ public function test_datasource_default_value() {
+ $fm = new Fieldmanager_Checkboxes( array(
+ 'name' => 'base_field',
+ 'default_value' => 'February',
+ 'datasource' => $this->custom_datasource,
+ ) );
+
+ $html = $this->_get_html_for( $fm );
+
+ foreach ( $this->months as $month ) {
+ $this->assertRegExp( $this->_get_input_field_regex( 'base_field', $month, ( 'February' === $month ) ), $html );
+ }
+ }
+
+ public function test_datasource_save() {
+ $fm = new Fieldmanager_Checkboxes( array(
+ 'name' => 'base_field',
+ 'datasource' => $this->custom_datasource,
+ ) );
+
+ $html = $this->_get_html_for( $fm, array( 'February' ) );
+
+ foreach ( $this->months as $month ) {
+ $this->assertRegExp( $this->_get_input_field_regex( 'base_field', $month, ( 'February' === $month ) ), $html );
+ }
+ }
+
+ public function test_datasource_default_value_all() {
+ $fm = new Fieldmanager_Checkboxes( array(
+ 'name' => 'base_field',
+ 'default_value' => 'checked',
+ 'datasource' => $this->custom_datasource,
+ ) );
+
+ $html = $this->_get_html_for( $fm );
+
+ foreach ( $this->months as $month ) {
+ $this->assertRegExp( $this->_get_input_field_regex( 'base_field', $month, true ), $html );
+ }
+ }
+
+}
diff --git a/tests/php/test-fieldmanager-datasource-term.php b/tests/php/test-fieldmanager-datasource-term.php
index 3811257cfa..fc3cc0e3b5 100644
--- a/tests/php/test-fieldmanager-datasource-term.php
+++ b/tests/php/test-fieldmanager-datasource-term.php
@@ -196,6 +196,39 @@ public function test_datasource_term_save_empty() {
$post_terms = wp_get_post_terms( $this->post->ID, $this->term->taxonomy, array( 'fields' => 'names' ) );
$this->assertCount( 0, $post_terms );
}
+
+ /**
+ * Test behavior when set to save only to taxonomy and not append.
+ * Ensure that all terms are removed in this case.
+ * This has a specific use case for Fieldmanager_Checkboxes so testing there.
+ * However, this applies to all field types that can create this scenario.
+ */
+ public function test_datasource_term_save_to_taxonomy_empty() {
+ $term_taxonomy = new Fieldmanager_Checkboxes( array(
+ 'name' => 'test_terms',
+ 'limit' => 0,
+ 'datasource' => new Fieldmanager_Datasource_Term( array(
+ 'taxonomy' => $this->term->taxonomy,
+ 'only_save_to_taxonomy' => true,
+ 'append_taxonomy' => false,
+ ) ),
+ ) );
+
+ $term = $this->factory->tag->create_and_get( array( 'name' => rand_str() ) );
+ $terms = array( $this->term->term_id, $term->term_id );
+
+ $this->save_values( $term_taxonomy, $this->post, $terms );
+
+ $post_terms = wp_get_post_terms( $this->post->ID, $this->term->taxonomy, array( 'fields' => 'ids' ) );
+ $this->assertSame( sort( $terms ), sort( $post_terms ) );
+
+ $terms = array();
+ $this->save_values( $term_taxonomy, $this->post, $terms );
+
+ $post_terms = wp_get_post_terms( $this->post->ID, $this->term->taxonomy, array( 'fields' => 'ids' ) );
+ $this->assertSame( $terms, $post_terms );
+
+ }
/**
* Test behavior when only saving to taxonomy within a single repeating
diff --git a/tests/php/test-fieldmanager-datasource-user.php b/tests/php/test-fieldmanager-datasource-user.php
new file mode 100644
index 0000000000..11fe5f2702
--- /dev/null
+++ b/tests/php/test-fieldmanager-datasource-user.php
@@ -0,0 +1,276 @@
+author = $this->factory->user->create( array( 'role' => 'author', 'user_login' => 'author', 'user_email' => 'test@test.com' ) );
+ $this->editor = $this->factory->user->create( array( 'role' => 'editor', 'user_login' => 'editor' ) );
+ $this->administrator = $this->factory->user->create( array( 'role' => 'administrator', 'user_login' => 'administrator' ) );
+
+ wp_set_current_user( $this->administrator );
+
+ $this->post = $this->factory->post->create_and_get( array(
+ 'post_status' => 'draft',
+ 'post_content' => rand_str(),
+ 'post_title' => rand_str(),
+ 'post_author' => $this->author,
+ ) );
+ }
+
+ /**
+ * Set up the request environment values and save the data.
+ *
+ * @param Fieldmanager_Field $field
+ * @param WP_Post $post
+ * @param mixed $values
+ */
+ public function save_values( $field, $post, $values ) {
+ $_POST = array(
+ 'post_ID' => $post->ID,
+ 'action' => 'editpost',
+ 'post_type' => $post->post_type,
+ "fieldmanager-{$field->name}-nonce" => wp_create_nonce( "fieldmanager-save-{$field->name}" ),
+ $field->name => $values,
+ );
+
+ $field->add_meta_box( $field->name, $post->post_type )->save_to_post_meta( $post->ID, $values );
+ }
+
+ /**
+ * Test that when configured to do so, child posts will store a reciprocal post ID in user meta.
+ */
+ public function test_reciprocal_meta() {
+ $reciprocal = new Fieldmanager_Autocomplete( array(
+ 'name' => 'test_reciprocal',
+ 'datasource' => new Fieldmanager_Datasource_User( array(
+ 'reciprocal' => 'reciprocal_post',
+ ) ),
+ ) );
+
+ $this->assertEquals( null, get_user_meta( $this->author, 'reciprocal_post', true ) );
+
+ $this->save_values( $reciprocal, $this->post, $this->author );
+
+ $this->assertEquals( $this->author, get_post_meta( $this->post->ID, 'test_reciprocal', true ) );
+ $this->assertEquals( $this->post->ID, get_user_meta( $this->author, 'reciprocal_post', true ) );
+ }
+
+ /**
+ * Test that various store properties work
+ */
+ public function test_store_properties() {
+ $user = get_userdata( $this->author );
+
+ $store_id = new Fieldmanager_Autocomplete( array(
+ 'name' => 'test_store_id',
+ 'datasource' => new Fieldmanager_Datasource_User( array(
+ 'store_property' => 'ID',
+ ) ),
+ ) );
+
+ $this->save_values( $store_id, $this->post, $user->ID );
+ $this->assertEquals( $user->ID, get_post_meta( $this->post->ID, 'test_store_id', true ) );
+
+ $store_user_login = new Fieldmanager_Autocomplete( array(
+ 'name' => 'test_store_user_login',
+ 'datasource' => new Fieldmanager_Datasource_User( array(
+ 'store_property' => 'user_login',
+ ) ),
+ ) );
+
+ $this->save_values( $store_user_login, $this->post, $user->user_login );
+ $this->assertEquals( $user->user_login, get_post_meta( $this->post->ID, 'test_store_user_login', true ) );
+
+ $store_user_email = new Fieldmanager_Autocomplete( array(
+ 'name' => 'test_store_user_email',
+ 'datasource' => new Fieldmanager_Datasource_User( array(
+ 'store_property' => 'user_email',
+ ) ),
+ ) );
+
+ $this->save_values( $store_user_email, $this->post, $user->user_email );
+ $this->assertEquals( $user->user_email, get_post_meta( $this->post->ID, 'test_store_user_email', true ) );
+
+ $store_user_nicename = new Fieldmanager_Autocomplete( array(
+ 'name' => 'test_store_user_nicename',
+ 'datasource' => new Fieldmanager_Datasource_User( array(
+ 'store_property' => 'user_nicename',
+ ) ),
+ ) );
+
+ $this->save_values( $store_user_nicename, $this->post, $user->user_nicename );
+ $this->assertEquals( $user->user_nicename, get_post_meta( $this->post->ID, 'test_store_user_nicename', true ) );
+ }
+
+ /**
+ * Test creating a field with an invalid store property.
+ * @expectedException FM_Developer_Exception
+ */
+ public function test_save_invalid_store_property() {
+ $test_invalid = new Fieldmanager_Autocomplete( array(
+ 'name' => 'test_invalid',
+ 'datasource' => new Fieldmanager_Datasource_User( array(
+ 'store_property' => 'invalid',
+ ) ),
+ ) );
+
+ $this->save_values( $test_invalid, $this->post, $this->author );
+ }
+
+ /**
+ * Test creating a field with an invalid display property.
+ * @expectedException FM_Developer_Exception
+ */
+ public function test_save_invalid_display_property() {
+ $test_invalid = new Fieldmanager_Autocomplete( array(
+ 'name' => 'test_invalid',
+ 'datasource' => new Fieldmanager_Datasource_User( array(
+ 'display_property' => 'invalid',
+ ) ),
+ ) );
+
+ $this->save_values( $test_invalid, $this->post, $this->author );
+ }
+
+ /**
+ * Test that we fail when trying to use reciprocals with something other than ID as a store property.
+ * @expectedException FM_Developer_Exception
+ */
+ public function test_save_invalid_reciprocal() {
+ $test_invalid = new Fieldmanager_Autocomplete( array(
+ 'name' => 'test_invalid',
+ 'datasource' => new Fieldmanager_Datasource_User( array(
+ 'store_property' => 'user_login',
+ 'reciprocal' => 'some_field',
+ ) ),
+ ) );
+
+ $this->save_values( $test_invalid, $this->post, $this->author );
+ }
+
+ /**
+ * Test that this fails when a user doesn't have permission to list users.
+ * @expectedException WPDieException
+ */
+ public function test_save_permissions() {
+ wp_set_current_user( $this->author );
+
+ $test_invalid = new Fieldmanager_Autocomplete( array(
+ 'name' => 'test_invalid',
+ 'datasource' => new Fieldmanager_Datasource_User(),
+ ) );
+
+ $this->save_values( $test_invalid, $this->post, $this->editor );
+
+ wp_set_current_user( $this->author );
+
+ $this->save_values( $test_invalid, $this->post, $this->editor );
+ }
+
+ /**
+ * Test that display property returns the correct value in all reasonable cases.
+ */
+ public function test_display_properties() {
+ $user = get_userdata( $this->author );
+
+ $test_display_name = new Fieldmanager_Autocomplete( array(
+ 'name' => 'test_display_name',
+ 'datasource' => new Fieldmanager_Datasource_User( array(
+ 'display_property' => 'display_name',
+ ) ),
+ ) );
+
+ $test_users_display_name = $test_display_name->datasource->get_items( $user->user_login );
+ $this->assertEquals( $user->display_name, $test_users_display_name[ $user->ID ] );
+
+ $test_user_login = new Fieldmanager_Autocomplete( array(
+ 'name' => 'test_user_login',
+ 'datasource' => new Fieldmanager_Datasource_User( array(
+ 'display_property' => 'user_login',
+ ) ),
+ ) );
+
+ $test_users_user_login = $test_user_login->datasource->get_items( $user->user_login );
+ $this->assertEquals( $user->user_login, $test_users_user_login[ $user->ID ] );
+
+ $test_user_email = new Fieldmanager_Autocomplete( array(
+ 'name' => 'test_user_email',
+ 'datasource' => new Fieldmanager_Datasource_User( array(
+ 'display_property' => 'user_email',
+ ) ),
+ ) );
+
+ $test_users_user_email = $test_user_email->datasource->get_items( $user->user_login );
+ $this->assertEquals( $user->user_email, $test_users_user_email[ $user->ID ] );
+
+ $test_user_nicename = new Fieldmanager_Autocomplete( array(
+ 'name' => 'test_user_nicename',
+ 'datasource' => new Fieldmanager_Datasource_User( array(
+ 'display_property' => 'user_nicename',
+ ) ),
+ ) );
+
+ $test_users_user_nicename = $test_user_nicename->datasource->get_items( $user->user_login );
+ $this->assertEquals( $user->user_nicename, $test_users_user_nicename[ $user->ID ] );
+ }
+
+ /**
+ * Test that store property returns the correct display value in all reasonable cases.
+ */
+ public function test_store_property_display() {
+ $user = get_userdata( $this->author );
+
+ $test_id = new Fieldmanager_Autocomplete( array(
+ 'name' => 'test_display_name',
+ 'datasource' => new Fieldmanager_Datasource_User( array(
+ 'store_property' => 'ID',
+ ) ),
+ ) );
+
+ $test_users_id = $test_id->datasource->get_items( $user->user_login );
+ $this->assertEquals( $user->display_name, $test_users_id[ $user->ID ] );
+ $this->assertEquals( $user->display_name, $test_id->datasource->get_value( $user->ID ) );
+
+ $test_user_login = new Fieldmanager_Autocomplete( array(
+ 'name' => 'test_user_login',
+ 'datasource' => new Fieldmanager_Datasource_User( array(
+ 'store_property' => 'user_login',
+ ) ),
+ ) );
+
+ $test_users_user_login = $test_user_login->datasource->get_items( $user->user_login );
+ $this->assertEquals( $user->display_name, $test_users_user_login[ $user->user_login ] );
+ $this->assertEquals( $user->display_name, $test_user_login->datasource->get_value( $user->user_login ) );
+
+ $test_user_email = new Fieldmanager_Autocomplete( array(
+ 'name' => 'test_user_email',
+ 'datasource' => new Fieldmanager_Datasource_User( array(
+ 'store_property' => 'user_email',
+ ) ),
+ ) );
+
+ $test_users_user_email = $test_user_email->datasource->get_items( $user->user_login );
+ $this->assertEquals( $user->display_name, $test_users_user_email[ $user->user_email ] );
+ $this->assertEquals( $user->display_name, $test_user_email->datasource->get_value( $user->user_email ) );
+
+ $test_user_nicename = new Fieldmanager_Autocomplete( array(
+ 'name' => 'test_user_nicename',
+ 'datasource' => new Fieldmanager_Datasource_User( array(
+ 'store_property' => 'user_nicename',
+ ) ),
+ ) );
+
+ $test_users_user_nicename = $test_user_nicename->datasource->get_items( $user->user_login );
+ $this->assertEquals( $user->display_name, $test_users_user_nicename[ $user->user_nicename ] );
+ $this->assertEquals( $user->display_name, $test_user_nicename->datasource->get_value( $user->user_nicename ) );
+ }
+}