wordpress-develop/tests/phpunit/tests/date/xmlrpc.php
Sergey Biryukov 85106c694d Code Modernization: Fix null to non-nullable deprecations in wp_xmlrpc_server::_insert_post().
The `wp_xmlrpc_server::_insert_post()` method creates a new post via `wp_insert_post()` or updates an existing one via `wp_update_post()`, which subsequently calls `wp_insert_post()`. However, the default/fallback values used in the function were not in line with the default/fallback values used in the `wp_insert_post()` function.

The `wp_insert_post()` function does a `wp_parse_args()` (array merge) of the received arguments with the defaults. If any of the received arguments are `null`, this would overwrite the default value, as seen in [https://3v4l.org/bfVlv array_merge() example], and lead to "passing null to non-nullable" deprecation notices on PHP 8.1 for certain arguments.

Unfortunately, the conditional logic within the `wp_xmlrpc_server::_insert_post()` function itself often uses an `isset()` to trigger certain code blocks, so syncing the defaults with those used in the `wp_insert_post()` function was not an option.

This commit:
* Updates the default/fallback values in the `$defaults` array only for those values where this would not lead to a change in the behavior of the function.
* Adds a safeguard function, filtering out all remaining `null` values from the `$post_data` array before it is passed on to the `wp_insert_post()` or `wp_update_post()` functions. Removing those values is safe as this means that these array keys will now:
 * either be set to the default/fallback value as defined in `wp_insert_post()`.
 * or not be set and for those values which don't have a default/fallback value in `wp_insert_post()`, the function does an `! empty()` or `isset()` check anyway and those array keys not being defined means that the result of those checks will remain the same.

Includes
* Removing a couple of conditions which are now redundant.
* Removing an `expectDeprecation()` in the `Tests_Date_XMLRPC` test class, which is now no longer needed.

Fixes various errors along the lines of:
{{{
36) Tests_XMLRPC_wp_newPost::test_no_content
json_decode(): Passing null to parameter #1 ($json) of type string is deprecated

/var/www/src/wp-includes/kses.php:2074
/var/www/src/wp-includes/class-wp-hook.php:307
/var/www/src/wp-includes/plugin.php:205
/var/www/src/wp-includes/post.php:2835
/var/www/src/wp-includes/post.php:2720
/var/www/src/wp-includes/post.php:4066
/var/www/src/wp-includes/class-wp-xmlrpc-server.php:1683
/var/www/src/wp-includes/class-wp-xmlrpc-server.php:1347
/var/www/tests/phpunit/tests/xmlrpc/wp/newPost.php:25
/var/www/vendor/bin/phpunit:123
}}}

Follow-up to [1563], [4793], [7900], [16824], [19848], [19873], [20632], [40677], [51968], [54320].

Props jrf.
See #55656.

git-svn-id: https://develop.svn.wordpress.org/trunk@54321 602fd350-edb4-49c9-b593-d223f7449a82
2022-09-27 02:16:28 +00:00

258 lines
5.5 KiB
PHP

