class-wp-unittest-factory-for-post.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * Unit test factory for posts.
  4. *
  5. * Note: The below @method notations are defined solely for the benefit of IDEs,
  6. * as a way to indicate expected return values from the given factory methods.
  7. *
  8. * @method int create( $args = array(), $generation_definitions = null )
  9. * @method WP_Post create_and_get( $args = array(), $generation_definitions = null )
  10. * @method int[] create_many( $count, $args = array(), $generation_definitions = null )
  11. */
  12. class WP_UnitTest_Factory_For_Post extends WP_UnitTest_Factory_For_Thing {
  13. public function __construct( $factory = null ) {
  14. parent::__construct( $factory );
  15. $this->default_generation_definitions = array(
  16. 'post_status' => 'publish',
  17. 'post_title' => new WP_UnitTest_Generator_Sequence( 'Post title %s' ),
  18. 'post_content' => new WP_UnitTest_Generator_Sequence( 'Post content %s' ),
  19. 'post_excerpt' => new WP_UnitTest_Generator_Sequence( 'Post excerpt %s' ),
  20. 'post_type' => 'post',
  21. );
  22. }
  23. /**
  24. * Creates a post object.
  25. *
  26. * @param array $args Array with elements for the post.
  27. *
  28. * @return int The post ID on success. The value 0 on failure.
  29. */
  30. public function create_object( $args ) {
  31. return wp_insert_post( $args );
  32. }
  33. /**
  34. * Updates an existing post object.
  35. *
  36. * @param int $post_id ID of the post to update.
  37. * @param array $fields Post data.
  38. *
  39. * @return int The post ID on success. The value 0 on failure.
  40. */
  41. public function update_object( $post_id, $fields ) {
  42. $fields['ID'] = $post_id;
  43. return wp_update_post( $fields );
  44. }
  45. /**
  46. * Retrieves a post by a given ID.
  47. *
  48. * @param int $post_id ID of the post to retrieve.
  49. *
  50. * @return WP_Post|null WP_Post object on success, null on failure.
  51. */
  52. public function get_object_by_id( $post_id ) {
  53. return get_post( $post_id );
  54. }
  55. }