wpAfterInsertPost.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * @group post
  4. */
  5. class Tests_Post_wpAfterInsertPost extends WP_UnitTestCase {
  6. /**
  7. * Admin user ID.
  8. *
  9. * @var int
  10. */
  11. public static $admin_id;
  12. /**
  13. * Attachment ID (no media attached).
  14. *
  15. * @var int
  16. */
  17. public static $attachment_id;
  18. /**
  19. * Post ID for testing updates.
  20. *
  21. * @var int
  22. */
  23. public static $post_id;
  24. /**
  25. * Title as passed to hook.
  26. *
  27. * @var string
  28. */
  29. public static $passed_post_title = '';
  30. /**
  31. * Status as passed to hook.
  32. *
  33. * @var string
  34. */
  35. public static $passed_post_status = '';
  36. /**
  37. * Before update title as passed to hook.
  38. *
  39. * @var string
  40. */
  41. public static $passed_post_before_title = '';
  42. /**
  43. * Before update status as passed to hook.
  44. *
  45. * @var string
  46. */
  47. public static $passed_post_before_status = '';
  48. public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  49. self::$admin_id = $factory->user->create(
  50. array(
  51. 'role' => 'administrator',
  52. 'user_login' => 'administrator',
  53. )
  54. );
  55. self::$post_id = $factory->post->create(
  56. array(
  57. 'post_status' => 'draft',
  58. 'post_title' => '45114 to be updated',
  59. )
  60. );
  61. self::$attachment_id = $factory->attachment->create(
  62. array(
  63. 'post_status' => 'inherit',
  64. 'post_title' => '45114 attachment to be updated',
  65. 'post_parent' => self::$post_id,
  66. )
  67. );
  68. }
  69. public function setUp() {
  70. parent::setUp();
  71. add_action( 'wp_after_insert_post', array( $this, 'action_wp_after_insert_post' ), 10, 4 );
  72. }
  73. public function tearDown() {
  74. self::$passed_post_title = '';
  75. self::$passed_post_status = '';
  76. self::$passed_post_before_title = '';
  77. self::$passed_post_before_status = '';
  78. parent::tearDown();
  79. }
  80. /**
  81. * Helper function to obtain data running on the hook `wp_after_insert_post`.
  82. *
  83. * @param int $post_id Post ID.
  84. * @param WP_Post $post Post object.
  85. * @param bool $update Whether this is an existing post being updated.
  86. * @param null|WP_Post $post_before Null for new posts, the WP_Post object prior
  87. * to the update for updated posts.
  88. */
  89. function action_wp_after_insert_post( $post_id, $post, $update, $post_before ) {
  90. self::$passed_post_title = $post->post_title;
  91. self::$passed_post_status = $post->post_status;
  92. if ( null === $post_before ) {
  93. self::$passed_post_before_title = null;
  94. self::$passed_post_before_status = null;
  95. return;
  96. }
  97. self::$passed_post_before_title = $post_before->post_title;
  98. self::$passed_post_before_status = $post_before->post_status;
  99. // Prevent this firing when the revision is generated.
  100. remove_action( 'wp_after_insert_post', array( $this, 'action_wp_after_insert_post' ), 10 );
  101. }
  102. /**
  103. * Ensure before post is correct when updating a post object.
  104. *
  105. * @ticket 45114
  106. */
  107. public function test_update_via_wp_update_post() {
  108. $post = get_post( self::$post_id, ARRAY_A );
  109. $post['post_title'] = 'new title';
  110. wp_update_post( $post );
  111. $this->assertSame( '45114 to be updated', self::$passed_post_before_title );
  112. $this->assertSame( 'new title', self::$passed_post_title );
  113. }
  114. /**
  115. * Ensure before post is correct when publishing a post object.
  116. *
  117. * @ticket 45114
  118. */
  119. public function test_update_via_wp_publish_post() {
  120. wp_publish_post( self::$post_id );
  121. $this->assertSame( 'draft', self::$passed_post_before_status );
  122. $this->assertSame( 'publish', self::$passed_post_status );
  123. }
  124. /**
  125. * Ensure before post is correct when inserting a new post.
  126. *
  127. * @ticket 45114
  128. */
  129. public function test_new_post_via_wp_insert_post() {
  130. wp_insert_post(
  131. array(
  132. 'post_status' => 'draft',
  133. 'post_title' => 'a new post',
  134. 'post_content' => 'new',
  135. )
  136. );
  137. $this->assertSame( null, self::$passed_post_before_status );
  138. $this->assertSame( 'a new post', self::$passed_post_title );
  139. }
  140. /**
  141. * Ensure before post is correct when updating post via REST API.
  142. *
  143. * @ticket 45114
  144. */
  145. public function test_update_via_rest_contoller() {
  146. wp_set_current_user( self::$admin_id );
  147. $post_id = self::$post_id;
  148. $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', $post_id ) );
  149. $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
  150. $request->set_body_params( array( 'title' => 'new title' ) );
  151. rest_get_server()->dispatch( $request );
  152. $this->assertSame( '45114 to be updated', self::$passed_post_before_title );
  153. $this->assertSame( 'new title', self::$passed_post_title );
  154. }
  155. /**
  156. * Ensure before post is correct when creating post via REST API.
  157. *
  158. * @ticket 45114
  159. */
  160. public function test_new_post_via_rest_contoller() {
  161. wp_set_current_user( self::$admin_id );
  162. $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts' ) );
  163. $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
  164. $request->set_body_params(
  165. array(
  166. 'title' => 'new title',
  167. 'status' => 'draft',
  168. )
  169. );
  170. rest_get_server()->dispatch( $request );
  171. $this->assertSame( null, self::$passed_post_before_title );
  172. $this->assertSame( 'new title', self::$passed_post_title );
  173. }
  174. /**
  175. * Ensure before post is correct when updating post via REST API.
  176. *
  177. * @ticket 45114
  178. */
  179. public function test_update_attachment_via_rest_contoller() {
  180. wp_set_current_user( self::$admin_id );
  181. $attachment_id = self::$attachment_id;
  182. $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/media/%d', $attachment_id ) );
  183. $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
  184. $request->set_body_params( array( 'title' => 'new attachment title' ) );
  185. rest_get_server()->dispatch( $request );
  186. $this->assertSame( '45114 attachment to be updated', self::$passed_post_before_title );
  187. $this->assertSame( 'new attachment title', self::$passed_post_title );
  188. }
  189. }