rest-comments-controller.php 114 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372
  1. <?php
  2. /**
  3. * Unit tests covering WP_REST_Comments_Controller functionality.
  4. *
  5. * @package WordPress
  6. * @subpackage REST API
  7. */
  8. /**
  9. * @group restapi
  10. */
  11. class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase {
  12. protected static $superadmin_id;
  13. protected static $admin_id;
  14. protected static $editor_id;
  15. protected static $moderator_id;
  16. protected static $subscriber_id;
  17. protected static $author_id;
  18. protected static $post_id;
  19. protected static $password_id;
  20. protected static $private_id;
  21. protected static $draft_id;
  22. protected static $trash_id;
  23. protected static $approved_id;
  24. protected static $hold_id;
  25. protected static $comment_ids = array();
  26. protected static $total_comments = 30;
  27. protected static $per_page = 50;
  28. protected $endpoint;
  29. public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  30. add_role(
  31. 'comment_moderator',
  32. 'Comment Moderator',
  33. array(
  34. 'read' => true,
  35. 'moderate_comments' => true,
  36. )
  37. );
  38. self::$superadmin_id = $factory->user->create(
  39. array(
  40. 'role' => 'administrator',
  41. 'user_login' => 'superadmin',
  42. )
  43. );
  44. self::$admin_id = $factory->user->create(
  45. array(
  46. 'role' => 'administrator',
  47. )
  48. );
  49. self::$editor_id = $factory->user->create(
  50. array(
  51. 'role' => 'editor',
  52. )
  53. );
  54. self::$moderator_id = $factory->user->create(
  55. array(
  56. 'role' => 'comment_moderator',
  57. )
  58. );
  59. self::$subscriber_id = $factory->user->create(
  60. array(
  61. 'role' => 'subscriber',
  62. )
  63. );
  64. self::$author_id = $factory->user->create(
  65. array(
  66. 'role' => 'author',
  67. 'display_name' => 'Sea Captain',
  68. 'first_name' => 'Horatio',
  69. 'last_name' => 'McCallister',
  70. 'user_email' => 'captain@thefryingdutchman.com',
  71. 'user_url' => 'http://thefryingdutchman.com',
  72. )
  73. );
  74. self::$post_id = $factory->post->create();
  75. self::$private_id = $factory->post->create(
  76. array(
  77. 'post_status' => 'private',
  78. )
  79. );
  80. self::$password_id = $factory->post->create(
  81. array(
  82. 'post_password' => 'toomanysecrets',
  83. )
  84. );
  85. self::$draft_id = $factory->post->create(
  86. array(
  87. 'post_status' => 'draft',
  88. )
  89. );
  90. self::$trash_id = $factory->post->create(
  91. array(
  92. 'post_status' => 'trash',
  93. )
  94. );
  95. self::$approved_id = $factory->comment->create(
  96. array(
  97. 'comment_approved' => 1,
  98. 'comment_post_ID' => self::$post_id,
  99. 'user_id' => 0,
  100. )
  101. );
  102. self::$hold_id = $factory->comment->create(
  103. array(
  104. 'comment_approved' => 0,
  105. 'comment_post_ID' => self::$post_id,
  106. 'user_id' => self::$subscriber_id,
  107. )
  108. );
  109. // Set up comments for pagination tests.
  110. for ( $i = 0; $i < self::$total_comments - 1; $i++ ) {
  111. $comment_ids[] = $factory->comment->create(
  112. array(
  113. 'comment_content' => "Comment {$i}",
  114. 'comment_post_ID' => self::$post_id,
  115. )
  116. );
  117. }
  118. }
  119. public static function wpTearDownAfterClass() {
  120. remove_role( 'comment_moderator' );
  121. self::delete_user( self::$superadmin_id );
  122. self::delete_user( self::$admin_id );
  123. self::delete_user( self::$editor_id );
  124. self::delete_user( self::$moderator_id );
  125. self::delete_user( self::$subscriber_id );
  126. self::delete_user( self::$author_id );
  127. wp_delete_post( self::$post_id, true );
  128. wp_delete_post( self::$private_id, true );
  129. wp_delete_post( self::$password_id, true );
  130. wp_delete_post( self::$draft_id, true );
  131. wp_delete_post( self::$trash_id, true );
  132. wp_delete_post( self::$approved_id, true );
  133. wp_delete_post( self::$hold_id, true );
  134. // Remove comments for pagination tests.
  135. foreach ( self::$comment_ids as $comment_id ) {
  136. wp_delete_comment( $comment_id, true );
  137. }
  138. }
  139. public function setUp() {
  140. parent::setUp();
  141. $this->endpoint = new WP_REST_Comments_Controller;
  142. if ( is_multisite() ) {
  143. update_site_option( 'site_admins', array( 'superadmin' ) );
  144. }
  145. }
  146. public function test_register_routes() {
  147. $routes = rest_get_server()->get_routes();
  148. $this->assertArrayHasKey( '/wp/v2/comments', $routes );
  149. $this->assertCount( 2, $routes['/wp/v2/comments'] );
  150. $this->assertArrayHasKey( '/wp/v2/comments/(?P<id>[\d]+)', $routes );
  151. $this->assertCount( 3, $routes['/wp/v2/comments/(?P<id>[\d]+)'] );
  152. }
  153. public function test_context_param() {
  154. // Collection.
  155. $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/comments' );
  156. $response = rest_get_server()->dispatch( $request );
  157. $data = $response->get_data();
  158. $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
  159. $this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
  160. // Single.
  161. $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/comments/' . self::$approved_id );
  162. $response = rest_get_server()->dispatch( $request );
  163. $data = $response->get_data();
  164. $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
  165. $this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
  166. }
  167. public function test_registered_query_params() {
  168. $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/comments' );
  169. $response = rest_get_server()->dispatch( $request );
  170. $data = $response->get_data();
  171. $keys = array_keys( $data['endpoints'][0]['args'] );
  172. sort( $keys );
  173. $this->assertSame(
  174. array(
  175. 'after',
  176. 'author',
  177. 'author_email',
  178. 'author_exclude',
  179. 'before',
  180. 'context',
  181. 'exclude',
  182. 'include',
  183. 'offset',
  184. 'order',
  185. 'orderby',
  186. 'page',
  187. 'parent',
  188. 'parent_exclude',
  189. 'password',
  190. 'per_page',
  191. 'post',
  192. 'search',
  193. 'status',
  194. 'type',
  195. ),
  196. $keys
  197. );
  198. }
  199. public function test_get_items() {
  200. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  201. $request->set_param( 'per_page', self::$per_page );
  202. $response = rest_get_server()->dispatch( $request );
  203. $this->assertSame( 200, $response->get_status() );
  204. $comments = $response->get_data();
  205. $this->assertCount( self::$total_comments, $comments );
  206. }
  207. /**
  208. * @ticket 38692
  209. */
  210. public function test_get_items_with_password() {
  211. wp_set_current_user( 0 );
  212. $args = array(
  213. 'comment_approved' => 1,
  214. 'comment_post_ID' => self::$password_id,
  215. );
  216. $password_comment = $this->factory->comment->create( $args );
  217. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  218. $request->set_param( 'password', 'toomanysecrets' );
  219. $request->set_param( 'post', self::$password_id );
  220. $response = rest_get_server()->dispatch( $request );
  221. $this->assertSame( 200, $response->get_status() );
  222. $collection_data = $response->get_data();
  223. $this->assertTrue( in_array( $password_comment, wp_list_pluck( $collection_data, 'id' ), true ) );
  224. }
  225. /**
  226. * @ticket 38692
  227. */
  228. public function test_get_items_with_password_without_post() {
  229. wp_set_current_user( 0 );
  230. $args = array(
  231. 'comment_approved' => 1,
  232. 'comment_post_ID' => self::$password_id,
  233. );
  234. $password_comment = $this->factory->comment->create( $args );
  235. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  236. $request->set_param( 'password', 'toomanysecrets' );
  237. $response = rest_get_server()->dispatch( $request );
  238. $this->assertSame( 200, $response->get_status() );
  239. $collection_data = $response->get_data();
  240. $this->assertFalse( in_array( $password_comment, wp_list_pluck( $collection_data, 'id' ), true ) );
  241. }
  242. /**
  243. * @ticket 38692
  244. */
  245. public function test_get_items_with_password_with_multiple_post() {
  246. wp_set_current_user( 0 );
  247. $args = array(
  248. 'comment_approved' => 1,
  249. 'comment_post_ID' => self::$password_id,
  250. );
  251. $password_comment = $this->factory->comment->create( $args );
  252. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  253. $request->set_param( 'password', 'toomanysecrets' );
  254. $request->set_param( 'post', array( self::$password_id, self::$post_id ) );
  255. $response = rest_get_server()->dispatch( $request );
  256. $this->assertErrorResponse( 'rest_cannot_read_post', $response, 401 );
  257. }
  258. public function test_get_password_items_without_edit_post_permission() {
  259. wp_set_current_user( 0 );
  260. $args = array(
  261. 'comment_approved' => 1,
  262. 'comment_post_ID' => self::$password_id,
  263. );
  264. $password_comment = $this->factory->comment->create( $args );
  265. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  266. $response = rest_get_server()->dispatch( $request );
  267. $this->assertSame( 200, $response->get_status() );
  268. $collection_data = $response->get_data();
  269. $this->assertFalse( in_array( $password_comment, wp_list_pluck( $collection_data, 'id' ), true ) );
  270. }
  271. public function test_get_password_items_with_edit_post_permission() {
  272. wp_set_current_user( self::$admin_id );
  273. $args = array(
  274. 'comment_approved' => 1,
  275. 'comment_post_ID' => self::$password_id,
  276. );
  277. $password_comment = $this->factory->comment->create( $args );
  278. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  279. $response = rest_get_server()->dispatch( $request );
  280. $this->assertSame( 200, $response->get_status() );
  281. $collection_data = $response->get_data();
  282. $this->assertTrue( in_array( $password_comment, wp_list_pluck( $collection_data, 'id' ), true ) );
  283. }
  284. public function test_get_items_without_private_post_permission() {
  285. wp_set_current_user( 0 );
  286. $args = array(
  287. 'comment_approved' => 1,
  288. 'comment_post_ID' => self::$private_id,
  289. );
  290. $private_comment = $this->factory->comment->create( $args );
  291. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  292. $response = rest_get_server()->dispatch( $request );
  293. $this->assertSame( 200, $response->get_status() );
  294. $collection_data = $response->get_data();
  295. $this->assertFalse( in_array( $private_comment, wp_list_pluck( $collection_data, 'id' ), true ) );
  296. }
  297. public function test_get_items_with_private_post_permission() {
  298. wp_set_current_user( self::$admin_id );
  299. $args = array(
  300. 'comment_approved' => 1,
  301. 'comment_post_ID' => self::$private_id,
  302. );
  303. $private_comment = $this->factory->comment->create( $args );
  304. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  305. $response = rest_get_server()->dispatch( $request );
  306. $this->assertSame( 200, $response->get_status() );
  307. $collection_data = $response->get_data();
  308. $this->assertTrue( in_array( $private_comment, wp_list_pluck( $collection_data, 'id' ), true ) );
  309. }
  310. public function test_get_items_with_invalid_post() {
  311. wp_set_current_user( 0 );
  312. $comment_id = $this->factory->comment->create(
  313. array(
  314. 'comment_approved' => 1,
  315. 'comment_post_ID' => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER,
  316. )
  317. );
  318. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  319. $response = rest_get_server()->dispatch( $request );
  320. $this->assertSame( 200, $response->get_status() );
  321. $collection_data = $response->get_data();
  322. $this->assertFalse( in_array( $comment_id, wp_list_pluck( $collection_data, 'id' ), true ) );
  323. wp_delete_comment( $comment_id );
  324. }
  325. public function test_get_items_with_invalid_post_permission() {
  326. wp_set_current_user( self::$admin_id );
  327. $comment_id = $this->factory->comment->create(
  328. array(
  329. 'comment_approved' => 1,
  330. 'comment_post_ID' => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER,
  331. )
  332. );
  333. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  334. $response = rest_get_server()->dispatch( $request );
  335. $this->assertSame( 200, $response->get_status() );
  336. $collection_data = $response->get_data();
  337. $this->assertTrue( in_array( $comment_id, wp_list_pluck( $collection_data, 'id' ), true ) );
  338. wp_delete_comment( $comment_id );
  339. }
  340. public function test_get_items_no_permission_for_context() {
  341. wp_set_current_user( 0 );
  342. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  343. $request->set_param( 'context', 'edit' );
  344. $response = rest_get_server()->dispatch( $request );
  345. $this->assertErrorResponse( 'rest_forbidden_context', $response, 401 );
  346. }
  347. public function test_get_items_no_post() {
  348. wp_set_current_user( self::$admin_id );
  349. $this->factory->comment->create_post_comments( 0, 2 );
  350. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  351. $request->set_param( 'post', 0 );
  352. $response = rest_get_server()->dispatch( $request );
  353. $this->assertSame( 200, $response->get_status() );
  354. $comments = $response->get_data();
  355. $this->assertCount( 2, $comments );
  356. }
  357. public function test_get_items_no_permission_for_no_post() {
  358. wp_set_current_user( 0 );
  359. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  360. $request->set_param( 'post', 0 );
  361. $response = rest_get_server()->dispatch( $request );
  362. $this->assertErrorResponse( 'rest_cannot_read', $response, 401 );
  363. }
  364. public function test_get_items_edit_context() {
  365. wp_set_current_user( self::$admin_id );
  366. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  367. $request->set_param( 'context', 'edit' );
  368. $response = rest_get_server()->dispatch( $request );
  369. $this->assertSame( 200, $response->get_status() );
  370. }
  371. public function test_get_items_for_post() {
  372. $second_post_id = $this->factory->post->create();
  373. $this->factory->comment->create_post_comments( $second_post_id, 2 );
  374. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  375. $request->set_query_params(
  376. array(
  377. 'post' => $second_post_id,
  378. )
  379. );
  380. $response = rest_get_server()->dispatch( $request );
  381. $this->assertSame( 200, $response->get_status() );
  382. $comments = $response->get_data();
  383. $this->assertCount( 2, $comments );
  384. }
  385. public function test_get_items_include_query() {
  386. wp_set_current_user( self::$admin_id );
  387. $args = array(
  388. 'comment_approved' => 1,
  389. 'comment_post_ID' => self::$post_id,
  390. );
  391. $id1 = $this->factory->comment->create( $args );
  392. $id2 = $this->factory->comment->create( $args );
  393. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  394. // 'order' => 'asc'.
  395. $request->set_param( 'order', 'asc' );
  396. $request->set_param( 'include', array( $id2, $id1 ) );
  397. $response = rest_get_server()->dispatch( $request );
  398. $data = $response->get_data();
  399. $this->assertSame( 2, count( $data ) );
  400. $this->assertSame( $id1, $data[0]['id'] );
  401. // 'orderby' => 'include'.
  402. $request->set_param( 'orderby', 'include' );
  403. $response = rest_get_server()->dispatch( $request );
  404. $data = $response->get_data();
  405. $this->assertSame( 2, count( $data ) );
  406. $this->assertSame( $id2, $data[0]['id'] );
  407. // Invalid 'orderby' should error.
  408. $request->set_param( 'orderby', 'invalid' );
  409. $response = rest_get_server()->dispatch( $request );
  410. $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
  411. // Invalid 'include' should error.
  412. $request->set_param( 'orderby', array( 'include' ) );
  413. $request->set_param( 'include', array( 'invalid' ) );
  414. $response = rest_get_server()->dispatch( $request );
  415. $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
  416. }
  417. public function test_get_items_exclude_query() {
  418. wp_set_current_user( self::$admin_id );
  419. $args = array(
  420. 'comment_approved' => 1,
  421. 'comment_post_ID' => self::$post_id,
  422. );
  423. $id1 = $this->factory->comment->create( $args );
  424. $id2 = $this->factory->comment->create( $args );
  425. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  426. $response = rest_get_server()->dispatch( $request );
  427. $data = $response->get_data();
  428. $ids = wp_list_pluck( $data, 'id' );
  429. $this->assertTrue( in_array( $id1, $ids, true ) );
  430. $this->assertTrue( in_array( $id2, $ids, true ) );
  431. $request->set_param( 'exclude', array( $id2 ) );
  432. $response = rest_get_server()->dispatch( $request );
  433. $data = $response->get_data();
  434. $ids = wp_list_pluck( $data, 'id' );
  435. $this->assertTrue( in_array( $id1, $ids, true ) );
  436. $this->assertFalse( in_array( $id2, $ids, true ) );
  437. // Invalid 'exclude' should error.
  438. $request->set_param( 'exclude', array( 'invalid' ) );
  439. $response = rest_get_server()->dispatch( $request );
  440. $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
  441. }
  442. public function test_get_items_offset_query() {
  443. wp_set_current_user( self::$admin_id );
  444. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  445. $request->set_param( 'per_page', self::$per_page );
  446. $request->set_param( 'offset', 1 );
  447. $response = rest_get_server()->dispatch( $request );
  448. $this->assertCount( self::$total_comments - 1, $response->get_data() );
  449. // 'offset' works with 'per_page'.
  450. $request->set_param( 'per_page', 2 );
  451. $response = rest_get_server()->dispatch( $request );
  452. $this->assertCount( 2, $response->get_data() );
  453. // 'offset' takes priority over 'page'.
  454. $request->set_param( 'page', 3 );
  455. $response = rest_get_server()->dispatch( $request );
  456. $this->assertCount( 2, $response->get_data() );
  457. // Invalid 'offset' should error.
  458. $request->set_param( 'offset', 'moreplease' );
  459. $response = rest_get_server()->dispatch( $request );
  460. $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
  461. }
  462. public function test_get_items_order_query() {
  463. wp_set_current_user( self::$admin_id );
  464. $args = array(
  465. 'comment_approved' => 1,
  466. 'comment_post_ID' => self::$post_id,
  467. );
  468. $id = $this->factory->comment->create( $args );
  469. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  470. // Order defaults to 'desc'.
  471. $response = rest_get_server()->dispatch( $request );
  472. $data = $response->get_data();
  473. $this->assertSame( $id, $data[0]['id'] );
  474. // 'order' => 'asc'.
  475. $request->set_param( 'order', 'asc' );
  476. $response = rest_get_server()->dispatch( $request );
  477. $data = $response->get_data();
  478. $this->assertSame( self::$approved_id, $data[0]['id'] );
  479. // 'order' => 'asc,id' should error.
  480. $request->set_param( 'order', 'asc,id' );
  481. $response = rest_get_server()->dispatch( $request );
  482. $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
  483. }
  484. public function test_get_items_private_post_no_permissions() {
  485. wp_set_current_user( 0 );
  486. $post_id = $this->factory->post->create( array( 'post_status' => 'private' ) );
  487. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  488. $request->set_param( 'post', $post_id );
  489. $response = rest_get_server()->dispatch( $request );
  490. $this->assertErrorResponse( 'rest_cannot_read_post', $response, 401 );
  491. }
  492. public function test_get_items_author_arg() {
  493. // Authorized.
  494. wp_set_current_user( self::$admin_id );
  495. $args = array(
  496. 'comment_approved' => 1,
  497. 'comment_post_ID' => self::$post_id,
  498. 'user_id' => self::$author_id,
  499. );
  500. $this->factory->comment->create( $args );
  501. $args['user_id'] = self::$subscriber_id;
  502. $this->factory->comment->create( $args );
  503. unset( $args['user_id'] );
  504. $this->factory->comment->create( $args );
  505. // Limit to comment author.
  506. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  507. $request->set_param( 'author', self::$author_id );
  508. $response = rest_get_server()->dispatch( $request );
  509. $this->assertSame( 200, $response->get_status() );
  510. $comments = $response->get_data();
  511. $this->assertCount( 1, $comments );
  512. // Multiple authors are supported.
  513. $request->set_param( 'author', array( self::$author_id, self::$subscriber_id ) );
  514. $response = rest_get_server()->dispatch( $request );
  515. $this->assertSame( 200, $response->get_status() );
  516. $comments = $response->get_data();
  517. $this->assertCount( 2, $comments );
  518. // Invalid 'author' should error.
  519. $request->set_param( 'author', 'skippy' );
  520. $response = rest_get_server()->dispatch( $request );
  521. $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
  522. // Unavailable to unauthenticated; defaults to error.
  523. wp_set_current_user( 0 );
  524. $request->set_param( 'author', array( self::$author_id, self::$subscriber_id ) );
  525. $response = rest_get_server()->dispatch( $request );
  526. $this->assertErrorResponse( 'rest_forbidden_param', $response, 401 );
  527. }
  528. public function test_get_items_author_exclude_arg() {
  529. // Authorized.
  530. wp_set_current_user( self::$admin_id );
  531. $args = array(
  532. 'comment_approved' => 1,
  533. 'comment_post_ID' => self::$post_id,
  534. 'user_id' => self::$author_id,
  535. );
  536. $this->factory->comment->create( $args );
  537. $args['user_id'] = self::$subscriber_id;
  538. $this->factory->comment->create( $args );
  539. unset( $args['user_id'] );
  540. $this->factory->comment->create( $args );
  541. $total_comments = self::$total_comments + 3;
  542. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  543. $request->set_param( 'per_page', self::$per_page );
  544. $response = rest_get_server()->dispatch( $request );
  545. $comments = $response->get_data();
  546. $this->assertCount( $total_comments, $comments );
  547. // Exclude comment author.
  548. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  549. $request->set_param( 'per_page', self::$per_page );
  550. $request->set_param( 'author_exclude', self::$author_id );
  551. $response = rest_get_server()->dispatch( $request );
  552. $this->assertSame( 200, $response->get_status() );
  553. $comments = $response->get_data();
  554. $this->assertCount( $total_comments - 1, $comments );
  555. // Exclude both comment authors.
  556. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  557. $request->set_param( 'per_page', self::$per_page );
  558. $request->set_param( 'author_exclude', array( self::$author_id, self::$subscriber_id ) );
  559. $response = rest_get_server()->dispatch( $request );
  560. $this->assertSame( 200, $response->get_status() );
  561. $comments = $response->get_data();
  562. $this->assertCount( $total_comments - 2, $comments );
  563. // 'author_exclude' for invalid author.
  564. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  565. $request->set_param( 'author_exclude', 'skippy' );
  566. $response = rest_get_server()->dispatch( $request );
  567. $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
  568. // Unavailable to unauthenticated; defaults to error.
  569. wp_set_current_user( 0 );
  570. $request->set_param( 'author_exclude', array( self::$author_id, self::$subscriber_id ) );
  571. $response = rest_get_server()->dispatch( $request );
  572. $this->assertErrorResponse( 'rest_forbidden_param', $response, 401 );
  573. }
  574. public function test_get_items_parent_arg() {
  575. $args = array(
  576. 'comment_approved' => 1,
  577. 'comment_post_ID' => self::$post_id,
  578. );
  579. $parent_id = $this->factory->comment->create( $args );
  580. $parent_id2 = $this->factory->comment->create( $args );
  581. $args['comment_parent'] = $parent_id;
  582. $this->factory->comment->create( $args );
  583. $args['comment_parent'] = $parent_id2;
  584. $this->factory->comment->create( $args );
  585. $total_comments = self::$total_comments + 4;
  586. // All comments in the database.
  587. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  588. $request->set_param( 'per_page', self::$per_page );
  589. $response = rest_get_server()->dispatch( $request );
  590. $this->assertCount( $total_comments, $response->get_data() );
  591. // Limit to the parent.
  592. $request->set_param( 'parent', $parent_id );
  593. $response = rest_get_server()->dispatch( $request );
  594. $this->assertCount( 1, $response->get_data() );
  595. // Limit to two parents.
  596. $request->set_param( 'parent', array( $parent_id, $parent_id2 ) );
  597. $response = rest_get_server()->dispatch( $request );
  598. $this->assertCount( 2, $response->get_data() );
  599. // Invalid 'parent' should error.
  600. $request->set_param( 'parent', 'invalid' );
  601. $response = rest_get_server()->dispatch( $request );
  602. $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
  603. }
  604. public function test_get_items_parent_exclude_arg() {
  605. $args = array(
  606. 'comment_approved' => 1,
  607. 'comment_post_ID' => self::$post_id,
  608. );
  609. $parent_id = $this->factory->comment->create( $args );
  610. $parent_id2 = $this->factory->comment->create( $args );
  611. $args['comment_parent'] = $parent_id;
  612. $this->factory->comment->create( $args );
  613. $args['comment_parent'] = $parent_id2;
  614. $this->factory->comment->create( $args );
  615. $total_comments = self::$total_comments + 4;
  616. // All comments in the database.
  617. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  618. $request->set_param( 'per_page', self::$per_page );
  619. $response = rest_get_server()->dispatch( $request );
  620. $this->assertCount( $total_comments, $response->get_data() );
  621. // Exclude this particular parent.
  622. $request->set_param( 'parent_exclude', $parent_id );
  623. $response = rest_get_server()->dispatch( $request );
  624. $this->assertCount( $total_comments - 1, $response->get_data() );
  625. // Exclude both comment parents.
  626. $request->set_param( 'parent_exclude', array( $parent_id, $parent_id2 ) );
  627. $response = rest_get_server()->dispatch( $request );
  628. $this->assertCount( $total_comments - 2, $response->get_data() );
  629. // Invalid 'parent_exclude' should error.
  630. $request->set_param( 'parent_exclude', 'invalid' );
  631. $response = rest_get_server()->dispatch( $request );
  632. $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
  633. }
  634. public function test_get_items_search_query() {
  635. wp_set_current_user( self::$admin_id );
  636. $args = array(
  637. 'comment_approved' => 1,
  638. 'comment_post_ID' => self::$post_id,
  639. 'comment_content' => 'foo',
  640. 'comment_author' => 'Homer J Simpson',
  641. );
  642. $id = $this->factory->comment->create( $args );
  643. $total_comments = self::$total_comments + 1;
  644. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  645. $request->set_param( 'per_page', self::$per_page );
  646. $response = rest_get_server()->dispatch( $request );
  647. $this->assertCount( $total_comments, $response->get_data() );
  648. // One matching comment.
  649. $request->set_param( 'search', 'foo' );
  650. $response = rest_get_server()->dispatch( $request );
  651. $data = $response->get_data();
  652. $this->assertCount( 1, $data );
  653. $this->assertSame( $id, $data[0]['id'] );
  654. }
  655. public function test_get_comments_pagination_headers() {
  656. $total_comments = self::$total_comments;
  657. $total_pages = (int) ceil( $total_comments / 10 );
  658. wp_set_current_user( self::$admin_id );
  659. // Start of the index.
  660. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  661. $response = rest_get_server()->dispatch( $request );
  662. $headers = $response->get_headers();
  663. $this->assertSame( $total_comments, $headers['X-WP-Total'] );
  664. $this->assertSame( $total_pages, $headers['X-WP-TotalPages'] );
  665. $next_link = add_query_arg(
  666. array(
  667. 'page' => 2,
  668. ),
  669. rest_url( '/wp/v2/comments' )
  670. );
  671. $this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) );
  672. $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
  673. // 3rd page.
  674. $this->factory->comment->create(
  675. array(
  676. 'comment_post_ID' => self::$post_id,
  677. )
  678. );
  679. $total_comments++;
  680. $total_pages++;
  681. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  682. $request->set_param( 'page', 3 );
  683. $response = rest_get_server()->dispatch( $request );
  684. $headers = $response->get_headers();
  685. $this->assertSame( $total_comments, $headers['X-WP-Total'] );
  686. $this->assertSame( $total_pages, $headers['X-WP-TotalPages'] );
  687. $prev_link = add_query_arg(
  688. array(
  689. 'page' => 2,
  690. ),
  691. rest_url( '/wp/v2/comments' )
  692. );
  693. $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
  694. $next_link = add_query_arg(
  695. array(
  696. 'page' => 4,
  697. ),
  698. rest_url( '/wp/v2/comments' )
  699. );
  700. $this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
  701. // Last page.
  702. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  703. $request->set_param( 'page', $total_pages );
  704. $response = rest_get_server()->dispatch( $request );
  705. $headers = $response->get_headers();
  706. $this->assertSame( $total_comments, $headers['X-WP-Total'] );
  707. $this->assertSame( $total_pages, $headers['X-WP-TotalPages'] );
  708. $prev_link = add_query_arg(
  709. array(
  710. 'page' => $total_pages - 1,
  711. ),
  712. rest_url( '/wp/v2/comments' )
  713. );
  714. $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
  715. $this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
  716. // Out of bounds.
  717. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  718. $request->set_param( 'page', 100 );
  719. $response = rest_get_server()->dispatch( $request );
  720. $headers = $response->get_headers();
  721. $this->assertSame( $total_comments, $headers['X-WP-Total'] );
  722. $this->assertEquals( $total_pages, $headers['X-WP-TotalPages'] );
  723. $prev_link = add_query_arg(
  724. array(
  725. 'page' => $total_pages,
  726. ),
  727. rest_url( '/wp/v2/comments' )
  728. );
  729. $this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
  730. $this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
  731. }
  732. public function test_get_comments_invalid_date() {
  733. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  734. $request->set_param( 'after', rand_str() );
  735. $request->set_param( 'before', rand_str() );
  736. $response = rest_get_server()->dispatch( $request );
  737. $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
  738. }
  739. public function test_get_comments_valid_date() {
  740. $comment1 = $this->factory->comment->create(
  741. array(
  742. 'comment_date' => '2016-01-15T00:00:00Z',
  743. 'comment_post_ID' => self::$post_id,
  744. )
  745. );
  746. $comment2 = $this->factory->comment->create(
  747. array(
  748. 'comment_date' => '2016-01-16T00:00:00Z',
  749. 'comment_post_ID' => self::$post_id,
  750. )
  751. );
  752. $comment3 = $this->factory->comment->create(
  753. array(
  754. 'comment_date' => '2016-01-17T00:00:00Z',
  755. 'comment_post_ID' => self::$post_id,
  756. )
  757. );
  758. $request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  759. $request->set_param( 'after', '2016-01-15T00:00:00Z' );
  760. $request->set_param( 'before', '2016-01-17T00:00:00Z' );
  761. $response = rest_get_server()->dispatch( $request );
  762. $data = $response->get_data();
  763. $this->assertCount( 1, $data );
  764. $this->assertSame( $comment2, $data[0]['id'] );
  765. }
  766. public function test_get_item() {
  767. $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
  768. $response = rest_get_server()->dispatch( $request );
  769. $this->assertSame( 200, $response->get_status() );
  770. $data = $response->get_data();
  771. $this->check_comment_data( $data, 'view', $response->get_links() );
  772. }
  773. public function test_prepare_item() {
  774. wp_set_current_user( self::$admin_id );
  775. $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
  776. $request->set_query_params(
  777. array(
  778. 'context' => 'edit',
  779. )
  780. );
  781. $response = rest_get_server()->dispatch( $request );
  782. $this->assertSame( 200, $response->get_status() );
  783. $data = $response->get_data();
  784. $this->check_comment_data( $data, 'edit', $response->get_links() );
  785. }
  786. public function test_prepare_item_limit_fields() {
  787. wp_set_current_user( self::$admin_id );
  788. $endpoint = new WP_REST_Comments_Controller;
  789. $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
  790. $request->set_param( 'context', 'edit' );
  791. $request->set_param( '_fields', 'id,status' );
  792. $obj = get_comment( self::$approved_id );
  793. $response = $endpoint->prepare_item_for_response( $obj, $request );
  794. $this->assertSame(
  795. array(
  796. 'id',
  797. 'status',
  798. ),
  799. array_keys( $response->get_data() )
  800. );
  801. }
  802. public function test_get_comment_author_avatar_urls() {
  803. $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
  804. $response = rest_get_server()->dispatch( $request );
  805. $data = $response->get_data();
  806. $this->assertArrayHasKey( 24, $data['author_avatar_urls'] );
  807. $this->assertArrayHasKey( 48, $data['author_avatar_urls'] );
  808. $this->assertArrayHasKey( 96, $data['author_avatar_urls'] );
  809. $comment = get_comment( self::$approved_id );
  810. // Ignore the subdomain, since get_avatar_url() randomly sets
  811. // the Gravatar server when building the URL string.
  812. $this->assertSame( substr( get_avatar_url( $comment->comment_author_email ), 9 ), substr( $data['author_avatar_urls'][96], 9 ) );
  813. }
  814. public function test_get_comment_invalid_id() {
  815. $request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
  816. $response = rest_get_server()->dispatch( $request );
  817. $this->assertErrorResponse( 'rest_comment_invalid_id', $response, 404 );
  818. }
  819. public function test_get_comment_invalid_context() {
  820. wp_set_current_user( 0 );
  821. $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', self::$approved_id ) );
  822. $request->set_param( 'context', 'edit' );
  823. $response = rest_get_server()->dispatch( $request );
  824. $this->assertErrorResponse( 'rest_forbidden_context', $response, 401 );
  825. }
  826. public function test_get_comment_invalid_post_id() {
  827. wp_set_current_user( 0 );
  828. $comment_id = $this->factory->comment->create(
  829. array(
  830. 'comment_approved' => 1,
  831. 'comment_post_ID' => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER,
  832. )
  833. );
  834. $request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $comment_id );
  835. $response = rest_get_server()->dispatch( $request );
  836. $this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 );
  837. }
  838. public function test_get_comment_invalid_post_id_as_admin() {
  839. wp_set_current_user( self::$admin_id );
  840. $comment_id = $this->factory->comment->create(
  841. array(
  842. 'comment_approved' => 1,
  843. 'comment_post_ID' => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER,
  844. )
  845. );
  846. $request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $comment_id );
  847. $response = rest_get_server()->dispatch( $request );
  848. $this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 );
  849. }
  850. public function test_get_comment_not_approved() {
  851. wp_set_current_user( 0 );
  852. $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$hold_id ) );
  853. $response = rest_get_server()->dispatch( $request );
  854. $this->assertErrorResponse( 'rest_cannot_read', $response, 401 );
  855. }
  856. public function test_get_comment_not_approved_same_user() {
  857. wp_set_current_user( self::$admin_id );
  858. $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$hold_id ) );
  859. $response = rest_get_server()->dispatch( $request );
  860. $this->assertSame( 200, $response->get_status() );
  861. }
  862. public function test_get_comment_with_children_link() {
  863. $comment_id_1 = $this->factory->comment->create(
  864. array(
  865. 'comment_approved' => 1,
  866. 'comment_post_ID' => self::$post_id,
  867. 'user_id' => self::$subscriber_id,
  868. )
  869. );
  870. $child_comment = $this->factory->comment->create(
  871. array(
  872. 'comment_approved' => 1,
  873. 'comment_parent' => $comment_id_1,
  874. 'comment_post_ID' => self::$post_id,
  875. 'user_id' => self::$subscriber_id,
  876. )
  877. );
  878. $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $comment_id_1 ) );
  879. $response = rest_get_server()->dispatch( $request );
  880. $this->assertSame( 200, $response->get_status() );
  881. $this->assertArrayHasKey( 'children', $response->get_links() );
  882. }
  883. public function test_get_comment_without_children_link() {
  884. $comment_id_1 = $this->factory->comment->create(
  885. array(
  886. 'comment_approved' => 1,
  887. 'comment_post_ID' => self::$post_id,
  888. 'user_id' => self::$subscriber_id,
  889. )
  890. );
  891. $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $comment_id_1 ) );
  892. $response = rest_get_server()->dispatch( $request );
  893. $this->assertSame( 200, $response->get_status() );
  894. $this->assertArrayNotHasKey( 'children', $response->get_links() );
  895. }
  896. public function test_get_comment_with_password_without_edit_post_permission() {
  897. wp_set_current_user( self::$subscriber_id );
  898. $args = array(
  899. 'comment_approved' => 1,
  900. 'comment_post_ID' => self::$password_id,
  901. );
  902. $password_comment = $this->factory->comment->create( $args );
  903. $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $password_comment ) );
  904. $response = rest_get_server()->dispatch( $request );
  905. $this->assertErrorResponse( 'rest_cannot_read', $response, 403 );
  906. }
  907. /**
  908. * @ticket 38692
  909. */
  910. public function test_get_comment_with_password_with_valid_password() {
  911. wp_set_current_user( self::$subscriber_id );
  912. $args = array(
  913. 'comment_approved' => 1,
  914. 'comment_post_ID' => self::$password_id,
  915. );
  916. $password_comment = $this->factory->comment->create( $args );
  917. $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $password_comment ) );
  918. $request->set_param( 'password', 'toomanysecrets' );
  919. $response = rest_get_server()->dispatch( $request );
  920. $this->assertSame( 200, $response->get_status() );
  921. }
  922. public function test_create_item() {
  923. wp_set_current_user( self::$admin_id );
  924. $params = array(
  925. 'post' => self::$post_id,
  926. 'author_name' => 'Comic Book Guy',
  927. 'author_email' => 'cbg@androidsdungeon.com',
  928. 'author_url' => 'http://androidsdungeon.com',
  929. 'content' => 'Worst Comment Ever!',
  930. 'date' => '2014-11-07T10:14:25',
  931. );
  932. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  933. $request->add_header( 'content-type', 'application/json' );
  934. $request->set_body( wp_json_encode( $params ) );
  935. $response = rest_get_server()->dispatch( $request );
  936. $this->assertSame( 201, $response->get_status() );
  937. $data = $response->get_data();
  938. $this->check_comment_data( $data, 'edit', $response->get_links() );
  939. $this->assertSame( 'hold', $data['status'] );
  940. $this->assertSame( '2014-11-07T10:14:25', $data['date'] );
  941. $this->assertSame( self::$post_id, $data['post'] );
  942. }
  943. public function comment_dates_provider() {
  944. return array(
  945. 'set date without timezone' => array(
  946. 'params' => array(
  947. 'timezone_string' => 'America/New_York',
  948. 'date' => '2016-12-12T14:00:00',
  949. ),
  950. 'results' => array(
  951. 'date' => '2016-12-12T14:00:00',
  952. 'date_gmt' => '2016-12-12T19:00:00',
  953. ),
  954. ),
  955. 'set date_gmt without timezone' => array(
  956. 'params' => array(
  957. 'timezone_string' => 'America/New_York',
  958. 'date_gmt' => '2016-12-12T19:00:00',
  959. ),
  960. 'results' => array(
  961. 'date' => '2016-12-12T14:00:00',
  962. 'date_gmt' => '2016-12-12T19:00:00',
  963. ),
  964. ),
  965. 'set date with timezone' => array(
  966. 'params' => array(
  967. 'timezone_string' => 'America/New_York',
  968. 'date' => '2016-12-12T18:00:00-01:00',
  969. ),
  970. 'results' => array(
  971. 'date' => '2016-12-12T14:00:00',
  972. 'date_gmt' => '2016-12-12T19:00:00',
  973. ),
  974. ),
  975. 'set date_gmt with timezone' => array(
  976. 'params' => array(
  977. 'timezone_string' => 'America/New_York',
  978. 'date_gmt' => '2016-12-12T18:00:00-01:00',
  979. ),
  980. 'results' => array(
  981. 'date' => '2016-12-12T14:00:00',
  982. 'date_gmt' => '2016-12-12T19:00:00',
  983. ),
  984. ),
  985. );
  986. }
  987. /**
  988. * @dataProvider comment_dates_provider
  989. */
  990. public function test_create_comment_date( $params, $results ) {
  991. wp_set_current_user( self::$admin_id );
  992. update_option( 'timezone_string', $params['timezone_string'] );
  993. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  994. $request->set_param( 'content', 'not empty' );
  995. $request->set_param( 'post', self::$post_id );
  996. if ( isset( $params['date'] ) ) {
  997. $request->set_param( 'date', $params['date'] );
  998. }
  999. if ( isset( $params['date_gmt'] ) ) {
  1000. $request->set_param( 'date_gmt', $params['date_gmt'] );
  1001. }
  1002. $response = rest_get_server()->dispatch( $request );
  1003. update_option( 'timezone_string', '' );
  1004. $this->assertSame( 201, $response->get_status() );
  1005. $data = $response->get_data();
  1006. $comment = get_comment( $data['id'] );
  1007. $this->assertSame( $results['date'], $data['date'] );
  1008. $comment_date = str_replace( 'T', ' ', $results['date'] );
  1009. $this->assertSame( $comment_date, $comment->comment_date );
  1010. $this->assertSame( $results['date_gmt'], $data['date_gmt'] );
  1011. $comment_date_gmt = str_replace( 'T', ' ', $results['date_gmt'] );
  1012. $this->assertSame( $comment_date_gmt, $comment->comment_date_gmt );
  1013. }
  1014. public function test_create_item_using_accepted_content_raw_value() {
  1015. wp_set_current_user( self::$admin_id );
  1016. $params = array(
  1017. 'post' => self::$post_id,
  1018. 'author_name' => 'Reverend Lovejoy',
  1019. 'author_email' => 'lovejoy@example.com',
  1020. 'author_url' => 'http://timothylovejoy.jr',
  1021. 'content' => array(
  1022. 'raw' => 'Once something has been approved by the government, it\'s no longer immoral.',
  1023. ),
  1024. );
  1025. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1026. $request->add_header( 'content-type', 'application/json' );
  1027. $request->set_body( wp_json_encode( $params ) );
  1028. $response = rest_get_server()->dispatch( $request );
  1029. $this->assertSame( 201, $response->get_status() );
  1030. $data = $response->get_data();
  1031. $new_comment = get_comment( $data['id'] );
  1032. $this->assertSame( $params['content']['raw'], $new_comment->comment_content );
  1033. }
  1034. public function test_create_item_error_from_filter() {
  1035. add_filter( 'rest_pre_insert_comment', array( $this, 'return_premade_error' ) );
  1036. wp_set_current_user( self::$admin_id );
  1037. $params = array(
  1038. 'post' => self::$post_id,
  1039. 'author_name' => 'Homer Jay Simpson',
  1040. 'author_email' => 'homer@example.org',
  1041. 'content' => array(
  1042. 'raw' => 'Aw, he loves beer. Here, little fella.',
  1043. ),
  1044. );
  1045. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1046. $request->add_header( 'content-type', 'application/json' );
  1047. $request->set_body( wp_json_encode( $params ) );
  1048. $response = rest_get_server()->dispatch( $request );
  1049. $this->assertErrorResponse( 'test_rest_premade_error', $response, 418 );
  1050. }
  1051. public function return_premade_error() {
  1052. return new WP_Error( 'test_rest_premade_error', "I'm sorry, I thought he was a party robot.", array( 'status' => 418 ) );
  1053. }
  1054. public function test_create_comment_missing_required_author_name() {
  1055. add_filter( 'rest_allow_anonymous_comments', '__return_true' );
  1056. update_option( 'require_name_email', 1 );
  1057. $params = array(
  1058. 'post' => self::$post_id,
  1059. 'author_email' => 'ekrabappel@springfield-elementary.edu',
  1060. 'content' => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
  1061. );
  1062. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1063. $request->add_header( 'content-type', 'application/json' );
  1064. $request->set_body( wp_json_encode( $params ) );
  1065. $response = rest_get_server()->dispatch( $request );
  1066. $this->assertErrorResponse( 'rest_comment_author_data_required', $response, 400 );
  1067. }
  1068. public function test_create_comment_empty_required_author_name() {
  1069. add_filter( 'rest_allow_anonymous_comments', '__return_true' );
  1070. update_option( 'require_name_email', 1 );
  1071. $params = array(
  1072. 'author_name' => '',
  1073. 'author_email' => 'ekrabappel@springfield-elementary.edu',
  1074. 'post' => self::$post_id,
  1075. 'content' => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
  1076. );
  1077. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1078. $request->add_header( 'content-type', 'application/json' );
  1079. $request->set_body( wp_json_encode( $params ) );
  1080. $response = rest_get_server()->dispatch( $request );
  1081. $this->assertErrorResponse( 'rest_comment_author_data_required', $response, 400 );
  1082. }
  1083. public function test_create_comment_missing_required_author_email() {
  1084. wp_set_current_user( self::$admin_id );
  1085. update_option( 'require_name_email', 1 );
  1086. $params = array(
  1087. 'post' => self::$post_id,
  1088. 'author_name' => 'Edna Krabappel',
  1089. 'content' => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
  1090. );
  1091. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1092. $request->add_header( 'content-type', 'application/json' );
  1093. $request->set_body( wp_json_encode( $params ) );
  1094. $response = rest_get_server()->dispatch( $request );
  1095. $this->assertErrorResponse( 'rest_comment_author_data_required', $response, 400 );
  1096. }
  1097. public function test_create_comment_empty_required_author_email() {
  1098. wp_set_current_user( self::$admin_id );
  1099. update_option( 'require_name_email', 1 );
  1100. $params = array(
  1101. 'post' => self::$post_id,
  1102. 'author_name' => 'Edna Krabappel',
  1103. 'author_email' => '',
  1104. 'content' => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
  1105. );
  1106. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1107. $request->add_header( 'content-type', 'application/json' );
  1108. $request->set_body( wp_json_encode( $params ) );
  1109. $response = rest_get_server()->dispatch( $request );
  1110. $this->assertErrorResponse( 'rest_comment_author_data_required', $response, 400 );
  1111. }
  1112. public function test_create_comment_author_email_too_short() {
  1113. wp_set_current_user( self::$admin_id );
  1114. $params = array(
  1115. 'post' => self::$post_id,
  1116. 'author_name' => 'Homer J. Simpson',
  1117. 'author_email' => 'a@b',
  1118. 'content' => 'in this house, we obey the laws of thermodynamics!',
  1119. );
  1120. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1121. $request->add_header( 'content-type', 'application/json' );
  1122. $request->set_body( wp_json_encode( $params ) );
  1123. $response = rest_get_server()->dispatch( $request );
  1124. $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
  1125. $data = $response->get_data();
  1126. $this->assertArrayHasKey( 'author_email', $data['data']['params'] );
  1127. }
  1128. public function test_create_item_invalid_no_content() {
  1129. wp_set_current_user( self::$admin_id );
  1130. $params = array(
  1131. 'post' => self::$post_id,
  1132. 'author_name' => 'Reverend Lovejoy',
  1133. 'author_email' => 'lovejoy@example.com',
  1134. 'author_url' => 'http://timothylovejoy.jr',
  1135. );
  1136. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1137. $request->add_header( 'content-type', 'application/json' );
  1138. $request->set_body( wp_json_encode( $params ) );
  1139. $response = rest_get_server()->dispatch( $request );
  1140. $this->assertErrorResponse( 'rest_comment_content_invalid', $response, 400 );
  1141. $params['content'] = '';
  1142. $request->set_body( wp_json_encode( $params ) );
  1143. $response = rest_get_server()->dispatch( $request );
  1144. $this->assertErrorResponse( 'rest_comment_content_invalid', $response, 400 );
  1145. }
  1146. /**
  1147. * @ticket 43177
  1148. */
  1149. public function test_create_item_invalid_only_spaces_content() {
  1150. wp_set_current_user( self::$admin_id );
  1151. $params = array(
  1152. 'post' => self::$post_id,
  1153. 'author_name' => 'Reverend Lovejoy',
  1154. 'author_email' => 'lovejoy@example.com',
  1155. 'author_url' => 'http://timothylovejoy.jr',
  1156. 'content' => ' ',
  1157. );
  1158. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1159. $request->add_header( 'content-type', 'application/json' );
  1160. $request->set_body( wp_json_encode( $params ) );
  1161. $response = rest_get_server()->dispatch( $request );
  1162. $this->assertErrorResponse( 'rest_comment_content_invalid', $response, 400 );
  1163. }
  1164. /**
  1165. * @ticket 43177
  1166. */
  1167. public function test_create_item_allows_0_as_content() {
  1168. wp_set_current_user( self::$admin_id );
  1169. $params = array(
  1170. 'post' => self::$post_id,
  1171. 'author_name' => 'Reverend Lovejoy',
  1172. 'author_email' => 'lovejoy@example.com',
  1173. 'author_url' => 'http://timothylovejoy.jr',
  1174. 'content' => '0',
  1175. );
  1176. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1177. $request->add_header( 'content-type', 'application/json' );
  1178. $request->set_body( wp_json_encode( $params ) );
  1179. $response = rest_get_server()->dispatch( $request );
  1180. $this->assertSame( 201, $response->get_status() );
  1181. $this->assertSame( '0', $response->get_data()['content']['raw'] );
  1182. }
  1183. /**
  1184. * @ticket 43177
  1185. */
  1186. public function test_create_item_allow_empty_comment_filter() {
  1187. add_filter( 'allow_empty_comment', '__return_true' );
  1188. wp_set_current_user( self::$admin_id );
  1189. $params = array(
  1190. 'post' => self::$post_id,
  1191. 'author_name' => 'Reverend Lovejoy',
  1192. 'author_email' => 'lovejoy@example.com',
  1193. 'author_url' => 'http://timothylovejoy.jr',
  1194. 'content' => '',
  1195. );
  1196. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1197. $request->add_header( 'content-type', 'application/json' );
  1198. $request->set_body( wp_json_encode( $params ) );
  1199. $response = rest_get_server()->dispatch( $request );
  1200. $this->assertSame( 201, $response->get_status() );
  1201. $this->assertSame( '', $response->get_data()['content']['raw'] );
  1202. }
  1203. public function test_create_item_invalid_date() {
  1204. wp_set_current_user( self::$admin_id );
  1205. $params = array(
  1206. 'post' => self::$post_id,
  1207. 'author_name' => 'Reverend Lovejoy',
  1208. 'author_email' => 'lovejoy@example.com',
  1209. 'author_url' => 'http://timothylovejoy.jr',
  1210. 'content' => 'It\'s all over\, people! We don\'t have a prayer!',
  1211. 'date' => rand_str(),
  1212. );
  1213. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1214. $request->add_header( 'content-type', 'application/json' );
  1215. $request->set_body( wp_json_encode( $params ) );
  1216. $response = rest_get_server()->dispatch( $request );
  1217. $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
  1218. }
  1219. public function test_create_item_assign_different_user() {
  1220. $subscriber_id = $this->factory->user->create(
  1221. array(
  1222. 'role' => 'subscriber',
  1223. 'user_email' => 'cbg@androidsdungeon.com',
  1224. )
  1225. );
  1226. wp_set_current_user( self::$admin_id );
  1227. $params = array(
  1228. 'post' => self::$post_id,
  1229. 'author_name' => 'Comic Book Guy',
  1230. 'author_email' => 'cbg@androidsdungeon.com',
  1231. 'author_url' => 'http://androidsdungeon.com',
  1232. 'author' => $subscriber_id,
  1233. 'content' => 'Worst Comment Ever!',
  1234. 'date' => '2014-11-07T10:14:25',
  1235. );
  1236. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1237. $request->add_header( 'content-type', 'application/json' );
  1238. $request->set_body( wp_json_encode( $params ) );
  1239. $response = rest_get_server()->dispatch( $request );
  1240. $this->assertSame( 201, $response->get_status() );
  1241. $data = $response->get_data();
  1242. $this->assertSame( $subscriber_id, $data['author'] );
  1243. $this->assertSame( '127.0.0.1', $data['author_ip'] );
  1244. }
  1245. public function test_create_comment_without_type() {
  1246. $post_id = $this->factory->post->create();
  1247. wp_set_current_user( self::$admin_id );
  1248. $params = array(
  1249. 'post' => $post_id,
  1250. 'author' => self::$admin_id,
  1251. 'author_name' => 'Comic Book Guy',
  1252. 'author_email' => 'cbg@androidsdungeon.com',
  1253. 'author_url' => 'http://androidsdungeon.com',
  1254. 'content' => 'Worst Comment Ever!',
  1255. 'date' => '2014-11-07T10:14:25',
  1256. );
  1257. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1258. $request->add_header( 'content-type', 'application/json' );
  1259. $request->set_body( wp_json_encode( $params ) );
  1260. $response = rest_get_server()->dispatch( $request );
  1261. $this->assertSame( 201, $response->get_status() );
  1262. $data = $response->get_data();
  1263. $this->assertSame( 'comment', $data['type'] );
  1264. $comment_id = $data['id'];
  1265. // Make sure the new comment is present in the collection.
  1266. $collection = new WP_REST_Request( 'GET', '/wp/v2/comments' );
  1267. $collection->set_param( 'post', $post_id );
  1268. $collection_response = rest_get_server()->dispatch( $collection );
  1269. $collection_data = $collection_response->get_data();
  1270. $this->assertSame( $comment_id, $collection_data[0]['id'] );
  1271. }
  1272. /**
  1273. * @ticket 38820
  1274. */
  1275. public function test_create_comment_with_invalid_type() {
  1276. $post_id = $this->factory->post->create();
  1277. wp_set_current_user( self::$admin_id );
  1278. $params = array(
  1279. 'post' => $post_id,
  1280. 'author' => self::$admin_id,
  1281. 'author_name' => 'Comic Book Guy',
  1282. 'author_email' => 'cbg@androidsdungeon.com',
  1283. 'author_url' => 'http://androidsdungeon.com',
  1284. 'content' => 'Worst Comment Ever!',
  1285. 'date' => '2014-11-07T10:14:25',
  1286. 'type' => 'foo',
  1287. );
  1288. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1289. $request->add_header( 'content-type', 'application/json' );
  1290. $request->set_body( wp_json_encode( $params ) );
  1291. $response = rest_get_server()->dispatch( $request );
  1292. $this->assertErrorResponse( 'rest_invalid_comment_type', $response, 400 );
  1293. }
  1294. public function test_create_comment_invalid_email() {
  1295. $post_id = $this->factory->post->create();
  1296. wp_set_current_user( self::$admin_id );
  1297. $params = array(
  1298. 'post' => $post_id,
  1299. 'author' => self::$admin_id,
  1300. 'author_name' => 'Comic Book Guy',
  1301. 'author_email' => 'hello:)',
  1302. 'author_url' => 'http://androidsdungeon.com',
  1303. 'content' => 'Worst Comment Ever!',
  1304. 'date' => '2014-11-07T10:14:25',
  1305. );
  1306. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1307. $request->add_header( 'content-type', 'application/json' );
  1308. $request->set_body( wp_json_encode( $params ) );
  1309. $response = rest_get_server()->dispatch( $request );
  1310. $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
  1311. }
  1312. public function test_create_item_current_user() {
  1313. $user_id = $this->factory->user->create(
  1314. array(
  1315. 'role' => 'subscriber',
  1316. 'user_email' => 'lylelanley@example.com',
  1317. 'first_name' => 'Lyle',
  1318. 'last_name' => 'Lanley',
  1319. 'display_name' => 'Lyle Lanley',
  1320. 'user_url' => 'http://simpsons.wikia.com/wiki/Lyle_Lanley',
  1321. )
  1322. );
  1323. wp_set_current_user( $user_id );
  1324. $params = array(
  1325. 'post' => self::$post_id,
  1326. 'content' => "Well sir, there's nothing on earth like a genuine, bona fide, electrified, six-car Monorail!",
  1327. );
  1328. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1329. $request->add_header( 'content-type', 'application/json' );
  1330. $request->set_body( wp_json_encode( $params ) );
  1331. $response = rest_get_server()->dispatch( $request );
  1332. $this->assertSame( 201, $response->get_status() );
  1333. $data = $response->get_data();
  1334. $this->assertSame( $user_id, $data['author'] );
  1335. // Check author data matches.
  1336. $author = get_user_by( 'id', $user_id );
  1337. $comment = get_comment( $data['id'] );
  1338. $this->assertSame( $author->display_name, $comment->comment_author );
  1339. $this->assertSame( $author->user_email, $comment->comment_author_email );
  1340. $this->assertSame( $author->user_url, $comment->comment_author_url );
  1341. }
  1342. public function test_create_comment_other_user() {
  1343. wp_set_current_user( self::$admin_id );
  1344. $params = array(
  1345. 'post' => self::$post_id,
  1346. 'author_name' => 'Homer Jay Simpson',
  1347. 'author_email' => 'chunkylover53@aol.com',
  1348. 'author_url' => 'http://compuglobalhypermeganet.com',
  1349. 'content' => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
  1350. 'author' => self::$subscriber_id,
  1351. );
  1352. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1353. $request->add_header( 'content-type', 'application/json' );
  1354. $request->set_body( wp_json_encode( $params ) );
  1355. $response = rest_get_server()->dispatch( $request );
  1356. $this->assertSame( 201, $response->get_status() );
  1357. $data = $response->get_data();
  1358. $this->assertSame( self::$subscriber_id, $data['author'] );
  1359. $this->assertSame( 'Homer Jay Simpson', $data['author_name'] );
  1360. $this->assertSame( 'chunkylover53@aol.com', $data['author_email'] );
  1361. $this->assertSame( 'http://compuglobalhypermeganet.com', $data['author_url'] );
  1362. }
  1363. public function test_create_comment_other_user_without_permission() {
  1364. wp_set_current_user( self::$subscriber_id );
  1365. $params = array(
  1366. 'post' => self::$post_id,
  1367. 'author_name' => 'Homer Jay Simpson',
  1368. 'author_email' => 'chunkylover53@aol.com',
  1369. 'author_url' => 'http://compuglobalhypermeganet.com',
  1370. 'content' => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
  1371. 'author' => self::$admin_id,
  1372. );
  1373. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1374. $request->add_header( 'content-type', 'application/json' );
  1375. $request->set_body( wp_json_encode( $params ) );
  1376. $response = rest_get_server()->dispatch( $request );
  1377. $this->assertErrorResponse( 'rest_comment_invalid_author', $response, 403 );
  1378. }
  1379. public function test_create_comment_invalid_post() {
  1380. wp_set_current_user( self::$subscriber_id );
  1381. $params = array(
  1382. 'post' => 'some-slug',
  1383. 'author_name' => 'Homer Jay Simpson',
  1384. 'author_email' => 'chunkylover53@aol.com',
  1385. 'author_url' => 'http://compuglobalhypermeganet.com',
  1386. 'content' => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
  1387. 'author' => self::$subscriber_id,
  1388. );
  1389. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1390. $request->add_header( 'content-type', 'application/json' );
  1391. $request->set_body( wp_json_encode( $params ) );
  1392. $response = rest_get_server()->dispatch( $request );
  1393. $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
  1394. }
  1395. public function test_create_comment_status_without_permission() {
  1396. wp_set_current_user( self::$subscriber_id );
  1397. $params = array(
  1398. 'post' => self::$post_id,
  1399. 'author_name' => 'Homer Jay Simpson',
  1400. 'author_email' => 'chunkylover53@aol.com',
  1401. 'author_url' => 'http://compuglobalhypermeganet.com',
  1402. 'content' => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
  1403. 'author' => self::$subscriber_id,
  1404. 'status' => 'approved',
  1405. );
  1406. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1407. $request->add_header( 'content-type', 'application/json' );
  1408. $request->set_body( wp_json_encode( $params ) );
  1409. $response = rest_get_server()->dispatch( $request );
  1410. $this->assertErrorResponse( 'rest_comment_invalid_status', $response, 403 );
  1411. }
  1412. public function test_create_comment_with_status_IP_and_user_agent() {
  1413. $post_id = $this->factory->post->create();
  1414. wp_set_current_user( self::$admin_id );
  1415. $params = array(
  1416. 'post' => $post_id,
  1417. 'author_name' => 'Comic Book Guy',
  1418. 'author_email' => 'cbg@androidsdungeon.com',
  1419. 'author_ip' => '139.130.4.5',
  1420. 'author_url' => 'http://androidsdungeon.com',
  1421. 'author_user_agent' => 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36',
  1422. 'content' => 'Worst Comment Ever!',
  1423. 'status' => 'approved',
  1424. );
  1425. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1426. $request->add_header( 'content-type', 'application/json' );
  1427. $request->set_body( wp_json_encode( $params ) );
  1428. $response = rest_get_server()->dispatch( $request );
  1429. $this->assertSame( 201, $response->get_status() );
  1430. $data = $response->get_data();
  1431. $this->assertSame( 'approved', $data['status'] );
  1432. $this->assertSame( '139.130.4.5', $data['author_ip'] );
  1433. $this->assertSame( 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36', $data['author_user_agent'] );
  1434. }
  1435. public function test_create_comment_user_agent_header() {
  1436. wp_set_current_user( self::$admin_id );
  1437. $params = array(
  1438. 'post' => self::$post_id,
  1439. 'author_name' => 'Homer Jay Simpson',
  1440. 'author_email' => 'chunkylover53@aol.com',
  1441. 'author_url' => 'http://compuglobalhypermeganet.com',
  1442. 'content' => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
  1443. );
  1444. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1445. $request->add_header( 'content-type', 'application/json' );
  1446. $request->add_header( 'user_agent', 'Mozilla/4.0 (compatible; MSIE 5.5; AOL 4.0; Windows 95)' );
  1447. $request->set_body( wp_json_encode( $params ) );
  1448. $response = rest_get_server()->dispatch( $request );
  1449. $this->assertSame( 201, $response->get_status() );
  1450. $data = $response->get_data();
  1451. $new_comment = get_comment( $data['id'] );
  1452. $this->assertSame( 'Mozilla/4.0 (compatible; MSIE 5.5; AOL 4.0; Windows 95)', $new_comment->comment_agent );
  1453. }
  1454. public function test_create_comment_author_ip() {
  1455. wp_set_current_user( self::$admin_id );
  1456. $params = array(
  1457. 'post' => self::$post_id,
  1458. 'author_name' => 'Comic Book Guy',
  1459. 'author_email' => 'cbg@androidsdungeon.com',
  1460. 'author_url' => 'http://androidsdungeon.com',
  1461. 'author_ip' => '127.0.0.3',
  1462. 'content' => 'Worst Comment Ever!',
  1463. 'status' => 'approved',
  1464. );
  1465. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1466. $request->add_header( 'content-type', 'application/json' );
  1467. $request->set_body( wp_json_encode( $params ) );
  1468. $response = rest_get_server()->dispatch( $request );
  1469. $data = $response->get_data();
  1470. $new_comment = get_comment( $data['id'] );
  1471. $this->assertSame( '127.0.0.3', $new_comment->comment_author_IP );
  1472. }
  1473. public function test_create_comment_invalid_author_IP() {
  1474. wp_set_current_user( self::$admin_id );
  1475. $params = array(
  1476. 'post' => self::$post_id,
  1477. 'author_name' => 'Comic Book Guy',
  1478. 'author_email' => 'cbg@androidsdungeon.com',
  1479. 'author_url' => 'http://androidsdungeon.com',
  1480. 'author_ip' => '867.5309',
  1481. 'content' => 'Worst Comment Ever!',
  1482. 'status' => 'approved',
  1483. );
  1484. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1485. $request->add_header( 'content-type', 'application/json' );
  1486. $request->set_body( wp_json_encode( $params ) );
  1487. $response = rest_get_server()->dispatch( $request );
  1488. $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
  1489. }
  1490. public function test_create_comment_author_ip_no_permission() {
  1491. wp_set_current_user( self::$subscriber_id );
  1492. $params = array(
  1493. 'author_name' => 'Comic Book Guy',
  1494. 'author_email' => 'cbg@androidsdungeon.com',
  1495. 'author_url' => 'http://androidsdungeon.com',
  1496. 'author_ip' => '10.0.10.1',
  1497. 'content' => 'Worst Comment Ever!',
  1498. 'status' => 'approved',
  1499. );
  1500. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1501. $request->add_header( 'content-type', 'application/json' );
  1502. $request->set_body( wp_json_encode( $params ) );
  1503. $response = rest_get_server()->dispatch( $request );
  1504. $this->assertErrorResponse( 'rest_comment_invalid_author_ip', $response, 403 );
  1505. }
  1506. public function test_create_comment_author_ip_defaults_to_remote_addr() {
  1507. wp_set_current_user( self::$admin_id );
  1508. $_SERVER['REMOTE_ADDR'] = '127.0.0.2';
  1509. $params = array(
  1510. 'post' => self::$post_id,
  1511. 'author_name' => 'Comic Book Guy',
  1512. 'author_email' => 'cbg@androidsdungeon.com',
  1513. 'author_url' => 'http://androidsdungeon.com',
  1514. 'content' => 'Worst Comment Ever!',
  1515. );
  1516. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1517. $request->add_header( 'content-type', 'application/json' );
  1518. $request->set_body( wp_json_encode( $params ) );
  1519. $response = rest_get_server()->dispatch( $request );
  1520. $data = $response->get_data();
  1521. $new_comment = get_comment( $data['id'] );
  1522. $this->assertSame( '127.0.0.2', $new_comment->comment_author_IP );
  1523. }
  1524. public function test_create_comment_no_post_id() {
  1525. wp_set_current_user( self::$admin_id );
  1526. $params = array(
  1527. 'author_name' => 'Comic Book Guy',
  1528. 'author_email' => 'cbg@androidsdungeon.com',
  1529. 'author_url' => 'http://androidsdungeon.com',
  1530. 'content' => 'Worst Comment Ever!',
  1531. 'status' => 'approved',
  1532. );
  1533. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1534. $request->add_header( 'content-type', 'application/json' );
  1535. $request->set_body( wp_json_encode( $params ) );
  1536. $response = rest_get_server()->dispatch( $request );
  1537. $this->assertErrorResponse( 'rest_comment_invalid_post_id', $response, 403 );
  1538. }
  1539. public function test_create_comment_no_post_id_no_permission() {
  1540. wp_set_current_user( self::$subscriber_id );
  1541. $params = array(
  1542. 'author_name' => 'Homer Jay Simpson',
  1543. 'author_email' => 'chunkylover53@aol.com',
  1544. 'author_url' => 'http://compuglobalhypermeganet.com',
  1545. 'content' => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
  1546. 'author' => self::$subscriber_id,
  1547. );
  1548. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1549. $request->add_header( 'content-type', 'application/json' );
  1550. $request->set_body( wp_json_encode( $params ) );
  1551. $response = rest_get_server()->dispatch( $request );
  1552. $this->assertErrorResponse( 'rest_comment_invalid_post_id', $response, 403 );
  1553. }
  1554. public function test_create_comment_invalid_post_id() {
  1555. wp_set_current_user( self::$admin_id );
  1556. $params = array(
  1557. 'author_name' => 'Homer Jay Simpson',
  1558. 'author_email' => 'chunkylover53@aol.com',
  1559. 'author_url' => 'http://compuglobalhypermeganet.com',
  1560. 'content' => 'Here\’s to alcohol: the cause of, and solution to, all of life\’s problems.',
  1561. 'status' => 'approved',
  1562. 'post' => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER,
  1563. );
  1564. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1565. $request->add_header( 'content-type', 'application/json' );
  1566. $request->set_body( wp_json_encode( $params ) );
  1567. $response = rest_get_server()->dispatch( $request );
  1568. $this->assertErrorResponse( 'rest_comment_invalid_post_id', $response, 403 );
  1569. }
  1570. public function test_create_comment_draft_post() {
  1571. wp_set_current_user( self::$subscriber_id );
  1572. $params = array(
  1573. 'post' => self::$draft_id,
  1574. 'author_name' => 'Ishmael',
  1575. 'author_email' => 'herman-melville@earthlink.net',
  1576. 'author_url' => 'https://en.wikipedia.org/wiki/Herman_Melville',
  1577. 'content' => 'Call me Ishmael.',
  1578. 'author' => self::$subscriber_id,
  1579. );
  1580. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1581. $request->add_header( 'content-type', 'application/json' );
  1582. $request->set_body( wp_json_encode( $params ) );
  1583. $response = rest_get_server()->dispatch( $request );
  1584. $this->assertErrorResponse( 'rest_comment_draft_post', $response, 403 );
  1585. }
  1586. public function test_create_comment_trash_post() {
  1587. wp_set_current_user( self::$subscriber_id );
  1588. $params = array(
  1589. 'post' => self::$trash_id,
  1590. 'author_name' => 'Ishmael',
  1591. 'author_email' => 'herman-melville@earthlink.net',
  1592. 'author_url' => 'https://en.wikipedia.org/wiki/Herman_Melville',
  1593. 'content' => 'Call me Ishmael.',
  1594. 'author' => self::$subscriber_id,
  1595. );
  1596. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1597. $request->add_header( 'content-type', 'application/json' );
  1598. $request->set_body( wp_json_encode( $params ) );
  1599. $response = rest_get_server()->dispatch( $request );
  1600. $this->assertErrorResponse( 'rest_comment_trash_post', $response, 403 );
  1601. }
  1602. public function test_create_comment_private_post_invalid_permission() {
  1603. wp_set_current_user( self::$subscriber_id );
  1604. $params = array(
  1605. 'post' => self::$private_id,
  1606. 'author_name' => 'Homer Jay Simpson',
  1607. 'author_email' => 'chunkylover53@aol.com',
  1608. 'author_url' => 'http://compuglobalhypermeganet.com',
  1609. 'content' => 'I\’d be a vegetarian if bacon grew on trees.',
  1610. 'author' => self::$subscriber_id,
  1611. );
  1612. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1613. $request->add_header( 'content-type', 'application/json' );
  1614. $request->set_body( wp_json_encode( $params ) );
  1615. $response = rest_get_server()->dispatch( $request );
  1616. $this->assertErrorResponse( 'rest_cannot_read_post', $response, 403 );
  1617. }
  1618. public function test_create_comment_password_post_invalid_permission() {
  1619. wp_set_current_user( self::$subscriber_id );
  1620. $params = array(
  1621. 'post' => self::$password_id,
  1622. 'author_name' => 'Homer Jay Simpson',
  1623. 'author_email' => 'chunkylover53@aol.com',
  1624. 'author_url' => 'http://compuglobalhypermeganet.com',
  1625. 'content' => 'I\’d be a vegetarian if bacon grew on trees.',
  1626. 'author' => self::$subscriber_id,
  1627. );
  1628. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1629. $request->add_header( 'content-type', 'application/json' );
  1630. $request->set_body( wp_json_encode( $params ) );
  1631. $response = rest_get_server()->dispatch( $request );
  1632. $this->assertErrorResponse( 'rest_cannot_read_post', $response, 403 );
  1633. }
  1634. public function test_create_item_duplicate() {
  1635. wp_set_current_user( self::$subscriber_id );
  1636. $this->factory->comment->create(
  1637. array(
  1638. 'comment_post_ID' => self::$post_id,
  1639. 'comment_author' => 'Guy N. Cognito',
  1640. 'comment_author_email' => 'chunkylover53@aol.co.uk',
  1641. 'comment_content' => 'Homer? Who is Homer? My name is Guy N. Cognito.',
  1642. )
  1643. );
  1644. $params = array(
  1645. 'post' => self::$post_id,
  1646. 'author_name' => 'Guy N. Cognito',
  1647. 'author_email' => 'chunkylover53@aol.co.uk',
  1648. 'content' => 'Homer? Who is Homer? My name is Guy N. Cognito.',
  1649. );
  1650. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1651. $request->add_header( 'content-type', 'application/json' );
  1652. $request->set_body( wp_json_encode( $params ) );
  1653. $response = rest_get_server()->dispatch( $request );
  1654. $this->assertSame( 409, $response->get_status() );
  1655. }
  1656. public function test_create_comment_closed() {
  1657. $post_id = $this->factory->post->create(
  1658. array(
  1659. 'comment_status' => 'closed',
  1660. )
  1661. );
  1662. wp_set_current_user( self::$subscriber_id );
  1663. $params = array(
  1664. 'post' => $post_id,
  1665. );
  1666. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1667. $request->add_header( 'content-type', 'application/json' );
  1668. $request->set_body( wp_json_encode( $params ) );
  1669. $response = rest_get_server()->dispatch( $request );
  1670. $this->assertSame( 403, $response->get_status() );
  1671. }
  1672. public function test_create_comment_require_login() {
  1673. wp_set_current_user( 0 );
  1674. update_option( 'comment_registration', 1 );
  1675. add_filter( 'rest_allow_anonymous_comments', '__return_true' );
  1676. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1677. $request->set_param( 'post', self::$post_id );
  1678. $response = rest_get_server()->dispatch( $request );
  1679. $this->assertSame( 401, $response->get_status() );
  1680. $data = $response->get_data();
  1681. $this->assertSame( 'rest_comment_login_required', $data['code'] );
  1682. }
  1683. public function test_create_item_invalid_author() {
  1684. wp_set_current_user( self::$admin_id );
  1685. $params = array(
  1686. 'post' => self::$post_id,
  1687. 'author' => REST_TESTS_IMPOSSIBLY_HIGH_NUMBER,
  1688. 'content' => 'It\'s all over\, people! We don\'t have a prayer!',
  1689. );
  1690. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1691. $request->add_header( 'content-type', 'application/json' );
  1692. $request->set_body( wp_json_encode( $params ) );
  1693. $response = rest_get_server()->dispatch( $request );
  1694. $this->assertErrorResponse( 'rest_comment_author_invalid', $response, 400 );
  1695. }
  1696. public function test_create_item_pull_author_info() {
  1697. wp_set_current_user( self::$admin_id );
  1698. $author = new WP_User( self::$author_id );
  1699. $params = array(
  1700. 'post' => self::$post_id,
  1701. 'author' => self::$author_id,
  1702. 'content' => 'It\'s all over\, people! We don\'t have a prayer!',
  1703. );
  1704. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1705. $request->add_header( 'content-type', 'application/json' );
  1706. $request->set_body( wp_json_encode( $params ) );
  1707. $response = rest_get_server()->dispatch( $request );
  1708. $result = $response->get_data();
  1709. $this->assertSame( self::$author_id, $result['author'] );
  1710. $this->assertSame( 'Sea Captain', $result['author_name'] );
  1711. $this->assertSame( 'captain@thefryingdutchman.com', $result['author_email'] );
  1712. $this->assertSame( 'http://thefryingdutchman.com', $result['author_url'] );
  1713. }
  1714. public function test_create_comment_two_times() {
  1715. add_filter( 'rest_allow_anonymous_comments', '__return_true' );
  1716. $params = array(
  1717. 'post' => self::$post_id,
  1718. 'author_name' => 'Comic Book Guy',
  1719. 'author_email' => 'cbg@androidsdungeon.com',
  1720. 'author_url' => 'http://androidsdungeon.com',
  1721. 'content' => 'Worst Comment Ever!',
  1722. );
  1723. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1724. $request->add_header( 'content-type', 'application/json' );
  1725. $request->set_body( wp_json_encode( $params ) );
  1726. $response = rest_get_server()->dispatch( $request );
  1727. $this->assertSame( 201, $response->get_status() );
  1728. $params = array(
  1729. 'post' => self::$post_id,
  1730. 'author_name' => 'Comic Book Guy',
  1731. 'author_email' => 'cbg@androidsdungeon.com',
  1732. 'author_url' => 'http://androidsdungeon.com',
  1733. 'content' => 'Shakes fist at sky',
  1734. );
  1735. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1736. $request->add_header( 'content-type', 'application/json' );
  1737. $request->set_body( wp_json_encode( $params ) );
  1738. $response = rest_get_server()->dispatch( $request );
  1739. $this->assertSame( 400, $response->get_status() );
  1740. }
  1741. public function anonymous_comments_callback_null() {
  1742. // I'm a plugin developer who forgot to include a return value
  1743. // for some code path in my 'rest_allow_anonymous_comments' filter.
  1744. }
  1745. public function test_allow_anonymous_comments_null() {
  1746. add_filter( 'rest_allow_anonymous_comments', array( $this, 'anonymous_comments_callback_null' ), 10, 2 );
  1747. $params = array(
  1748. 'post' => self::$post_id,
  1749. 'author_name' => 'Comic Book Guy',
  1750. 'author_email' => 'cbg@androidsdungeon.com',
  1751. 'author_url' => 'http://androidsdungeon.com',
  1752. 'content' => 'Worst Comment Ever!',
  1753. );
  1754. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1755. $request->add_header( 'content-type', 'application/json' );
  1756. $request->set_body( wp_json_encode( $params ) );
  1757. $response = rest_get_server()->dispatch( $request );
  1758. remove_filter( 'rest_allow_anonymous_comments', array( $this, 'anonymous_comments_callback_null' ), 10, 2 );
  1759. $this->assertErrorResponse( 'rest_comment_login_required', $response, 401 );
  1760. }
  1761. /**
  1762. * @ticket 38477
  1763. */
  1764. public function test_create_comment_author_name_too_long() {
  1765. wp_set_current_user( self::$subscriber_id );
  1766. $params = array(
  1767. 'post' => self::$post_id,
  1768. 'author_name' => rand_long_str( 246 ),
  1769. 'author_email' => 'murphy@gingivitis.com',
  1770. 'author_url' => 'http://jazz.gingivitis.com',
  1771. 'content' => 'This isn\'t a saxophone. It\'s an umbrella.',
  1772. 'date' => '1995-04-30T10:22:00',
  1773. );
  1774. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1775. $request->add_header( 'content-type', 'application/json' );
  1776. $request->set_body( wp_json_encode( $params ) );
  1777. $response = rest_get_server()->dispatch( $request );
  1778. $this->assertErrorResponse( 'comment_author_column_length', $response, 400 );
  1779. }
  1780. /**
  1781. * @ticket 38477
  1782. */
  1783. public function test_create_comment_author_email_too_long() {
  1784. wp_set_current_user( self::$subscriber_id );
  1785. $params = array(
  1786. 'post' => self::$post_id,
  1787. 'author_name' => 'Bleeding Gums Murphy',
  1788. 'author_email' => 'murphy@' . rand_long_str( 190 ) . '.com',
  1789. 'author_url' => 'http://jazz.gingivitis.com',
  1790. 'content' => 'This isn\'t a saxophone. It\'s an umbrella.',
  1791. 'date' => '1995-04-30T10:22:00',
  1792. );
  1793. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1794. $request->add_header( 'content-type', 'application/json' );
  1795. $request->set_body( wp_json_encode( $params ) );
  1796. $response = rest_get_server()->dispatch( $request );
  1797. $this->assertErrorResponse( 'comment_author_email_column_length', $response, 400 );
  1798. }
  1799. /**
  1800. * @ticket 38477
  1801. */
  1802. public function test_create_comment_author_url_too_long() {
  1803. wp_set_current_user( self::$subscriber_id );
  1804. $params = array(
  1805. 'post' => self::$post_id,
  1806. 'author_name' => 'Bleeding Gums Murphy',
  1807. 'author_email' => 'murphy@gingivitis.com',
  1808. 'author_url' => 'http://jazz.' . rand_long_str( 185 ) . '.com',
  1809. 'content' => 'This isn\'t a saxophone. It\'s an umbrella.',
  1810. 'date' => '1995-04-30T10:22:00',
  1811. );
  1812. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1813. $request->add_header( 'content-type', 'application/json' );
  1814. $request->set_body( wp_json_encode( $params ) );
  1815. $response = rest_get_server()->dispatch( $request );
  1816. $this->assertErrorResponse( 'comment_author_url_column_length', $response, 400 );
  1817. }
  1818. /**
  1819. * @ticket 38477
  1820. */
  1821. public function test_create_comment_content_too_long() {
  1822. wp_set_current_user( self::$subscriber_id );
  1823. $params = array(
  1824. 'post' => self::$post_id,
  1825. 'author_name' => 'Bleeding Gums Murphy',
  1826. 'author_email' => 'murphy@gingivitis.com',
  1827. 'author_url' => 'http://jazz.gingivitis.com',
  1828. 'content' => rand_long_str( 66525 ),
  1829. 'date' => '1995-04-30T10:22:00',
  1830. );
  1831. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1832. $request->add_header( 'content-type', 'application/json' );
  1833. $request->set_body( wp_json_encode( $params ) );
  1834. $response = rest_get_server()->dispatch( $request );
  1835. $this->assertErrorResponse( 'comment_content_column_length', $response, 400 );
  1836. }
  1837. public function test_create_comment_without_password() {
  1838. wp_set_current_user( self::$subscriber_id );
  1839. $params = array(
  1840. 'post' => self::$password_id,
  1841. 'author_name' => 'Bleeding Gums Murphy',
  1842. 'author_email' => 'murphy@gingivitis.com',
  1843. 'author_url' => 'http://jazz.gingivitis.com',
  1844. 'content' => 'This isn\'t a saxophone. It\'s an umbrella.',
  1845. );
  1846. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1847. $request->add_header( 'content-type', 'application/json' );
  1848. $request->set_body( wp_json_encode( $params ) );
  1849. $response = rest_get_server()->dispatch( $request );
  1850. $this->assertErrorResponse( 'rest_cannot_read_post', $response, 403 );
  1851. }
  1852. public function test_create_comment_with_password() {
  1853. add_filter( 'rest_allow_anonymous_comments', '__return_true' );
  1854. $params = array(
  1855. 'post' => self::$password_id,
  1856. 'author_name' => 'Bleeding Gums Murphy',
  1857. 'author_email' => 'murphy@gingivitis.com',
  1858. 'author_url' => 'http://jazz.gingivitis.com',
  1859. 'content' => 'This isn\'t a saxophone. It\'s an umbrella.',
  1860. 'password' => 'toomanysecrets',
  1861. );
  1862. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  1863. $request->add_header( 'content-type', 'application/json' );
  1864. $request->set_body( wp_json_encode( $params ) );
  1865. $response = rest_get_server()->dispatch( $request );
  1866. $this->assertSame( 201, $response->get_status() );
  1867. }
  1868. public function test_update_item() {
  1869. $post_id = $this->factory->post->create();
  1870. wp_set_current_user( self::$admin_id );
  1871. $params = array(
  1872. 'author' => self::$subscriber_id,
  1873. 'author_name' => 'Disco Stu',
  1874. 'author_url' => 'http://stusdisco.com',
  1875. 'author_email' => 'stu@stusdisco.com',
  1876. 'author_ip' => '4.4.4.4',
  1877. 'content' => 'Testing.',
  1878. 'date' => '2014-11-07T10:14:25',
  1879. 'post' => $post_id,
  1880. );
  1881. $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
  1882. $request->add_header( 'content-type', 'application/json' );
  1883. $request->set_body( wp_json_encode( $params ) );
  1884. $response = rest_get_server()->dispatch( $request );
  1885. $this->assertSame( 200, $response->get_status() );
  1886. $comment = $response->get_data();
  1887. $updated = get_comment( self::$approved_id );
  1888. $this->assertSame( $params['content'], $comment['content']['raw'] );
  1889. $this->assertSame( $params['author'], $comment['author'] );
  1890. $this->assertSame( $params['author_name'], $comment['author_name'] );
  1891. $this->assertSame( $params['author_url'], $comment['author_url'] );
  1892. $this->assertSame( $params['author_email'], $comment['author_email'] );
  1893. $this->assertSame( $params['author_ip'], $comment['author_ip'] );
  1894. $this->assertSame( $params['post'], $comment['post'] );
  1895. $this->assertSame( mysql_to_rfc3339( $updated->comment_date ), $comment['date'] );
  1896. $this->assertSame( '2014-11-07T10:14:25', $comment['date'] );
  1897. }
  1898. /**
  1899. * @dataProvider comment_dates_provider
  1900. */
  1901. public function test_update_comment_date( $params, $results ) {
  1902. wp_set_current_user( self::$editor_id );
  1903. update_option( 'timezone_string', $params['timezone_string'] );
  1904. $comment_id = $this->factory->comment->create();
  1905. $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $comment_id ) );
  1906. if ( isset( $params['date'] ) ) {
  1907. $request->set_param( 'date', $params['date'] );
  1908. }
  1909. if ( isset( $params['date_gmt'] ) ) {
  1910. $request->set_param( 'date_gmt', $params['date_gmt'] );
  1911. }
  1912. $response = rest_get_server()->dispatch( $request );
  1913. update_option( 'timezone_string', '' );
  1914. $this->assertSame( 200, $response->get_status() );
  1915. $data = $response->get_data();
  1916. $comment = get_comment( $data['id'] );
  1917. $this->assertSame( $results['date'], $data['date'] );
  1918. $comment_date = str_replace( 'T', ' ', $results['date'] );
  1919. $this->assertSame( $comment_date, $comment->comment_date );
  1920. $this->assertSame( $results['date_gmt'], $data['date_gmt'] );
  1921. $comment_date_gmt = str_replace( 'T', ' ', $results['date_gmt'] );
  1922. $this->assertSame( $comment_date_gmt, $comment->comment_date_gmt );
  1923. }
  1924. public function test_update_item_no_content() {
  1925. $post_id = $this->factory->post->create();
  1926. wp_set_current_user( self::$admin_id );
  1927. $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
  1928. $request->set_param( 'author_email', 'another@email.com' );
  1929. // Sending a request without content is fine.
  1930. $response = rest_get_server()->dispatch( $request );
  1931. $this->assertSame( 200, $response->get_status() );
  1932. // Sending a request with empty comment is not fine.
  1933. $request->set_param( 'author_email', 'yetanother@email.com' );
  1934. $request->set_param( 'content', '' );
  1935. $response = rest_get_server()->dispatch( $request );
  1936. $this->assertErrorResponse( 'rest_comment_content_invalid', $response, 400 );
  1937. }
  1938. public function test_update_item_no_change() {
  1939. $comment = get_comment( self::$approved_id );
  1940. wp_set_current_user( self::$admin_id );
  1941. $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
  1942. $request->set_param( 'post', $comment->comment_post_ID );
  1943. // Run twice to make sure that the update still succeeds
  1944. // even if no DB rows are updated.
  1945. $response = rest_get_server()->dispatch( $request );
  1946. $this->assertSame( 200, $response->get_status() );
  1947. $response = rest_get_server()->dispatch( $request );
  1948. $this->assertSame( 200, $response->get_status() );
  1949. }
  1950. public function test_update_comment_status() {
  1951. wp_set_current_user( self::$admin_id );
  1952. $comment_id = $this->factory->comment->create(
  1953. array(
  1954. 'comment_approved' => 0,
  1955. 'comment_post_ID' => self::$post_id,
  1956. )
  1957. );
  1958. $params = array(
  1959. 'status' => 'approve',
  1960. );
  1961. $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $comment_id ) );
  1962. $request->add_header( 'content-type', 'application/json' );
  1963. $request->set_body( wp_json_encode( $params ) );
  1964. $response = rest_get_server()->dispatch( $request );
  1965. $this->assertSame( 200, $response->get_status() );
  1966. $comment = $response->get_data();
  1967. $updated = get_comment( $comment_id );
  1968. $this->assertSame( 'approved', $comment['status'] );
  1969. $this->assertEquals( 1, $updated->comment_approved );
  1970. }
  1971. public function test_update_comment_field_does_not_use_default_values() {
  1972. wp_set_current_user( self::$admin_id );
  1973. $comment_id = $this->factory->comment->create(
  1974. array(
  1975. 'comment_approved' => 0,
  1976. 'comment_post_ID' => self::$post_id,
  1977. 'comment_content' => 'some content',
  1978. )
  1979. );
  1980. $params = array(
  1981. 'status' => 'approve',
  1982. );
  1983. $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $comment_id ) );
  1984. $request->add_header( 'content-type', 'application/json' );
  1985. $request->set_body( wp_json_encode( $params ) );
  1986. $response = rest_get_server()->dispatch( $request );
  1987. $this->assertSame( 200, $response->get_status() );
  1988. $comment = $response->get_data();
  1989. $updated = get_comment( $comment_id );
  1990. $this->assertSame( 'approved', $comment['status'] );
  1991. $this->assertEquals( 1, $updated->comment_approved );
  1992. $this->assertSame( 'some content', $updated->comment_content );
  1993. }
  1994. public function test_update_comment_date_gmt() {
  1995. wp_set_current_user( self::$admin_id );
  1996. $params = array(
  1997. 'date_gmt' => '2015-05-07T10:14:25',
  1998. 'content' => 'I\'ll be deep in the cold, cold ground before I recognize Missouri.',
  1999. );
  2000. $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
  2001. $request->add_header( 'content-type', 'application/json' );
  2002. $request->set_body( wp_json_encode( $params ) );
  2003. $response = rest_get_server()->dispatch( $request );
  2004. $this->assertSame( 200, $response->get_status() );
  2005. $comment = $response->get_data();
  2006. $updated = get_comment( self::$approved_id );
  2007. $this->assertSame( $params['date_gmt'], $comment['date_gmt'] );
  2008. $this->assertSame( $params['date_gmt'], mysql_to_rfc3339( $updated->comment_date_gmt ) );
  2009. }
  2010. public function test_update_comment_author_email_only() {
  2011. wp_set_current_user( self::$editor_id );
  2012. update_option( 'require_name_email', 1 );
  2013. $params = array(
  2014. 'post' => self::$post_id,
  2015. 'author_email' => 'ekrabappel@springfield-elementary.edu',
  2016. 'content' => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
  2017. );
  2018. $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
  2019. $request->add_header( 'content-type', 'application/json' );
  2020. $request->set_body( wp_json_encode( $params ) );
  2021. $response = rest_get_server()->dispatch( $request );
  2022. $this->assertSame( 200, $response->get_status() );
  2023. }
  2024. public function test_update_comment_empty_author_name() {
  2025. wp_set_current_user( self::$editor_id );
  2026. update_option( 'require_name_email', 1 );
  2027. $params = array(
  2028. 'author_name' => '',
  2029. 'author_email' => 'ekrabappel@springfield-elementary.edu',
  2030. 'post' => self::$post_id,
  2031. 'content' => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
  2032. );
  2033. $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
  2034. $request->add_header( 'content-type', 'application/json' );
  2035. $request->set_body( wp_json_encode( $params ) );
  2036. $response = rest_get_server()->dispatch( $request );
  2037. $this->assertSame( 200, $response->get_status() );
  2038. }
  2039. public function test_update_comment_author_name_only() {
  2040. wp_set_current_user( self::$admin_id );
  2041. update_option( 'require_name_email', 1 );
  2042. $params = array(
  2043. 'post' => self::$post_id,
  2044. 'author_name' => 'Edna Krabappel',
  2045. 'content' => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
  2046. );
  2047. $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
  2048. $request->add_header( 'content-type', 'application/json' );
  2049. $request->set_body( wp_json_encode( $params ) );
  2050. $response = rest_get_server()->dispatch( $request );
  2051. $this->assertSame( 200, $response->get_status() );
  2052. }
  2053. public function test_update_comment_empty_author_email() {
  2054. wp_set_current_user( self::$admin_id );
  2055. update_option( 'require_name_email', 1 );
  2056. $params = array(
  2057. 'post' => self::$post_id,
  2058. 'author_name' => 'Edna Krabappel',
  2059. 'author_email' => '',
  2060. 'content' => 'Now, I don\'t want you to worry class. These tests will have no affect on your grades. They merely determine your future social status and financial success. If any.',
  2061. );
  2062. $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
  2063. $request->add_header( 'content-type', 'application/json' );
  2064. $request->set_body( wp_json_encode( $params ) );
  2065. $response = rest_get_server()->dispatch( $request );
  2066. $this->assertSame( 200, $response->get_status() );
  2067. }
  2068. public function test_update_comment_author_email_too_short() {
  2069. wp_set_current_user( self::$admin_id );
  2070. $params = array(
  2071. 'post' => self::$post_id,
  2072. 'author_name' => 'Homer J. Simpson',
  2073. 'author_email' => 'a@b',
  2074. 'content' => 'in this house, we obey the laws of thermodynamics!',
  2075. );
  2076. $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
  2077. $request->add_header( 'content-type', 'application/json' );
  2078. $request->set_body( wp_json_encode( $params ) );
  2079. $response = rest_get_server()->dispatch( $request );
  2080. $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
  2081. $data = $response->get_data();
  2082. $this->assertArrayHasKey( 'author_email', $data['data']['params'] );
  2083. }
  2084. public function test_update_comment_invalid_type() {
  2085. wp_set_current_user( self::$admin_id );
  2086. $params = array(
  2087. 'type' => 'trackback',
  2088. );
  2089. $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
  2090. $request->add_header( 'content-type', 'application/json' );
  2091. $request->set_body( wp_json_encode( $params ) );
  2092. $response = rest_get_server()->dispatch( $request );
  2093. $this->assertErrorResponse( 'rest_comment_invalid_type', $response, 404 );
  2094. }
  2095. public function test_update_comment_with_raw_property() {
  2096. wp_set_current_user( self::$admin_id );
  2097. $params = array(
  2098. 'content' => array(
  2099. 'raw' => 'What the heck kind of name is Persephone?',
  2100. ),
  2101. );
  2102. $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
  2103. $request->add_header( 'content-type', 'application/json' );
  2104. $request->set_body( wp_json_encode( $params ) );
  2105. $response = rest_get_server()->dispatch( $request );
  2106. $this->assertSame( 200, $response->get_status() );
  2107. $comment = $response->get_data();
  2108. $updated = get_comment( self::$approved_id );
  2109. $this->assertSame( $params['content']['raw'], $updated->comment_content );
  2110. }
  2111. public function test_update_item_invalid_date() {
  2112. wp_set_current_user( self::$admin_id );
  2113. $params = array(
  2114. 'content' => rand_str(),
  2115. 'date' => rand_str(),
  2116. );
  2117. $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
  2118. $request->add_header( 'content-type', 'application/json' );
  2119. $request->set_body( wp_json_encode( $params ) );
  2120. $response = rest_get_server()->dispatch( $request );
  2121. $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
  2122. }
  2123. public function test_update_item_invalid_date_gmt() {
  2124. wp_set_current_user( self::$admin_id );
  2125. $params = array(
  2126. 'content' => rand_str(),
  2127. 'date_gmt' => rand_str(),
  2128. );
  2129. $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
  2130. $request->add_header( 'content-type', 'application/json' );
  2131. $request->set_body( wp_json_encode( $params ) );
  2132. $response = rest_get_server()->dispatch( $request );
  2133. $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
  2134. }
  2135. public function test_update_comment_invalid_id() {
  2136. wp_set_current_user( self::$subscriber_id );
  2137. $params = array(
  2138. 'content' => 'Oh, they have the internet on computers now!',
  2139. );
  2140. $request = new WP_REST_Request( 'PUT', '/wp/v2/comments/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
  2141. $request->add_header( 'content-type', 'application/json' );
  2142. $request->set_body( wp_json_encode( $params ) );
  2143. $response = rest_get_server()->dispatch( $request );
  2144. $this->assertErrorResponse( 'rest_comment_invalid_id', $response, 404 );
  2145. }
  2146. public function test_update_comment_invalid_post_id() {
  2147. wp_set_current_user( self::$admin_id );
  2148. $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
  2149. $request->set_param( 'post', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER );
  2150. $response = rest_get_server()->dispatch( $request );
  2151. $this->assertErrorResponse( 'rest_comment_invalid_post_id', $response, 403 );
  2152. }
  2153. public function test_update_comment_invalid_permission() {
  2154. add_filter( 'rest_allow_anonymous_comments', '__return_true' );
  2155. $params = array(
  2156. 'content' => 'Disco Stu likes disco music.',
  2157. );
  2158. $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$hold_id ) );
  2159. $request->add_header( 'content-type', 'application/json' );
  2160. $request->set_body( wp_json_encode( $params ) );
  2161. $response = rest_get_server()->dispatch( $request );
  2162. $this->assertErrorResponse( 'rest_cannot_edit', $response, 401 );
  2163. }
  2164. /**
  2165. * @ticket 47024
  2166. */
  2167. public function test_update_comment_when_can_moderate_comments() {
  2168. wp_set_current_user( self::$moderator_id );
  2169. $params = array(
  2170. 'content' => 'Updated comment.',
  2171. 'date' => '2019-10-07T23:14:25',
  2172. );
  2173. $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
  2174. $request->add_header( 'content-type', 'application/json' );
  2175. $request->set_body( wp_json_encode( $params ) );
  2176. $response = rest_get_server()->dispatch( $request );
  2177. $this->assertSame( 200, $response->get_status() );
  2178. $comment = $response->get_data();
  2179. $updated = get_comment( self::$approved_id );
  2180. $this->assertSame( $params['content'], $updated->comment_content );
  2181. $this->assertSame( self::$post_id, $comment['post'] );
  2182. $this->assertSame( '2019-10-07T23:14:25', $comment['date'] );
  2183. }
  2184. public function test_update_comment_private_post_invalid_permission() {
  2185. $private_comment_id = $this->factory->comment->create(
  2186. array(
  2187. 'comment_approved' => 1,
  2188. 'comment_post_ID' => self::$private_id,
  2189. 'user_id' => 0,
  2190. )
  2191. );
  2192. wp_set_current_user( self::$subscriber_id );
  2193. $params = array(
  2194. 'content' => 'Disco Stu likes disco music.',
  2195. );
  2196. $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $private_comment_id ) );
  2197. $request->add_header( 'content-type', 'application/json' );
  2198. $request->set_body( wp_json_encode( $params ) );
  2199. $response = rest_get_server()->dispatch( $request );
  2200. $this->assertErrorResponse( 'rest_cannot_edit', $response, 403 );
  2201. }
  2202. public function test_update_comment_with_children_link() {
  2203. wp_set_current_user( self::$admin_id );
  2204. $comment_id_1 = $this->factory->comment->create(
  2205. array(
  2206. 'comment_approved' => 1,
  2207. 'comment_post_ID' => self::$post_id,
  2208. 'user_id' => self::$subscriber_id,
  2209. )
  2210. );
  2211. $child_comment = $this->factory->comment->create(
  2212. array(
  2213. 'comment_approved' => 1,
  2214. 'comment_post_ID' => self::$post_id,
  2215. 'user_id' => self::$subscriber_id,
  2216. )
  2217. );
  2218. // Check if comment 1 does not have the child link.
  2219. $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $comment_id_1 ) );
  2220. $response = rest_get_server()->dispatch( $request );
  2221. $this->assertSame( 200, $response->get_status() );
  2222. $this->assertArrayNotHasKey( 'children', $response->get_links() );
  2223. // Change the comment parent.
  2224. $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%s', $child_comment ) );
  2225. $request->set_param( 'parent', $comment_id_1 );
  2226. $request->set_param( 'content', rand_str() );
  2227. $response = rest_get_server()->dispatch( $request );
  2228. $this->assertSame( 200, $response->get_status() );
  2229. // Check if comment 1 now has the child link.
  2230. $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $comment_id_1 ) );
  2231. $response = rest_get_server()->dispatch( $request );
  2232. $this->assertSame( 200, $response->get_status() );
  2233. $this->assertArrayHasKey( 'children', $response->get_links() );
  2234. }
  2235. /**
  2236. * @ticket 38477
  2237. */
  2238. public function test_update_comment_author_name_too_long() {
  2239. wp_set_current_user( self::$admin_id );
  2240. $params = array(
  2241. 'author_name' => rand_long_str( 246 ),
  2242. 'content' => 'This isn\'t a saxophone. It\'s an umbrella.',
  2243. );
  2244. $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
  2245. $request->add_header( 'content-type', 'application/json' );
  2246. $request->set_body( wp_json_encode( $params ) );
  2247. $response = rest_get_server()->dispatch( $request );
  2248. $this->assertErrorResponse( 'comment_author_column_length', $response, 400 );
  2249. }
  2250. /**
  2251. * @ticket 38477
  2252. */
  2253. public function test_update_comment_author_email_too_long() {
  2254. wp_set_current_user( self::$admin_id );
  2255. $params = array(
  2256. 'author_email' => 'murphy@' . rand_long_str( 190 ) . '.com',
  2257. 'content' => 'This isn\'t a saxophone. It\'s an umbrella.',
  2258. );
  2259. $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
  2260. $request->add_header( 'content-type', 'application/json' );
  2261. $request->set_body( wp_json_encode( $params ) );
  2262. $response = rest_get_server()->dispatch( $request );
  2263. $this->assertErrorResponse( 'comment_author_email_column_length', $response, 400 );
  2264. }
  2265. /**
  2266. * @ticket 38477
  2267. */
  2268. public function test_update_comment_author_url_too_long() {
  2269. wp_set_current_user( self::$admin_id );
  2270. $params = array(
  2271. 'author_url' => 'http://jazz.' . rand_long_str( 185 ) . '.com',
  2272. 'content' => 'This isn\'t a saxophone. It\'s an umbrella.',
  2273. );
  2274. $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
  2275. $request->add_header( 'content-type', 'application/json' );
  2276. $request->set_body( wp_json_encode( $params ) );
  2277. $response = rest_get_server()->dispatch( $request );
  2278. $this->assertErrorResponse( 'comment_author_url_column_length', $response, 400 );
  2279. }
  2280. /**
  2281. * @ticket 38477
  2282. */
  2283. public function test_update_comment_content_too_long() {
  2284. wp_set_current_user( self::$admin_id );
  2285. $params = array(
  2286. 'content' => rand_long_str( 66525 ),
  2287. );
  2288. $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
  2289. $request->add_header( 'content-type', 'application/json' );
  2290. $request->set_body( wp_json_encode( $params ) );
  2291. $response = rest_get_server()->dispatch( $request );
  2292. $this->assertErrorResponse( 'comment_content_column_length', $response, 400 );
  2293. }
  2294. /**
  2295. * @ticket 39732
  2296. */
  2297. public function test_update_comment_is_wp_error() {
  2298. wp_set_current_user( self::$admin_id );
  2299. $params = array(
  2300. 'content' => 'This isn\'t a saxophone. It\'s an umbrella.',
  2301. );
  2302. add_filter( 'wp_update_comment_data', array( $this, '_wp_update_comment_data_filter' ), 10, 3 );
  2303. $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
  2304. $request->add_header( 'content-type', 'application/json' );
  2305. $request->set_body( wp_json_encode( $params ) );
  2306. $response = rest_get_server()->dispatch( $request );
  2307. remove_filter( 'wp_update_comment_data', array( $this, '_wp_update_comment_data_filter' ), 10, 3 );
  2308. $this->assertErrorResponse( 'rest_comment_failed_edit', $response, 500 );
  2309. }
  2310. /**
  2311. * Blocks comments from being updated by returning WP_Error.
  2312. */
  2313. public function _wp_update_comment_data_filter( $data, $comment, $commentarr ) {
  2314. return new WP_Error( 'comment_wrong', 'wp_update_comment_data filter fails for this comment.', array( 'status' => 500 ) );
  2315. }
  2316. public function verify_comment_roundtrip( $input = array(), $expected_output = array() ) {
  2317. // Create the comment.
  2318. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  2319. $request->set_param( 'author_email', 'cbg@androidsdungeon.com' );
  2320. $request->set_param( 'post', self::$post_id );
  2321. foreach ( $input as $name => $value ) {
  2322. $request->set_param( $name, $value );
  2323. }
  2324. $response = rest_get_server()->dispatch( $request );
  2325. $this->assertSame( 201, $response->get_status() );
  2326. $actual_output = $response->get_data();
  2327. // Compare expected API output to actual API output.
  2328. $this->assertInternalType( 'array', $actual_output['content'] );
  2329. $this->assertArrayHasKey( 'raw', $actual_output['content'] );
  2330. $this->assertSame( $expected_output['content']['raw'], $actual_output['content']['raw'] );
  2331. $this->assertSame( $expected_output['content']['rendered'], trim( $actual_output['content']['rendered'] ) );
  2332. $this->assertSame( $expected_output['author_name'], $actual_output['author_name'] );
  2333. $this->assertSame( $expected_output['author_user_agent'], $actual_output['author_user_agent'] );
  2334. // Compare expected API output to WP internal values.
  2335. $comment = get_comment( $actual_output['id'] );
  2336. $this->assertSame( $expected_output['content']['raw'], $comment->comment_content );
  2337. $this->assertSame( $expected_output['author_name'], $comment->comment_author );
  2338. $this->assertSame( $expected_output['author_user_agent'], $comment->comment_agent );
  2339. // Update the comment.
  2340. $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/comments/%d', $actual_output['id'] ) );
  2341. foreach ( $input as $name => $value ) {
  2342. $request->set_param( $name, $value );
  2343. }
  2344. // FIXME At least one value must change, or update fails.
  2345. // See https://core.trac.wordpress.org/ticket/38700
  2346. $request->set_param( 'author_ip', '127.0.0.2' );
  2347. $response = rest_get_server()->dispatch( $request );
  2348. $this->assertSame( 200, $response->get_status() );
  2349. $actual_output = $response->get_data();
  2350. // Compare expected API output to actual API output.
  2351. $this->assertSame( $expected_output['content']['raw'], $actual_output['content']['raw'] );
  2352. $this->assertSame( $expected_output['content']['rendered'], trim( $actual_output['content']['rendered'] ) );
  2353. $this->assertSame( $expected_output['author_name'], $actual_output['author_name'] );
  2354. $this->assertSame( $expected_output['author_user_agent'], $actual_output['author_user_agent'] );
  2355. // Compare expected API output to WP internal values.
  2356. $comment = get_comment( $actual_output['id'] );
  2357. $this->assertSame( $expected_output['content']['raw'], $comment->comment_content );
  2358. $this->assertSame( $expected_output['author_name'], $comment->comment_author );
  2359. $this->assertSame( $expected_output['author_user_agent'], $comment->comment_agent );
  2360. }
  2361. public function test_comment_roundtrip_as_editor() {
  2362. wp_set_current_user( self::$editor_id );
  2363. $this->assertSame( ! is_multisite(), current_user_can( 'unfiltered_html' ) );
  2364. $this->verify_comment_roundtrip(
  2365. array(
  2366. 'content' => '\o/ ¯\_(ツ)_/¯',
  2367. 'author_name' => '\o/ ¯\_(ツ)_/¯',
  2368. 'author_user_agent' => '\o/ ¯\_(ツ)_/¯',
  2369. ),
  2370. array(
  2371. 'content' => array(
  2372. 'raw' => '\o/ ¯\_(ツ)_/¯',
  2373. 'rendered' => '<p>\o/ ¯\_(ツ)_/¯</p>',
  2374. ),
  2375. 'author_name' => '\o/ ¯\_(ツ)_/¯',
  2376. 'author_user_agent' => '\o/ ¯\_(ツ)_/¯',
  2377. )
  2378. );
  2379. }
  2380. public function test_comment_roundtrip_as_editor_unfiltered_html() {
  2381. wp_set_current_user( self::$editor_id );
  2382. if ( is_multisite() ) {
  2383. $this->assertFalse( current_user_can( 'unfiltered_html' ) );
  2384. $this->verify_comment_roundtrip(
  2385. array(
  2386. 'content' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
  2387. 'author_name' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
  2388. 'author_user_agent' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
  2389. ),
  2390. array(
  2391. 'content' => array(
  2392. 'raw' => 'div <strong>strong</strong> oh noes',
  2393. 'rendered' => '<p>div <strong>strong</strong> oh noes</p>',
  2394. ),
  2395. 'author_name' => 'div strong',
  2396. 'author_user_agent' => 'div strong',
  2397. )
  2398. );
  2399. } else {
  2400. $this->assertTrue( current_user_can( 'unfiltered_html' ) );
  2401. $this->verify_comment_roundtrip(
  2402. array(
  2403. 'content' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
  2404. 'author_name' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
  2405. 'author_user_agent' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
  2406. ),
  2407. array(
  2408. 'content' => array(
  2409. 'raw' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
  2410. 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>",
  2411. ),
  2412. 'author_name' => 'div strong',
  2413. 'author_user_agent' => 'div strong',
  2414. )
  2415. );
  2416. }
  2417. }
  2418. public function test_comment_roundtrip_as_superadmin() {
  2419. wp_set_current_user( self::$superadmin_id );
  2420. $this->assertTrue( current_user_can( 'unfiltered_html' ) );
  2421. $this->verify_comment_roundtrip(
  2422. array(
  2423. 'content' => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
  2424. 'author_name' => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
  2425. 'author_user_agent' => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
  2426. ),
  2427. array(
  2428. 'content' => array(
  2429. 'raw' => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
  2430. 'rendered' => '<p>\\\&#038;\\\ &amp; &invalid; < &lt; &amp;lt;' . "\n</p>",
  2431. ),
  2432. 'author_name' => '\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;',
  2433. 'author_user_agent' => '\\\&\\\ &amp; &invalid; &lt; &lt; &amp;lt;',
  2434. )
  2435. );
  2436. }
  2437. public function test_comment_roundtrip_as_superadmin_unfiltered_html() {
  2438. wp_set_current_user( self::$superadmin_id );
  2439. $this->assertTrue( current_user_can( 'unfiltered_html' ) );
  2440. $this->verify_comment_roundtrip(
  2441. array(
  2442. 'content' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
  2443. 'author_name' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
  2444. 'author_user_agent' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
  2445. ),
  2446. array(
  2447. 'content' => array(
  2448. 'raw' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
  2449. 'rendered' => "<div>div</div>\n<p> <strong>strong</strong> <script>oh noes</script></p>",
  2450. ),
  2451. 'author_name' => 'div strong',
  2452. 'author_user_agent' => 'div strong',
  2453. )
  2454. );
  2455. }
  2456. public function test_delete_item() {
  2457. wp_set_current_user( self::$admin_id );
  2458. $comment_id = $this->factory->comment->create(
  2459. array(
  2460. 'comment_approved' => 1,
  2461. 'comment_post_ID' => self::$post_id,
  2462. 'user_id' => self::$subscriber_id,
  2463. )
  2464. );
  2465. $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) );
  2466. $request->set_param( 'force', 'false' );
  2467. $response = rest_get_server()->dispatch( $request );
  2468. $this->assertSame( 200, $response->get_status() );
  2469. $data = $response->get_data();
  2470. $this->assertSame( 'trash', $data['status'] );
  2471. }
  2472. public function test_delete_item_skip_trash() {
  2473. wp_set_current_user( self::$admin_id );
  2474. $comment_id = $this->factory->comment->create(
  2475. array(
  2476. 'comment_approved' => 1,
  2477. 'comment_post_ID' => self::$post_id,
  2478. 'user_id' => self::$subscriber_id,
  2479. )
  2480. );
  2481. $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) );
  2482. $request['force'] = true;
  2483. $response = rest_get_server()->dispatch( $request );
  2484. $this->assertSame( 200, $response->get_status() );
  2485. $data = $response->get_data();
  2486. $this->assertTrue( $data['deleted'] );
  2487. $this->assertNotEmpty( $data['previous']['post'] );
  2488. }
  2489. public function test_delete_item_already_trashed() {
  2490. wp_set_current_user( self::$admin_id );
  2491. $comment_id = $this->factory->comment->create(
  2492. array(
  2493. 'comment_approved' => 1,
  2494. 'comment_post_ID' => self::$post_id,
  2495. 'user_id' => self::$subscriber_id,
  2496. )
  2497. );
  2498. $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', $comment_id ) );
  2499. $response = rest_get_server()->dispatch( $request );
  2500. $this->assertSame( 200, $response->get_status() );
  2501. $data = $response->get_data();
  2502. $response = rest_get_server()->dispatch( $request );
  2503. $this->assertErrorResponse( 'rest_already_trashed', $response, 410 );
  2504. }
  2505. public function test_delete_comment_invalid_id() {
  2506. wp_set_current_user( self::$admin_id );
  2507. $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ) );
  2508. $response = rest_get_server()->dispatch( $request );
  2509. $this->assertErrorResponse( 'rest_comment_invalid_id', $response, 404 );
  2510. }
  2511. public function test_delete_comment_without_permission() {
  2512. wp_set_current_user( self::$subscriber_id );
  2513. $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
  2514. $response = rest_get_server()->dispatch( $request );
  2515. $this->assertErrorResponse( 'rest_cannot_delete', $response, 403 );
  2516. }
  2517. public function test_delete_child_comment_link() {
  2518. wp_set_current_user( self::$admin_id );
  2519. $comment_id_1 = $this->factory->comment->create(
  2520. array(
  2521. 'comment_approved' => 1,
  2522. 'comment_post_ID' => self::$post_id,
  2523. 'user_id' => self::$subscriber_id,
  2524. )
  2525. );
  2526. $child_comment = $this->factory->comment->create(
  2527. array(
  2528. 'comment_approved' => 1,
  2529. 'comment_parent' => $comment_id_1,
  2530. 'comment_post_ID' => self::$post_id,
  2531. 'user_id' => self::$subscriber_id,
  2532. )
  2533. );
  2534. $request = new WP_REST_Request( 'DELETE', sprintf( '/wp/v2/comments/%s', $child_comment ) );
  2535. $response = rest_get_server()->dispatch( $request );
  2536. $this->assertSame( 200, $response->get_status() );
  2537. // Verify children link is gone.
  2538. $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $comment_id_1 ) );
  2539. $response = rest_get_server()->dispatch( $request );
  2540. $this->assertSame( 200, $response->get_status() );
  2541. $this->assertArrayNotHasKey( 'children', $response->get_links() );
  2542. }
  2543. public function test_get_item_schema() {
  2544. $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/comments' );
  2545. $response = rest_get_server()->dispatch( $request );
  2546. $data = $response->get_data();
  2547. $properties = $data['schema']['properties'];
  2548. $this->assertSame( 17, count( $properties ) );
  2549. $this->assertArrayHasKey( 'id', $properties );
  2550. $this->assertArrayHasKey( 'author', $properties );
  2551. $this->assertArrayHasKey( 'author_avatar_urls', $properties );
  2552. $this->assertArrayHasKey( 'author_email', $properties );
  2553. $this->assertArrayHasKey( 'author_ip', $properties );
  2554. $this->assertArrayHasKey( 'author_name', $properties );
  2555. $this->assertArrayHasKey( 'author_url', $properties );
  2556. $this->assertArrayHasKey( 'author_user_agent', $properties );
  2557. $this->assertArrayHasKey( 'content', $properties );
  2558. $this->assertArrayHasKey( 'date', $properties );
  2559. $this->assertArrayHasKey( 'date_gmt', $properties );
  2560. $this->assertArrayHasKey( 'link', $properties );
  2561. $this->assertArrayHasKey( 'meta', $properties );
  2562. $this->assertArrayHasKey( 'parent', $properties );
  2563. $this->assertArrayHasKey( 'post', $properties );
  2564. $this->assertArrayHasKey( 'status', $properties );
  2565. $this->assertArrayHasKey( 'type', $properties );
  2566. $this->assertSame( 0, $properties['parent']['default'] );
  2567. $this->assertSame( 0, $properties['post']['default'] );
  2568. $this->assertTrue( $properties['link']['readonly'] );
  2569. $this->assertTrue( $properties['type']['readonly'] );
  2570. }
  2571. public function test_get_item_schema_show_avatar() {
  2572. update_option( 'show_avatars', false );
  2573. $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/users' );
  2574. $response = rest_get_server()->dispatch( $request );
  2575. $data = $response->get_data();
  2576. $properties = $data['schema']['properties'];
  2577. $this->assertArrayNotHasKey( 'author_avatar_urls', $properties );
  2578. }
  2579. public function test_get_additional_field_registration() {
  2580. $schema = array(
  2581. 'type' => 'integer',
  2582. 'description' => 'Some integer of mine',
  2583. 'enum' => array( 1, 2, 3, 4 ),
  2584. 'context' => array( 'view', 'edit' ),
  2585. );
  2586. register_rest_field(
  2587. 'comment',
  2588. 'my_custom_int',
  2589. array(
  2590. 'schema' => $schema,
  2591. 'get_callback' => array( $this, 'additional_field_get_callback' ),
  2592. 'update_callback' => array( $this, 'additional_field_update_callback' ),
  2593. )
  2594. );
  2595. $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/comments' );
  2596. $response = rest_get_server()->dispatch( $request );
  2597. $data = $response->get_data();
  2598. $this->assertArrayHasKey( 'my_custom_int', $data['schema']['properties'] );
  2599. $this->assertSame( $schema, $data['schema']['properties']['my_custom_int'] );
  2600. $request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . self::$approved_id );
  2601. $response = rest_get_server()->dispatch( $request );
  2602. $this->assertArrayHasKey( 'my_custom_int', $response->data );
  2603. $request = new WP_REST_Request( 'POST', '/wp/v2/comments/' . self::$approved_id );
  2604. $request->set_body_params(
  2605. array(
  2606. 'my_custom_int' => 123,
  2607. 'content' => 'abc',
  2608. )
  2609. );
  2610. wp_set_current_user( 1 );
  2611. rest_get_server()->dispatch( $request );
  2612. $this->assertEquals( 123, get_comment_meta( self::$approved_id, 'my_custom_int', true ) );
  2613. $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
  2614. $request->set_body_params(
  2615. array(
  2616. 'my_custom_int' => 123,
  2617. 'title' => 'hello',
  2618. 'content' => 'goodbye',
  2619. 'post' => self::$post_id,
  2620. )
  2621. );
  2622. $response = rest_get_server()->dispatch( $request );
  2623. $this->assertEquals( 123, $response->data['my_custom_int'] );
  2624. global $wp_rest_additional_fields;
  2625. $wp_rest_additional_fields = array();
  2626. }
  2627. public function test_additional_field_update_errors() {
  2628. $schema = array(
  2629. 'type' => 'integer',
  2630. 'description' => 'Some integer of mine',
  2631. 'enum' => array( 1, 2, 3, 4 ),
  2632. 'context' => array( 'view', 'edit' ),
  2633. );
  2634. register_rest_field(
  2635. 'comment',
  2636. 'my_custom_int',
  2637. array(
  2638. 'schema' => $schema,
  2639. 'get_callback' => array( $this, 'additional_field_get_callback' ),
  2640. 'update_callback' => array( $this, 'additional_field_update_callback' ),
  2641. )
  2642. );
  2643. wp_set_current_user( self::$admin_id );
  2644. // Check for error on update.
  2645. $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
  2646. $request->set_body_params(
  2647. array(
  2648. 'my_custom_int' => 'returnError',
  2649. 'content' => 'abc',
  2650. )
  2651. );
  2652. $response = rest_get_server()->dispatch( $request );
  2653. $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
  2654. global $wp_rest_additional_fields;
  2655. $wp_rest_additional_fields = array();
  2656. }
  2657. public function additional_field_get_callback( $object ) {
  2658. return get_comment_meta( $object['id'], 'my_custom_int', true );
  2659. }
  2660. public function additional_field_update_callback( $value, $comment ) {
  2661. if ( 'returnError' === $value ) {
  2662. return new WP_Error( 'rest_invalid_param', 'Testing an error.', array( 'status' => 400 ) );
  2663. }
  2664. update_comment_meta( $comment->comment_ID, 'my_custom_int', $value );
  2665. }
  2666. protected function check_comment_data( $data, $context, $links ) {
  2667. $comment = get_comment( $data['id'] );
  2668. $this->assertEquals( $comment->comment_ID, $data['id'] );
  2669. $this->assertEquals( $comment->comment_post_ID, $data['post'] );
  2670. $this->assertEquals( $comment->comment_parent, $data['parent'] );
  2671. $this->assertEquals( $comment->user_id, $data['author'] );
  2672. $this->assertSame( $comment->comment_author, $data['author_name'] );
  2673. $this->assertSame( $comment->comment_author_url, $data['author_url'] );
  2674. $this->assertSame( wpautop( $comment->comment_content ), $data['content']['rendered'] );
  2675. $this->assertSame( mysql_to_rfc3339( $comment->comment_date ), $data['date'] );
  2676. $this->assertSame( mysql_to_rfc3339( $comment->comment_date_gmt ), $data['date_gmt'] );
  2677. $this->assertSame( get_comment_link( $comment ), $data['link'] );
  2678. $this->assertArrayHasKey( 'author_avatar_urls', $data );
  2679. $this->assertSameSets(
  2680. array(
  2681. 'self',
  2682. 'collection',
  2683. 'up',
  2684. ),
  2685. array_keys( $links )
  2686. );
  2687. if ( $comment->comment_post_ID ) {
  2688. $this->assertSame( rest_url( '/wp/v2/posts/' . $comment->comment_post_ID ), $links['up'][0]['href'] );
  2689. }
  2690. if ( 'edit' === $context ) {
  2691. $this->assertSame( $comment->comment_author_email, $data['author_email'] );
  2692. $this->assertSame( $comment->comment_author_IP, $data['author_ip'] );
  2693. $this->assertSame( $comment->comment_agent, $data['author_user_agent'] );
  2694. $this->assertSame( $comment->comment_content, $data['content']['raw'] );
  2695. }
  2696. if ( 'edit' !== $context ) {
  2697. $this->assertArrayNotHasKey( 'author_email', $data );
  2698. $this->assertArrayNotHasKey( 'author_ip', $data );
  2699. $this->assertArrayNotHasKey( 'author_user_agent', $data );
  2700. $this->assertArrayNotHasKey( 'raw', $data['content'] );
  2701. }
  2702. }
  2703. /**
  2704. * @ticket 42238
  2705. */
  2706. public function test_check_read_post_permission_with_invalid_post_type() {
  2707. register_post_type(
  2708. 'bug-post',
  2709. array(
  2710. 'label' => 'Bug Posts',
  2711. 'supports' => array( 'title', 'editor', 'author', 'comments' ),
  2712. 'show_in_rest' => true,
  2713. 'public' => true,
  2714. )
  2715. );
  2716. create_initial_rest_routes();
  2717. $post_id = self::factory()->post->create( array( 'post_type' => 'bug-post' ) );
  2718. $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => $post_id ) );
  2719. _unregister_post_type( 'bug-post' );
  2720. $this->setExpectedIncorrectUsage( 'map_meta_cap' );
  2721. wp_set_current_user( self::$admin_id );
  2722. $request = new WP_REST_Request( 'GET', '/wp/v2/comments/' . $comment_id );
  2723. $response = rest_get_server()->dispatch( $request );
  2724. $this->assertSame( 403, $response->get_status() );
  2725. }
  2726. }