2016-01-19 04:55:19 +01:00
|
|
|
<?php
|
|
|
|
|
2017-06-30 06:35:39 +02:00
|
|
|
/**
|
|
|
|
* Unit test factory for posts.
|
|
|
|
*
|
|
|
|
* Note: The below @method notations are defined solely for the benefit of IDEs,
|
|
|
|
* as a way to indicate expected return values from the given factory methods.
|
|
|
|
*
|
|
|
|
* @method int create( $args = array(), $generation_definitions = null )
|
|
|
|
* @method WP_Post create_and_get( $args = array(), $generation_definitions = null )
|
|
|
|
* @method int[] create_many( $count, $args = array(), $generation_definitions = null )
|
|
|
|
*/
|
2016-01-19 04:55:19 +01:00
|
|
|
class WP_UnitTest_Factory_For_Post extends WP_UnitTest_Factory_For_Thing {
|
|
|
|
|
2019-03-15 13:15:08 +01:00
|
|
|
public function __construct( $factory = null ) {
|
2016-01-19 04:55:19 +01:00
|
|
|
parent::__construct( $factory );
|
|
|
|
$this->default_generation_definitions = array(
|
2017-12-01 00:09:33 +01:00
|
|
|
'post_status' => 'publish',
|
|
|
|
'post_title' => new WP_UnitTest_Generator_Sequence( 'Post title %s' ),
|
2016-01-19 04:55:19 +01:00
|
|
|
'post_content' => new WP_UnitTest_Generator_Sequence( 'Post content %s' ),
|
|
|
|
'post_excerpt' => new WP_UnitTest_Generator_Sequence( 'Post excerpt %s' ),
|
2017-12-01 00:09:33 +01:00
|
|
|
'post_type' => 'post',
|
2016-01-19 04:55:19 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-09 06:43:14 +01:00
|
|
|
/**
|
|
|
|
* Creates a post object.
|
|
|
|
*
|
|
|
|
* @param array $args Array with elements for the post.
|
|
|
|
*
|
|
|
|
* @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure.
|
|
|
|
*/
|
2019-03-15 13:15:08 +01:00
|
|
|
public function create_object( $args ) {
|
2016-01-19 04:55:19 +01:00
|
|
|
return wp_insert_post( $args );
|
|
|
|
}
|
|
|
|
|
2019-01-09 06:43:14 +01:00
|
|
|
/**
|
|
|
|
* Updates an existing post object.
|
|
|
|
*
|
2019-12-18 01:17:54 +01:00
|
|
|
* @param int $post_id ID of the post to update.
|
2019-01-09 06:43:14 +01:00
|
|
|
* @param array $fields Post data.
|
|
|
|
*
|
2019-12-18 01:17:54 +01:00
|
|
|
* @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure.
|
2019-01-09 06:43:14 +01:00
|
|
|
*/
|
2019-03-15 13:15:08 +01:00
|
|
|
public function update_object( $post_id, $fields ) {
|
2016-01-19 04:55:19 +01:00
|
|
|
$fields['ID'] = $post_id;
|
|
|
|
return wp_update_post( $fields );
|
|
|
|
}
|
|
|
|
|
2019-01-09 06:43:14 +01:00
|
|
|
/**
|
2019-12-18 01:17:54 +01:00
|
|
|
* Retrieves a post by a given ID.
|
2019-01-09 06:43:14 +01:00
|
|
|
*
|
2019-12-18 01:17:54 +01:00
|
|
|
* @param int $post_id ID of the post to retrieve.
|
2019-01-09 06:43:14 +01:00
|
|
|
*
|
2019-12-18 01:17:54 +01:00
|
|
|
* @return WP_Post|null WP_Post object on success, null on failure.
|
2019-01-09 06:43:14 +01:00
|
|
|
*/
|
2019-03-15 13:15:08 +01:00
|
|
|
public function get_object_by_id( $post_id ) {
|
2016-01-19 04:55:19 +01:00
|
|
|
return get_post( $post_id );
|
|
|
|
}
|
|
|
|
}
|