Skip to content

Commit f46e92d

Browse files
polevaultwebclaude
andcommitted
Add WPUnit tests for taxonomy field display fix (#392)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent a2c334e commit f46e92d

1 file changed

Lines changed: 292 additions & 0 deletions

File tree

Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
1+
<?php
2+
/**
3+
* Tests for the taxonomy field display fix (PR #392).
4+
*
5+
* When a WPUM field has type 'taxonomy' and its user_meta_key starts with
6+
* 'wpum_', the set_user_meta() method should retrieve the value via
7+
* get_user_meta() instead of carbon_get_user_meta(), because Carbon Fields
8+
* returns empty for taxonomy-type fields.
9+
*
10+
* The fix adds `&& $this->get_type() !== 'taxonomy'` to the elseif branch
11+
* in set_user_meta() so taxonomy fields fall through to the get_user_meta()
12+
* path.
13+
*
14+
* @see https://github.com/WPUserManager/wp-user-manager/pull/392
15+
*/
16+
17+
require_once __DIR__ . '/FieldsTestCase.php';
18+
19+
class TaxonomyFieldDisplayTest extends FieldsTestCase {
20+
21+
/**
22+
* @var int
23+
*/
24+
protected $test_group_id;
25+
26+
/**
27+
* @var int
28+
*/
29+
protected $user_id;
30+
31+
public function _setUp() {
32+
parent::_setUp();
33+
34+
// Get or create a field group.
35+
$groups = $this->groups_db->get_groups( array(
36+
'primary' => true,
37+
) );
38+
39+
if ( ! empty( $groups ) ) {
40+
$this->test_group_id = $groups[0]->get_ID();
41+
} else {
42+
$this->test_group_id = $this->groups_db->insert( array(
43+
'name' => 'Test Group',
44+
'is_primary' => 1,
45+
) );
46+
}
47+
48+
// Create a test user.
49+
$this->user_id = $this->factory()->user->create( array(
50+
'user_login' => 'taxonomy_test_user',
51+
'user_email' => 'taxonomy_test@example.com',
52+
'role' => 'subscriber',
53+
) );
54+
55+
// Register a custom taxonomy for testing.
56+
if ( ! taxonomy_exists( 'wpum_test_skill' ) ) {
57+
register_taxonomy( 'wpum_test_skill', 'user', array(
58+
'label' => 'Skills',
59+
'public' => true,
60+
'hierarchical' => false,
61+
) );
62+
}
63+
}
64+
65+
public function _tearDown() {
66+
if ( $this->user_id ) {
67+
wp_delete_user( $this->user_id );
68+
}
69+
70+
parent::_tearDown();
71+
}
72+
73+
/**
74+
* Helper: create a WPUM field in the database and return the WPUM_Field object.
75+
*
76+
* @param string $type The field type (e.g. 'taxonomy', 'text').
77+
* @param string $name The field name.
78+
* @param string $meta_key The user_meta_key value.
79+
*
80+
* @return \WPUM_Field
81+
*/
82+
private function create_field( $type, $name, $meta_key ) {
83+
$field_id = $this->fields_db->insert( array(
84+
'group_id' => $this->test_group_id,
85+
'type' => $type,
86+
'name' => $name,
87+
'field_order' => 99,
88+
) );
89+
90+
$this->assertGreaterThan( 0, $field_id, "Field '{$name}' should be inserted." );
91+
92+
// Set the user_meta_key meta.
93+
$this->field_meta_db->add_meta( $field_id, 'user_meta_key', $meta_key );
94+
95+
$field = new \WPUM_Field( $field_id );
96+
97+
$this->assertNotEmpty( $field->get_ID(), 'WPUM_Field should be instantiated from DB.' );
98+
99+
return $field;
100+
}
101+
102+
/**
103+
* Test that a taxonomy field with a 'wpum_' prefixed meta key retrieves
104+
* its value from get_user_meta() (not carbon_get_user_meta).
105+
*
106+
* This is the core scenario that PR #392 fixes: taxonomy field data was
107+
* not displayed because carbon_get_user_meta() returned empty.
108+
*/
109+
public function test_taxonomy_field_with_wpum_prefix_uses_get_user_meta() {
110+
$meta_key = 'wpum_test_skill';
111+
$field = $this->create_field( 'taxonomy', 'Test Taxonomy Field', $meta_key );
112+
113+
// Confirm the field is of type 'taxonomy'.
114+
$this->assertSame( 'taxonomy', $field->get_type(), 'Field type should be taxonomy.' );
115+
116+
// Confirm the meta key starts with 'wpum_'.
117+
$this->assertStringStartsWith( 'wpum_', $field->get_meta( 'user_meta_key' ), 'Meta key should start with wpum_.' );
118+
119+
// Store taxonomy term IDs in regular user meta (this is how WP stores it).
120+
$term_ids = array( 1, 2, 3 );
121+
update_user_meta( $this->user_id, $meta_key, $term_ids );
122+
123+
// Call set_user_meta — with the fix, this should go through
124+
// get_user_meta() and find our stored value.
125+
$field->set_user_meta( $this->user_id );
126+
127+
$value = $field->get_value();
128+
129+
$this->assertNotNull( $value, 'Taxonomy field value should not be null after set_user_meta().' );
130+
$this->assertNotEmpty( $value, 'Taxonomy field value should not be empty — it should be retrieved via get_user_meta().' );
131+
}
132+
133+
/**
134+
* Test that a taxonomy field correctly retrieves a single term ID stored
135+
* as a scalar value in user meta.
136+
*/
137+
public function test_taxonomy_field_retrieves_single_term_id() {
138+
$meta_key = 'wpum_single_taxonomy';
139+
$field = $this->create_field( 'taxonomy', 'Single Taxonomy Field', $meta_key );
140+
141+
// Store a single term ID.
142+
$term_id = 42;
143+
update_user_meta( $this->user_id, $meta_key, $term_id );
144+
145+
$field->set_user_meta( $this->user_id );
146+
147+
$value = $field->get_value();
148+
149+
$this->assertNotNull( $value, 'Single term ID should be retrieved.' );
150+
$this->assertNotEmpty( $value, 'Taxonomy field should retrieve a single term ID from user meta.' );
151+
}
152+
153+
/**
154+
* Test that a taxonomy field with a non-wpum prefix also retrieves
155+
* its value via get_user_meta() (the else branch).
156+
*/
157+
public function test_taxonomy_field_with_non_wpum_prefix_uses_get_user_meta() {
158+
$meta_key = 'custom_taxonomy_field';
159+
$field = $this->create_field( 'taxonomy', 'Custom Prefix Taxonomy', $meta_key );
160+
161+
$this->assertSame( 'taxonomy', $field->get_type() );
162+
163+
$term_ids = array( 10, 20 );
164+
update_user_meta( $this->user_id, $meta_key, $term_ids );
165+
166+
$field->set_user_meta( $this->user_id );
167+
168+
$value = $field->get_value();
169+
170+
$this->assertNotNull( $value, 'Non-wpum-prefix taxonomy field should retrieve value from user meta.' );
171+
$this->assertNotEmpty( $value, 'Non-wpum-prefix taxonomy field should not be empty.' );
172+
}
173+
174+
/**
175+
* Test that a non-taxonomy field with a 'wpum_' prefix does NOT retrieve
176+
* its value from get_user_meta() — it should go through the Carbon Fields
177+
* path (carbon_get_user_meta).
178+
*
179+
* We store a value in regular user meta and verify that the text field
180+
* does NOT see it (because it takes the carbon_get_user_meta branch,
181+
* which stores/retrieves data differently).
182+
*/
183+
public function test_non_taxonomy_field_with_wpum_prefix_uses_carbon_path() {
184+
$meta_key = 'wpum_custom_text';
185+
$field = $this->create_field( 'text', 'Custom Text Field', $meta_key );
186+
187+
$this->assertSame( 'text', $field->get_type(), 'Field type should be text.' );
188+
$this->assertStringStartsWith( 'wpum_', $field->get_meta( 'user_meta_key' ) );
189+
190+
// Store a value in regular user meta.
191+
update_user_meta( $this->user_id, $meta_key, 'plain_meta_value' );
192+
193+
$field->set_user_meta( $this->user_id );
194+
195+
$value = $field->get_value();
196+
197+
// A text field with wpum_ prefix should NOT get the plain user meta
198+
// value, because it goes through carbon_get_user_meta which uses a
199+
// different storage format (prefixed with _wpum_custom_text).
200+
// The value should be null/empty because Carbon Fields has no data
201+
// stored for this field.
202+
$this->assertTrue(
203+
empty( $value ) || $value !== 'plain_meta_value',
204+
'Non-taxonomy field with wpum_ prefix should use carbon_get_user_meta, not get_user_meta.'
205+
);
206+
}
207+
208+
/**
209+
* Test that a non-taxonomy field without a 'wpum_' prefix uses regular
210+
* get_user_meta() and retrieves the value correctly.
211+
*/
212+
public function test_non_taxonomy_field_without_wpum_prefix_uses_get_user_meta() {
213+
$meta_key = 'custom_text_field';
214+
$field = $this->create_field( 'text', 'Plain Meta Text Field', $meta_key );
215+
216+
$this->assertSame( 'text', $field->get_type() );
217+
218+
update_user_meta( $this->user_id, $meta_key, 'hello world' );
219+
220+
$field->set_user_meta( $this->user_id );
221+
222+
$value = $field->get_value();
223+
224+
$this->assertNotEmpty( $value, 'Text field without wpum_ prefix should retrieve value from get_user_meta.' );
225+
}
226+
227+
/**
228+
* Test that set_user_meta() handles an invalid user ID gracefully.
229+
*/
230+
public function test_set_user_meta_with_invalid_user_id() {
231+
$field = $this->create_field( 'taxonomy', 'Invalid User Taxonomy', 'wpum_invalid_tax' );
232+
233+
// Passing 0 should return early.
234+
$field->set_user_meta( 0 );
235+
236+
$this->assertNull( $field->get_value(), 'Value should remain null for invalid user ID.' );
237+
}
238+
239+
/**
240+
* Test that a taxonomy field with empty user meta returns null/empty.
241+
*/
242+
public function test_taxonomy_field_with_no_user_meta_returns_empty() {
243+
$field = $this->create_field( 'taxonomy', 'Empty Taxonomy', 'wpum_empty_tax' );
244+
245+
// Do NOT store any user meta for this key.
246+
$field->set_user_meta( $this->user_id );
247+
248+
$value = $field->get_value();
249+
250+
$this->assertEmpty( $value, 'Taxonomy field with no stored meta should return empty.' );
251+
}
252+
253+
/**
254+
* Test that the branching condition in set_user_meta() correctly
255+
* distinguishes taxonomy from non-taxonomy when both have 'wpum_' prefix.
256+
*
257+
* This is a combined test to validate the exact condition:
258+
* strpos(meta_key, 'wpum_') === 0 && get_type() !== 'taxonomy'
259+
*/
260+
public function test_branching_condition_taxonomy_vs_non_taxonomy() {
261+
$meta_key = 'wpum_shared_key';
262+
263+
// Create taxonomy field.
264+
$tax_field = $this->create_field( 'taxonomy', 'Tax Branching Field', $meta_key );
265+
266+
// Create text field with the same meta key pattern.
267+
$text_field = $this->create_field( 'text', 'Text Branching Field', $meta_key );
268+
269+
// Store data in regular user meta.
270+
update_user_meta( $this->user_id, $meta_key, 'stored_value' );
271+
272+
// Taxonomy field should retrieve the value (goes through get_user_meta).
273+
$tax_field->set_user_meta( $this->user_id );
274+
$tax_value = $tax_field->get_value();
275+
276+
// Text field should NOT retrieve the plain meta value (goes through carbon_get_user_meta).
277+
$text_field->set_user_meta( $this->user_id );
278+
$text_value = $text_field->get_value();
279+
280+
$this->assertNotEmpty(
281+
$tax_value,
282+
'Taxonomy field should retrieve value from get_user_meta (PR #392 fix).'
283+
);
284+
285+
// The text field either gets nothing (Carbon returns empty) or gets
286+
// a different value. Either way, the paths are different.
287+
$this->assertTrue(
288+
empty( $text_value ) || $text_value !== $tax_value,
289+
'Text field with same wpum_ prefix key should take the Carbon Fields path, not get_user_meta.'
290+
);
291+
}
292+
}

0 commit comments

Comments
 (0)