class-wp-unittest-factory-for-comment.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * Unit test factory for comments.
  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_Comment 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_Comment extends WP_UnitTest_Factory_For_Thing {
  13. public function __construct( $factory = null ) {
  14. parent::__construct( $factory );
  15. $this->default_generation_definitions = array(
  16. 'comment_author' => new WP_UnitTest_Generator_Sequence( 'Commenter %s' ),
  17. 'comment_author_url' => new WP_UnitTest_Generator_Sequence( 'http://example.com/%s/' ),
  18. 'comment_approved' => 1,
  19. 'comment_content' => 'This is a comment',
  20. );
  21. }
  22. /**
  23. * Inserts a comment.
  24. *
  25. * @param array $args The comment details.
  26. *
  27. * @return int|false The comment's ID on success, false on failure.
  28. */
  29. public function create_object( $args ) {
  30. return wp_insert_comment( $this->addslashes_deep( $args ) );
  31. }
  32. /**
  33. * Updates a comment.
  34. *
  35. * @param int $comment_id The comment ID.
  36. * @param array $fields The comment details.
  37. *
  38. * @return int The value 1 if the comment was updated, 0 if not updated.
  39. */
  40. public function update_object( $comment_id, $fields ) {
  41. $fields['comment_ID'] = $comment_id;
  42. return wp_update_comment( $this->addslashes_deep( $fields ) );
  43. }
  44. /**
  45. * Creates multiple comments on a given post.
  46. *
  47. * @param int $post_id ID of the post to create comments for.
  48. * @param int $count Total amount of comments to create.
  49. * @param array $args The comment details.
  50. * @param null $generation_definitions Default values.
  51. *
  52. * @return int[] Array with the comment IDs.
  53. */
  54. public function create_post_comments( $post_id, $count = 1, $args = array(), $generation_definitions = null ) {
  55. $args['comment_post_ID'] = $post_id;
  56. return $this->create_many( $count, $args, $generation_definitions );
  57. }
  58. /**
  59. * Retrieves a comment by a given ID.
  60. *
  61. * @param int $comment_id ID of the comment to retrieve.
  62. *
  63. * @return WP_Comment|null WP_Comment object on success, null on failure.
  64. */
  65. public function get_object_by_id( $comment_id ) {
  66. return get_comment( $comment_id );
  67. }
  68. }