query.php 112 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944
  1. <?php
  2. // Test the output of Comment Querying functions.
  3. /**
  4. * @group comment
  5. */
  6. class Tests_Comment_Query extends WP_UnitTestCase {
  7. protected static $post_id;
  8. protected $comment_id;
  9. public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  10. self::$post_id = $factory->post->create();
  11. }
  12. public function test_query() {
  13. $c1 = self::factory()->comment->create(
  14. array(
  15. 'comment_post_ID' => self::$post_id,
  16. 'comment_approved' => '1',
  17. )
  18. );
  19. $c2 = self::factory()->comment->create(
  20. array(
  21. 'comment_post_ID' => self::$post_id,
  22. 'comment_approved' => '1',
  23. 'comment_type' => 'pingback',
  24. )
  25. );
  26. $c3 = self::factory()->comment->create(
  27. array(
  28. 'comment_post_ID' => self::$post_id,
  29. 'comment_approved' => '1',
  30. 'comment_type' => 'trackback',
  31. )
  32. );
  33. $c4 = self::factory()->comment->create(
  34. array(
  35. 'comment_post_ID' => self::$post_id,
  36. 'comment_approved' => '1',
  37. 'comment_type' => 'mario',
  38. )
  39. );
  40. $c5 = self::factory()->comment->create(
  41. array(
  42. 'comment_post_ID' => self::$post_id,
  43. 'comment_approved' => '1',
  44. 'comment_type' => 'luigi',
  45. )
  46. );
  47. $q = new WP_Comment_Query();
  48. $found = $q->query(
  49. array(
  50. 'fields' => 'ids',
  51. )
  52. );
  53. $this->assertSameSets( array( $c1, $c2, $c3, $c4, $c5 ), $found );
  54. }
  55. public function test_query_post_id_0() {
  56. $c1 = self::factory()->comment->create(
  57. array(
  58. 'comment_post_ID' => self::$post_id,
  59. 'comment_approved' => '1',
  60. )
  61. );
  62. $q = new WP_Comment_Query();
  63. $found = $q->query(
  64. array(
  65. 'post_id' => 0,
  66. 'fields' => 'ids',
  67. )
  68. );
  69. $this->assertSameSets( array( $c1 ), $found );
  70. }
  71. /**
  72. * @ticket 12668
  73. */
  74. public function test_query_type_empty_string() {
  75. $c1 = self::factory()->comment->create(
  76. array(
  77. 'comment_post_ID' => self::$post_id,
  78. 'comment_approved' => '1',
  79. )
  80. );
  81. $c2 = self::factory()->comment->create(
  82. array(
  83. 'comment_post_ID' => self::$post_id,
  84. 'comment_approved' => '1',
  85. 'comment_type' => 'pingback',
  86. )
  87. );
  88. $c3 = self::factory()->comment->create(
  89. array(
  90. 'comment_post_ID' => self::$post_id,
  91. 'comment_approved' => '1',
  92. 'comment_type' => 'trackback',
  93. )
  94. );
  95. $c4 = self::factory()->comment->create(
  96. array(
  97. 'comment_post_ID' => self::$post_id,
  98. 'comment_approved' => '1',
  99. 'comment_type' => 'mario',
  100. )
  101. );
  102. $c5 = self::factory()->comment->create(
  103. array(
  104. 'comment_post_ID' => self::$post_id,
  105. 'comment_approved' => '1',
  106. 'comment_type' => 'luigi',
  107. )
  108. );
  109. $q = new WP_Comment_Query();
  110. $found = $q->query(
  111. array(
  112. 'type' => '',
  113. 'fields' => 'ids',
  114. )
  115. );
  116. $this->assertSameSets( array( $c1, $c2, $c3, $c4, $c5 ), $found );
  117. }
  118. /**
  119. * @ticket 12668
  120. */
  121. public function test_query_type_comment() {
  122. $c1 = self::factory()->comment->create(
  123. array(
  124. 'comment_post_ID' => self::$post_id,
  125. 'comment_approved' => '1',
  126. )
  127. );
  128. $c2 = self::factory()->comment->create(
  129. array(
  130. 'comment_post_ID' => self::$post_id,
  131. 'comment_approved' => '1',
  132. 'comment_type' => 'pingback',
  133. )
  134. );
  135. $c3 = self::factory()->comment->create(
  136. array(
  137. 'comment_post_ID' => self::$post_id,
  138. 'comment_approved' => '1',
  139. 'comment_type' => 'trackback',
  140. )
  141. );
  142. $c4 = self::factory()->comment->create(
  143. array(
  144. 'comment_post_ID' => self::$post_id,
  145. 'comment_approved' => '1',
  146. 'comment_type' => 'mario',
  147. )
  148. );
  149. $c5 = self::factory()->comment->create(
  150. array(
  151. 'comment_post_ID' => self::$post_id,
  152. 'comment_approved' => '1',
  153. 'comment_type' => 'luigi',
  154. )
  155. );
  156. $q = new WP_Comment_Query();
  157. $found = $q->query(
  158. array(
  159. 'type' => 'comment',
  160. 'fields' => 'ids',
  161. )
  162. );
  163. $this->assertSameSets( array( $c1 ), $found );
  164. }
  165. public function test_query_type_pingback() {
  166. $c1 = self::factory()->comment->create(
  167. array(
  168. 'comment_post_ID' => self::$post_id,
  169. 'comment_approved' => '1',
  170. )
  171. );
  172. $c2 = self::factory()->comment->create(
  173. array(
  174. 'comment_post_ID' => self::$post_id,
  175. 'comment_approved' => '1',
  176. 'comment_type' => 'pingback',
  177. )
  178. );
  179. $c3 = self::factory()->comment->create(
  180. array(
  181. 'comment_post_ID' => self::$post_id,
  182. 'comment_approved' => '1',
  183. 'comment_type' => 'pingback',
  184. )
  185. );
  186. $c4 = self::factory()->comment->create(
  187. array(
  188. 'comment_post_ID' => self::$post_id,
  189. 'comment_approved' => '1',
  190. 'comment_type' => 'trackback',
  191. )
  192. );
  193. $q = new WP_Comment_Query();
  194. $found = $q->query(
  195. array(
  196. 'type' => 'pingback',
  197. 'fields' => 'ids',
  198. )
  199. );
  200. $this->assertSameSets( array( $c2, $c3 ), $found );
  201. }
  202. public function test_query_type_trackback() {
  203. $c1 = self::factory()->comment->create(
  204. array(
  205. 'comment_post_ID' => self::$post_id,
  206. 'comment_approved' => '1',
  207. )
  208. );
  209. $c2 = self::factory()->comment->create(
  210. array(
  211. 'comment_post_ID' => self::$post_id,
  212. 'comment_approved' => '1',
  213. 'comment_type' => 'trackback',
  214. )
  215. );
  216. $c3 = self::factory()->comment->create(
  217. array(
  218. 'comment_post_ID' => self::$post_id,
  219. 'comment_approved' => '1',
  220. 'comment_type' => 'trackback',
  221. )
  222. );
  223. $c4 = self::factory()->comment->create(
  224. array(
  225. 'comment_post_ID' => self::$post_id,
  226. 'comment_approved' => '1',
  227. 'comment_type' => 'pingback',
  228. )
  229. );
  230. $q = new WP_Comment_Query();
  231. $found = $q->query(
  232. array(
  233. 'type' => 'trackback',
  234. 'fields' => 'ids',
  235. )
  236. );
  237. $this->assertSameSets( array( $c2, $c3 ), $found );
  238. }
  239. /**
  240. * 'pings' is an alias for 'trackback' + 'pingback'.
  241. */
  242. public function test_query_type_pings() {
  243. $c1 = self::factory()->comment->create(
  244. array(
  245. 'comment_post_ID' => self::$post_id,
  246. 'comment_approved' => '1',
  247. )
  248. );
  249. $c2 = self::factory()->comment->create(
  250. array(
  251. 'comment_post_ID' => self::$post_id,
  252. 'comment_approved' => '1',
  253. 'comment_type' => 'pingback',
  254. )
  255. );
  256. $c3 = self::factory()->comment->create(
  257. array(
  258. 'comment_post_ID' => self::$post_id,
  259. 'comment_approved' => '1',
  260. 'comment_type' => 'trackback',
  261. )
  262. );
  263. $c4 = self::factory()->comment->create(
  264. array(
  265. 'comment_post_ID' => self::$post_id,
  266. 'comment_approved' => '1',
  267. 'comment_type' => 'mario',
  268. )
  269. );
  270. $c5 = self::factory()->comment->create(
  271. array(
  272. 'comment_post_ID' => self::$post_id,
  273. 'comment_approved' => '1',
  274. 'comment_type' => 'luigi',
  275. )
  276. );
  277. $q = new WP_Comment_Query();
  278. $found = $q->query(
  279. array(
  280. 'type' => 'pings',
  281. 'fields' => 'ids',
  282. )
  283. );
  284. $this->assertSameSets( array( $c2, $c3 ), $found );
  285. }
  286. /**
  287. * Comments and custom
  288. *
  289. * @ticket 12668
  290. */
  291. public function test_type_array_comments_and_custom() {
  292. $c1 = self::factory()->comment->create(
  293. array(
  294. 'comment_post_ID' => self::$post_id,
  295. 'comment_approved' => '1',
  296. )
  297. );
  298. $c2 = self::factory()->comment->create(
  299. array(
  300. 'comment_post_ID' => self::$post_id,
  301. 'comment_approved' => '1',
  302. 'comment_type' => 'pingback',
  303. )
  304. );
  305. $c3 = self::factory()->comment->create(
  306. array(
  307. 'comment_post_ID' => self::$post_id,
  308. 'comment_approved' => '1',
  309. 'comment_type' => 'trackback',
  310. )
  311. );
  312. $c4 = self::factory()->comment->create(
  313. array(
  314. 'comment_post_ID' => self::$post_id,
  315. 'comment_approved' => '1',
  316. 'comment_type' => 'mario',
  317. )
  318. );
  319. $c5 = self::factory()->comment->create(
  320. array(
  321. 'comment_post_ID' => self::$post_id,
  322. 'comment_approved' => '1',
  323. 'comment_type' => 'luigi',
  324. )
  325. );
  326. $c6 = self::factory()->comment->create(
  327. array(
  328. 'comment_post_ID' => self::$post_id,
  329. 'comment_approved' => '1',
  330. 'comment_type' => 'mario',
  331. )
  332. );
  333. $q = new WP_Comment_Query();
  334. $found = $q->query(
  335. array(
  336. 'type' => array( 'comments', 'mario' ),
  337. 'fields' => 'ids',
  338. )
  339. );
  340. $this->assertSameSets( array( $c1, $c4, $c6 ), $found );
  341. }
  342. /**
  343. * @ticket 12668
  344. */
  345. public function test_type_not__in_array_custom() {
  346. $c1 = self::factory()->comment->create(
  347. array(
  348. 'comment_post_ID' => self::$post_id,
  349. 'comment_approved' => '1',
  350. )
  351. );
  352. $c2 = self::factory()->comment->create(
  353. array(
  354. 'comment_post_ID' => self::$post_id,
  355. 'comment_approved' => '1',
  356. 'comment_type' => 'pingback',
  357. )
  358. );
  359. $c3 = self::factory()->comment->create(
  360. array(
  361. 'comment_post_ID' => self::$post_id,
  362. 'comment_approved' => '1',
  363. 'comment_type' => 'trackback',
  364. )
  365. );
  366. $c4 = self::factory()->comment->create(
  367. array(
  368. 'comment_post_ID' => self::$post_id,
  369. 'comment_approved' => '1',
  370. 'comment_type' => 'mario',
  371. )
  372. );
  373. $c5 = self::factory()->comment->create(
  374. array(
  375. 'comment_post_ID' => self::$post_id,
  376. 'comment_approved' => '1',
  377. 'comment_type' => 'luigi',
  378. )
  379. );
  380. $c6 = self::factory()->comment->create(
  381. array(
  382. 'comment_post_ID' => self::$post_id,
  383. 'comment_approved' => '1',
  384. 'comment_type' => 'mario',
  385. )
  386. );
  387. $q = new WP_Comment_Query();
  388. $found = $q->query(
  389. array(
  390. 'type__not_in' => array( 'luigi' ),
  391. 'fields' => 'ids',
  392. )
  393. );
  394. $this->assertSameSets( array( $c1, $c2, $c3, $c4, $c6 ), $found );
  395. }
  396. /**
  397. * @ticket 12668
  398. */
  399. public function test_type__in_array_and_not_type_array_custom() {
  400. $c1 = self::factory()->comment->create(
  401. array(
  402. 'comment_post_ID' => self::$post_id,
  403. 'comment_approved' => '1',
  404. )
  405. );
  406. $c2 = self::factory()->comment->create(
  407. array(
  408. 'comment_post_ID' => self::$post_id,
  409. 'comment_approved' => '1',
  410. 'comment_type' => 'pingback',
  411. )
  412. );
  413. $c3 = self::factory()->comment->create(
  414. array(
  415. 'comment_post_ID' => self::$post_id,
  416. 'comment_approved' => '1',
  417. 'comment_type' => 'trackback',
  418. )
  419. );
  420. $c4 = self::factory()->comment->create(
  421. array(
  422. 'comment_post_ID' => self::$post_id,
  423. 'comment_approved' => '1',
  424. 'comment_type' => 'mario',
  425. )
  426. );
  427. $c5 = self::factory()->comment->create(
  428. array(
  429. 'comment_post_ID' => self::$post_id,
  430. 'comment_approved' => '1',
  431. 'comment_type' => 'luigi',
  432. )
  433. );
  434. $c6 = self::factory()->comment->create(
  435. array(
  436. 'comment_post_ID' => self::$post_id,
  437. 'comment_approved' => '1',
  438. 'comment_type' => 'mario',
  439. )
  440. );
  441. $q = new WP_Comment_Query();
  442. $found = $q->query(
  443. array(
  444. 'type__in' => array( 'comments' ),
  445. 'type__not_in' => array( 'luigi' ),
  446. 'fields' => 'ids',
  447. )
  448. );
  449. $this->assertSameSets( array( $c1 ), $found );
  450. }
  451. /**
  452. * @ticket 12668
  453. */
  454. public function test_type_array_and_type__not_in_array_custom() {
  455. $c1 = self::factory()->comment->create(
  456. array(
  457. 'comment_post_ID' => self::$post_id,
  458. 'comment_approved' => '1',
  459. )
  460. );
  461. $c2 = self::factory()->comment->create(
  462. array(
  463. 'comment_post_ID' => self::$post_id,
  464. 'comment_approved' => '1',
  465. 'comment_type' => 'pingback',
  466. )
  467. );
  468. $c3 = self::factory()->comment->create(
  469. array(
  470. 'comment_post_ID' => self::$post_id,
  471. 'comment_approved' => '1',
  472. 'comment_type' => 'trackback',
  473. )
  474. );
  475. $c4 = self::factory()->comment->create(
  476. array(
  477. 'comment_post_ID' => self::$post_id,
  478. 'comment_approved' => '1',
  479. 'comment_type' => 'mario',
  480. )
  481. );
  482. $c5 = self::factory()->comment->create(
  483. array(
  484. 'comment_post_ID' => self::$post_id,
  485. 'comment_approved' => '1',
  486. 'comment_type' => 'luigi',
  487. )
  488. );
  489. $c6 = self::factory()->comment->create(
  490. array(
  491. 'comment_post_ID' => self::$post_id,
  492. 'comment_approved' => '1',
  493. 'comment_type' => 'mario',
  494. )
  495. );
  496. $q = new WP_Comment_Query();
  497. $found = $q->query(
  498. array(
  499. 'type' => array( 'pings' ),
  500. 'type__not_in' => array( 'mario' ),
  501. 'fields' => 'ids',
  502. )
  503. );
  504. $this->assertSameSets( array( $c2, $c3 ), $found );
  505. }
  506. /**
  507. * @ticket 12668
  508. */
  509. public function test_type__not_in_custom() {
  510. $c1 = self::factory()->comment->create(
  511. array(
  512. 'comment_post_ID' => self::$post_id,
  513. 'comment_approved' => '1',
  514. )
  515. );
  516. $c2 = self::factory()->comment->create(
  517. array(
  518. 'comment_post_ID' => self::$post_id,
  519. 'comment_approved' => '1',
  520. 'comment_type' => 'pingback',
  521. )
  522. );
  523. $c3 = self::factory()->comment->create(
  524. array(
  525. 'comment_post_ID' => self::$post_id,
  526. 'comment_approved' => '1',
  527. 'comment_type' => 'trackback',
  528. )
  529. );
  530. $c4 = self::factory()->comment->create(
  531. array(
  532. 'comment_post_ID' => self::$post_id,
  533. 'comment_approved' => '1',
  534. 'comment_type' => 'mario',
  535. )
  536. );
  537. $c5 = self::factory()->comment->create(
  538. array(
  539. 'comment_post_ID' => self::$post_id,
  540. 'comment_approved' => '1',
  541. 'comment_type' => 'luigi',
  542. )
  543. );
  544. $c6 = self::factory()->comment->create(
  545. array(
  546. 'comment_post_ID' => self::$post_id,
  547. 'comment_approved' => '1',
  548. 'comment_type' => 'mario',
  549. )
  550. );
  551. $q = new WP_Comment_Query();
  552. $found = $q->query(
  553. array(
  554. 'type__not_in' => 'luigi',
  555. 'fields' => 'ids',
  556. )
  557. );
  558. $this->assertSameSets( array( $c1, $c2, $c3, $c4, $c6 ), $found );
  559. }
  560. /**
  561. * @ticket 12668
  562. */
  563. public function test_type_array_comments_and_pings() {
  564. $c1 = self::factory()->comment->create(
  565. array(
  566. 'comment_post_ID' => self::$post_id,
  567. 'comment_approved' => '1',
  568. )
  569. );
  570. $c2 = self::factory()->comment->create(
  571. array(
  572. 'comment_post_ID' => self::$post_id,
  573. 'comment_approved' => '1',
  574. 'comment_type' => 'pingback',
  575. )
  576. );
  577. $c3 = self::factory()->comment->create(
  578. array(
  579. 'comment_post_ID' => self::$post_id,
  580. 'comment_approved' => '1',
  581. 'comment_type' => 'trackback',
  582. )
  583. );
  584. $c4 = self::factory()->comment->create(
  585. array(
  586. 'comment_post_ID' => self::$post_id,
  587. 'comment_approved' => '1',
  588. 'comment_type' => 'mario',
  589. )
  590. );
  591. $c5 = self::factory()->comment->create(
  592. array(
  593. 'comment_post_ID' => self::$post_id,
  594. 'comment_approved' => '1',
  595. 'comment_type' => 'luigi',
  596. )
  597. );
  598. $q = new WP_Comment_Query();
  599. $found = $q->query(
  600. array(
  601. 'type' => array( 'comments', 'pings' ),
  602. 'fields' => 'ids',
  603. )
  604. );
  605. $this->assertSameSets( array( $c1, $c2, $c3 ), $found );
  606. }
  607. /**
  608. * @ticket 12668
  609. */
  610. public function test_type_array_comment_pings() {
  611. $c1 = self::factory()->comment->create(
  612. array(
  613. 'comment_post_ID' => self::$post_id,
  614. 'comment_approved' => '1',
  615. )
  616. );
  617. $c2 = self::factory()->comment->create(
  618. array(
  619. 'comment_post_ID' => self::$post_id,
  620. 'comment_approved' => '1',
  621. 'comment_type' => 'pingback',
  622. )
  623. );
  624. $c3 = self::factory()->comment->create(
  625. array(
  626. 'comment_post_ID' => self::$post_id,
  627. 'comment_approved' => '1',
  628. 'comment_type' => 'trackback',
  629. )
  630. );
  631. $q = new WP_Comment_Query();
  632. $found = $q->query(
  633. array(
  634. 'type' => array( 'comment', 'pings' ),
  635. 'fields' => 'ids',
  636. )
  637. );
  638. $this->assertSameSets( array( $c1, $c2, $c3 ), $found );
  639. }
  640. /**
  641. * @ticket 12668
  642. */
  643. public function test_type_array_pingback() {
  644. $c1 = self::factory()->comment->create(
  645. array(
  646. 'comment_post_ID' => self::$post_id,
  647. 'comment_approved' => '1',
  648. )
  649. );
  650. $c2 = self::factory()->comment->create(
  651. array(
  652. 'comment_post_ID' => self::$post_id,
  653. 'comment_approved' => '1',
  654. 'comment_type' => 'pingback',
  655. )
  656. );
  657. $c3 = self::factory()->comment->create(
  658. array(
  659. 'comment_post_ID' => self::$post_id,
  660. 'comment_approved' => '1',
  661. 'comment_type' => 'trackback',
  662. )
  663. );
  664. $q = new WP_Comment_Query();
  665. $found = $q->query(
  666. array(
  667. 'type' => array( 'pingback' ),
  668. 'fields' => 'ids',
  669. )
  670. );
  671. $this->assertSame( array( $c2 ), $found );
  672. }
  673. /**
  674. * @ticket 12668
  675. */
  676. public function test_type_array_custom_pingpack() {
  677. $c1 = self::factory()->comment->create(
  678. array(
  679. 'comment_post_ID' => self::$post_id,
  680. 'comment_approved' => '1',
  681. )
  682. );
  683. $c2 = self::factory()->comment->create(
  684. array(
  685. 'comment_post_ID' => self::$post_id,
  686. 'comment_approved' => '1',
  687. 'comment_type' => 'pingback',
  688. )
  689. );
  690. $c3 = self::factory()->comment->create(
  691. array(
  692. 'comment_post_ID' => self::$post_id,
  693. 'comment_approved' => '1',
  694. 'comment_type' => 'trackback',
  695. )
  696. );
  697. $q = new WP_Comment_Query();
  698. $found = $q->query(
  699. array(
  700. 'type' => array( 'peach', 'pingback' ),
  701. 'fields' => 'ids',
  702. )
  703. );
  704. $this->assertSame( array( $c2 ), $found );
  705. }
  706. /**
  707. * @ticket 12668
  708. */
  709. public function test_type_array_pings() {
  710. $c1 = self::factory()->comment->create(
  711. array(
  712. 'comment_post_ID' => self::$post_id,
  713. 'comment_approved' => '1',
  714. )
  715. );
  716. $c2 = self::factory()->comment->create(
  717. array(
  718. 'comment_post_ID' => self::$post_id,
  719. 'comment_approved' => '1',
  720. 'comment_type' => 'pingback',
  721. )
  722. );
  723. $c3 = self::factory()->comment->create(
  724. array(
  725. 'comment_post_ID' => self::$post_id,
  726. 'comment_approved' => '1',
  727. 'comment_type' => 'pingback',
  728. )
  729. );
  730. $q = new WP_Comment_Query();
  731. $found = $q->query(
  732. array(
  733. 'type' => array( 'pings' ),
  734. 'fields' => 'ids',
  735. )
  736. );
  737. $this->assertSameSets( array( $c2, $c3 ), $found );
  738. }
  739. /**
  740. * @ticket 12668
  741. */
  742. public function test_type_status_approved_array_comment_pings() {
  743. $c1 = self::factory()->comment->create(
  744. array(
  745. 'comment_post_ID' => self::$post_id,
  746. 'comment_approved' => '1',
  747. )
  748. );
  749. $c2 = self::factory()->comment->create(
  750. array(
  751. 'comment_post_ID' => self::$post_id,
  752. 'comment_approved' => '1',
  753. 'comment_type' => 'pingback',
  754. )
  755. );
  756. $c3 = self::factory()->comment->create(
  757. array(
  758. 'comment_post_ID' => self::$post_id,
  759. 'comment_approved' => '1',
  760. 'comment_type' => 'pingback',
  761. )
  762. );
  763. $c4 = self::factory()->comment->create(
  764. array(
  765. 'comment_post_ID' => self::$post_id,
  766. 'comment_approved' => '0',
  767. 'comment_type' => 'pingback',
  768. )
  769. );
  770. $q = new WP_Comment_Query();
  771. $found = $q->query(
  772. array(
  773. 'status' => 'approve',
  774. 'type' => array( 'pings' ),
  775. 'fields' => 'ids',
  776. )
  777. );
  778. $this->assertSameSets( array( $c3, $c2 ), $found );
  779. }
  780. /**
  781. * @ticket 12668
  782. */
  783. public function test_type_array_trackback() {
  784. $c1 = self::factory()->comment->create(
  785. array(
  786. 'comment_post_ID' => self::$post_id,
  787. 'comment_approved' => '1',
  788. )
  789. );
  790. $c2 = self::factory()->comment->create(
  791. array(
  792. 'comment_post_ID' => self::$post_id,
  793. 'comment_approved' => '1',
  794. 'comment_type' => 'trackback',
  795. )
  796. );
  797. $c3 = self::factory()->comment->create(
  798. array(
  799. 'comment_post_ID' => self::$post_id,
  800. 'comment_approved' => '1',
  801. 'comment_type' => 'pingback',
  802. )
  803. );
  804. $q = new WP_Comment_Query();
  805. $found = $q->query(
  806. array(
  807. 'type' => array( 'trackback' ),
  808. 'fields' => 'ids',
  809. )
  810. );
  811. $this->assertSame( array( $c2 ), $found );
  812. }
  813. /**
  814. * @ticket 12668
  815. */
  816. public function test_type_array_custom_trackback() {
  817. $c1 = self::factory()->comment->create(
  818. array(
  819. 'comment_post_ID' => self::$post_id,
  820. 'comment_approved' => '1',
  821. )
  822. );
  823. $c2 = self::factory()->comment->create(
  824. array(
  825. 'comment_post_ID' => self::$post_id,
  826. 'comment_approved' => '1',
  827. 'comment_type' => 'trackback',
  828. )
  829. );
  830. $c3 = self::factory()->comment->create(
  831. array(
  832. 'comment_post_ID' => self::$post_id,
  833. 'comment_approved' => '1',
  834. 'comment_type' => 'pingback',
  835. )
  836. );
  837. $q = new WP_Comment_Query();
  838. $found = $q->query(
  839. array(
  840. 'type' => array( 'toad', 'trackback' ),
  841. 'fields' => 'ids',
  842. )
  843. );
  844. $this->assertSame( array( $c2 ), $found );
  845. }
  846. /**
  847. * @ticket 12668
  848. */
  849. public function test_type_array_pings_approved() {
  850. $c1 = self::factory()->comment->create(
  851. array(
  852. 'comment_post_ID' => self::$post_id,
  853. 'comment_approved' => '1',
  854. )
  855. );
  856. $c2 = self::factory()->comment->create(
  857. array(
  858. 'comment_post_ID' => self::$post_id,
  859. 'comment_approved' => '1',
  860. 'comment_type' => 'trackback',
  861. )
  862. );
  863. $c3 = self::factory()->comment->create(
  864. array(
  865. 'comment_post_ID' => self::$post_id,
  866. 'comment_approved' => '1',
  867. 'comment_type' => 'trackback',
  868. )
  869. );
  870. $c4 = self::factory()->comment->create(
  871. array(
  872. 'comment_post_ID' => self::$post_id,
  873. 'comment_approved' => '0',
  874. 'comment_type' => 'trackback',
  875. )
  876. );
  877. $q = new WP_Comment_Query();
  878. $found = $q->query(
  879. array(
  880. 'status' => 'approve',
  881. 'type' => array( 'pings' ),
  882. 'fields' => 'ids',
  883. )
  884. );
  885. $this->assertSameSets( array( $c3, $c2 ), $found );
  886. }
  887. /**
  888. * @ticket 29612
  889. */
  890. public function test_status_empty_string() {
  891. $c1 = self::factory()->comment->create(
  892. array(
  893. 'comment_post_ID' => self::$post_id,
  894. 'comment_approved' => '1',
  895. )
  896. );
  897. $c2 = self::factory()->comment->create(
  898. array(
  899. 'comment_post_ID' => self::$post_id,
  900. 'comment_approved' => '0',
  901. )
  902. );
  903. $c3 = self::factory()->comment->create(
  904. array(
  905. 'comment_post_ID' => self::$post_id,
  906. 'comment_approved' => 'spam',
  907. )
  908. );
  909. $q = new WP_Comment_Query();
  910. $found = $q->query(
  911. array(
  912. 'status' => '',
  913. 'fields' => 'ids',
  914. )
  915. );
  916. $this->assertSameSets( array( $c1, $c2 ), $found );
  917. }
  918. /**
  919. * @ticket 21101
  920. */
  921. public function test_status_hold() {
  922. $c1 = self::factory()->comment->create(
  923. array(
  924. 'comment_post_ID' => self::$post_id,
  925. 'comment_approved' => '1',
  926. )
  927. );
  928. $c2 = self::factory()->comment->create(
  929. array(
  930. 'comment_post_ID' => self::$post_id,
  931. 'comment_approved' => '0',
  932. )
  933. );
  934. $q = new WP_Comment_Query();
  935. $found = $q->query(
  936. array(
  937. 'status' => 'hold',
  938. 'fields' => 'ids',
  939. )
  940. );
  941. $this->assertSame( array( $c2 ), $found );
  942. }
  943. /**
  944. * @ticket 21101
  945. */
  946. public function test_status_approve() {
  947. $c1 = self::factory()->comment->create(
  948. array(
  949. 'comment_post_ID' => self::$post_id,
  950. 'comment_approved' => '1',
  951. )
  952. );
  953. $c2 = self::factory()->comment->create(
  954. array(
  955. 'comment_post_ID' => self::$post_id,
  956. 'comment_approved' => '0',
  957. )
  958. );
  959. $q = new WP_Comment_Query();
  960. $found = $q->query(
  961. array(
  962. 'status' => 'approve',
  963. 'fields' => 'ids',
  964. )
  965. );
  966. $this->assertSame( array( $c1 ), $found );
  967. }
  968. public function test_status_custom() {
  969. $c1 = self::factory()->comment->create(
  970. array(
  971. 'comment_post_ID' => self::$post_id,
  972. 'comment_approved' => '1',
  973. )
  974. );
  975. $c2 = self::factory()->comment->create(
  976. array(
  977. 'comment_post_ID' => self::$post_id,
  978. 'comment_approved' => 'foo',
  979. )
  980. );
  981. $c3 = self::factory()->comment->create(
  982. array(
  983. 'comment_post_ID' => self::$post_id,
  984. 'comment_approved' => 'foo1',
  985. )
  986. );
  987. $q = new WP_Comment_Query();
  988. $found = $q->query(
  989. array(
  990. 'status' => 'foo',
  991. 'fields' => 'ids',
  992. )
  993. );
  994. $this->assertSame( array( $c2 ), $found );
  995. }
  996. public function test_status_all() {
  997. $c1 = self::factory()->comment->create(
  998. array(
  999. 'comment_post_ID' => self::$post_id,
  1000. 'comment_approved' => '1',
  1001. )
  1002. );
  1003. $c2 = self::factory()->comment->create(
  1004. array(
  1005. 'comment_post_ID' => self::$post_id,
  1006. 'comment_approved' => 'foo',
  1007. )
  1008. );
  1009. $c3 = self::factory()->comment->create(
  1010. array(
  1011. 'comment_post_ID' => self::$post_id,
  1012. 'comment_approved' => '0',
  1013. )
  1014. );
  1015. $q = new WP_Comment_Query();
  1016. $found = $q->query(
  1017. array(
  1018. 'status' => 'all',
  1019. 'fields' => 'ids',
  1020. )
  1021. );
  1022. $this->assertSameSets( array( $c1, $c3 ), $found );
  1023. }
  1024. public function test_status_default_to_all() {
  1025. $c1 = self::factory()->comment->create(
  1026. array(
  1027. 'comment_post_ID' => self::$post_id,
  1028. 'comment_approved' => '1',
  1029. )
  1030. );
  1031. $c2 = self::factory()->comment->create(
  1032. array(
  1033. 'comment_post_ID' => self::$post_id,
  1034. 'comment_approved' => 'foo',
  1035. )
  1036. );
  1037. $c3 = self::factory()->comment->create(
  1038. array(
  1039. 'comment_post_ID' => self::$post_id,
  1040. 'comment_approved' => '0',
  1041. )
  1042. );
  1043. $q = new WP_Comment_Query();
  1044. $found = $q->query(
  1045. array(
  1046. 'fields' => 'ids',
  1047. )
  1048. );
  1049. $this->assertSameSets( array( $c1, $c3 ), $found );
  1050. }
  1051. /**
  1052. * @ticket 29612
  1053. */
  1054. public function test_status_comma_any() {
  1055. $c1 = self::factory()->comment->create(
  1056. array(
  1057. 'comment_post_ID' => self::$post_id,
  1058. 'comment_approved' => '1',
  1059. )
  1060. );
  1061. $c2 = self::factory()->comment->create(
  1062. array(
  1063. 'comment_post_ID' => self::$post_id,
  1064. 'comment_approved' => 'foo',
  1065. )
  1066. );
  1067. $c3 = self::factory()->comment->create(
  1068. array(
  1069. 'comment_post_ID' => self::$post_id,
  1070. 'comment_approved' => '0',
  1071. )
  1072. );
  1073. $q = new WP_Comment_Query();
  1074. $found = $q->query(
  1075. array(
  1076. 'status' => 'any',
  1077. 'fields' => 'ids',
  1078. )
  1079. );
  1080. $this->assertSameSets( array( $c1, $c2, $c3 ), $found );
  1081. }
  1082. /**
  1083. * @ticket 29612
  1084. */
  1085. public function test_status_comma_separated() {
  1086. $c1 = self::factory()->comment->create(
  1087. array(
  1088. 'comment_post_ID' => self::$post_id,
  1089. 'comment_approved' => '1',
  1090. )
  1091. );
  1092. $c2 = self::factory()->comment->create(
  1093. array(
  1094. 'comment_post_ID' => self::$post_id,
  1095. 'comment_approved' => 'foo',
  1096. )
  1097. );
  1098. $c3 = self::factory()->comment->create(
  1099. array(
  1100. 'comment_post_ID' => self::$post_id,
  1101. 'comment_approved' => '0',
  1102. )
  1103. );
  1104. $q = new WP_Comment_Query();
  1105. $found = $q->query(
  1106. array(
  1107. 'status' => 'approve,foo,bar',
  1108. 'fields' => 'ids',
  1109. )
  1110. );
  1111. $this->assertSameSets( array( $c1, $c2 ), $found );
  1112. }
  1113. /**
  1114. * @ticket 29612
  1115. */
  1116. public function test_status_array() {
  1117. $c1 = self::factory()->comment->create(
  1118. array(
  1119. 'comment_post_ID' => self::$post_id,
  1120. 'comment_approved' => '1',
  1121. )
  1122. );
  1123. $c2 = self::factory()->comment->create(
  1124. array(
  1125. 'comment_post_ID' => self::$post_id,
  1126. 'comment_approved' => 'foo',
  1127. )
  1128. );
  1129. $c3 = self::factory()->comment->create(
  1130. array(
  1131. 'comment_post_ID' => self::$post_id,
  1132. 'comment_approved' => '0',
  1133. )
  1134. );
  1135. $q = new WP_Comment_Query();
  1136. $found = $q->query(
  1137. array(
  1138. 'status' => array( 'approve', 'foo', 'bar' ),
  1139. 'fields' => 'ids',
  1140. )
  1141. );
  1142. $this->assertSameSets( array( $c1, $c2 ), $found );
  1143. }
  1144. /**
  1145. * @ticket 35478
  1146. */
  1147. public function test_multiple_post_fields_should_all_be_respected() {
  1148. $posts = array();
  1149. $posts[] = self::factory()->post->create(
  1150. array(
  1151. 'post_status' => 'publish',
  1152. 'post_author' => 3,
  1153. )
  1154. );
  1155. $posts[] = self::factory()->post->create(
  1156. array(
  1157. 'post_status' => 'draft',
  1158. 'post_author' => 4,
  1159. )
  1160. );
  1161. $posts[] = self::factory()->post->create(
  1162. array(
  1163. 'post_status' => 'draft',
  1164. 'post_author' => 3,
  1165. )
  1166. );
  1167. $comments = array();
  1168. foreach ( $posts as $post ) {
  1169. $comments[] = self::factory()->comment->create(
  1170. array(
  1171. 'comment_post_ID' => $post,
  1172. )
  1173. );
  1174. }
  1175. $q = new WP_Comment_Query(
  1176. array(
  1177. 'post_status' => 'draft',
  1178. 'post_author' => 3,
  1179. 'fields' => 'ids',
  1180. )
  1181. );
  1182. $this->assertSame( array( $comments[2] ), $q->comments );
  1183. }
  1184. function test_get_comments_for_post() {
  1185. $limit = 5;
  1186. $post_id = self::factory()->post->create();
  1187. self::factory()->comment->create_post_comments( $post_id, $limit );
  1188. $comments = get_comments( array( 'post_id' => $post_id ) );
  1189. $this->assertSame( $limit, count( $comments ) );
  1190. foreach ( $comments as $comment ) {
  1191. $this->assertEquals( $post_id, $comment->comment_post_ID );
  1192. }
  1193. $post_id2 = self::factory()->post->create();
  1194. self::factory()->comment->create_post_comments( $post_id2, $limit );
  1195. $comments = get_comments( array( 'post_id' => $post_id2 ) );
  1196. $this->assertSame( $limit, count( $comments ) );
  1197. foreach ( $comments as $comment ) {
  1198. $this->assertEquals( $post_id2, $comment->comment_post_ID );
  1199. }
  1200. $post_id3 = self::factory()->post->create();
  1201. self::factory()->comment->create_post_comments( $post_id3, $limit, array( 'comment_approved' => '0' ) );
  1202. $comments = get_comments( array( 'post_id' => $post_id3 ) );
  1203. $this->assertSame( $limit, count( $comments ) );
  1204. foreach ( $comments as $comment ) {
  1205. $this->assertEquals( $post_id3, $comment->comment_post_ID );
  1206. }
  1207. $comments = get_comments(
  1208. array(
  1209. 'post_id' => $post_id3,
  1210. 'status' => 'hold',
  1211. )
  1212. );
  1213. $this->assertSame( $limit, count( $comments ) );
  1214. foreach ( $comments as $comment ) {
  1215. $this->assertEquals( $post_id3, $comment->comment_post_ID );
  1216. }
  1217. $comments = get_comments(
  1218. array(
  1219. 'post_id' => $post_id3,
  1220. 'status' => 'approve',
  1221. )
  1222. );
  1223. $this->assertSame( 0, count( $comments ) );
  1224. self::factory()->comment->create_post_comments( $post_id3, $limit, array( 'comment_approved' => '1' ) );
  1225. $comments = get_comments( array( 'post_id' => $post_id3 ) );
  1226. $this->assertSame( $limit * 2, count( $comments ) );
  1227. foreach ( $comments as $comment ) {
  1228. $this->assertEquals( $post_id3, $comment->comment_post_ID );
  1229. }
  1230. }
  1231. /**
  1232. * @ticket 21003
  1233. */
  1234. function test_orderby_meta() {
  1235. $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) );
  1236. $comment_id2 = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) );
  1237. $comment_id3 = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) );
  1238. add_comment_meta( $comment_id, 'key', 'value1', true );
  1239. add_comment_meta( $comment_id, 'key1', 'value1', true );
  1240. add_comment_meta( $comment_id, 'key3', 'value3', true );
  1241. add_comment_meta( $comment_id2, 'key', 'value2', true );
  1242. add_comment_meta( $comment_id2, 'key2', 'value2', true );
  1243. add_comment_meta( $comment_id3, 'key3', 'value3', true );
  1244. $comments = get_comments(
  1245. array(
  1246. 'meta_key' => 'key',
  1247. 'orderby' => array( 'key' ),
  1248. )
  1249. );
  1250. $this->assertSame( 2, count( $comments ) );
  1251. $this->assertEquals( $comment_id2, $comments[0]->comment_ID );
  1252. $this->assertEquals( $comment_id, $comments[1]->comment_ID );
  1253. $comments = get_comments(
  1254. array(
  1255. 'meta_key' => 'key',
  1256. 'orderby' => array( 'meta_value' ),
  1257. )
  1258. );
  1259. $this->assertSame( 2, count( $comments ) );
  1260. $this->assertEquals( $comment_id2, $comments[0]->comment_ID );
  1261. $this->assertEquals( $comment_id, $comments[1]->comment_ID );
  1262. $comments = get_comments(
  1263. array(
  1264. 'meta_key' => 'key',
  1265. 'orderby' => array( 'key' ),
  1266. 'order' => 'ASC',
  1267. )
  1268. );
  1269. $this->assertSame( 2, count( $comments ) );
  1270. $this->assertEquals( $comment_id, $comments[0]->comment_ID );
  1271. $this->assertEquals( $comment_id2, $comments[1]->comment_ID );
  1272. $comments = get_comments(
  1273. array(
  1274. 'meta_key' => 'key',
  1275. 'orderby' => array( 'meta_value' ),
  1276. 'order' => 'ASC',
  1277. )
  1278. );
  1279. $this->assertSame( 2, count( $comments ) );
  1280. $this->assertEquals( $comment_id, $comments[0]->comment_ID );
  1281. $this->assertEquals( $comment_id2, $comments[1]->comment_ID );
  1282. $comments = get_comments(
  1283. array(
  1284. 'meta_value' => 'value3',
  1285. 'orderby' => array( 'key' ),
  1286. )
  1287. );
  1288. $this->assertEquals( array( $comment_id3, $comment_id ), wp_list_pluck( $comments, 'comment_ID' ) );
  1289. $comments = get_comments(
  1290. array(
  1291. 'meta_value' => 'value3',
  1292. 'orderby' => array( 'meta_value' ),
  1293. )
  1294. );
  1295. $this->assertEquals( array( $comment_id3, $comment_id ), wp_list_pluck( $comments, 'comment_ID' ) );
  1296. // 'value1' is present on two different keys for $comment_id,
  1297. // yet we should get only one instance of that comment in the results.
  1298. $comments = get_comments(
  1299. array(
  1300. 'meta_value' => 'value1',
  1301. 'orderby' => array( 'key' ),
  1302. )
  1303. );
  1304. $this->assertSame( 1, count( $comments ) );
  1305. $comments = get_comments(
  1306. array(
  1307. 'meta_value' => 'value1',
  1308. 'orderby' => array( 'meta_value' ),
  1309. )
  1310. );
  1311. $this->assertSame( 1, count( $comments ) );
  1312. }
  1313. /**
  1314. * @ticket 30478
  1315. */
  1316. public function test_orderby_clause_key() {
  1317. $comments = self::factory()->comment->create_many( 3 );
  1318. add_comment_meta( $comments[0], 'foo', 'aaa' );
  1319. add_comment_meta( $comments[1], 'foo', 'zzz' );
  1320. add_comment_meta( $comments[2], 'foo', 'jjj' );
  1321. $q = new WP_Comment_Query();
  1322. $found = $q->query(
  1323. array(
  1324. 'fields' => 'ids',
  1325. 'meta_query' => array(
  1326. 'foo_key' => array(
  1327. 'key' => 'foo',
  1328. 'compare' => 'EXISTS',
  1329. ),
  1330. ),
  1331. 'orderby' => 'foo_key',
  1332. 'order' => 'DESC',
  1333. )
  1334. );
  1335. $this->assertSame( array( $comments[1], $comments[2], $comments[0] ), $found );
  1336. }
  1337. /**
  1338. * @ticket 30478
  1339. */
  1340. public function test_orderby_clause_key_as_secondary_sort() {
  1341. $c1 = self::factory()->comment->create(
  1342. array(
  1343. 'comment_date' => '2015-01-28 03:00:00',
  1344. )
  1345. );
  1346. $c2 = self::factory()->comment->create(
  1347. array(
  1348. 'comment_date' => '2015-01-28 05:00:00',
  1349. )
  1350. );
  1351. $c3 = self::factory()->comment->create(
  1352. array(
  1353. 'comment_date' => '2015-01-28 03:00:00',
  1354. )
  1355. );
  1356. add_comment_meta( $c1, 'foo', 'jjj' );
  1357. add_comment_meta( $c2, 'foo', 'zzz' );
  1358. add_comment_meta( $c3, 'foo', 'aaa' );
  1359. $q = new WP_Comment_Query();
  1360. $found = $q->query(
  1361. array(
  1362. 'fields' => 'ids',
  1363. 'meta_query' => array(
  1364. 'foo_key' => array(
  1365. 'key' => 'foo',
  1366. 'compare' => 'EXISTS',
  1367. ),
  1368. ),
  1369. 'orderby' => array(
  1370. 'comment_date' => 'asc',
  1371. 'foo_key' => 'asc',
  1372. ),
  1373. )
  1374. );
  1375. $this->assertSame( array( $c3, $c1, $c2 ), $found );
  1376. }
  1377. /**
  1378. * @ticket 30478
  1379. */
  1380. public function test_orderby_more_than_one_clause_key() {
  1381. $comments = self::factory()->comment->create_many( 3 );
  1382. add_comment_meta( $comments[0], 'foo', 'jjj' );
  1383. add_comment_meta( $comments[1], 'foo', 'zzz' );
  1384. add_comment_meta( $comments[2], 'foo', 'jjj' );
  1385. add_comment_meta( $comments[0], 'bar', 'aaa' );
  1386. add_comment_meta( $comments[1], 'bar', 'ccc' );
  1387. add_comment_meta( $comments[2], 'bar', 'bbb' );
  1388. $q = new WP_Comment_Query();
  1389. $found = $q->query(
  1390. array(
  1391. 'fields' => 'ids',
  1392. 'meta_query' => array(
  1393. 'foo_key' => array(
  1394. 'key' => 'foo',
  1395. 'compare' => 'EXISTS',
  1396. ),
  1397. 'bar_key' => array(
  1398. 'key' => 'bar',
  1399. 'compare' => 'EXISTS',
  1400. ),
  1401. ),
  1402. 'orderby' => array(
  1403. 'foo_key' => 'asc',
  1404. 'bar_key' => 'desc',
  1405. ),
  1406. )
  1407. );
  1408. $this->assertSame( array( $comments[2], $comments[0], $comments[1] ), $found );
  1409. }
  1410. /**
  1411. * @ticket 32081
  1412. */
  1413. public function test_meta_query_should_work_with_comment__in() {
  1414. $comments = self::factory()->comment->create_many( 3 );
  1415. add_comment_meta( $comments[0], 'foo', 'jjj' );
  1416. add_comment_meta( $comments[1], 'foo', 'zzz' );
  1417. add_comment_meta( $comments[2], 'foo', 'jjj' );
  1418. $q = new WP_Comment_Query(
  1419. array(
  1420. 'comment__in' => array( $comments[1], $comments[2] ),
  1421. 'meta_query' => array(
  1422. array(
  1423. 'key' => 'foo',
  1424. 'value' => 'jjj',
  1425. ),
  1426. ),
  1427. 'fields' => 'ids',
  1428. )
  1429. );
  1430. $this->assertSame( array( $comments[2] ), $q->get_comments() );
  1431. }
  1432. /**
  1433. * @ticket 32081
  1434. */
  1435. public function test_meta_query_should_work_with_comment__not_in() {
  1436. $comments = self::factory()->comment->create_many( 3 );
  1437. add_comment_meta( $comments[0], 'foo', 'jjj' );
  1438. add_comment_meta( $comments[1], 'foo', 'zzz' );
  1439. add_comment_meta( $comments[2], 'foo', 'jjj' );
  1440. $q = new WP_Comment_Query(
  1441. array(
  1442. 'comment__not_in' => array( $comments[1], $comments[2] ),
  1443. 'meta_query' => array(
  1444. array(
  1445. 'key' => 'foo',
  1446. 'value' => 'jjj',
  1447. ),
  1448. ),
  1449. 'fields' => 'ids',
  1450. )
  1451. );
  1452. $this->assertSame( array( $comments[0] ), $q->get_comments() );
  1453. }
  1454. /**
  1455. * @ticket 27064
  1456. */
  1457. function test_get_comments_by_user() {
  1458. $users = self::factory()->user->create_many( 2 );
  1459. self::factory()->comment->create(
  1460. array(
  1461. 'user_id' => $users[0],
  1462. 'comment_post_ID' => self::$post_id,
  1463. 'comment_approved' => '1',
  1464. )
  1465. );
  1466. self::factory()->comment->create(
  1467. array(
  1468. 'user_id' => $users[0],
  1469. 'comment_post_ID' => self::$post_id,
  1470. 'comment_approved' => '1',
  1471. )
  1472. );
  1473. self::factory()->comment->create(
  1474. array(
  1475. 'user_id' => $users[1],
  1476. 'comment_post_ID' => self::$post_id,
  1477. 'comment_approved' => '1',
  1478. )
  1479. );
  1480. $comments = get_comments(
  1481. array(
  1482. 'user_id' => $users[0],
  1483. 'orderby' => 'comment_ID',
  1484. 'order' => 'ASC',
  1485. )
  1486. );
  1487. $this->assertCount( 2, $comments );
  1488. $this->assertEquals( $users[0], $comments[0]->user_id );
  1489. $this->assertEquals( $users[0], $comments[1]->user_id );
  1490. $comments = get_comments(
  1491. array(
  1492. 'user_id' => $users,
  1493. 'orderby' => 'comment_ID',
  1494. 'order' => 'ASC',
  1495. )
  1496. );
  1497. $this->assertCount( 3, $comments );
  1498. $this->assertEquals( $users[0], $comments[0]->user_id );
  1499. $this->assertEquals( $users[0], $comments[1]->user_id );
  1500. $this->assertEquals( $users[1], $comments[2]->user_id );
  1501. }
  1502. /**
  1503. * @ticket 35377
  1504. */
  1505. public function test_get_comments_by_author_url() {
  1506. $c1 = self::factory()->comment->create(
  1507. array(
  1508. 'comment_post_ID' => self::$post_id,
  1509. 'comment_author' => 'bar',
  1510. 'comment_author_email' => 'bar@example.com',
  1511. 'comment_author_url' => 'http://foo.bar',
  1512. )
  1513. );
  1514. $c2 = self::factory()->comment->create(
  1515. array(
  1516. 'comment_post_ID' => self::$post_id,
  1517. 'comment_author' => 'bar',
  1518. 'comment_author_email' => 'bar@example.com',
  1519. 'comment_author_url' => 'http://foo.bar',
  1520. )
  1521. );
  1522. $c3 = self::factory()->comment->create(
  1523. array(
  1524. 'comment_post_ID' => self::$post_id,
  1525. 'comment_author' => 'bar',
  1526. 'comment_author_email' => 'bar@example.com',
  1527. 'comment_author_url' => 'http://foo.bar/baz',
  1528. )
  1529. );
  1530. $comments = get_comments(
  1531. array(
  1532. 'author_url' => 'http://foo.bar',
  1533. 'fields' => 'ids',
  1534. )
  1535. );
  1536. $this->assertSameSets( array( $c1, $c2 ), $comments );
  1537. }
  1538. /**
  1539. * @ticket 28434
  1540. */
  1541. function test_fields_ids_query() {
  1542. $comment_1 = self::factory()->comment->create(
  1543. array(
  1544. 'comment_post_ID' => self::$post_id,
  1545. 'user_id' => 7,
  1546. 'comment_approved' => '1',
  1547. )
  1548. );
  1549. $comment_2 = self::factory()->comment->create(
  1550. array(
  1551. 'comment_post_ID' => self::$post_id,
  1552. 'user_id' => 1,
  1553. 'comment_approved' => '1',
  1554. )
  1555. );
  1556. $comment_3 = self::factory()->comment->create(
  1557. array(
  1558. 'comment_post_ID' => self::$post_id,
  1559. 'user_id' => 1,
  1560. 'comment_approved' => '1',
  1561. )
  1562. );
  1563. // Ensure we are dealing with integers, and not objects.
  1564. $this->assertInternalType( 'integer', $comment_1 );
  1565. $this->assertInternalType( 'integer', $comment_2 );
  1566. $this->assertInternalType( 'integer', $comment_3 );
  1567. $comment_ids = get_comments( array( 'fields' => 'ids' ) );
  1568. $this->assertCount( 3, $comment_ids );
  1569. $this->assertSameSets( array( $comment_1, $comment_2, $comment_3 ), $comment_ids );
  1570. }
  1571. /**
  1572. * @ticket 29189
  1573. */
  1574. function test_fields_comment__in() {
  1575. $comment_1 = self::factory()->comment->create(
  1576. array(
  1577. 'comment_post_ID' => self::$post_id,
  1578. 'user_id' => 7,
  1579. 'comment_approved' => '1',
  1580. )
  1581. );
  1582. $comment_2 = self::factory()->comment->create(
  1583. array(
  1584. 'comment_post_ID' => self::$post_id,
  1585. 'user_id' => 1,
  1586. 'comment_approved' => '1',
  1587. )
  1588. );
  1589. $comment_3 = self::factory()->comment->create(
  1590. array(
  1591. 'comment_post_ID' => self::$post_id,
  1592. 'user_id' => 1,
  1593. 'comment_approved' => '1',
  1594. )
  1595. );
  1596. $comment_ids = get_comments(
  1597. array(
  1598. 'fields' => 'ids',
  1599. 'comment__in' => array( $comment_1, $comment_3 ),
  1600. )
  1601. );
  1602. $this->assertSameSets( array( $comment_1, $comment_3 ), $comment_ids );
  1603. }
  1604. /**
  1605. * @ticket 29189
  1606. */
  1607. function test_fields_comment__not_in() {
  1608. $comment_1 = self::factory()->comment->create(
  1609. array(
  1610. 'comment_post_ID' => self::$post_id,
  1611. 'user_id' => 7,
  1612. 'comment_approved' => '1',
  1613. )
  1614. );
  1615. $comment_2 = self::factory()->comment->create(
  1616. array(
  1617. 'comment_post_ID' => self::$post_id,
  1618. 'user_id' => 1,
  1619. 'comment_approved' => '1',
  1620. )
  1621. );
  1622. $comment_3 = self::factory()->comment->create(
  1623. array(
  1624. 'comment_post_ID' => self::$post_id,
  1625. 'user_id' => 1,
  1626. 'comment_approved' => '1',
  1627. )
  1628. );
  1629. $comment_ids = get_comments(
  1630. array(
  1631. 'fields' => 'ids',
  1632. 'comment__not_in' => array( $comment_2, $comment_3 ),
  1633. )
  1634. );
  1635. $this->assertSameSets( array( $comment_1 ), $comment_ids );
  1636. }
  1637. /**
  1638. * @ticket 29189
  1639. */
  1640. function test_fields_post__in() {
  1641. $p1 = self::factory()->post->create();
  1642. $p2 = self::factory()->post->create();
  1643. $p3 = self::factory()->post->create();
  1644. $c1 = self::factory()->comment->create(
  1645. array(
  1646. 'comment_post_ID' => $p1,
  1647. 'user_id' => 7,
  1648. 'comment_approved' => '1',
  1649. )
  1650. );
  1651. $c2 = self::factory()->comment->create(
  1652. array(
  1653. 'comment_post_ID' => $p2,
  1654. 'user_id' => 1,
  1655. 'comment_approved' => '1',
  1656. )
  1657. );
  1658. $c3 = self::factory()->comment->create(
  1659. array(
  1660. 'comment_post_ID' => $p3,
  1661. 'user_id' => 1,
  1662. 'comment_approved' => '1',
  1663. )
  1664. );
  1665. $comment_ids = get_comments(
  1666. array(
  1667. 'fields' => 'ids',
  1668. 'post__in' => array( $p1, $p2 ),
  1669. )
  1670. );
  1671. $this->assertSameSets( array( $c1, $c2 ), $comment_ids );
  1672. }
  1673. /**
  1674. * @ticket 29189
  1675. */
  1676. function test_fields_post__not_in() {
  1677. $p1 = self::factory()->post->create();
  1678. $p2 = self::factory()->post->create();
  1679. $p3 = self::factory()->post->create();
  1680. $c1 = self::factory()->comment->create(
  1681. array(
  1682. 'comment_post_ID' => $p1,
  1683. 'user_id' => 7,
  1684. 'comment_approved' => '1',
  1685. )
  1686. );
  1687. $c2 = self::factory()->comment->create(
  1688. array(
  1689. 'comment_post_ID' => $p2,
  1690. 'user_id' => 1,
  1691. 'comment_approved' => '1',
  1692. )
  1693. );
  1694. $c3 = self::factory()->comment->create(
  1695. array(
  1696. 'comment_post_ID' => $p3,
  1697. 'user_id' => 1,
  1698. 'comment_approved' => '1',
  1699. )
  1700. );
  1701. $comment_ids = get_comments(
  1702. array(
  1703. 'fields' => 'ids',
  1704. 'post__not_in' => array( $p1, $p2 ),
  1705. )
  1706. );
  1707. $this->assertSameSets( array( $c3 ), $comment_ids );
  1708. }
  1709. /**
  1710. * @ticket 29885
  1711. */
  1712. function test_fields_post_author__in() {
  1713. $author_id1 = 105;
  1714. $author_id2 = 106;
  1715. $p1 = self::factory()->post->create( array( 'post_author' => $author_id1 ) );
  1716. $p2 = self::factory()->post->create( array( 'post_author' => $author_id1 ) );
  1717. $p3 = self::factory()->post->create( array( 'post_author' => $author_id2 ) );
  1718. $c1 = self::factory()->comment->create(
  1719. array(
  1720. 'comment_post_ID' => $p1,
  1721. 'user_id' => 1,
  1722. 'comment_approved' => '1',
  1723. )
  1724. );
  1725. $c2 = self::factory()->comment->create(
  1726. array(
  1727. 'comment_post_ID' => $p2,
  1728. 'user_id' => 1,
  1729. 'comment_approved' => '1',
  1730. )
  1731. );
  1732. $c3 = self::factory()->comment->create(
  1733. array(
  1734. 'comment_post_ID' => $p3,
  1735. 'user_id' => 1,
  1736. 'comment_approved' => '1',
  1737. )
  1738. );
  1739. $comment_ids = get_comments(
  1740. array(
  1741. 'fields' => 'ids',
  1742. 'post_author__in' => array( $author_id1 ),
  1743. )
  1744. );
  1745. $this->assertSameSets( array( $c1, $c2 ), $comment_ids );
  1746. }
  1747. /**
  1748. * @ticket 29885
  1749. */
  1750. function test_fields_post_author__not_in() {
  1751. $author_id1 = 111;
  1752. $author_id2 = 112;
  1753. $p1 = self::factory()->post->create( array( 'post_author' => $author_id1 ) );
  1754. $p2 = self::factory()->post->create( array( 'post_author' => $author_id1 ) );
  1755. $p3 = self::factory()->post->create( array( 'post_author' => $author_id2 ) );
  1756. $c1 = self::factory()->comment->create(
  1757. array(
  1758. 'comment_post_ID' => $p1,
  1759. 'user_id' => 1,
  1760. 'comment_approved' => '1',
  1761. )
  1762. );
  1763. $c2 = self::factory()->comment->create(
  1764. array(
  1765. 'comment_post_ID' => $p2,
  1766. 'user_id' => 1,
  1767. 'comment_approved' => '1',
  1768. )
  1769. );
  1770. $c3 = self::factory()->comment->create(
  1771. array(
  1772. 'comment_post_ID' => $p3,
  1773. 'user_id' => 1,
  1774. 'comment_approved' => '1',
  1775. )
  1776. );
  1777. $comment_ids = get_comments(
  1778. array(
  1779. 'fields' => 'ids',
  1780. 'post_author__not_in' => array( $author_id1 ),
  1781. )
  1782. );
  1783. $this->assertSameSets( array( $c3 ), $comment_ids );
  1784. }
  1785. /**
  1786. * @ticket 29885
  1787. */
  1788. function test_fields_author__in() {
  1789. $p1 = self::factory()->post->create();
  1790. $p2 = self::factory()->post->create();
  1791. $p3 = self::factory()->post->create();
  1792. $p4 = self::factory()->post->create();
  1793. $c1 = self::factory()->comment->create(
  1794. array(
  1795. 'comment_post_ID' => $p1,
  1796. 'user_id' => 1,
  1797. 'comment_approved' => '1',
  1798. )
  1799. );
  1800. $c2 = self::factory()->comment->create(
  1801. array(
  1802. 'comment_post_ID' => $p1,
  1803. 'user_id' => 2,
  1804. 'comment_approved' => '1',
  1805. )
  1806. );
  1807. $c3 = self::factory()->comment->create(
  1808. array(
  1809. 'comment_post_ID' => $p2,
  1810. 'user_id' => 3,
  1811. 'comment_approved' => '1',
  1812. )
  1813. );
  1814. $c4 = self::factory()->comment->create(
  1815. array(
  1816. 'comment_post_ID' => $p4,
  1817. 'user_id' => 4,
  1818. 'comment_approved' => '1',
  1819. )
  1820. );
  1821. $comment_ids = get_comments(
  1822. array(
  1823. 'fields' => 'ids',
  1824. 'author__in' => array( 1, 3 ),
  1825. )
  1826. );
  1827. $this->assertSameSets( array( $c1, $c3 ), $comment_ids );
  1828. }
  1829. /**
  1830. * @ticket 29885
  1831. */
  1832. function test_fields_author__not_in() {
  1833. $p1 = self::factory()->post->create();
  1834. $p2 = self::factory()->post->create();
  1835. $p3 = self::factory()->post->create();
  1836. $p4 = self::factory()->post->create();
  1837. $c1 = self::factory()->comment->create(
  1838. array(
  1839. 'comment_post_ID' => $p1,
  1840. 'user_id' => 1,
  1841. 'comment_approved' => '1',
  1842. )
  1843. );
  1844. $c2 = self::factory()->comment->create(
  1845. array(
  1846. 'comment_post_ID' => $p1,
  1847. 'user_id' => 2,
  1848. 'comment_approved' => '1',
  1849. )
  1850. );
  1851. $c3 = self::factory()->comment->create(
  1852. array(
  1853. 'comment_post_ID' => $p2,
  1854. 'user_id' => 3,
  1855. 'comment_approved' => '1',
  1856. )
  1857. );
  1858. $c4 = self::factory()->comment->create(
  1859. array(
  1860. 'comment_post_ID' => $p4,
  1861. 'user_id' => 4,
  1862. 'comment_approved' => '1',
  1863. )
  1864. );
  1865. $comment_ids = get_comments(
  1866. array(
  1867. 'fields' => 'ids',
  1868. 'author__not_in' => array( 1, 2 ),
  1869. )
  1870. );
  1871. $this->assertSameSets( array( $c3, $c4 ), $comment_ids );
  1872. }
  1873. /**
  1874. * @ticket 19623
  1875. */
  1876. public function test_get_comments_with_status_all() {
  1877. $comment_1 = self::factory()->comment->create(
  1878. array(
  1879. 'comment_post_ID' => self::$post_id,
  1880. 'user_id' => 7,
  1881. 'comment_approved' => '1',
  1882. )
  1883. );
  1884. $comment_2 = self::factory()->comment->create(
  1885. array(
  1886. 'comment_post_ID' => self::$post_id,
  1887. 'user_id' => 1,
  1888. 'comment_approved' => '1',
  1889. )
  1890. );
  1891. $comment_3 = self::factory()->comment->create(
  1892. array(
  1893. 'comment_post_ID' => self::$post_id,
  1894. 'user_id' => 1,
  1895. 'comment_approved' => '0',
  1896. )
  1897. );
  1898. $comments_approved_1 = get_comments( array( 'status' => 'all' ) );
  1899. $comment_ids = get_comments( array( 'fields' => 'ids' ) );
  1900. $this->assertSameSets( array( $comment_1, $comment_2, $comment_3 ), $comment_ids );
  1901. }
  1902. /**
  1903. * @ticket 19623
  1904. */
  1905. public function test_get_comments_with_include_unapproved_user_id() {
  1906. $c1 = self::factory()->comment->create(
  1907. array(
  1908. 'comment_post_ID' => self::$post_id,
  1909. 'user_id' => 7,
  1910. 'comment_approved' => '1',
  1911. )
  1912. );
  1913. $c2 = self::factory()->comment->create(
  1914. array(
  1915. 'comment_post_ID' => self::$post_id,
  1916. 'user_id' => 1,
  1917. 'comment_approved' => '1',
  1918. )
  1919. );
  1920. $c3 = self::factory()->comment->create(
  1921. array(
  1922. 'comment_post_ID' => self::$post_id,
  1923. 'user_id' => 1,
  1924. 'comment_approved' => '0',
  1925. )
  1926. );
  1927. $c4 = self::factory()->comment->create(
  1928. array(
  1929. 'comment_post_ID' => self::$post_id,
  1930. 'user_id' => 6,
  1931. 'comment_approved' => '0',
  1932. )
  1933. );
  1934. $found = get_comments(
  1935. array(
  1936. 'fields' => 'ids',
  1937. 'include_unapproved' => 1,
  1938. 'status' => 'approve',
  1939. )
  1940. );
  1941. $this->assertSameSets( array( $c1, $c2, $c3 ), $found );
  1942. }
  1943. /**
  1944. * @ticket 19623
  1945. */
  1946. public function test_get_comments_with_include_unapproved_user_id_array() {
  1947. $c1 = self::factory()->comment->create(
  1948. array(
  1949. 'comment_post_ID' => self::$post_id,
  1950. 'user_id' => 7,
  1951. 'comment_approved' => '1',
  1952. )
  1953. );
  1954. $c2 = self::factory()->comment->create(
  1955. array(
  1956. 'comment_post_ID' => self::$post_id,
  1957. 'user_id' => 1,
  1958. 'comment_approved' => '1',
  1959. )
  1960. );
  1961. $c3 = self::factory()->comment->create(
  1962. array(
  1963. 'comment_post_ID' => self::$post_id,
  1964. 'user_id' => 1,
  1965. 'comment_approved' => '0',
  1966. )
  1967. );
  1968. $c4 = self::factory()->comment->create(
  1969. array(
  1970. 'comment_post_ID' => self::$post_id,
  1971. 'user_id' => 6,
  1972. 'comment_approved' => '0',
  1973. )
  1974. );
  1975. $c5 = self::factory()->comment->create(
  1976. array(
  1977. 'comment_post_ID' => self::$post_id,
  1978. 'user_id' => 8,
  1979. 'comment_approved' => '0',
  1980. )
  1981. );
  1982. $found = get_comments(
  1983. array(
  1984. 'fields' => 'ids',
  1985. 'include_unapproved' => array( 1, 8 ),
  1986. 'status' => 'approve',
  1987. )
  1988. );
  1989. $this->assertSameSets( array( $c1, $c2, $c3, $c5 ), $found );
  1990. }
  1991. /**
  1992. * @ticket 19623
  1993. */
  1994. public function test_get_comments_with_include_unapproved_user_id_comma_separated() {
  1995. $c1 = self::factory()->comment->create(
  1996. array(
  1997. 'comment_post_ID' => self::$post_id,
  1998. 'user_id' => 7,
  1999. 'comment_approved' => '1',
  2000. )
  2001. );
  2002. $c2 = self::factory()->comment->create(
  2003. array(
  2004. 'comment_post_ID' => self::$post_id,
  2005. 'user_id' => 1,
  2006. 'comment_approved' => '1',
  2007. )
  2008. );
  2009. $c3 = self::factory()->comment->create(
  2010. array(
  2011. 'comment_post_ID' => self::$post_id,
  2012. 'user_id' => 1,
  2013. 'comment_approved' => '0',
  2014. )
  2015. );
  2016. $c4 = self::factory()->comment->create(
  2017. array(
  2018. 'comment_post_ID' => self::$post_id,
  2019. 'user_id' => 6,
  2020. 'comment_approved' => '0',
  2021. )
  2022. );
  2023. $c5 = self::factory()->comment->create(
  2024. array(
  2025. 'comment_post_ID' => self::$post_id,
  2026. 'user_id' => 8,
  2027. 'comment_approved' => '0',
  2028. )
  2029. );
  2030. $found = get_comments(
  2031. array(
  2032. 'fields' => 'ids',
  2033. 'include_unapproved' => '1,8',
  2034. 'status' => 'approve',
  2035. )
  2036. );
  2037. $this->assertSameSets( array( $c1, $c2, $c3, $c5 ), $found );
  2038. }
  2039. /**
  2040. * @ticket 19623
  2041. */
  2042. public function test_get_comments_with_include_unapproved_author_email() {
  2043. $c1 = self::factory()->comment->create(
  2044. array(
  2045. 'comment_post_ID' => self::$post_id,
  2046. 'user_id' => 7,
  2047. 'comment_approved' => '1',
  2048. )
  2049. );
  2050. $c2 = self::factory()->comment->create(
  2051. array(
  2052. 'comment_post_ID' => self::$post_id,
  2053. 'user_id' => 0,
  2054. 'comment_approved' => '1',
  2055. 'comment_author' => 'foo',
  2056. 'comment_author_email' => 'foo@example.com',
  2057. )
  2058. );
  2059. $c3 = self::factory()->comment->create(
  2060. array(
  2061. 'comment_post_ID' => self::$post_id,
  2062. 'user_id' => 0,
  2063. 'comment_approved' => '0',
  2064. 'comment_author' => 'foo',
  2065. 'comment_author_email' => 'foo@example.com',
  2066. )
  2067. );
  2068. $c4 = self::factory()->comment->create(
  2069. array(
  2070. 'comment_post_ID' => self::$post_id,
  2071. 'user_id' => 0,
  2072. 'comment_approved' => '0',
  2073. 'comment_author' => 'foo',
  2074. 'comment_author_email' => 'bar@example.com',
  2075. )
  2076. );
  2077. $found = get_comments(
  2078. array(
  2079. 'fields' => 'ids',
  2080. 'include_unapproved' => 'foo@example.com',
  2081. 'status' => 'approve',
  2082. )
  2083. );
  2084. $this->assertSameSets( array( $c1, $c2, $c3 ), $found );
  2085. }
  2086. /**
  2087. * @ticket 19623
  2088. */
  2089. public function test_get_comments_with_include_unapproved_mixed_array() {
  2090. $c1 = self::factory()->comment->create(
  2091. array(
  2092. 'comment_post_ID' => self::$post_id,
  2093. 'user_id' => 7,
  2094. 'comment_approved' => '1',
  2095. )
  2096. );
  2097. $c2 = self::factory()->comment->create(
  2098. array(
  2099. 'comment_post_ID' => self::$post_id,
  2100. 'user_id' => 0,
  2101. 'comment_approved' => '1',
  2102. 'comment_author' => 'foo',
  2103. 'comment_author_email' => 'foo@example.com',
  2104. )
  2105. );
  2106. $c3 = self::factory()->comment->create(
  2107. array(
  2108. 'comment_post_ID' => self::$post_id,
  2109. 'user_id' => 0,
  2110. 'comment_approved' => '0',
  2111. 'comment_author' => 'foo',
  2112. 'comment_author_email' => 'foo@example.com',
  2113. )
  2114. );
  2115. $c4 = self::factory()->comment->create(
  2116. array(
  2117. 'comment_post_ID' => self::$post_id,
  2118. 'user_id' => 0,
  2119. 'comment_approved' => '0',
  2120. 'comment_author' => 'foo',
  2121. 'comment_author_email' => 'bar@example.com',
  2122. )
  2123. );
  2124. $c5 = self::factory()->comment->create(
  2125. array(
  2126. 'comment_post_ID' => self::$post_id,
  2127. 'user_id' => 4,
  2128. 'comment_approved' => '0',
  2129. 'comment_author' => 'foo',
  2130. 'comment_author_email' => 'bar@example.com',
  2131. )
  2132. );
  2133. $found = get_comments(
  2134. array(
  2135. 'fields' => 'ids',
  2136. 'include_unapproved' => array( 'foo@example.com', 4 ),
  2137. 'status' => 'approve',
  2138. )
  2139. );
  2140. $this->assertSameSets( array( $c1, $c2, $c3, $c5 ), $found );
  2141. }
  2142. /**
  2143. * @ticket 19623
  2144. */
  2145. public function test_get_comments_with_include_unapproved_mixed_comma_separated() {
  2146. $c1 = self::factory()->comment->create(
  2147. array(
  2148. 'comment_post_ID' => self::$post_id,
  2149. 'user_id' => 7,
  2150. 'comment_approved' => '1',
  2151. )
  2152. );
  2153. $c2 = self::factory()->comment->create(
  2154. array(
  2155. 'comment_post_ID' => self::$post_id,
  2156. 'user_id' => 0,
  2157. 'comment_approved' => '1',
  2158. 'comment_author' => 'foo',
  2159. 'comment_author_email' => 'foo@example.com',
  2160. )
  2161. );
  2162. $c3 = self::factory()->comment->create(
  2163. array(
  2164. 'comment_post_ID' => self::$post_id,
  2165. 'user_id' => 0,
  2166. 'comment_approved' => '0',
  2167. 'comment_author' => 'foo',
  2168. 'comment_author_email' => 'foo@example.com',
  2169. )
  2170. );
  2171. $c4 = self::factory()->comment->create(
  2172. array(
  2173. 'comment_post_ID' => self::$post_id,
  2174. 'user_id' => 0,
  2175. 'comment_approved' => '0',
  2176. 'comment_author' => 'foo',
  2177. 'comment_author_email' => 'bar@example.com',
  2178. )
  2179. );
  2180. $c5 = self::factory()->comment->create(
  2181. array(
  2182. 'comment_post_ID' => self::$post_id,
  2183. 'user_id' => 4,
  2184. 'comment_approved' => '0',
  2185. 'comment_author' => 'foo',
  2186. 'comment_author_email' => 'bar@example.com',
  2187. )
  2188. );
  2189. $found = get_comments(
  2190. array(
  2191. 'fields' => 'ids',
  2192. 'include_unapproved' => 'foo@example.com, 4',
  2193. 'status' => 'approve',
  2194. )
  2195. );
  2196. $this->assertSameSets( array( $c1, $c2, $c3, $c5 ), $found );
  2197. }
  2198. public function test_search() {
  2199. $c1 = self::factory()->comment->create(
  2200. array(
  2201. 'comment_post_ID' => self::$post_id,
  2202. 'user_id' => 4,
  2203. 'comment_approved' => '0',
  2204. 'comment_author' => 'foo',
  2205. 'comment_author_email' => 'bar@example.com',
  2206. )
  2207. );
  2208. $c2 = self::factory()->comment->create(
  2209. array(
  2210. 'comment_post_ID' => self::$post_id,
  2211. 'user_id' => 4,
  2212. 'comment_approved' => '0',
  2213. 'comment_author' => 'bar',
  2214. 'comment_author_email' => 'foo@example.com',
  2215. )
  2216. );
  2217. $c3 = self::factory()->comment->create(
  2218. array(
  2219. 'comment_post_ID' => self::$post_id,
  2220. 'user_id' => 4,
  2221. 'comment_approved' => '0',
  2222. 'comment_author' => 'bar',
  2223. 'comment_author_email' => 'bar@example.com',
  2224. 'comment_author_url' => 'http://foo.bar',
  2225. )
  2226. );
  2227. $c4 = self::factory()->comment->create(
  2228. array(
  2229. 'comment_post_ID' => self::$post_id,
  2230. 'user_id' => 4,
  2231. 'comment_approved' => '0',
  2232. 'comment_author' => 'bar',
  2233. 'comment_author_email' => 'bar@example.com',
  2234. 'comment_author_url' => 'http://example.com',
  2235. 'comment_author_IP' => 'foo.bar',
  2236. )
  2237. );
  2238. $c5 = self::factory()->comment->create(
  2239. array(
  2240. 'comment_post_ID' => self::$post_id,
  2241. 'user_id' => 4,
  2242. 'comment_approved' => '0',
  2243. 'comment_author' => 'bar',
  2244. 'comment_author_email' => 'bar@example.com',
  2245. 'comment_author_url' => 'http://example.com',
  2246. 'comment_content' => 'Nice foo comment',
  2247. )
  2248. );
  2249. $c6 = self::factory()->comment->create(
  2250. array(
  2251. 'comment_post_ID' => self::$post_id,
  2252. 'user_id' => 4,
  2253. 'comment_approved' => '0',
  2254. 'comment_author' => 'bar',
  2255. 'comment_author_email' => 'bar@example.com',
  2256. 'comment_author_url' => 'http://example.com',
  2257. )
  2258. );
  2259. $q = new WP_Comment_Query();
  2260. $found = $q->query(
  2261. array(
  2262. 'search' => 'foo',
  2263. 'fields' => 'ids',
  2264. )
  2265. );
  2266. $this->assertSameSets( array( $c1, $c2, $c3, $c4, $c5 ), $found );
  2267. }
  2268. /**
  2269. * @ticket 35513
  2270. */
  2271. public function test_search_false_should_be_ignored() {
  2272. $q = new WP_Comment_Query();
  2273. $q->query(
  2274. array(
  2275. 'search' => false,
  2276. )
  2277. );
  2278. $this->assertNotContains( 'comment_author LIKE', $q->request );
  2279. }
  2280. /**
  2281. * @ticket 35513
  2282. */
  2283. public function test_search_null_should_be_ignored() {
  2284. $q = new WP_Comment_Query();
  2285. $q->query(
  2286. array(
  2287. 'search' => null,
  2288. )
  2289. );
  2290. $this->assertNotContains( 'comment_author LIKE', $q->request );
  2291. }
  2292. /**
  2293. * @ticket 35513
  2294. */
  2295. public function test_search_empty_string_should_be_ignored() {
  2296. $q = new WP_Comment_Query();
  2297. $q->query(
  2298. array(
  2299. 'search' => false,
  2300. )
  2301. );
  2302. $this->assertNotContains( 'comment_author LIKE', $q->request );
  2303. }
  2304. /**
  2305. * @ticket 35513
  2306. */
  2307. public function test_search_int_0_should_not_be_ignored() {
  2308. global $wpdb;
  2309. $q = new WP_Comment_Query();
  2310. $q->query(
  2311. array(
  2312. 'search' => 0,
  2313. )
  2314. );
  2315. $this->assertContains( "comment_author LIKE '%0%'", $wpdb->remove_placeholder_escape( $q->request ) );
  2316. }
  2317. /**
  2318. * @ticket 35513
  2319. */
  2320. public function test_search_string_0_should_not_be_ignored() {
  2321. global $wpdb;
  2322. $q = new WP_Comment_Query();
  2323. $q->query(
  2324. array(
  2325. 'search' => '0',
  2326. )
  2327. );
  2328. $this->assertContains( "comment_author LIKE '%0%'", $wpdb->remove_placeholder_escape( $q->request ) );
  2329. }
  2330. public function test_orderby_default() {
  2331. global $wpdb;
  2332. $q = new WP_Comment_Query();
  2333. $q->query( array() );
  2334. $this->assertContains( "ORDER BY $wpdb->comments.comment_date_gmt", $q->request );
  2335. }
  2336. public function test_orderby_single() {
  2337. global $wpdb;
  2338. $q = new WP_Comment_Query();
  2339. $q->query(
  2340. array(
  2341. 'orderby' => 'comment_agent',
  2342. )
  2343. );
  2344. $this->assertContains( "ORDER BY $wpdb->comments.comment_agent", $q->request );
  2345. }
  2346. public function test_orderby_single_invalid() {
  2347. global $wpdb;
  2348. $q = new WP_Comment_Query();
  2349. $q->query(
  2350. array(
  2351. 'orderby' => 'foo',
  2352. )
  2353. );
  2354. $this->assertContains( "ORDER BY $wpdb->comments.comment_date_gmt", $q->request );
  2355. }
  2356. public function test_orderby_space_separated() {
  2357. global $wpdb;
  2358. $q = new WP_Comment_Query();
  2359. $q->query(
  2360. array(
  2361. 'orderby' => 'comment_agent comment_approved',
  2362. )
  2363. );
  2364. $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_approved DESC", $q->request );
  2365. }
  2366. public function test_orderby_comma_separated() {
  2367. global $wpdb;
  2368. $q = new WP_Comment_Query();
  2369. $q->query(
  2370. array(
  2371. 'orderby' => 'comment_agent, comment_approved',
  2372. )
  2373. );
  2374. $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_approved DESC", $q->request );
  2375. }
  2376. public function test_orderby_flat_array() {
  2377. global $wpdb;
  2378. $q = new WP_Comment_Query();
  2379. $q->query(
  2380. array(
  2381. 'orderby' => array( 'comment_agent', 'comment_approved' ),
  2382. )
  2383. );
  2384. $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_approved DESC", $q->request );
  2385. }
  2386. public function test_orderby_array_contains_invalid_item() {
  2387. global $wpdb;
  2388. $q = new WP_Comment_Query();
  2389. $q->query(
  2390. array(
  2391. 'orderby' => array( 'comment_agent', 'foo', 'comment_approved' ),
  2392. )
  2393. );
  2394. $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_approved DESC", $q->request );
  2395. }
  2396. public function test_orderby_array_contains_all_invalid_items() {
  2397. global $wpdb;
  2398. $q = new WP_Comment_Query();
  2399. $q->query(
  2400. array(
  2401. 'orderby' => array( 'foo', 'bar', 'baz' ),
  2402. )
  2403. );
  2404. $this->assertContains( "ORDER BY $wpdb->comments.comment_date_gmt", $q->request );
  2405. }
  2406. /**
  2407. * @ticket 29902
  2408. */
  2409. public function test_orderby_none() {
  2410. $q = new WP_Comment_Query();
  2411. $q->query(
  2412. array(
  2413. 'orderby' => 'none',
  2414. )
  2415. );
  2416. $this->assertNotContains( 'ORDER BY', $q->request );
  2417. }
  2418. /**
  2419. * @ticket 29902
  2420. */
  2421. public function test_orderby_empty_array() {
  2422. $q = new WP_Comment_Query();
  2423. $q->query(
  2424. array(
  2425. 'orderby' => array(),
  2426. )
  2427. );
  2428. $this->assertNotContains( 'ORDER BY', $q->request );
  2429. }
  2430. /**
  2431. * @ticket 29902
  2432. */
  2433. public function test_orderby_false() {
  2434. $q = new WP_Comment_Query();
  2435. $q->query(
  2436. array(
  2437. 'orderby' => false,
  2438. )
  2439. );
  2440. $this->assertNotContains( 'ORDER BY', $q->request );
  2441. }
  2442. /**
  2443. * @ticket 30478
  2444. */
  2445. public function test_orderby_array() {
  2446. global $wpdb;
  2447. $q = new WP_Comment_Query();
  2448. $found = $q->query(
  2449. array(
  2450. 'fields' => 'ids',
  2451. 'orderby' => array(
  2452. 'comment_agent' => 'DESC',
  2453. 'comment_date_gmt' => 'ASC',
  2454. 'comment_ID' => 'DESC',
  2455. ),
  2456. )
  2457. );
  2458. $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_date_gmt ASC, $wpdb->comments.comment_ID DESC", $q->request );
  2459. }
  2460. /**
  2461. * @ticket 30478
  2462. */
  2463. public function test_orderby_array_should_discard_invalid_columns() {
  2464. global $wpdb;
  2465. $q = new WP_Comment_Query();
  2466. $found = $q->query(
  2467. array(
  2468. 'fields' => 'ids',
  2469. 'orderby' => array(
  2470. 'comment_agent' => 'DESC',
  2471. 'foo' => 'ASC',
  2472. 'comment_ID' => 'DESC',
  2473. ),
  2474. )
  2475. );
  2476. $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_ID DESC", $q->request );
  2477. }
  2478. /**
  2479. * @ticket 30478
  2480. */
  2481. public function test_orderby_array_should_convert_invalid_order_to_DESC() {
  2482. global $wpdb;
  2483. $q = new WP_Comment_Query();
  2484. $found = $q->query(
  2485. array(
  2486. 'fields' => 'ids',
  2487. 'orderby' => array(
  2488. 'comment_agent' => 'DESC',
  2489. 'comment_date_gmt' => 'foo',
  2490. 'comment_ID' => 'DESC',
  2491. ),
  2492. )
  2493. );
  2494. $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_date_gmt DESC, $wpdb->comments.comment_ID DESC", $q->request );
  2495. }
  2496. /**
  2497. * @ticket 30478
  2498. */
  2499. public function test_orderby_array_should_sort_by_comment_ID_as_fallback_and_should_inherit_order_from_comment_date_gmt() {
  2500. global $wpdb;
  2501. $q = new WP_Comment_Query();
  2502. $found = $q->query(
  2503. array(
  2504. 'fields' => 'ids',
  2505. 'orderby' => array(
  2506. 'comment_agent' => 'DESC',
  2507. 'comment_date_gmt' => 'ASC',
  2508. ),
  2509. )
  2510. );
  2511. $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_date_gmt ASC, $wpdb->comments.comment_ID ASC", $q->request );
  2512. }
  2513. /**
  2514. * @ticket 30478
  2515. */
  2516. public function test_orderby_array_should_sort_by_comment_ID_as_fallback_and_should_inherit_order_from_comment_date() {
  2517. global $wpdb;
  2518. $q = new WP_Comment_Query();
  2519. $found = $q->query(
  2520. array(
  2521. 'fields' => 'ids',
  2522. 'orderby' => array(
  2523. 'comment_agent' => 'DESC',
  2524. 'comment_date' => 'ASC',
  2525. ),
  2526. )
  2527. );
  2528. $this->assertContains( "ORDER BY $wpdb->comments.comment_agent DESC, $wpdb->comments.comment_date ASC, $wpdb->comments.comment_ID ASC", $q->request );
  2529. }
  2530. /**
  2531. * @ticket 30478
  2532. */
  2533. public function test_orderby_array_should_sort_by_comment_ID_DESC_as_fallback_when_not_sorted_by_date() {
  2534. global $wpdb;
  2535. $q = new WP_Comment_Query();
  2536. $found = $q->query(
  2537. array(
  2538. 'fields' => 'ids',
  2539. 'orderby' => array(
  2540. 'comment_agent' => 'ASC',
  2541. ),
  2542. )
  2543. );
  2544. $this->assertContains( "ORDER BY $wpdb->comments.comment_agent ASC, $wpdb->comments.comment_ID DESC", $q->request );
  2545. }
  2546. /**
  2547. * @ticket 30478
  2548. */
  2549. public function test_orderby_date_modified_gmt_should_order_by_comment_ID_in_case_of_tie_ASC() {
  2550. $now = current_time( 'mysql', 1 );
  2551. $comments = self::factory()->comment->create_many(
  2552. 5,
  2553. array(
  2554. 'comment_post_ID' => self::$post_id,
  2555. 'comment_date_gmt' => $now,
  2556. )
  2557. );
  2558. $q = new WP_Comment_Query();
  2559. $found = $q->query(
  2560. array(
  2561. 'orderby' => 'comment_date_gmt',
  2562. 'order' => 'ASC',
  2563. )
  2564. );
  2565. // $comments is ASC by default.
  2566. $this->assertEquals( $comments, wp_list_pluck( $found, 'comment_ID' ) );
  2567. }
  2568. /**
  2569. * @ticket 30478
  2570. */
  2571. public function test_orderby_date_modified_gmt_should_order_by_comment_ID_in_case_of_tie_DESC() {
  2572. $now = current_time( 'mysql', 1 );
  2573. $comments = self::factory()->comment->create_many(
  2574. 5,
  2575. array(
  2576. 'comment_post_ID' => self::$post_id,
  2577. 'comment_date_gmt' => $now,
  2578. )
  2579. );
  2580. $q = new WP_Comment_Query();
  2581. $found = $q->query(
  2582. array(
  2583. 'orderby' => 'comment_date_gmt',
  2584. 'order' => 'DESC',
  2585. )
  2586. );
  2587. // $comments is ASC by default.
  2588. rsort( $comments );
  2589. $this->assertEquals( $comments, wp_list_pluck( $found, 'comment_ID' ) );
  2590. }
  2591. public function test_meta_vars_should_be_converted_to_meta_query() {
  2592. $q = new WP_Comment_Query();
  2593. $q->query(
  2594. array(
  2595. 'meta_key' => 'foo',
  2596. 'meta_value' => '5',
  2597. 'meta_compare' => '>',
  2598. 'meta_type' => 'SIGNED',
  2599. )
  2600. );
  2601. $this->assertSame( 'foo', $q->meta_query->queries[0]['key'] );
  2602. $this->assertSame( '5', $q->meta_query->queries[0]['value'] );
  2603. $this->assertSame( '>', $q->meta_query->queries[0]['compare'] );
  2604. $this->assertSame( 'SIGNED', $q->meta_query->queries[0]['type'] );
  2605. }
  2606. public function test_count() {
  2607. $c1 = self::factory()->comment->create(
  2608. array(
  2609. 'comment_post_ID' => self::$post_id,
  2610. 'user_id' => 7,
  2611. )
  2612. );
  2613. $c2 = self::factory()->comment->create(
  2614. array(
  2615. 'comment_post_ID' => self::$post_id,
  2616. 'user_id' => 7,
  2617. )
  2618. );
  2619. $q = new WP_Comment_Query();
  2620. $found = $q->query(
  2621. array(
  2622. 'count' => true,
  2623. )
  2624. );
  2625. $this->assertSame( 2, $found );
  2626. }
  2627. /**
  2628. * @ticket 23369
  2629. */
  2630. public function test_count_with_meta_query() {
  2631. $c1 = self::factory()->comment->create(
  2632. array(
  2633. 'comment_post_ID' => self::$post_id,
  2634. 'user_id' => 7,
  2635. )
  2636. );
  2637. $c2 = self::factory()->comment->create(
  2638. array(
  2639. 'comment_post_ID' => self::$post_id,
  2640. 'user_id' => 7,
  2641. )
  2642. );
  2643. $c3 = self::factory()->comment->create(
  2644. array(
  2645. 'comment_post_ID' => self::$post_id,
  2646. 'user_id' => 7,
  2647. )
  2648. );
  2649. add_comment_meta( $c1, 'foo', 'bar' );
  2650. add_comment_meta( $c3, 'foo', 'bar' );
  2651. $q = new WP_Comment_Query();
  2652. $found = $q->query(
  2653. array(
  2654. 'count' => true,
  2655. 'meta_query' => array(
  2656. array(
  2657. 'key' => 'foo',
  2658. 'value' => 'bar',
  2659. ),
  2660. ),
  2661. )
  2662. );
  2663. $this->assertSame( 2, $found );
  2664. }
  2665. /**
  2666. * @ticket 38268
  2667. */
  2668. public function test_paged() {
  2669. $now = time();
  2670. $c1 = self::factory()->comment->create(
  2671. array(
  2672. 'comment_post_ID' => self::$post_id,
  2673. 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $now - 50 ),
  2674. )
  2675. );
  2676. $c2 = self::factory()->comment->create(
  2677. array(
  2678. 'comment_post_ID' => self::$post_id,
  2679. 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $now - 40 ),
  2680. )
  2681. );
  2682. $c3 = self::factory()->comment->create(
  2683. array(
  2684. 'comment_post_ID' => self::$post_id,
  2685. 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $now - 30 ),
  2686. )
  2687. );
  2688. $c4 = self::factory()->comment->create(
  2689. array(
  2690. 'comment_post_ID' => self::$post_id,
  2691. 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $now - 20 ),
  2692. )
  2693. );
  2694. $query = new WP_Comment_Query();
  2695. $found = $query->query(
  2696. array(
  2697. 'paged' => 2,
  2698. 'number' => 2,
  2699. 'orderby' => 'comment_date_gmt',
  2700. 'order' => 'DESC',
  2701. 'fields' => 'ids',
  2702. )
  2703. );
  2704. $expected = array( $c2, $c1 );
  2705. $this->assertSame( $expected, $found );
  2706. }
  2707. /**
  2708. * @ticket 38268
  2709. */
  2710. public function test_offset_should_take_precedence_over_paged() {
  2711. $now = time();
  2712. $c1 = self::factory()->comment->create(
  2713. array(
  2714. 'comment_post_ID' => self::$post_id,
  2715. 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $now - 50 ),
  2716. )
  2717. );
  2718. $c2 = self::factory()->comment->create(
  2719. array(
  2720. 'comment_post_ID' => self::$post_id,
  2721. 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $now - 40 ),
  2722. )
  2723. );
  2724. $c3 = self::factory()->comment->create(
  2725. array(
  2726. 'comment_post_ID' => self::$post_id,
  2727. 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $now - 30 ),
  2728. )
  2729. );
  2730. $c4 = self::factory()->comment->create(
  2731. array(
  2732. 'comment_post_ID' => self::$post_id,
  2733. 'comment_date_gmt' => gmdate( 'Y-m-d H:i:s', $now - 20 ),
  2734. )
  2735. );
  2736. $query = new WP_Comment_Query();
  2737. $found = $query->query(
  2738. array(
  2739. 'paged' => 2,
  2740. 'offset' => 1,
  2741. 'number' => 2,
  2742. 'orderby' => 'comment_date_gmt',
  2743. 'order' => 'DESC',
  2744. 'fields' => 'ids',
  2745. )
  2746. );
  2747. $expected = array( $c3, $c2 );
  2748. $this->assertSame( $expected, $found );
  2749. }
  2750. public function test_post_type_single_value() {
  2751. register_post_type( 'post-type-1' );
  2752. register_post_type( 'post-type-2' );
  2753. $p1 = self::factory()->post->create( array( 'post_type' => 'post-type-1' ) );
  2754. $p2 = self::factory()->post->create( array( 'post_type' => 'post-type-2' ) );
  2755. $c1 = self::factory()->comment->create_post_comments( $p1, 1 );
  2756. $c2 = self::factory()->comment->create_post_comments( $p2, 1 );
  2757. $q = new WP_Comment_Query();
  2758. $found = $q->query(
  2759. array(
  2760. 'fields' => 'ids',
  2761. 'post_type' => 'post-type-2',
  2762. )
  2763. );
  2764. $this->assertSameSets( $c2, $found );
  2765. _unregister_post_type( 'post-type-1' );
  2766. _unregister_post_type( 'post-type-2' );
  2767. }
  2768. /**
  2769. * @ticket 20006
  2770. */
  2771. public function test_post_type_singleton_array() {
  2772. register_post_type( 'post-type-1' );
  2773. register_post_type( 'post-type-2' );
  2774. $p1 = self::factory()->post->create( array( 'post_type' => 'post-type-1' ) );
  2775. $p2 = self::factory()->post->create( array( 'post_type' => 'post-type-2' ) );
  2776. $c1 = self::factory()->comment->create_post_comments( $p1, 1 );
  2777. $c2 = self::factory()->comment->create_post_comments( $p2, 1 );
  2778. $q = new WP_Comment_Query();
  2779. $found = $q->query(
  2780. array(
  2781. 'fields' => 'ids',
  2782. 'post_type' => array( 'post-type-2' ),
  2783. )
  2784. );
  2785. $this->assertSameSets( $c2, $found );
  2786. _unregister_post_type( 'post-type-1' );
  2787. _unregister_post_type( 'post-type-2' );
  2788. }
  2789. /**
  2790. * @ticket 20006
  2791. */
  2792. public function test_post_type_array() {
  2793. register_post_type( 'post-type-1' );
  2794. register_post_type( 'post-type-2' );
  2795. register_post_type( 'post-type-3' );
  2796. $p1 = self::factory()->post->create( array( 'post_type' => 'post-type-1' ) );
  2797. $p2 = self::factory()->post->create( array( 'post_type' => 'post-type-2' ) );
  2798. $p3 = self::factory()->post->create( array( 'post_type' => 'post-type-3' ) );
  2799. $c1 = self::factory()->comment->create_post_comments( $p1, 1 );
  2800. $c2 = self::factory()->comment->create_post_comments( $p2, 1 );
  2801. $c3 = self::factory()->comment->create_post_comments( $p3, 1 );
  2802. $q = new WP_Comment_Query();
  2803. $found = $q->query(
  2804. array(
  2805. 'fields' => 'ids',
  2806. 'post_type' => array( 'post-type-1', 'post-type-3' ),
  2807. )
  2808. );
  2809. $this->assertSameSets( array_merge( $c1, $c3 ), $found );
  2810. }
  2811. public function test_post_name_single_value() {
  2812. $p1 = self::factory()->post->create( array( 'post_name' => 'foo' ) );
  2813. $p2 = self::factory()->post->create( array( 'post_name' => 'bar' ) );
  2814. $c1 = self::factory()->comment->create_post_comments( $p1, 1 );
  2815. $c2 = self::factory()->comment->create_post_comments( $p2, 1 );
  2816. $q = new WP_Comment_Query();
  2817. $found = $q->query(
  2818. array(
  2819. 'fields' => 'ids',
  2820. 'post_name' => 'bar',
  2821. )
  2822. );
  2823. $this->assertSameSets( $c2, $found );
  2824. }
  2825. /**
  2826. * @ticket 20006
  2827. */
  2828. public function test_post_name_singleton_array() {
  2829. $p1 = self::factory()->post->create( array( 'post_name' => 'foo' ) );
  2830. $p2 = self::factory()->post->create( array( 'post_name' => 'bar' ) );
  2831. $c1 = self::factory()->comment->create_post_comments( $p1, 1 );
  2832. $c2 = self::factory()->comment->create_post_comments( $p2, 1 );
  2833. $q = new WP_Comment_Query();
  2834. $found = $q->query(
  2835. array(
  2836. 'fields' => 'ids',
  2837. 'post_name' => array( 'bar' ),
  2838. )
  2839. );
  2840. $this->assertSameSets( $c2, $found );
  2841. }
  2842. /**
  2843. * @ticket 20006
  2844. */
  2845. public function test_post_name_array() {
  2846. $p1 = self::factory()->post->create( array( 'post_name' => 'foo' ) );
  2847. $p2 = self::factory()->post->create( array( 'post_name' => 'bar' ) );
  2848. $p3 = self::factory()->post->create( array( 'post_name' => 'baz' ) );
  2849. $c1 = self::factory()->comment->create_post_comments( $p1, 1 );
  2850. $c2 = self::factory()->comment->create_post_comments( $p2, 1 );
  2851. $c3 = self::factory()->comment->create_post_comments( $p3, 1 );
  2852. $q = new WP_Comment_Query();
  2853. $found = $q->query(
  2854. array(
  2855. 'fields' => 'ids',
  2856. 'post_name' => array( 'foo', 'baz' ),
  2857. )
  2858. );
  2859. $this->assertSameSets( array_merge( $c1, $c3 ), $found );
  2860. }
  2861. public function test_post_status_single_value() {
  2862. $p1 = self::factory()->post->create( array( 'post_status' => 'publish' ) );
  2863. $p2 = self::factory()->post->create( array( 'post_status' => 'draft' ) );
  2864. $c1 = self::factory()->comment->create_post_comments( $p1, 1 );
  2865. $c2 = self::factory()->comment->create_post_comments( $p2, 1 );
  2866. $q = new WP_Comment_Query();
  2867. $found = $q->query(
  2868. array(
  2869. 'fields' => 'ids',
  2870. 'post_status' => 'draft',
  2871. )
  2872. );
  2873. $this->assertSameSets( $c2, $found );
  2874. }
  2875. /**
  2876. * @ticket 20006
  2877. */
  2878. public function test_post_status_singleton_array() {
  2879. $p1 = self::factory()->post->create( array( 'post_status' => 'publish' ) );
  2880. $p2 = self::factory()->post->create( array( 'post_status' => 'draft' ) );
  2881. $c1 = self::factory()->comment->create_post_comments( $p1, 1 );
  2882. $c2 = self::factory()->comment->create_post_comments( $p2, 1 );
  2883. $q = new WP_Comment_Query();
  2884. $found = $q->query(
  2885. array(
  2886. 'fields' => 'ids',
  2887. 'post_status' => array( 'draft' ),
  2888. )
  2889. );
  2890. $this->assertSameSets( $c2, $found );
  2891. }
  2892. /**
  2893. * @ticket 20006
  2894. */
  2895. public function test_post_status_array() {
  2896. $p1 = self::factory()->post->create( array( 'post_status' => 'publish' ) );
  2897. $p2 = self::factory()->post->create( array( 'post_status' => 'draft' ) );
  2898. $p3 = self::factory()->post->create( array( 'post_status' => 'future' ) );
  2899. $c1 = self::factory()->comment->create_post_comments( $p1, 1 );
  2900. $c2 = self::factory()->comment->create_post_comments( $p2, 1 );
  2901. $c3 = self::factory()->comment->create_post_comments( $p3, 1 );
  2902. $q = new WP_Comment_Query();
  2903. $found = $q->query(
  2904. array(
  2905. 'fields' => 'ids',
  2906. 'post_status' => array( 'publish', 'future' ),
  2907. )
  2908. );
  2909. $this->assertSameSets( array_merge( $c1, $c3 ), $found );
  2910. }
  2911. /**
  2912. * @ticket 35512
  2913. */
  2914. public function test_post_type_any_should_override_other_post_types() {
  2915. register_post_type( 'post-type-1', array( 'exclude_from_search' => false ) );
  2916. register_post_type( 'post-type-2', array( 'exclude_from_search' => false ) );
  2917. $p1 = self::factory()->post->create( array( 'post_type' => 'post-type-1' ) );
  2918. $p2 = self::factory()->post->create( array( 'post_type' => 'post-type-2' ) );
  2919. $c1 = self::factory()->comment->create_post_comments( $p1, 1 );
  2920. $c2 = self::factory()->comment->create_post_comments( $p2, 1 );
  2921. $q = new WP_Comment_Query();
  2922. $found = $q->query(
  2923. array(
  2924. 'fields' => 'ids',
  2925. 'post_type' => array( 'any', 'post-type-1' ),
  2926. )
  2927. );
  2928. $this->assertSameSets( array_merge( $c1, $c2 ), $found );
  2929. }
  2930. /**
  2931. * @ticket 35512
  2932. */
  2933. public function test_post_type_any_as_part_of_an_array_of_post_types() {
  2934. register_post_type( 'post-type-1', array( 'exclude_from_search' => false ) );
  2935. register_post_type( 'post-type-2', array( 'exclude_from_search' => false ) );
  2936. $p1 = self::factory()->post->create( array( 'post_type' => 'post-type-1' ) );
  2937. $p2 = self::factory()->post->create( array( 'post_type' => 'post-type-2' ) );
  2938. $c1 = self::factory()->comment->create_post_comments( $p1, 1 );
  2939. $c2 = self::factory()->comment->create_post_comments( $p2, 1 );
  2940. $q = new WP_Comment_Query();
  2941. $found = $q->query(
  2942. array(
  2943. 'fields' => 'ids',
  2944. 'post_type' => array( 'any' ),
  2945. )
  2946. );
  2947. $this->assertSameSets( array_merge( $c1, $c2 ), $found );
  2948. }
  2949. /**
  2950. * @ticket 35512
  2951. */
  2952. public function test_post_status_any_should_override_other_post_statuses() {
  2953. $p1 = self::factory()->post->create( array( 'post_status' => 'publish' ) );
  2954. $p2 = self::factory()->post->create( array( 'post_status' => 'draft' ) );
  2955. $c1 = self::factory()->comment->create_post_comments( $p1, 1 );
  2956. $c2 = self::factory()->comment->create_post_comments( $p2, 1 );
  2957. $q = new WP_Comment_Query();
  2958. $found = $q->query(
  2959. array(
  2960. 'fields' => 'ids',
  2961. 'post_status' => array( 'any', 'draft' ),
  2962. )
  2963. );
  2964. $this->assertSameSets( array_merge( $c1, $c2 ), $found );
  2965. }
  2966. /**
  2967. * @ticket 35512
  2968. */
  2969. public function test_post_status_any_as_part_of_an_array_of_post_statuses() {
  2970. $p1 = self::factory()->post->create( array( 'post_status' => 'publish' ) );
  2971. $p2 = self::factory()->post->create( array( 'post_status' => 'draft' ) );
  2972. $c1 = self::factory()->comment->create_post_comments( $p1, 1 );
  2973. $c2 = self::factory()->comment->create_post_comments( $p2, 1 );
  2974. $q = new WP_Comment_Query();
  2975. $found = $q->query(
  2976. array(
  2977. 'fields' => 'ids',
  2978. 'post_status' => array( 'any' ),
  2979. )
  2980. );
  2981. $this->assertSameSets( array_merge( $c1, $c2 ), $found );
  2982. }
  2983. /**
  2984. * @ticket 24826
  2985. */
  2986. public function test_comment_query_object() {
  2987. $comment_id = self::factory()->comment->create();
  2988. $query1 = new WP_Comment_Query();
  2989. $this->assertNull( $query1->query_vars );
  2990. $this->assertEmpty( $query1->comments );
  2991. $comments = $query1->query( array( 'status' => 'all' ) );
  2992. $this->assertInternalType( 'array', $query1->query_vars );
  2993. $this->assertNotEmpty( $query1->comments );
  2994. $this->assertInternalType( 'array', $query1->comments );
  2995. $query2 = new WP_Comment_Query( array( 'status' => 'all' ) );
  2996. $this->assertNotEmpty( $query2->query_vars );
  2997. $this->assertNotEmpty( $query2->comments );
  2998. $this->assertEquals( $query2->comments, $query1->get_comments() );
  2999. }
  3000. /**
  3001. * @ticket 22400
  3002. */
  3003. public function test_comment_cache_key_should_ignore_custom_params() {
  3004. global $wpdb;
  3005. $p = self::factory()->post->create();
  3006. $c = self::factory()->comment->create( array( 'comment_post_ID' => $p ) );
  3007. $q1 = new WP_Comment_Query();
  3008. $q1->query(
  3009. array(
  3010. 'post_id' => $p,
  3011. 'fields' => 'ids',
  3012. )
  3013. );
  3014. $num_queries = $wpdb->num_queries;
  3015. $q2 = new WP_Comment_Query();
  3016. $q2->query(
  3017. array(
  3018. 'post_id' => $p,
  3019. 'fields' => 'ids',
  3020. 'foo' => 'bar',
  3021. )
  3022. );
  3023. $this->assertSame( $num_queries, $wpdb->num_queries );
  3024. }
  3025. /**
  3026. * @ticket 35677
  3027. */
  3028. public function test_cache_should_be_sensitive_to_parent__in() {
  3029. global $wpdb;
  3030. $q1 = new WP_Comment_Query(
  3031. array(
  3032. 'parent__in' => array( 1, 2, 3 ),
  3033. )
  3034. );
  3035. $num_queries = $wpdb->num_queries;
  3036. $q2 = new WP_Comment_Query(
  3037. array(
  3038. 'parent__in' => array( 4, 5, 6 ),
  3039. )
  3040. );
  3041. $this->assertNotEquals( $num_queries, $wpdb->num_queries );
  3042. }
  3043. /**
  3044. * @ticket 35677
  3045. */
  3046. public function test_cache_should_be_sensitive_to_parent__not_in() {
  3047. global $wpdb;
  3048. $q1 = new WP_Comment_Query(
  3049. array(
  3050. 'parent__not_in' => array( 1, 2, 3 ),
  3051. )
  3052. );
  3053. $num_queries = $wpdb->num_queries;
  3054. $q2 = new WP_Comment_Query(
  3055. array(
  3056. 'parent__not_in' => array( 4, 5, 6 ),
  3057. )
  3058. );
  3059. $this->assertNotEquals( $num_queries, $wpdb->num_queries );
  3060. }
  3061. /**
  3062. * @ticket 32762
  3063. */
  3064. public function test_it_should_be_possible_to_modify_meta_query_using_pre_get_comments_action() {
  3065. $comments = self::factory()->comment->create_many(
  3066. 2,
  3067. array(
  3068. 'comment_post_ID' => self::$post_id,
  3069. )
  3070. );
  3071. add_comment_meta( $comments[1], 'foo', 'bar' );
  3072. add_action( 'pre_get_comments', array( $this, 'modify_meta_query' ) );
  3073. $q = new WP_Comment_Query(
  3074. array(
  3075. 'comment_post_ID' => self::$post_id,
  3076. 'fields' => 'ids',
  3077. )
  3078. );
  3079. remove_action( 'pre_get_comments', array( $this, 'modify_meta_query' ) );
  3080. $this->assertSameSets( array( $comments[1] ), $q->comments );
  3081. }
  3082. public function modify_meta_query( $q ) {
  3083. $q->meta_query = new WP_Meta_Query(
  3084. array(
  3085. array(
  3086. 'key' => 'foo',
  3087. 'value' => 'bar',
  3088. ),
  3089. )
  3090. );
  3091. }
  3092. /**
  3093. * @ticket 32762
  3094. */
  3095. public function test_it_should_be_possible_to_modify_meta_params_using_pre_get_comments_action() {
  3096. $comments = self::factory()->comment->create_many(
  3097. 2,
  3098. array(
  3099. 'comment_post_ID' => self::$post_id,
  3100. )
  3101. );
  3102. add_comment_meta( $comments[1], 'foo', 'bar' );
  3103. add_action( 'pre_get_comments', array( $this, 'modify_meta_params' ) );
  3104. $q = new WP_Comment_Query(
  3105. array(
  3106. 'comment_post_ID' => self::$post_id,
  3107. 'fields' => 'ids',
  3108. )
  3109. );
  3110. remove_action( 'pre_get_comments', array( $this, 'modify_meta_params' ) );
  3111. $this->assertSameSets( array( $comments[1] ), $q->comments );
  3112. }
  3113. public function modify_meta_params( $q ) {
  3114. $q->query_vars['meta_key'] = 'foo';
  3115. $q->query_vars['meta_value'] = 'bar';
  3116. }
  3117. /**
  3118. * @ticket 33882
  3119. */
  3120. public function test_parent__in() {
  3121. $c1 = self::factory()->comment->create(
  3122. array(
  3123. 'comment_post_ID' => self::$post_id,
  3124. 'comment_approved' => '1',
  3125. )
  3126. );
  3127. $c2 = self::factory()->comment->create(
  3128. array(
  3129. 'comment_post_ID' => self::$post_id,
  3130. 'comment_approved' => '1',
  3131. 'comment_parent' => $c1,
  3132. )
  3133. );
  3134. $ids = new WP_Comment_Query(
  3135. array(
  3136. 'comment_post_ID' => self::$post_id,
  3137. 'fields' => 'ids',
  3138. 'parent__in' => array( $c1 ),
  3139. )
  3140. );
  3141. $this->assertSameSets( array( $c2 ), $ids->comments );
  3142. }
  3143. /**
  3144. * @ticket 33882
  3145. */
  3146. public function test_parent__in_commas() {
  3147. $c1 = self::factory()->comment->create(
  3148. array(
  3149. 'comment_post_ID' => self::$post_id,
  3150. 'comment_approved' => '1',
  3151. )
  3152. );
  3153. $c2 = self::factory()->comment->create(
  3154. array(
  3155. 'comment_post_ID' => self::$post_id,
  3156. 'comment_approved' => '1',
  3157. )
  3158. );
  3159. $c3 = self::factory()->comment->create(
  3160. array(
  3161. 'comment_post_ID' => self::$post_id,
  3162. 'comment_approved' => '1',
  3163. 'comment_parent' => $c1,
  3164. )
  3165. );
  3166. $c4 = self::factory()->comment->create(
  3167. array(
  3168. 'comment_post_ID' => self::$post_id,
  3169. 'comment_approved' => '1',
  3170. 'comment_parent' => $c2,
  3171. )
  3172. );
  3173. $ids = new WP_Comment_Query(
  3174. array(
  3175. 'comment_post_ID' => self::$post_id,
  3176. 'fields' => 'ids',
  3177. 'parent__in' => "$c1,$c2",
  3178. )
  3179. );
  3180. $this->assertSameSets( array( $c3, $c4 ), $ids->comments );
  3181. }
  3182. /**
  3183. * @ticket 33882
  3184. */
  3185. public function test_parent__not_in() {
  3186. $c1 = self::factory()->comment->create(
  3187. array(
  3188. 'comment_post_ID' => self::$post_id,
  3189. 'comment_approved' => '1',
  3190. )
  3191. );
  3192. self::factory()->comment->create(
  3193. array(
  3194. 'comment_post_ID' => self::$post_id,
  3195. 'comment_approved' => '1',
  3196. 'comment_parent' => $c1,
  3197. )
  3198. );
  3199. $ids = new WP_Comment_Query(
  3200. array(
  3201. 'comment_post_ID' => self::$post_id,
  3202. 'fields' => 'ids',
  3203. 'parent__not_in' => array( $c1 ),
  3204. )
  3205. );
  3206. $this->assertSameSets( array( $c1 ), $ids->comments );
  3207. }
  3208. /**
  3209. * @ticket 33882
  3210. */
  3211. public function test_parent__not_in_commas() {
  3212. $c1 = self::factory()->comment->create(
  3213. array(
  3214. 'comment_post_ID' => self::$post_id,
  3215. 'comment_approved' => '1',
  3216. )
  3217. );
  3218. $c2 = self::factory()->comment->create(
  3219. array(
  3220. 'comment_post_ID' => self::$post_id,
  3221. 'comment_approved' => '1',
  3222. )
  3223. );
  3224. self::factory()->comment->create(
  3225. array(
  3226. 'comment_post_ID' => self::$post_id,
  3227. 'comment_approved' => '1',
  3228. 'comment_parent' => $c1,
  3229. )
  3230. );
  3231. self::factory()->comment->create(
  3232. array(
  3233. 'comment_post_ID' => self::$post_id,
  3234. 'comment_approved' => '1',
  3235. 'comment_parent' => $c2,
  3236. )
  3237. );
  3238. $ids = new WP_Comment_Query(
  3239. array(
  3240. 'comment_post_ID' => self::$post_id,
  3241. 'fields' => 'ids',
  3242. 'parent__not_in' => "$c1,$c2",
  3243. )
  3244. );
  3245. $this->assertSameSets( array( $c1, $c2 ), $ids->comments );
  3246. }
  3247. /**
  3248. * @ticket 33883
  3249. */
  3250. public function test_orderby_comment__in() {
  3251. self::factory()->comment->create(
  3252. array(
  3253. 'comment_post_ID' => self::$post_id,
  3254. 'comment_approved' => '1',
  3255. )
  3256. );
  3257. $c2 = self::factory()->comment->create(
  3258. array(
  3259. 'comment_post_ID' => self::$post_id,
  3260. 'comment_approved' => '1',
  3261. )
  3262. );
  3263. $c3 = self::factory()->comment->create(
  3264. array(
  3265. 'comment_post_ID' => self::$post_id,
  3266. 'comment_approved' => '1',
  3267. )
  3268. );
  3269. self::factory()->comment->create(
  3270. array(
  3271. 'comment_post_ID' => self::$post_id,
  3272. 'comment_approved' => '1',
  3273. )
  3274. );
  3275. $ids = new WP_Comment_Query(
  3276. array(
  3277. 'fields' => 'ids',
  3278. 'comment__in' => array( $c2, $c3 ),
  3279. 'orderby' => 'comment__in',
  3280. )
  3281. );
  3282. $this->assertSame( array( $c2, $c3 ), $ids->comments );
  3283. }
  3284. /**
  3285. * @ticket 8071
  3286. */
  3287. public function test_no_found_rows_should_default_to_true() {
  3288. $comments = self::factory()->comment->create_many( 3, array( 'comment_post_ID' => self::$post_id ) );
  3289. $q = new WP_Comment_Query(
  3290. array(
  3291. 'post_id' => self::$post_id,
  3292. 'number' => 2,
  3293. )
  3294. );
  3295. $this->assertSame( 0, $q->found_comments );
  3296. $this->assertSame( 0, $q->max_num_pages );
  3297. }
  3298. /**
  3299. * @ticket 8071
  3300. */
  3301. public function test_should_respect_no_found_rows_true() {
  3302. $comments = self::factory()->comment->create_many( 3, array( 'comment_post_ID' => self::$post_id ) );
  3303. $q = new WP_Comment_Query(
  3304. array(
  3305. 'post_id' => self::$post_id,
  3306. 'number' => 2,
  3307. 'no_found_rows' => true,
  3308. )
  3309. );
  3310. $this->assertSame( 0, $q->found_comments );
  3311. $this->assertSame( 0, $q->max_num_pages );
  3312. }
  3313. /**
  3314. * @ticket 8071
  3315. */
  3316. public function test_should_respect_no_found_rows_false() {
  3317. $comments = self::factory()->comment->create_many( 3, array( 'comment_post_ID' => self::$post_id ) );
  3318. $q = new WP_Comment_Query(
  3319. array(
  3320. 'post_id' => self::$post_id,
  3321. 'number' => 2,
  3322. 'no_found_rows' => false,
  3323. )
  3324. );
  3325. $this->assertSame( 3, $q->found_comments );
  3326. $this->assertEquals( 2, $q->max_num_pages );
  3327. }
  3328. /**
  3329. * @ticket 37184
  3330. */
  3331. public function test_found_rows_should_be_fetched_from_the_cache() {
  3332. $comments = self::factory()->comment->create_many( 3, array( 'comment_post_ID' => self::$post_id ) );
  3333. // Prime cache.
  3334. new WP_Comment_Query(
  3335. array(
  3336. 'post_id' => self::$post_id,
  3337. 'number' => 2,
  3338. 'no_found_rows' => false,
  3339. )
  3340. );
  3341. $q = new WP_Comment_Query(
  3342. array(
  3343. 'post_id' => self::$post_id,
  3344. 'number' => 2,
  3345. 'no_found_rows' => false,
  3346. )
  3347. );
  3348. $this->assertEquals( 3, $q->found_comments );
  3349. $this->assertEquals( 2, $q->max_num_pages );
  3350. }
  3351. /**
  3352. * @ticket 8071
  3353. */
  3354. public function test_hierarchical_should_skip_child_comments_in_offset() {
  3355. $top_level_0 = self::factory()->comment->create(
  3356. array(
  3357. 'comment_post_ID' => self::$post_id,
  3358. 'comment_approved' => '1',
  3359. )
  3360. );
  3361. $child_of_0 = self::factory()->comment->create(
  3362. array(
  3363. 'comment_post_ID' => self::$post_id,
  3364. 'comment_approved' => '1',
  3365. 'comment_parent' => $top_level_0,
  3366. )
  3367. );
  3368. $top_level_comments = self::factory()->comment->create_many(
  3369. 3,
  3370. array(
  3371. 'comment_post_ID' => self::$post_id,
  3372. 'comment_approved' => '1',
  3373. )
  3374. );
  3375. $q = new WP_Comment_Query(
  3376. array(
  3377. 'post_id' => self::$post_id,
  3378. 'hierarchical' => 'flat',
  3379. 'number' => 2,
  3380. 'offset' => 1,
  3381. 'orderby' => 'comment_ID',
  3382. 'order' => 'ASC',
  3383. 'fields' => 'ids',
  3384. )
  3385. );
  3386. $this->assertSame( array( $top_level_comments[0], $top_level_comments[1] ), $q->comments );
  3387. }
  3388. /**
  3389. * @ticket 8071
  3390. */
  3391. public function test_hierarchical_should_not_include_child_comments_in_number() {
  3392. $top_level_0 = self::factory()->comment->create(
  3393. array(
  3394. 'comment_post_ID' => self::$post_id,
  3395. 'comment_approved' => '1',
  3396. )
  3397. );
  3398. $child_of_0 = self::factory()->comment->create(
  3399. array(
  3400. 'comment_post_ID' => self::$post_id,
  3401. 'comment_approved' => '1',
  3402. 'comment_parent' => $top_level_0,
  3403. )
  3404. );
  3405. $top_level_comments = self::factory()->comment->create_many(
  3406. 3,
  3407. array(
  3408. 'comment_post_ID' => self::$post_id,
  3409. 'comment_approved' => '1',
  3410. )
  3411. );
  3412. $q = new WP_Comment_Query(
  3413. array(
  3414. 'post_id' => self::$post_id,
  3415. 'hierarchical' => 'flat',
  3416. 'number' => 2,
  3417. 'orderby' => 'comment_ID',
  3418. 'order' => 'ASC',
  3419. )
  3420. );
  3421. $this->assertEqualSets( array( $top_level_0, $child_of_0, $top_level_comments[0] ), wp_list_pluck( $q->comments, 'comment_ID' ) );
  3422. }
  3423. /**
  3424. * @ticket 8071
  3425. */
  3426. public function test_hierarchical_threaded() {
  3427. $c1 = self::factory()->comment->create(
  3428. array(
  3429. 'comment_post_ID' => self::$post_id,
  3430. 'comment_approved' => '1',
  3431. )
  3432. );
  3433. $c2 = self::factory()->comment->create(
  3434. array(
  3435. 'comment_post_ID' => self::$post_id,
  3436. 'comment_approved' => '1',
  3437. 'comment_parent' => $c1,
  3438. )
  3439. );
  3440. $c3 = self::factory()->comment->create(
  3441. array(
  3442. 'comment_post_ID' => self::$post_id,
  3443. 'comment_approved' => '1',
  3444. 'comment_parent' => $c2,
  3445. )
  3446. );
  3447. $c4 = self::factory()->comment->create(
  3448. array(
  3449. 'comment_post_ID' => self::$post_id,
  3450. 'comment_approved' => '1',
  3451. 'comment_parent' => $c1,
  3452. )
  3453. );
  3454. $c5 = self::factory()->comment->create(
  3455. array(
  3456. 'comment_post_ID' => self::$post_id,
  3457. 'comment_approved' => '1',
  3458. )
  3459. );
  3460. $c6 = self::factory()->comment->create(
  3461. array(
  3462. 'comment_post_ID' => self::$post_id,
  3463. 'comment_approved' => '1',
  3464. 'comment_parent' => $c5,
  3465. )
  3466. );
  3467. $args = array(
  3468. 'hierarchical' => 'threaded',
  3469. 'orderby' => 'comment_ID',
  3470. 'order' => 'ASC',
  3471. );
  3472. $query_args = array_merge(
  3473. $args,
  3474. array(
  3475. 'post_id' => self::$post_id,
  3476. )
  3477. );
  3478. $q = new WP_Comment_Query( $query_args );
  3479. // Top-level comments.
  3480. $this->assertEqualSets( array( $c1, $c5 ), array_values( wp_list_pluck( $q->comments, 'comment_ID' ) ) );
  3481. // Direct descendants of $c1.
  3482. $this->assertEqualSets( array( $c2, $c4 ), array_values( wp_list_pluck( $q->comments[ $c1 ]->get_children( $args ), 'comment_ID' ) ) );
  3483. // Direct descendants of $c2.
  3484. $this->assertEqualSets( array( $c3 ), array_values( wp_list_pluck( $q->comments[ $c1 ]->get_child( $c2 )->get_children( $args ), 'comment_ID' ) ) );
  3485. // Direct descendants of $c5.
  3486. $this->assertEqualSets( array( $c6 ), array_values( wp_list_pluck( $q->comments[ $c5 ]->get_children( $args ), 'comment_ID' ) ) );
  3487. }
  3488. /**
  3489. * @ticket 8071
  3490. */
  3491. public function test_hierarchical_threaded_approved() {
  3492. $c1 = self::factory()->comment->create(
  3493. array(
  3494. 'comment_post_ID' => self::$post_id,
  3495. 'comment_approved' => '1',
  3496. )
  3497. );
  3498. $c2 = self::factory()->comment->create(
  3499. array(
  3500. 'comment_post_ID' => self::$post_id,
  3501. 'comment_approved' => '1',
  3502. 'comment_parent' => $c1,
  3503. )
  3504. );
  3505. $c3 = self::factory()->comment->create(
  3506. array(
  3507. 'comment_post_ID' => self::$post_id,
  3508. 'comment_approved' => '0',
  3509. 'comment_parent' => $c2,
  3510. )
  3511. );
  3512. $c4 = self::factory()->comment->create(
  3513. array(
  3514. 'comment_post_ID' => self::$post_id,
  3515. 'comment_approved' => '1',
  3516. 'comment_parent' => $c1,
  3517. )
  3518. );
  3519. $c5 = self::factory()->comment->create(
  3520. array(
  3521. 'comment_post_ID' => self::$post_id,
  3522. 'comment_approved' => '1',
  3523. )
  3524. );
  3525. self::factory()->comment->create(
  3526. array(
  3527. 'comment_post_ID' => self::$post_id,
  3528. 'comment_approved' => '1',
  3529. 'comment_parent' => $c5,
  3530. )
  3531. );
  3532. $args = array(
  3533. 'hierarchical' => 'threaded',
  3534. 'status' => 'approve',
  3535. 'orderby' => 'comment_ID',
  3536. 'order' => 'ASC',
  3537. );
  3538. $query_args = array_merge(
  3539. $args,
  3540. array(
  3541. 'post_id' => self::$post_id,
  3542. )
  3543. );
  3544. $q = new WP_Comment_Query( $query_args );
  3545. // Top-level comments.
  3546. $this->assertEqualSets( array( $c1, $c5 ), array_values( wp_list_pluck( $q->comments, 'comment_ID' ) ) );
  3547. // Direct descendants of $c1.
  3548. $this->assertEqualSets( array( $c2, $c4 ), array_values( wp_list_pluck( $q->comments[ $c1 ]->get_children( $args ), 'comment_ID' ) ) );
  3549. // Direct descendants of $c2.
  3550. $this->assertEqualSets( array(), array_values( wp_list_pluck( $q->comments[ $c1 ]->get_child( $c2 )->get_children( $args ), 'comment_ID' ) ) );
  3551. }
  3552. /**
  3553. * @ticket 35192
  3554. */
  3555. public function test_comment_clauses_prepend_callback_should_be_respected_when_filling_descendants() {
  3556. $top_level_0 = self::factory()->comment->create(
  3557. array(
  3558. 'comment_post_ID' => self::$post_id,
  3559. 'comment_approved' => '1',
  3560. )
  3561. );
  3562. $child1_of_0 = self::factory()->comment->create(
  3563. array(
  3564. 'comment_post_ID' => self::$post_id,
  3565. 'comment_approved' => '1',
  3566. 'comment_parent' => $top_level_0,
  3567. )
  3568. );
  3569. $child2_of_0 = self::factory()->comment->create(
  3570. array(
  3571. 'comment_post_ID' => self::$post_id,
  3572. 'comment_approved' => '1',
  3573. 'comment_parent' => $top_level_0,
  3574. )
  3575. );
  3576. $top_level_comments = self::factory()->comment->create_many(
  3577. 3,
  3578. array(
  3579. 'comment_post_ID' => self::$post_id,
  3580. 'comment_approved' => '1',
  3581. )
  3582. );
  3583. $this->to_exclude = array( $child2_of_0, $top_level_comments[1] );
  3584. add_filter( 'comments_clauses', array( $this, 'prepend_exclusions' ) );
  3585. $q = new WP_Comment_Query(
  3586. array(
  3587. 'post_id' => self::$post_id,
  3588. 'hierarchical' => 'flat',
  3589. )
  3590. );
  3591. remove_filter( 'comments_clauses', array( $this, 'prepend_exclusions' ) );
  3592. unset( $this->to_exclude );
  3593. $this->assertEqualSets( array( $top_level_0, $child1_of_0, $top_level_comments[0], $top_level_comments[2] ), wp_list_pluck( $q->comments, 'comment_ID' ) );
  3594. }
  3595. public function prepend_exclusions( $clauses ) {
  3596. global $wpdb;
  3597. $clauses['where'] = $wpdb->prepare( 'comment_ID != %d AND comment_ID != %d AND ', $this->to_exclude[0], $this->to_exclude[1] ) . $clauses['where'];
  3598. return $clauses;
  3599. }
  3600. /**
  3601. * @ticket 35192
  3602. */
  3603. public function test_comment_clauses_append_callback_should_be_respected_when_filling_descendants() {
  3604. $top_level_0 = self::factory()->comment->create(
  3605. array(
  3606. 'comment_post_ID' => self::$post_id,
  3607. 'comment_approved' => '1',
  3608. )
  3609. );
  3610. $child1_of_0 = self::factory()->comment->create(
  3611. array(
  3612. 'comment_post_ID' => self::$post_id,
  3613. 'comment_approved' => '1',
  3614. 'comment_parent' => $top_level_0,
  3615. )
  3616. );
  3617. $child2_of_0 = self::factory()->comment->create(
  3618. array(
  3619. 'comment_post_ID' => self::$post_id,
  3620. 'comment_approved' => '1',
  3621. 'comment_parent' => $top_level_0,
  3622. )
  3623. );
  3624. $top_level_comments = self::factory()->comment->create_many(
  3625. 3,
  3626. array(
  3627. 'comment_post_ID' => self::$post_id,
  3628. 'comment_approved' => '1',
  3629. )
  3630. );
  3631. $this->to_exclude = array( $child2_of_0, $top_level_comments[1] );
  3632. add_filter( 'comments_clauses', array( $this, 'append_exclusions' ) );
  3633. $q = new WP_Comment_Query(
  3634. array(
  3635. 'post_id' => self::$post_id,
  3636. 'hierarchical' => 'flat',
  3637. )
  3638. );
  3639. remove_filter( 'comments_clauses', array( $this, 'append_exclusions' ) );
  3640. unset( $this->to_exclude );
  3641. $this->assertEqualSets( array( $top_level_0, $child1_of_0, $top_level_comments[0], $top_level_comments[2] ), wp_list_pluck( $q->comments, 'comment_ID' ) );
  3642. }
  3643. public function append_exclusions( $clauses ) {
  3644. global $wpdb;
  3645. $clauses['where'] .= $wpdb->prepare( ' AND comment_ID != %d AND comment_ID != %d', $this->to_exclude[0], $this->to_exclude[1] );
  3646. return $clauses;
  3647. }
  3648. /**
  3649. * @ticket 36487
  3650. */
  3651. public function test_cache_should_be_hit_when_querying_descendants() {
  3652. global $wpdb;
  3653. $p = self::factory()->post->create();
  3654. $comment_1 = self::factory()->comment->create(
  3655. array(
  3656. 'comment_post_ID' => $p,
  3657. 'comment_approved' => '1',
  3658. )
  3659. );
  3660. $comment_2 = self::factory()->comment->create(
  3661. array(
  3662. 'comment_post_ID' => $p,
  3663. 'comment_approved' => '1',
  3664. 'comment_parent' => $comment_1,
  3665. )
  3666. );
  3667. $comment_3 = self::factory()->comment->create(
  3668. array(
  3669. 'comment_post_ID' => $p,
  3670. 'comment_approved' => '1',
  3671. 'comment_parent' => $comment_1,
  3672. )
  3673. );
  3674. $comment_4 = self::factory()->comment->create(
  3675. array(
  3676. 'comment_post_ID' => $p,
  3677. 'comment_approved' => '1',
  3678. 'comment_parent' => $comment_2,
  3679. )
  3680. );
  3681. $q1 = new WP_Comment_Query(
  3682. array(
  3683. 'post_id' => $p,
  3684. 'hierarchical' => true,
  3685. )
  3686. );
  3687. $q1_ids = wp_list_pluck( $q1->comments, 'comment_ID' );
  3688. $num_queries = $wpdb->num_queries;
  3689. $q2 = new WP_Comment_Query(
  3690. array(
  3691. 'post_id' => $p,
  3692. 'hierarchical' => true,
  3693. )
  3694. );
  3695. $q2_ids = wp_list_pluck( $q2->comments, 'comment_ID' );
  3696. $this->assertSameSets( $q1_ids, $q2_ids );
  3697. $this->assertSame( $num_queries, $wpdb->num_queries );
  3698. }
  3699. /**
  3700. * @ticket 37696
  3701. */
  3702. public function test_hierarchy_should_be_filled_when_cache_is_incomplete() {
  3703. global $wpdb;
  3704. $p = self::factory()->post->create();
  3705. $comment_1 = self::factory()->comment->create(
  3706. array(
  3707. 'comment_post_ID' => $p,
  3708. 'comment_approved' => '1',
  3709. )
  3710. );
  3711. $comment_2 = self::factory()->comment->create(
  3712. array(
  3713. 'comment_post_ID' => $p,
  3714. 'comment_approved' => '1',
  3715. 'comment_parent' => $comment_1,
  3716. )
  3717. );
  3718. $comment_3 = self::factory()->comment->create(
  3719. array(
  3720. 'comment_post_ID' => $p,
  3721. 'comment_approved' => '1',
  3722. 'comment_parent' => $comment_1,
  3723. )
  3724. );
  3725. $comment_4 = self::factory()->comment->create(
  3726. array(
  3727. 'comment_post_ID' => $p,
  3728. 'comment_approved' => '1',
  3729. 'comment_parent' => $comment_2,
  3730. )
  3731. );
  3732. // Prime cache.
  3733. $q1 = new WP_Comment_Query(
  3734. array(
  3735. 'post_id' => $p,
  3736. 'hierarchical' => true,
  3737. )
  3738. );
  3739. $q1_ids = wp_list_pluck( $q1->comments, 'comment_ID' );
  3740. $this->assertEqualSets( array( $comment_1, $comment_2, $comment_3, $comment_4 ), $q1_ids );
  3741. // Delete one of the parent caches.
  3742. $last_changed = wp_cache_get( 'last_changed', 'comment' );
  3743. $key = md5( serialize( wp_array_slice_assoc( $q1->query_vars, array_keys( $q1->query_var_defaults ) ) ) );
  3744. $cache_key = "get_comment_child_ids:$comment_2:$key:$last_changed";
  3745. wp_cache_delete( $cache_key, 'comment' );
  3746. $q2 = new WP_Comment_Query(
  3747. array(
  3748. 'post_id' => $p,
  3749. 'hierarchical' => true,
  3750. )
  3751. );
  3752. $q2_ids = wp_list_pluck( $q2->comments, 'comment_ID' );
  3753. $this->assertSameSets( $q1_ids, $q2_ids );
  3754. }
  3755. /**
  3756. * @ticket 37966
  3757. * @ticket 37696
  3758. */
  3759. public function test_fill_hierarchy_should_disregard_offset_and_number() {
  3760. $c0 = self::factory()->comment->create(
  3761. array(
  3762. 'comment_post_ID' => self::$post_id,
  3763. 'comment_approved' => '1',
  3764. )
  3765. );
  3766. $c1 = self::factory()->comment->create(
  3767. array(
  3768. 'comment_post_ID' => self::$post_id,
  3769. 'comment_approved' => '1',
  3770. )
  3771. );
  3772. $c2 = self::factory()->comment->create(
  3773. array(
  3774. 'comment_post_ID' => self::$post_id,
  3775. 'comment_approved' => '1',
  3776. 'comment_parent' => $c1,
  3777. )
  3778. );
  3779. $c3 = self::factory()->comment->create(
  3780. array(
  3781. 'comment_post_ID' => self::$post_id,
  3782. 'comment_approved' => '1',
  3783. )
  3784. );
  3785. $c4 = self::factory()->comment->create(
  3786. array(
  3787. 'comment_post_ID' => self::$post_id,
  3788. 'comment_approved' => '1',
  3789. 'comment_parent' => $c3,
  3790. )
  3791. );
  3792. $c5 = self::factory()->comment->create(
  3793. array(
  3794. 'comment_post_ID' => self::$post_id,
  3795. 'comment_approved' => '1',
  3796. 'comment_parent' => $c3,
  3797. )
  3798. );
  3799. $q = new WP_Comment_Query();
  3800. $found = $q->query(
  3801. array(
  3802. 'orderby' => 'comment_date_gmt',
  3803. 'order' => 'ASC',
  3804. 'status' => 'approve',
  3805. 'post_id' => self::$post_id,
  3806. 'no_found_rows' => false,
  3807. 'hierarchical' => 'threaded',
  3808. 'number' => 2,
  3809. 'offset' => 1,
  3810. )
  3811. );
  3812. $found_1 = $found[ $c1 ];
  3813. $children_1 = $found_1->get_children();
  3814. $this->assertSameSets( array( $c2 ), array_keys( $children_1 ) );
  3815. $found_3 = $found[ $c3 ];
  3816. $children_3 = $found_3->get_children();
  3817. $this->assertSameSets( array( $c4, $c5 ), array_keys( $children_3 ) );
  3818. }
  3819. /**
  3820. * @ticket 27571
  3821. */
  3822. public function test_update_comment_post_cache_should_be_disabled_by_default() {
  3823. global $wpdb;
  3824. $p = self::factory()->post->create();
  3825. $c = self::factory()->comment->create( array( 'comment_post_ID' => $p ) );
  3826. $q = new WP_Comment_Query(
  3827. array(
  3828. 'post_ID' => $p,
  3829. )
  3830. );
  3831. $num_queries = $wpdb->num_queries;
  3832. $this->assertTrue( isset( $q->comments[0]->post_name ) );
  3833. $this->assertSame( $num_queries + 1, $wpdb->num_queries );
  3834. }
  3835. /**
  3836. * @ticket 27571
  3837. */
  3838. public function test_should_respect_update_comment_post_cache_true() {
  3839. global $wpdb;
  3840. $p = self::factory()->post->create();
  3841. $c = self::factory()->comment->create( array( 'comment_post_ID' => $p ) );
  3842. $q = new WP_Comment_Query(
  3843. array(
  3844. 'post_ID' => $p,
  3845. 'update_comment_post_cache' => true,
  3846. )
  3847. );
  3848. $num_queries = $wpdb->num_queries;
  3849. $this->assertTrue( isset( $q->comments[0]->post_name ) );
  3850. $this->assertSame( $num_queries, $wpdb->num_queries );
  3851. }
  3852. /**
  3853. * @ticket 34138
  3854. */
  3855. public function test_comment_objects_should_be_filled_from_cache() {
  3856. global $wpdb;
  3857. $comments = self::factory()->comment->create_many( 3, array( 'comment_post_ID' => self::$post_id ) );
  3858. clean_comment_cache( $comments );
  3859. $num_queries = $wpdb->num_queries;
  3860. $q = new WP_Comment_Query(
  3861. array(
  3862. 'post_id' => self::$post_id,
  3863. 'no_found_rows' => true,
  3864. 'update_comment_post_cache' => false,
  3865. 'update_comment_meta_cache' => false,
  3866. )
  3867. );
  3868. // 2 queries should have been fired: one for IDs, one to prime comment caches.
  3869. $num_queries += 2;
  3870. $found = wp_list_pluck( $q->comments, 'comment_ID' );
  3871. $this->assertEqualSets( $comments, $found );
  3872. $this->assertSame( $num_queries, $wpdb->num_queries );
  3873. }
  3874. /**
  3875. * @ticket 34138
  3876. */
  3877. public function test_comment_objects_should_be_fetched_from_database_when_suspend_cache_addition() {
  3878. $suspend = wp_suspend_cache_addition();
  3879. wp_suspend_cache_addition( true );
  3880. $c = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) );
  3881. $q = new WP_Comment_Query(
  3882. array(
  3883. 'post_id' => self::$post_id,
  3884. )
  3885. );
  3886. wp_suspend_cache_addition( $suspend );
  3887. $found = wp_list_pluck( $q->comments, 'comment_ID' );
  3888. $this->assertEqualSets( array( $c ), $found );
  3889. }
  3890. public function test_comment_query_should_be_cached() {
  3891. global $wpdb;
  3892. $c = wp_insert_comment(
  3893. array(
  3894. 'comment_author' => 'Foo',
  3895. 'comment_author_email' => 'foo@example.com',
  3896. 'comment_post_ID' => self::$post_id,
  3897. )
  3898. );
  3899. $q = new WP_Comment_Query(
  3900. array(
  3901. 'post_id' => self::$post_id,
  3902. 'fields' => 'ids',
  3903. )
  3904. );
  3905. $num_queries = $wpdb->num_queries;
  3906. $q2 = new WP_Comment_Query(
  3907. array(
  3908. 'post_id' => self::$post_id,
  3909. 'fields' => 'ids',
  3910. )
  3911. );
  3912. $this->assertSame( $num_queries, $wpdb->num_queries );
  3913. }
  3914. public function test_created_comment_should_invalidate_query_cache() {
  3915. global $wpdb;
  3916. $c = self::factory()->comment->create(
  3917. array(
  3918. 'comment_post_ID' => self::$post_id,
  3919. 'comment_approved' => '1',
  3920. )
  3921. );
  3922. $q = new WP_Comment_Query(
  3923. array(
  3924. 'post_id' => self::$post_id,
  3925. 'fields' => 'ids',
  3926. )
  3927. );
  3928. $num_queries = $wpdb->num_queries;
  3929. $q = new WP_Comment_Query(
  3930. array(
  3931. 'post_id' => self::$post_id,
  3932. 'fields' => 'ids',
  3933. )
  3934. );
  3935. $this->assertSame( $num_queries, $wpdb->num_queries );
  3936. $this->assertSameSets( array( $c ), $q->comments );
  3937. }
  3938. public function test_updated_comment_should_invalidate_query_cache() {
  3939. global $wpdb;
  3940. $c = self::factory()->comment->create(
  3941. array(
  3942. 'comment_post_ID' => self::$post_id,
  3943. 'comment_approved' => '1',
  3944. )
  3945. );
  3946. $q = new WP_Comment_Query(
  3947. array(
  3948. 'post_id' => self::$post_id,
  3949. 'fields' => 'ids',
  3950. )
  3951. );
  3952. wp_update_comment(
  3953. array(
  3954. 'comment_ID' => $c,
  3955. 'comment_author' => 'Foo',
  3956. 'comment_author_email' => 'foo@example.com',
  3957. 'comment_post_ID' => self::$post_id,
  3958. )
  3959. );
  3960. $num_queries = $wpdb->num_queries;
  3961. $q = new WP_Comment_Query(
  3962. array(
  3963. 'post_id' => self::$post_id,
  3964. 'fields' => 'ids',
  3965. )
  3966. );
  3967. $num_queries++;
  3968. $this->assertSame( $num_queries, $wpdb->num_queries );
  3969. $this->assertSameSets( array( $c ), $q->comments );
  3970. }
  3971. public function test_deleted_comment_should_invalidate_query_cache() {
  3972. global $wpdb;
  3973. $c = self::factory()->comment->create(
  3974. array(
  3975. 'comment_post_ID' => self::$post_id,
  3976. 'comment_approved' => '1',
  3977. )
  3978. );
  3979. $q = new WP_Comment_Query(
  3980. array(
  3981. 'post_id' => self::$post_id,
  3982. 'fields' => 'ids',
  3983. )
  3984. );
  3985. wp_delete_comment( $c );
  3986. $num_queries = $wpdb->num_queries;
  3987. $q = new WP_Comment_Query(
  3988. array(
  3989. 'post_id' => self::$post_id,
  3990. 'fields' => 'ids',
  3991. )
  3992. );
  3993. $num_queries++;
  3994. $this->assertSame( $num_queries, $wpdb->num_queries );
  3995. $this->assertSameSets( array(), $q->comments );
  3996. }
  3997. public function test_trashed_comment_should_invalidate_query_cache() {
  3998. global $wpdb;
  3999. $c = self::factory()->comment->create(
  4000. array(
  4001. 'comment_post_ID' => self::$post_id,
  4002. 'comment_approved' => '1',
  4003. )
  4004. );
  4005. $q = new WP_Comment_Query(
  4006. array(
  4007. 'post_id' => self::$post_id,
  4008. 'fields' => 'ids',
  4009. )
  4010. );
  4011. wp_trash_comment( $c );
  4012. $num_queries = $wpdb->num_queries;
  4013. $q = new WP_Comment_Query(
  4014. array(
  4015. 'post_id' => self::$post_id,
  4016. 'fields' => 'ids',
  4017. )
  4018. );
  4019. $num_queries++;
  4020. $this->assertSame( $num_queries, $wpdb->num_queries );
  4021. $this->assertSameSets( array(), $q->comments );
  4022. }
  4023. public function test_untrashed_comment_should_invalidate_query_cache() {
  4024. global $wpdb;
  4025. $c = self::factory()->comment->create(
  4026. array(
  4027. 'comment_post_ID' => self::$post_id,
  4028. 'comment_approved' => '1',
  4029. )
  4030. );
  4031. wp_trash_comment( $c );
  4032. $q = new WP_Comment_Query(
  4033. array(
  4034. 'post_id' => self::$post_id,
  4035. 'fields' => 'ids',
  4036. )
  4037. );
  4038. wp_untrash_comment( $c );
  4039. $num_queries = $wpdb->num_queries;
  4040. $q = new WP_Comment_Query(
  4041. array(
  4042. 'post_id' => self::$post_id,
  4043. 'fields' => 'ids',
  4044. )
  4045. );
  4046. $num_queries++;
  4047. $this->assertSame( $num_queries, $wpdb->num_queries );
  4048. $this->assertSameSets( array( $c ), $q->comments );
  4049. }
  4050. public function test_spammed_comment_should_invalidate_query_cache() {
  4051. global $wpdb;
  4052. $c = self::factory()->comment->create(
  4053. array(
  4054. 'comment_post_ID' => self::$post_id,
  4055. 'comment_approved' => '1',
  4056. )
  4057. );
  4058. $q = new WP_Comment_Query(
  4059. array(
  4060. 'post_id' => self::$post_id,
  4061. 'fields' => 'ids',
  4062. )
  4063. );
  4064. wp_spam_comment( $c );
  4065. $num_queries = $wpdb->num_queries;
  4066. $q = new WP_Comment_Query(
  4067. array(
  4068. 'post_id' => self::$post_id,
  4069. 'fields' => 'ids',
  4070. )
  4071. );
  4072. $num_queries++;
  4073. $this->assertSame( $num_queries, $wpdb->num_queries );
  4074. $this->assertSameSets( array(), $q->comments );
  4075. }
  4076. public function test_unspammed_comment_should_invalidate_query_cache() {
  4077. global $wpdb;
  4078. $c = self::factory()->comment->create(
  4079. array(
  4080. 'comment_post_ID' => self::$post_id,
  4081. 'comment_approved' => '1',
  4082. )
  4083. );
  4084. wp_spam_comment( $c );
  4085. $q = new WP_Comment_Query(
  4086. array(
  4087. 'post_id' => self::$post_id,
  4088. 'fields' => 'ids',
  4089. )
  4090. );
  4091. wp_unspam_comment( $c );
  4092. $num_queries = $wpdb->num_queries;
  4093. $q = new WP_Comment_Query(
  4094. array(
  4095. 'post_id' => self::$post_id,
  4096. 'fields' => 'ids',
  4097. )
  4098. );
  4099. $num_queries++;
  4100. $this->assertSame( $num_queries, $wpdb->num_queries );
  4101. $this->assertSameSets( array( $c ), $q->comments );
  4102. }
  4103. /**
  4104. * @ticket 41348
  4105. */
  4106. public function test_count_query_should_miss_noncount_cache() {
  4107. global $wpdb;
  4108. $q = new WP_Comment_Query();
  4109. $query_1 = $q->query(
  4110. array(
  4111. 'fields' => 'ids',
  4112. 'number' => 3,
  4113. 'order' => 'ASC',
  4114. )
  4115. );
  4116. $number_of_queries = $wpdb->num_queries;
  4117. $query_2 = $q->query(
  4118. array(
  4119. 'fields' => 'ids',
  4120. 'number' => 3,
  4121. 'order' => 'ASC',
  4122. 'count' => true,
  4123. )
  4124. );
  4125. $this->assertSame( $number_of_queries + 1, $wpdb->num_queries );
  4126. }
  4127. /**
  4128. * @ticket 41348
  4129. */
  4130. public function test_count_query_should_hit_count_cache() {
  4131. global $wpdb;
  4132. $q = new WP_Comment_Query();
  4133. $query_1 = $q->query(
  4134. array(
  4135. 'fields' => 'ids',
  4136. 'number' => 3,
  4137. 'order' => 'ASC',
  4138. 'count' => true,
  4139. )
  4140. );
  4141. $number_of_queries = $wpdb->num_queries;
  4142. $query_2 = $q->query(
  4143. array(
  4144. 'fields' => 'ids',
  4145. 'number' => 3,
  4146. 'order' => 'ASC',
  4147. 'count' => true,
  4148. )
  4149. );
  4150. $this->assertSame( $number_of_queries, $wpdb->num_queries );
  4151. }
  4152. /**
  4153. * @ticket 41348
  4154. */
  4155. public function test_different_values_of_fields_should_share_cached_values() {
  4156. global $wpdb;
  4157. $q = new WP_Comment_Query();
  4158. $query_1 = $q->query(
  4159. array(
  4160. 'fields' => 'all',
  4161. 'number' => 3,
  4162. 'order' => 'ASC',
  4163. )
  4164. );
  4165. $number_of_queries = $wpdb->num_queries;
  4166. $query_2 = $q->query(
  4167. array(
  4168. 'fields' => 'ids',
  4169. 'number' => 3,
  4170. 'order' => 'ASC',
  4171. )
  4172. );
  4173. $this->assertSame( $number_of_queries, $wpdb->num_queries );
  4174. }
  4175. /**
  4176. * @ticket 40669
  4177. */
  4178. public function test_add_comment_meta_should_invalidate_query_cache() {
  4179. global $wpdb;
  4180. $p = self::factory()->post->create( array( 'post_status' => 'publish' ) );
  4181. $c1 = self::factory()->comment->create_post_comments( $p, 1 );
  4182. $c2 = self::factory()->comment->create_post_comments( $p, 1 );
  4183. foreach ( $c1 as $cid ) {
  4184. add_comment_meta( $cid, 'sauce', 'fire' );
  4185. }
  4186. $cached = get_comments(
  4187. array(
  4188. 'post_id' => $p,
  4189. 'fields' => 'ids',
  4190. 'meta_query' => array(
  4191. array(
  4192. 'key' => 'sauce',
  4193. 'value' => 'fire',
  4194. ),
  4195. ),
  4196. )
  4197. );
  4198. $this->assertSameSets( $c1, $cached );
  4199. foreach ( $c2 as $cid ) {
  4200. add_comment_meta( $cid, 'sauce', 'fire' );
  4201. }
  4202. $found = get_comments(
  4203. array(
  4204. 'post_id' => $p,
  4205. 'fields' => 'ids',
  4206. 'meta_query' => array(
  4207. array(
  4208. 'key' => 'sauce',
  4209. 'value' => 'fire',
  4210. ),
  4211. ),
  4212. )
  4213. );
  4214. $this->assertSameSets( array_merge( $c1, $c2 ), $found );
  4215. }
  4216. /**
  4217. * @ticket 40669
  4218. */
  4219. public function test_update_comment_meta_should_invalidate_query_cache() {
  4220. global $wpdb;
  4221. $p = self::factory()->post->create( array( 'post_status' => 'publish' ) );
  4222. $c1 = self::factory()->comment->create_post_comments( $p, 1 );
  4223. $c2 = self::factory()->comment->create_post_comments( $p, 1 );
  4224. foreach ( array_merge( $c1, $c2 ) as $cid ) {
  4225. add_comment_meta( $cid, 'sauce', 'fire' );
  4226. }
  4227. $cached = get_comments(
  4228. array(
  4229. 'post_id' => $p,
  4230. 'fields' => 'ids',
  4231. 'meta_query' => array(
  4232. array(
  4233. 'key' => 'sauce',
  4234. 'value' => 'fire',
  4235. ),
  4236. ),
  4237. )
  4238. );
  4239. $this->assertSameSets( array_merge( $c1, $c2 ), $cached );
  4240. foreach ( $c2 as $cid ) {
  4241. update_comment_meta( $cid, 'sauce', 'foo' );
  4242. }
  4243. $found = get_comments(
  4244. array(
  4245. 'post_id' => $p,
  4246. 'fields' => 'ids',
  4247. 'meta_query' => array(
  4248. array(
  4249. 'key' => 'sauce',
  4250. 'value' => 'fire',
  4251. ),
  4252. ),
  4253. )
  4254. );
  4255. $this->assertSameSets( $c1, $found );
  4256. }
  4257. /**
  4258. * @ticket 40669
  4259. */
  4260. public function test_delete_comment_meta_should_invalidate_query_cache() {
  4261. global $wpdb;
  4262. $p = self::factory()->post->create( array( 'post_status' => 'publish' ) );
  4263. $c1 = self::factory()->comment->create_post_comments( $p, 1 );
  4264. $c2 = self::factory()->comment->create_post_comments( $p, 1 );
  4265. foreach ( array_merge( $c1, $c2 ) as $cid ) {
  4266. add_comment_meta( $cid, 'sauce', 'fire' );
  4267. }
  4268. $cached = get_comments(
  4269. array(
  4270. 'post_id' => $p,
  4271. 'fields' => 'ids',
  4272. 'meta_query' => array(
  4273. array(
  4274. 'key' => 'sauce',
  4275. 'value' => 'fire',
  4276. ),
  4277. ),
  4278. )
  4279. );
  4280. $this->assertSameSets( array_merge( $c1, $c2 ), $cached );
  4281. foreach ( $c2 as $cid ) {
  4282. delete_comment_meta( $cid, 'sauce' );
  4283. }
  4284. $found = get_comments(
  4285. array(
  4286. 'post_id' => $p,
  4287. 'fields' => 'ids',
  4288. 'meta_query' => array(
  4289. array(
  4290. 'key' => 'sauce',
  4291. 'value' => 'fire',
  4292. ),
  4293. ),
  4294. )
  4295. );
  4296. $this->assertSameSets( $c1, $found );
  4297. }
  4298. /**
  4299. * @ticket 45800
  4300. */
  4301. public function test_comments_pre_query_filter_should_bypass_database_query() {
  4302. global $wpdb;
  4303. add_filter( 'comments_pre_query', array( __CLASS__, 'filter_comments_pre_query' ), 10, 2 );
  4304. $num_queries = $wpdb->num_queries;
  4305. $q = new WP_Comment_Query();
  4306. $results = $q->query( array() );
  4307. remove_filter( 'comments_pre_query', array( __CLASS__, 'filter_comments_pre_query' ), 10, 2 );
  4308. // Make sure no queries were executed.
  4309. $this->assertSame( $num_queries, $wpdb->num_queries );
  4310. // We manually inserted a non-existing site and overrode the results with it.
  4311. $this->assertSame( array( 555 ), $results );
  4312. // Make sure manually setting found_comments doesn't get overwritten.
  4313. $this->assertSame( 1, $q->found_comments );
  4314. }
  4315. public static function filter_comments_pre_query( $comments, $query ) {
  4316. $query->found_comments = 1;
  4317. return array( 555 );
  4318. }
  4319. /**
  4320. * @ticket 50521
  4321. */
  4322. public function test_comments_pre_query_filter_should_set_comments_property() {
  4323. add_filter( 'comments_pre_query', array( __CLASS__, 'filter_comments_pre_query_and_set_comments' ), 10, 2 );
  4324. $q = new WP_Comment_Query();
  4325. $results = $q->query( array() );
  4326. remove_filter( 'comments_pre_query', array( __CLASS__, 'filter_comments_pre_query_and_set_comments' ), 10 );
  4327. // Make sure the comments property is the same as the results.
  4328. $this->assertSame( $results, $q->comments );
  4329. // Make sure the comment type is `foobar`.
  4330. $this->assertSame( 'foobar', $q->comments[0]->comment_type );
  4331. }
  4332. public static function filter_comments_pre_query_and_set_comments( $comments, $query ) {
  4333. $c = self::factory()->comment->create(
  4334. array(
  4335. 'comment_type' => 'foobar',
  4336. 'comment_approved' => '1',
  4337. )
  4338. );
  4339. return array( get_comment( $c ) );
  4340. }
  4341. }