Skip to content

Commit

Permalink
Test that a revision with an array in meta saves correctly even when …
Browse files Browse the repository at this point in the history
…included in `_wp_post_revision_fields`.
  • Loading branch information
adamsilverstein committed Nov 17, 2023
1 parent 7c492d3 commit ba94b81
Showing 1 changed file with 83 additions and 1 deletion.
84 changes: 83 additions & 1 deletion tests/phpunit/tests/post/metaRevisions.php
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ public function test_revisionining_of_meta_with_default_value() {
$revisions = wp_get_post_revisions( $post_id );
$last_revision = array_shift( $revisions );

// No ,eta data should be stored in the revision.
// No meta data should be stored in the revision.
$this->assertEquals( array(), get_post_meta( $last_revision->ID ) );

// Set the test meta again.
Expand Down Expand Up @@ -719,4 +719,86 @@ public function page_post_type_data_provider() {
),
);
}

/**
* Test that a revision with an array in meta saves correctly even when included in `_wp_post_revision_fields`.
*
* @ticket 59827
*/
public function test_wp_save_post_revision_with_array_meta() {

// Add a meta field to revision that includes a default value.
register_post_meta(
'post',
'meta_revision_test',
array(
'single' => true,
'type' => 'array',
'revisions_enabled' => true,
)
);

// Add the meta value to `_wp_post_revision_fields` which triggers it being compared for save consideration.
add_filter( '_wp_post_revision_fields', function( $fields ) {

Check failure on line 742 in tests/phpunit/tests/post/metaRevisions.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Opening parenthesis of a multi-line function call must be the last content on the line

Check failure on line 742 in tests/phpunit/tests/post/metaRevisions.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Only one argument is allowed per line in a multi-line function call

Check failure on line 742 in tests/phpunit/tests/post/metaRevisions.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 space after FUNCTION keyword; 0 found
$fields[ 'meta_revision_test' ] = 'Meta Revisions Test';

Check failure on line 743 in tests/phpunit/tests/post/metaRevisions.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Array keys must NOT be surrounded by spaces if they only contain a string or an integer.
return $fields;
} );

Check failure on line 745 in tests/phpunit/tests/post/metaRevisions.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Closing parenthesis of a multi-line function call must be on a line by itself

// Set up a new post.
$post_id = $this->factory->post->create(
array(
'post_content' => 'initial content',
'meta_input' => array(
'meta_revision_test' => 'foo',
),
)
);

// Set the test meta to an array.
update_post_meta( $post_id, 'meta_revision_test', array( 'test' ) );

// Update to save.
wp_update_post( array( 'ID' => $post_id ) );

// Check that the meta is stored correctly.
$stored_data = get_post_meta( $post_id, 'meta_revision_test', true );
$this->assertEquals( array( 'test' ) , $stored_data );

Check failure on line 765 in tests/phpunit/tests/post/metaRevisions.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Space found before comma in argument list

// Also verify that the latest revision has stored the meta.
$revisions = wp_get_post_revisions( $post_id );
$last_revision = array_shift( $revisions );
$stored_data = get_post_meta( $last_revision->ID, 'meta_revision_test', true );
$this->assertEquals( array( 'test' ) , $stored_data );

Check failure on line 771 in tests/phpunit/tests/post/metaRevisions.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Space found before comma in argument list

// Update the meta.
update_post_meta( $post_id, 'meta_revision_test', array( 'changed' ) );

// Update to save.
wp_update_post(
array(
'ID' => $post_id,
'post_content' => 'content update 1',
)
);

// Check that the correct meta value is returned.
$this->assertEquals( array( 'changed' ), get_post_meta( $post_id, 'meta_revision_test', true ) );

// Also verify that the latest revision has the correct meta value.
$revisions = wp_get_post_revisions( $post_id );
$last_revision = array_shift( $revisions );

// Set the test meta again.
update_post_meta( $post_id, 'meta_revision_test', 'test' );

// Update to save.
wp_update_post( array( 'ID' => $post_id ) );

// Now restore the previous revision.
wp_restore_post_revision( $last_revision->ID );

// Verify the correct meta value is still returned.
$this->assertEquals( array( 'changed' ), get_post_meta( $post_id, 'meta_revision_test', true ) );

}

Check failure on line 803 in tests/phpunit/tests/post/metaRevisions.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Function closing brace must go on the next line following the body; found 1 blank lines before brace
}

0 comments on commit ba94b81

Please sign in to comment.