Skip to content

Commit b289a3e

Browse files
committed
"check_meta_values" function test added.
1 parent 2c8c48a commit b289a3e

File tree

2 files changed

+137
-3
lines changed

2 files changed

+137
-3
lines changed

connectors/class-connector-acf.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ public function callback_delete_user_meta() {
418418
*/
419419
public function check_meta( $type, $action, $meta_id, $object_id, $meta_key, $meta_value = null ) {
420420
$post = get_post( $object_id );
421-
if ( 'post' !== $type || ! $post || ( 'acf' !== $post->post_type && 'acf-field-group' !== $post->post_type ) ) {
421+
if ( 'post' !== $type || ! $post || ! in_array( $post->post_type, array( 'acf', 'acf-field-group' ), true ) ) {
422422
$this->check_meta_values( $type, $action, $meta_id, $object_id, $meta_key, $meta_value );
423423
return;
424424
}

tests/tests/connectors/test-class-connector-acf.php

Lines changed: 136 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@
77
namespace WP_Stream;
88

99
class Test_WP_Stream_Connector_ACF extends WP_StreamTestCase {
10+
/**
11+
* Holds ACF group key used throughout test.
12+
*
13+
* @var string
14+
*/
1015
protected $group_key = 'test_group';
1116

17+
/**
18+
* Runs before each test
19+
*/
1220
public function setUp() {
1321
parent::setUp();
1422

@@ -21,11 +29,19 @@ public function setUp() {
2129
$this->mock->register();
2230
}
2331

32+
/**
33+
* Runs after each test
34+
*/
2435
public function tearDown() {
2536
parent::tearDown();
2637
}
2738

28-
public function update_acf_field_group( $config = array() ) {
39+
/**
40+
* Create/Update ACF field group
41+
*
42+
* @param array $config ACF field group configuration.
43+
*/
44+
private function update_acf_field_group( $config = array() ) {
2945
$defaults = array(
3046
'key' => $this->group_key,
3147
'title' => 'Test Group',
@@ -52,7 +68,12 @@ public function update_acf_field_group( $config = array() ) {
5268
return \acf_update_field_group( array_merge( $defaults, $config ) );
5369
}
5470

55-
public function update_acf_field( $config = array() ) {
71+
/**
72+
* Create/Update ACF field.
73+
*
74+
* @param array $config ACF field configuration.
75+
*/
76+
private function update_acf_field( $config = array() ) {
5677
$defaults = [
5778
'parent' => $this->group_key,
5879
'key' => uniqid(),
@@ -77,10 +98,16 @@ public function update_acf_field( $config = array() ) {
7798
return \acf_update_field( array_merge( $defaults, $config ) );
7899
}
79100

101+
/**
102+
* Confirm that ACF is installed and active.
103+
*/
80104
public function test_acf_installed_and_activated() {
81105
$this->assertTrue( is_callable( 'acf' ) );
82106
}
83107

108+
/**
109+
* Tests the "callback_save_post" callback.
110+
*/
84111
public function test_callback_save_post() {
85112
$this->mock->expects( $this->atLeastOnce() )
86113
->method( 'log' )
@@ -161,6 +188,9 @@ public function test_callback_save_post() {
161188
$this->assertSame( 1, did_action( 'acf/update_field_group' ) );
162189
}
163190

191+
/**
192+
* Tests the "callback_post_updated" callback.
193+
*/
164194
public function test_callback_post_updated() {
165195
// Register test ACF field group and field for later use.
166196
$field_group = $this->update_acf_field_group();
@@ -199,4 +229,108 @@ public function test_callback_post_updated() {
199229
*/
200230
$this->assertSame( 2, did_action( 'acf/update_field_group' ) );
201231
}
232+
233+
/**
234+
* Tests the "check_meta_values" function and some of the connected callbacks.
235+
*/
236+
public function test_check_meta_values() {
237+
// Register test ACF field group and field for later use.
238+
$field_group = $this->update_acf_field_group(
239+
array(
240+
'location' => array(
241+
array(
242+
array(
243+
'param' => 'post_type',
244+
'operator' => '==',
245+
'value' => 'post',
246+
),
247+
),
248+
array(
249+
array(
250+
'param' => 'current_user',
251+
'operator' => '==',
252+
'value' => 'logged_in',
253+
),
254+
),
255+
),
256+
)
257+
);
258+
$field = $this->update_acf_field();
259+
260+
// Create post for later use.
261+
$post_id = self::factory()->post->create( array( 'post_title' => 'Test post' ) );
262+
$user_id = self::factory()->user->create(
263+
array(
264+
'username' => 'testuser',
265+
'display_name' => 'testuser',
266+
)
267+
);
268+
269+
// Expected log() calls.
270+
$this->mock->expects( $this->exactly( 3 ) )
271+
->method( 'log' )
272+
->withConsecutive(
273+
array(
274+
$this->equalTo( esc_html_x( '"%1$s" of "%2$s" %3$s updated', 'acf', 'stream' ) ),
275+
$this->equalTo(
276+
array(
277+
'field_label' => $field['label'],
278+
'title' => 'Test post',
279+
'singular_name' => 'post',
280+
'meta_value' => 'Yes sir!',
281+
'meta_key' => 'test_field',
282+
'meta_type' => 'post',
283+
)
284+
),
285+
$this->equalTo( $post_id ),
286+
$this->equalTo( 'values' ),
287+
$this->equalTo( 'updated' )
288+
),
289+
array(
290+
$this->equalTo( esc_html_x( '"%1$s" of "%2$s" %3$s updated', 'acf', 'stream' ) ),
291+
$this->equalTo(
292+
array(
293+
'field_label' => $field['label'],
294+
'title' => 'Test post',
295+
'singular_name' => 'post',
296+
'meta_value' => '',
297+
'meta_key' => 'test_field',
298+
'meta_type' => 'post',
299+
)
300+
),
301+
$this->equalTo( $post_id ),
302+
$this->equalTo( 'values' ),
303+
$this->equalTo( 'updated' )
304+
),
305+
array(
306+
$this->equalTo( esc_html_x( '"%1$s" of "%2$s" %3$s updated', 'acf', 'stream' ) ),
307+
$this->equalTo(
308+
array(
309+
'field_label' => $field['label'],
310+
'title' => 'testuser',
311+
'singular_name' => 'user',
312+
'meta_value' => 'Yes sir!',
313+
'meta_key' => 'test_field',
314+
'meta_type' => 'user',
315+
)
316+
),
317+
$this->equalTo( $user_id ),
318+
$this->equalTo( 'values' ),
319+
$this->equalTo( 'updated' )
320+
)
321+
);
322+
323+
// Do stuff.
324+
update_field( 'test_field', 'Yes sir!', $post_id );
325+
update_field( 'test_field', '', $post_id );
326+
327+
\wp_set_current_user( $user_id );
328+
update_field( 'test_field', 'Yes sir!', "user_{$user_id}" );
329+
330+
/**
331+
* Test that the proper callbacks are executed.
332+
*/
333+
$this->assertFalse( 0 === did_action( 'wp_stream_test_callback_added_post_meta' ) );
334+
$this->assertFalse( 0 === did_action( 'wp_stream_test_callback_updated_post_meta' ) );
335+
}
202336
}

0 commit comments

Comments
 (0)