Skip to content

Commit 9a11cc5

Browse files
committed
Adjust the tests to cover all meta types.
1 parent 54f7d51 commit 9a11cc5

File tree

1 file changed

+115
-54
lines changed

1 file changed

+115
-54
lines changed

tests/phpunit/tests/meta/bulkAddMetadata.php

Lines changed: 115 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -22,114 +22,135 @@ class Tests_Meta_BulkAddMetadata extends WP_UnitTestCase {
2222

2323
/**
2424
* @ticket 59269
25+
* @dataProvider data_meta_types
2526
*/
26-
public function test_all_meta_fields_should_be_added() {
27+
public function test_all_meta_fields_should_be_added( string $meta_type ) {
2728
global $wpdb;
2829

29-
$post_id = self::factory()->post->create();
30-
$meta = array(
30+
// Create the object.
31+
$object_id = self::factory()->{$meta_type}->create();
32+
33+
// Prepare the meta values to add in bulk.
34+
$meta = array(
3135
'key1' => '1',
3236
'key2' => '2',
3337
'key3' => '3',
3438
);
3539

36-
$latest_mid = (int) $wpdb->get_var( "SELECT MAX( meta_id ) FROM {$wpdb->postmeta}" );
40+
// Track the mid before adding new meta.
41+
$next_mid = self::get_autoincrement( $meta_type );
3742

43+
// Set up mock actions and filters to track calls.
3844
$action1 = new MockAction();
3945
$action2 = new MockAction();
4046
$action3 = new MockAction();
47+
add_filter( "add_{$meta_type}_metadata", array( $action1, 'filter' ), 10, 5 );
48+
add_action( "add_{$meta_type}_meta", array( $action2, 'action' ) );
49+
add_action( "added_{$meta_type}_meta", array( $action3, 'action' ) );
4150

42-
add_filter( 'add_post_metadata', array( $action1, 'filter' ), 10, 5 );
43-
add_action( 'add_post_meta', array( $action2, 'action' ) );
44-
add_action( 'added_post_meta', array( $action3, 'action' ) );
45-
46-
$result = bulk_add_metadata( 'post', $post_id, $meta );
51+
// Bulk add the meta.
52+
$result = bulk_add_metadata( $meta_type, $object_id, $meta );
4753

54+
// Read back the actual meta values.
4855
$actual_vals = array(
49-
'key1' => get_post_meta( $post_id, 'key1', true ),
50-
'key2' => get_post_meta( $post_id, 'key2', true ),
51-
'key3' => get_post_meta( $post_id, 'key3', true ),
56+
'key1' => get_metadata( $meta_type, $object_id, 'key1', true ),
57+
'key2' => get_metadata( $meta_type, $object_id, 'key2', true ),
58+
'key3' => get_metadata( $meta_type, $object_id, 'key3', true ),
5259
);
5360

54-
$expected_vals = $meta;
61+
// Prepare expected meta IDs.
5562
$expected_mids = array(
56-
'key1' => ( $latest_mid + 1 ),
57-
'key2' => ( $latest_mid + 2 ),
58-
'key3' => ( $latest_mid + 3 ),
63+
'key1' => ( $next_mid ),
64+
'key2' => ( $next_mid + 1 ),
65+
'key3' => ( $next_mid + 2 ),
5966
);
6067

61-
$this->assertSame( $expected_vals, $actual_vals );
62-
$this->assertSame( $expected_mids, $result );
63-
$this->assertSame( 3, $action1->get_call_count() );
64-
$this->assertSame( 3, $action2->get_call_count() );
65-
$this->assertSame( 3, $action3->get_call_count() );
68+
$this->assertSame( $meta, $actual_vals, 'Actual meta values should match expected values.' );
69+
$this->assertSame( $expected_mids, $result, 'Actual meta IDs should match expected meta IDs.' );
70+
$this->assertSame( 3, $action1->get_call_count(), "'add_{$meta_type}_metadata' filter should be called the correct number of times." );
71+
$this->assertSame( 3, $action2->get_call_count(), "'add_{$meta_type}_meta' action should be called the correct number of times." );
72+
$this->assertSame( 3, $action3->get_call_count(), "'added_{$meta_type}_meta' action should be called the correct number of times." );
6673
}
6774

6875
/**
6976
* @ticket 59269
77+
* @dataProvider data_meta_types
7078
*/
71-
public function test_correct_mids_should_be_returned_when_filter_is_in_place() {
79+
public function test_correct_mids_should_be_returned_when_filter_is_in_place( string $meta_type ) {
7280
global $wpdb;
7381

82+
// Set up a filter to modify the mid for 'key2'.
7483
add_filter(
75-
'add_post_metadata',
84+
"add_{$meta_type}_metadata",
7685
static function ( $check, $object_id, $meta_key ) {
7786
return ( 'key2' === $meta_key ) ? 123456 : $check;
7887
},
7988
10,
8089
3
8190
);
8291

83-
$post_id = self::factory()->post->create();
84-
$meta = array(
92+
// Create the object.
93+
$object_id = self::factory()->{$meta_type}->create();
94+
95+
// Prepare the meta values to add in bulk.
96+
$meta = array(
8597
'key1' => '1',
8698
'key2' => '2',
8799
'key3' => '3',
88100
);
89101

90-
$latest_mid = (int) $wpdb->get_var( "SELECT MAX( meta_id ) FROM {$wpdb->postmeta}" );
102+
// Track the mid before adding new meta.
103+
$next_mid = self::get_autoincrement( $meta_type );
91104

105+
// Set up mock actions and filters to track calls.
92106
$action1 = new MockAction();
93107
$action2 = new MockAction();
94108
$action3 = new MockAction();
109+
add_filter( "add_{$meta_type}_metadata", array( $action1, 'filter' ), 10, 5 );
110+
add_action( "add_{$meta_type}_meta", array( $action2, 'action' ) );
111+
add_action( "added_{$meta_type}_meta", array( $action3, 'action' ) );
95112

96-
add_filter( 'add_post_metadata', array( $action1, 'filter' ), 10, 5 );
97-
add_action( 'add_post_meta', array( $action2, 'action' ) );
98-
add_action( 'added_post_meta', array( $action3, 'action' ) );
99-
100-
$result = bulk_add_metadata( 'post', $post_id, $meta );
113+
// Bulk add the meta.
114+
$result = bulk_add_metadata( $meta_type, $object_id, $meta );
101115

116+
// Prepare expected meta values.
102117
$expected_vals = array(
103118
'key1' => '1',
104119
'key2' => '',
105120
'key3' => '3',
106121
);
107122

123+
// Read back the actual meta values.
108124
$actual_vals = array(
109-
'key1' => get_post_meta( $post_id, 'key1', true ),
110-
'key2' => get_post_meta( $post_id, 'key2', true ),
111-
'key3' => get_post_meta( $post_id, 'key3', true ),
125+
'key1' => get_metadata( $meta_type, $object_id, 'key1', true ),
126+
'key2' => get_metadata( $meta_type, $object_id, 'key2', true ),
127+
'key3' => get_metadata( $meta_type, $object_id, 'key3', true ),
112128
);
113129

130+
// Prepare expected meta IDs.
114131
$expected_mids = array(
115132
'key2' => 123456,
116-
'key1' => ( $latest_mid + 1 ),
117-
'key3' => ( $latest_mid + 2 ),
133+
'key1' => ( $next_mid ),
134+
'key3' => ( $next_mid + 1 ),
118135
);
119136

120-
$this->assertSame( $expected_vals, $actual_vals );
121-
$this->assertSame( $expected_mids, $result );
122-
$this->assertSame( 3, $action1->get_call_count() );
123-
$this->assertSame( 2, $action2->get_call_count() );
124-
$this->assertSame( 2, $action3->get_call_count() );
137+
$this->assertSame( $expected_vals, $actual_vals, 'Actual meta values should match expected values.' );
138+
$this->assertSame( $expected_mids, $result , 'Actual meta IDs should match expected meta IDs.' );
139+
$this->assertSame( 3, $action1->get_call_count(), "'add_{$meta_type}_metadata' filter should be called the correct number of times." );
140+
$this->assertSame( 2, $action2->get_call_count(), "'add_{$meta_type}_meta' action should be called the correct number of times." );
141+
$this->assertSame( 2, $action3->get_call_count(), "'added_{$meta_type}_meta' action should be called the correct number of times." );
125142
}
126143

127144
/**
128145
* @ticket 59269
146+
* @dataProvider data_meta_types
129147
*/
130-
public function test_slashed_data_should_be_handled_correctly() {
131-
$post_id = self::factory()->post->create();
132-
$meta = array(
148+
public function test_slashed_data_should_be_handled_correctly( string $meta_type ) {
149+
// Create the object.
150+
$object_id = self::factory()->{$meta_type}->create();
151+
152+
// Prepare the meta values to add in bulk.
153+
$meta = array(
133154
'key1' => addslashes( self::SLASH_1 ),
134155
'key2' => addslashes( self::SLASH_2 ),
135156
'key3' => addslashes( self::SLASH_3 ),
@@ -139,18 +160,21 @@ public function test_slashed_data_should_be_handled_correctly() {
139160
'key7' => addslashes( self::SLASH_7 ),
140161
);
141162

142-
$result = bulk_add_metadata( 'post', $post_id, $meta );
163+
// Bulk add the meta.
164+
$result = bulk_add_metadata( $meta_type, $object_id, $meta );
143165

166+
// Read back the actual meta values.
144167
$actual_vals = array(
145-
'key1' => get_post_meta( $post_id, 'key1', true ),
146-
'key2' => get_post_meta( $post_id, 'key2', true ),
147-
'key3' => get_post_meta( $post_id, 'key3', true ),
148-
'key4' => get_post_meta( $post_id, 'key4', true ),
149-
'key5' => get_post_meta( $post_id, 'key5', true ),
150-
'key6' => get_post_meta( $post_id, 'key6', true ),
151-
'key7' => get_post_meta( $post_id, 'key7', true ),
168+
'key1' => get_metadata( $meta_type, $object_id, 'key1', true ),
169+
'key2' => get_metadata( $meta_type, $object_id, 'key2', true ),
170+
'key3' => get_metadata( $meta_type, $object_id, 'key3', true ),
171+
'key4' => get_metadata( $meta_type, $object_id, 'key4', true ),
172+
'key5' => get_metadata( $meta_type, $object_id, 'key5', true ),
173+
'key6' => get_metadata( $meta_type, $object_id, 'key6', true ),
174+
'key7' => get_metadata( $meta_type, $object_id, 'key7', true ),
152175
);
153176

177+
// Prepare expected meta values.
154178
$expected_vals = array(
155179
'key1' => self::SLASH_1,
156180
'key2' => self::SLASH_2,
@@ -161,7 +185,44 @@ public function test_slashed_data_should_be_handled_correctly() {
161185
'key7' => self::SLASH_7,
162186
);
163187

164-
$this->assertCount( 7, $result );
165-
$this->assertSame( $expected_vals, $actual_vals );
188+
$this->assertCount( 7, $result, 'The correct number of meta IDs should be returned.' );
189+
$this->assertSame( $expected_vals, $actual_vals, 'Actual meta values should match expected values.' );
190+
}
191+
192+
/**
193+
* @return array<string,array<string>>
194+
*/
195+
protected function data_meta_types(): array {
196+
$types = [
197+
'post' => [ 'post' ],
198+
'user' => [ 'user' ],
199+
'comment' => [ 'comment' ],
200+
'term' => [ 'term' ],
201+
];
202+
203+
if ( is_multisite() ) {
204+
$types['blog'] = [ 'blog' ];
205+
}
206+
207+
return $types;
208+
}
209+
210+
private static function get_autoincrement( string $meta_type ): int {
211+
global $wpdb;
212+
213+
$table = "{$meta_type}meta";
214+
$sql = "
215+
SELECT AUTO_INCREMENT
216+
FROM INFORMATION_SCHEMA.TABLES
217+
WHERE TABLE_SCHEMA = DATABASE()
218+
AND TABLE_NAME = %s
219+
";
220+
221+
return (int) $wpdb->get_var(
222+
$wpdb->prepare(
223+
$sql,
224+
$wpdb->$table
225+
)
226+
);
166227
}
167228
}

0 commit comments

Comments
 (0)