<?php
/**
* @group date
* @group datetime
* @group xmlrpc
* @covers IXR_Date
*/
class Tests_Date_XMLRPC extends WP_XMLRPC_UnitTestCase {
/**
* Cleans up.
*/
public function tear_down() {
// Reset the timezone option to the default value.
update_option( 'timezone_string', '' );
parent::tear_down();
}
/**
* @ticket 30429
*
* @covers wp_xmlrpc_server::mw_newPost
*/
public function test_date_new_post() {
$timezone = 'Europe/Helsinki';
update_option( 'timezone_string', $timezone );
$datetime = new DateTimeImmutable( 'now', new DateTimeZone( $timezone ) );
$datetimeutc = $datetime->setTimezone( new DateTimeZone( 'UTC' ) );
$this->make_user_by_role( 'editor' );
$post = get_post(
$this->myxmlrpcserver->mw_newPost(
array(
1,
'editor',
'editor',
array(
'title' => 'test',
'post_content' => 'test',
'dateCreated' => new IXR_Date( $datetimeutc->format( 'Ymd\TH:i:s\Z' ) ),
),
)
)
);
$this->assertSame(
$datetime->format( 'Y-m-d H:i:s' ),
$post->post_date,
'UTC time with explicit time zone into mw_newPost'
);
$post = get_post(
$this->myxmlrpcserver->mw_newPost(
array(
1,
'editor',
'editor',
array(
'title' => 'test',
'post_content' => 'test',
'dateCreated' => new IXR_Date( $datetime->format( 'Ymd\TH:i:s' ) ),
),
)
)
);
$this->assertSame(
$datetime->format( 'Y-m-d H:i:s' ),
$post->post_date,
'Local time w/o time zone into mw_newPost'
);
$post = get_post(
$this->myxmlrpcserver->mw_newPost(
array(
1,
'editor',
'editor',
array(
'title' => 'test',
'post_content' => 'test',
'date_created_gmt' => new IXR_Date( $datetimeutc->format( 'Ymd\TH:i:s' ) ),
),
)
)
);
$this->assertSame(
$datetime->format( 'Y-m-d H:i:s' ),
$post->post_date,
'UTC time into mw_newPost'
);
$post = get_post(
$this->myxmlrpcserver->wp_newPost(
array(
1,
'editor',
'editor',
array(
'title' => 'test',
'post_content' => 'test',
'post_date' => $datetime->format( 'Ymd\TH:i:s' ),
),
)
)
);
$this->assertSame(
$datetime->format( 'Y-m-d H:i:s' ),
$post->post_date,
'Local time into wp_newPost'
);
$post = get_post(
$this->myxmlrpcserver->wp_newPost(
array(
1,
'editor',
'editor',
array(
'title' => 'test',
'post_content' => 'test',
'post_date_gmt' => $datetimeutc->format( 'Ymd\TH:i:s' ),
),
)
)
);
$this->assertSame(
$datetime->format( 'Y-m-d H:i:s' ),
$post->post_date,
'UTC time into wp_newPost'
);
}
/**
* @ticket 30429
*
* @covers wp_xmlrpc_server::mw_editPost
*/
public function test_date_edit_post() {
$timezone = 'Europe/Helsinki';
update_option( 'timezone_string', $timezone );
$datetime = new DateTimeImmutable( 'now', new DateTimeZone( $timezone ) );
$datetimeutc = $datetime->setTimezone( new DateTimeZone( 'UTC' ) );
$editor_id = $this->make_user_by_role( 'editor' );
$post_id = self::factory()->post->create(
array(
'post_author' => $editor_id,
'post_date' => $datetime->modify( '-1 hour' )->format( 'Y-m-d H:i:s' ),
)
);
$result = $this->myxmlrpcserver->mw_editPost(
array(
$post_id,
'editor',
'editor',
array(
'dateCreated' => new IXR_Date( $datetime->format( 'Ymd\TH:i:s' ) ),
),
)
);
$fetched_post = get_post( $post_id );
$this->assertTrue( $result );
$this->assertSame(
$datetime->format( 'Y-m-d H:i:s' ),
$fetched_post->post_date,
'Local time into mw_editPost'
);
$post_id = self::factory()->post->create(
array(
'post_author' => $editor_id,
'post_date' => $datetime->modify( '-1 hour' )->format( 'Y-m-d H:i:s' ),
)
);
$result = $this->myxmlrpcserver->mw_editPost(
array(
$post_id,
'editor',
'editor',
array(
'date_created_gmt' => new IXR_Date( $datetimeutc->format( 'Ymd\TH:i:s' ) ),
),
)
);
$fetched_post = get_post( $post_id );
$this->assertTrue( $result );
$this->assertSame(
$datetime->format( 'Y-m-d H:i:s' ),
$fetched_post->post_date,
'UTC time into mw_editPost'
);
}
/**
* @ticket 30429
*
* @covers wp_xmlrpc_server::wp_editComment
*/
public function test_date_edit_comment() {
$timezone = 'Europe/Helsinki';
update_option( 'timezone_string', $timezone );
$datetime = new DateTimeImmutable( 'now', new DateTimeZone( $timezone ) );
$datetime = $datetime->modify( '-1 hour' );
$datetimeutc = $datetime->setTimezone( new DateTimeZone( 'UTC' ) );
$this->make_user_by_role( 'administrator' );
$post_id = self::factory()->post->create();
$comment_data = array(
'comment_post_ID' => $post_id,
'comment_author' => 'Test commenter',
'comment_author_url' => 'http://example.com/',
'comment_author_email' => 'example@example.com',
'comment_content' => 'Hello, world!',
'comment_approved' => '1',
);
$comment_id = wp_insert_comment( $comment_data );
$result = $this->myxmlrpcserver->wp_editComment(
array(
1,
'administrator',
'administrator',
$comment_id,
array(
'date_created_gmt' => new IXR_Date( $datetimeutc->format( 'Ymd\TH:i:s' ) ),
),
)
);
$fetched_comment = get_comment( $comment_id );
$this->assertTrue( $result );
$this->assertSame(
$datetime->format( 'Y-m-d H:i:s' ),
$fetched_comment->comment_date,
'UTC time into wp_editComment'
);
}
}