database_test.test 152 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437
  1. <?php
  2. /**
  3. * Dummy class for fetching into a class.
  4. *
  5. * PDO supports using a new instance of an arbitrary class for records
  6. * rather than just a stdClass or array. This class is for testing that
  7. * functionality. (See testQueryFetchClass() below)
  8. */
  9. class FakeRecord { }
  10. /**
  11. * Base test class for databases.
  12. *
  13. * Because all database tests share the same test data, we can centralize that
  14. * here.
  15. */
  16. class DatabaseTestCase extends DrupalWebTestCase {
  17. protected $profile = 'testing';
  18. function setUp() {
  19. parent::setUp('database_test');
  20. $schema['test'] = drupal_get_schema('test');
  21. $schema['test_people'] = drupal_get_schema('test_people');
  22. $schema['test_people_copy'] = drupal_get_schema('test_people_copy');
  23. $schema['test_one_blob'] = drupal_get_schema('test_one_blob');
  24. $schema['test_two_blobs'] = drupal_get_schema('test_two_blobs');
  25. $schema['test_task'] = drupal_get_schema('test_task');
  26. $this->installTables($schema);
  27. $this->addSampleData();
  28. }
  29. /**
  30. * Set up several tables needed by a certain test.
  31. *
  32. * @param $schema
  33. * An array of table definitions to install.
  34. */
  35. function installTables($schema) {
  36. // This ends up being a test for table drop and create, too, which is nice.
  37. foreach ($schema as $name => $data) {
  38. if (db_table_exists($name)) {
  39. db_drop_table($name);
  40. }
  41. db_create_table($name, $data);
  42. }
  43. foreach ($schema as $name => $data) {
  44. $this->assertTrue(db_table_exists($name), format_string('Table @name created successfully.', array('@name' => $name)));
  45. }
  46. }
  47. /**
  48. * Set up tables for NULL handling.
  49. */
  50. function ensureSampleDataNull() {
  51. $schema['test_null'] = drupal_get_schema('test_null');
  52. $this->installTables($schema);
  53. db_insert('test_null')
  54. ->fields(array('name', 'age'))
  55. ->values(array(
  56. 'name' => 'Kermit',
  57. 'age' => 25,
  58. ))
  59. ->values(array(
  60. 'name' => 'Fozzie',
  61. 'age' => NULL,
  62. ))
  63. ->values(array(
  64. 'name' => 'Gonzo',
  65. 'age' => 27,
  66. ))
  67. ->execute();
  68. }
  69. /**
  70. * Setup our sample data.
  71. *
  72. * These are added using db_query(), since we're not trying to test the
  73. * INSERT operations here, just populate.
  74. */
  75. function addSampleData() {
  76. // We need the IDs, so we can't use a multi-insert here.
  77. $john = db_insert('test')
  78. ->fields(array(
  79. 'name' => 'John',
  80. 'age' => 25,
  81. 'job' => 'Singer',
  82. ))
  83. ->execute();
  84. $george = db_insert('test')
  85. ->fields(array(
  86. 'name' => 'George',
  87. 'age' => 27,
  88. 'job' => 'Singer',
  89. ))
  90. ->execute();
  91. $ringo = db_insert('test')
  92. ->fields(array(
  93. 'name' => 'Ringo',
  94. 'age' => 28,
  95. 'job' => 'Drummer',
  96. ))
  97. ->execute();
  98. $paul = db_insert('test')
  99. ->fields(array(
  100. 'name' => 'Paul',
  101. 'age' => 26,
  102. 'job' => 'Songwriter',
  103. ))
  104. ->execute();
  105. db_insert('test_people')
  106. ->fields(array(
  107. 'name' => 'Meredith',
  108. 'age' => 30,
  109. 'job' => 'Speaker',
  110. ))
  111. ->execute();
  112. db_insert('test_task')
  113. ->fields(array('pid', 'task', 'priority'))
  114. ->values(array(
  115. 'pid' => $john,
  116. 'task' => 'eat',
  117. 'priority' => 3,
  118. ))
  119. ->values(array(
  120. 'pid' => $john,
  121. 'task' => 'sleep',
  122. 'priority' => 4,
  123. ))
  124. ->values(array(
  125. 'pid' => $john,
  126. 'task' => 'code',
  127. 'priority' => 1,
  128. ))
  129. ->values(array(
  130. 'pid' => $george,
  131. 'task' => 'sing',
  132. 'priority' => 2,
  133. ))
  134. ->values(array(
  135. 'pid' => $george,
  136. 'task' => 'sleep',
  137. 'priority' => 2,
  138. ))
  139. ->values(array(
  140. 'pid' => $paul,
  141. 'task' => 'found new band',
  142. 'priority' => 1,
  143. ))
  144. ->values(array(
  145. 'pid' => $paul,
  146. 'task' => 'perform at superbowl',
  147. 'priority' => 3,
  148. ))
  149. ->execute();
  150. db_insert('virtual')
  151. ->fields(array(
  152. 'function' => 'Function value 1',
  153. ))
  154. ->execute();
  155. }
  156. }
  157. /**
  158. * Test connection management.
  159. */
  160. class DatabaseConnectionTestCase extends DatabaseTestCase {
  161. public static function getInfo() {
  162. return array(
  163. 'name' => 'Connection tests',
  164. 'description' => 'Tests of the core database system.',
  165. 'group' => 'Database',
  166. );
  167. }
  168. /**
  169. * Test that connections return appropriate connection objects.
  170. */
  171. function testConnectionRouting() {
  172. // Clone the master credentials to a slave connection.
  173. // Note this will result in two independent connection objects that happen
  174. // to point to the same place.
  175. $connection_info = Database::getConnectionInfo('default');
  176. Database::addConnectionInfo('default', 'slave', $connection_info['default']);
  177. $db1 = Database::getConnection('default', 'default');
  178. $db2 = Database::getConnection('slave', 'default');
  179. $this->assertNotNull($db1, 'default connection is a real connection object.');
  180. $this->assertNotNull($db2, 'slave connection is a real connection object.');
  181. $this->assertNotIdentical($db1, $db2, 'Each target refers to a different connection.');
  182. // Try to open those targets another time, that should return the same objects.
  183. $db1b = Database::getConnection('default', 'default');
  184. $db2b = Database::getConnection('slave', 'default');
  185. $this->assertIdentical($db1, $db1b, 'A second call to getConnection() returns the same object.');
  186. $this->assertIdentical($db2, $db2b, 'A second call to getConnection() returns the same object.');
  187. // Try to open an unknown target.
  188. $unknown_target = $this->randomName();
  189. $db3 = Database::getConnection($unknown_target, 'default');
  190. $this->assertNotNull($db3, 'Opening an unknown target returns a real connection object.');
  191. $this->assertIdentical($db1, $db3, 'An unknown target opens the default connection.');
  192. // Try to open that unknown target another time, that should return the same object.
  193. $db3b = Database::getConnection($unknown_target, 'default');
  194. $this->assertIdentical($db3, $db3b, 'A second call to getConnection() returns the same object.');
  195. }
  196. /**
  197. * Test that connections return appropriate connection objects.
  198. */
  199. function testConnectionRoutingOverride() {
  200. // Clone the master credentials to a slave connection.
  201. // Note this will result in two independent connection objects that happen
  202. // to point to the same place.
  203. $connection_info = Database::getConnectionInfo('default');
  204. Database::addConnectionInfo('default', 'slave', $connection_info['default']);
  205. Database::ignoreTarget('default', 'slave');
  206. $db1 = Database::getConnection('default', 'default');
  207. $db2 = Database::getConnection('slave', 'default');
  208. $this->assertIdentical($db1, $db2, 'Both targets refer to the same connection.');
  209. }
  210. /**
  211. * Tests the closing of a database connection.
  212. */
  213. function testConnectionClosing() {
  214. // Open the default target so we have an object to compare.
  215. $db1 = Database::getConnection('default', 'default');
  216. // Try to close the default connection, then open a new one.
  217. Database::closeConnection('default', 'default');
  218. $db2 = Database::getConnection('default', 'default');
  219. // Opening a connection after closing it should yield an object different than the original.
  220. $this->assertNotIdentical($db1, $db2, 'Opening the default connection after it is closed returns a new object.');
  221. }
  222. /**
  223. * Tests the connection options of the active database.
  224. */
  225. function testConnectionOptions() {
  226. $connection_info = Database::getConnectionInfo('default');
  227. // Be sure we're connected to the default database.
  228. $db = Database::getConnection('default', 'default');
  229. $connectionOptions = $db->getConnectionOptions();
  230. // In the MySQL driver, the port can be different, so check individual
  231. // options.
  232. $this->assertEqual($connection_info['default']['driver'], $connectionOptions['driver'], 'The default connection info driver matches the current connection options driver.');
  233. $this->assertEqual($connection_info['default']['database'], $connectionOptions['database'], 'The default connection info database matches the current connection options database.');
  234. // Set up identical slave and confirm connection options are identical.
  235. Database::addConnectionInfo('default', 'slave', $connection_info['default']);
  236. $db2 = Database::getConnection('slave', 'default');
  237. $connectionOptions2 = $db2->getConnectionOptions();
  238. // Get a fresh copy of the default connection options.
  239. $connectionOptions = $db->getConnectionOptions();
  240. $this->assertIdentical($connectionOptions, $connectionOptions2, 'The default and slave connection options are identical.');
  241. // Set up a new connection with different connection info.
  242. $test = $connection_info['default'];
  243. $test['database'] .= 'test';
  244. Database::addConnectionInfo('test', 'default', $test);
  245. $connection_info = Database::getConnectionInfo('test');
  246. // Get a fresh copy of the default connection options.
  247. $connectionOptions = $db->getConnectionOptions();
  248. $this->assertNotEqual($connection_info['default']['database'], $connectionOptions['database'], 'The test connection info database does not match the current connection options database.');
  249. }
  250. }
  251. /**
  252. * Test cloning Select queries.
  253. */
  254. class DatabaseSelectCloneTest extends DatabaseTestCase {
  255. public static function getInfo() {
  256. return array(
  257. 'name' => 'Select tests, cloning',
  258. 'description' => 'Test cloning Select queries.',
  259. 'group' => 'Database',
  260. );
  261. }
  262. /**
  263. * Test that subqueries as value within conditions are cloned properly.
  264. */
  265. function testSelectConditionSubQueryCloning() {
  266. $subquery = db_select('test', 't');
  267. $subquery->addField('t', 'id', 'id');
  268. $subquery->condition('age', 28, '<');
  269. $query = db_select('test', 't');
  270. $query->addField('t', 'name', 'name');
  271. $query->condition('id', $subquery, 'IN');
  272. $clone = clone $query;
  273. // Cloned query should not be altered by the following modification
  274. // happening on original query.
  275. $subquery->condition('age', 25, '>');
  276. $clone_result = $clone->countQuery()->execute()->fetchField();
  277. $query_result = $query->countQuery()->execute()->fetchField();
  278. // Make sure the cloned query has not been modified
  279. $this->assertEqual(3, $clone_result, 'The cloned query returns the expected number of rows');
  280. $this->assertEqual(2, $query_result, 'The query returns the expected number of rows');
  281. }
  282. }
  283. /**
  284. * Test fetch actions, part 1.
  285. *
  286. * We get timeout errors if we try to run too many tests at once.
  287. */
  288. class DatabaseFetchTestCase extends DatabaseTestCase {
  289. public static function getInfo() {
  290. return array(
  291. 'name' => 'Fetch tests',
  292. 'description' => 'Test the Database system\'s various fetch capabilities.',
  293. 'group' => 'Database',
  294. );
  295. }
  296. /**
  297. * Confirm that we can fetch a record properly in default object mode.
  298. */
  299. function testQueryFetchDefault() {
  300. $records = array();
  301. $result = db_query('SELECT name FROM {test} WHERE age = :age', array(':age' => 25));
  302. $this->assertTrue($result instanceof DatabaseStatementInterface, 'Result set is a Drupal statement object.');
  303. foreach ($result as $record) {
  304. $records[] = $record;
  305. $this->assertTrue(is_object($record), 'Record is an object.');
  306. $this->assertIdentical($record->name, 'John', '25 year old is John.');
  307. }
  308. $this->assertIdentical(count($records), 1, 'There is only one record.');
  309. }
  310. /**
  311. * Confirm that we can fetch a record to an object explicitly.
  312. */
  313. function testQueryFetchObject() {
  314. $records = array();
  315. $result = db_query('SELECT name FROM {test} WHERE age = :age', array(':age' => 25), array('fetch' => PDO::FETCH_OBJ));
  316. foreach ($result as $record) {
  317. $records[] = $record;
  318. $this->assertTrue(is_object($record), 'Record is an object.');
  319. $this->assertIdentical($record->name, 'John', '25 year old is John.');
  320. }
  321. $this->assertIdentical(count($records), 1, 'There is only one record.');
  322. }
  323. /**
  324. * Confirm that we can fetch a record to an array associative explicitly.
  325. */
  326. function testQueryFetchArray() {
  327. $records = array();
  328. $result = db_query('SELECT name FROM {test} WHERE age = :age', array(':age' => 25), array('fetch' => PDO::FETCH_ASSOC));
  329. foreach ($result as $record) {
  330. $records[] = $record;
  331. if ($this->assertTrue(is_array($record), 'Record is an array.')) {
  332. $this->assertIdentical($record['name'], 'John', 'Record can be accessed associatively.');
  333. }
  334. }
  335. $this->assertIdentical(count($records), 1, 'There is only one record.');
  336. }
  337. /**
  338. * Confirm that we can fetch a record into a new instance of a custom class.
  339. *
  340. * @see FakeRecord
  341. */
  342. function testQueryFetchClass() {
  343. $records = array();
  344. $result = db_query('SELECT name FROM {test} WHERE age = :age', array(':age' => 25), array('fetch' => 'FakeRecord'));
  345. foreach ($result as $record) {
  346. $records[] = $record;
  347. if ($this->assertTrue($record instanceof FakeRecord, 'Record is an object of class FakeRecord.')) {
  348. $this->assertIdentical($record->name, 'John', '25 year old is John.');
  349. }
  350. }
  351. $this->assertIdentical(count($records), 1, 'There is only one record.');
  352. }
  353. }
  354. /**
  355. * Test fetch actions, part 2.
  356. *
  357. * We get timeout errors if we try to run too many tests at once.
  358. */
  359. class DatabaseFetch2TestCase extends DatabaseTestCase {
  360. public static function getInfo() {
  361. return array(
  362. 'name' => 'Fetch tests, part 2',
  363. 'description' => 'Test the Database system\'s various fetch capabilities.',
  364. 'group' => 'Database',
  365. );
  366. }
  367. function setUp() {
  368. parent::setUp();
  369. }
  370. // Confirm that we can fetch a record into an indexed array explicitly.
  371. function testQueryFetchNum() {
  372. $records = array();
  373. $result = db_query('SELECT name FROM {test} WHERE age = :age', array(':age' => 25), array('fetch' => PDO::FETCH_NUM));
  374. foreach ($result as $record) {
  375. $records[] = $record;
  376. if ($this->assertTrue(is_array($record), 'Record is an array.')) {
  377. $this->assertIdentical($record[0], 'John', 'Record can be accessed numerically.');
  378. }
  379. }
  380. $this->assertIdentical(count($records), 1, 'There is only one record');
  381. }
  382. /**
  383. * Confirm that we can fetch a record into a doubly-keyed array explicitly.
  384. */
  385. function testQueryFetchBoth() {
  386. $records = array();
  387. $result = db_query('SELECT name FROM {test} WHERE age = :age', array(':age' => 25), array('fetch' => PDO::FETCH_BOTH));
  388. foreach ($result as $record) {
  389. $records[] = $record;
  390. if ($this->assertTrue(is_array($record), 'Record is an array.')) {
  391. $this->assertIdentical($record[0], 'John', 'Record can be accessed numerically.');
  392. $this->assertIdentical($record['name'], 'John', 'Record can be accessed associatively.');
  393. }
  394. }
  395. $this->assertIdentical(count($records), 1, 'There is only one record.');
  396. }
  397. /**
  398. * Confirm that we can fetch an entire column of a result set at once.
  399. */
  400. function testQueryFetchCol() {
  401. $records = array();
  402. $result = db_query('SELECT name FROM {test} WHERE age > :age', array(':age' => 25));
  403. $column = $result->fetchCol();
  404. $this->assertIdentical(count($column), 3, 'fetchCol() returns the right number of records.');
  405. $result = db_query('SELECT name FROM {test} WHERE age > :age', array(':age' => 25));
  406. $i = 0;
  407. foreach ($result as $record) {
  408. $this->assertIdentical($record->name, $column[$i++], 'Column matches direct accesss.');
  409. }
  410. }
  411. }
  412. /**
  413. * Test the insert builder.
  414. */
  415. class DatabaseInsertTestCase extends DatabaseTestCase {
  416. public static function getInfo() {
  417. return array(
  418. 'name' => 'Insert tests',
  419. 'description' => 'Test the Insert query builder.',
  420. 'group' => 'Database',
  421. );
  422. }
  423. /**
  424. * Test the very basic insert functionality.
  425. */
  426. function testSimpleInsert() {
  427. $num_records_before = db_query('SELECT COUNT(*) FROM {test}')->fetchField();
  428. $query = db_insert('test');
  429. $query->fields(array(
  430. 'name' => 'Yoko',
  431. 'age' => '29',
  432. ));
  433. $query->execute();
  434. $num_records_after = db_query('SELECT COUNT(*) FROM {test}')->fetchField();
  435. $this->assertIdentical($num_records_before + 1, (int) $num_records_after, 'Record inserts correctly.');
  436. $saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Yoko'))->fetchField();
  437. $this->assertIdentical($saved_age, '29', 'Can retrieve after inserting.');
  438. }
  439. /**
  440. * Test that we can insert multiple records in one query object.
  441. */
  442. function testMultiInsert() {
  443. $num_records_before = (int) db_query('SELECT COUNT(*) FROM {test}')->fetchField();
  444. $query = db_insert('test');
  445. $query->fields(array(
  446. 'name' => 'Larry',
  447. 'age' => '30',
  448. ));
  449. // We should be able to specify values in any order if named.
  450. $query->values(array(
  451. 'age' => '31',
  452. 'name' => 'Curly',
  453. ));
  454. // We should be able to say "use the field order".
  455. // This is not the recommended mechanism for most cases, but it should work.
  456. $query->values(array('Moe', '32'));
  457. $query->execute();
  458. $num_records_after = (int) db_query('SELECT COUNT(*) FROM {test}')->fetchField();
  459. $this->assertIdentical($num_records_before + 3, $num_records_after, 'Record inserts correctly.');
  460. $saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Larry'))->fetchField();
  461. $this->assertIdentical($saved_age, '30', 'Can retrieve after inserting.');
  462. $saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Curly'))->fetchField();
  463. $this->assertIdentical($saved_age, '31', 'Can retrieve after inserting.');
  464. $saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Moe'))->fetchField();
  465. $this->assertIdentical($saved_age, '32', 'Can retrieve after inserting.');
  466. }
  467. /**
  468. * Test that an insert object can be reused with new data after it executes.
  469. */
  470. function testRepeatedInsert() {
  471. $num_records_before = db_query('SELECT COUNT(*) FROM {test}')->fetchField();
  472. $query = db_insert('test');
  473. $query->fields(array(
  474. 'name' => 'Larry',
  475. 'age' => '30',
  476. ));
  477. $query->execute(); // This should run the insert, but leave the fields intact.
  478. // We should be able to specify values in any order if named.
  479. $query->values(array(
  480. 'age' => '31',
  481. 'name' => 'Curly',
  482. ));
  483. $query->execute();
  484. // We should be able to say "use the field order".
  485. $query->values(array('Moe', '32'));
  486. $query->execute();
  487. $num_records_after = db_query('SELECT COUNT(*) FROM {test}')->fetchField();
  488. $this->assertIdentical((int) $num_records_before + 3, (int) $num_records_after, 'Record inserts correctly.');
  489. $saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Larry'))->fetchField();
  490. $this->assertIdentical($saved_age, '30', 'Can retrieve after inserting.');
  491. $saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Curly'))->fetchField();
  492. $this->assertIdentical($saved_age, '31', 'Can retrieve after inserting.');
  493. $saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Moe'))->fetchField();
  494. $this->assertIdentical($saved_age, '32', 'Can retrieve after inserting.');
  495. }
  496. /**
  497. * Test that we can specify fields without values and specify values later.
  498. */
  499. function testInsertFieldOnlyDefinintion() {
  500. // This is useful for importers, when we want to create a query and define
  501. // its fields once, then loop over a multi-insert execution.
  502. db_insert('test')
  503. ->fields(array('name', 'age'))
  504. ->values(array('Larry', '30'))
  505. ->values(array('Curly', '31'))
  506. ->values(array('Moe', '32'))
  507. ->execute();
  508. $saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Larry'))->fetchField();
  509. $this->assertIdentical($saved_age, '30', 'Can retrieve after inserting.');
  510. $saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Curly'))->fetchField();
  511. $this->assertIdentical($saved_age, '31', 'Can retrieve after inserting.');
  512. $saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Moe'))->fetchField();
  513. $this->assertIdentical($saved_age, '32', 'Can retrieve after inserting.');
  514. }
  515. /**
  516. * Test that inserts return the proper auto-increment ID.
  517. */
  518. function testInsertLastInsertID() {
  519. $id = db_insert('test')
  520. ->fields(array(
  521. 'name' => 'Larry',
  522. 'age' => '30',
  523. ))
  524. ->execute();
  525. $this->assertIdentical($id, '5', 'Auto-increment ID returned successfully.');
  526. }
  527. /**
  528. * Test that the INSERT INTO ... SELECT (fields) ... syntax works.
  529. */
  530. function testInsertSelectFields() {
  531. $query = db_select('test_people', 'tp');
  532. // The query builder will always append expressions after fields.
  533. // Add the expression first to test that the insert fields are correctly
  534. // re-ordered.
  535. $query->addExpression('tp.age', 'age');
  536. $query
  537. ->fields('tp', array('name','job'))
  538. ->condition('tp.name', 'Meredith');
  539. // The resulting query should be equivalent to:
  540. // INSERT INTO test (age, name, job)
  541. // SELECT tp.age AS age, tp.name AS name, tp.job AS job
  542. // FROM test_people tp
  543. // WHERE tp.name = 'Meredith'
  544. db_insert('test')
  545. ->from($query)
  546. ->execute();
  547. $saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Meredith'))->fetchField();
  548. $this->assertIdentical($saved_age, '30', 'Can retrieve after inserting.');
  549. }
  550. /**
  551. * Tests that the INSERT INTO ... SELECT * ... syntax works.
  552. */
  553. function testInsertSelectAll() {
  554. $query = db_select('test_people', 'tp')
  555. ->fields('tp')
  556. ->condition('tp.name', 'Meredith');
  557. // The resulting query should be equivalent to:
  558. // INSERT INTO test_people_copy
  559. // SELECT *
  560. // FROM test_people tp
  561. // WHERE tp.name = 'Meredith'
  562. db_insert('test_people_copy')
  563. ->from($query)
  564. ->execute();
  565. $saved_age = db_query('SELECT age FROM {test_people_copy} WHERE name = :name', array(':name' => 'Meredith'))->fetchField();
  566. $this->assertIdentical($saved_age, '30', 'Can retrieve after inserting.');
  567. }
  568. }
  569. /**
  570. * Insert tests using LOB fields, which are weird on some databases.
  571. */
  572. class DatabaseInsertLOBTestCase extends DatabaseTestCase {
  573. public static function getInfo() {
  574. return array(
  575. 'name' => 'Insert tests, LOB fields',
  576. 'description' => 'Test the Insert query builder with LOB fields.',
  577. 'group' => 'Database',
  578. );
  579. }
  580. /**
  581. * Test that we can insert a single blob field successfully.
  582. */
  583. function testInsertOneBlob() {
  584. $data = "This is\000a test.";
  585. $this->assertTrue(strlen($data) === 15, 'Test data contains a NULL.');
  586. $id = db_insert('test_one_blob')
  587. ->fields(array('blob1' => $data))
  588. ->execute();
  589. $r = db_query('SELECT * FROM {test_one_blob} WHERE id = :id', array(':id' => $id))->fetchAssoc();
  590. $this->assertTrue($r['blob1'] === $data, format_string('Can insert a blob: id @id, @data.', array('@id' => $id, '@data' => serialize($r))));
  591. }
  592. /**
  593. * Test that we can insert multiple blob fields in the same query.
  594. */
  595. function testInsertMultipleBlob() {
  596. $id = db_insert('test_two_blobs')
  597. ->fields(array(
  598. 'blob1' => 'This is',
  599. 'blob2' => 'a test',
  600. ))
  601. ->execute();
  602. $r = db_query('SELECT * FROM {test_two_blobs} WHERE id = :id', array(':id' => $id))->fetchAssoc();
  603. $this->assertTrue($r['blob1'] === 'This is' && $r['blob2'] === 'a test', 'Can insert multiple blobs per row.');
  604. }
  605. }
  606. /**
  607. * Insert tests for "database default" values.
  608. */
  609. class DatabaseInsertDefaultsTestCase extends DatabaseTestCase {
  610. public static function getInfo() {
  611. return array(
  612. 'name' => 'Insert tests, default fields',
  613. 'description' => 'Test the Insert query builder with default values.',
  614. 'group' => 'Database',
  615. );
  616. }
  617. /**
  618. * Test that we can run a query that is "default values for everything".
  619. */
  620. function testDefaultInsert() {
  621. $query = db_insert('test')->useDefaults(array('job'));
  622. $id = $query->execute();
  623. $schema = drupal_get_schema('test');
  624. $job = db_query('SELECT job FROM {test} WHERE id = :id', array(':id' => $id))->fetchField();
  625. $this->assertEqual($job, $schema['fields']['job']['default'], 'Default field value is set.');
  626. }
  627. /**
  628. * Test that no action will be preformed if no fields are specified.
  629. */
  630. function testDefaultEmptyInsert() {
  631. $num_records_before = (int) db_query('SELECT COUNT(*) FROM {test}')->fetchField();
  632. try {
  633. $result = db_insert('test')->execute();
  634. // This is only executed if no exception has been thrown.
  635. $this->fail('Expected exception NoFieldsException has not been thrown.');
  636. } catch (NoFieldsException $e) {
  637. $this->pass('Expected exception NoFieldsException has been thrown.');
  638. }
  639. $num_records_after = (int) db_query('SELECT COUNT(*) FROM {test}')->fetchField();
  640. $this->assertIdentical($num_records_before, $num_records_after, 'Do nothing as no fields are specified.');
  641. }
  642. /**
  643. * Test that we can insert fields with values and defaults in the same query.
  644. */
  645. function testDefaultInsertWithFields() {
  646. $query = db_insert('test')
  647. ->fields(array('name' => 'Bob'))
  648. ->useDefaults(array('job'));
  649. $id = $query->execute();
  650. $schema = drupal_get_schema('test');
  651. $job = db_query('SELECT job FROM {test} WHERE id = :id', array(':id' => $id))->fetchField();
  652. $this->assertEqual($job, $schema['fields']['job']['default'], 'Default field value is set.');
  653. }
  654. }
  655. /**
  656. * Update builder tests.
  657. */
  658. class DatabaseUpdateTestCase extends DatabaseTestCase {
  659. public static function getInfo() {
  660. return array(
  661. 'name' => 'Update tests',
  662. 'description' => 'Test the Update query builder.',
  663. 'group' => 'Database',
  664. );
  665. }
  666. /**
  667. * Confirm that we can update a single record successfully.
  668. */
  669. function testSimpleUpdate() {
  670. $num_updated = db_update('test')
  671. ->fields(array('name' => 'Tiffany'))
  672. ->condition('id', 1)
  673. ->execute();
  674. $this->assertIdentical($num_updated, 1, 'Updated 1 record.');
  675. $saved_name = db_query('SELECT name FROM {test} WHERE id = :id', array(':id' => 1))->fetchField();
  676. $this->assertIdentical($saved_name, 'Tiffany', 'Updated name successfully.');
  677. }
  678. /**
  679. * Confirm updating to NULL.
  680. */
  681. function testSimpleNullUpdate() {
  682. $this->ensureSampleDataNull();
  683. $num_updated = db_update('test_null')
  684. ->fields(array('age' => NULL))
  685. ->condition('name', 'Kermit')
  686. ->execute();
  687. $this->assertIdentical($num_updated, 1, 'Updated 1 record.');
  688. $saved_age = db_query('SELECT age FROM {test_null} WHERE name = :name', array(':name' => 'Kermit'))->fetchField();
  689. $this->assertNull($saved_age, 'Updated name successfully.');
  690. }
  691. /**
  692. * Confirm that we can update a multiple records successfully.
  693. */
  694. function testMultiUpdate() {
  695. $num_updated = db_update('test')
  696. ->fields(array('job' => 'Musician'))
  697. ->condition('job', 'Singer')
  698. ->execute();
  699. $this->assertIdentical($num_updated, 2, 'Updated 2 records.');
  700. $num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', array(':job' => 'Musician'))->fetchField();
  701. $this->assertIdentical($num_matches, '2', 'Updated fields successfully.');
  702. }
  703. /**
  704. * Confirm that we can update a multiple records with a non-equality condition.
  705. */
  706. function testMultiGTUpdate() {
  707. $num_updated = db_update('test')
  708. ->fields(array('job' => 'Musician'))
  709. ->condition('age', 26, '>')
  710. ->execute();
  711. $this->assertIdentical($num_updated, 2, 'Updated 2 records.');
  712. $num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', array(':job' => 'Musician'))->fetchField();
  713. $this->assertIdentical($num_matches, '2', 'Updated fields successfully.');
  714. }
  715. /**
  716. * Confirm that we can update a multiple records with a where call.
  717. */
  718. function testWhereUpdate() {
  719. $num_updated = db_update('test')
  720. ->fields(array('job' => 'Musician'))
  721. ->where('age > :age', array(':age' => 26))
  722. ->execute();
  723. $this->assertIdentical($num_updated, 2, 'Updated 2 records.');
  724. $num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', array(':job' => 'Musician'))->fetchField();
  725. $this->assertIdentical($num_matches, '2', 'Updated fields successfully.');
  726. }
  727. /**
  728. * Confirm that we can stack condition and where calls.
  729. */
  730. function testWhereAndConditionUpdate() {
  731. $update = db_update('test')
  732. ->fields(array('job' => 'Musician'))
  733. ->where('age > :age', array(':age' => 26))
  734. ->condition('name', 'Ringo');
  735. $num_updated = $update->execute();
  736. $this->assertIdentical($num_updated, 1, 'Updated 1 record.');
  737. $num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', array(':job' => 'Musician'))->fetchField();
  738. $this->assertIdentical($num_matches, '1', 'Updated fields successfully.');
  739. }
  740. /**
  741. * Test updating with expressions.
  742. */
  743. function testExpressionUpdate() {
  744. // Set age = 1 for a single row for this test to work.
  745. db_update('test')
  746. ->condition('id', 1)
  747. ->fields(array('age' => 1))
  748. ->execute();
  749. // Ensure that expressions are handled properly. This should set every
  750. // record's age to a square of itself, which will change only three of the
  751. // four records in the table since 1*1 = 1. That means only three records
  752. // are modified, so we should get back 3, not 4, from execute().
  753. $num_rows = db_update('test')
  754. ->expression('age', 'age * age')
  755. ->execute();
  756. $this->assertIdentical($num_rows, 3, 'Number of affected rows are returned.');
  757. }
  758. /**
  759. * Confirm that we can update the primary key of a record successfully.
  760. */
  761. function testPrimaryKeyUpdate() {
  762. $num_updated = db_update('test')
  763. ->fields(array('id' => 42, 'name' => 'John'))
  764. ->condition('id', 1)
  765. ->execute();
  766. $this->assertIdentical($num_updated, 1, 'Updated 1 record.');
  767. $saved_name= db_query('SELECT name FROM {test} WHERE id = :id', array(':id' => 42))->fetchField();
  768. $this->assertIdentical($saved_name, 'John', 'Updated primary key successfully.');
  769. }
  770. }
  771. /**
  772. * Tests for more complex update statements.
  773. */
  774. class DatabaseUpdateComplexTestCase extends DatabaseTestCase {
  775. public static function getInfo() {
  776. return array(
  777. 'name' => 'Update tests, Complex',
  778. 'description' => 'Test the Update query builder, complex queries.',
  779. 'group' => 'Database',
  780. );
  781. }
  782. /**
  783. * Test updates with OR conditionals.
  784. */
  785. function testOrConditionUpdate() {
  786. $update = db_update('test')
  787. ->fields(array('job' => 'Musician'))
  788. ->condition(db_or()
  789. ->condition('name', 'John')
  790. ->condition('name', 'Paul')
  791. );
  792. $num_updated = $update->execute();
  793. $this->assertIdentical($num_updated, 2, 'Updated 2 records.');
  794. $num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', array(':job' => 'Musician'))->fetchField();
  795. $this->assertIdentical($num_matches, '2', 'Updated fields successfully.');
  796. }
  797. /**
  798. * Test WHERE IN clauses.
  799. */
  800. function testInConditionUpdate() {
  801. $num_updated = db_update('test')
  802. ->fields(array('job' => 'Musician'))
  803. ->condition('name', array('John', 'Paul'), 'IN')
  804. ->execute();
  805. $this->assertIdentical($num_updated, 2, 'Updated 2 records.');
  806. $num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', array(':job' => 'Musician'))->fetchField();
  807. $this->assertIdentical($num_matches, '2', 'Updated fields successfully.');
  808. }
  809. /**
  810. * Test WHERE NOT IN clauses.
  811. */
  812. function testNotInConditionUpdate() {
  813. // The o is lowercase in the 'NoT IN' operator, to make sure the operators
  814. // work in mixed case.
  815. $num_updated = db_update('test')
  816. ->fields(array('job' => 'Musician'))
  817. ->condition('name', array('John', 'Paul', 'George'), 'NoT IN')
  818. ->execute();
  819. $this->assertIdentical($num_updated, 1, 'Updated 1 record.');
  820. $num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', array(':job' => 'Musician'))->fetchField();
  821. $this->assertIdentical($num_matches, '1', 'Updated fields successfully.');
  822. }
  823. /**
  824. * Test BETWEEN conditional clauses.
  825. */
  826. function testBetweenConditionUpdate() {
  827. $num_updated = db_update('test')
  828. ->fields(array('job' => 'Musician'))
  829. ->condition('age', array(25, 26), 'BETWEEN')
  830. ->execute();
  831. $this->assertIdentical($num_updated, 2, 'Updated 2 records.');
  832. $num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', array(':job' => 'Musician'))->fetchField();
  833. $this->assertIdentical($num_matches, '2', 'Updated fields successfully.');
  834. }
  835. /**
  836. * Test LIKE conditionals.
  837. */
  838. function testLikeConditionUpdate() {
  839. $num_updated = db_update('test')
  840. ->fields(array('job' => 'Musician'))
  841. ->condition('name', '%ge%', 'LIKE')
  842. ->execute();
  843. $this->assertIdentical($num_updated, 1, 'Updated 1 record.');
  844. $num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', array(':job' => 'Musician'))->fetchField();
  845. $this->assertIdentical($num_matches, '1', 'Updated fields successfully.');
  846. }
  847. /**
  848. * Test update with expression values.
  849. */
  850. function testUpdateExpression() {
  851. $before_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Ringo'))->fetchField();
  852. $GLOBALS['larry_test'] = 1;
  853. $num_updated = db_update('test')
  854. ->condition('name', 'Ringo')
  855. ->fields(array('job' => 'Musician'))
  856. ->expression('age', 'age + :age', array(':age' => 4))
  857. ->execute();
  858. $this->assertIdentical($num_updated, 1, 'Updated 1 record.');
  859. $num_matches = db_query('SELECT COUNT(*) FROM {test} WHERE job = :job', array(':job' => 'Musician'))->fetchField();
  860. $this->assertIdentical($num_matches, '1', 'Updated fields successfully.');
  861. $person = db_query('SELECT * FROM {test} WHERE name = :name', array(':name' => 'Ringo'))->fetch();
  862. $this->assertEqual($person->name, 'Ringo', 'Name set correctly.');
  863. $this->assertEqual($person->age, $before_age + 4, 'Age set correctly.');
  864. $this->assertEqual($person->job, 'Musician', 'Job set correctly.');
  865. $GLOBALS['larry_test'] = 0;
  866. }
  867. /**
  868. * Test update with only expression values.
  869. */
  870. function testUpdateOnlyExpression() {
  871. $before_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Ringo'))->fetchField();
  872. $num_updated = db_update('test')
  873. ->condition('name', 'Ringo')
  874. ->expression('age', 'age + :age', array(':age' => 4))
  875. ->execute();
  876. $this->assertIdentical($num_updated, 1, 'Updated 1 record.');
  877. $after_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Ringo'))->fetchField();
  878. $this->assertEqual($before_age + 4, $after_age, 'Age updated correctly');
  879. }
  880. }
  881. /**
  882. * Test update queries involving LOB values.
  883. */
  884. class DatabaseUpdateLOBTestCase extends DatabaseTestCase {
  885. public static function getInfo() {
  886. return array(
  887. 'name' => 'Update tests, LOB',
  888. 'description' => 'Test the Update query builder with LOB fields.',
  889. 'group' => 'Database',
  890. );
  891. }
  892. /**
  893. * Confirm that we can update a blob column.
  894. */
  895. function testUpdateOneBlob() {
  896. $data = "This is\000a test.";
  897. $this->assertTrue(strlen($data) === 15, 'Test data contains a NULL.');
  898. $id = db_insert('test_one_blob')
  899. ->fields(array('blob1' => $data))
  900. ->execute();
  901. $data .= $data;
  902. db_update('test_one_blob')
  903. ->condition('id', $id)
  904. ->fields(array('blob1' => $data))
  905. ->execute();
  906. $r = db_query('SELECT * FROM {test_one_blob} WHERE id = :id', array(':id' => $id))->fetchAssoc();
  907. $this->assertTrue($r['blob1'] === $data, format_string('Can update a blob: id @id, @data.', array('@id' => $id, '@data' => serialize($r))));
  908. }
  909. /**
  910. * Confirm that we can update two blob columns in the same table.
  911. */
  912. function testUpdateMultipleBlob() {
  913. $id = db_insert('test_two_blobs')
  914. ->fields(array(
  915. 'blob1' => 'This is',
  916. 'blob2' => 'a test',
  917. ))
  918. ->execute();
  919. db_update('test_two_blobs')
  920. ->condition('id', $id)
  921. ->fields(array('blob1' => 'and so', 'blob2' => 'is this'))
  922. ->execute();
  923. $r = db_query('SELECT * FROM {test_two_blobs} WHERE id = :id', array(':id' => $id))->fetchAssoc();
  924. $this->assertTrue($r['blob1'] === 'and so' && $r['blob2'] === 'is this', 'Can update multiple blobs per row.');
  925. }
  926. }
  927. /**
  928. * Delete/Truncate tests.
  929. *
  930. * The DELETE tests are not as extensive, as all of the interesting code for
  931. * DELETE queries is in the conditional which is identical to the UPDATE and
  932. * SELECT conditional handling.
  933. *
  934. * The TRUNCATE tests are not extensive either, because the behavior of
  935. * TRUNCATE queries is not consistent across database engines. We only test
  936. * that a TRUNCATE query actually deletes all rows from the target table.
  937. */
  938. class DatabaseDeleteTruncateTestCase extends DatabaseTestCase {
  939. public static function getInfo() {
  940. return array(
  941. 'name' => 'Delete/Truncate tests',
  942. 'description' => 'Test the Delete and Truncate query builders.',
  943. 'group' => 'Database',
  944. );
  945. }
  946. /**
  947. * Confirm that we can use a subselect in a delete successfully.
  948. */
  949. function testSubselectDelete() {
  950. $num_records_before = db_query('SELECT COUNT(*) FROM {test_task}')->fetchField();
  951. $pid_to_delete = db_query("SELECT * FROM {test_task} WHERE task = 'sleep'")->fetchField();
  952. $subquery = db_select('test', 't')
  953. ->fields('t', array('id'))
  954. ->condition('t.id', array($pid_to_delete), 'IN');
  955. $delete = db_delete('test_task')
  956. ->condition('task', 'sleep')
  957. ->condition('pid', $subquery, 'IN');
  958. $num_deleted = $delete->execute();
  959. $this->assertEqual($num_deleted, 1, "Deleted 1 record.");
  960. $num_records_after = db_query('SELECT COUNT(*) FROM {test_task}')->fetchField();
  961. $this->assertEqual($num_records_before, $num_records_after + $num_deleted, 'Deletion adds up.');
  962. }
  963. /**
  964. * Confirm that we can delete a single record successfully.
  965. */
  966. function testSimpleDelete() {
  967. $num_records_before = db_query('SELECT COUNT(*) FROM {test}')->fetchField();
  968. $num_deleted = db_delete('test')
  969. ->condition('id', 1)
  970. ->execute();
  971. $this->assertIdentical($num_deleted, 1, 'Deleted 1 record.');
  972. $num_records_after = db_query('SELECT COUNT(*) FROM {test}')->fetchField();
  973. $this->assertEqual($num_records_before, $num_records_after + $num_deleted, 'Deletion adds up.');
  974. }
  975. /**
  976. * Confirm that we can truncate a whole table successfully.
  977. */
  978. function testTruncate() {
  979. $num_records_before = db_query("SELECT COUNT(*) FROM {test}")->fetchField();
  980. db_truncate('test')->execute();
  981. $num_records_after = db_query("SELECT COUNT(*) FROM {test}")->fetchField();
  982. $this->assertEqual(0, $num_records_after, 'Truncate really deletes everything.');
  983. }
  984. }
  985. /**
  986. * Test the MERGE query builder.
  987. */
  988. class DatabaseMergeTestCase extends DatabaseTestCase {
  989. public static function getInfo() {
  990. return array(
  991. 'name' => 'Merge tests',
  992. 'description' => 'Test the Merge query builder.',
  993. 'group' => 'Database',
  994. );
  995. }
  996. /**
  997. * Confirm that we can merge-insert a record successfully.
  998. */
  999. function testMergeInsert() {
  1000. $num_records_before = db_query('SELECT COUNT(*) FROM {test_people}')->fetchField();
  1001. $result = db_merge('test_people')
  1002. ->key(array('job' => 'Presenter'))
  1003. ->fields(array(
  1004. 'age' => 31,
  1005. 'name' => 'Tiffany',
  1006. ))
  1007. ->execute();
  1008. $this->assertEqual($result, MergeQuery::STATUS_INSERT, 'Insert status returned.');
  1009. $num_records_after = db_query('SELECT COUNT(*) FROM {test_people}')->fetchField();
  1010. $this->assertEqual($num_records_before + 1, $num_records_after, 'Merge inserted properly.');
  1011. $person = db_query('SELECT * FROM {test_people} WHERE job = :job', array(':job' => 'Presenter'))->fetch();
  1012. $this->assertEqual($person->name, 'Tiffany', 'Name set correctly.');
  1013. $this->assertEqual($person->age, 31, 'Age set correctly.');
  1014. $this->assertEqual($person->job, 'Presenter', 'Job set correctly.');
  1015. }
  1016. /**
  1017. * Confirm that we can merge-update a record successfully.
  1018. */
  1019. function testMergeUpdate() {
  1020. $num_records_before = db_query('SELECT COUNT(*) FROM {test_people}')->fetchField();
  1021. $result = db_merge('test_people')
  1022. ->key(array('job' => 'Speaker'))
  1023. ->fields(array(
  1024. 'age' => 31,
  1025. 'name' => 'Tiffany',
  1026. ))
  1027. ->execute();
  1028. $this->assertEqual($result, MergeQuery::STATUS_UPDATE, 'Update status returned.');
  1029. $num_records_after = db_query('SELECT COUNT(*) FROM {test_people}')->fetchField();
  1030. $this->assertEqual($num_records_before, $num_records_after, 'Merge updated properly.');
  1031. $person = db_query('SELECT * FROM {test_people} WHERE job = :job', array(':job' => 'Speaker'))->fetch();
  1032. $this->assertEqual($person->name, 'Tiffany', 'Name set correctly.');
  1033. $this->assertEqual($person->age, 31, 'Age set correctly.');
  1034. $this->assertEqual($person->job, 'Speaker', 'Job set correctly.');
  1035. }
  1036. /**
  1037. * Confirm that we can merge-update a record successfully, with different insert and update.
  1038. */
  1039. function testMergeUpdateExcept() {
  1040. $num_records_before = db_query('SELECT COUNT(*) FROM {test_people}')->fetchField();
  1041. db_merge('test_people')
  1042. ->key(array('job' => 'Speaker'))
  1043. ->insertFields(array('age' => 31))
  1044. ->updateFields(array('name' => 'Tiffany'))
  1045. ->execute();
  1046. $num_records_after = db_query('SELECT COUNT(*) FROM {test_people}')->fetchField();
  1047. $this->assertEqual($num_records_before, $num_records_after, 'Merge updated properly.');
  1048. $person = db_query('SELECT * FROM {test_people} WHERE job = :job', array(':job' => 'Speaker'))->fetch();
  1049. $this->assertEqual($person->name, 'Tiffany', 'Name set correctly.');
  1050. $this->assertEqual($person->age, 30, 'Age skipped correctly.');
  1051. $this->assertEqual($person->job, 'Speaker', 'Job set correctly.');
  1052. }
  1053. /**
  1054. * Confirm that we can merge-update a record successfully, with alternate replacement.
  1055. */
  1056. function testMergeUpdateExplicit() {
  1057. $num_records_before = db_query('SELECT COUNT(*) FROM {test_people}')->fetchField();
  1058. db_merge('test_people')
  1059. ->key(array('job' => 'Speaker'))
  1060. ->insertFields(array(
  1061. 'age' => 31,
  1062. 'name' => 'Tiffany',
  1063. ))
  1064. ->updateFields(array(
  1065. 'name' => 'Joe',
  1066. ))
  1067. ->execute();
  1068. $num_records_after = db_query('SELECT COUNT(*) FROM {test_people}')->fetchField();
  1069. $this->assertEqual($num_records_before, $num_records_after, 'Merge updated properly.');
  1070. $person = db_query('SELECT * FROM {test_people} WHERE job = :job', array(':job' => 'Speaker'))->fetch();
  1071. $this->assertEqual($person->name, 'Joe', 'Name set correctly.');
  1072. $this->assertEqual($person->age, 30, 'Age skipped correctly.');
  1073. $this->assertEqual($person->job, 'Speaker', 'Job set correctly.');
  1074. }
  1075. /**
  1076. * Confirm that we can merge-update a record successfully, with expressions.
  1077. */
  1078. function testMergeUpdateExpression() {
  1079. $num_records_before = db_query('SELECT COUNT(*) FROM {test_people}')->fetchField();
  1080. $age_before = db_query('SELECT age FROM {test_people} WHERE job = :job', array(':job' => 'Speaker'))->fetchField();
  1081. // This is a very contrived example, as I have no idea why you'd want to
  1082. // change age this way, but that's beside the point.
  1083. // Note that we are also double-setting age here, once as a literal and
  1084. // once as an expression. This test will only pass if the expression wins,
  1085. // which is what is supposed to happen.
  1086. db_merge('test_people')
  1087. ->key(array('job' => 'Speaker'))
  1088. ->fields(array('name' => 'Tiffany'))
  1089. ->insertFields(array('age' => 31))
  1090. ->expression('age', 'age + :age', array(':age' => 4))
  1091. ->execute();
  1092. $num_records_after = db_query('SELECT COUNT(*) FROM {test_people}')->fetchField();
  1093. $this->assertEqual($num_records_before, $num_records_after, 'Merge updated properly.');
  1094. $person = db_query('SELECT * FROM {test_people} WHERE job = :job', array(':job' => 'Speaker'))->fetch();
  1095. $this->assertEqual($person->name, 'Tiffany', 'Name set correctly.');
  1096. $this->assertEqual($person->age, $age_before + 4, 'Age updated correctly.');
  1097. $this->assertEqual($person->job, 'Speaker', 'Job set correctly.');
  1098. }
  1099. /**
  1100. * Test that we can merge-insert without any update fields.
  1101. */
  1102. function testMergeInsertWithoutUpdate() {
  1103. $num_records_before = db_query('SELECT COUNT(*) FROM {test_people}')->fetchField();
  1104. db_merge('test_people')
  1105. ->key(array('job' => 'Presenter'))
  1106. ->execute();
  1107. $num_records_after = db_query('SELECT COUNT(*) FROM {test_people}')->fetchField();
  1108. $this->assertEqual($num_records_before + 1, $num_records_after, 'Merge inserted properly.');
  1109. $person = db_query('SELECT * FROM {test_people} WHERE job = :job', array(':job' => 'Presenter'))->fetch();
  1110. $this->assertEqual($person->name, '', 'Name set correctly.');
  1111. $this->assertEqual($person->age, 0, 'Age set correctly.');
  1112. $this->assertEqual($person->job, 'Presenter', 'Job set correctly.');
  1113. }
  1114. /**
  1115. * Confirm that we can merge-update without any update fields.
  1116. */
  1117. function testMergeUpdateWithoutUpdate() {
  1118. $num_records_before = db_query('SELECT COUNT(*) FROM {test_people}')->fetchField();
  1119. db_merge('test_people')
  1120. ->key(array('job' => 'Speaker'))
  1121. ->execute();
  1122. $num_records_after = db_query('SELECT COUNT(*) FROM {test_people}')->fetchField();
  1123. $this->assertEqual($num_records_before, $num_records_after, 'Merge skipped properly.');
  1124. $person = db_query('SELECT * FROM {test_people} WHERE job = :job', array(':job' => 'Speaker'))->fetch();
  1125. $this->assertEqual($person->name, 'Meredith', 'Name skipped correctly.');
  1126. $this->assertEqual($person->age, 30, 'Age skipped correctly.');
  1127. $this->assertEqual($person->job, 'Speaker', 'Job skipped correctly.');
  1128. db_merge('test_people')
  1129. ->key(array('job' => 'Speaker'))
  1130. ->insertFields(array('age' => 31))
  1131. ->execute();
  1132. $num_records_after = db_query('SELECT COUNT(*) FROM {test_people}')->fetchField();
  1133. $this->assertEqual($num_records_before, $num_records_after, 'Merge skipped properly.');
  1134. $person = db_query('SELECT * FROM {test_people} WHERE job = :job', array(':job' => 'Speaker'))->fetch();
  1135. $this->assertEqual($person->name, 'Meredith', 'Name skipped correctly.');
  1136. $this->assertEqual($person->age, 30, 'Age skipped correctly.');
  1137. $this->assertEqual($person->job, 'Speaker', 'Job skipped correctly.');
  1138. }
  1139. /**
  1140. * Test that an invalid merge query throws an exception like it is supposed to.
  1141. */
  1142. function testInvalidMerge() {
  1143. try {
  1144. // This query should die because there is no key field specified.
  1145. db_merge('test_people')
  1146. ->fields(array(
  1147. 'age' => 31,
  1148. 'name' => 'Tiffany',
  1149. ))
  1150. ->execute();
  1151. }
  1152. catch (InvalidMergeQueryException $e) {
  1153. $this->pass('InvalidMergeQueryException thrown for invalid query.');
  1154. return;
  1155. }
  1156. $this->fail('No InvalidMergeQueryException thrown');
  1157. }
  1158. }
  1159. /**
  1160. * Test the SELECT builder.
  1161. */
  1162. class DatabaseSelectTestCase extends DatabaseTestCase {
  1163. public static function getInfo() {
  1164. return array(
  1165. 'name' => 'Select tests',
  1166. 'description' => 'Test the Select query builder.',
  1167. 'group' => 'Database',
  1168. );
  1169. }
  1170. /**
  1171. * Test rudimentary SELECT statements.
  1172. */
  1173. function testSimpleSelect() {
  1174. $query = db_select('test');
  1175. $name_field = $query->addField('test', 'name');
  1176. $age_field = $query->addField('test', 'age', 'age');
  1177. $result = $query->execute();
  1178. $num_records = 0;
  1179. foreach ($result as $record) {
  1180. $num_records++;
  1181. }
  1182. $this->assertEqual($num_records, 4, 'Returned the correct number of rows.');
  1183. }
  1184. /**
  1185. * Test rudimentary SELECT statement with a COMMENT.
  1186. */
  1187. function testSimpleComment() {
  1188. $query = db_select('test')->comment('Testing query comments');
  1189. $name_field = $query->addField('test', 'name');
  1190. $age_field = $query->addField('test', 'age', 'age');
  1191. $result = $query->execute();
  1192. $num_records = 0;
  1193. foreach ($result as $record) {
  1194. $num_records++;
  1195. }
  1196. $query = (string)$query;
  1197. $expected = "/* Testing query comments */ SELECT test.name AS name, test.age AS age\nFROM \n{test} test";
  1198. $this->assertEqual($num_records, 4, 'Returned the correct number of rows.');
  1199. $this->assertEqual($query, $expected, 'The flattened query contains the comment string.');
  1200. }
  1201. /**
  1202. * Test query COMMENT system against vulnerabilities.
  1203. */
  1204. function testVulnerableComment() {
  1205. $query = db_select('test')->comment('Testing query comments */ SELECT nid FROM {node}; --');
  1206. $name_field = $query->addField('test', 'name');
  1207. $age_field = $query->addField('test', 'age', 'age');
  1208. $result = $query->execute();
  1209. $num_records = 0;
  1210. foreach ($result as $record) {
  1211. $num_records++;
  1212. }
  1213. $query = (string)$query;
  1214. $expected = "/* Testing query comments * / SELECT nid FROM {node}; -- */ SELECT test.name AS name, test.age AS age\nFROM \n{test} test";
  1215. $this->assertEqual($num_records, 4, 'Returned the correct number of rows.');
  1216. $this->assertEqual($query, $expected, 'The flattened query contains the sanitised comment string.');
  1217. $connection = Database::getConnection();
  1218. foreach ($this->makeCommentsProvider() as $test_set) {
  1219. list($expected, $comments) = $test_set;
  1220. $this->assertEqual($expected, $connection->makeComment($comments));
  1221. }
  1222. }
  1223. /**
  1224. * Provides expected and input values for testVulnerableComment().
  1225. */
  1226. function makeCommentsProvider() {
  1227. return array(
  1228. array(
  1229. '/* */ ',
  1230. array(''),
  1231. ),
  1232. // Try and close the comment early.
  1233. array(
  1234. '/* Exploit * / DROP TABLE node; -- */ ',
  1235. array('Exploit */ DROP TABLE node; --'),
  1236. ),
  1237. // Variations on comment closing.
  1238. array(
  1239. '/* Exploit * / * / DROP TABLE node; -- */ ',
  1240. array('Exploit */*/ DROP TABLE node; --'),
  1241. ),
  1242. array(
  1243. '/* Exploit * * // DROP TABLE node; -- */ ',
  1244. array('Exploit **// DROP TABLE node; --'),
  1245. ),
  1246. // Try closing the comment in the second string which is appended.
  1247. array(
  1248. '/* Exploit * / DROP TABLE node; --; Another try * / DROP TABLE node; -- */ ',
  1249. array('Exploit */ DROP TABLE node; --', 'Another try */ DROP TABLE node; --'),
  1250. ),
  1251. );
  1252. }
  1253. /**
  1254. * Test basic conditionals on SELECT statements.
  1255. */
  1256. function testSimpleSelectConditional() {
  1257. $query = db_select('test');
  1258. $name_field = $query->addField('test', 'name');
  1259. $age_field = $query->addField('test', 'age', 'age');
  1260. $query->condition('age', 27);
  1261. $result = $query->execute();
  1262. // Check that the aliases are being created the way we want.
  1263. $this->assertEqual($name_field, 'name', 'Name field alias is correct.');
  1264. $this->assertEqual($age_field, 'age', 'Age field alias is correct.');
  1265. // Ensure that we got the right record.
  1266. $record = $result->fetch();
  1267. $this->assertEqual($record->$name_field, 'George', 'Fetched name is correct.');
  1268. $this->assertEqual($record->$age_field, 27, 'Fetched age is correct.');
  1269. }
  1270. /**
  1271. * Test SELECT statements with expressions.
  1272. */
  1273. function testSimpleSelectExpression() {
  1274. $query = db_select('test');
  1275. $name_field = $query->addField('test', 'name');
  1276. $age_field = $query->addExpression("age*2", 'double_age');
  1277. $query->condition('age', 27);
  1278. $result = $query->execute();
  1279. // Check that the aliases are being created the way we want.
  1280. $this->assertEqual($name_field, 'name', 'Name field alias is correct.');
  1281. $this->assertEqual($age_field, 'double_age', 'Age field alias is correct.');
  1282. // Ensure that we got the right record.
  1283. $record = $result->fetch();
  1284. $this->assertEqual($record->$name_field, 'George', 'Fetched name is correct.');
  1285. $this->assertEqual($record->$age_field, 27*2, 'Fetched age expression is correct.');
  1286. }
  1287. /**
  1288. * Test SELECT statements with multiple expressions.
  1289. */
  1290. function testSimpleSelectExpressionMultiple() {
  1291. $query = db_select('test');
  1292. $name_field = $query->addField('test', 'name');
  1293. $age_double_field = $query->addExpression("age*2");
  1294. $age_triple_field = $query->addExpression("age*3");
  1295. $query->condition('age', 27);
  1296. $result = $query->execute();
  1297. // Check that the aliases are being created the way we want.
  1298. $this->assertEqual($age_double_field, 'expression', 'Double age field alias is correct.');
  1299. $this->assertEqual($age_triple_field, 'expression_2', 'Triple age field alias is correct.');
  1300. // Ensure that we got the right record.
  1301. $record = $result->fetch();
  1302. $this->assertEqual($record->$name_field, 'George', 'Fetched name is correct.');
  1303. $this->assertEqual($record->$age_double_field, 27*2, 'Fetched double age expression is correct.');
  1304. $this->assertEqual($record->$age_triple_field, 27*3, 'Fetched triple age expression is correct.');
  1305. }
  1306. /**
  1307. * Test adding multiple fields to a select statement at the same time.
  1308. */
  1309. function testSimpleSelectMultipleFields() {
  1310. $record = db_select('test')
  1311. ->fields('test', array('id', 'name', 'age', 'job'))
  1312. ->condition('age', 27)
  1313. ->execute()->fetchObject();
  1314. // Check that all fields we asked for are present.
  1315. $this->assertNotNull($record->id, 'ID field is present.');
  1316. $this->assertNotNull($record->name, 'Name field is present.');
  1317. $this->assertNotNull($record->age, 'Age field is present.');
  1318. $this->assertNotNull($record->job, 'Job field is present.');
  1319. // Ensure that we got the right record.
  1320. // Check that all fields we asked for are present.
  1321. $this->assertEqual($record->id, 2, 'ID field has the correct value.');
  1322. $this->assertEqual($record->name, 'George', 'Name field has the correct value.');
  1323. $this->assertEqual($record->age, 27, 'Age field has the correct value.');
  1324. $this->assertEqual($record->job, 'Singer', 'Job field has the correct value.');
  1325. }
  1326. /**
  1327. * Test adding all fields from a given table to a select statement.
  1328. */
  1329. function testSimpleSelectAllFields() {
  1330. $record = db_select('test')
  1331. ->fields('test')
  1332. ->condition('age', 27)
  1333. ->execute()->fetchObject();
  1334. // Check that all fields we asked for are present.
  1335. $this->assertNotNull($record->id, 'ID field is present.');
  1336. $this->assertNotNull($record->name, 'Name field is present.');
  1337. $this->assertNotNull($record->age, 'Age field is present.');
  1338. $this->assertNotNull($record->job, 'Job field is present.');
  1339. // Ensure that we got the right record.
  1340. // Check that all fields we asked for are present.
  1341. $this->assertEqual($record->id, 2, 'ID field has the correct value.');
  1342. $this->assertEqual($record->name, 'George', 'Name field has the correct value.');
  1343. $this->assertEqual($record->age, 27, 'Age field has the correct value.');
  1344. $this->assertEqual($record->job, 'Singer', 'Job field has the correct value.');
  1345. }
  1346. /**
  1347. * Test that we can find a record with a NULL value.
  1348. */
  1349. function testNullCondition() {
  1350. $this->ensureSampleDataNull();
  1351. $names = db_select('test_null', 'tn')
  1352. ->fields('tn', array('name'))
  1353. ->isNull('age')
  1354. ->execute()->fetchCol();
  1355. $this->assertEqual(count($names), 1, 'Correct number of records found with NULL age.');
  1356. $this->assertEqual($names[0], 'Fozzie', 'Correct record returned for NULL age.');
  1357. }
  1358. /**
  1359. * Test that we can find a record without a NULL value.
  1360. */
  1361. function testNotNullCondition() {
  1362. $this->ensureSampleDataNull();
  1363. $names = db_select('test_null', 'tn')
  1364. ->fields('tn', array('name'))
  1365. ->isNotNull('tn.age')
  1366. ->orderBy('name')
  1367. ->execute()->fetchCol();
  1368. $this->assertEqual(count($names), 2, 'Correct number of records found withNOT NULL age.');
  1369. $this->assertEqual($names[0], 'Gonzo', 'Correct record returned for NOT NULL age.');
  1370. $this->assertEqual($names[1], 'Kermit', 'Correct record returned for NOT NULL age.');
  1371. }
  1372. /**
  1373. * Test that we can UNION multiple Select queries together. This is
  1374. * semantically equal to UNION DISTINCT, so we don't explicity test that.
  1375. */
  1376. function testUnion() {
  1377. $query_1 = db_select('test', 't')
  1378. ->fields('t', array('name'))
  1379. ->condition('age', array(27, 28), 'IN');
  1380. $query_2 = db_select('test', 't')
  1381. ->fields('t', array('name'))
  1382. ->condition('age', 28);
  1383. $query_1->union($query_2);
  1384. $names = $query_1->execute()->fetchCol();
  1385. // Ensure we only get 2 records.
  1386. $this->assertEqual(count($names), 2, 'UNION correctly discarded duplicates.');
  1387. $this->assertEqual($names[0], 'George', 'First query returned correct name.');
  1388. $this->assertEqual($names[1], 'Ringo', 'Second query returned correct name.');
  1389. }
  1390. /**
  1391. * Test that we can UNION ALL multiple Select queries together.
  1392. */
  1393. function testUnionAll() {
  1394. $query_1 = db_select('test', 't')
  1395. ->fields('t', array('name'))
  1396. ->condition('age', array(27, 28), 'IN');
  1397. $query_2 = db_select('test', 't')
  1398. ->fields('t', array('name'))
  1399. ->condition('age', 28);
  1400. $query_1->union($query_2, 'ALL');
  1401. $names = $query_1->execute()->fetchCol();
  1402. // Ensure we get all 3 records.
  1403. $this->assertEqual(count($names), 3, 'UNION ALL correctly preserved duplicates.');
  1404. $this->assertEqual($names[0], 'George', 'First query returned correct first name.');
  1405. $this->assertEqual($names[1], 'Ringo', 'Second query returned correct second name.');
  1406. $this->assertEqual($names[2], 'Ringo', 'Third query returned correct name.');
  1407. }
  1408. /**
  1409. * Test that random ordering of queries works.
  1410. *
  1411. * We take the approach of testing the Drupal layer only, rather than trying
  1412. * to test that the database's random number generator actually produces
  1413. * random queries (which is very difficult to do without an unacceptable risk
  1414. * of the test failing by accident).
  1415. *
  1416. * Therefore, in this test we simply run the same query twice and assert that
  1417. * the two results are reordered versions of each other (as well as of the
  1418. * same query without the random ordering). It is reasonable to assume that
  1419. * if we run the same select query twice and the results are in a different
  1420. * order each time, the only way this could happen is if we have successfully
  1421. * triggered the database's random ordering functionality.
  1422. */
  1423. function testRandomOrder() {
  1424. // Use 52 items, so the chance that this test fails by accident will be the
  1425. // same as the chance that a deck of cards will come out in the same order
  1426. // after shuffling it (in other words, nearly impossible).
  1427. $number_of_items = 52;
  1428. while (db_query("SELECT MAX(id) FROM {test}")->fetchField() < $number_of_items) {
  1429. db_insert('test')->fields(array('name' => $this->randomName()))->execute();
  1430. }
  1431. // First select the items in order and make sure we get an ordered list.
  1432. $expected_ids = range(1, $number_of_items);
  1433. $ordered_ids = db_select('test', 't')
  1434. ->fields('t', array('id'))
  1435. ->range(0, $number_of_items)
  1436. ->orderBy('id')
  1437. ->execute()
  1438. ->fetchCol();
  1439. $this->assertEqual($ordered_ids, $expected_ids, 'A query without random ordering returns IDs in the correct order.');
  1440. // Now perform the same query, but instead choose a random ordering. We
  1441. // expect this to contain a differently ordered version of the original
  1442. // result.
  1443. $randomized_ids = db_select('test', 't')
  1444. ->fields('t', array('id'))
  1445. ->range(0, $number_of_items)
  1446. ->orderRandom()
  1447. ->execute()
  1448. ->fetchCol();
  1449. $this->assertNotEqual($randomized_ids, $ordered_ids, 'A query with random ordering returns an unordered set of IDs.');
  1450. $sorted_ids = $randomized_ids;
  1451. sort($sorted_ids);
  1452. $this->assertEqual($sorted_ids, $ordered_ids, 'After sorting the random list, the result matches the original query.');
  1453. // Now perform the exact same query again, and make sure the order is
  1454. // different.
  1455. $randomized_ids_second_set = db_select('test', 't')
  1456. ->fields('t', array('id'))
  1457. ->range(0, $number_of_items)
  1458. ->orderRandom()
  1459. ->execute()
  1460. ->fetchCol();
  1461. $this->assertNotEqual($randomized_ids_second_set, $randomized_ids, 'Performing the query with random ordering a second time returns IDs in a different order.');
  1462. $sorted_ids_second_set = $randomized_ids_second_set;
  1463. sort($sorted_ids_second_set);
  1464. $this->assertEqual($sorted_ids_second_set, $sorted_ids, 'After sorting the second random list, the result matches the sorted version of the first random list.');
  1465. }
  1466. /**
  1467. * Test that aliases are renamed when duplicates.
  1468. */
  1469. function testSelectDuplicateAlias() {
  1470. $query = db_select('test', 't');
  1471. $alias1 = $query->addField('t', 'name', 'the_alias');
  1472. $alias2 = $query->addField('t', 'age', 'the_alias');
  1473. $this->assertNotIdentical($alias1, $alias2, 'Duplicate aliases are renamed.');
  1474. }
  1475. }
  1476. /**
  1477. * Test case for subselects in a dynamic SELECT query.
  1478. */
  1479. class DatabaseSelectSubqueryTestCase extends DatabaseTestCase {
  1480. public static function getInfo() {
  1481. return array(
  1482. 'name' => 'Select tests, subqueries',
  1483. 'description' => 'Test the Select query builder.',
  1484. 'group' => 'Database',
  1485. );
  1486. }
  1487. /**
  1488. * Test that we can use a subquery in a FROM clause.
  1489. */
  1490. function testFromSubquerySelect() {
  1491. // Create a subquery, which is just a normal query object.
  1492. $subquery = db_select('test_task', 'tt');
  1493. $subquery->addField('tt', 'pid', 'pid');
  1494. $subquery->addField('tt', 'task', 'task');
  1495. $subquery->condition('priority', 1);
  1496. for ($i = 0; $i < 2; $i++) {
  1497. // Create another query that joins against the virtual table resulting
  1498. // from the subquery.
  1499. $select = db_select($subquery, 'tt2');
  1500. $select->join('test', 't', 't.id=tt2.pid');
  1501. $select->addField('t', 'name');
  1502. if ($i) {
  1503. // Use a different number of conditions here to confuse the subquery
  1504. // placeholder counter, testing http://drupal.org/node/1112854.
  1505. $select->condition('name', 'John');
  1506. }
  1507. $select->condition('task', 'code');
  1508. // The resulting query should be equivalent to:
  1509. // SELECT t.name
  1510. // FROM (SELECT tt.pid AS pid, tt.task AS task FROM test_task tt WHERE priority=1) tt
  1511. // INNER JOIN test t ON t.id=tt.pid
  1512. // WHERE tt.task = 'code'
  1513. $people = $select->execute()->fetchCol();
  1514. $this->assertEqual(count($people), 1, 'Returned the correct number of rows.');
  1515. }
  1516. }
  1517. /**
  1518. * Test that we can use a subquery in a FROM clause with a limit.
  1519. */
  1520. function testFromSubquerySelectWithLimit() {
  1521. // Create a subquery, which is just a normal query object.
  1522. $subquery = db_select('test_task', 'tt');
  1523. $subquery->addField('tt', 'pid', 'pid');
  1524. $subquery->addField('tt', 'task', 'task');
  1525. $subquery->orderBy('priority', 'DESC');
  1526. $subquery->range(0, 1);
  1527. // Create another query that joins against the virtual table resulting
  1528. // from the subquery.
  1529. $select = db_select($subquery, 'tt2');
  1530. $select->join('test', 't', 't.id=tt2.pid');
  1531. $select->addField('t', 'name');
  1532. // The resulting query should be equivalent to:
  1533. // SELECT t.name
  1534. // FROM (SELECT tt.pid AS pid, tt.task AS task FROM test_task tt ORDER BY priority DESC LIMIT 1 OFFSET 0) tt
  1535. // INNER JOIN test t ON t.id=tt.pid
  1536. $people = $select->execute()->fetchCol();
  1537. $this->assertEqual(count($people), 1, 'Returned the correct number of rows.');
  1538. }
  1539. /**
  1540. * Test that we can use a subquery in a WHERE clause.
  1541. */
  1542. function testConditionSubquerySelect() {
  1543. // Create a subquery, which is just a normal query object.
  1544. $subquery = db_select('test_task', 'tt');
  1545. $subquery->addField('tt', 'pid', 'pid');
  1546. $subquery->condition('tt.priority', 1);
  1547. // Create another query that joins against the virtual table resulting
  1548. // from the subquery.
  1549. $select = db_select('test_task', 'tt2');
  1550. $select->addField('tt2', 'task');
  1551. $select->condition('tt2.pid', $subquery, 'IN');
  1552. // The resulting query should be equivalent to:
  1553. // SELECT tt2.name
  1554. // FROM test tt2
  1555. // WHERE tt2.pid IN (SELECT tt.pid AS pid FROM test_task tt WHERE tt.priority=1)
  1556. $people = $select->execute()->fetchCol();
  1557. $this->assertEqual(count($people), 5, 'Returned the correct number of rows.');
  1558. }
  1559. /**
  1560. * Test that we can use a subquery in a JOIN clause.
  1561. */
  1562. function testJoinSubquerySelect() {
  1563. // Create a subquery, which is just a normal query object.
  1564. $subquery = db_select('test_task', 'tt');
  1565. $subquery->addField('tt', 'pid', 'pid');
  1566. $subquery->condition('priority', 1);
  1567. // Create another query that joins against the virtual table resulting
  1568. // from the subquery.
  1569. $select = db_select('test', 't');
  1570. $select->join($subquery, 'tt', 't.id=tt.pid');
  1571. $select->addField('t', 'name');
  1572. // The resulting query should be equivalent to:
  1573. // SELECT t.name
  1574. // FROM test t
  1575. // INNER JOIN (SELECT tt.pid AS pid FROM test_task tt WHERE priority=1) tt ON t.id=tt.pid
  1576. $people = $select->execute()->fetchCol();
  1577. $this->assertEqual(count($people), 2, 'Returned the correct number of rows.');
  1578. }
  1579. /**
  1580. * Test EXISTS subquery conditionals on SELECT statements.
  1581. *
  1582. * We essentially select all rows from the {test} table that have matching
  1583. * rows in the {test_people} table based on the shared name column.
  1584. */
  1585. function testExistsSubquerySelect() {
  1586. // Put George into {test_people}.
  1587. db_insert('test_people')
  1588. ->fields(array(
  1589. 'name' => 'George',
  1590. 'age' => 27,
  1591. 'job' => 'Singer',
  1592. ))
  1593. ->execute();
  1594. // Base query to {test}.
  1595. $query = db_select('test', 't')
  1596. ->fields('t', array('name'));
  1597. // Subquery to {test_people}.
  1598. $subquery = db_select('test_people', 'tp')
  1599. ->fields('tp', array('name'))
  1600. ->where('tp.name = t.name');
  1601. $query->exists($subquery);
  1602. $result = $query->execute();
  1603. // Ensure that we got the right record.
  1604. $record = $result->fetch();
  1605. $this->assertEqual($record->name, 'George', 'Fetched name is correct using EXISTS query.');
  1606. }
  1607. /**
  1608. * Test NOT EXISTS subquery conditionals on SELECT statements.
  1609. *
  1610. * We essentially select all rows from the {test} table that don't have
  1611. * matching rows in the {test_people} table based on the shared name column.
  1612. */
  1613. function testNotExistsSubquerySelect() {
  1614. // Put George into {test_people}.
  1615. db_insert('test_people')
  1616. ->fields(array(
  1617. 'name' => 'George',
  1618. 'age' => 27,
  1619. 'job' => 'Singer',
  1620. ))
  1621. ->execute();
  1622. // Base query to {test}.
  1623. $query = db_select('test', 't')
  1624. ->fields('t', array('name'));
  1625. // Subquery to {test_people}.
  1626. $subquery = db_select('test_people', 'tp')
  1627. ->fields('tp', array('name'))
  1628. ->where('tp.name = t.name');
  1629. $query->notExists($subquery);
  1630. // Ensure that we got the right number of records.
  1631. $people = $query->execute()->fetchCol();
  1632. $this->assertEqual(count($people), 3, 'NOT EXISTS query returned the correct results.');
  1633. }
  1634. }
  1635. /**
  1636. * Test select with order by clauses.
  1637. */
  1638. class DatabaseSelectOrderedTestCase extends DatabaseTestCase {
  1639. public static function getInfo() {
  1640. return array(
  1641. 'name' => 'Select tests, ordered',
  1642. 'description' => 'Test the Select query builder.',
  1643. 'group' => 'Database',
  1644. );
  1645. }
  1646. /**
  1647. * Test basic order by.
  1648. */
  1649. function testSimpleSelectOrdered() {
  1650. $query = db_select('test');
  1651. $name_field = $query->addField('test', 'name');
  1652. $age_field = $query->addField('test', 'age', 'age');
  1653. $query->orderBy($age_field);
  1654. $result = $query->execute();
  1655. $num_records = 0;
  1656. $last_age = 0;
  1657. foreach ($result as $record) {
  1658. $num_records++;
  1659. $this->assertTrue($record->age >= $last_age, 'Results returned in correct order.');
  1660. $last_age = $record->age;
  1661. }
  1662. $this->assertEqual($num_records, 4, 'Returned the correct number of rows.');
  1663. }
  1664. /**
  1665. * Test multiple order by.
  1666. */
  1667. function testSimpleSelectMultiOrdered() {
  1668. $query = db_select('test');
  1669. $name_field = $query->addField('test', 'name');
  1670. $age_field = $query->addField('test', 'age', 'age');
  1671. $job_field = $query->addField('test', 'job');
  1672. $query->orderBy($job_field);
  1673. $query->orderBy($age_field);
  1674. $result = $query->execute();
  1675. $num_records = 0;
  1676. $expected = array(
  1677. array('Ringo', 28, 'Drummer'),
  1678. array('John', 25, 'Singer'),
  1679. array('George', 27, 'Singer'),
  1680. array('Paul', 26, 'Songwriter'),
  1681. );
  1682. $results = $result->fetchAll(PDO::FETCH_NUM);
  1683. foreach ($expected as $k => $record) {
  1684. $num_records++;
  1685. foreach ($record as $kk => $col) {
  1686. if ($expected[$k][$kk] != $results[$k][$kk]) {
  1687. $this->assertTrue(FALSE, 'Results returned in correct order.');
  1688. }
  1689. }
  1690. }
  1691. $this->assertEqual($num_records, 4, 'Returned the correct number of rows.');
  1692. }
  1693. /**
  1694. * Test order by descending.
  1695. */
  1696. function testSimpleSelectOrderedDesc() {
  1697. $query = db_select('test');
  1698. $name_field = $query->addField('test', 'name');
  1699. $age_field = $query->addField('test', 'age', 'age');
  1700. $query->orderBy($age_field, 'DESC');
  1701. $result = $query->execute();
  1702. $num_records = 0;
  1703. $last_age = 100000000;
  1704. foreach ($result as $record) {
  1705. $num_records++;
  1706. $this->assertTrue($record->age <= $last_age, 'Results returned in correct order.');
  1707. $last_age = $record->age;
  1708. }
  1709. $this->assertEqual($num_records, 4, 'Returned the correct number of rows.');
  1710. }
  1711. /**
  1712. * Tests that the sort direction is sanitized properly.
  1713. */
  1714. function testOrderByEscaping() {
  1715. $query = db_select('test')->orderBy('name', 'invalid direction');
  1716. $order_bys = $query->getOrderBy();
  1717. $this->assertEqual($order_bys['name'], 'ASC', 'Invalid order by direction is converted to ASC.');
  1718. }
  1719. }
  1720. /**
  1721. * Test more complex select statements.
  1722. */
  1723. class DatabaseSelectComplexTestCase extends DatabaseTestCase {
  1724. public static function getInfo() {
  1725. return array(
  1726. 'name' => 'Select tests, complex',
  1727. 'description' => 'Test the Select query builder with more complex queries.',
  1728. 'group' => 'Database',
  1729. );
  1730. }
  1731. /**
  1732. * Test simple JOIN statements.
  1733. */
  1734. function testDefaultJoin() {
  1735. $query = db_select('test_task', 't');
  1736. $people_alias = $query->join('test', 'p', 't.pid = p.id');
  1737. $name_field = $query->addField($people_alias, 'name', 'name');
  1738. $task_field = $query->addField('t', 'task', 'task');
  1739. $priority_field = $query->addField('t', 'priority', 'priority');
  1740. $query->orderBy($priority_field);
  1741. $result = $query->execute();
  1742. $num_records = 0;
  1743. $last_priority = 0;
  1744. foreach ($result as $record) {
  1745. $num_records++;
  1746. $this->assertTrue($record->$priority_field >= $last_priority, 'Results returned in correct order.');
  1747. $this->assertNotEqual($record->$name_field, 'Ringo', 'Taskless person not selected.');
  1748. $last_priority = $record->$priority_field;
  1749. }
  1750. $this->assertEqual($num_records, 7, 'Returned the correct number of rows.');
  1751. }
  1752. /**
  1753. * Test LEFT OUTER joins.
  1754. */
  1755. function testLeftOuterJoin() {
  1756. $query = db_select('test', 'p');
  1757. $people_alias = $query->leftJoin('test_task', 't', 't.pid = p.id');
  1758. $name_field = $query->addField('p', 'name', 'name');
  1759. $task_field = $query->addField($people_alias, 'task', 'task');
  1760. $priority_field = $query->addField($people_alias, 'priority', 'priority');
  1761. $query->orderBy($name_field);
  1762. $result = $query->execute();
  1763. $num_records = 0;
  1764. $last_name = 0;
  1765. foreach ($result as $record) {
  1766. $num_records++;
  1767. $this->assertTrue(strcmp($record->$name_field, $last_name) >= 0, 'Results returned in correct order.');
  1768. $last_priority = $record->$name_field;
  1769. }
  1770. $this->assertEqual($num_records, 8, 'Returned the correct number of rows.');
  1771. }
  1772. /**
  1773. * Test GROUP BY clauses.
  1774. */
  1775. function testGroupBy() {
  1776. $query = db_select('test_task', 't');
  1777. $count_field = $query->addExpression('COUNT(task)', 'num');
  1778. $task_field = $query->addField('t', 'task');
  1779. $query->orderBy($count_field);
  1780. $query->groupBy($task_field);
  1781. $result = $query->execute();
  1782. $num_records = 0;
  1783. $last_count = 0;
  1784. $records = array();
  1785. foreach ($result as $record) {
  1786. $num_records++;
  1787. $this->assertTrue($record->$count_field >= $last_count, 'Results returned in correct order.');
  1788. $last_count = $record->$count_field;
  1789. $records[$record->$task_field] = $record->$count_field;
  1790. }
  1791. $correct_results = array(
  1792. 'eat' => 1,
  1793. 'sleep' => 2,
  1794. 'code' => 1,
  1795. 'found new band' => 1,
  1796. 'perform at superbowl' => 1,
  1797. );
  1798. foreach ($correct_results as $task => $count) {
  1799. $this->assertEqual($records[$task], $count, format_string("Correct number of '@task' records found.", array('@task' => $task)));
  1800. }
  1801. $this->assertEqual($num_records, 6, 'Returned the correct number of total rows.');
  1802. }
  1803. /**
  1804. * Test GROUP BY and HAVING clauses together.
  1805. */
  1806. function testGroupByAndHaving() {
  1807. $query = db_select('test_task', 't');
  1808. $count_field = $query->addExpression('COUNT(task)', 'num');
  1809. $task_field = $query->addField('t', 'task');
  1810. $query->orderBy($count_field);
  1811. $query->groupBy($task_field);
  1812. $query->having('COUNT(task) >= 2');
  1813. $result = $query->execute();
  1814. $num_records = 0;
  1815. $last_count = 0;
  1816. $records = array();
  1817. foreach ($result as $record) {
  1818. $num_records++;
  1819. $this->assertTrue($record->$count_field >= 2, 'Record has the minimum count.');
  1820. $this->assertTrue($record->$count_field >= $last_count, 'Results returned in correct order.');
  1821. $last_count = $record->$count_field;
  1822. $records[$record->$task_field] = $record->$count_field;
  1823. }
  1824. $correct_results = array(
  1825. 'sleep' => 2,
  1826. );
  1827. foreach ($correct_results as $task => $count) {
  1828. $this->assertEqual($records[$task], $count, format_string("Correct number of '@task' records found.", array('@task' => $task)));
  1829. }
  1830. $this->assertEqual($num_records, 1, 'Returned the correct number of total rows.');
  1831. }
  1832. /**
  1833. * Test range queries. The SQL clause varies with the database.
  1834. */
  1835. function testRange() {
  1836. $query = db_select('test');
  1837. $name_field = $query->addField('test', 'name');
  1838. $age_field = $query->addField('test', 'age', 'age');
  1839. $query->range(0, 2);
  1840. $result = $query->execute();
  1841. $num_records = 0;
  1842. foreach ($result as $record) {
  1843. $num_records++;
  1844. }
  1845. $this->assertEqual($num_records, 2, 'Returned the correct number of rows.');
  1846. }
  1847. /**
  1848. * Test distinct queries.
  1849. */
  1850. function testDistinct() {
  1851. $query = db_select('test_task');
  1852. $task_field = $query->addField('test_task', 'task');
  1853. $query->distinct();
  1854. $result = $query->execute();
  1855. $num_records = 0;
  1856. foreach ($result as $record) {
  1857. $num_records++;
  1858. }
  1859. $this->assertEqual($num_records, 6, 'Returned the correct number of rows.');
  1860. }
  1861. /**
  1862. * Test that we can generate a count query from a built query.
  1863. */
  1864. function testCountQuery() {
  1865. $query = db_select('test');
  1866. $name_field = $query->addField('test', 'name');
  1867. $age_field = $query->addField('test', 'age', 'age');
  1868. $query->orderBy('name');
  1869. $count = $query->countQuery()->execute()->fetchField();
  1870. $this->assertEqual($count, 4, 'Counted the correct number of records.');
  1871. // Now make sure we didn't break the original query! We should still have
  1872. // all of the fields we asked for.
  1873. $record = $query->execute()->fetch();
  1874. $this->assertEqual($record->$name_field, 'George', 'Correct data retrieved.');
  1875. $this->assertEqual($record->$age_field, 27, 'Correct data retrieved.');
  1876. }
  1877. function testHavingCountQuery() {
  1878. $query = db_select('test')
  1879. ->extend('PagerDefault')
  1880. ->groupBy('age')
  1881. ->having('age + 1 > 0');
  1882. $query->addField('test', 'age');
  1883. $query->addExpression('age + 1');
  1884. $count = count($query->execute()->fetchCol());
  1885. $this->assertEqual($count, 4, 'Counted the correct number of records.');
  1886. }
  1887. /**
  1888. * Test that countQuery properly removes 'all_fields' statements and
  1889. * ordering clauses.
  1890. */
  1891. function testCountQueryRemovals() {
  1892. $query = db_select('test');
  1893. $query->fields('test');
  1894. $query->orderBy('name');
  1895. $count = $query->countQuery();
  1896. // Check that the 'all_fields' statement is handled properly.
  1897. $tables = $query->getTables();
  1898. $this->assertEqual($tables['test']['all_fields'], 1, 'Query correctly sets \'all_fields\' statement.');
  1899. $tables = $count->getTables();
  1900. $this->assertFalse(isset($tables['test']['all_fields']), 'Count query correctly unsets \'all_fields\' statement.');
  1901. // Check that the ordering clause is handled properly.
  1902. $orderby = $query->getOrderBy();
  1903. $this->assertEqual($orderby['name'], 'ASC', 'Query correctly sets ordering clause.');
  1904. $orderby = $count->getOrderBy();
  1905. $this->assertFalse(isset($orderby['name']), 'Count query correctly unsets ordering caluse.');
  1906. // Make sure that the count query works.
  1907. $count = $count->execute()->fetchField();
  1908. $this->assertEqual($count, 4, 'Counted the correct number of records.');
  1909. }
  1910. /**
  1911. * Test that countQuery properly removes fields and expressions.
  1912. */
  1913. function testCountQueryFieldRemovals() {
  1914. // countQuery should remove all fields and expressions, so this can be
  1915. // tested by adding a non-existent field and expression: if it ends
  1916. // up in the query, an error will be thrown. If not, it will return the
  1917. // number of records, which in this case happens to be 4 (there are four
  1918. // records in the {test} table).
  1919. $query = db_select('test');
  1920. $query->fields('test', array('fail'));
  1921. $this->assertEqual(4, $query->countQuery()->execute()->fetchField(), 'Count Query removed fields');
  1922. $query = db_select('test');
  1923. $query->addExpression('fail');
  1924. $this->assertEqual(4, $query->countQuery()->execute()->fetchField(), 'Count Query removed expressions');
  1925. }
  1926. /**
  1927. * Test that we can generate a count query from a query with distinct.
  1928. */
  1929. function testCountQueryDistinct() {
  1930. $query = db_select('test_task');
  1931. $task_field = $query->addField('test_task', 'task');
  1932. $query->distinct();
  1933. $count = $query->countQuery()->execute()->fetchField();
  1934. $this->assertEqual($count, 6, 'Counted the correct number of records.');
  1935. }
  1936. /**
  1937. * Test that we can generate a count query from a query with GROUP BY.
  1938. */
  1939. function testCountQueryGroupBy() {
  1940. $query = db_select('test_task');
  1941. $pid_field = $query->addField('test_task', 'pid');
  1942. $query->groupBy('pid');
  1943. $count = $query->countQuery()->execute()->fetchField();
  1944. $this->assertEqual($count, 3, 'Counted the correct number of records.');
  1945. // Use a column alias as, without one, the query can succeed for the wrong
  1946. // reason.
  1947. $query = db_select('test_task');
  1948. $pid_field = $query->addField('test_task', 'pid', 'pid_alias');
  1949. $query->addExpression('COUNT(test_task.task)', 'count');
  1950. $query->groupBy('pid_alias');
  1951. $query->orderBy('pid_alias', 'asc');
  1952. $count = $query->countQuery()->execute()->fetchField();
  1953. $this->assertEqual($count, 3, 'Counted the correct number of records.');
  1954. }
  1955. /**
  1956. * Confirm that we can properly nest conditional clauses.
  1957. */
  1958. function testNestedConditions() {
  1959. // This query should translate to:
  1960. // "SELECT job FROM {test} WHERE name = 'Paul' AND (age = 26 OR age = 27)"
  1961. // That should find only one record. Yes it's a non-optimal way of writing
  1962. // that query but that's not the point!
  1963. $query = db_select('test');
  1964. $query->addField('test', 'job');
  1965. $query->condition('name', 'Paul');
  1966. $query->condition(db_or()->condition('age', 26)->condition('age', 27));
  1967. $job = $query->execute()->fetchField();
  1968. $this->assertEqual($job, 'Songwriter', 'Correct data retrieved.');
  1969. }
  1970. /**
  1971. * Confirm we can join on a single table twice with a dynamic alias.
  1972. */
  1973. function testJoinTwice() {
  1974. $query = db_select('test')->fields('test');
  1975. $alias = $query->join('test', 'test', 'test.job = %alias.job');
  1976. $query->addField($alias, 'name', 'othername');
  1977. $query->addField($alias, 'job', 'otherjob');
  1978. $query->where("$alias.name <> test.name");
  1979. $crowded_job = $query->execute()->fetch();
  1980. $this->assertEqual($crowded_job->job, $crowded_job->otherjob, 'Correctly joined same table twice.');
  1981. $this->assertNotEqual($crowded_job->name, $crowded_job->othername, 'Correctly joined same table twice.');
  1982. }
  1983. }
  1984. /**
  1985. * Test more complex select statements, part 2.
  1986. */
  1987. class DatabaseSelectComplexTestCase2 extends DatabaseTestCase {
  1988. public static function getInfo() {
  1989. return array(
  1990. 'name' => 'Select tests, complex 2',
  1991. 'description' => 'Test the Select query builder with even more complex queries.',
  1992. 'group' => 'Database',
  1993. );
  1994. }
  1995. function setUp() {
  1996. DrupalWebTestCase::setUp('database_test', 'node_access_test');
  1997. $schema['test'] = drupal_get_schema('test');
  1998. $schema['test_people'] = drupal_get_schema('test_people');
  1999. $schema['test_one_blob'] = drupal_get_schema('test_one_blob');
  2000. $schema['test_two_blobs'] = drupal_get_schema('test_two_blobs');
  2001. $schema['test_task'] = drupal_get_schema('test_task');
  2002. $this->installTables($schema);
  2003. $this->addSampleData();
  2004. }
  2005. /**
  2006. * Test that we can join on a query.
  2007. */
  2008. function testJoinSubquery() {
  2009. $acct = $this->drupalCreateUser(array('access content'));
  2010. $this->drupalLogin($acct);
  2011. $query = db_select('test_task', 'tt', array('target' => 'slave'));
  2012. $query->addExpression('tt.pid + 1', 'abc');
  2013. $query->condition('priority', 1, '>');
  2014. $query->condition('priority', 100, '<');
  2015. $subquery = db_select('test', 'tp');
  2016. $subquery->join('test_one_blob', 'tpb', 'tp.id = tpb.id');
  2017. $subquery->join('node', 'n', 'tp.id = n.nid');
  2018. $subquery->addTag('node_access');
  2019. $subquery->addMetaData('account', $acct);
  2020. $subquery->addField('tp', 'id');
  2021. $subquery->condition('age', 5, '>');
  2022. $subquery->condition('age', 500, '<');
  2023. $query->leftJoin($subquery, 'sq', 'tt.pid = sq.id');
  2024. $query->join('test_one_blob', 'tb3', 'tt.pid = tb3.id');
  2025. // Construct the query string.
  2026. // This is the same sequence that SelectQuery::execute() goes through.
  2027. $query->preExecute();
  2028. $query->getArguments();
  2029. $str = (string) $query;
  2030. // Verify that the string only has one copy of condition placeholder 0.
  2031. $pos = strpos($str, 'db_condition_placeholder_0', 0);
  2032. $pos2 = strpos($str, 'db_condition_placeholder_0', $pos + 1);
  2033. $this->assertFalse($pos2, 'Condition placeholder is not repeated.');
  2034. }
  2035. }
  2036. class DatabaseSelectPagerDefaultTestCase extends DatabaseTestCase {
  2037. public static function getInfo() {
  2038. return array(
  2039. 'name' => 'Pager query tests',
  2040. 'description' => 'Test the pager query extender.',
  2041. 'group' => 'Database',
  2042. );
  2043. }
  2044. /**
  2045. * Confirm that a pager query returns the correct results.
  2046. *
  2047. * Note that we have to make an HTTP request to a test page handler
  2048. * because the pager depends on GET parameters.
  2049. */
  2050. function testEvenPagerQuery() {
  2051. // To keep the test from being too brittle, we determine up front
  2052. // what the page count should be dynamically, and pass the control
  2053. // information forward to the actual query on the other side of the
  2054. // HTTP request.
  2055. $limit = 2;
  2056. $count = db_query('SELECT COUNT(*) FROM {test}')->fetchField();
  2057. $correct_number = $limit;
  2058. $num_pages = floor($count / $limit);
  2059. // If there is no remainder from rounding, subtract 1 since we index from 0.
  2060. if (!($num_pages * $limit < $count)) {
  2061. $num_pages--;
  2062. }
  2063. for ($page = 0; $page <= $num_pages; ++$page) {
  2064. $this->drupalGet('database_test/pager_query_even/' . $limit, array('query' => array('page' => $page)));
  2065. $data = json_decode($this->drupalGetContent());
  2066. if ($page == $num_pages) {
  2067. $correct_number = $count - ($limit * $page);
  2068. }
  2069. $this->assertEqual(count($data->names), $correct_number, format_string('Correct number of records returned by pager: @number', array('@number' => $correct_number)));
  2070. }
  2071. }
  2072. /**
  2073. * Confirm that a pager query returns the correct results.
  2074. *
  2075. * Note that we have to make an HTTP request to a test page handler
  2076. * because the pager depends on GET parameters.
  2077. */
  2078. function testOddPagerQuery() {
  2079. // To keep the test from being too brittle, we determine up front
  2080. // what the page count should be dynamically, and pass the control
  2081. // information forward to the actual query on the other side of the
  2082. // HTTP request.
  2083. $limit = 2;
  2084. $count = db_query('SELECT COUNT(*) FROM {test_task}')->fetchField();
  2085. $correct_number = $limit;
  2086. $num_pages = floor($count / $limit);
  2087. // If there is no remainder from rounding, subtract 1 since we index from 0.
  2088. if (!($num_pages * $limit < $count)) {
  2089. $num_pages--;
  2090. }
  2091. for ($page = 0; $page <= $num_pages; ++$page) {
  2092. $this->drupalGet('database_test/pager_query_odd/' . $limit, array('query' => array('page' => $page)));
  2093. $data = json_decode($this->drupalGetContent());
  2094. if ($page == $num_pages) {
  2095. $correct_number = $count - ($limit * $page);
  2096. }
  2097. $this->assertEqual(count($data->names), $correct_number, format_string('Correct number of records returned by pager: @number', array('@number' => $correct_number)));
  2098. }
  2099. }
  2100. /**
  2101. * Confirm that a pager query with inner pager query returns valid results.
  2102. *
  2103. * This is a regression test for #467984.
  2104. */
  2105. function testInnerPagerQuery() {
  2106. $query = db_select('test', 't')->extend('PagerDefault');
  2107. $query
  2108. ->fields('t', array('age'))
  2109. ->orderBy('age')
  2110. ->limit(5);
  2111. $outer_query = db_select($query);
  2112. $outer_query->addField('subquery', 'age');
  2113. $ages = $outer_query
  2114. ->execute()
  2115. ->fetchCol();
  2116. $this->assertEqual($ages, array(25, 26, 27, 28), 'Inner pager query returned the correct ages.');
  2117. }
  2118. /**
  2119. * Confirm that a paging query with a having expression returns valid results.
  2120. *
  2121. * This is a regression test for #467984.
  2122. */
  2123. function testHavingPagerQuery() {
  2124. $query = db_select('test', 't')->extend('PagerDefault');
  2125. $query
  2126. ->fields('t', array('name'))
  2127. ->orderBy('name')
  2128. ->groupBy('name')
  2129. ->having('MAX(age) > :count', array(':count' => 26))
  2130. ->limit(5);
  2131. $ages = $query
  2132. ->execute()
  2133. ->fetchCol();
  2134. $this->assertEqual($ages, array('George', 'Ringo'), 'Pager query with having expression returned the correct ages.');
  2135. }
  2136. /**
  2137. * Confirm that every pager gets a valid non-overlaping element ID.
  2138. */
  2139. function testElementNumbers() {
  2140. $_GET['page'] = '3, 2, 1, 0';
  2141. $name = db_select('test', 't')->extend('PagerDefault')
  2142. ->element(2)
  2143. ->fields('t', array('name'))
  2144. ->orderBy('age')
  2145. ->limit(1)
  2146. ->execute()
  2147. ->fetchField();
  2148. $this->assertEqual($name, 'Paul', 'Pager query #1 with a specified element ID returned the correct results.');
  2149. // Setting an element smaller than the previous one
  2150. // should not overwrite the pager $maxElement with a smaller value.
  2151. $name = db_select('test', 't')->extend('PagerDefault')
  2152. ->element(1)
  2153. ->fields('t', array('name'))
  2154. ->orderBy('age')
  2155. ->limit(1)
  2156. ->execute()
  2157. ->fetchField();
  2158. $this->assertEqual($name, 'George', 'Pager query #2 with a specified element ID returned the correct results.');
  2159. $name = db_select('test', 't')->extend('PagerDefault')
  2160. ->fields('t', array('name'))
  2161. ->orderBy('age')
  2162. ->limit(1)
  2163. ->execute()
  2164. ->fetchField();
  2165. $this->assertEqual($name, 'John', 'Pager query #3 with a generated element ID returned the correct results.');
  2166. unset($_GET['page']);
  2167. }
  2168. }
  2169. class DatabaseSelectTableSortDefaultTestCase extends DatabaseTestCase {
  2170. public static function getInfo() {
  2171. return array(
  2172. 'name' => 'Tablesort query tests',
  2173. 'description' => 'Test the tablesort query extender.',
  2174. 'group' => 'Database',
  2175. );
  2176. }
  2177. /**
  2178. * Confirm that a tablesort query returns the correct results.
  2179. *
  2180. * Note that we have to make an HTTP request to a test page handler
  2181. * because the pager depends on GET parameters.
  2182. */
  2183. function testTableSortQuery() {
  2184. $sorts = array(
  2185. array('field' => t('Task ID'), 'sort' => 'desc', 'first' => 'perform at superbowl', 'last' => 'eat'),
  2186. array('field' => t('Task ID'), 'sort' => 'asc', 'first' => 'eat', 'last' => 'perform at superbowl'),
  2187. array('field' => t('Task'), 'sort' => 'asc', 'first' => 'code', 'last' => 'sleep'),
  2188. array('field' => t('Task'), 'sort' => 'desc', 'first' => 'sleep', 'last' => 'code'),
  2189. // more elements here
  2190. );
  2191. foreach ($sorts as $sort) {
  2192. $this->drupalGet('database_test/tablesort/', array('query' => array('order' => $sort['field'], 'sort' => $sort['sort'])));
  2193. $data = json_decode($this->drupalGetContent());
  2194. $first = array_shift($data->tasks);
  2195. $last = array_pop($data->tasks);
  2196. $this->assertEqual($first->task, $sort['first'], 'Items appear in the correct order.');
  2197. $this->assertEqual($last->task, $sort['last'], 'Items appear in the correct order.');
  2198. }
  2199. }
  2200. /**
  2201. * Confirm that if a tablesort's orderByHeader is called before another orderBy, that the header happens first.
  2202. *
  2203. */
  2204. function testTableSortQueryFirst() {
  2205. $sorts = array(
  2206. array('field' => t('Task ID'), 'sort' => 'desc', 'first' => 'perform at superbowl', 'last' => 'eat'),
  2207. array('field' => t('Task ID'), 'sort' => 'asc', 'first' => 'eat', 'last' => 'perform at superbowl'),
  2208. array('field' => t('Task'), 'sort' => 'asc', 'first' => 'code', 'last' => 'sleep'),
  2209. array('field' => t('Task'), 'sort' => 'desc', 'first' => 'sleep', 'last' => 'code'),
  2210. // more elements here
  2211. );
  2212. foreach ($sorts as $sort) {
  2213. $this->drupalGet('database_test/tablesort_first/', array('query' => array('order' => $sort['field'], 'sort' => $sort['sort'])));
  2214. $data = json_decode($this->drupalGetContent());
  2215. $first = array_shift($data->tasks);
  2216. $last = array_pop($data->tasks);
  2217. $this->assertEqual($first->task, $sort['first'], format_string('Items appear in the correct order sorting by @field @sort.', array('@field' => $sort['field'], '@sort' => $sort['sort'])));
  2218. $this->assertEqual($last->task, $sort['last'], format_string('Items appear in the correct order sorting by @field @sort.', array('@field' => $sort['field'], '@sort' => $sort['sort'])));
  2219. }
  2220. }
  2221. /**
  2222. * Confirm that if a sort is not set in a tableselect form there is no error thrown when using the default.
  2223. */
  2224. function testTableSortDefaultSort() {
  2225. $this->drupalGet('database_test/tablesort_default_sort');
  2226. // Any PHP errors or notices thrown would trigger a simpletest exception, so
  2227. // no additional assertions are needed.
  2228. }
  2229. }
  2230. /**
  2231. * Select tagging tests.
  2232. *
  2233. * Tags are a way to flag queries for alter hooks so they know
  2234. * what type of query it is, such as "node_access".
  2235. */
  2236. class DatabaseTaggingTestCase extends DatabaseTestCase {
  2237. public static function getInfo() {
  2238. return array(
  2239. 'name' => 'Query tagging tests',
  2240. 'description' => 'Test the tagging capabilities of the Select builder.',
  2241. 'group' => 'Database',
  2242. );
  2243. }
  2244. /**
  2245. * Confirm that a query has a "tag" added to it.
  2246. */
  2247. function testHasTag() {
  2248. $query = db_select('test');
  2249. $query->addField('test', 'name');
  2250. $query->addField('test', 'age', 'age');
  2251. $query->addTag('test');
  2252. $this->assertTrue($query->hasTag('test'), 'hasTag() returned true.');
  2253. $this->assertFalse($query->hasTag('other'), 'hasTag() returned false.');
  2254. }
  2255. /**
  2256. * Test query tagging "has all of these tags" functionality.
  2257. */
  2258. function testHasAllTags() {
  2259. $query = db_select('test');
  2260. $query->addField('test', 'name');
  2261. $query->addField('test', 'age', 'age');
  2262. $query->addTag('test');
  2263. $query->addTag('other');
  2264. $this->assertTrue($query->hasAllTags('test', 'other'), 'hasAllTags() returned true.');
  2265. $this->assertFalse($query->hasAllTags('test', 'stuff'), 'hasAllTags() returned false.');
  2266. }
  2267. /**
  2268. * Test query tagging "has at least one of these tags" functionality.
  2269. */
  2270. function testHasAnyTag() {
  2271. $query = db_select('test');
  2272. $query->addField('test', 'name');
  2273. $query->addField('test', 'age', 'age');
  2274. $query->addTag('test');
  2275. $this->assertTrue($query->hasAnyTag('test', 'other'), 'hasAnyTag() returned true.');
  2276. $this->assertFalse($query->hasAnyTag('other', 'stuff'), 'hasAnyTag() returned false.');
  2277. }
  2278. /**
  2279. * Confirm that an extended query has a "tag" added to it.
  2280. */
  2281. function testExtenderHasTag() {
  2282. $query = db_select('test')
  2283. ->extend('SelectQueryExtender');
  2284. $query->addField('test', 'name');
  2285. $query->addField('test', 'age', 'age');
  2286. $query->addTag('test');
  2287. $this->assertTrue($query->hasTag('test'), 'hasTag() returned true.');
  2288. $this->assertFalse($query->hasTag('other'), 'hasTag() returned false.');
  2289. }
  2290. /**
  2291. * Test extended query tagging "has all of these tags" functionality.
  2292. */
  2293. function testExtenderHasAllTags() {
  2294. $query = db_select('test')
  2295. ->extend('SelectQueryExtender');
  2296. $query->addField('test', 'name');
  2297. $query->addField('test', 'age', 'age');
  2298. $query->addTag('test');
  2299. $query->addTag('other');
  2300. $this->assertTrue($query->hasAllTags('test', 'other'), 'hasAllTags() returned true.');
  2301. $this->assertFalse($query->hasAllTags('test', 'stuff'), 'hasAllTags() returned false.');
  2302. }
  2303. /**
  2304. * Test extended query tagging "has at least one of these tags" functionality.
  2305. */
  2306. function testExtenderHasAnyTag() {
  2307. $query = db_select('test')
  2308. ->extend('SelectQueryExtender');
  2309. $query->addField('test', 'name');
  2310. $query->addField('test', 'age', 'age');
  2311. $query->addTag('test');
  2312. $this->assertTrue($query->hasAnyTag('test', 'other'), 'hasAnyTag() returned true.');
  2313. $this->assertFalse($query->hasAnyTag('other', 'stuff'), 'hasAnyTag() returned false.');
  2314. }
  2315. /**
  2316. * Test that we can attach meta data to a query object.
  2317. *
  2318. * This is how we pass additional context to alter hooks.
  2319. */
  2320. function testMetaData() {
  2321. $query = db_select('test');
  2322. $query->addField('test', 'name');
  2323. $query->addField('test', 'age', 'age');
  2324. $data = array(
  2325. 'a' => 'A',
  2326. 'b' => 'B',
  2327. );
  2328. $query->addMetaData('test', $data);
  2329. $return = $query->getMetaData('test');
  2330. $this->assertEqual($data, $return, 'Corect metadata returned.');
  2331. $return = $query->getMetaData('nothere');
  2332. $this->assertNull($return, 'Non-existent key returned NULL.');
  2333. }
  2334. }
  2335. /**
  2336. * Select alter tests.
  2337. *
  2338. * @see database_test_query_alter()
  2339. */
  2340. class DatabaseAlterTestCase extends DatabaseTestCase {
  2341. public static function getInfo() {
  2342. return array(
  2343. 'name' => 'Query altering tests',
  2344. 'description' => 'Test the hook_query_alter capabilities of the Select builder.',
  2345. 'group' => 'Database',
  2346. );
  2347. }
  2348. /**
  2349. * Test that we can do basic alters.
  2350. */
  2351. function testSimpleAlter() {
  2352. $query = db_select('test');
  2353. $query->addField('test', 'name');
  2354. $query->addField('test', 'age', 'age');
  2355. $query->addTag('database_test_alter_add_range');
  2356. $result = $query->execute();
  2357. $num_records = 0;
  2358. foreach ($result as $record) {
  2359. $num_records++;
  2360. }
  2361. $this->assertEqual($num_records, 2, 'Returned the correct number of rows.');
  2362. }
  2363. /**
  2364. * Test that we can alter the joins on a query.
  2365. */
  2366. function testAlterWithJoin() {
  2367. $query = db_select('test_task');
  2368. $tid_field = $query->addField('test_task', 'tid');
  2369. $task_field = $query->addField('test_task', 'task');
  2370. $query->orderBy($task_field);
  2371. $query->addTag('database_test_alter_add_join');
  2372. $result = $query->execute();
  2373. $records = $result->fetchAll();
  2374. $this->assertEqual(count($records), 2, 'Returned the correct number of rows.');
  2375. $this->assertEqual($records[0]->name, 'George', 'Correct data retrieved.');
  2376. $this->assertEqual($records[0]->$tid_field, 4, 'Correct data retrieved.');
  2377. $this->assertEqual($records[0]->$task_field, 'sing', 'Correct data retrieved.');
  2378. $this->assertEqual($records[1]->name, 'George', 'Correct data retrieved.');
  2379. $this->assertEqual($records[1]->$tid_field, 5, 'Correct data retrieved.');
  2380. $this->assertEqual($records[1]->$task_field, 'sleep', 'Correct data retrieved.');
  2381. }
  2382. /**
  2383. * Test that we can alter a query's conditionals.
  2384. */
  2385. function testAlterChangeConditional() {
  2386. $query = db_select('test_task');
  2387. $tid_field = $query->addField('test_task', 'tid');
  2388. $pid_field = $query->addField('test_task', 'pid');
  2389. $task_field = $query->addField('test_task', 'task');
  2390. $people_alias = $query->join('test', 'people', "test_task.pid = people.id");
  2391. $name_field = $query->addField($people_alias, 'name', 'name');
  2392. $query->condition('test_task.tid', '1');
  2393. $query->orderBy($tid_field);
  2394. $query->addTag('database_test_alter_change_conditional');
  2395. $result = $query->execute();
  2396. $records = $result->fetchAll();
  2397. $this->assertEqual(count($records), 1, 'Returned the correct number of rows.');
  2398. $this->assertEqual($records[0]->$name_field, 'John', 'Correct data retrieved.');
  2399. $this->assertEqual($records[0]->$tid_field, 2, 'Correct data retrieved.');
  2400. $this->assertEqual($records[0]->$pid_field, 1, 'Correct data retrieved.');
  2401. $this->assertEqual($records[0]->$task_field, 'sleep', 'Correct data retrieved.');
  2402. }
  2403. /**
  2404. * Test that we can alter the fields of a query.
  2405. */
  2406. function testAlterChangeFields() {
  2407. $query = db_select('test');
  2408. $name_field = $query->addField('test', 'name');
  2409. $age_field = $query->addField('test', 'age', 'age');
  2410. $query->orderBy('name');
  2411. $query->addTag('database_test_alter_change_fields');
  2412. $record = $query->execute()->fetch();
  2413. $this->assertEqual($record->$name_field, 'George', 'Correct data retrieved.');
  2414. $this->assertFalse(isset($record->$age_field), 'Age field not found, as intended.');
  2415. }
  2416. /**
  2417. * Test that we can alter expressions in the query.
  2418. */
  2419. function testAlterExpression() {
  2420. $query = db_select('test');
  2421. $name_field = $query->addField('test', 'name');
  2422. $age_field = $query->addExpression("age*2", 'double_age');
  2423. $query->condition('age', 27);
  2424. $query->addTag('database_test_alter_change_expressions');
  2425. $result = $query->execute();
  2426. // Ensure that we got the right record.
  2427. $record = $result->fetch();
  2428. $this->assertEqual($record->$name_field, 'George', 'Fetched name is correct.');
  2429. $this->assertEqual($record->$age_field, 27*3, 'Fetched age expression is correct.');
  2430. }
  2431. /**
  2432. * Test that we can remove a range() value from a query. This also tests hook_query_TAG_alter().
  2433. */
  2434. function testAlterRemoveRange() {
  2435. $query = db_select('test');
  2436. $query->addField('test', 'name');
  2437. $query->addField('test', 'age', 'age');
  2438. $query->range(0, 2);
  2439. $query->addTag('database_test_alter_remove_range');
  2440. $num_records = count($query->execute()->fetchAll());
  2441. $this->assertEqual($num_records, 4, 'Returned the correct number of rows.');
  2442. }
  2443. /**
  2444. * Test that we can do basic alters on subqueries.
  2445. */
  2446. function testSimpleAlterSubquery() {
  2447. // Create a sub-query with an alter tag.
  2448. $subquery = db_select('test', 'p');
  2449. $subquery->addField('p', 'name');
  2450. $subquery->addField('p', 'id');
  2451. // Pick out George.
  2452. $subquery->condition('age', 27);
  2453. $subquery->addExpression("age*2", 'double_age');
  2454. // This query alter should change it to age * 3.
  2455. $subquery->addTag('database_test_alter_change_expressions');
  2456. // Create a main query and join to sub-query.
  2457. $query = db_select('test_task', 'tt');
  2458. $query->join($subquery, 'pq', 'pq.id = tt.pid');
  2459. $age_field = $query->addField('pq', 'double_age');
  2460. $name_field = $query->addField('pq', 'name');
  2461. $record = $query->execute()->fetch();
  2462. $this->assertEqual($record->$name_field, 'George', 'Fetched name is correct.');
  2463. $this->assertEqual($record->$age_field, 27*3, 'Fetched age expression is correct.');
  2464. }
  2465. }
  2466. /**
  2467. * Regression tests.
  2468. */
  2469. class DatabaseRegressionTestCase extends DatabaseTestCase {
  2470. public static function getInfo() {
  2471. return array(
  2472. 'name' => 'Regression tests',
  2473. 'description' => 'Regression tests cases for the database layer.',
  2474. 'group' => 'Database',
  2475. );
  2476. }
  2477. /**
  2478. * Regression test for #310447.
  2479. *
  2480. * Tries to insert non-ascii UTF-8 data in a database column and checks
  2481. * if its stored properly.
  2482. */
  2483. function testRegression_310447() {
  2484. // That's a 255 character UTF-8 string.
  2485. $name = str_repeat("é", 255);
  2486. db_insert('test')
  2487. ->fields(array(
  2488. 'name' => $name,
  2489. 'age' => 20,
  2490. 'job' => 'Dancer',
  2491. ))->execute();
  2492. $from_database = db_query('SELECT name FROM {test} WHERE name = :name', array(':name' => $name))->fetchField();
  2493. $this->assertIdentical($name, $from_database, "The database handles UTF-8 characters cleanly.");
  2494. }
  2495. /**
  2496. * Test the db_table_exists() function.
  2497. */
  2498. function testDBTableExists() {
  2499. $this->assertIdentical(TRUE, db_table_exists('node'), 'Returns true for existent table.');
  2500. $this->assertIdentical(FALSE, db_table_exists('nosuchtable'), 'Returns false for nonexistent table.');
  2501. }
  2502. /**
  2503. * Test the db_field_exists() function.
  2504. */
  2505. function testDBFieldExists() {
  2506. $this->assertIdentical(TRUE, db_field_exists('node', 'nid'), 'Returns true for existent column.');
  2507. $this->assertIdentical(FALSE, db_field_exists('node', 'nosuchcolumn'), 'Returns false for nonexistent column.');
  2508. }
  2509. /**
  2510. * Test the db_index_exists() function.
  2511. */
  2512. function testDBIndexExists() {
  2513. $this->assertIdentical(TRUE, db_index_exists('node', 'node_created'), 'Returns true for existent index.');
  2514. $this->assertIdentical(FALSE, db_index_exists('node', 'nosuchindex'), 'Returns false for nonexistent index.');
  2515. }
  2516. }
  2517. /**
  2518. * Query logging tests.
  2519. */
  2520. class DatabaseLoggingTestCase extends DatabaseTestCase {
  2521. public static function getInfo() {
  2522. return array(
  2523. 'name' => 'Query logging',
  2524. 'description' => 'Test the query logging facility.',
  2525. 'group' => 'Database',
  2526. );
  2527. }
  2528. /**
  2529. * Test that we can log the existence of a query.
  2530. */
  2531. function testEnableLogging() {
  2532. $log = Database::startLog('testing');
  2533. db_query('SELECT name FROM {test} WHERE age > :age', array(':age' => 25))->fetchCol();
  2534. db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Ringo'))->fetchCol();
  2535. // Trigger a call that does not have file in the backtrace.
  2536. call_user_func_array('db_query', array('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Ringo')))->fetchCol();
  2537. $queries = Database::getLog('testing', 'default');
  2538. $this->assertEqual(count($queries), 3, 'Correct number of queries recorded.');
  2539. foreach ($queries as $query) {
  2540. $this->assertEqual($query['caller']['function'], __FUNCTION__, 'Correct function in query log.');
  2541. }
  2542. }
  2543. /**
  2544. * Test that we can run two logs in parallel.
  2545. */
  2546. function testEnableMultiLogging() {
  2547. Database::startLog('testing1');
  2548. db_query('SELECT name FROM {test} WHERE age > :age', array(':age' => 25))->fetchCol();
  2549. Database::startLog('testing2');
  2550. db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Ringo'))->fetchCol();
  2551. $queries1 = Database::getLog('testing1');
  2552. $queries2 = Database::getLog('testing2');
  2553. $this->assertEqual(count($queries1), 2, 'Correct number of queries recorded for log 1.');
  2554. $this->assertEqual(count($queries2), 1, 'Correct number of queries recorded for log 2.');
  2555. }
  2556. /**
  2557. * Test that we can log queries against multiple targets on the same connection.
  2558. */
  2559. function testEnableTargetLogging() {
  2560. // Clone the master credentials to a slave connection and to another fake
  2561. // connection.
  2562. $connection_info = Database::getConnectionInfo('default');
  2563. Database::addConnectionInfo('default', 'slave', $connection_info['default']);
  2564. Database::startLog('testing1');
  2565. db_query('SELECT name FROM {test} WHERE age > :age', array(':age' => 25))->fetchCol();
  2566. db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Ringo'), array('target' => 'slave'));//->fetchCol();
  2567. $queries1 = Database::getLog('testing1');
  2568. $this->assertEqual(count($queries1), 2, 'Recorded queries from all targets.');
  2569. $this->assertEqual($queries1[0]['target'], 'default', 'First query used default target.');
  2570. $this->assertEqual($queries1[1]['target'], 'slave', 'Second query used slave target.');
  2571. }
  2572. /**
  2573. * Test that logs to separate targets collapse to the same connection properly.
  2574. *
  2575. * This test is identical to the one above, except that it doesn't create
  2576. * a fake target so the query should fall back to running on the default
  2577. * target.
  2578. */
  2579. function testEnableTargetLoggingNoTarget() {
  2580. Database::startLog('testing1');
  2581. db_query('SELECT name FROM {test} WHERE age > :age', array(':age' => 25))->fetchCol();
  2582. // We use "fake" here as a target because any non-existent target will do.
  2583. // However, because all of the tests in this class share a single page
  2584. // request there is likely to be a target of "slave" from one of the other
  2585. // unit tests, so we use a target here that we know with absolute certainty
  2586. // does not exist.
  2587. db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Ringo'), array('target' => 'fake'))->fetchCol();
  2588. $queries1 = Database::getLog('testing1');
  2589. $this->assertEqual(count($queries1), 2, 'Recorded queries from all targets.');
  2590. $this->assertEqual($queries1[0]['target'], 'default', 'First query used default target.');
  2591. $this->assertEqual($queries1[1]['target'], 'default', 'Second query used default target as fallback.');
  2592. }
  2593. /**
  2594. * Test that we can log queries separately on different connections.
  2595. */
  2596. function testEnableMultiConnectionLogging() {
  2597. // Clone the master credentials to a fake connection.
  2598. // That both connections point to the same physical database is irrelevant.
  2599. $connection_info = Database::getConnectionInfo('default');
  2600. Database::addConnectionInfo('test2', 'default', $connection_info['default']);
  2601. Database::startLog('testing1');
  2602. Database::startLog('testing1', 'test2');
  2603. db_query('SELECT name FROM {test} WHERE age > :age', array(':age' => 25))->fetchCol();
  2604. $old_key = db_set_active('test2');
  2605. db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Ringo'), array('target' => 'slave'))->fetchCol();
  2606. db_set_active($old_key);
  2607. $queries1 = Database::getLog('testing1');
  2608. $queries2 = Database::getLog('testing1', 'test2');
  2609. $this->assertEqual(count($queries1), 1, 'Correct number of queries recorded for first connection.');
  2610. $this->assertEqual(count($queries2), 1, 'Correct number of queries recorded for second connection.');
  2611. }
  2612. }
  2613. /**
  2614. * Query serialization tests.
  2615. */
  2616. class DatabaseSerializeQueryTestCase extends DatabaseTestCase {
  2617. public static function getInfo() {
  2618. return array(
  2619. 'name' => 'Serialize query',
  2620. 'description' => 'Test serializing and unserializing a query.',
  2621. 'group' => 'Database',
  2622. );
  2623. }
  2624. /**
  2625. * Confirm that a query can be serialized and unserialized.
  2626. */
  2627. function testSerializeQuery() {
  2628. $query = db_select('test');
  2629. $query->addField('test', 'age');
  2630. $query->condition('name', 'Ringo');
  2631. // If this doesn't work, it will throw an exception, so no need for an
  2632. // assertion.
  2633. $query = unserialize(serialize($query));
  2634. $results = $query->execute()->fetchCol();
  2635. $this->assertEqual($results[0], 28, 'Query properly executed after unserialization.');
  2636. }
  2637. }
  2638. /**
  2639. * Range query tests.
  2640. */
  2641. class DatabaseRangeQueryTestCase extends DrupalWebTestCase {
  2642. public static function getInfo() {
  2643. return array(
  2644. 'name' => 'Range query test',
  2645. 'description' => 'Test the Range query functionality.',
  2646. 'group' => 'Database',
  2647. );
  2648. }
  2649. function setUp() {
  2650. parent::setUp('database_test');
  2651. }
  2652. /**
  2653. * Confirm that range query work and return correct result.
  2654. */
  2655. function testRangeQuery() {
  2656. // Test if return correct number of rows.
  2657. $range_rows = db_query_range("SELECT name FROM {system} ORDER BY name", 2, 3)->fetchAll();
  2658. $this->assertEqual(count($range_rows), 3, 'Range query work and return correct number of rows.');
  2659. // Test if return target data.
  2660. $raw_rows = db_query('SELECT name FROM {system} ORDER BY name')->fetchAll();
  2661. $raw_rows = array_slice($raw_rows, 2, 3);
  2662. $this->assertEqual($range_rows, $raw_rows, 'Range query work and return target data.');
  2663. }
  2664. }
  2665. /**
  2666. * Temporary query tests.
  2667. */
  2668. class DatabaseTemporaryQueryTestCase extends DrupalWebTestCase {
  2669. public static function getInfo() {
  2670. return array(
  2671. 'name' => 'Temporary query test',
  2672. 'description' => 'Test the temporary query functionality.',
  2673. 'group' => 'Database',
  2674. );
  2675. }
  2676. function setUp() {
  2677. parent::setUp('database_test');
  2678. }
  2679. /**
  2680. * Return the number of rows of a table.
  2681. */
  2682. function countTableRows($table_name) {
  2683. return db_select($table_name)->countQuery()->execute()->fetchField();
  2684. }
  2685. /**
  2686. * Confirm that temporary tables work and are limited to one request.
  2687. */
  2688. function testTemporaryQuery() {
  2689. $this->drupalGet('database_test/db_query_temporary');
  2690. $data = json_decode($this->drupalGetContent());
  2691. if ($data) {
  2692. $this->assertEqual($this->countTableRows("system"), $data->row_count, 'The temporary table contains the correct amount of rows.');
  2693. $this->assertFalse(db_table_exists($data->table_name), 'The temporary table is, indeed, temporary.');
  2694. }
  2695. else {
  2696. $this->fail("The creation of the temporary table failed.");
  2697. }
  2698. // Now try to run two db_query_temporary() in the same request.
  2699. $table_name_system = db_query_temporary('SELECT status FROM {system}', array());
  2700. $table_name_users = db_query_temporary('SELECT uid FROM {users}', array());
  2701. $this->assertEqual($this->countTableRows($table_name_system), $this->countTableRows("system"), 'A temporary table was created successfully in this request.');
  2702. $this->assertEqual($this->countTableRows($table_name_users), $this->countTableRows("users"), 'A second temporary table was created successfully in this request.');
  2703. // Check that leading whitespace and comments do not cause problems
  2704. // in the modified query.
  2705. $sql = "
  2706. -- Let's select some rows into a temporary table
  2707. SELECT name FROM {test}
  2708. ";
  2709. $table_name_test = db_query_temporary($sql, array());
  2710. $this->assertEqual($this->countTableRows($table_name_test), $this->countTableRows('test'), 'Leading white space and comments do not interfere with temporary table creation.');
  2711. }
  2712. }
  2713. /**
  2714. * Test how the current database driver interprets the SQL syntax.
  2715. *
  2716. * In order to ensure consistent SQL handling throughout Drupal
  2717. * across multiple kinds of database systems, we test that the
  2718. * database system interprets SQL syntax in an expected fashion.
  2719. */
  2720. class DatabaseBasicSyntaxTestCase extends DatabaseTestCase {
  2721. public static function getInfo() {
  2722. return array(
  2723. 'name' => 'Basic SQL syntax tests',
  2724. 'description' => 'Test SQL syntax interpretation.',
  2725. 'group' => 'Database',
  2726. );
  2727. }
  2728. function setUp() {
  2729. parent::setUp('database_test');
  2730. }
  2731. /**
  2732. * Test for string concatenation.
  2733. */
  2734. function testBasicConcat() {
  2735. $result = db_query('SELECT CONCAT(:a1, CONCAT(:a2, CONCAT(:a3, CONCAT(:a4, :a5))))', array(
  2736. ':a1' => 'This',
  2737. ':a2' => ' ',
  2738. ':a3' => 'is',
  2739. ':a4' => ' a ',
  2740. ':a5' => 'test.',
  2741. ));
  2742. $this->assertIdentical($result->fetchField(), 'This is a test.', 'Basic CONCAT works.');
  2743. }
  2744. /**
  2745. * Test for string concatenation with field values.
  2746. */
  2747. function testFieldConcat() {
  2748. $result = db_query('SELECT CONCAT(:a1, CONCAT(name, CONCAT(:a2, CONCAT(age, :a3)))) FROM {test} WHERE age = :age', array(
  2749. ':a1' => 'The age of ',
  2750. ':a2' => ' is ',
  2751. ':a3' => '.',
  2752. ':age' => 25,
  2753. ));
  2754. $this->assertIdentical($result->fetchField(), 'The age of John is 25.', 'Field CONCAT works.');
  2755. }
  2756. /**
  2757. * Test escaping of LIKE wildcards.
  2758. */
  2759. function testLikeEscape() {
  2760. db_insert('test')
  2761. ->fields(array(
  2762. 'name' => 'Ring_',
  2763. ))
  2764. ->execute();
  2765. // Match both "Ringo" and "Ring_".
  2766. $num_matches = db_select('test', 't')
  2767. ->condition('name', 'Ring_', 'LIKE')
  2768. ->countQuery()
  2769. ->execute()
  2770. ->fetchField();
  2771. $this->assertIdentical($num_matches, '2', 'Found 2 records.');
  2772. // Match only "Ring_" using a LIKE expression with no wildcards.
  2773. $num_matches = db_select('test', 't')
  2774. ->condition('name', db_like('Ring_'), 'LIKE')
  2775. ->countQuery()
  2776. ->execute()
  2777. ->fetchField();
  2778. $this->assertIdentical($num_matches, '1', 'Found 1 record.');
  2779. }
  2780. /**
  2781. * Test LIKE query containing a backslash.
  2782. */
  2783. function testLikeBackslash() {
  2784. db_insert('test')
  2785. ->fields(array('name'))
  2786. ->values(array(
  2787. 'name' => 'abcde\f',
  2788. ))
  2789. ->values(array(
  2790. 'name' => 'abc%\_',
  2791. ))
  2792. ->execute();
  2793. // Match both rows using a LIKE expression with two wildcards and a verbatim
  2794. // backslash.
  2795. $num_matches = db_select('test', 't')
  2796. ->condition('name', 'abc%\\\\_', 'LIKE')
  2797. ->countQuery()
  2798. ->execute()
  2799. ->fetchField();
  2800. $this->assertIdentical($num_matches, '2', 'Found 2 records.');
  2801. // Match only the former using a LIKE expression with no wildcards.
  2802. $num_matches = db_select('test', 't')
  2803. ->condition('name', db_like('abc%\_'), 'LIKE')
  2804. ->countQuery()
  2805. ->execute()
  2806. ->fetchField();
  2807. $this->assertIdentical($num_matches, '1', 'Found 1 record.');
  2808. }
  2809. }
  2810. /**
  2811. * Test case sensitivity handling.
  2812. */
  2813. class DatabaseCaseSensitivityTestCase extends DatabaseTestCase {
  2814. public static function getInfo() {
  2815. return array(
  2816. 'name' => 'Case sensitivity',
  2817. 'description' => 'Test handling case sensitive collation.',
  2818. 'group' => 'Database',
  2819. );
  2820. }
  2821. /**
  2822. * Test BINARY collation in MySQL.
  2823. */
  2824. function testCaseSensitiveInsert() {
  2825. $num_records_before = db_query('SELECT COUNT(*) FROM {test}')->fetchField();
  2826. $john = db_insert('test')
  2827. ->fields(array(
  2828. 'name' => 'john', // <- A record already exists with name 'John'.
  2829. 'age' => 2,
  2830. 'job' => 'Baby',
  2831. ))
  2832. ->execute();
  2833. $num_records_after = db_query('SELECT COUNT(*) FROM {test}')->fetchField();
  2834. $this->assertIdentical($num_records_before + 1, (int) $num_records_after, 'Record inserts correctly.');
  2835. $saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'john'))->fetchField();
  2836. $this->assertIdentical($saved_age, '2', 'Can retrieve after inserting.');
  2837. }
  2838. }
  2839. /**
  2840. * Test invalid data handling.
  2841. */
  2842. class DatabaseInvalidDataTestCase extends DatabaseTestCase {
  2843. public static function getInfo() {
  2844. return array(
  2845. 'name' => 'Invalid data',
  2846. 'description' => 'Test handling of some invalid data.',
  2847. 'group' => 'Database',
  2848. );
  2849. }
  2850. function setUp() {
  2851. parent::setUp('database_test');
  2852. }
  2853. /**
  2854. * Traditional SQL database systems abort inserts when invalid data is encountered.
  2855. */
  2856. function testInsertDuplicateData() {
  2857. // Try to insert multiple records where at least one has bad data.
  2858. try {
  2859. db_insert('test')
  2860. ->fields(array('name', 'age', 'job'))
  2861. ->values(array(
  2862. 'name' => 'Elvis',
  2863. 'age' => 63,
  2864. 'job' => 'Singer',
  2865. ))->values(array(
  2866. 'name' => 'John', // <-- Duplicate value on unique field.
  2867. 'age' => 17,
  2868. 'job' => 'Consultant',
  2869. ))
  2870. ->values(array(
  2871. 'name' => 'Frank',
  2872. 'age' => 75,
  2873. 'job' => 'Singer',
  2874. ))
  2875. ->execute();
  2876. $this->fail('Insert succeedded when it should not have.');
  2877. }
  2878. catch (Exception $e) {
  2879. // Check if the first record was inserted.
  2880. $name = db_query('SELECT name FROM {test} WHERE age = :age', array(':age' => 63))->fetchField();
  2881. if ($name == 'Elvis') {
  2882. if (!Database::getConnection()->supportsTransactions()) {
  2883. // This is an expected fail.
  2884. // Database engines that don't support transactions can leave partial
  2885. // inserts in place when an error occurs. This is the case for MySQL
  2886. // when running on a MyISAM table.
  2887. $this->pass("The whole transaction has not been rolled-back when a duplicate key insert occurs, this is expected because the database doesn't support transactions");
  2888. }
  2889. else {
  2890. $this->fail('The whole transaction is rolled back when a duplicate key insert occurs.');
  2891. }
  2892. }
  2893. else {
  2894. $this->pass('The whole transaction is rolled back when a duplicate key insert occurs.');
  2895. }
  2896. // Ensure the other values were not inserted.
  2897. $record = db_select('test')
  2898. ->fields('test', array('name', 'age'))
  2899. ->condition('age', array(17, 75), 'IN')
  2900. ->execute()->fetchObject();
  2901. $this->assertFalse($record, 'The rest of the insert aborted as expected.');
  2902. }
  2903. }
  2904. }
  2905. /**
  2906. * Drupal-specific SQL syntax tests.
  2907. */
  2908. class DatabaseQueryTestCase extends DatabaseTestCase {
  2909. public static function getInfo() {
  2910. return array(
  2911. 'name' => 'Custom query syntax tests',
  2912. 'description' => 'Test Drupal\'s extended prepared statement syntax..',
  2913. 'group' => 'Database',
  2914. );
  2915. }
  2916. function setUp() {
  2917. parent::setUp('database_test');
  2918. }
  2919. /**
  2920. * Test that we can specify an array of values in the query by simply passing in an array.
  2921. */
  2922. function testArraySubstitution() {
  2923. $names = db_query('SELECT name FROM {test} WHERE age IN (:ages) ORDER BY age', array(':ages' => array(25, 26, 27)))->fetchAll();
  2924. $this->assertEqual(count($names), 3, 'Correct number of names returned');
  2925. }
  2926. /**
  2927. * Test SQL injection via database query array arguments.
  2928. */
  2929. public function testArrayArgumentsSQLInjection() {
  2930. // Attempt SQL injection and verify that it does not work.
  2931. $condition = array(
  2932. "1 ;INSERT INTO {test} (name) VALUES ('test12345678'); -- " => '',
  2933. '1' => '',
  2934. );
  2935. try {
  2936. db_query("SELECT * FROM {test} WHERE name = :name", array(':name' => $condition))->fetchObject();
  2937. $this->fail('SQL injection attempt via array arguments should result in a PDOException.');
  2938. }
  2939. catch (PDOException $e) {
  2940. $this->pass('SQL injection attempt via array arguments should result in a PDOException.');
  2941. }
  2942. // Test that the insert query that was used in the SQL injection attempt did
  2943. // not result in a row being inserted in the database.
  2944. $result = db_select('test')
  2945. ->condition('name', 'test12345678')
  2946. ->countQuery()
  2947. ->execute()
  2948. ->fetchField();
  2949. $this->assertFalse($result, 'SQL injection attempt did not result in a row being inserted in the database table.');
  2950. }
  2951. }
  2952. /**
  2953. * Test transaction support, particularly nesting.
  2954. *
  2955. * We test nesting by having two transaction layers, an outer and inner. The
  2956. * outer layer encapsulates the inner layer. Our transaction nesting abstraction
  2957. * should allow the outer layer function to call any function it wants,
  2958. * especially the inner layer that starts its own transaction, and be
  2959. * confident that, when the function it calls returns, its own transaction
  2960. * is still "alive."
  2961. *
  2962. * Call structure:
  2963. * transactionOuterLayer()
  2964. * Start transaction
  2965. * transactionInnerLayer()
  2966. * Start transaction (does nothing in database)
  2967. * [Maybe decide to roll back]
  2968. * Do more stuff
  2969. * Should still be in transaction A
  2970. *
  2971. */
  2972. class DatabaseTransactionTestCase extends DatabaseTestCase {
  2973. public static function getInfo() {
  2974. return array(
  2975. 'name' => 'Transaction tests',
  2976. 'description' => 'Test the transaction abstraction system.',
  2977. 'group' => 'Database',
  2978. );
  2979. }
  2980. /**
  2981. * Helper method for transaction unit test.
  2982. *
  2983. * This "outer layer" transaction starts and then encapsulates the
  2984. * "inner layer" transaction. This nesting is used to evaluate whether the
  2985. * database transaction API properly supports nesting. By "properly supports,"
  2986. * we mean the outer transaction continues to exist regardless of what
  2987. * functions are called and whether those functions start their own
  2988. * transactions.
  2989. *
  2990. * In contrast, a typical database would commit the outer transaction, start
  2991. * a new transaction for the inner layer, commit the inner layer transaction,
  2992. * and then be confused when the outer layer transaction tries to commit its
  2993. * transaction (which was already committed when the inner transaction
  2994. * started).
  2995. *
  2996. * @param $suffix
  2997. * Suffix to add to field values to differentiate tests.
  2998. * @param $rollback
  2999. * Whether or not to try rolling back the transaction when we're done.
  3000. * @param $ddl_statement
  3001. * Whether to execute a DDL statement during the inner transaction.
  3002. */
  3003. protected function transactionOuterLayer($suffix, $rollback = FALSE, $ddl_statement = FALSE) {
  3004. $connection = Database::getConnection();
  3005. $depth = $connection->transactionDepth();
  3006. $txn = db_transaction();
  3007. // Insert a single row into the testing table.
  3008. db_insert('test')
  3009. ->fields(array(
  3010. 'name' => 'David' . $suffix,
  3011. 'age' => '24',
  3012. ))
  3013. ->execute();
  3014. $this->assertTrue($connection->inTransaction(), 'In transaction before calling nested transaction.');
  3015. // We're already in a transaction, but we call ->transactionInnerLayer
  3016. // to nest another transaction inside the current one.
  3017. $this->transactionInnerLayer($suffix, $rollback, $ddl_statement);
  3018. $this->assertTrue($connection->inTransaction(), 'In transaction after calling nested transaction.');
  3019. if ($rollback) {
  3020. // Roll back the transaction, if requested.
  3021. // This rollback should propagate to the last savepoint.
  3022. $txn->rollback();
  3023. $this->assertTrue(($connection->transactionDepth() == $depth), 'Transaction has rolled back to the last savepoint after calling rollback().');
  3024. }
  3025. }
  3026. /**
  3027. * Helper method for transaction unit tests. This "inner layer" transaction
  3028. * is either used alone or nested inside of the "outer layer" transaction.
  3029. *
  3030. * @param $suffix
  3031. * Suffix to add to field values to differentiate tests.
  3032. * @param $rollback
  3033. * Whether or not to try rolling back the transaction when we're done.
  3034. * @param $ddl_statement
  3035. * Whether to execute a DDL statement during the transaction.
  3036. */
  3037. protected function transactionInnerLayer($suffix, $rollback = FALSE, $ddl_statement = FALSE) {
  3038. $connection = Database::getConnection();
  3039. $depth = $connection->transactionDepth();
  3040. // Start a transaction. If we're being called from ->transactionOuterLayer,
  3041. // then we're already in a transaction. Normally, that would make starting
  3042. // a transaction here dangerous, but the database API handles this problem
  3043. // for us by tracking the nesting and avoiding the danger.
  3044. $txn = db_transaction();
  3045. $depth2 = $connection->transactionDepth();
  3046. $this->assertTrue($depth < $depth2, 'Transaction depth is has increased with new transaction.');
  3047. // Insert a single row into the testing table.
  3048. db_insert('test')
  3049. ->fields(array(
  3050. 'name' => 'Daniel' . $suffix,
  3051. 'age' => '19',
  3052. ))
  3053. ->execute();
  3054. $this->assertTrue($connection->inTransaction(), 'In transaction inside nested transaction.');
  3055. if ($ddl_statement) {
  3056. $table = array(
  3057. 'fields' => array(
  3058. 'id' => array(
  3059. 'type' => 'serial',
  3060. 'unsigned' => TRUE,
  3061. 'not null' => TRUE,
  3062. ),
  3063. ),
  3064. 'primary key' => array('id'),
  3065. );
  3066. db_create_table('database_test_1', $table);
  3067. $this->assertTrue($connection->inTransaction(), 'In transaction inside nested transaction.');
  3068. }
  3069. if ($rollback) {
  3070. // Roll back the transaction, if requested.
  3071. // This rollback should propagate to the last savepoint.
  3072. $txn->rollback();
  3073. $this->assertTrue(($connection->transactionDepth() == $depth), 'Transaction has rolled back to the last savepoint after calling rollback().');
  3074. }
  3075. }
  3076. /**
  3077. * Test transaction rollback on a database that supports transactions.
  3078. *
  3079. * If the active connection does not support transactions, this test does nothing.
  3080. */
  3081. function testTransactionRollBackSupported() {
  3082. // This test won't work right if transactions are not supported.
  3083. if (!Database::getConnection()->supportsTransactions()) {
  3084. return;
  3085. }
  3086. try {
  3087. // Create two nested transactions. Roll back from the inner one.
  3088. $this->transactionOuterLayer('B', TRUE);
  3089. // Neither of the rows we inserted in the two transaction layers
  3090. // should be present in the tables post-rollback.
  3091. $saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'DavidB'))->fetchField();
  3092. $this->assertNotIdentical($saved_age, '24', 'Cannot retrieve DavidB row after commit.');
  3093. $saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'DanielB'))->fetchField();
  3094. $this->assertNotIdentical($saved_age, '19', 'Cannot retrieve DanielB row after commit.');
  3095. }
  3096. catch (Exception $e) {
  3097. $this->fail($e->getMessage());
  3098. }
  3099. }
  3100. /**
  3101. * Test transaction rollback on a database that does not support transactions.
  3102. *
  3103. * If the active driver supports transactions, this test does nothing.
  3104. */
  3105. function testTransactionRollBackNotSupported() {
  3106. // This test won't work right if transactions are supported.
  3107. if (Database::getConnection()->supportsTransactions()) {
  3108. return;
  3109. }
  3110. try {
  3111. // Create two nested transactions. Attempt to roll back from the inner one.
  3112. $this->transactionOuterLayer('B', TRUE);
  3113. // Because our current database claims to not support transactions,
  3114. // the inserted rows should be present despite the attempt to roll back.
  3115. $saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'DavidB'))->fetchField();
  3116. $this->assertIdentical($saved_age, '24', 'DavidB not rolled back, since transactions are not supported.');
  3117. $saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'DanielB'))->fetchField();
  3118. $this->assertIdentical($saved_age, '19', 'DanielB not rolled back, since transactions are not supported.');
  3119. }
  3120. catch (Exception $e) {
  3121. $this->fail($e->getMessage());
  3122. }
  3123. }
  3124. /**
  3125. * Test committed transaction.
  3126. *
  3127. * The behavior of this test should be identical for connections that support
  3128. * transactions and those that do not.
  3129. */
  3130. function testCommittedTransaction() {
  3131. try {
  3132. // Create two nested transactions. The changes should be committed.
  3133. $this->transactionOuterLayer('A');
  3134. // Because we committed, both of the inserted rows should be present.
  3135. $saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'DavidA'))->fetchField();
  3136. $this->assertIdentical($saved_age, '24', 'Can retrieve DavidA row after commit.');
  3137. $saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'DanielA'))->fetchField();
  3138. $this->assertIdentical($saved_age, '19', 'Can retrieve DanielA row after commit.');
  3139. }
  3140. catch (Exception $e) {
  3141. $this->fail($e->getMessage());
  3142. }
  3143. }
  3144. /**
  3145. * Test the compatibility of transactions with DDL statements.
  3146. */
  3147. function testTransactionWithDdlStatement() {
  3148. // First, test that a commit works normally, even with DDL statements.
  3149. $transaction = db_transaction();
  3150. $this->insertRow('row');
  3151. $this->executeDDLStatement();
  3152. unset($transaction);
  3153. $this->assertRowPresent('row');
  3154. // Even in different order.
  3155. $this->cleanUp();
  3156. $transaction = db_transaction();
  3157. $this->executeDDLStatement();
  3158. $this->insertRow('row');
  3159. unset($transaction);
  3160. $this->assertRowPresent('row');
  3161. // Even with stacking.
  3162. $this->cleanUp();
  3163. $transaction = db_transaction();
  3164. $transaction2 = db_transaction();
  3165. $this->executeDDLStatement();
  3166. unset($transaction2);
  3167. $transaction3 = db_transaction();
  3168. $this->insertRow('row');
  3169. unset($transaction3);
  3170. unset($transaction);
  3171. $this->assertRowPresent('row');
  3172. // A transaction after a DDL statement should still work the same.
  3173. $this->cleanUp();
  3174. $transaction = db_transaction();
  3175. $transaction2 = db_transaction();
  3176. $this->executeDDLStatement();
  3177. unset($transaction2);
  3178. $transaction3 = db_transaction();
  3179. $this->insertRow('row');
  3180. $transaction3->rollback();
  3181. unset($transaction3);
  3182. unset($transaction);
  3183. $this->assertRowAbsent('row');
  3184. // The behavior of a rollback depends on the type of database server.
  3185. if (Database::getConnection()->supportsTransactionalDDL()) {
  3186. // For database servers that support transactional DDL, a rollback
  3187. // of a transaction including DDL statements should be possible.
  3188. $this->cleanUp();
  3189. $transaction = db_transaction();
  3190. $this->insertRow('row');
  3191. $this->executeDDLStatement();
  3192. $transaction->rollback();
  3193. unset($transaction);
  3194. $this->assertRowAbsent('row');
  3195. // Including with stacking.
  3196. $this->cleanUp();
  3197. $transaction = db_transaction();
  3198. $transaction2 = db_transaction();
  3199. $this->executeDDLStatement();
  3200. unset($transaction2);
  3201. $transaction3 = db_transaction();
  3202. $this->insertRow('row');
  3203. unset($transaction3);
  3204. $transaction->rollback();
  3205. unset($transaction);
  3206. $this->assertRowAbsent('row');
  3207. }
  3208. else {
  3209. // For database servers that do not support transactional DDL,
  3210. // the DDL statement should commit the transaction stack.
  3211. $this->cleanUp();
  3212. $transaction = db_transaction();
  3213. $this->insertRow('row');
  3214. $this->executeDDLStatement();
  3215. set_error_handler(array($this, 'rollBackWithoutTransactionErrorHandler'));
  3216. try {
  3217. // Rollback the outer transaction.
  3218. $transaction->rollback();
  3219. // @see \DatabaseConnection_mysql::rollback()
  3220. if (PHP_VERSION_ID >= 80000) {
  3221. $this->fail('Rolling back a transaction containing DDL should produce a warning.');
  3222. }
  3223. }
  3224. catch (Exception $e) {
  3225. $this->assertEqual('Rollback attempted when there is no active transaction.', $e->getMessage());
  3226. }
  3227. restore_error_handler();
  3228. unset($transaction);
  3229. $this->assertRowPresent('row');
  3230. }
  3231. }
  3232. /**
  3233. * Special handling of "rollback without transaction" errors.
  3234. */
  3235. public function rollBackWithoutTransactionErrorHandler($error_level, $message, $filename, $line) {
  3236. // Throw an exception if this is a "rollback without transaction" error.
  3237. if (strpos($message, 'Rollback attempted when there is no active transaction.') !== FALSE ) {
  3238. throw new Exception('Rollback attempted when there is no active transaction.');
  3239. }
  3240. _drupal_error_handler($error_level, $message, $filename, $line);
  3241. }
  3242. /**
  3243. * Insert a single row into the testing table.
  3244. */
  3245. protected function insertRow($name) {
  3246. db_insert('test')
  3247. ->fields(array(
  3248. 'name' => $name,
  3249. ))
  3250. ->execute();
  3251. }
  3252. /**
  3253. * Execute a DDL statement.
  3254. */
  3255. protected function executeDDLStatement() {
  3256. static $count = 0;
  3257. $table = array(
  3258. 'fields' => array(
  3259. 'id' => array(
  3260. 'type' => 'serial',
  3261. 'unsigned' => TRUE,
  3262. 'not null' => TRUE,
  3263. ),
  3264. ),
  3265. 'primary key' => array('id'),
  3266. );
  3267. db_create_table('database_test_' . ++$count, $table);
  3268. }
  3269. /**
  3270. * Start over for a new test.
  3271. */
  3272. protected function cleanUp() {
  3273. db_truncate('test')
  3274. ->execute();
  3275. }
  3276. /**
  3277. * Assert that a given row is present in the test table.
  3278. *
  3279. * @param $name
  3280. * The name of the row.
  3281. * @param $message
  3282. * The message to log for the assertion.
  3283. */
  3284. function assertRowPresent($name, $message = NULL) {
  3285. if (!isset($message)) {
  3286. $message = format_string('Row %name is present.', array('%name' => $name));
  3287. }
  3288. $present = (boolean) db_query('SELECT 1 FROM {test} WHERE name = :name', array(':name' => $name))->fetchField();
  3289. return $this->assertTrue($present, $message);
  3290. }
  3291. /**
  3292. * Assert that a given row is absent from the test table.
  3293. *
  3294. * @param $name
  3295. * The name of the row.
  3296. * @param $message
  3297. * The message to log for the assertion.
  3298. */
  3299. function assertRowAbsent($name, $message = NULL) {
  3300. if (!isset($message)) {
  3301. $message = format_string('Row %name is absent.', array('%name' => $name));
  3302. }
  3303. $present = (boolean) db_query('SELECT 1 FROM {test} WHERE name = :name', array(':name' => $name))->fetchField();
  3304. return $this->assertFalse($present, $message);
  3305. }
  3306. /**
  3307. * Test transaction stacking and commit / rollback.
  3308. */
  3309. function testTransactionStacking() {
  3310. // This test won't work right if transactions are not supported.
  3311. if (!Database::getConnection()->supportsTransactions()) {
  3312. return;
  3313. }
  3314. $database = Database::getConnection();
  3315. // Standard case: pop the inner transaction before the outer transaction.
  3316. $transaction = db_transaction();
  3317. $this->insertRow('outer');
  3318. $transaction2 = db_transaction();
  3319. $this->insertRow('inner');
  3320. // Pop the inner transaction.
  3321. unset($transaction2);
  3322. $this->assertTrue($database->inTransaction(), 'Still in a transaction after popping the inner transaction');
  3323. // Pop the outer transaction.
  3324. unset($transaction);
  3325. $this->assertFalse($database->inTransaction(), 'Transaction closed after popping the outer transaction');
  3326. $this->assertRowPresent('outer');
  3327. $this->assertRowPresent('inner');
  3328. // Pop the transaction in a different order they have been pushed.
  3329. $this->cleanUp();
  3330. $transaction = db_transaction();
  3331. $this->insertRow('outer');
  3332. $transaction2 = db_transaction();
  3333. $this->insertRow('inner');
  3334. // Pop the outer transaction, nothing should happen.
  3335. unset($transaction);
  3336. $this->insertRow('inner-after-outer-commit');
  3337. $this->assertTrue($database->inTransaction(), 'Still in a transaction after popping the outer transaction');
  3338. // Pop the inner transaction, the whole transaction should commit.
  3339. unset($transaction2);
  3340. $this->assertFalse($database->inTransaction(), 'Transaction closed after popping the inner transaction');
  3341. $this->assertRowPresent('outer');
  3342. $this->assertRowPresent('inner');
  3343. $this->assertRowPresent('inner-after-outer-commit');
  3344. // Rollback the inner transaction.
  3345. $this->cleanUp();
  3346. $transaction = db_transaction();
  3347. $this->insertRow('outer');
  3348. $transaction2 = db_transaction();
  3349. $this->insertRow('inner');
  3350. // Now rollback the inner transaction.
  3351. $transaction2->rollback();
  3352. unset($transaction2);
  3353. $this->assertTrue($database->inTransaction(), 'Still in a transaction after popping the outer transaction');
  3354. // Pop the outer transaction, it should commit.
  3355. $this->insertRow('outer-after-inner-rollback');
  3356. unset($transaction);
  3357. $this->assertFalse($database->inTransaction(), 'Transaction closed after popping the inner transaction');
  3358. $this->assertRowPresent('outer');
  3359. $this->assertRowAbsent('inner');
  3360. $this->assertRowPresent('outer-after-inner-rollback');
  3361. // Rollback the inner transaction after committing the outer one.
  3362. $this->cleanUp();
  3363. $transaction = db_transaction();
  3364. $this->insertRow('outer');
  3365. $transaction2 = db_transaction();
  3366. $this->insertRow('inner');
  3367. // Pop the outer transaction, nothing should happen.
  3368. unset($transaction);
  3369. $this->assertTrue($database->inTransaction(), 'Still in a transaction after popping the outer transaction');
  3370. // Now rollback the inner transaction, it should rollback.
  3371. $transaction2->rollback();
  3372. unset($transaction2);
  3373. $this->assertFalse($database->inTransaction(), 'Transaction closed after popping the inner transaction');
  3374. $this->assertRowPresent('outer');
  3375. $this->assertRowAbsent('inner');
  3376. // Rollback the outer transaction while the inner transaction is active.
  3377. // In that case, an exception will be triggered because we cannot
  3378. // ensure that the final result will have any meaning.
  3379. $this->cleanUp();
  3380. $transaction = db_transaction();
  3381. $this->insertRow('outer');
  3382. $transaction2 = db_transaction();
  3383. $this->insertRow('inner');
  3384. $transaction3 = db_transaction();
  3385. $this->insertRow('inner2');
  3386. // Rollback the outer transaction.
  3387. try {
  3388. $transaction->rollback();
  3389. unset($transaction);
  3390. $this->fail('Rolling back the outer transaction while the inner transaction is active resulted in an exception.');
  3391. }
  3392. catch (DatabaseTransactionOutOfOrderException $e) {
  3393. $this->pass('Rolling back the outer transaction while the inner transaction is active resulted in an exception.');
  3394. }
  3395. $this->assertFalse($database->inTransaction(), 'No more in a transaction after rolling back the outer transaction');
  3396. // Try to commit one inner transaction.
  3397. unset($transaction3);
  3398. $this->pass('Trying to commit an inner transaction resulted in an exception.');
  3399. // Try to rollback one inner transaction.
  3400. try {
  3401. $transaction->rollback();
  3402. unset($transaction2);
  3403. $this->fail('Trying to commit an inner transaction resulted in an exception.');
  3404. }
  3405. catch (DatabaseTransactionNoActiveException $e) {
  3406. $this->pass('Trying to commit an inner transaction resulted in an exception.');
  3407. }
  3408. $this->assertRowAbsent('outer');
  3409. $this->assertRowAbsent('inner');
  3410. $this->assertRowAbsent('inner2');
  3411. }
  3412. }
  3413. /**
  3414. * Check the sequences API.
  3415. */
  3416. class DatabaseNextIdCase extends DrupalWebTestCase {
  3417. public static function getInfo() {
  3418. return array(
  3419. 'name' => 'Sequences API',
  3420. 'description' => 'Test the secondary sequences API.',
  3421. 'group' => 'Database',
  3422. );
  3423. }
  3424. /**
  3425. * Test that the sequences API work.
  3426. */
  3427. function testDbNextId() {
  3428. $first = db_next_id();
  3429. $second = db_next_id();
  3430. // We can test for exact increase in here because we know there is no
  3431. // other process operating on these tables -- normally we could only
  3432. // expect $second > $first.
  3433. $this->assertEqual($first + 1, $second, 'The second call from a sequence provides a number increased by one.');
  3434. $result = db_next_id(1000);
  3435. $this->assertEqual($result, 1001, 'Sequence provides a larger number than the existing ID.');
  3436. }
  3437. }
  3438. /**
  3439. * Tests the empty pseudo-statement class.
  3440. */
  3441. class DatabaseEmptyStatementTestCase extends DrupalWebTestCase {
  3442. public static function getInfo() {
  3443. return array(
  3444. 'name' => 'Empty statement',
  3445. 'description' => 'Test the empty pseudo-statement class.',
  3446. 'group' => 'Database',
  3447. );
  3448. }
  3449. /**
  3450. * Test that the empty result set behaves as empty.
  3451. */
  3452. function testEmpty() {
  3453. $result = new DatabaseStatementEmpty();
  3454. $this->assertTrue($result instanceof DatabaseStatementInterface, 'Class implements expected interface');
  3455. $this->assertNull($result->fetchObject(), 'Null result returned.');
  3456. }
  3457. /**
  3458. * Test that the empty result set iterates safely.
  3459. */
  3460. function testEmptyIteration() {
  3461. $result = new DatabaseStatementEmpty();
  3462. foreach ($result as $record) {
  3463. $this->fail('Iterating empty result set should not iterate.');
  3464. return;
  3465. }
  3466. $this->pass('Iterating empty result set skipped iteration.');
  3467. }
  3468. /**
  3469. * Test that the empty result set mass-fetches in an expected way.
  3470. */
  3471. function testEmptyFetchAll() {
  3472. $result = new DatabaseStatementEmpty();
  3473. $this->assertEqual($result->fetchAll(), array(), 'Empty array returned from empty result set.');
  3474. }
  3475. }
  3476. /**
  3477. * Tests management of database connections.
  3478. */
  3479. class ConnectionUnitTest extends DrupalUnitTestCase {
  3480. protected $key;
  3481. protected $target;
  3482. protected $monitor;
  3483. protected $originalCount;
  3484. protected $skipTest;
  3485. public static function getInfo() {
  3486. return array(
  3487. 'name' => 'Connection unit tests',
  3488. 'description' => 'Tests management of database connections.',
  3489. 'group' => 'Database',
  3490. );
  3491. }
  3492. function setUp() {
  3493. parent::setUp();
  3494. $this->key = 'default';
  3495. $this->originalTarget = 'default';
  3496. $this->target = 'DatabaseConnectionUnitTest';
  3497. // Determine whether the database driver is MySQL. If it is not, the test
  3498. // methods will not be executed.
  3499. // @todo Make this test driver-agnostic, or find a proper way to skip it.
  3500. // @see http://drupal.org/node/1273478
  3501. $connection_info = Database::getConnectionInfo('default');
  3502. $this->skipTest = (bool) ($connection_info['default']['driver'] != 'mysql');
  3503. if ($this->skipTest) {
  3504. // Insert an assertion to prevent Simpletest from interpreting the test
  3505. // as failure.
  3506. $this->pass('This test is only compatible with MySQL.');
  3507. }
  3508. // Create an additional connection to monitor the connections being opened
  3509. // and closed in this test.
  3510. // @see TestBase::changeDatabasePrefix()
  3511. $connection_info = Database::getConnectionInfo('default');
  3512. Database::addConnectionInfo('default', 'monitor', $connection_info['default']);
  3513. global $databases;
  3514. $databases['default']['monitor'] = $connection_info['default'];
  3515. $this->monitor = Database::getConnection('monitor');
  3516. }
  3517. /**
  3518. * Adds a new database connection info to Database.
  3519. */
  3520. protected function addConnection() {
  3521. // Add a new target to the connection, by cloning the current connection.
  3522. $connection_info = Database::getConnectionInfo($this->key);
  3523. Database::addConnectionInfo($this->key, $this->target, $connection_info[$this->originalTarget]);
  3524. // Verify that the new target exists.
  3525. $info = Database::getConnectionInfo($this->key);
  3526. // Note: Custom assertion message to not expose database credentials.
  3527. $this->assertIdentical($info[$this->target], $connection_info[$this->key], 'New connection info found.');
  3528. }
  3529. /**
  3530. * Returns the connection ID of the current test connection.
  3531. *
  3532. * @return integer
  3533. */
  3534. protected function getConnectionID() {
  3535. return (int) Database::getConnection($this->target, $this->key)->query('SELECT CONNECTION_ID()')->fetchField();
  3536. }
  3537. /**
  3538. * Asserts that a connection ID exists.
  3539. *
  3540. * @param integer $id
  3541. * The connection ID to verify.
  3542. */
  3543. protected function assertConnection($id) {
  3544. $list = $this->monitor->query('SHOW PROCESSLIST')->fetchAllKeyed(0, 0);
  3545. return $this->assertTrue(isset($list[$id]), format_string('Connection ID @id found.', array('@id' => $id)));
  3546. }
  3547. /**
  3548. * Asserts that a connection ID does not exist.
  3549. *
  3550. * @param integer $id
  3551. * The connection ID to verify.
  3552. */
  3553. protected function assertNoConnection($id) {
  3554. $list = $this->monitor->query('SHOW PROCESSLIST')->fetchAllKeyed(0, 0);
  3555. return $this->assertFalse(isset($list[$id]), format_string('Connection ID @id not found.', array('@id' => $id)));
  3556. }
  3557. /**
  3558. * Tests Database::closeConnection() without query.
  3559. *
  3560. * @todo getConnectionID() executes a query.
  3561. */
  3562. function testOpenClose() {
  3563. if ($this->skipTest) {
  3564. return;
  3565. }
  3566. // Add and open a new connection.
  3567. $this->addConnection();
  3568. $id = $this->getConnectionID();
  3569. Database::getConnection($this->target, $this->key);
  3570. // Verify that there is a new connection.
  3571. $this->assertConnection($id);
  3572. // Close the connection.
  3573. Database::closeConnection($this->target, $this->key);
  3574. // Wait 20ms to give the database engine sufficient time to react.
  3575. usleep(20000);
  3576. // Verify that we are back to the original connection count.
  3577. $this->assertNoConnection($id);
  3578. }
  3579. /**
  3580. * Tests Database::closeConnection() with a query.
  3581. */
  3582. function testOpenQueryClose() {
  3583. if ($this->skipTest) {
  3584. return;
  3585. }
  3586. // Add and open a new connection.
  3587. $this->addConnection();
  3588. $id = $this->getConnectionID();
  3589. Database::getConnection($this->target, $this->key);
  3590. // Verify that there is a new connection.
  3591. $this->assertConnection($id);
  3592. // Execute a query.
  3593. Database::getConnection($this->target, $this->key)->query('SHOW TABLES');
  3594. // Close the connection.
  3595. Database::closeConnection($this->target, $this->key);
  3596. // Wait 20ms to give the database engine sufficient time to react.
  3597. usleep(20000);
  3598. // Verify that we are back to the original connection count.
  3599. $this->assertNoConnection($id);
  3600. }
  3601. /**
  3602. * Tests Database::closeConnection() with a query and custom prefetch method.
  3603. */
  3604. function testOpenQueryPrefetchClose() {
  3605. if ($this->skipTest) {
  3606. return;
  3607. }
  3608. // Add and open a new connection.
  3609. $this->addConnection();
  3610. $id = $this->getConnectionID();
  3611. Database::getConnection($this->target, $this->key);
  3612. // Verify that there is a new connection.
  3613. $this->assertConnection($id);
  3614. // Execute a query.
  3615. Database::getConnection($this->target, $this->key)->query('SHOW TABLES')->fetchCol();
  3616. // Close the connection.
  3617. Database::closeConnection($this->target, $this->key);
  3618. // Wait 20ms to give the database engine sufficient time to react.
  3619. usleep(20000);
  3620. // Verify that we are back to the original connection count.
  3621. $this->assertNoConnection($id);
  3622. }
  3623. /**
  3624. * Tests Database::closeConnection() with a select query.
  3625. */
  3626. function testOpenSelectQueryClose() {
  3627. if ($this->skipTest) {
  3628. return;
  3629. }
  3630. // Add and open a new connection.
  3631. $this->addConnection();
  3632. $id = $this->getConnectionID();
  3633. Database::getConnection($this->target, $this->key);
  3634. // Verify that there is a new connection.
  3635. $this->assertConnection($id);
  3636. // Create a table.
  3637. $name = 'foo';
  3638. Database::getConnection($this->target, $this->key)->schema()->createTable($name, array(
  3639. 'fields' => array(
  3640. 'name' => array(
  3641. 'type' => 'varchar',
  3642. 'length' => 255,
  3643. ),
  3644. ),
  3645. ));
  3646. // Execute a query.
  3647. Database::getConnection($this->target, $this->key)->select('foo', 'f')
  3648. ->fields('f', array('name'))
  3649. ->execute()
  3650. ->fetchAll();
  3651. // Drop the table.
  3652. Database::getConnection($this->target, $this->key)->schema()->dropTable($name);
  3653. // Close the connection.
  3654. Database::closeConnection($this->target, $this->key);
  3655. // Wait 20ms to give the database engine sufficient time to react.
  3656. usleep(20000);
  3657. // Verify that we are back to the original connection count.
  3658. $this->assertNoConnection($id);
  3659. }
  3660. }
  3661. /**
  3662. * Test reserved keyword handling (introduced for MySQL 8+)
  3663. */
  3664. class DatabaseReservedKeywordTestCase extends DatabaseTestCase {
  3665. public static function getInfo() {
  3666. return array(
  3667. 'name' => 'Reserved Keywords',
  3668. 'description' => 'Test handling of reserved keywords.',
  3669. 'group' => 'Database',
  3670. );
  3671. }
  3672. function setUp() {
  3673. parent::setUp('database_test');
  3674. }
  3675. public function testTableNameQuoting() {
  3676. // Test db_query with {table} pattern.
  3677. $record = db_query('SELECT * FROM {system} LIMIT 1')->fetchObject();
  3678. $this->assertTrue(isset($record->filename), 'Successfully queried the {system} table.');
  3679. $connection = Database::getConnection()->getConnectionOptions();
  3680. if ($connection['driver'] === 'sqlite') {
  3681. // In SQLite simpletest's prefixed db tables exist in their own schema
  3682. // (e.g. simpletest124904.system), so we cannot test the schema.{table}
  3683. // syntax here as the table name will have the schema name prepended to it
  3684. // when prefixes are processed.
  3685. $this->assert(TRUE, 'Skipping schema.{system} test for SQLite.');
  3686. }
  3687. else {
  3688. $database = $connection['database'];
  3689. // Test db_query with schema.{table} pattern
  3690. db_query('SELECT * FROM ' . $database . '.{system} LIMIT 1')->fetchObject();
  3691. $this->assertTrue(isset($record->filename), 'Successfully queried the schema.{system} table.');
  3692. }
  3693. }
  3694. public function testSelectReservedWordTableCount() {
  3695. $rows = db_select('virtual')
  3696. ->countQuery()
  3697. ->execute()
  3698. ->fetchField();
  3699. $this->assertEqual($rows, 1, 'Successful count query on a table with a reserved name.');
  3700. }
  3701. public function testSelectReservedWordTableSpecificField() {
  3702. $record = db_select('virtual')
  3703. ->fields('virtual', array('function'))
  3704. ->execute()
  3705. ->fetchAssoc();
  3706. $this->assertEqual($record['function'], 'Function value 1', 'Successfully read a field from a table with a name and column which are reserved words.');
  3707. }
  3708. public function testSelectReservedWordTableAllFields() {
  3709. $record = db_select('virtual')
  3710. ->fields('virtual')
  3711. ->execute()
  3712. ->fetchAssoc();
  3713. $this->assertEqual($record['function'], 'Function value 1', 'Successful all_fields query from a table with a name and column which are reserved words.');
  3714. }
  3715. public function testSelectReservedWordAliasCount() {
  3716. $rows = db_select('test', 'character')
  3717. ->countQuery()
  3718. ->execute()
  3719. ->fetchField();
  3720. $this->assertEqual($rows, 4, 'Successful count query using an alias which is a reserved word.');
  3721. }
  3722. public function testSelectReservedWordAliasSpecificFields() {
  3723. $record = db_select('test', 'high_priority')
  3724. ->fields('high_priority', array('name'))
  3725. ->condition('age', 27)
  3726. ->execute()->fetchAssoc();
  3727. $this->assertEqual($record['name'], 'George', 'Successful query using an alias which is a reserved word.');
  3728. }
  3729. public function testSelectReservedWordAliasAllFields() {
  3730. $record = db_select('test', 'high_priority')
  3731. ->fields('high_priority')
  3732. ->condition('age', 27)
  3733. ->execute()->fetchAssoc();
  3734. $this->assertEqual($record['name'], 'George', 'Successful all_fields query using an alias which is a reserved word.');
  3735. }
  3736. public function testInsertReservedWordTable() {
  3737. $num_records_before = db_query('SELECT COUNT(*) FROM {virtual}')->fetchField();
  3738. db_insert('virtual')
  3739. ->fields(array(
  3740. 'function' => 'Inserted function',
  3741. ))
  3742. ->execute();
  3743. $num_records_after = db_query('SELECT COUNT(*) FROM {virtual}')->fetchField();
  3744. $this->assertIdentical($num_records_before + 1, (int) $num_records_after, 'Successful insert into a table with a name and column which are reserved words.');
  3745. }
  3746. public function testDeleteReservedWordTable() {
  3747. $delete = db_delete('virtual')
  3748. ->condition('function', 'Function value 1');
  3749. $num_deleted = $delete->execute();
  3750. $this->assertEqual($num_deleted, 1, "Deleted 1 record from a table with a name and column which are reserved words..");
  3751. }
  3752. function testTruncateReservedWordTable() {
  3753. db_truncate('virtual')->execute();
  3754. $num_records_after = db_query("SELECT COUNT(*) FROM {virtual}")->fetchField();
  3755. $this->assertEqual(0, $num_records_after, 'Truncated a table with a reserved name.');
  3756. }
  3757. function testUpdateReservedWordTable() {
  3758. $num_updated = db_update('virtual')
  3759. ->fields(array('function' => 'Updated function'))
  3760. ->execute();
  3761. $this->assertIdentical($num_updated, 1, 'Updated 1 record in a table with a name and column which are reserved words.');
  3762. }
  3763. function testMergeReservedWordTable() {
  3764. $key = db_query('SELECT id FROM {virtual} LIMIT 1')->fetchField();
  3765. $num_records_before = db_query('SELECT COUNT(*) FROM {virtual}')->fetchField();
  3766. db_merge('virtual')
  3767. ->key(array('id' => $key))
  3768. ->fields(array('function' => 'Merged function'))
  3769. ->execute();
  3770. $num_records_after = db_query('SELECT COUNT(*) FROM {virtual}')->fetchField();
  3771. $this->assertIdentical($num_records_before, $num_records_after, 'Successful merge query on a table with a name and column which are reserved words.');
  3772. }
  3773. }
  3774. /**
  3775. * Test table prefix handling.
  3776. */
  3777. class DatabaseTablePrefixTestCase extends DatabaseTestCase {
  3778. public static function getInfo() {
  3779. return array(
  3780. 'name' => 'Table prefixes',
  3781. 'description' => 'Test handling of table prefixes.',
  3782. 'group' => 'Database',
  3783. );
  3784. }
  3785. public function testSchemaDotTablePrefixes() {
  3786. // Get a copy of the default connection options.
  3787. $db = Database::getConnection('default', 'default');
  3788. $connection_options = $db->getConnectionOptions();
  3789. if ($connection_options['driver'] === 'sqlite') {
  3790. // In SQLite simpletest's prefixed db tables exist in their own schema
  3791. // (e.g. simpletest124904.system), so we cannot test the schema.table
  3792. // prefix syntax here.
  3793. $this->assert(TRUE, 'Skipping schema.table prefixed tables test for SQLite.');
  3794. return;
  3795. }
  3796. $db_name = $connection_options['database'];
  3797. // This prefix is usually something like simpletest12345
  3798. $test_prefix = $connection_options['prefix']['default'];
  3799. // Set up a new connection with table prefixes in the form "schema.table"
  3800. $prefixed = $connection_options;
  3801. $prefixed['prefix'] = array(
  3802. 'default' => $test_prefix,
  3803. 'users' => $db_name . '.' . $test_prefix,
  3804. 'role' => $db_name . '.' . $test_prefix,
  3805. );
  3806. Database::addConnectionInfo('default', 'prefixed', $prefixed);
  3807. // Test that the prefixed database connection can query the prefixed tables.
  3808. $num_users_prefixed = Database::getConnection('prefixed', 'default')->query('SELECT COUNT(1) FROM {users}')->fetchField();
  3809. $this->assertTrue((int) $num_users_prefixed > 0, 'Successfully queried the users table using a schema.table prefix');
  3810. $num_users_default = Database::getConnection('default', 'default')->query('SELECT COUNT(1) FROM {users}')->fetchField();
  3811. $this->assertEqual($num_users_default, $num_users_prefixed, 'Verified results of query using a connection with schema.table prefixed tables');
  3812. }
  3813. }