editPost.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. /**
  3. * @group xmlrpc
  4. */
  5. class Tests_XMLRPC_mw_editPost extends WP_XMLRPC_UnitTestCase {
  6. function test_invalid_username_password() {
  7. $post = array();
  8. $result = $this->myxmlrpcserver->mw_editPost( array( 1, 'username', 'password', $post ) );
  9. $this->assertIXRError( $result );
  10. $this->assertSame( 403, $result->code );
  11. }
  12. function test_edit_own_post() {
  13. $contributor_id = $this->make_user_by_role( 'contributor' );
  14. $post = array(
  15. 'post_title' => 'Post test',
  16. 'post_author' => $contributor_id,
  17. );
  18. $post_id = wp_insert_post( $post );
  19. $new_title = 'Post test (updated)';
  20. $post2 = array( 'title' => $new_title );
  21. $result = $this->myxmlrpcserver->mw_editPost( array( $post_id, 'contributor', 'contributor', $post2 ) );
  22. $this->assertNotIXRError( $result );
  23. $this->assertTrue( $result );
  24. $out = get_post( $post_id );
  25. $this->assertSame( $new_title, $out->post_title );
  26. }
  27. function test_capable_edit_others_post() {
  28. $this->make_user_by_role( 'editor' );
  29. $contributor_id = $this->make_user_by_role( 'contributor' );
  30. $post = array(
  31. 'post_title' => 'Post test',
  32. 'post_author' => $contributor_id,
  33. );
  34. $post_id = wp_insert_post( $post );
  35. $new_title = 'Post test (updated)';
  36. $post2 = array( 'title' => $new_title );
  37. $result = $this->myxmlrpcserver->mw_editPost( array( $post_id, 'editor', 'editor', $post2 ) );
  38. $this->assertNotIXRError( $result );
  39. $this->assertTrue( $result );
  40. $out = get_post( $post_id );
  41. $this->assertSame( $new_title, $out->post_title );
  42. }
  43. function test_incapable_edit_others_post() {
  44. $this->make_user_by_role( 'contributor' );
  45. $author_id = $this->make_user_by_role( 'author' );
  46. $original_title = 'Post test';
  47. $post = array(
  48. 'post_title' => $original_title,
  49. 'post_author' => $author_id,
  50. );
  51. $post_id = wp_insert_post( $post );
  52. $new_title = 'Post test (updated)';
  53. $post2 = array( 'title' => $new_title );
  54. $result = $this->myxmlrpcserver->mw_editPost( array( $post_id, 'contributor', 'contributor', $post2 ) );
  55. $this->assertIXRError( $result );
  56. $this->assertSame( 401, $result->code );
  57. $out = get_post( $post_id );
  58. $this->assertSame( $original_title, $out->post_title );
  59. }
  60. function test_capable_reassign_author() {
  61. $contributor_id = $this->make_user_by_role( 'contributor' );
  62. $author_id = $this->make_user_by_role( 'author' );
  63. $this->make_user_by_role( 'editor' );
  64. $post = array(
  65. 'post_title' => 'Post test',
  66. 'post_author' => $contributor_id,
  67. );
  68. $post_id = wp_insert_post( $post );
  69. $post2 = array( 'wp_author_id' => $author_id );
  70. $result = $this->myxmlrpcserver->mw_editPost( array( $post_id, 'editor', 'editor', $post2 ) );
  71. $this->assertNotIXRError( $result );
  72. $this->assertTrue( $result );
  73. $out = get_post( $post_id );
  74. $this->assertEquals( $author_id, $out->post_author );
  75. }
  76. function test_incapable_reassign_author() {
  77. $contributor_id = $this->make_user_by_role( 'contributor' );
  78. $author_id = $this->make_user_by_role( 'author' );
  79. $post = array(
  80. 'post_title' => 'Post test',
  81. 'post_author' => $contributor_id,
  82. );
  83. $post_id = wp_insert_post( $post );
  84. $post2 = array( 'wp_author_id' => $author_id );
  85. $result = $this->myxmlrpcserver->mw_editPost( array( $post_id, 'contributor', 'contributor', $post2 ) );
  86. $this->assertIXRError( $result );
  87. $this->assertSame( 401, $result->code );
  88. $out = get_post( $post_id );
  89. $this->assertEquals( $contributor_id, $out->post_author );
  90. }
  91. /**
  92. * @ticket 24916
  93. */
  94. function test_capable_reassign_author_to_self() {
  95. $contributor_id = $this->make_user_by_role( 'contributor' );
  96. $editor_id = $this->make_user_by_role( 'editor' );
  97. $post = array(
  98. 'post_title' => 'Post test',
  99. 'post_author' => $contributor_id,
  100. );
  101. $post_id = wp_insert_post( $post );
  102. $post2 = array( 'wp_author_id' => $editor_id );
  103. $result = $this->myxmlrpcserver->mw_editPost( array( $post_id, 'editor', 'editor', $post2 ) );
  104. $this->assertNotIXRError( $result );
  105. $this->assertTrue( $result );
  106. $out = get_post( $post_id );
  107. $this->assertEquals( $editor_id, $out->post_author );
  108. }
  109. function test_post_thumbnail() {
  110. add_theme_support( 'post-thumbnails' );
  111. $author_id = $this->make_user_by_role( 'author' );
  112. $post = array(
  113. 'post_title' => 'Post Thumbnail Test',
  114. 'post_author' => $author_id,
  115. );
  116. $post_id = wp_insert_post( $post );
  117. $this->assertSame( '', get_post_meta( $post_id, '_thumbnail_id', true ) );
  118. // Create attachment.
  119. $filename = ( DIR_TESTDATA . '/images/a2-small.jpg' );
  120. $attachment_id = self::factory()->attachment->create_upload_object( $filename, $post_id );
  121. // Add post thumbnail to post that does not have one.
  122. $post2 = array( 'wp_post_thumbnail' => $attachment_id );
  123. $result = $this->myxmlrpcserver->mw_editPost( array( $post_id, 'author', 'author', $post2 ) );
  124. $this->assertNotIXRError( $result );
  125. $this->assertEquals( $attachment_id, get_post_meta( $post_id, '_thumbnail_id', true ) );
  126. // Edit the post without supplying a post_thumbnail and check that it didn't change.
  127. $post3 = array( 'post_content' => 'Updated post' );
  128. $result = $this->myxmlrpcserver->mw_editPost( array( $post_id, 'author', 'author', $post3 ) );
  129. $this->assertNotIXRError( $result );
  130. $this->assertEquals( $attachment_id, get_post_meta( $post_id, '_thumbnail_id', true ) );
  131. // Create another attachment.
  132. $attachment2_id = self::factory()->attachment->create_upload_object( $filename, $post_id );
  133. // Change the post's post_thumbnail.
  134. $post4 = array( 'wp_post_thumbnail' => $attachment2_id );
  135. $result = $this->myxmlrpcserver->mw_editPost( array( $post_id, 'author', 'author', $post4 ) );
  136. $this->assertNotIXRError( $result );
  137. $this->assertEquals( $attachment2_id, get_post_meta( $post_id, '_thumbnail_id', true ) );
  138. // Unset the post's post_thumbnail.
  139. $post5 = array( 'wp_post_thumbnail' => '' );
  140. $result = $this->myxmlrpcserver->mw_editPost( array( $post_id, 'author', 'author', $post5 ) );
  141. $this->assertNotIXRError( $result );
  142. $this->assertSame( '', get_post_meta( $post_id, '_thumbnail_id', true ) );
  143. remove_theme_support( 'post-thumbnails' );
  144. }
  145. function test_edit_basic_post_info() {
  146. $contributor_id = $this->make_user_by_role( 'contributor' );
  147. $post = array(
  148. 'post_title' => 'Title',
  149. 'post_content' => 'Content',
  150. 'post_excerpt' => 'Excerpt',
  151. 'post_author' => $contributor_id,
  152. );
  153. $post_id = wp_insert_post( $post );
  154. $post2 = array(
  155. 'title' => 'New Title',
  156. 'post_author' => $contributor_id,
  157. );
  158. $result = $this->myxmlrpcserver->mw_editPost( array( $post_id, 'contributor', 'contributor', $post2 ) );
  159. $this->assertNotIXRError( $result );
  160. $this->assertTrue( $result );
  161. $out = get_post( $post_id );
  162. $this->assertSame( $post2['title'], $out->post_title );
  163. $post3 = array(
  164. 'description' => 'New Content',
  165. 'post_author' => $contributor_id,
  166. );
  167. $result = $this->myxmlrpcserver->mw_editPost( array( $post_id, 'contributor', 'contributor', $post3 ) );
  168. $this->assertNotIXRError( $result );
  169. $this->assertTrue( $result );
  170. $out = get_post( $post_id );
  171. $this->assertSame( $post2['title'], $out->post_title );
  172. $this->assertSame( $post3['description'], $out->post_content );
  173. $post4 = array(
  174. 'mt_excerpt' => 'New Excerpt',
  175. 'post_author' => $contributor_id,
  176. );
  177. $result = $this->myxmlrpcserver->mw_editPost( array( $post_id, 'contributor', 'contributor', $post4 ) );
  178. $this->assertNotIXRError( $result );
  179. $this->assertTrue( $result );
  180. $out = get_post( $post_id );
  181. $this->assertSame( $post2['title'], $out->post_title );
  182. $this->assertSame( $post3['description'], $out->post_content );
  183. $this->assertSame( $post4['mt_excerpt'], $out->post_excerpt );
  184. }
  185. /**
  186. * @ticket 20662
  187. */
  188. function test_make_post_sticky() {
  189. $author_id = $this->make_user_by_role( 'editor' );
  190. $post = array(
  191. 'post_title' => 'Title',
  192. 'post_content' => 'Content',
  193. 'post_author' => $author_id,
  194. 'post_status' => 'publish',
  195. );
  196. $post_id = wp_insert_post( $post );
  197. $result = $this->myxmlrpcserver->mw_editPost( array( $post_id, 'editor', 'editor', array( 'sticky' => '1' ) ) );
  198. $this->assertNotIXRError( $result );
  199. $this->assertTrue( $result );
  200. }
  201. // Not allowed since [19914].
  202. function test_change_post_type() {
  203. $contributor_id = $this->make_user_by_role( 'contributor' );
  204. $post = array(
  205. 'post_title' => 'Title',
  206. 'post_author' => $contributor_id,
  207. );
  208. $post_id = wp_insert_post( $post );
  209. $post2 = array( 'post_type' => 'page' );
  210. $result = $this->myxmlrpcserver->mw_editPost( array( $post_id, 'contributor', 'contributor', $post2 ) );
  211. $this->assertIXRError( $result );
  212. $this->assertSame( $result->code, 401 );
  213. }
  214. /**
  215. * @ticket 16980
  216. */
  217. function test_empty_not_null() {
  218. $editor_id = $this->make_user_by_role( 'editor' );
  219. $post_id = self::factory()->post->create(
  220. array(
  221. 'post_title' => 'Title',
  222. 'post_author' => $editor_id,
  223. 'tags_input' => 'taco',
  224. )
  225. );
  226. $tags1 = get_the_tags( $post_id );
  227. $this->assertNotEmpty( $tags1 );
  228. $this->myxmlrpcserver->mw_editPost(
  229. array(
  230. $post_id,
  231. 'editor',
  232. 'editor',
  233. array(
  234. 'mt_keywords' => '',
  235. ),
  236. )
  237. );
  238. $tags2 = get_the_tags( $post_id );
  239. $this->assertEmpty( $tags2 );
  240. }
  241. /**
  242. * @ticket 35874
  243. */
  244. function test_draft_not_prematurely_published() {
  245. $editor_id = $this->make_user_by_role( 'editor' );
  246. $post = array(
  247. 'title' => 'Title',
  248. );
  249. /**
  250. * We have to use wp_newPost method, rather than the factory
  251. * post->create method to create the database conditions that exhibit
  252. * the bug.
  253. */
  254. $post_id = $this->myxmlrpcserver->mw_newPost( array( 1, 'editor', 'editor', $post ) );
  255. // Change the post's status to publish and date to future.
  256. $future_time = strtotime( '+1 day' );
  257. $future_date = new IXR_Date( $future_time );
  258. $this->myxmlrpcserver->mw_editPost(
  259. array(
  260. $post_id,
  261. 'editor',
  262. 'editor',
  263. array(
  264. 'dateCreated' => $future_date,
  265. 'post_status' => 'publish',
  266. ),
  267. )
  268. );
  269. $after = get_post( $post_id );
  270. $this->assertSame( 'future', $after->post_status );
  271. $future_date_string = strftime( '%Y-%m-%d %H:%M:%S', $future_time );
  272. $this->assertSame( $future_date_string, $after->post_date );
  273. }
  274. }