FormHelperTest.php 183 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @since CakePHP(tm) v 1.2.0.4206
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\View\Helper;
  18. use Cake\Controller\Controller;
  19. use Cake\Core\App;
  20. use Cake\Core\Configure;
  21. use Cake\Core\Plugin;
  22. use Cake\Network\Request;
  23. use Cake\ORM\Entity;
  24. use Cake\ORM\Table;
  25. use Cake\ORM\TableRegistry;
  26. use Cake\Routing\Router;
  27. use Cake\TestSuite\TestCase;
  28. use Cake\Utility\Security;
  29. use Cake\View\Helper\FormHelper;
  30. use Cake\View\Helper\HtmlHelper;
  31. use Cake\View\View;
  32. /**
  33. * Test stub.
  34. */
  35. class Article extends Entity {
  36. }
  37. /**
  38. * ContactTestController class
  39. *
  40. */
  41. class ContactTestController extends Controller {
  42. /**
  43. * uses property
  44. *
  45. * @var mixed null
  46. */
  47. public $uses = null;
  48. }
  49. /**
  50. * Contact class
  51. *
  52. */
  53. class ContactsTable extends Table {
  54. /**
  55. * Default schema
  56. *
  57. * @var array
  58. */
  59. protected $_schema = array(
  60. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  61. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  62. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  63. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  64. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  65. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  66. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  67. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  68. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null),
  69. '_constraints' => array('primary' => ['type' => 'primary', 'columns' => ['id']])
  70. );
  71. /**
  72. * validate property
  73. *
  74. * @var array
  75. */
  76. public $validate = array(
  77. 'non_existing' => array(),
  78. 'idontexist' => array(),
  79. 'imrequired' => array('_allowEmpty' => false, array('rule' => array('between', 5, 30))),
  80. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  81. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  82. 'imrequiredonboth' => array(
  83. 'required' => array('rule' => 'alphaNumeric'),
  84. ),
  85. 'string_required' => 'notEmpty',
  86. 'imalsorequired' => array('_allowEmpty' => false, array('rule' => 'alphaNumeric')),
  87. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  88. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  89. 'imnotrequired' => array(
  90. '_allowEmpty' => true,
  91. array('rule' => 'alphaNumeric')
  92. ),
  93. 'imalsonotrequired' => array(
  94. '_allowEmpty' => true,
  95. 'alpha' => array('rule' => 'alphaNumeric'),
  96. 'between' => array('rule' => array('between', 5, 30)),
  97. ),
  98. 'imnotrequiredeither' => array(
  99. '_allowEmpty' => true,
  100. array('rule' => array('between', 5, 30))
  101. ),
  102. 'iamrequiredalways' => array(
  103. 'email' => array('rule' => 'email'),
  104. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  105. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  106. ),
  107. 'boolean_field' => array('rule' => 'boolean')
  108. );
  109. /**
  110. * hasAndBelongsToMany property
  111. *
  112. * @var array
  113. */
  114. public $hasAndBelongsToMany = array(
  115. 'ContactTag' => array(
  116. 'className' => 'Cake\Test\TestCase\View\Helper\ContactTag',
  117. 'with' => 'Cake\Test\TestCase\View\Helper\ContactTagsContact'
  118. )
  119. );
  120. /**
  121. * hasAndBelongsToMany property
  122. *
  123. * @var array
  124. */
  125. public $belongsTo = array(
  126. 'User' => array('className' => 'UserForm'
  127. ));
  128. /**
  129. * Initializes the schema and validation rules.
  130. *
  131. * @return void
  132. */
  133. public function initialize(array $config) {
  134. $this->schema($this->_schema);
  135. }
  136. }
  137. /**
  138. * ContactTagsContact class
  139. *
  140. */
  141. class ContactTagsContactsTable extends Table {
  142. /**
  143. * Default schema
  144. *
  145. * @var array
  146. */
  147. protected $_schema = array(
  148. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  149. 'contact_tag_id' => array(
  150. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  151. )
  152. );
  153. /**
  154. * schema method
  155. *
  156. * @return void
  157. */
  158. public function setSchema($schema) {
  159. $this->_schema = $schema;
  160. }
  161. }
  162. /**
  163. * ContactNonStandardPk class
  164. *
  165. */
  166. class ContactNonStandardPk extends ContactsTable {
  167. /**
  168. * primaryKey property
  169. *
  170. * @var string
  171. */
  172. public $primaryKey = 'pk';
  173. /**
  174. * schema method
  175. *
  176. * @return void
  177. */
  178. public function schema($field = false) {
  179. $this->_schema = parent::schema();
  180. $this->_schema['pk'] = $this->_schema['id'];
  181. unset($this->_schema['id']);
  182. return $this->_schema;
  183. }
  184. }
  185. /**
  186. * ContactTag class
  187. *
  188. */
  189. class ContactTagsTable extends Table {
  190. /**
  191. * schema definition
  192. *
  193. * @var array
  194. */
  195. protected $_schema = array(
  196. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  197. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  198. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  199. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  200. );
  201. }
  202. /**
  203. * UserForm class
  204. *
  205. */
  206. class UserFormsTable extends Table {
  207. /**
  208. * hasMany property
  209. *
  210. * @var array
  211. */
  212. public $hasMany = array(
  213. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  214. ));
  215. /**
  216. * schema definition
  217. *
  218. * @var array
  219. */
  220. protected $_schema = array(
  221. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  222. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  223. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  224. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  225. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  226. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  227. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  228. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  229. );
  230. }
  231. /**
  232. * OpenidUrl class
  233. *
  234. */
  235. class OpenidUrlsTable extends Table {
  236. /**
  237. * belongsTo property
  238. *
  239. * @var array
  240. */
  241. public $belongsTo = array('UserForm' => array(
  242. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  243. ));
  244. /**
  245. * validate property
  246. *
  247. * @var array
  248. */
  249. public $validate = array('openid_not_registered' => array());
  250. /**
  251. * schema method
  252. *
  253. * @var array
  254. */
  255. protected $_schema = array(
  256. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  257. 'user_form_id' => array(
  258. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  259. ),
  260. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  261. );
  262. /**
  263. * beforeValidate method
  264. *
  265. * @return void
  266. */
  267. public function beforeValidate($options = array()) {
  268. $this->invalidate('openid_not_registered');
  269. return true;
  270. }
  271. }
  272. /**
  273. * ValidateUser class
  274. *
  275. */
  276. class ValidateUsersTable extends Table {
  277. /**
  278. * hasOne property
  279. *
  280. * @var array
  281. */
  282. public $hasOne = array('ValidateProfile' => array(
  283. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  284. ));
  285. /**
  286. * schema method
  287. *
  288. * @var array
  289. */
  290. protected $_schema = array(
  291. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  292. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  293. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  294. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  295. 'cost_decimal' => array('type' => 'decimal', 'null' => false, 'length' => '6,3'),
  296. 'ratio' => array('type' => 'decimal', 'null' => false, 'length' => '10,6'),
  297. 'population' => array('type' => 'decimal', 'null' => false, 'length' => '15,0'),
  298. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  299. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  300. '_constraints' => array('primary' => ['type' => 'primary', 'columns' => ['id']])
  301. );
  302. /**
  303. * Initializes the schema
  304. *
  305. * @return void
  306. */
  307. public function initialize(array $config) {
  308. $this->schema($this->_schema);
  309. }
  310. /**
  311. * beforeValidate method
  312. *
  313. * @param array $options
  314. * @return void
  315. */
  316. public function beforeValidate($options = array()) {
  317. $this->invalidate('email');
  318. return false;
  319. }
  320. }
  321. /**
  322. * ValidateProfile class
  323. *
  324. */
  325. class ValidateProfilesTable extends Table {
  326. /**
  327. * useTable property
  328. *
  329. * @var boolean
  330. */
  331. public $useTable = false;
  332. /**
  333. * schema property
  334. *
  335. * @var array
  336. */
  337. protected $_schema = array(
  338. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  339. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  340. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  341. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  342. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  343. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  344. );
  345. /**
  346. * hasOne property
  347. *
  348. * @var array
  349. */
  350. public $hasOne = array('ValidateItem' => array(
  351. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  352. ));
  353. /**
  354. * belongsTo property
  355. *
  356. * @var array
  357. */
  358. public $belongsTo = array('ValidateUser' => array(
  359. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  360. ));
  361. /**
  362. * beforeValidate method
  363. *
  364. * @return void
  365. */
  366. public function beforeValidate($options = array()) {
  367. $this->invalidate('full_name');
  368. $this->invalidate('city');
  369. return false;
  370. }
  371. }
  372. /**
  373. * ValidateItem class
  374. *
  375. */
  376. class ValidateItemsTable extends Table {
  377. /**
  378. * schema property
  379. *
  380. * @var array
  381. */
  382. protected $_schema = array(
  383. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  384. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  385. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  386. 'description' => array(
  387. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  388. ),
  389. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  390. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  391. );
  392. /**
  393. * belongsTo property
  394. *
  395. * @var array
  396. */
  397. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  398. /**
  399. * beforeValidate method
  400. *
  401. * @return void
  402. */
  403. public function beforeValidate($options = array()) {
  404. $this->invalidate('description');
  405. return false;
  406. }
  407. }
  408. /**
  409. * TestMail class
  410. *
  411. */
  412. class TestMailsTable extends Table {
  413. }
  414. /**
  415. * FormHelperTest class
  416. *
  417. * @property FormHelper $Form
  418. */
  419. class FormHelperTest extends TestCase {
  420. /**
  421. * Fixtures to be used
  422. *
  423. * @var array
  424. */
  425. public $fixtures = array('core.article');
  426. /**
  427. * Do not load the fixtures by default
  428. *
  429. * @var boolean
  430. */
  431. public $autoFixtures = false;
  432. /**
  433. * setUp method
  434. *
  435. * @return void
  436. */
  437. public function setUp() {
  438. parent::setUp();
  439. Configure::write('Config.language', 'eng');
  440. Configure::write('App.base', '');
  441. Configure::write('App.namespace', 'Cake\Test\TestCase\View\Helper');
  442. Configure::delete('Asset');
  443. $this->View = new View(null);
  444. $this->Form = new FormHelper($this->View);
  445. $this->Form->Html = new HtmlHelper($this->View);
  446. $this->Form->request = new Request('articles/add');
  447. $this->Form->request->here = '/articles/add';
  448. $this->Form->request['controller'] = 'articles';
  449. $this->Form->request['action'] = 'add';
  450. $this->Form->request->webroot = '';
  451. $this->Form->request->base = '';
  452. $this->dateRegex = array(
  453. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  454. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  455. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  456. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  457. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  458. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  459. );
  460. $this->article = [
  461. 'schema' => [
  462. 'id' => ['type' => 'integer'],
  463. 'author_id' => ['type' => 'integer', 'null' => true],
  464. 'title' => ['type' => 'string', 'null' => true],
  465. 'body' => 'text',
  466. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  467. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  468. ],
  469. 'required' => [
  470. 'author_id' => true,
  471. 'title' => true,
  472. ]
  473. ];
  474. Configure::write('Security.salt', 'foo!');
  475. Router::connect('/:controller', array('action' => 'index'));
  476. Router::connect('/:controller/:action/*');
  477. }
  478. /**
  479. * tearDown method
  480. *
  481. * @return void
  482. */
  483. public function tearDown() {
  484. parent::tearDown();
  485. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  486. TableRegistry::clear();
  487. }
  488. /**
  489. * Test registering a new widget class and rendering it.
  490. *
  491. * @return void
  492. */
  493. public function testAddWidgetAndRenderWidget() {
  494. $data = [
  495. 'val' => 1
  496. ];
  497. $mock = $this->getMock('Cake\View\Widget\WidgetInterface');
  498. $this->assertNull($this->Form->addWidget('test', $mock));
  499. $mock->expects($this->once())
  500. ->method('render')
  501. ->with($data)
  502. ->will($this->returnValue('HTML'));
  503. $result = $this->Form->widget('test', $data);
  504. $this->assertEquals('HTML', $result);
  505. }
  506. /**
  507. * Test registering an invalid widget class.
  508. *
  509. * @expectedException \RuntimeException
  510. * @return void
  511. */
  512. public function testAddWidgetInvalid() {
  513. $mock = new \StdClass();
  514. $this->Form->addWidget('test', $mock);
  515. $this->Form->widget('test');
  516. }
  517. /**
  518. * Test adding a new context class.
  519. *
  520. * @return void
  521. */
  522. public function testAddContextProvider() {
  523. $context = 'My data';
  524. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  525. $this->assertInstanceOf('Cake\Network\Request', $request);
  526. $this->assertEquals($context, $data['entity']);
  527. return $this->getMock('Cake\View\Form\ContextInterface');
  528. });
  529. $this->Form->create($context);
  530. }
  531. /**
  532. * Test adding an invalid context class.
  533. *
  534. * @expectedException RuntimeException
  535. * @expectedExceptionMessage Context objects must implement Cake\View\Form\ContextInterface
  536. * @return void
  537. */
  538. public function testAddContextProviderInvalid() {
  539. $context = 'My data';
  540. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  541. return new \StdClass();
  542. });
  543. $this->Form->create($context);
  544. }
  545. /**
  546. * Provides context options for create().
  547. *
  548. * @return array
  549. */
  550. public function contextSelectionProvider() {
  551. $entity = new Article();
  552. $collection = $this->getMock('Cake\Collection\Collection', ['extract'], [[$entity]]);
  553. $data = [
  554. 'schema' => [
  555. 'title' => ['type' => 'string']
  556. ]
  557. ];
  558. return [
  559. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  560. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  561. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  562. 'none' => [null, 'Cake\View\Form\NullContext'],
  563. 'false' => [false, 'Cake\View\Form\NullContext'],
  564. ];
  565. }
  566. /**
  567. * Test default context selection in create()
  568. *
  569. * @dataProvider contextSelectionProvider
  570. * @return void
  571. */
  572. public function testCreateContextSelectionBuiltIn($data, $class) {
  573. $this->loadFixtures('Article');
  574. $this->Form->create($data);
  575. $this->assertInstanceOf($class, $this->Form->context());
  576. }
  577. /**
  578. * Data provider for type option.
  579. *
  580. * @return array
  581. */
  582. public static function requestTypeProvider() {
  583. return [
  584. // type, method, override
  585. ['post', 'post', 'POST'],
  586. ['put', 'post', 'PUT'],
  587. ['patch', 'post', 'PATCH'],
  588. ['delete', 'post', 'DELETE'],
  589. ];
  590. }
  591. /**
  592. * Test creating file forms.
  593. *
  594. * @return void
  595. */
  596. public function testCreateFile() {
  597. $encoding = strtolower(Configure::read('App.encoding'));
  598. $result = $this->Form->create(false, array('type' => 'file'));
  599. $expected = array(
  600. 'form' => array(
  601. 'method' => 'post', 'action' => '/articles/add',
  602. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  603. ),
  604. 'div' => array('style' => 'display:none;'),
  605. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  606. '/div'
  607. );
  608. $this->assertTags($result, $expected);
  609. }
  610. /**
  611. * Test creating GET forms.
  612. *
  613. * @return void
  614. */
  615. public function testCreateGet() {
  616. $encoding = strtolower(Configure::read('App.encoding'));
  617. $result = $this->Form->create(false, array('type' => 'get'));
  618. $expected = array('form' => array(
  619. 'method' => 'get', 'action' => '/articles/add',
  620. 'accept-charset' => $encoding
  621. ));
  622. $this->assertTags($result, $expected);
  623. }
  624. /**
  625. * test the create() method
  626. *
  627. * @dataProvider requestTypeProvider
  628. * @return void
  629. */
  630. public function testCreateTypeOptions($type, $method, $override) {
  631. $encoding = strtolower(Configure::read('App.encoding'));
  632. $result = $this->Form->create(false, array('type' => $type));
  633. $expected = array(
  634. 'form' => array(
  635. 'method' => $method, 'action' => '/articles/add',
  636. 'accept-charset' => $encoding
  637. ),
  638. 'div' => array('style' => 'display:none;'),
  639. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => $override),
  640. '/div'
  641. );
  642. $this->assertTags($result, $expected);
  643. }
  644. /**
  645. * Test opening a form for an update operation.
  646. *
  647. * @return void
  648. */
  649. public function testCreateUpdateForm() {
  650. $encoding = strtolower(Configure::read('App.encoding'));
  651. $this->Form->request->here = '/articles/edit/1';
  652. $this->Form->request['action'] = 'edit';
  653. $this->article['defaults']['id'] = 1;
  654. $result = $this->Form->create($this->article);
  655. $expected = array(
  656. 'form' => array(
  657. 'method' => 'post', 'action' => '/articles/edit/1',
  658. 'accept-charset' => $encoding
  659. ),
  660. 'div' => array('style' => 'display:none;'),
  661. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  662. '/div'
  663. );
  664. $this->assertTags($result, $expected);
  665. }
  666. /**
  667. * Test the onsubmit option for create()
  668. *
  669. * @return void
  670. */
  671. public function testCreateOnSubmit() {
  672. $this->Form->request->data = [];
  673. $this->Form->request['controller'] = 'articles';
  674. $result = $this->Form->create($this->article, ['url' => ['action' => 'index', 'param'], 'default' => false]);
  675. $expected = array(
  676. 'form' => array(
  677. 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/articles/index/param',
  678. 'accept-charset' => 'utf-8'
  679. ),
  680. 'div' => array('style' => 'display:none;'),
  681. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  682. '/div'
  683. );
  684. $this->assertTags($result, $expected);
  685. $this->Form->request->data = [];
  686. $this->Form->request['controller'] = 'articles';
  687. $result = $this->Form->create($this->article, array(
  688. 'url' => array('action' => 'index', 'param'),
  689. 'default' => false,
  690. 'onsubmit' => 'someFunction();'
  691. ));
  692. $expected = array(
  693. 'form' => array(
  694. 'method' => 'post',
  695. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  696. 'action' => '/articles/index/param',
  697. 'accept-charset' => 'utf-8'
  698. ),
  699. 'div' => array('style' => 'display:none;'),
  700. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  701. '/div'
  702. );
  703. $this->assertTags($result, $expected);
  704. }
  705. /**
  706. * test create() with automatic url generation
  707. *
  708. * @return void
  709. */
  710. public function testCreateAutoUrl() {
  711. $encoding = strtolower(Configure::read('App.encoding'));
  712. $this->Form->request['action'] = 'delete';
  713. $this->Form->request->here = '/articles/delete/10';
  714. $this->Form->request->base = '';
  715. $result = $this->Form->create($this->article);
  716. $expected = array(
  717. 'form' => array(
  718. 'method' => 'post', 'action' => '/articles/delete/10',
  719. 'accept-charset' => $encoding
  720. ),
  721. 'div' => array('style' => 'display:none;'),
  722. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  723. '/div'
  724. );
  725. $this->assertTags($result, $expected);
  726. $this->article['defaults'] = ['id' => 1];
  727. $this->Form->request->here = '/articles/edit/1';
  728. $this->Form->request['action'] = 'delete';
  729. $result = $this->Form->create($this->article, ['action' => 'edit']);
  730. $expected = array(
  731. 'form' => array(
  732. 'method' => 'post',
  733. 'action' => '/articles/edit/1',
  734. 'accept-charset' => $encoding
  735. ),
  736. 'div' => array('style' => 'display:none;'),
  737. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  738. '/div'
  739. );
  740. $this->assertTags($result, $expected);
  741. $this->Form->request['action'] = 'add';
  742. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  743. $expected = array(
  744. 'form' => array(
  745. 'method' => 'post',
  746. 'action' => '/articles/publish/1',
  747. 'accept-charset' => $encoding
  748. ),
  749. 'div' => array('style' => 'display:none;'),
  750. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  751. '/div'
  752. );
  753. $this->assertTags($result, $expected);
  754. $result = $this->Form->create($this->article, array('url' => '/articles/publish'));
  755. $expected = array(
  756. 'form' => array('method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding),
  757. 'div' => array('style' => 'display:none;'),
  758. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  759. '/div'
  760. );
  761. $this->assertTags($result, $expected);
  762. $this->Form->request['controller'] = 'pages';
  763. $result = $this->Form->create($this->article, array('action' => 'signup'));
  764. $expected = array(
  765. 'form' => array(
  766. 'method' => 'post', 'action' => '/pages/signup/1',
  767. 'accept-charset' => $encoding
  768. ),
  769. 'div' => array('style' => 'display:none;'),
  770. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  771. '/div'
  772. );
  773. $this->assertTags($result, $expected);
  774. }
  775. /**
  776. * test create() with a custom route
  777. *
  778. * @return void
  779. */
  780. public function testCreateCustomRoute() {
  781. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  782. $encoding = strtolower(Configure::read('App.encoding'));
  783. $this->Form->request['controller'] = 'users';
  784. $result = $this->Form->create(false, array('action' => 'login'));
  785. $expected = array(
  786. 'form' => array(
  787. 'method' => 'post', 'action' => '/login',
  788. 'accept-charset' => $encoding
  789. ),
  790. 'div' => array('style' => 'display:none;'),
  791. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  792. '/div'
  793. );
  794. $this->assertTags($result, $expected);
  795. }
  796. /**
  797. * test automatic accept-charset overriding
  798. *
  799. * @return void
  800. */
  801. public function testCreateWithAcceptCharset() {
  802. $result = $this->Form->create($this->article, array(
  803. 'type' => 'post', 'action' => 'index', 'encoding' => 'iso-8859-1'
  804. )
  805. );
  806. $expected = array(
  807. 'form' => array(
  808. 'method' => 'post', 'action' => '/articles',
  809. 'accept-charset' => 'iso-8859-1'
  810. ),
  811. 'div' => array('style' => 'display:none;'),
  812. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  813. '/div'
  814. );
  815. $this->assertTags($result, $expected);
  816. }
  817. /**
  818. * Test base form URL when url param is passed with multiple parameters (&)
  819. *
  820. */
  821. public function testCreateQuerystringrequest() {
  822. $encoding = strtolower(Configure::read('App.encoding'));
  823. $result = $this->Form->create($this->article, array(
  824. 'type' => 'post',
  825. 'escape' => false,
  826. 'url' => array(
  827. 'controller' => 'controller',
  828. 'action' => 'action',
  829. '?' => array('param1' => 'value1', 'param2' => 'value2')
  830. )
  831. ));
  832. $expected = array(
  833. 'form' => array(
  834. 'method' => 'post',
  835. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  836. 'accept-charset' => $encoding
  837. ),
  838. 'div' => array('style' => 'display:none;'),
  839. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  840. '/div'
  841. );
  842. $this->assertTags($result, $expected);
  843. $result = $this->Form->create($this->article, array(
  844. 'type' => 'post',
  845. 'url' => array(
  846. 'controller' => 'controller',
  847. 'action' => 'action',
  848. '?' => array('param1' => 'value1', 'param2' => 'value2')
  849. )
  850. ));
  851. $this->assertTags($result, $expected);
  852. }
  853. /**
  854. * test that create() doesn't cause errors by multiple id's being in the primary key
  855. * as could happen with multiple select or checkboxes.
  856. *
  857. * @return void
  858. */
  859. public function testCreateWithMultipleIdInData() {
  860. $encoding = strtolower(Configure::read('App.encoding'));
  861. $this->Form->request->data['Article']['id'] = array(1, 2);
  862. $result = $this->Form->create($this->article);
  863. $expected = array(
  864. 'form' => array(
  865. 'method' => 'post',
  866. 'action' => '/articles/add',
  867. 'accept-charset' => $encoding
  868. ),
  869. 'div' => array('style' => 'display:none;'),
  870. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  871. '/div'
  872. );
  873. $this->assertTags($result, $expected);
  874. }
  875. /**
  876. * test that create() doesn't add in extra passed params.
  877. *
  878. * @return void
  879. */
  880. public function testCreatePassedArgs() {
  881. $encoding = strtolower(Configure::read('App.encoding'));
  882. $this->Form->request->data['Article']['id'] = 1;
  883. $result = $this->Form->create($this->article, array(
  884. 'type' => 'post',
  885. 'escape' => false,
  886. 'url' => array(
  887. 'action' => 'edit',
  888. 'myparam'
  889. )
  890. ));
  891. $expected = array(
  892. 'form' => array(
  893. 'method' => 'post',
  894. 'action' => '/articles/edit/myparam',
  895. 'accept-charset' => $encoding
  896. ),
  897. 'div' => array('style' => 'display:none;'),
  898. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  899. '/div'
  900. );
  901. $this->assertTags($result, $expected);
  902. }
  903. /**
  904. * test creating a get form, and get form inputs.
  905. *
  906. * @return void
  907. */
  908. public function testGetFormCreate() {
  909. $encoding = strtolower(Configure::read('App.encoding'));
  910. $result = $this->Form->create($this->article, array('type' => 'get'));
  911. $this->assertTags($result, array('form' => array(
  912. 'method' => 'get', 'action' => '/articles/add',
  913. 'accept-charset' => $encoding
  914. )));
  915. $result = $this->Form->text('title');
  916. $this->assertTags($result, array('input' => array(
  917. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  918. )));
  919. $result = $this->Form->password('password');
  920. $this->assertTags($result, array('input' => array(
  921. 'name' => 'password', 'type' => 'password'
  922. )));
  923. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  924. $result = $this->Form->text('user_form');
  925. $this->assertTags($result, array('input' => array(
  926. 'name' => 'user_form', 'type' => 'text'
  927. )));
  928. }
  929. /**
  930. * test get form, and inputs when the model param is false
  931. *
  932. * @return void
  933. */
  934. public function testGetFormWithFalseModel() {
  935. $encoding = strtolower(Configure::read('App.encoding'));
  936. $this->Form->request['controller'] = 'contact_test';
  937. $result = $this->Form->create(false, array(
  938. 'type' => 'get', 'url' => array('controller' => 'contact_test')
  939. ));
  940. $expected = array('form' => array(
  941. 'method' => 'get', 'action' => '/contact_test/add',
  942. 'accept-charset' => $encoding
  943. ));
  944. $this->assertTags($result, $expected);
  945. $result = $this->Form->text('reason');
  946. $expected = array(
  947. 'input' => array('type' => 'text', 'name' => 'reason')
  948. );
  949. $this->assertTags($result, $expected);
  950. }
  951. /**
  952. * testFormCreateWithSecurity method
  953. *
  954. * Test form->create() with security key.
  955. *
  956. * @return void
  957. */
  958. public function testCreateWithSecurity() {
  959. $this->Form->request->params['_csrfToken'] = 'testKey';
  960. $encoding = strtolower(Configure::read('App.encoding'));
  961. $result = $this->Form->create($this->article, [
  962. 'url' => '/articles/publish',
  963. ]);
  964. $expected = array(
  965. 'form' => array('method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding),
  966. 'div' => array('style' => 'display:none;'),
  967. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  968. array('input' => array(
  969. 'type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testKey'
  970. )),
  971. '/div'
  972. );
  973. $this->assertTags($result, $expected);
  974. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  975. $expected['form']['id'] = 'MyForm';
  976. $this->assertTags($result, $expected);
  977. }
  978. /**
  979. * testFormCreateGetNoSecurity method
  980. *
  981. * Test form->create() with no security key as its a get form
  982. *
  983. * @return void
  984. */
  985. public function testCreateEndGetNoSecurity() {
  986. $this->Form->request->params['_csrfToken'] = 'testKey';
  987. $encoding = strtolower(Configure::read('App.encoding'));
  988. $article = new Article();
  989. $result = $this->Form->create($article, [
  990. 'type' => 'get',
  991. 'url' => '/contacts/add'
  992. ]);
  993. $this->assertNotContains('testKey', $result);
  994. $result = $this->Form->end('Save');
  995. $this->assertNotContains('testKey', $result);
  996. }
  997. /**
  998. * test that create() clears the fields property so it starts fresh
  999. *
  1000. * @return void
  1001. */
  1002. public function testCreateClearingFields() {
  1003. $this->Form->fields = array('model_id');
  1004. $this->Form->create($this->article);
  1005. $this->assertEquals(array(), $this->Form->fields);
  1006. }
  1007. /**
  1008. * Tests form hash generation with model-less data
  1009. *
  1010. * @return void
  1011. */
  1012. public function testValidateHashNoModel() {
  1013. $this->Form->request->params['_Token'] = 'foo';
  1014. $result = $this->Form->secure(array('anything'));
  1015. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  1016. }
  1017. /**
  1018. * Tests that hidden fields generated for checkboxes don't get locked
  1019. *
  1020. * @return void
  1021. */
  1022. public function testNoCheckboxLocking() {
  1023. $this->Form->request->params['_Token'] = 'foo';
  1024. $this->assertSame([], $this->Form->fields);
  1025. $this->Form->checkbox('check', array('value' => '1'));
  1026. $this->assertSame($this->Form->fields, array('check'));
  1027. }
  1028. /**
  1029. * testFormSecurityFields method
  1030. *
  1031. * Test generation of secure form hash generation.
  1032. *
  1033. * @return void
  1034. */
  1035. public function testFormSecurityFields() {
  1036. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  1037. $this->Form->request->params['_Token'] = 'testKey';
  1038. $result = $this->Form->secure($fields);
  1039. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  1040. $hash .= ':' . 'Model.valid';
  1041. $hash = urlencode($hash);
  1042. $expected = array(
  1043. 'div' => array('style' => 'display:none;'),
  1044. array('input' => array(
  1045. 'type' => 'hidden',
  1046. 'name' => '_Token[fields]',
  1047. 'value' => $hash
  1048. )),
  1049. array('input' => array(
  1050. 'type' => 'hidden',
  1051. 'name' => '_Token[unlocked]',
  1052. 'value' => '',
  1053. )),
  1054. '/div'
  1055. );
  1056. $this->assertTags($result, $expected);
  1057. }
  1058. /**
  1059. * Tests correct generation of number fields for double and float fields
  1060. *
  1061. * @return void
  1062. */
  1063. public function testTextFieldGenerationForFloats() {
  1064. $this->article['schema'] = [
  1065. 'foo' => [
  1066. 'type' => 'float',
  1067. 'null' => false,
  1068. 'default' => null,
  1069. 'length' => 10
  1070. ]
  1071. ];
  1072. $this->Form->create($this->article);
  1073. $result = $this->Form->input('foo');
  1074. $expected = array(
  1075. 'div' => array('class' => 'input number'),
  1076. 'label' => array('for' => 'foo'),
  1077. 'Foo',
  1078. '/label',
  1079. array('input' => array(
  1080. 'type' => 'number',
  1081. 'name' => 'foo',
  1082. 'id' => 'foo',
  1083. 'step' => 'any'
  1084. )),
  1085. '/div'
  1086. );
  1087. $this->assertTags($result, $expected);
  1088. $result = $this->Form->input('foo', array('step' => 0.5));
  1089. $expected = array(
  1090. 'div' => array('class' => 'input number'),
  1091. 'label' => array('for' => 'foo'),
  1092. 'Foo',
  1093. '/label',
  1094. array('input' => array(
  1095. 'type' => 'number',
  1096. 'name' => 'foo',
  1097. 'id' => 'foo',
  1098. 'step' => '0.5'
  1099. )),
  1100. '/div'
  1101. );
  1102. $this->assertTags($result, $expected);
  1103. }
  1104. /**
  1105. * Tests correct generation of number fields for integer fields
  1106. *
  1107. * @return void
  1108. */
  1109. public function testTextFieldTypeNumberGenerationForIntegers() {
  1110. TableRegistry::get('Contacts', [
  1111. 'className' => __NAMESPACE__ . '\ContactsTable'
  1112. ]);
  1113. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1114. $result = $this->Form->input('age');
  1115. $expected = array(
  1116. 'div' => array('class' => 'input number'),
  1117. 'label' => array('for' => 'age'),
  1118. 'Age',
  1119. '/label',
  1120. array('input' => array(
  1121. 'type' => 'number', 'name' => 'age',
  1122. 'id' => 'age'
  1123. )),
  1124. '/div'
  1125. );
  1126. $this->assertTags($result, $expected);
  1127. }
  1128. /**
  1129. * Tests correct generation of file upload fields for binary fields
  1130. *
  1131. * @return void
  1132. */
  1133. public function testFileUploadFieldTypeGenerationForBinaries() {
  1134. $table = TableRegistry::get('Contacts', [
  1135. 'className' => __NAMESPACE__ . '\ContactsTable'
  1136. ]);
  1137. $table->schema(array('foo' => array(
  1138. 'type' => 'binary',
  1139. 'null' => false,
  1140. 'default' => null,
  1141. 'length' => 1024
  1142. )));
  1143. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1144. $result = $this->Form->input('foo');
  1145. $expected = array(
  1146. 'div' => array('class' => 'input file'),
  1147. 'label' => array('for' => 'foo'),
  1148. 'Foo',
  1149. '/label',
  1150. array('input' => array(
  1151. 'type' => 'file', 'name' => 'foo',
  1152. 'id' => 'foo'
  1153. )),
  1154. '/div'
  1155. );
  1156. $this->assertTags($result, $expected);
  1157. }
  1158. /**
  1159. * testFormSecurityMultipleFields method
  1160. *
  1161. * Test secure() with multiple row form. Ensure hash is correct.
  1162. *
  1163. * @return void
  1164. */
  1165. public function testFormSecurityMultipleFields() {
  1166. $this->Form->request->params['_Token'] = 'foo';
  1167. $fields = array(
  1168. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1169. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1170. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1171. );
  1172. $result = $this->Form->secure($fields);
  1173. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  1174. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  1175. $expected = array(
  1176. 'div' => array('style' => 'display:none;'),
  1177. array('input' => array(
  1178. 'type' => 'hidden', 'name' => '_Token[fields]',
  1179. 'value' => $hash
  1180. )),
  1181. array('input' => array(
  1182. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1183. 'value' => ''
  1184. )),
  1185. '/div'
  1186. );
  1187. $this->assertTags($result, $expected);
  1188. }
  1189. /**
  1190. * testFormSecurityMultipleSubmitButtons
  1191. *
  1192. * test form submit generation and ensure that _Token is only created on end()
  1193. *
  1194. * @return void
  1195. */
  1196. public function testFormSecurityMultipleSubmitButtons() {
  1197. $this->Form->request->params['_Token'] = 'testKey';
  1198. $this->Form->create($this->article);
  1199. $this->Form->text('Address.title');
  1200. $this->Form->text('Address.first_name');
  1201. $result = $this->Form->submit('Save', array('name' => 'save'));
  1202. $expected = array(
  1203. 'div' => array('class' => 'submit'),
  1204. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  1205. '/div',
  1206. );
  1207. $this->assertTags($result, $expected);
  1208. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  1209. $expected = array(
  1210. 'div' => array('class' => 'submit'),
  1211. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  1212. '/div',
  1213. );
  1214. $this->assertTags($result, $expected);
  1215. $result = $this->Form->end();
  1216. $expected = array(
  1217. 'div' => array('style' => 'display:none;'),
  1218. array('input' => array(
  1219. 'type' => 'hidden',
  1220. 'name' => '_Token[fields]',
  1221. 'value'
  1222. )),
  1223. array('input' => array(
  1224. 'type' => 'hidden',
  1225. 'name' => '_Token[unlocked]',
  1226. 'value' => 'cancel%7Csave'
  1227. )),
  1228. '/div'
  1229. );
  1230. $this->assertTags($result, $expected);
  1231. }
  1232. /**
  1233. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1234. *
  1235. * @return void
  1236. */
  1237. public function testSecurityButtonNestedNamed() {
  1238. $key = 'testKey';
  1239. $this->Form->request->params['_csrfToken'] = $key;
  1240. $this->Form->create('Addresses');
  1241. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  1242. $result = $this->Form->unlockField();
  1243. $this->assertEquals(array('Address.button'), $result);
  1244. }
  1245. /**
  1246. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1247. *
  1248. * @return void
  1249. */
  1250. public function testSecuritySubmitNestedNamed() {
  1251. $this->Form->request->params['_Token'] = 'testKey';
  1252. $this->Form->create($this->article);
  1253. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  1254. $result = $this->Form->unlockField();
  1255. $this->assertEquals(array('Address.button'), $result);
  1256. }
  1257. /**
  1258. * Test that the correct fields are unlocked for image submits with no names.
  1259. *
  1260. * @return void
  1261. */
  1262. public function testSecuritySubmitImageNoName() {
  1263. $key = 'testKey';
  1264. $this->Form->request->params['_Token'] = 'testKey';
  1265. $this->Form->create(false);
  1266. $result = $this->Form->submit('save.png');
  1267. $expected = array(
  1268. 'div' => array('class' => 'submit'),
  1269. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  1270. '/div'
  1271. );
  1272. $this->assertTags($result, $expected);
  1273. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  1274. }
  1275. /**
  1276. * Test that the correct fields are unlocked for image submits with names.
  1277. *
  1278. * @return void
  1279. */
  1280. public function testSecuritySubmitImageName() {
  1281. $this->Form->request->params['_Token'] = 'testKey';
  1282. $this->Form->create(null);
  1283. $result = $this->Form->submit('save.png', array('name' => 'test'));
  1284. $expected = array(
  1285. 'div' => array('class' => 'submit'),
  1286. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  1287. '/div'
  1288. );
  1289. $this->assertTags($result, $expected);
  1290. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  1291. }
  1292. /**
  1293. * testFormSecurityMultipleInputFields method
  1294. *
  1295. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1296. *
  1297. * @return void
  1298. */
  1299. public function testFormSecurityMultipleInputFields() {
  1300. $this->Form->request->params['_Token'] = 'testKey';
  1301. $this->Form->create();
  1302. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1303. $this->Form->input('Addresses.0.title');
  1304. $this->Form->input('Addresses.0.first_name');
  1305. $this->Form->input('Addresses.0.last_name');
  1306. $this->Form->input('Addresses.0.address');
  1307. $this->Form->input('Addresses.0.city');
  1308. $this->Form->input('Addresses.0.phone');
  1309. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  1310. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1311. $this->Form->input('Addresses.1.title');
  1312. $this->Form->input('Addresses.1.first_name');
  1313. $this->Form->input('Addresses.1.last_name');
  1314. $this->Form->input('Addresses.1.address');
  1315. $this->Form->input('Addresses.1.city');
  1316. $this->Form->input('Addresses.1.phone');
  1317. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  1318. $result = $this->Form->secure($this->Form->fields);
  1319. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  1320. $expected = array(
  1321. 'div' => array('style' => 'display:none;'),
  1322. array('input' => array(
  1323. 'type' => 'hidden', 'name' => '_Token[fields]',
  1324. 'value' => $hash
  1325. )),
  1326. array('input' => array(
  1327. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1328. 'value' => ''
  1329. )),
  1330. '/div'
  1331. );
  1332. $this->assertTags($result, $expected);
  1333. }
  1334. /**
  1335. * Test form security with Model.field.0 style inputs
  1336. *
  1337. * @return void
  1338. */
  1339. public function testFormSecurityArrayFields() {
  1340. $this->Form->request->params['_Token'] = 'testKey';
  1341. $this->Form->create();
  1342. $this->Form->text('Address.primary.1');
  1343. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1344. $this->Form->text('Address.secondary.1.0');
  1345. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1346. }
  1347. /**
  1348. * testFormSecurityMultipleInputDisabledFields method
  1349. *
  1350. * test secure form generation with multiple records and disabled fields.
  1351. *
  1352. * @return void
  1353. */
  1354. public function testFormSecurityMultipleInputDisabledFields() {
  1355. $this->Form->request->params['_Token'] = array(
  1356. 'unlockedFields' => array('first_name', 'address')
  1357. );
  1358. $this->Form->create();
  1359. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1360. $this->Form->text('Addresses.0.title');
  1361. $this->Form->text('Addresses.0.first_name');
  1362. $this->Form->text('Addresses.0.last_name');
  1363. $this->Form->text('Addresses.0.address');
  1364. $this->Form->text('Addresses.0.city');
  1365. $this->Form->text('Addresses.0.phone');
  1366. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1367. $this->Form->text('Addresses.1.title');
  1368. $this->Form->text('Addresses.1.first_name');
  1369. $this->Form->text('Addresses.1.last_name');
  1370. $this->Form->text('Addresses.1.address');
  1371. $this->Form->text('Addresses.1.city');
  1372. $this->Form->text('Addresses.1.phone');
  1373. $result = $this->Form->secure($this->Form->fields);
  1374. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  1375. $expected = array(
  1376. 'div' => array('style' => 'display:none;'),
  1377. array('input' => array(
  1378. 'type' => 'hidden',
  1379. 'name' => '_Token[fields]',
  1380. 'value' => $hash
  1381. )),
  1382. array('input' => array(
  1383. 'type' => 'hidden',
  1384. 'name' => '_Token[unlocked]',
  1385. 'value' => 'address%7Cfirst_name',
  1386. )),
  1387. '/div'
  1388. );
  1389. $this->assertTags($result, $expected);
  1390. }
  1391. /**
  1392. * testFormSecurityInputDisabledFields method
  1393. *
  1394. * Test single record form with disabled fields.
  1395. *
  1396. * @return void
  1397. */
  1398. public function testFormSecurityInputUnlockedFields() {
  1399. $this->Form->request['_Token'] = array(
  1400. 'unlockedFields' => array('first_name', 'address')
  1401. );
  1402. $this->Form->create();
  1403. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1404. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1405. $this->Form->text('Addresses.title');
  1406. $this->Form->text('Addresses.first_name');
  1407. $this->Form->text('Addresses.last_name');
  1408. $this->Form->text('Addresses.address');
  1409. $this->Form->text('Addresses.city');
  1410. $this->Form->text('Addresses.phone');
  1411. $result = $this->Form->fields;
  1412. $expected = array(
  1413. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1414. 'Addresses.city', 'Addresses.phone'
  1415. );
  1416. $this->assertEquals($expected, $result);
  1417. $result = $this->Form->secure($expected);
  1418. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1419. $expected = array(
  1420. 'div' => array('style' => 'display:none;'),
  1421. array('input' => array(
  1422. 'type' => 'hidden',
  1423. 'name' => '_Token[fields]',
  1424. 'value' => $hash
  1425. )),
  1426. array('input' => array(
  1427. 'type' => 'hidden',
  1428. 'name' => '_Token[unlocked]',
  1429. 'value' => 'address%7Cfirst_name',
  1430. )),
  1431. '/div'
  1432. );
  1433. $this->assertTags($result, $expected);
  1434. }
  1435. /**
  1436. * test securing inputs with custom name attributes.
  1437. *
  1438. * @return void
  1439. */
  1440. public function testFormSecureWithCustomNameAttribute() {
  1441. $this->Form->request->params['_Token'] = 'testKey';
  1442. $this->Form->text('UserForm.published', array('name' => 'User[custom]'));
  1443. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1444. $this->Form->text('UserForm.published', array('name' => 'User[custom][another][value]'));
  1445. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1446. }
  1447. /**
  1448. * testFormSecuredInput method
  1449. *
  1450. * Test generation of entire secure form, assertions made on input() output.
  1451. *
  1452. * @return void
  1453. */
  1454. public function testFormSecuredInput() {
  1455. $this->markTestIncomplete('Need to revisit once models work again.');
  1456. $this->Form->request->params['_csrfToken'] = 'testKey';
  1457. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1458. $encoding = strtolower(Configure::read('App.encoding'));
  1459. $expected = array(
  1460. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1461. 'div' => array('style' => 'display:none;'),
  1462. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1463. array('input' => array(
  1464. 'type' => 'hidden', 'name' => '_csrfToken',
  1465. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1466. )),
  1467. '/div'
  1468. );
  1469. $this->assertTags($result, $expected);
  1470. $result = $this->Form->input('ValidateUser.ratio');
  1471. $expected = array(
  1472. 'div' => array('class'),
  1473. 'label' => array('for'),
  1474. 'Ratio',
  1475. '/label',
  1476. 'input' => array('name', 'type' => 'number', 'step' => '0.000001', 'id'),
  1477. '/div',
  1478. );
  1479. $this->assertTags($result, $expected);
  1480. $result = $this->Form->input('ValidateUser.population');
  1481. $expected = array(
  1482. 'div' => array('class'),
  1483. 'label' => array('for'),
  1484. 'Population',
  1485. '/label',
  1486. 'input' => array('name', 'type' => 'number', 'step' => '1', 'id'),
  1487. '/div',
  1488. );
  1489. $this->assertTags($result, $expected);
  1490. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1491. $expected = array(
  1492. 'div' => array('class' => 'input text'),
  1493. 'label' => array('for' => 'UserFormPublished'),
  1494. 'Published',
  1495. '/label',
  1496. array('input' => array(
  1497. 'type' => 'text', 'name' => 'UserForm[published]',
  1498. 'id' => 'UserFormPublished'
  1499. )),
  1500. '/div'
  1501. );
  1502. $this->assertTags($result, $expected);
  1503. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1504. $expected = array(
  1505. 'div' => array('class' => 'input text'),
  1506. 'label' => array('for' => 'UserFormOther'),
  1507. 'Other',
  1508. '/label',
  1509. array('input' => array(
  1510. 'type' => 'text', 'name' => 'UserForm[other]',
  1511. 'id' => 'UserFormOther'
  1512. )),
  1513. '/div'
  1514. );
  1515. $this->assertTags($result, $expected);
  1516. $result = $this->Form->hidden('UserForm.stuff');
  1517. $expected = array(
  1518. 'input' => array(
  1519. 'type' => 'hidden', 'name' => 'UserForm[stuff]',
  1520. 'id' => 'UserFormStuff'
  1521. ));
  1522. $this->assertTags($result, $expected);
  1523. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1524. $expected = array('input' => array(
  1525. 'type' => 'hidden', 'name' => 'UserForm[hidden]',
  1526. 'value' => '0', 'id' => 'UserFormHidden'
  1527. ));
  1528. $this->assertTags($result, $expected);
  1529. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1530. $expected = array(
  1531. 'div' => array('class' => 'input checkbox'),
  1532. array('input' => array(
  1533. 'type' => 'hidden', 'name' => 'UserForm[something]',
  1534. 'value' => '0', 'id' => 'UserFormSomething_'
  1535. )),
  1536. array('input' => array(
  1537. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  1538. 'value' => '1', 'id' => 'UserFormSomething'
  1539. )),
  1540. 'label' => array('for' => 'UserFormSomething'),
  1541. 'Something',
  1542. '/label',
  1543. '/div'
  1544. );
  1545. $this->assertTags($result, $expected);
  1546. $result = $this->Form->fields;
  1547. $expected = array(
  1548. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1549. 'UserForm.hidden' => '0', 'UserForm.something'
  1550. );
  1551. $this->assertEquals($expected, $result);
  1552. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1553. $result = $this->Form->secure($this->Form->fields);
  1554. $expected = array(
  1555. 'div' => array('style' => 'display:none;'),
  1556. array('input' => array(
  1557. 'type' => 'hidden', 'name' => '_Token[fields]',
  1558. 'value' => $hash
  1559. )),
  1560. array('input' => array(
  1561. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1562. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1563. )),
  1564. '/div'
  1565. );
  1566. $this->assertTags($result, $expected);
  1567. }
  1568. /**
  1569. * Test secured inputs with custom names.
  1570. *
  1571. * @return void
  1572. */
  1573. public function testSecuredInputCustomName() {
  1574. $this->Form->request->params['_Token'] = 'testKey';
  1575. $this->assertEquals(array(), $this->Form->fields);
  1576. $this->Form->text('text_input', array(
  1577. 'name' => 'Option[General.default_role]',
  1578. ));
  1579. $expected = array('Option.General.default_role');
  1580. $this->assertEquals($expected, $this->Form->fields);
  1581. $this->Form->select('select_box', [1, 2], [
  1582. 'name' => 'Option[General.select_role]',
  1583. ]);
  1584. $expected = ['Option.General.default_role', 'Option.General.select_role'];
  1585. $this->assertEquals($expected, $this->Form->fields);
  1586. }
  1587. /**
  1588. * Tests that the correct keys are added to the field hash index
  1589. *
  1590. * @return void
  1591. */
  1592. public function testFormSecuredFileInput() {
  1593. $this->assertEquals(array(), $this->Form->fields);
  1594. $this->Form->file('Attachment.file');
  1595. $expected = array(
  1596. 'Attachment.file.name', 'Attachment.file.type',
  1597. 'Attachment.file.tmp_name', 'Attachment.file.error',
  1598. 'Attachment.file.size'
  1599. );
  1600. $this->assertEquals($expected, $this->Form->fields);
  1601. }
  1602. /**
  1603. * test that multiple selects keys are added to field hash
  1604. *
  1605. * @return void
  1606. */
  1607. public function testFormSecuredMultipleSelect() {
  1608. $this->Form->request->params['_csrfToken'] = 'testKey';
  1609. $this->assertEquals(array(), $this->Form->fields);
  1610. $options = array('1' => 'one', '2' => 'two');
  1611. $this->Form->select('Model.select', $options);
  1612. $expected = array('Model.select');
  1613. $this->assertEquals($expected, $this->Form->fields);
  1614. $this->Form->fields = array();
  1615. $this->Form->select('Model.select', $options, array('multiple' => true));
  1616. $this->assertEquals($expected, $this->Form->fields);
  1617. }
  1618. /**
  1619. * testFormSecuredRadio method
  1620. *
  1621. * @return void
  1622. */
  1623. public function testFormSecuredRadio() {
  1624. $this->Form->request->params['_Token'] = 'testKey';
  1625. $this->assertEquals(array(), $this->Form->fields);
  1626. $options = array('1' => 'option1', '2' => 'option2');
  1627. $this->Form->radio('Test.test', $options);
  1628. $expected = array('Test.test');
  1629. $this->assertEquals($expected, $this->Form->fields);
  1630. }
  1631. /**
  1632. * Test that when disabled is in a list based attribute array it works.
  1633. *
  1634. * @return void
  1635. */
  1636. public function testFormSecuredAndDisabledNotAssoc() {
  1637. $this->Form->request->params['_Token'] = 'testKey';
  1638. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1639. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1640. $this->Form->text('Model.text', array('disabled'));
  1641. $this->Form->textarea('Model.textarea', array('disabled'));
  1642. $this->Form->password('Model.password', array('disabled'));
  1643. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1644. $expected = array(
  1645. 'Model.radio' => ''
  1646. );
  1647. $this->assertEquals($expected, $this->Form->fields);
  1648. }
  1649. /**
  1650. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1651. * hashing.
  1652. *
  1653. * @return void
  1654. */
  1655. public function testFormSecuredAndDisabled() {
  1656. $this->Form->request->params['_Token'] = 'testKey';
  1657. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1658. $this->Form->text('Model.text', array('disabled' => true));
  1659. $this->Form->password('Model.text', array('disabled' => true));
  1660. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1661. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1662. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1663. $this->Form->year('Model.year', array('disabled' => true));
  1664. $this->Form->month('Model.month', array('disabled' => true));
  1665. $this->Form->day('Model.day', array('disabled' => true));
  1666. $this->Form->hour('Model.hour', array('disabled' => true));
  1667. $this->Form->minute('Model.minute', array('disabled' => true));
  1668. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1669. $expected = array(
  1670. 'Model.radio' => ''
  1671. );
  1672. $this->assertEquals($expected, $this->Form->fields);
  1673. }
  1674. /**
  1675. * testDisableSecurityUsingForm method
  1676. *
  1677. * @return void
  1678. */
  1679. public function testDisableSecurityUsingForm() {
  1680. $this->Form->request['_Token'] = [
  1681. 'disabledFields' => []
  1682. ];
  1683. $this->Form->create();
  1684. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1685. $this->Form->text('Addresses.title');
  1686. $this->Form->text('Addresses.first_name', ['secure' => false]);
  1687. $this->Form->textarea('Addresses.city', ['secure' => false]);
  1688. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  1689. $result = $this->Form->fields;
  1690. $expected = [
  1691. 'Addresses.id' => '123456', 'Addresses.title',
  1692. ];
  1693. $this->assertEquals($expected, $result);
  1694. }
  1695. /**
  1696. * test disableField
  1697. *
  1698. * @return void
  1699. */
  1700. public function testUnlockFieldAddsToList() {
  1701. $this->Form->request['_Token'] = array(
  1702. 'unlockedFields' => array()
  1703. );
  1704. $this->Form->unlockField('Contact.name');
  1705. $this->Form->text('Contact.name');
  1706. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1707. $this->assertEquals(array(), $this->Form->fields);
  1708. }
  1709. /**
  1710. * test unlockField removing from fields array.
  1711. *
  1712. * @return void
  1713. */
  1714. public function testUnlockFieldRemovingFromFields() {
  1715. $this->Form->request['_Token'] = array(
  1716. 'unlockedFields' => array()
  1717. );
  1718. $this->Form->create($this->article);
  1719. $this->Form->hidden('Article.id', array('value' => 1));
  1720. $this->Form->text('Article.title');
  1721. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  1722. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  1723. $this->Form->unlockField('Article.title');
  1724. $this->Form->unlockField('Article.id');
  1725. $this->assertEquals(array(), $this->Form->fields);
  1726. }
  1727. /**
  1728. * test error message display
  1729. *
  1730. * @return void
  1731. */
  1732. public function testErrorMessageDisplay() {
  1733. $this->article['errors'] = [
  1734. 'Article' => ['title' => 'error message']
  1735. ];
  1736. $this->Form->create($this->article);
  1737. $result = $this->Form->input('Article.title');
  1738. $expected = [
  1739. 'div' => ['class' => 'input text error'],
  1740. 'label' => ['for' => 'article-title'],
  1741. 'Title',
  1742. '/label',
  1743. 'input' => [
  1744. 'type' => 'text', 'name' => 'Article[title]',
  1745. 'id' => 'article-title', 'class' => 'form-error'
  1746. ],
  1747. ['div' => ['class' => 'error-message']],
  1748. 'error message',
  1749. '/div',
  1750. '/div'
  1751. ];
  1752. $this->assertTags($result, $expected);
  1753. $result = $this->Form->input('Article.title', [
  1754. 'templates' => [
  1755. 'groupContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  1756. ]
  1757. ]);
  1758. $expected = [
  1759. 'div' => ['class' => 'input text error'],
  1760. 'label' => ['for' => 'article-title'],
  1761. 'Title',
  1762. '/label',
  1763. 'input' => [
  1764. 'type' => 'text', 'name' => 'Article[title]',
  1765. 'id' => 'article-title', 'class' => 'form-error'
  1766. ],
  1767. '/div'
  1768. ];
  1769. $this->assertTags($result, $expected);
  1770. }
  1771. /**
  1772. * Test validation errors, when validation message is an empty string.
  1773. *
  1774. * @return void
  1775. */
  1776. public function testEmptyErrorValidation() {
  1777. $this->article['errors'] = [
  1778. 'Article' => ['title' => '']
  1779. ];
  1780. $this->Form->create($this->article);
  1781. $result = $this->Form->input('Article.title');
  1782. $expected = [
  1783. 'div' => ['class' => 'input text error'],
  1784. 'label' => ['for' => 'article-title'],
  1785. 'Title',
  1786. '/label',
  1787. 'input' => [
  1788. 'type' => 'text', 'name' => 'Article[title]',
  1789. 'id' => 'article-title', 'class' => 'form-error'
  1790. ],
  1791. ['div' => ['class' => 'error-message']],
  1792. [],
  1793. '/div',
  1794. '/div'
  1795. ];
  1796. $this->assertTags($result, $expected);
  1797. }
  1798. /**
  1799. * Test validation errors, when calling input() overriding validation message by an empty string.
  1800. *
  1801. * @return void
  1802. */
  1803. public function testEmptyInputErrorValidation() {
  1804. $this->article['errors'] = [
  1805. 'Article' => ['title' => 'error message']
  1806. ];
  1807. $this->Form->create($this->article);
  1808. $result = $this->Form->input('Article.title', array('error' => ''));
  1809. $expected = [
  1810. 'div' => ['class' => 'input text error'],
  1811. 'label' => ['for' => 'article-title'],
  1812. 'Title',
  1813. '/label',
  1814. 'input' => [
  1815. 'type' => 'text', 'name' => 'Article[title]',
  1816. 'id' => 'article-title', 'class' => 'form-error'
  1817. ],
  1818. ['div' => ['class' => 'error-message']],
  1819. [],
  1820. '/div',
  1821. '/div'
  1822. ];
  1823. $this->assertTags($result, $expected);
  1824. }
  1825. /**
  1826. * Tests displaying errors for nested entities
  1827. *
  1828. * @return void
  1829. */
  1830. public function testFormValidationAssociated() {
  1831. $nested = new Entity(['foo' => 'bar']);
  1832. $nested->errors('foo', ['not a valid bar']);
  1833. $entity = new Entity(['nested' => $nested]);
  1834. $this->Form->create($entity);
  1835. $result = $this->Form->error('nested.foo');
  1836. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  1837. }
  1838. /**
  1839. * testFormValidationAssociatedSecondLevel method
  1840. *
  1841. * test form error display with associated model.
  1842. *
  1843. * @return void
  1844. */
  1845. public function testFormValidationAssociatedSecondLevel() {
  1846. $inner = new Entity(['bar' => 'baz']);
  1847. $nested = new Entity(['foo' => $inner]);
  1848. $entity = new Entity(['nested' => $nested]);
  1849. $inner->errors('bar', ['not a valid one']);
  1850. $this->Form->create($entity);
  1851. $result = $this->Form->error('nested.foo.bar');
  1852. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  1853. }
  1854. /**
  1855. * testFormValidationMultiRecord method
  1856. *
  1857. * test form error display with multiple records.
  1858. *
  1859. * @return void
  1860. */
  1861. public function testFormValidationMultiRecord() {
  1862. $one = new Entity;
  1863. $two = new Entity;
  1864. TableRegistry::get('Contacts', [
  1865. 'className' => __NAMESPACE__ . '\ContactsTable'
  1866. ]);
  1867. $one->errors('email', ['invalid email']);
  1868. $two->errors('name', ['This is wrong']);
  1869. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  1870. $result = $this->Form->input('Contacts.0.email');
  1871. $expected = array(
  1872. 'div' => array('class' => 'input email error'),
  1873. 'label' => array('for' => 'contacts-0-email'),
  1874. 'Email',
  1875. '/label',
  1876. 'input' => array(
  1877. 'type' => 'text', 'name' => 'Contacts[0][email]', 'id' => 'contacts-0-email',
  1878. 'class' => 'form-error', 'maxlength' => 255
  1879. ),
  1880. array('div' => array('class' => 'error-message')),
  1881. 'invalid email',
  1882. '/div',
  1883. '/div'
  1884. );
  1885. $result = $this->Form->input('Contacts.1.name');
  1886. $expected = array(
  1887. 'div' => array('class' => 'input text error'),
  1888. 'label' => array('for' => 'contacts-1-name'),
  1889. 'Name',
  1890. '/label',
  1891. 'input' => array(
  1892. 'type' => 'text', 'name' => 'Contacts[1][name]', 'id' => 'contacts-1-name',
  1893. 'class' => 'form-error', 'maxlength' => 255
  1894. ),
  1895. array('div' => array('class' => 'error-message')),
  1896. 'This is wrong',
  1897. '/div',
  1898. '/div'
  1899. );
  1900. $this->assertTags($result, $expected);
  1901. }
  1902. /**
  1903. * testInput method
  1904. *
  1905. * Test various incarnations of input().
  1906. *
  1907. * @return void
  1908. */
  1909. public function testInput() {
  1910. TableRegistry::get('ValidateUsers', [
  1911. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  1912. ]);
  1913. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  1914. $result = $this->Form->input('ValidateUsers.balance');
  1915. $expected = array(
  1916. 'div' => array('class'),
  1917. 'label' => array('for'),
  1918. 'Balance',
  1919. '/label',
  1920. 'input' => array('name', 'type' => 'number', 'id', 'step'),
  1921. '/div',
  1922. );
  1923. $this->assertTags($result, $expected);
  1924. $result = $this->Form->input('ValidateUser.cost_decimal');
  1925. $expected = array(
  1926. 'div' => array('class'),
  1927. 'label' => array('for'),
  1928. 'Cost Decimal',
  1929. '/label',
  1930. 'input' => array('name', 'type' => 'number', 'step' => '0.001', 'id'),
  1931. '/div',
  1932. );
  1933. $this->assertTags($result, $expected);
  1934. }
  1935. /**
  1936. * Tests the input method and passing custom options
  1937. *
  1938. * @return void
  1939. */
  1940. public function testInputCustomization() {
  1941. TableRegistry::get('Contacts', [
  1942. 'className' => __NAMESPACE__ . '\ContactsTable'
  1943. ]);
  1944. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1945. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1946. $expected = array(
  1947. 'div' => array('class' => 'input email'),
  1948. 'label' => array('for' => 'custom'),
  1949. 'Email',
  1950. '/label',
  1951. array('input' => array(
  1952. 'type' => 'email', 'name' => 'Contact[email]',
  1953. 'id' => 'custom', 'maxlength' => 255
  1954. )),
  1955. '/div'
  1956. );
  1957. $this->assertTags($result, $expected);
  1958. $result = $this->Form->input('Contact.email', array(
  1959. 'templates' => ['groupContainer' => '<div>{{content}}</div>']
  1960. ));
  1961. $expected = array(
  1962. '<div',
  1963. 'label' => array('for' => 'contact-email'),
  1964. 'Email',
  1965. '/label',
  1966. array('input' => array(
  1967. 'type' => 'email', 'name' => 'Contact[email]',
  1968. 'id' => 'contact-email', 'maxlength' => 255
  1969. )),
  1970. '/div'
  1971. );
  1972. $this->assertTags($result, $expected);
  1973. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1974. $expected = array(
  1975. 'div' => array('class' => 'input text'),
  1976. 'label' => array('for' => 'contact-email'),
  1977. 'Email',
  1978. '/label',
  1979. array('input' => array(
  1980. 'type' => 'text', 'name' => 'Contact[email]',
  1981. 'id' => 'contact-email', 'maxlength' => '255'
  1982. )),
  1983. '/div'
  1984. );
  1985. $this->assertTags($result, $expected);
  1986. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1987. $expected = array(
  1988. 'div' => array('class' => 'input text'),
  1989. 'label' => array('for' => 'contact-5-email'),
  1990. 'Email',
  1991. '/label',
  1992. array('input' => array(
  1993. 'type' => 'text', 'name' => 'Contact[5][email]',
  1994. 'id' => 'contact-5-email', 'maxlength' => '255'
  1995. )),
  1996. '/div'
  1997. );
  1998. $this->assertTags($result, $expected);
  1999. $result = $this->Form->input('Contact.password');
  2000. $expected = array(
  2001. 'div' => array('class' => 'input password'),
  2002. 'label' => array('for' => 'contact-password'),
  2003. 'Password',
  2004. '/label',
  2005. array('input' => array(
  2006. 'type' => 'password', 'name' => 'Contact[password]',
  2007. 'id' => 'contact-password'
  2008. )),
  2009. '/div'
  2010. );
  2011. $this->assertTags($result, $expected);
  2012. $result = $this->Form->input('Contact.email', array(
  2013. 'type' => 'file', 'class' => 'textbox'
  2014. ));
  2015. $expected = array(
  2016. 'div' => array('class' => 'input file'),
  2017. 'label' => array('for' => 'contact-email'),
  2018. 'Email',
  2019. '/label',
  2020. array('input' => array(
  2021. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2022. 'id' => 'contact-email'
  2023. )),
  2024. '/div'
  2025. );
  2026. $this->assertTags($result, $expected);
  2027. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2028. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2029. $result = $this->Form->input('Contact.phone');
  2030. $expected = array(
  2031. 'div' => array('class' => 'input tel'),
  2032. 'label' => array('for' => 'contact-phone'),
  2033. 'Phone',
  2034. '/label',
  2035. array('input' => array(
  2036. 'type' => 'tel', 'name' => 'Contact[phone]',
  2037. 'value' => 'Hello &amp; World &gt; weird chars',
  2038. 'id' => 'contact-phone', 'maxlength' => 255
  2039. )),
  2040. '/div'
  2041. );
  2042. $this->assertTags($result, $expected);
  2043. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2044. $this->Form->create();
  2045. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  2046. $expected = array(
  2047. 'div' => array('class' => 'input text'),
  2048. 'label' => array('for' => 'myId'),
  2049. 'Field',
  2050. '/label',
  2051. 'input' => array(
  2052. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2053. 'value' => 'My value', 'id' => 'myId'
  2054. ),
  2055. '/div'
  2056. );
  2057. $this->assertTags($result, $expected);
  2058. unset($this->Form->request->data);
  2059. $entity->errors('field', 'Badness!');
  2060. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2061. $result = $this->Form->input('Contact.field');
  2062. $expected = array(
  2063. 'div' => array('class' => 'input text error'),
  2064. 'label' => array('for' => 'contact-field'),
  2065. 'Field',
  2066. '/label',
  2067. 'input' => array(
  2068. 'type' => 'text', 'name' => 'Contact[field]',
  2069. 'id' => 'contact-field', 'class' => 'form-error'
  2070. ),
  2071. array('div' => array('class' => 'error-message')),
  2072. 'Badness!',
  2073. '/div',
  2074. '/div'
  2075. );
  2076. $this->assertTags($result, $expected);
  2077. $result = $this->Form->input('Contact.field', array(
  2078. 'templates' => [
  2079. 'groupContainerError' => '{{content}}{{error}}',
  2080. 'error' => '<span class="error-message">{{content}}</span>'
  2081. ]
  2082. ));
  2083. $expected = array(
  2084. 'label' => array('for' => 'contact-field'),
  2085. 'Field',
  2086. '/label',
  2087. 'input' => array(
  2088. 'type' => 'text', 'name' => 'Contact[field]',
  2089. 'id' => 'contact-field', 'class' => 'form-error'
  2090. ),
  2091. array('span' => array('class' => 'error-message')),
  2092. 'Badness!',
  2093. '/span'
  2094. );
  2095. $this->assertTags($result, $expected);
  2096. $entity->errors('field', ['minLength']);
  2097. $result = $this->Form->input('Contact.field', array(
  2098. 'error' => array(
  2099. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2100. 'maxLength' => 'login too large'
  2101. )
  2102. ));
  2103. $expected = array(
  2104. 'div' => array('class' => 'input text error'),
  2105. 'label' => array('for' => 'contact-field'),
  2106. 'Field',
  2107. '/label',
  2108. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'contact-field', 'class' => 'form-error'),
  2109. array('div' => array('class' => 'error-message')),
  2110. 'Le login doit contenir au moins 2 caractères',
  2111. '/div',
  2112. '/div'
  2113. );
  2114. $this->assertTags($result, $expected);
  2115. $entity->errors('field', ['maxLength']);
  2116. $result = $this->Form->input('Contact.field', array(
  2117. 'error' => array(
  2118. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2119. 'maxLength' => 'login too large',
  2120. )
  2121. ));
  2122. $expected = array(
  2123. 'div' => array('class' => 'input text error'),
  2124. 'label' => array('for' => 'contact-field'),
  2125. 'Field',
  2126. '/label',
  2127. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'contact-field', 'class' => 'form-error'),
  2128. array('div' => array('class' => 'error-message')),
  2129. 'login too large',
  2130. '/div',
  2131. '/div'
  2132. );
  2133. $this->assertTags($result, $expected);
  2134. }
  2135. /**
  2136. * Test that inputs with 0 can be created.
  2137. *
  2138. * @return void
  2139. */
  2140. public function testInputZero() {
  2141. TableRegistry::get('Contacts', [
  2142. 'className' => __NAMESPACE__ . '\ContactsTable'
  2143. ]);
  2144. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2145. $result = $this->Form->input('0');
  2146. $expected = array(
  2147. 'div' => array('class' => 'input text'),
  2148. 'label' => array('for' => '0'), '/label',
  2149. 'input' => array('type' => 'text', 'name' => '0', 'id' => '0'),
  2150. '/div'
  2151. );
  2152. $this->assertTags($result, $expected);
  2153. }
  2154. /**
  2155. * test input() with checkbox creation
  2156. *
  2157. * @return void
  2158. */
  2159. public function testInputCheckbox() {
  2160. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  2161. $expected = array(
  2162. 'div' => array('class' => 'input checkbox'),
  2163. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
  2164. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
  2165. '/div'
  2166. );
  2167. $this->assertTags($result, $expected);
  2168. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  2169. $expected = array(
  2170. 'div' => array('class' => 'input checkbox'),
  2171. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
  2172. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
  2173. '/div'
  2174. );
  2175. $this->assertTags($result, $expected);
  2176. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  2177. $expected = array(
  2178. 'div' => array('class' => 'input checkbox'),
  2179. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
  2180. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
  2181. '/div'
  2182. );
  2183. $this->assertTags($result, $expected);
  2184. $result = $this->Form->input('User.disabled', array(
  2185. 'label' => 'Disabled',
  2186. 'type' => 'checkbox',
  2187. 'data-foo' => 'disabled'
  2188. ));
  2189. $expected = array(
  2190. 'div' => array('class' => 'input checkbox'),
  2191. 'input' => array('type' => 'hidden', 'name' => 'User[disabled]', 'value' => '0'),
  2192. array('input' => array(
  2193. 'type' => 'checkbox',
  2194. 'name' => 'User[disabled]',
  2195. 'value' => '1',
  2196. 'id' => 'user-disabled',
  2197. 'data-foo' => 'disabled'
  2198. )),
  2199. 'label' => array('for' => 'user-disabled'),
  2200. 'Disabled',
  2201. '/label',
  2202. '/div'
  2203. );
  2204. $this->assertTags($result, $expected);
  2205. }
  2206. /**
  2207. * test form->input() with datetime
  2208. *
  2209. * @return void
  2210. */
  2211. public function testInputDatetime() {
  2212. $this->Form = $this->getMock(
  2213. 'Cake\View\Helper\FormHelper',
  2214. ['datetime'],
  2215. [new View(null)]
  2216. );
  2217. $this->Form->expects($this->once())->method('datetime')
  2218. ->with('prueba', [
  2219. 'type' => 'datetime',
  2220. 'timeFormat' => 24,
  2221. 'minYear' => 2008,
  2222. 'maxYear' => 2011,
  2223. 'interval' => 15,
  2224. 'options' => null,
  2225. 'empty' => false,
  2226. 'id' => 'prueba',
  2227. 'required' => false,
  2228. ])
  2229. ->will($this->returnValue('This is it!'));
  2230. $result = $this->Form->input('prueba', array(
  2231. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  2232. 'maxYear' => 2011, 'interval' => 15
  2233. ));
  2234. $expected = array(
  2235. 'div' => array('class' => 'input datetime'),
  2236. 'label' => array('for' => 'prueba'),
  2237. 'Prueba',
  2238. '/label',
  2239. 'This is it!',
  2240. '/div'
  2241. );
  2242. $this->assertTags($result, $expected);
  2243. }
  2244. /**
  2245. * Test generating checkboxes with disabled elements.
  2246. *
  2247. * @return void
  2248. */
  2249. public function testInputCheckboxWithDisabledElements() {
  2250. $this->markTestIncomplete('Need to revisit once models work again.');
  2251. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2252. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2253. $expected = array(
  2254. array('div' => array('class' => 'input select')),
  2255. array('label' => array('for' => "ContactMultiple")),
  2256. 'Multiple',
  2257. '/label',
  2258. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2259. array('div' => array('class' => 'checkbox')),
  2260. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2261. array('label' => array('for' => "ContactMultiple1")),
  2262. 'One',
  2263. '/label',
  2264. '/div',
  2265. array('div' => array('class' => 'checkbox')),
  2266. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2267. array('label' => array('for' => "ContactMultiple2")),
  2268. 'Two',
  2269. '/label',
  2270. '/div',
  2271. array('div' => array('class' => 'checkbox')),
  2272. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2273. array('label' => array('for' => "ContactMultiple3")),
  2274. 'Three',
  2275. '/label',
  2276. '/div',
  2277. '/div'
  2278. );
  2279. $this->assertTags($result, $expected);
  2280. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  2281. $expected = array(
  2282. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'),
  2283. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1/2', 'id' => 'ModelField12')),
  2284. 'label' => array('for' => 'ModelField12'),
  2285. 'half',
  2286. '/label'
  2287. );
  2288. $this->assertTags($result, $expected);
  2289. $disabled = array('2', 3);
  2290. $expected = array(
  2291. array('div' => array('class' => 'input select')),
  2292. array('label' => array('for' => "ContactMultiple")),
  2293. 'Multiple',
  2294. '/label',
  2295. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2296. array('div' => array('class' => 'checkbox')),
  2297. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2298. array('label' => array('for' => "ContactMultiple1")),
  2299. 'One',
  2300. '/label',
  2301. '/div',
  2302. array('div' => array('class' => 'checkbox')),
  2303. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2304. array('label' => array('for' => "ContactMultiple2")),
  2305. 'Two',
  2306. '/label',
  2307. '/div',
  2308. array('div' => array('class' => 'checkbox')),
  2309. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2310. array('label' => array('for' => "ContactMultiple3")),
  2311. 'Three',
  2312. '/label',
  2313. '/div',
  2314. '/div'
  2315. );
  2316. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2317. $this->assertTags($result, $expected);
  2318. // make sure 50 does only disable 50, and not 50f5c0cf
  2319. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2320. $disabled = array(50);
  2321. $expected = array(
  2322. array('div' => array('class' => 'input select')),
  2323. array('label' => array('for' => "ContactMultiple")),
  2324. 'Multiple',
  2325. '/label',
  2326. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2327. array('div' => array('class' => 'checkbox')),
  2328. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2329. array('label' => array('for' => "ContactMultiple50")),
  2330. 'Fifty',
  2331. '/label',
  2332. '/div',
  2333. array('div' => array('class' => 'checkbox')),
  2334. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2335. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2336. 'Stringy',
  2337. '/label',
  2338. '/div',
  2339. '/div'
  2340. );
  2341. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2342. $this->assertTags($result, $expected);
  2343. }
  2344. /**
  2345. * test input name with leading integer, ensure attributes are generated correctly.
  2346. *
  2347. * @return void
  2348. */
  2349. public function testInputWithLeadingInteger() {
  2350. $result = $this->Form->text('0.Node.title');
  2351. $expected = array(
  2352. 'input' => array('name' => '0[Node][title]', 'type' => 'text')
  2353. );
  2354. $this->assertTags($result, $expected);
  2355. }
  2356. /**
  2357. * test form->input() with select type inputs.
  2358. *
  2359. * @return void
  2360. */
  2361. public function testInputSelectType() {
  2362. $result = $this->Form->input('email', array(
  2363. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2364. );
  2365. $expected = array(
  2366. 'div' => array('class' => 'input select'),
  2367. 'label' => array('for' => 'email'),
  2368. 'Email',
  2369. '/label',
  2370. array('select' => array('name' => 'email', 'id' => 'email')),
  2371. array('option' => array('value' => '')),
  2372. '/option',
  2373. array('option' => array('value' => 'è')),
  2374. 'Firést',
  2375. '/option',
  2376. array('option' => array('value' => 'é')),
  2377. 'Secoènd',
  2378. '/option',
  2379. '/select',
  2380. '/div'
  2381. );
  2382. $this->assertTags($result, $expected);
  2383. $result = $this->Form->input('email', array(
  2384. 'options' => array('First', 'Second'), 'empty' => true)
  2385. );
  2386. $expected = array(
  2387. 'div' => array('class' => 'input select'),
  2388. 'label' => array('for' => 'email'),
  2389. 'Email',
  2390. '/label',
  2391. array('select' => array('name' => 'email', 'id' => 'email')),
  2392. array('option' => array('value' => '')),
  2393. '/option',
  2394. array('option' => array('value' => '0')),
  2395. 'First',
  2396. '/option',
  2397. array('option' => array('value' => '1')),
  2398. 'Second',
  2399. '/option',
  2400. '/select',
  2401. '/div'
  2402. );
  2403. $this->assertTags($result, $expected);
  2404. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2405. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2406. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2407. $expected = array(
  2408. 'div' => array('class' => 'input select'),
  2409. 'label' => array('for' => 'model-user-id'),
  2410. 'User',
  2411. '/label',
  2412. 'select' => array('name' => 'Model[user_id]', 'id' => 'model-user-id'),
  2413. array('option' => array('value' => '')),
  2414. '/option',
  2415. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2416. 'good',
  2417. '/option',
  2418. array('option' => array('value' => 'other')),
  2419. 'bad',
  2420. '/option',
  2421. '/select',
  2422. '/div'
  2423. );
  2424. $this->assertTags($result, $expected);
  2425. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2426. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2427. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2428. $expected = array(
  2429. 'div' => array('class' => 'input select'),
  2430. 'label' => array('for' => 'thing-user-id'),
  2431. 'User',
  2432. '/label',
  2433. 'select' => array('name' => 'Thing[user_id]', 'id' => 'thing-user-id'),
  2434. array('option' => array('value' => '')),
  2435. 'Some Empty',
  2436. '/option',
  2437. array('option' => array('value' => 'value')),
  2438. 'good',
  2439. '/option',
  2440. array('option' => array('value' => 'other')),
  2441. 'bad',
  2442. '/option',
  2443. '/select',
  2444. '/div'
  2445. );
  2446. $this->assertTags($result, $expected);
  2447. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2448. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2449. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2450. $expected = array(
  2451. 'div' => array('class' => 'input select'),
  2452. 'label' => array('for' => 'thing-user-id'),
  2453. 'User',
  2454. '/label',
  2455. 'select' => array('name' => 'Thing[user_id]', 'id' => 'thing-user-id'),
  2456. array('option' => array('value' => '')),
  2457. 'Some Empty',
  2458. '/option',
  2459. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2460. 'good',
  2461. '/option',
  2462. array('option' => array('value' => 'other')),
  2463. 'bad',
  2464. '/option',
  2465. '/select',
  2466. '/div'
  2467. );
  2468. $this->assertTags($result, $expected);
  2469. $this->Form->data = array();
  2470. $result = $this->Form->input('Publisher.id', array(
  2471. 'label' => 'Publisher',
  2472. 'type' => 'select',
  2473. 'multiple' => 'checkbox',
  2474. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2475. ));
  2476. $expected = array(
  2477. array('div' => array('class' => 'input select')),
  2478. array('label' => array('for' => 'publisher-id')),
  2479. 'Publisher',
  2480. '/label',
  2481. 'input' => array('type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''),
  2482. array('div' => array('class' => 'checkbox')),
  2483. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1')),
  2484. array('label' => array('for' => 'publisher-id-value-1')),
  2485. 'Label 1',
  2486. '/label',
  2487. '/div',
  2488. array('div' => array('class' => 'checkbox')),
  2489. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2')),
  2490. array('label' => array('for' => 'publisher-id-value-2')),
  2491. 'Label 2',
  2492. '/label',
  2493. '/div',
  2494. '/div'
  2495. );
  2496. $this->assertTags($result, $expected);
  2497. }
  2498. /**
  2499. * test that input() and a non standard primary key makes a hidden input by default.
  2500. *
  2501. * @return void
  2502. */
  2503. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2504. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  2505. $this->Form->create($this->article);
  2506. $result = $this->Form->input('title');
  2507. $expected = array(
  2508. 'input' => array('type' => 'hidden', 'name' => 'title', 'id' => 'title'),
  2509. );
  2510. $this->assertTags($result, $expected);
  2511. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  2512. $this->Form->create($this->article);
  2513. $result = $this->Form->input('title');
  2514. $expected = array(
  2515. 'input' => array('type' => 'hidden', 'name' => 'title', 'id' => 'title'),
  2516. );
  2517. $this->assertTags($result, $expected);
  2518. $result = $this->Form->input('body');
  2519. $expected = array(
  2520. 'input' => array('type' => 'hidden', 'name' => 'body', 'id' => 'body'),
  2521. );
  2522. $this->assertTags($result, $expected);
  2523. }
  2524. /**
  2525. * test that overriding the magic select type widget is possible
  2526. *
  2527. * @return void
  2528. */
  2529. public function testInputOverridingMagicSelectType() {
  2530. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2531. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2532. $expected = array(
  2533. 'div' => array('class' => 'input text'),
  2534. 'label' => array('for' => 'model-user-id'), 'User', '/label',
  2535. 'input' => array('name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'),
  2536. '/div'
  2537. );
  2538. $this->assertTags($result, $expected);
  2539. //Check that magic types still work for plural/singular vars
  2540. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2541. $result = $this->Form->input('Model.type');
  2542. $expected = array(
  2543. 'div' => array('class' => 'input select'),
  2544. 'label' => array('for' => 'model-type'), 'Type', '/label',
  2545. 'select' => array('name' => 'Model[type]', 'id' => 'model-type'),
  2546. array('option' => array('value' => 'value')), 'good', '/option',
  2547. array('option' => array('value' => 'other')), 'bad', '/option',
  2548. '/select',
  2549. '/div'
  2550. );
  2551. $this->assertTags($result, $expected);
  2552. }
  2553. /**
  2554. * Test that inferred types do not override developer input
  2555. *
  2556. * @return void
  2557. */
  2558. public function testInputMagicTypeDoesNotOverride() {
  2559. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2560. $result = $this->Form->input('Model.user', array('type' => 'checkbox'));
  2561. $expected = array(
  2562. 'div' => array('class' => 'input checkbox'),
  2563. array('input' => array(
  2564. 'type' => 'hidden',
  2565. 'name' => 'Model[user]',
  2566. 'value' => 0,
  2567. )),
  2568. array('input' => array(
  2569. 'name' => 'Model[user]',
  2570. 'type' => 'checkbox',
  2571. 'id' => 'model-user',
  2572. 'value' => 1
  2573. )),
  2574. 'label' => array('for' => 'model-user'), 'User', '/label',
  2575. '/div'
  2576. );
  2577. $this->assertTags($result, $expected);
  2578. }
  2579. /**
  2580. * Test that magic input() selects are created for type=number
  2581. *
  2582. * @return void
  2583. */
  2584. public function testInputMagicSelectForTypeNumber() {
  2585. TableRegistry::get('ValidateUsers', [
  2586. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2587. ]);
  2588. $entity = new Entity(['balance' => 1]);
  2589. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  2590. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2591. $result = $this->Form->input('ValidateUser.balance');
  2592. $expected = array(
  2593. 'div' => array('class' => 'input select'),
  2594. 'label' => array('for' => 'validateuser-balance'),
  2595. 'Balance',
  2596. '/label',
  2597. 'select' => array('name' => 'ValidateUser[balance]', 'id' => 'validateuser-balance'),
  2598. array('option' => array('value' => '0')),
  2599. 'nothing',
  2600. '/option',
  2601. array('option' => array('value' => '1', 'selected' => 'selected')),
  2602. 'some',
  2603. '/option',
  2604. array('option' => array('value' => '100')),
  2605. 'a lot',
  2606. '/option',
  2607. '/select',
  2608. '/div'
  2609. );
  2610. $this->assertTags($result, $expected);
  2611. }
  2612. /**
  2613. * Test that magic input() selects can easily be converted into radio types without error.
  2614. *
  2615. * @return void
  2616. */
  2617. public function testInputMagicSelectChangeToRadio() {
  2618. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2619. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2620. $this->assertRegExp('/input type="radio"/', $result);
  2621. }
  2622. /**
  2623. * testFormInputs method
  2624. *
  2625. * test correct results from form::inputs().
  2626. *
  2627. * @return void
  2628. */
  2629. public function testFormInputs() {
  2630. $this->markTestIncomplete('Need to revisit once models work again.');
  2631. $this->Form->create('Cake\Test\TestCase\View\Helper\Contact');
  2632. $result = $this->Form->inputs('The Legend');
  2633. $expected = array(
  2634. '<fieldset',
  2635. '<legend',
  2636. 'The Legend',
  2637. '/legend',
  2638. '*/fieldset',
  2639. );
  2640. $this->assertTags($result, $expected);
  2641. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2642. $expected = array(
  2643. 'fieldset' => array('class' => 'classy-stuff'),
  2644. '<legend',
  2645. 'Field of Dreams',
  2646. '/legend',
  2647. '*/fieldset'
  2648. );
  2649. $this->assertTags($result, $expected);
  2650. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2651. $this->assertTags($result, $expected);
  2652. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2653. $this->assertTags($result, $expected);
  2654. $this->Form->create('Contact');
  2655. $this->Form->request['prefix'] = 'admin';
  2656. $this->Form->request['action'] = 'admin_edit';
  2657. $result = $this->Form->inputs();
  2658. $expected = array(
  2659. '<fieldset',
  2660. '<legend',
  2661. 'Edit Contact',
  2662. '/legend',
  2663. '*/fieldset',
  2664. );
  2665. $this->assertTags($result, $expected);
  2666. $this->Form->create('Contact');
  2667. $result = $this->Form->inputs(false);
  2668. $expected = array(
  2669. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2670. array('div' => array('class' => 'input text')),
  2671. '*/div',
  2672. array('div' => array('class' => 'input email')),
  2673. '*/div',
  2674. array('div' => array('class' => 'input tel')),
  2675. '*/div',
  2676. array('div' => array('class' => 'input password')),
  2677. '*/div',
  2678. array('div' => array('class' => 'input date')),
  2679. '*/div',
  2680. array('div' => array('class' => 'input date')),
  2681. '*/div',
  2682. array('div' => array('class' => 'input datetime')),
  2683. '*/div',
  2684. array('div' => array('class' => 'input number')),
  2685. '*/div',
  2686. array('div' => array('class' => 'input select')),
  2687. '*/div',
  2688. );
  2689. $this->assertTags($result, $expected);
  2690. $this->Form->create('Contact');
  2691. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2692. $expected = array(
  2693. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2694. array('div' => array('class' => 'input text')),
  2695. '*/div',
  2696. array('div' => array('class' => 'input email')),
  2697. '*/div',
  2698. array('div' => array('class' => 'input tel')),
  2699. '*/div',
  2700. array('div' => array('class' => 'input password')),
  2701. '*/div',
  2702. array('div' => array('class' => 'input date')),
  2703. '*/div',
  2704. array('div' => array('class' => 'input date')),
  2705. '*/div',
  2706. array('div' => array('class' => 'input datetime')),
  2707. '*/div',
  2708. array('div' => array('class' => 'input number')),
  2709. '*/div',
  2710. array('div' => array('class' => 'input select')),
  2711. '*/div',
  2712. );
  2713. $this->assertTags($result, $expected);
  2714. $this->Form->create('Contact');
  2715. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2716. $this->assertTags($result, $expected);
  2717. $this->Form->create('Contact');
  2718. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2719. $expected = array(
  2720. 'fieldset' => array(),
  2721. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2722. array('div' => array('class' => 'input text')),
  2723. '*/div',
  2724. array('div' => array('class' => 'input email')),
  2725. '*/div',
  2726. array('div' => array('class' => 'input tel')),
  2727. '*/div',
  2728. array('div' => array('class' => 'input password')),
  2729. '*/div',
  2730. array('div' => array('class' => 'input date')),
  2731. '*/div',
  2732. array('div' => array('class' => 'input date')),
  2733. '*/div',
  2734. array('div' => array('class' => 'input datetime')),
  2735. '*/div',
  2736. array('div' => array('class' => 'input number')),
  2737. '*/div',
  2738. array('div' => array('class' => 'input select')),
  2739. '*/div',
  2740. '/fieldset'
  2741. );
  2742. $this->assertTags($result, $expected);
  2743. $this->Form->create('Contact');
  2744. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2745. $expected = array(
  2746. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2747. array('div' => array('class' => 'input text')),
  2748. '*/div',
  2749. array('div' => array('class' => 'input email')),
  2750. '*/div',
  2751. array('div' => array('class' => 'input tel')),
  2752. '*/div',
  2753. array('div' => array('class' => 'input password')),
  2754. '*/div',
  2755. array('div' => array('class' => 'input date')),
  2756. '*/div',
  2757. array('div' => array('class' => 'input date')),
  2758. '*/div',
  2759. array('div' => array('class' => 'input datetime')),
  2760. '*/div',
  2761. array('div' => array('class' => 'input number')),
  2762. '*/div',
  2763. array('div' => array('class' => 'input select')),
  2764. '*/div',
  2765. );
  2766. $this->assertTags($result, $expected);
  2767. $this->Form->create('Contact');
  2768. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2769. $this->assertTags($result, $expected);
  2770. $this->Form->create('Contact');
  2771. $result = $this->Form->inputs('Hello');
  2772. $expected = array(
  2773. 'fieldset' => array(),
  2774. 'legend' => array(),
  2775. 'Hello',
  2776. '/legend',
  2777. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2778. array('div' => array('class' => 'input text')),
  2779. '*/div',
  2780. array('div' => array('class' => 'input email')),
  2781. '*/div',
  2782. array('div' => array('class' => 'input tel')),
  2783. '*/div',
  2784. array('div' => array('class' => 'input password')),
  2785. '*/div',
  2786. array('div' => array('class' => 'input date')),
  2787. '*/div',
  2788. array('div' => array('class' => 'input date')),
  2789. '*/div',
  2790. array('div' => array('class' => 'input datetime')),
  2791. '*/div',
  2792. array('div' => array('class' => 'input number')),
  2793. '*/div',
  2794. array('div' => array('class' => 'input select')),
  2795. '*/div',
  2796. '/fieldset'
  2797. );
  2798. $this->assertTags($result, $expected);
  2799. $this->Form->create('Contact');
  2800. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2801. $expected = array(
  2802. 'fieldset' => array(),
  2803. 'legend' => array(),
  2804. 'Hello',
  2805. '/legend',
  2806. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2807. array('div' => array('class' => 'input text')),
  2808. '*/div',
  2809. array('div' => array('class' => 'input email')),
  2810. '*/div',
  2811. array('div' => array('class' => 'input tel')),
  2812. '*/div',
  2813. array('div' => array('class' => 'input password')),
  2814. '*/div',
  2815. array('div' => array('class' => 'input date')),
  2816. '*/div',
  2817. array('div' => array('class' => 'input date')),
  2818. '*/div',
  2819. array('div' => array('class' => 'input datetime')),
  2820. '*/div',
  2821. array('div' => array('class' => 'input number')),
  2822. '*/div',
  2823. array('div' => array('class' => 'input select')),
  2824. '*/div',
  2825. '/fieldset'
  2826. );
  2827. $this->assertTags($result, $expected);
  2828. $this->Form->create('Contact');
  2829. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2830. $this->assertTags($result, $expected);
  2831. $this->Form->end();
  2832. $this->Form->create(false);
  2833. $expected = array(
  2834. 'fieldset' => array(),
  2835. array('div' => array('class' => 'input text')),
  2836. 'label' => array('for' => 'foo'),
  2837. 'Foo',
  2838. '/label',
  2839. 'input' => array('type' => 'text', 'name' => 'foo', 'id' => 'foo'),
  2840. '*/div',
  2841. '/fieldset'
  2842. );
  2843. $result = $this->Form->inputs(
  2844. array('foo' => array('type' => 'text')),
  2845. array(),
  2846. array('legend' => false)
  2847. );
  2848. $this->assertTags($result, $expected);
  2849. }
  2850. /**
  2851. * testSelectAsCheckbox method
  2852. *
  2853. * test multi-select widget with checkbox formatting.
  2854. *
  2855. * @return void
  2856. */
  2857. public function testSelectAsCheckbox() {
  2858. $result = $this->Form->select(
  2859. 'Model.multi_field',
  2860. array('first', 'second', 'third'),
  2861. array('multiple' => 'checkbox', 'value' => array(0, 1))
  2862. );
  2863. $expected = array(
  2864. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  2865. array('div' => array('class' => 'checkbox')),
  2866. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0')),
  2867. array('label' => array('for' => 'model-multi-field-0', 'class' => 'selected')),
  2868. 'first',
  2869. '/label',
  2870. '/div',
  2871. array('div' => array('class' => 'checkbox')),
  2872. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1')),
  2873. array('label' => array('for' => 'model-multi-field-1', 'class' => 'selected')),
  2874. 'second',
  2875. '/label',
  2876. '/div',
  2877. array('div' => array('class' => 'checkbox')),
  2878. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2')),
  2879. array('label' => array('for' => 'model-multi-field-2')),
  2880. 'third',
  2881. '/label',
  2882. '/div',
  2883. );
  2884. $this->assertTags($result, $expected);
  2885. $result = $this->Form->select(
  2886. 'Model.multi_field',
  2887. array('1/2' => 'half'),
  2888. array('multiple' => 'checkbox')
  2889. );
  2890. $expected = array(
  2891. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  2892. array('div' => array('class' => 'checkbox')),
  2893. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2')),
  2894. array('label' => array('for' => 'model-multi-field-1-2')),
  2895. 'half',
  2896. '/label',
  2897. '/div',
  2898. );
  2899. $this->assertTags($result, $expected);
  2900. }
  2901. /**
  2902. * testLabel method
  2903. *
  2904. * test label generation.
  2905. *
  2906. * @return void
  2907. */
  2908. public function testLabel() {
  2909. $result = $this->Form->label('Person.name');
  2910. $this->assertTags($result, array('label' => array('for' => 'person-name'), 'Name', '/label'));
  2911. $result = $this->Form->label('Person.name');
  2912. $this->assertTags($result, array('label' => array('for' => 'person-name'), 'Name', '/label'));
  2913. $result = $this->Form->label('Person.first_name');
  2914. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), 'First Name', '/label'));
  2915. $result = $this->Form->label('Person.first_name', 'Your first name');
  2916. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), 'Your first name', '/label'));
  2917. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2918. $this->assertTags($result, array('label' => array('for' => 'person-first-name', 'class' => 'my-class'), 'Your first name', '/label'));
  2919. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2920. $this->assertTags($result, array('label' => array('for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2921. $result = $this->Form->label('Person.first_name', '');
  2922. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), '/label'));
  2923. $result = $this->Form->label('Person.2.name', '');
  2924. $this->assertTags($result, array('label' => array('for' => 'person-2-name'), '/label'));
  2925. }
  2926. /**
  2927. * testTextbox method
  2928. *
  2929. * test textbox element generation
  2930. *
  2931. * @return void
  2932. */
  2933. public function testTextbox() {
  2934. $result = $this->Form->text('Model.field');
  2935. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]')));
  2936. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2937. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Model[field]')));
  2938. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2939. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'theID')));
  2940. }
  2941. /**
  2942. * Test that text() hooks up with request data and error fields.
  2943. *
  2944. * @return void
  2945. */
  2946. public function testTextBoxDataAndError() {
  2947. $this->article['errors'] = [
  2948. 'Contact' => ['text' => 'wrong']
  2949. ];
  2950. $this->Form->create($this->article);
  2951. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2952. $result = $this->Form->text('Model.text');
  2953. $expected = [
  2954. 'input' => [
  2955. 'type' => 'text',
  2956. 'name' => 'Model[text]',
  2957. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  2958. ]
  2959. ];
  2960. $this->assertTags($result, $expected);
  2961. $this->Form->request->data['Contact']['text'] = 'test';
  2962. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  2963. $expected = [
  2964. 'input' => [
  2965. 'type' => 'text',
  2966. 'name' => 'Contact[text]',
  2967. 'value' => 'test',
  2968. 'id' => 'theID',
  2969. 'class' => 'form-error'
  2970. ]
  2971. ];
  2972. $this->assertTags($result, $expected);
  2973. }
  2974. /**
  2975. * testDefaultValue method
  2976. *
  2977. * Test default value setting
  2978. *
  2979. * @return void
  2980. */
  2981. public function testTextDefaultValue() {
  2982. $this->Form->request->data['Model']['field'] = 'test';
  2983. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2984. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  2985. $this->assertTags($result, $expected);
  2986. unset($this->Form->request->data['Model']['field']);
  2987. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2988. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  2989. $this->assertTags($result, $expected);
  2990. }
  2991. /**
  2992. * testError method
  2993. *
  2994. * Test field error generation
  2995. *
  2996. * @return void
  2997. */
  2998. public function testError() {
  2999. $this->article['errors'] = [
  3000. 'Article' => ['field' => 'email']
  3001. ];
  3002. $this->Form->create($this->article);
  3003. $result = $this->Form->error('Article.field');
  3004. $expected = [
  3005. ['div' => ['class' => 'error-message']],
  3006. 'email',
  3007. '/div',
  3008. ];
  3009. $this->assertTags($result, $expected);
  3010. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>");
  3011. $expected = [
  3012. ['div' => ['class' => 'error-message']],
  3013. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  3014. '/div',
  3015. ];
  3016. $this->assertTags($result, $expected);
  3017. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>", ['escape' => false]);
  3018. $expected = [
  3019. ['div' => ['class' => 'error-message']],
  3020. '<strong', 'Badness!', '/strong',
  3021. '/div',
  3022. ];
  3023. $this->assertTags($result, $expected);
  3024. }
  3025. /**
  3026. * Test error with nested lists.
  3027. *
  3028. * @return void
  3029. */
  3030. public function testErrorMessages() {
  3031. $this->article['errors'] = [
  3032. 'Article' => ['field' => 'email']
  3033. ];
  3034. $this->Form->create($this->article);
  3035. $result = $this->Form->error('Article.field', array(
  3036. 'email' => 'No good!'
  3037. ));
  3038. $expected = array(
  3039. 'div' => array('class' => 'error-message'),
  3040. 'No good!',
  3041. '/div'
  3042. );
  3043. $this->assertTags($result, $expected);
  3044. }
  3045. /**
  3046. * test error() with multiple messages.
  3047. *
  3048. * @return void
  3049. */
  3050. public function testErrorMultipleMessages() {
  3051. $this->article['errors'] = [
  3052. 'field' => ['notEmpty', 'email', 'Something else']
  3053. ];
  3054. $this->Form->create($this->article);
  3055. $result = $this->Form->error('field', array(
  3056. 'notEmpty' => 'Cannot be empty',
  3057. 'email' => 'No good!'
  3058. ));
  3059. $expected = array(
  3060. 'div' => array('class' => 'error-message'),
  3061. 'ul' => array(),
  3062. '<li', 'Cannot be empty', '/li',
  3063. '<li', 'No good!', '/li',
  3064. '<li', 'Something else', '/li',
  3065. '/ul',
  3066. '/div'
  3067. );
  3068. $this->assertTags($result, $expected);
  3069. }
  3070. /**
  3071. * testPassword method
  3072. *
  3073. * Test password element generation
  3074. *
  3075. * @return void
  3076. */
  3077. public function testPassword() {
  3078. $this->article['errors'] = [
  3079. 'Contact' => [
  3080. 'passwd' => 1
  3081. ]
  3082. ];
  3083. $this->Form->create($this->article);
  3084. $result = $this->Form->password('Contact.field');
  3085. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[field]')));
  3086. $this->Form->request->data['Contact']['passwd'] = 'test';
  3087. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3088. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3089. }
  3090. /**
  3091. * testRadio method
  3092. *
  3093. * Test radio element set generation
  3094. *
  3095. * @return void
  3096. */
  3097. public function testRadio() {
  3098. $result = $this->Form->radio('Model.field', array('option A'));
  3099. $expected = array(
  3100. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  3101. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  3102. 'label' => array('for' => 'model-field-0'),
  3103. 'option A',
  3104. '/label'
  3105. );
  3106. $this->assertTags($result, $expected);
  3107. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3108. $expected = array(
  3109. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  3110. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  3111. array('label' => array('for' => 'model-field-0')),
  3112. 'option A',
  3113. '/label',
  3114. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1')),
  3115. array('label' => array('for' => 'model-field-1')),
  3116. 'option B',
  3117. '/label',
  3118. );
  3119. $this->assertTags($result, $expected);
  3120. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3121. $expected = array(
  3122. 'input' => array('type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''),
  3123. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male')),
  3124. array('label' => array('for' => 'employee-gender-male')),
  3125. 'Male',
  3126. '/label',
  3127. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female')),
  3128. array('label' => array('for' => 'employee-gender-female')),
  3129. 'Female',
  3130. '/label',
  3131. );
  3132. $this->assertTags($result, $expected);
  3133. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'Model[custom]'));
  3134. $expected = array(
  3135. array('input' => array('type' => 'hidden', 'name' => 'Model[custom]', 'value' => '')),
  3136. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0')),
  3137. array('label' => array('for' => 'model-custom-0')),
  3138. 'option A',
  3139. '/label',
  3140. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1')),
  3141. array('label' => array('for' => 'model-custom-1')),
  3142. 'option B',
  3143. '/label',
  3144. );
  3145. $this->assertTags($result, $expected);
  3146. }
  3147. /**
  3148. * test generating radio input inside label ala twitter bootstrap
  3149. *
  3150. * @return void
  3151. */
  3152. public function testRadioInputInsideLabel() {
  3153. $this->Form->templates([
  3154. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3155. 'radioContainer' => '{{label}}'
  3156. ]);
  3157. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  3158. $expected = [
  3159. ['input' => [
  3160. 'type' => 'hidden',
  3161. 'name' => 'Model[field]',
  3162. 'value' => ''
  3163. ]],
  3164. ['label' => ['for' => 'model-field-0']],
  3165. ['input' => [
  3166. 'type' => 'radio',
  3167. 'name' => 'Model[field]',
  3168. 'value' => '0',
  3169. 'id' => 'model-field-0'
  3170. ]],
  3171. 'option A',
  3172. '/label',
  3173. ['label' => ['for' => 'model-field-1']],
  3174. ['input' => [
  3175. 'type' => 'radio',
  3176. 'name' => 'Model[field]',
  3177. 'value' => '1',
  3178. 'id' => 'model-field-1'
  3179. ]],
  3180. 'option B',
  3181. '/label',
  3182. ];
  3183. $this->assertTags($result, $expected);
  3184. }
  3185. /**
  3186. * test disabling the hidden input for radio buttons
  3187. *
  3188. * @return void
  3189. */
  3190. public function testRadioHiddenInputDisabling() {
  3191. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3192. $expected = array(
  3193. 'input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'),
  3194. 'label' => array('for' => 'model-1-field-0'),
  3195. 'option A',
  3196. '/label'
  3197. );
  3198. $this->assertTags($result, $expected);
  3199. }
  3200. /**
  3201. * testSelect method
  3202. *
  3203. * Test select element generation.
  3204. *
  3205. * @return void
  3206. */
  3207. public function testSelect() {
  3208. $result = $this->Form->select('Model.field', array());
  3209. $expected = array(
  3210. 'select' => array('name' => 'Model[field]'),
  3211. '/select'
  3212. );
  3213. $this->assertTags($result, $expected);
  3214. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3215. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3216. $expected = array(
  3217. 'select' => array('name' => 'Model[field]'),
  3218. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3219. 'good',
  3220. '/option',
  3221. array('option' => array('value' => 'other')),
  3222. 'bad',
  3223. '/option',
  3224. '/select'
  3225. );
  3226. $this->assertTags($result, $expected);
  3227. $this->Form->request->data = array();
  3228. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3229. $expected = array(
  3230. 'select' => array('name' => 'Model[field]'),
  3231. array('option' => array('value' => 'value')),
  3232. 'good',
  3233. '/option',
  3234. array('option' => array('value' => 'other')),
  3235. 'bad',
  3236. '/option',
  3237. '/select'
  3238. );
  3239. $this->assertTags($result, $expected);
  3240. $result = $this->Form->select(
  3241. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3242. array('empty' => false)
  3243. );
  3244. $expected = array(
  3245. 'select' => array('name' => 'Model[field]'),
  3246. array('option' => array('value' => 'first')),
  3247. 'first &quot;html&quot; &lt;chars&gt;',
  3248. '/option',
  3249. array('option' => array('value' => 'second')),
  3250. 'value',
  3251. '/option',
  3252. '/select'
  3253. );
  3254. $this->assertTags($result, $expected);
  3255. $result = $this->Form->select(
  3256. 'Model.field',
  3257. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3258. array('escape' => false, 'empty' => false)
  3259. );
  3260. $expected = array(
  3261. 'select' => array('name' => 'Model[field]'),
  3262. array('option' => array('value' => 'first')),
  3263. 'first "html" <chars>',
  3264. '/option',
  3265. array('option' => array('value' => 'second')),
  3266. 'value',
  3267. '/option',
  3268. '/select'
  3269. );
  3270. $this->assertTags($result, $expected);
  3271. $options = array(
  3272. array('value' => 'first', 'text' => 'First'),
  3273. array('value' => 'first', 'text' => 'Another First'),
  3274. );
  3275. $result = $this->Form->select(
  3276. 'Model.field',
  3277. $options,
  3278. array('escape' => false, 'empty' => false)
  3279. );
  3280. $expected = array(
  3281. 'select' => array('name' => 'Model[field]'),
  3282. array('option' => array('value' => 'first')),
  3283. 'First',
  3284. '/option',
  3285. array('option' => array('value' => 'first')),
  3286. 'Another First',
  3287. '/option',
  3288. '/select'
  3289. );
  3290. $this->assertTags($result, $expected);
  3291. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3292. $result = $this->Form->select(
  3293. 'Model.contact_id',
  3294. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3295. array('escape' => false, 'empty' => 'pick something')
  3296. );
  3297. $expected = array(
  3298. 'select' => array('name' => 'Model[contact_id]'),
  3299. array('option' => array('value' => '')), 'pick something', '/option',
  3300. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3301. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3302. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3303. '/select'
  3304. );
  3305. $this->assertTags($result, $expected);
  3306. $this->Form->request->data['Model']['field'] = 0;
  3307. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3308. $expected = array(
  3309. 'select' => array('name' => 'Model[field]'),
  3310. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3311. array('option' => array('value' => '1')), 'Yes', '/option',
  3312. '/select'
  3313. );
  3314. $this->assertTags($result, $expected);
  3315. }
  3316. /**
  3317. * test select() with required and disabled attributes.
  3318. *
  3319. * @return void
  3320. */
  3321. public function testSelectRequired() {
  3322. $this->article['required'] = [
  3323. 'user_id' => true
  3324. ];
  3325. $this->Form->create($this->article);
  3326. $result = $this->Form->select('user_id', array('option A'));
  3327. $expected = array(
  3328. 'select' => array(
  3329. 'name' => 'user_id',
  3330. 'required' => 'required'
  3331. ),
  3332. array('option' => array('value' => '0')), 'option A', '/option',
  3333. '/select'
  3334. );
  3335. $this->assertTags($result, $expected);
  3336. $result = $this->Form->select('user_id', array('option A'), array('disabled' => true));
  3337. $expected = array(
  3338. 'select' => array(
  3339. 'name' => 'user_id',
  3340. 'disabled' => 'disabled'
  3341. ),
  3342. array('option' => array('value' => '0')), 'option A', '/option',
  3343. '/select'
  3344. );
  3345. $this->assertTags($result, $expected);
  3346. }
  3347. /**
  3348. * testNestedSelect method
  3349. *
  3350. * test select element generation with optgroups
  3351. *
  3352. * @return void
  3353. */
  3354. public function testNestedSelect() {
  3355. $result = $this->Form->select(
  3356. 'Model.field',
  3357. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3358. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3359. )), array('empty' => false)
  3360. );
  3361. $expected = array(
  3362. 'select' => array('name' => 'Model[field]'),
  3363. array('option' => array('value' => 1)),
  3364. 'One',
  3365. '/option',
  3366. array('option' => array('value' => 2)),
  3367. 'Two',
  3368. '/option',
  3369. array('optgroup' => array('label' => 'Three')),
  3370. array('option' => array('value' => 3)),
  3371. 'Three',
  3372. '/option',
  3373. array('option' => array('value' => 4)),
  3374. 'Four',
  3375. '/option',
  3376. array('option' => array('value' => 5)),
  3377. 'Five',
  3378. '/option',
  3379. '/optgroup',
  3380. '/select'
  3381. );
  3382. $this->assertTags($result, $expected);
  3383. }
  3384. /**
  3385. * testSelectMultiple method
  3386. *
  3387. * test generation of multiple select elements
  3388. *
  3389. * @return void
  3390. */
  3391. public function testSelectMultiple() {
  3392. $options = array('first', 'second', 'third');
  3393. $result = $this->Form->select(
  3394. 'Model.multi_field',
  3395. $options,
  3396. array('multiple' => true)
  3397. );
  3398. $expected = array(
  3399. 'input' => array(
  3400. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3401. ),
  3402. 'select' => array(
  3403. 'name' => 'Model[multi_field][]',
  3404. 'multiple' => 'multiple'
  3405. ),
  3406. array('option' => array('value' => '0')),
  3407. 'first',
  3408. '/option',
  3409. array('option' => array('value' => '1')),
  3410. 'second',
  3411. '/option',
  3412. array('option' => array('value' => '2')),
  3413. 'third',
  3414. '/option',
  3415. '/select'
  3416. );
  3417. $this->assertTags($result, $expected);
  3418. $result = $this->Form->select(
  3419. 'Model.multi_field',
  3420. $options,
  3421. array('multiple' => 'multiple')
  3422. );
  3423. $this->assertTags($result, $expected);
  3424. }
  3425. /**
  3426. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  3427. *
  3428. * @return void
  3429. */
  3430. public function testCheckboxZeroValue() {
  3431. $result = $this->Form->input('User.get_spam', array(
  3432. 'type' => 'checkbox',
  3433. 'value' => '0',
  3434. 'hiddenField' => '1',
  3435. ));
  3436. $expected = array(
  3437. 'div' => array('class' => 'input checkbox'),
  3438. array('input' => array(
  3439. 'type' => 'hidden', 'name' => 'User[get_spam]',
  3440. 'value' => '1'
  3441. )),
  3442. array('input' => array(
  3443. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  3444. 'value' => '0', 'id' => 'user-get-spam'
  3445. )),
  3446. 'label' => array('for' => 'user-get-spam'),
  3447. 'Get Spam',
  3448. '/label',
  3449. '/div'
  3450. );
  3451. $this->assertTags($result, $expected);
  3452. }
  3453. /**
  3454. * test generation of habtm select boxes.
  3455. *
  3456. * @return void
  3457. */
  3458. public function testHabtmSelectBox() {
  3459. $this->markTestIncomplete('Need to revisit once models work again.');
  3460. $this->View->viewVars['contactTags'] = array(
  3461. 1 => 'blue',
  3462. 2 => 'red',
  3463. 3 => 'green'
  3464. );
  3465. $this->Form->request->data = array(
  3466. 'Contact' => array(),
  3467. 'ContactTag' => array(
  3468. array(
  3469. 'id' => '1',
  3470. 'name' => 'blue'
  3471. ),
  3472. array(
  3473. 'id' => 3,
  3474. 'name' => 'green'
  3475. )
  3476. )
  3477. );
  3478. $this->Form->create('Contact');
  3479. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3480. $expected = array(
  3481. 'input' => array(
  3482. 'type' => 'hidden', 'name' => 'ContactTag[ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3483. ),
  3484. 'select' => array(
  3485. 'name' => 'ContactTag[ContactTag][]', 'id' => 'ContactTagContactTag',
  3486. 'multiple' => 'multiple'
  3487. ),
  3488. array('option' => array('value' => '1', 'selected' => 'selected')),
  3489. 'blue',
  3490. '/option',
  3491. array('option' => array('value' => '2')),
  3492. 'red',
  3493. '/option',
  3494. array('option' => array('value' => '3', 'selected' => 'selected')),
  3495. 'green',
  3496. '/option',
  3497. '/select'
  3498. );
  3499. $this->assertTags($result, $expected);
  3500. // make sure only 50 is selected, and not 50f5c0cf
  3501. $this->View->viewVars['contactTags'] = array(
  3502. '1' => 'blue',
  3503. '50f5c0cf' => 'red',
  3504. '50' => 'green'
  3505. );
  3506. $this->Form->request->data = array(
  3507. 'Contact' => array(),
  3508. 'ContactTag' => array(
  3509. array(
  3510. 'id' => 1,
  3511. 'name' => 'blue'
  3512. ),
  3513. array(
  3514. 'id' => 50,
  3515. 'name' => 'green'
  3516. )
  3517. )
  3518. );
  3519. $this->Form->create('Contact');
  3520. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3521. $expected = array(
  3522. 'input' => array(
  3523. 'type' => 'hidden', 'name' => 'ContactTag[ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3524. ),
  3525. 'select' => array(
  3526. 'name' => 'ContactTag[ContactTag][]', 'id' => 'ContactTagContactTag',
  3527. 'multiple' => 'multiple'
  3528. ),
  3529. array('option' => array('value' => '1', 'selected' => 'selected')),
  3530. 'blue',
  3531. '/option',
  3532. array('option' => array('value' => '50f5c0cf')),
  3533. 'red',
  3534. '/option',
  3535. array('option' => array('value' => '50', 'selected' => 'selected')),
  3536. 'green',
  3537. '/option',
  3538. '/select'
  3539. );
  3540. $this->assertTags($result, $expected);
  3541. }
  3542. /**
  3543. * test generation of multi select elements in checkbox format
  3544. *
  3545. * @return void
  3546. */
  3547. public function testSelectMultipleCheckboxes() {
  3548. $result = $this->Form->select(
  3549. 'Model.multi_field',
  3550. array('first', 'second', 'third'),
  3551. array('multiple' => 'checkbox')
  3552. );
  3553. $expected = array(
  3554. 'input' => array(
  3555. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3556. ),
  3557. array('div' => array('class' => 'checkbox')),
  3558. array('input' => array(
  3559. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3560. 'value' => '0', 'id' => 'model-multi-field-0'
  3561. )),
  3562. array('label' => array('for' => 'model-multi-field-0')),
  3563. 'first',
  3564. '/label',
  3565. '/div',
  3566. array('div' => array('class' => 'checkbox')),
  3567. array('input' => array(
  3568. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3569. 'value' => '1', 'id' => 'model-multi-field-1'
  3570. )),
  3571. array('label' => array('for' => 'model-multi-field-1')),
  3572. 'second',
  3573. '/label',
  3574. '/div',
  3575. array('div' => array('class' => 'checkbox')),
  3576. array('input' => array(
  3577. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3578. 'value' => '2', 'id' => 'model-multi-field-2'
  3579. )),
  3580. array('label' => array('for' => 'model-multi-field-2')),
  3581. 'third',
  3582. '/label',
  3583. '/div'
  3584. );
  3585. $this->assertTags($result, $expected);
  3586. $result = $this->Form->select(
  3587. 'Model.multi_field',
  3588. array('a+' => 'first', 'a++' => 'second', 'a+++' => 'third'),
  3589. array('multiple' => 'checkbox')
  3590. );
  3591. $expected = array(
  3592. 'input' => array(
  3593. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3594. ),
  3595. array('div' => array('class' => 'checkbox')),
  3596. array('input' => array(
  3597. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3598. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  3599. )),
  3600. array('label' => array('for' => 'model-multi-field-a+')),
  3601. 'first',
  3602. '/label',
  3603. '/div',
  3604. array('div' => array('class' => 'checkbox')),
  3605. array('input' => array(
  3606. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3607. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  3608. )),
  3609. array('label' => array('for' => 'model-multi-field-a++')),
  3610. 'second',
  3611. '/label',
  3612. '/div',
  3613. array('div' => array('class' => 'checkbox')),
  3614. array('input' => array(
  3615. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3616. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  3617. )),
  3618. array('label' => array('for' => 'model-multi-field-a+++')),
  3619. 'third',
  3620. '/label',
  3621. '/div'
  3622. );
  3623. $this->assertTags($result, $expected);
  3624. $result = $this->Form->select(
  3625. 'Model.multi_field',
  3626. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  3627. array('multiple' => 'checkbox')
  3628. );
  3629. $expected = array(
  3630. 'input' => array(
  3631. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3632. ),
  3633. array('div' => array('class' => 'checkbox')),
  3634. array('input' => array(
  3635. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3636. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  3637. )),
  3638. array('label' => array('for' => 'model-multi-field-a-b')),
  3639. 'first',
  3640. '/label',
  3641. '/div',
  3642. array('div' => array('class' => 'checkbox')),
  3643. array('input' => array(
  3644. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3645. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  3646. )),
  3647. array('label' => array('for' => 'model-multi-field-a-b1')),
  3648. 'second',
  3649. '/label',
  3650. '/div',
  3651. array('div' => array('class' => 'checkbox')),
  3652. array('input' => array(
  3653. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3654. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  3655. )),
  3656. array('label' => array('for' => 'model-multi-field-a-b2')),
  3657. 'third',
  3658. '/label',
  3659. '/div'
  3660. );
  3661. $this->assertTags($result, $expected);
  3662. }
  3663. /**
  3664. * Ensure that multiCheckbox reads from the request data.
  3665. *
  3666. * @return void
  3667. */
  3668. public function testSelectMultipleCheckboxRequestData() {
  3669. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3670. $result = $this->Form->select(
  3671. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3672. );
  3673. $expected = array(
  3674. 'input' => array(
  3675. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  3676. ),
  3677. array('div' => array('class' => 'checkbox')),
  3678. array('input' => array(
  3679. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  3680. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  3681. )),
  3682. array('label' => array('for' => 'model-tags-1', 'class' => 'selected')),
  3683. 'first',
  3684. '/label',
  3685. '/div',
  3686. array('div' => array('class' => 'checkbox')),
  3687. array('input' => array(
  3688. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  3689. 'value' => 'Array', 'id' => 'model-tags-array'
  3690. )),
  3691. array('label' => array('for' => 'model-tags-array')),
  3692. 'Array',
  3693. '/label',
  3694. '/div'
  3695. );
  3696. $this->assertTags($result, $expected);
  3697. }
  3698. /**
  3699. * Checks the security hash array generated for multiple-input checkbox elements
  3700. *
  3701. * @return void
  3702. */
  3703. public function testSelectMultipleCheckboxSecurity() {
  3704. $this->Form->request->params['_Token'] = 'testKey';
  3705. $this->assertEquals(array(), $this->Form->fields);
  3706. $result = $this->Form->select(
  3707. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  3708. array('multiple' => 'checkbox')
  3709. );
  3710. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  3711. $result = $this->Form->secure($this->Form->fields);
  3712. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  3713. $this->assertRegExp('/"' . $key . '"/', $result);
  3714. }
  3715. /**
  3716. * Multiple select elements should always be secured as they always participate
  3717. * in the POST data.
  3718. *
  3719. * @return void
  3720. */
  3721. public function testSelectMultipleSecureWithNoOptions() {
  3722. $this->assertEquals(array(), $this->Form->fields);
  3723. $this->Form->select(
  3724. 'Model.select',
  3725. array(),
  3726. array('multiple' => true)
  3727. );
  3728. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3729. }
  3730. /**
  3731. * When a select box has no options it should not be added to the fields list
  3732. * as it always fail post validation.
  3733. *
  3734. * @return void
  3735. */
  3736. public function testSelectNoSecureWithNoOptions() {
  3737. $this->Form->request->params['_Token'] = 'testkey';
  3738. $this->assertEquals([], $this->Form->fields);
  3739. $this->Form->select(
  3740. 'Model.select',
  3741. []
  3742. );
  3743. $this->assertEquals([], $this->Form->fields);
  3744. $this->Form->select(
  3745. 'Model.user_id',
  3746. [],
  3747. ['empty' => true]
  3748. );
  3749. $this->assertEquals(array('Model.user_id'), $this->Form->fields);
  3750. }
  3751. /**
  3752. * testInputMultipleCheckboxes method
  3753. *
  3754. * test input() resulting in multi select elements being generated.
  3755. *
  3756. * @return void
  3757. */
  3758. public function testInputMultipleCheckboxes() {
  3759. $result = $this->Form->input('Model.multi_field', array(
  3760. 'options' => array('first', 'second', 'third'),
  3761. 'multiple' => 'checkbox'
  3762. ));
  3763. $expected = array(
  3764. array('div' => array('class' => 'input select')),
  3765. array('label' => array('for' => 'model-multi-field')),
  3766. 'Multi Field',
  3767. '/label',
  3768. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  3769. array('div' => array('class' => 'checkbox')),
  3770. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0')),
  3771. array('label' => array('for' => 'model-multi-field-0')),
  3772. 'first',
  3773. '/label',
  3774. '/div',
  3775. array('div' => array('class' => 'checkbox')),
  3776. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1')),
  3777. array('label' => array('for' => 'model-multi-field-1')),
  3778. 'second',
  3779. '/label',
  3780. '/div',
  3781. array('div' => array('class' => 'checkbox')),
  3782. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2')),
  3783. array('label' => array('for' => 'model-multi-field-2')),
  3784. 'third',
  3785. '/label',
  3786. '/div',
  3787. '/div'
  3788. );
  3789. $this->assertTags($result, $expected);
  3790. $result = $this->Form->input('Model.multi_field', array(
  3791. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3792. 'multiple' => 'checkbox'
  3793. ));
  3794. $expected = array(
  3795. array('div' => array('class' => 'input select')),
  3796. array('label' => array('for' => 'model-multi-field')),
  3797. 'Multi Field',
  3798. '/label',
  3799. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  3800. array('div' => array('class' => 'checkbox')),
  3801. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a')),
  3802. array('label' => array('for' => 'model-multi-field-a')),
  3803. 'first',
  3804. '/label',
  3805. '/div',
  3806. array('div' => array('class' => 'checkbox')),
  3807. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b')),
  3808. array('label' => array('for' => 'model-multi-field-b')),
  3809. 'second',
  3810. '/label',
  3811. '/div',
  3812. array('div' => array('class' => 'checkbox')),
  3813. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c')),
  3814. array('label' => array('for' => 'model-multi-field-c')),
  3815. 'third',
  3816. '/label',
  3817. '/div',
  3818. '/div'
  3819. );
  3820. $this->assertTags($result, $expected);
  3821. }
  3822. /**
  3823. * testSelectHiddenFieldOmission method
  3824. *
  3825. * test that select() with 'hiddenField' => false omits the hidden field
  3826. *
  3827. * @return void
  3828. */
  3829. public function testSelectHiddenFieldOmission() {
  3830. $result = $this->Form->select('Model.multi_field',
  3831. array('first', 'second'),
  3832. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  3833. );
  3834. $this->assertNotContains('type="hidden"', $result);
  3835. }
  3836. /**
  3837. * test that select() with multiple = checkbox works with overriding name attribute.
  3838. *
  3839. * @return void
  3840. */
  3841. public function testSelectCheckboxMultipleOverrideName() {
  3842. $result = $this->Form->select('category', ['1', '2'], [
  3843. 'multiple' => 'checkbox',
  3844. 'name' => 'fish',
  3845. ]);
  3846. $expected = array(
  3847. 'input' => array('type' => 'hidden', 'name' => 'fish', 'value' => ''),
  3848. array('div' => array('class' => 'checkbox')),
  3849. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0')),
  3850. array('label' => array('for' => 'fish-0')), '1', '/label',
  3851. '/div',
  3852. array('div' => array('class' => 'checkbox')),
  3853. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1')),
  3854. array('label' => array('for' => 'fish-1')), '2', '/label',
  3855. '/div'
  3856. );
  3857. $this->assertTags($result, $expected);
  3858. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  3859. 'name' => 'fish',
  3860. ]);
  3861. $this->assertTags($result, $expected);
  3862. }
  3863. /**
  3864. * testCheckbox method
  3865. *
  3866. * Test generation of checkboxes
  3867. *
  3868. * @return void
  3869. */
  3870. public function testCheckbox() {
  3871. $result = $this->Form->checkbox('Model.field');
  3872. $expected = array(
  3873. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'),
  3874. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1'))
  3875. );
  3876. $this->assertTags($result, $expected);
  3877. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  3878. $expected = array(
  3879. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'),
  3880. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => 'myvalue', 'id' => 'theID'))
  3881. );
  3882. $this->assertTags($result, $expected);
  3883. }
  3884. /**
  3885. * testCheckboxDefaultValue method
  3886. *
  3887. * Test default value setting on checkbox() method
  3888. *
  3889. * @return void
  3890. */
  3891. public function testCheckboxDefaultValue() {
  3892. $this->Form->request->data['Model']['field'] = false;
  3893. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3894. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1')));
  3895. unset($this->Form->request->data['Model']['field']);
  3896. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3897. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked')));
  3898. $this->Form->request->data['Model']['field'] = true;
  3899. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3900. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked')));
  3901. unset($this->Form->request->data['Model']['field']);
  3902. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3903. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1')));
  3904. }
  3905. /**
  3906. * Test checkbox being checked or having errors.
  3907. *
  3908. * @return void
  3909. */
  3910. public function testCheckboxCheckedAndError() {
  3911. $this->article['errors'] = [
  3912. 'published' => true
  3913. ];
  3914. $this->Form->request->data['published'] = 'myvalue';
  3915. $this->Form->create($this->article);
  3916. $result = $this->Form->checkbox('published', array('id' => 'theID', 'value' => 'myvalue'));
  3917. $expected = array(
  3918. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  3919. array('input' => array(
  3920. 'type' => 'checkbox',
  3921. 'name' => 'published',
  3922. 'value' => 'myvalue',
  3923. 'id' => 'theID',
  3924. 'checked' => 'checked',
  3925. 'class' => 'form-error'
  3926. ))
  3927. );
  3928. $this->assertTags($result, $expected);
  3929. $this->Form->request->data['published'] = '';
  3930. $result = $this->Form->checkbox('published');
  3931. $expected = array(
  3932. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  3933. array('input' => array('type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error'))
  3934. );
  3935. $this->assertTags($result, $expected);
  3936. }
  3937. /**
  3938. * test checkbox() with a custom name attribute
  3939. *
  3940. * @return void
  3941. */
  3942. public function testCheckboxCustomNameAttribute() {
  3943. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  3944. $expected = array(
  3945. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0'),
  3946. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1'))
  3947. );
  3948. $this->assertTags($result, $expected);
  3949. }
  3950. /**
  3951. * Test that the hidden input for checkboxes can be omitted or set to a
  3952. * specific value.
  3953. *
  3954. * @return void
  3955. */
  3956. public function testCheckboxHiddenField() {
  3957. $result = $this->Form->checkbox('UserForm.something', array(
  3958. 'hiddenField' => false
  3959. ));
  3960. $expected = array(
  3961. 'input' => array(
  3962. 'type' => 'checkbox',
  3963. 'name' => 'UserForm[something]',
  3964. 'value' => '1'
  3965. ),
  3966. );
  3967. $this->assertTags($result, $expected);
  3968. $result = $this->Form->checkbox('UserForm.something', array(
  3969. 'value' => 'Y',
  3970. 'hiddenField' => 'N',
  3971. ));
  3972. $expected = array(
  3973. array('input' => array(
  3974. 'type' => 'hidden', 'name' => 'UserForm[something]',
  3975. 'value' => 'N'
  3976. )),
  3977. array('input' => array(
  3978. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  3979. 'value' => 'Y'
  3980. )),
  3981. );
  3982. $this->assertTags($result, $expected);
  3983. }
  3984. /**
  3985. * testDateTime method
  3986. *
  3987. * Test generation of date/time select elements
  3988. *
  3989. * @return void
  3990. */
  3991. public function testDateTime() {
  3992. extract($this->dateRegex);
  3993. $result = $this->Form->dateTime('Contact.date', array('empty' => false));
  3994. $now = strtotime('now');
  3995. $expected = array(
  3996. array('select' => array('name' => 'Contact[date][month]')),
  3997. $monthsRegex,
  3998. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  3999. date('F', $now),
  4000. '/option',
  4001. '*/select',
  4002. array('select' => array('name' => 'Contact[date][day]')),
  4003. $daysRegex,
  4004. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4005. date('j', $now),
  4006. '/option',
  4007. '*/select',
  4008. array('select' => array('name' => 'Contact[date][year]')),
  4009. $yearsRegex,
  4010. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4011. date('Y', $now),
  4012. '/option',
  4013. '*/select',
  4014. array('select' => array('name' => 'Contact[date][hour]')),
  4015. $hoursRegex,
  4016. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4017. date('g', $now),
  4018. '/option',
  4019. '*/select',
  4020. array('select' => array('name' => 'Contact[date][minute]')),
  4021. $minutesRegex,
  4022. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4023. date('i', $now),
  4024. '/option',
  4025. '*/select',
  4026. array('select' => array('name' => 'Contact[date][meridian]')),
  4027. $meridianRegex,
  4028. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4029. date('a', $now),
  4030. '/option',
  4031. '*/select'
  4032. );
  4033. $this->assertTags($result, $expected);
  4034. }
  4035. /**
  4036. * Test empty defaulting to true for datetime.
  4037. *
  4038. * @return void
  4039. */
  4040. public function testDatetimeEmpty() {
  4041. extract($this->dateRegex);
  4042. $now = strtotime('now');
  4043. $result = $this->Form->dateTime('Contact.date', array(
  4044. 'empty' => true,
  4045. ));
  4046. $expected = array(
  4047. array('select' => array('name' => 'Contact[date][month]')),
  4048. $monthsRegex,
  4049. array('option' => array('value' => '')),
  4050. '/option',
  4051. '*/select',
  4052. array('select' => array('name' => 'Contact[date][day]')),
  4053. $daysRegex,
  4054. array('option' => array('value' => '')),
  4055. '/option',
  4056. '*/select',
  4057. array('select' => array('name' => 'Contact[date][year]')),
  4058. $yearsRegex,
  4059. array('option' => array('value' => '')),
  4060. '/option',
  4061. '*/select',
  4062. array('select' => array('name' => 'Contact[date][hour]')),
  4063. $hoursRegex,
  4064. array('option' => array('value' => '')),
  4065. '/option',
  4066. '*/select',
  4067. array('select' => array('name' => 'Contact[date][minute]')),
  4068. $minutesRegex,
  4069. array('option' => array('value' => '')),
  4070. '/option',
  4071. '*/select',
  4072. array('select' => array('name' => 'Contact[date][meridian]')),
  4073. $meridianRegex,
  4074. array('option' => array('value' => '')),
  4075. '/option',
  4076. '*/select'
  4077. );
  4078. $this->assertTags($result, $expected);
  4079. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4080. }
  4081. /**
  4082. * Test datetime with interval option.
  4083. *
  4084. * @return void
  4085. */
  4086. public function testDatetimeMinuteInterval() {
  4087. extract($this->dateRegex);
  4088. $now = strtotime('now');
  4089. $result = $this->Form->dateTime('Contact.date', array(
  4090. 'interval' => 5,
  4091. 'value' => ''
  4092. ));
  4093. $expected = array(
  4094. array('select' => array('name' => 'Contact[date][month]')),
  4095. $monthsRegex,
  4096. array('option' => array('selected' => 'selected', 'value' => '')),
  4097. '/option',
  4098. '*/select',
  4099. array('select' => array('name' => 'Contact[date][day]')),
  4100. $daysRegex,
  4101. array('option' => array('selected' => 'selected', 'value' => '')),
  4102. '/option',
  4103. '*/select',
  4104. array('select' => array('name' => 'Contact[date][year]')),
  4105. $yearsRegex,
  4106. array('option' => array('selected' => 'selected', 'value' => '')),
  4107. '/option',
  4108. '*/select',
  4109. array('select' => array('name' => 'Contact[date][hour]')),
  4110. $hoursRegex,
  4111. array('option' => array('selected' => 'selected', 'value' => '')),
  4112. '/option',
  4113. '*/select',
  4114. array('select' => array('name' => 'Contact[date][minute]')),
  4115. $minutesRegex,
  4116. array('option' => array('selected' => 'selected', 'value' => '')),
  4117. '/option',
  4118. array('option' => array('value' => '00')),
  4119. '00',
  4120. '/option',
  4121. array('option' => array('value' => '05')),
  4122. '05',
  4123. '/option',
  4124. array('option' => array('value' => '10')),
  4125. '10',
  4126. '/option',
  4127. '*/select',
  4128. );
  4129. $this->assertTags($result, $expected);
  4130. }
  4131. /**
  4132. * Test dateTime with rounding
  4133. *
  4134. * @return void
  4135. */
  4136. public function testDateTimeRounding() {
  4137. $this->Form->request->data['Contact'] = array(
  4138. 'date' => array(
  4139. 'day' => '13',
  4140. 'month' => '12',
  4141. 'year' => '2010',
  4142. 'hour' => '04',
  4143. 'minute' => '19',
  4144. 'meridian' => 'AM'
  4145. )
  4146. );
  4147. $result = $this->Form->dateTime('Contact.date', array('interval' => 15));
  4148. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  4149. $result = $this->Form->dateTime('Contact.date', array('interval' => 15, 'round' => 'up'));
  4150. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  4151. $result = $this->Form->dateTime('Contact.date', array('interval' => 5, 'round' => 'down'));
  4152. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  4153. }
  4154. /**
  4155. * test that datetime() and default values work.
  4156. *
  4157. * @return void
  4158. */
  4159. public function testDatetimeWithDefault() {
  4160. $result = $this->Form->dateTime('Contact.updated', array('value' => '2009-06-01 11:15:30'));
  4161. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4162. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4163. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4164. $result = $this->Form->dateTime('Contact.updated', array(
  4165. 'default' => '2009-06-01 11:15:30'
  4166. ));
  4167. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4168. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4169. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4170. }
  4171. /**
  4172. * testDateTime all zeros
  4173. *
  4174. * @return void
  4175. */
  4176. public function testDateTimeAllZeros() {
  4177. $result = $this->Form->dateTime('Contact.date', array(
  4178. 'timeFormat' => false,
  4179. 'empty' => array('day' => '-', 'month' => '-', 'year' => '-'),
  4180. 'value' => '0000-00-00'
  4181. ));
  4182. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  4183. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  4184. }
  4185. /**
  4186. * testDateTimeEmptyAsArray
  4187. *
  4188. * @return void
  4189. */
  4190. public function testDateTimeEmptyAsArray() {
  4191. $result = $this->Form->dateTime('Contact.date', array(
  4192. 'empty' => array(
  4193. 'day' => 'DAY',
  4194. 'month' => 'MONTH',
  4195. 'year' => 'YEAR',
  4196. 'hour' => 'HOUR',
  4197. 'minute' => 'MINUTE',
  4198. 'meridian' => false
  4199. )
  4200. ));
  4201. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4202. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4203. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4204. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4205. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4206. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4207. $result = $this->Form->dateTime('Contact.date', array(
  4208. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4209. ));
  4210. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4211. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4212. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4213. }
  4214. /**
  4215. * testFormDateTimeMulti method
  4216. *
  4217. * test multiple datetime element generation
  4218. *
  4219. * @return void
  4220. */
  4221. public function testFormDateTimeMulti() {
  4222. extract($this->dateRegex);
  4223. $result = $this->Form->dateTime('Contact.1.updated');
  4224. $this->assertContains('Contact[1][updated][month]', $result);
  4225. $this->assertContains('Contact[1][updated][day]', $result);
  4226. $this->assertContains('Contact[1][updated][year]', $result);
  4227. $this->assertContains('Contact[1][updated][hour]', $result);
  4228. $this->assertContains('Contact[1][updated][minute]', $result);
  4229. $this->assertContains('Contact[1][updated][meridian]', $result);
  4230. }
  4231. /**
  4232. * When changing the date format, the label should always focus the first select box when
  4233. * clicked.
  4234. *
  4235. * @return void
  4236. */
  4237. public function testDateTimeLabelIdMatchesFirstInput() {
  4238. $this->markTestIncomplete('Need to revisit once models work again.');
  4239. $result = $this->Form->input('Model.date', array('type' => 'date'));
  4240. $this->assertContains('label for="ModelDateMonth"', $result);
  4241. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  4242. $this->assertContains('label for="ModelDateDay"', $result);
  4243. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  4244. $this->assertContains('label for="ModelDateYear"', $result);
  4245. }
  4246. /**
  4247. * testMonth method
  4248. *
  4249. * @return void
  4250. */
  4251. public function testMonth() {
  4252. $result = $this->Form->month('Model.field', ['value' => '']);
  4253. $expected = array(
  4254. array('select' => array('name' => 'Model[field][month]')),
  4255. array('option' => array('value' => '', 'selected' => 'selected')),
  4256. '/option',
  4257. array('option' => array('value' => '01')),
  4258. date('F', strtotime('2008-01-01 00:00:00')),
  4259. '/option',
  4260. array('option' => array('value' => '02')),
  4261. date('F', strtotime('2008-02-01 00:00:00')),
  4262. '/option',
  4263. '*/select',
  4264. );
  4265. $this->assertTags($result, $expected);
  4266. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  4267. $expected = array(
  4268. array('select' => array('name' => 'Model[field][month]')),
  4269. array('option' => array('selected' => 'selected', 'value' => '')),
  4270. '/option',
  4271. array('option' => array('value' => '01')),
  4272. date('F', strtotime('2008-01-01 00:00:00')),
  4273. '/option',
  4274. array('option' => array('value' => '02')),
  4275. date('F', strtotime('2008-02-01 00:00:00')),
  4276. '/option',
  4277. '*/select',
  4278. );
  4279. $this->assertTags($result, $expected);
  4280. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  4281. $expected = array(
  4282. array('select' => array('name' => 'Model[field][month]')),
  4283. array('option' => array('selected' => 'selected', 'value' => '')),
  4284. '/option',
  4285. array('option' => array('value' => '01')),
  4286. '1',
  4287. '/option',
  4288. array('option' => array('value' => '02')),
  4289. '2',
  4290. '/option',
  4291. '*/select',
  4292. );
  4293. $this->assertTags($result, $expected);
  4294. $monthNames = [
  4295. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  4296. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  4297. ];
  4298. $result = $this->Form->month('Model.field', array('value' => '1', 'monthNames' => $monthNames));
  4299. $expected = array(
  4300. array('select' => array('name' => 'Model[field][month]')),
  4301. array('option' => array('value' => '')),
  4302. '/option',
  4303. array('option' => array('value' => '01', 'selected' => 'selected')),
  4304. 'Jan',
  4305. '/option',
  4306. array('option' => array('value' => '02')),
  4307. 'Feb',
  4308. '/option',
  4309. '*/select',
  4310. );
  4311. $this->assertTags($result, $expected);
  4312. $this->Form->request->data['Project']['release'] = '2050-02-10';
  4313. $result = $this->Form->month('Project.release');
  4314. $expected = array(
  4315. array('select' => array('name' => 'Project[release][month]')),
  4316. array('option' => array('value' => '')),
  4317. '/option',
  4318. array('option' => array('value' => '01')),
  4319. 'January',
  4320. '/option',
  4321. array('option' => array('value' => '02', 'selected' => 'selected')),
  4322. 'February',
  4323. '/option',
  4324. '*/select',
  4325. );
  4326. $this->assertTags($result, $expected);
  4327. }
  4328. /**
  4329. * testDay method
  4330. *
  4331. * @return void
  4332. */
  4333. public function testDay() {
  4334. extract($this->dateRegex);
  4335. $result = $this->Form->day('Model.field', array('value' => false));
  4336. $expected = array(
  4337. array('select' => array('name' => 'Model[field][day]')),
  4338. array('option' => array('selected' => 'selected', 'value' => '')),
  4339. '/option',
  4340. array('option' => array('value' => '01')),
  4341. '1',
  4342. '/option',
  4343. array('option' => array('value' => '02')),
  4344. '2',
  4345. '/option',
  4346. $daysRegex,
  4347. '/select',
  4348. );
  4349. $this->assertTags($result, $expected);
  4350. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  4351. $result = $this->Form->day('Model.field');
  4352. $expected = array(
  4353. array('select' => array('name' => 'Model[field][day]')),
  4354. array('option' => array('value' => '')),
  4355. '/option',
  4356. array('option' => array('value' => '01')),
  4357. '1',
  4358. '/option',
  4359. array('option' => array('value' => '02')),
  4360. '2',
  4361. '/option',
  4362. $daysRegex,
  4363. array('option' => array('value' => '10', 'selected' => 'selected')),
  4364. '10',
  4365. '/option',
  4366. $daysRegex,
  4367. '/select',
  4368. );
  4369. $this->assertTags($result, $expected);
  4370. $this->Form->request->data['Model']['field'] = '';
  4371. $result = $this->Form->day('Model.field', array('value' => '10'));
  4372. $expected = array(
  4373. array('select' => array('name' => 'Model[field][day]')),
  4374. array('option' => array('value' => '')),
  4375. '/option',
  4376. array('option' => array('value' => '01')),
  4377. '1',
  4378. '/option',
  4379. array('option' => array('value' => '02')),
  4380. '2',
  4381. '/option',
  4382. $daysRegex,
  4383. array('option' => array('value' => '10', 'selected' => 'selected')),
  4384. '10',
  4385. '/option',
  4386. $daysRegex,
  4387. '/select',
  4388. );
  4389. $this->assertTags($result, $expected);
  4390. $this->Form->request->data['Project']['release'] = '2050-10-10';
  4391. $result = $this->Form->day('Project.release');
  4392. $expected = array(
  4393. array('select' => array('name' => 'Project[release][day]')),
  4394. array('option' => array('value' => '')),
  4395. '/option',
  4396. array('option' => array('value' => '01')),
  4397. '1',
  4398. '/option',
  4399. array('option' => array('value' => '02')),
  4400. '2',
  4401. '/option',
  4402. $daysRegex,
  4403. array('option' => array('value' => '10', 'selected' => 'selected')),
  4404. '10',
  4405. '/option',
  4406. $daysRegex,
  4407. '/select',
  4408. );
  4409. $this->assertTags($result, $expected);
  4410. }
  4411. /**
  4412. * testMinute method
  4413. *
  4414. * @return void
  4415. */
  4416. public function testMinute() {
  4417. extract($this->dateRegex);
  4418. $result = $this->Form->minute('Model.field', ['value' => '']);
  4419. $expected = array(
  4420. array('select' => array('name' => 'Model[field][minute]')),
  4421. array('option' => array('selected' => 'selected', 'value' => '')),
  4422. '/option',
  4423. array('option' => array('value' => '00')),
  4424. '00',
  4425. '/option',
  4426. array('option' => array('value' => '01')),
  4427. '01',
  4428. '/option',
  4429. array('option' => array('value' => '02')),
  4430. '02',
  4431. '/option',
  4432. $minutesRegex,
  4433. '/select',
  4434. );
  4435. $this->assertTags($result, $expected);
  4436. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4437. $result = $this->Form->minute('Model.field');
  4438. $expected = array(
  4439. array('select' => array('name' => 'Model[field][minute]')),
  4440. array('option' => array('value' => '')),
  4441. '/option',
  4442. array('option' => array('value' => '00')),
  4443. '00',
  4444. '/option',
  4445. array('option' => array('value' => '01')),
  4446. '01',
  4447. '/option',
  4448. array('option' => array('value' => '02')),
  4449. '02',
  4450. '/option',
  4451. $minutesRegex,
  4452. array('option' => array('value' => '12', 'selected' => 'selected')),
  4453. '12',
  4454. '/option',
  4455. $minutesRegex,
  4456. '/select',
  4457. );
  4458. $this->assertTags($result, $expected);
  4459. $this->Form->request->data['Model']['field'] = '';
  4460. $result = $this->Form->minute('Model.field', array('interval' => 5));
  4461. $expected = array(
  4462. array('select' => array('name' => 'Model[field][minute]')),
  4463. array('option' => array('selected' => 'selected', 'value' => '')),
  4464. '/option',
  4465. array('option' => array('value' => '00')),
  4466. '00',
  4467. '/option',
  4468. array('option' => array('value' => '05')),
  4469. '05',
  4470. '/option',
  4471. array('option' => array('value' => '10')),
  4472. '10',
  4473. '/option',
  4474. $minutesRegex,
  4475. '/select',
  4476. );
  4477. $this->assertTags($result, $expected);
  4478. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  4479. $result = $this->Form->minute('Model.field', array('interval' => 5));
  4480. $expected = array(
  4481. array('select' => array('name' => 'Model[field][minute]')),
  4482. array('option' => array('value' => '')),
  4483. '/option',
  4484. array('option' => array('value' => '00')),
  4485. '00',
  4486. '/option',
  4487. array('option' => array('value' => '05')),
  4488. '05',
  4489. '/option',
  4490. array('option' => array('value' => '10', 'selected' => 'selected')),
  4491. '10',
  4492. '/option',
  4493. $minutesRegex,
  4494. '/select',
  4495. );
  4496. $this->assertTags($result, $expected);
  4497. }
  4498. /**
  4499. * Test generating an input for the meridian.
  4500. *
  4501. * @return void
  4502. */
  4503. public function testMeridian() {
  4504. extract($this->dateRegex);
  4505. $now = time();
  4506. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  4507. $expected = [
  4508. array('select' => array('name' => 'Model[field][meridian]')),
  4509. array('option' => array('value' => '')),
  4510. '/option',
  4511. $meridianRegex,
  4512. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4513. date('a', $now),
  4514. '/option',
  4515. '*/select'
  4516. ];
  4517. $this->assertTags($result, $expected);
  4518. }
  4519. /**
  4520. * testHour method
  4521. *
  4522. * @return void
  4523. */
  4524. public function testHour() {
  4525. extract($this->dateRegex);
  4526. $result = $this->Form->hour('Model.field', ['value' => '']);
  4527. $expected = array(
  4528. array('select' => array('name' => 'Model[field][hour]')),
  4529. array('option' => array('selected' => 'selected', 'value' => '')),
  4530. '/option',
  4531. array('option' => array('value' => '01')),
  4532. '1',
  4533. '/option',
  4534. array('option' => array('value' => '02')),
  4535. '2',
  4536. '/option',
  4537. $hoursRegex,
  4538. '/select',
  4539. );
  4540. $this->assertTags($result, $expected);
  4541. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4542. $result = $this->Form->hour('Model.field', ['format' => 12]);
  4543. $expected = array(
  4544. array('select' => array('name' => 'Model[field][hour]')),
  4545. array('option' => array('value' => '')),
  4546. '/option',
  4547. array('option' => array('value' => '01')),
  4548. '1',
  4549. '/option',
  4550. array('option' => array('value' => '02')),
  4551. '2',
  4552. '/option',
  4553. $hoursRegex,
  4554. array('option' => array('value' => '12', 'selected' => 'selected')),
  4555. '12',
  4556. '/option',
  4557. '/select',
  4558. );
  4559. $this->assertTags($result, $expected);
  4560. $this->Form->request->data['Model']['field'] = '';
  4561. $result = $this->Form->hour('Model.field', array('format' => 24, 'value' => '23'));
  4562. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  4563. $result = $this->Form->hour('Model.field', array('format' => 12, 'value' => '23'));
  4564. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  4565. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4566. $result = $this->Form->hour('Model.field', ['format' => 24]);
  4567. $expected = array(
  4568. array('select' => array('name' => 'Model[field][hour]')),
  4569. array('option' => array('value' => '')),
  4570. '/option',
  4571. array('option' => array('value' => '00', 'selected' => 'selected')),
  4572. '0',
  4573. '/option',
  4574. array('option' => array('value' => '01')),
  4575. '1',
  4576. '/option',
  4577. array('option' => array('value' => '02')),
  4578. '2',
  4579. '/option',
  4580. $hoursRegex,
  4581. '/select',
  4582. );
  4583. $this->assertTags($result, $expected);
  4584. unset($this->Form->request->data['Model']['field']);
  4585. $result = $this->Form->hour('Model.field', array('format' => 24, 'value' => 'now'));
  4586. $thisHour = date('H');
  4587. $optValue = date('G');
  4588. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  4589. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  4590. $result = $this->Form->hour('Model.field', ['format' => 24]);
  4591. $expected = array(
  4592. array('select' => array('name' => 'Model[field][hour]')),
  4593. array('option' => array('value' => '')),
  4594. '/option',
  4595. array('option' => array('value' => '00')),
  4596. '0',
  4597. '/option',
  4598. array('option' => array('value' => '01', 'selected' => 'selected')),
  4599. '1',
  4600. '/option',
  4601. array('option' => array('value' => '02')),
  4602. '2',
  4603. '/option',
  4604. $hoursRegex,
  4605. '/select',
  4606. );
  4607. $this->assertTags($result, $expected);
  4608. }
  4609. /**
  4610. * testYear method
  4611. *
  4612. * @return void
  4613. */
  4614. public function testYear() {
  4615. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  4616. $expected = array(
  4617. array('select' => array('name' => 'Model[field][year]')),
  4618. array('option' => array('selected' => 'selected', 'value' => '')),
  4619. '/option',
  4620. array('option' => array('value' => '2007')),
  4621. '2007',
  4622. '/option',
  4623. array('option' => array('value' => '2006')),
  4624. '2006',
  4625. '/option',
  4626. '/select',
  4627. );
  4628. $this->assertTags($result, $expected);
  4629. $result = $this->Form->year('Model.field', [
  4630. 'value' => '',
  4631. 'minYear' => 2006,
  4632. 'maxYear' => 2007,
  4633. 'orderYear' => 'asc'
  4634. ]);
  4635. $expected = array(
  4636. array('select' => array('name' => 'Model[field][year]')),
  4637. array('option' => array('selected' => 'selected', 'value' => '')),
  4638. '/option',
  4639. array('option' => array('value' => '2006')),
  4640. '2006',
  4641. '/option',
  4642. array('option' => array('value' => '2007')),
  4643. '2007',
  4644. '/option',
  4645. '/select',
  4646. );
  4647. $this->assertTags($result, $expected);
  4648. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  4649. $result = $this->Form->year('Contact.published', [
  4650. 'empty' => false,
  4651. 'minYear' => 2006,
  4652. 'maxYear' => 2007,
  4653. ]);
  4654. $expected = array(
  4655. array('select' => array('name' => 'Contact[published][year]')),
  4656. array('option' => array('value' => '2007')),
  4657. '2007',
  4658. '/option',
  4659. array('option' => array('value' => '2006', 'selected' => 'selected')),
  4660. '2006',
  4661. '/option',
  4662. '/select',
  4663. );
  4664. $this->assertTags($result, $expected);
  4665. }
  4666. /**
  4667. * testYearAutoExpandRange method
  4668. *
  4669. * @return void
  4670. */
  4671. public function testYearAutoExpandRange() {
  4672. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  4673. $result = $this->Form->year('User.birthday');
  4674. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4675. $result = $matches[1];
  4676. $expected = range(date('Y') + 5, 1930);
  4677. $this->assertEquals($expected, $result);
  4678. $this->Form->request->data['Project']['release'] = '2050-10-10';
  4679. $result = $this->Form->year('Project.release');
  4680. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4681. $result = $matches[1];
  4682. $expected = range(2050, date('Y') - 5);
  4683. $this->assertEquals($expected, $result);
  4684. $this->Form->request->data['Project']['release'] = '1881-10-10';
  4685. $result = $this->Form->year('Project.release', [
  4686. 'minYear' => 1890,
  4687. 'maxYear' => 1900
  4688. ]);
  4689. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4690. $result = $matches[1];
  4691. $expected = range(1900, 1881);
  4692. $this->assertEquals($expected, $result);
  4693. }
  4694. /**
  4695. * Test that input() accepts the type of date and passes options in.
  4696. *
  4697. * @return void
  4698. */
  4699. public function testInputDate() {
  4700. $this->Form->request->data = array(
  4701. 'month_year' => array('month' => date('m')),
  4702. );
  4703. $this->Form->create($this->article);
  4704. $result = $this->Form->input('month_year', array(
  4705. 'label' => false,
  4706. 'type' => 'date',
  4707. 'minYear' => 2006,
  4708. 'maxYear' => 2008
  4709. ));
  4710. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  4711. $this->assertNotContains('value="2008" selected="selected"', $result);
  4712. }
  4713. /**
  4714. * testInputDateMaxYear method
  4715. *
  4716. * Let's say we want to only allow users born from 2006 to 2008 to register
  4717. * This being the first singup page, we still don't have any data
  4718. *
  4719. * @return void
  4720. */
  4721. public function testInputDateMaxYear() {
  4722. $this->Form->request->data = [];
  4723. $this->Form->create($this->article);
  4724. $result = $this->Form->input('birthday', array(
  4725. 'label' => false,
  4726. 'type' => 'date',
  4727. 'minYear' => 2006,
  4728. 'maxYear' => 2008
  4729. ));
  4730. $this->assertContains('value="2008" selected="selected"', $result);
  4731. }
  4732. /**
  4733. * testTextArea method
  4734. *
  4735. * @return void
  4736. */
  4737. public function testTextArea() {
  4738. $this->Form->request->data = array('field' => 'some test data');
  4739. $result = $this->Form->textarea('field');
  4740. $expected = array(
  4741. 'textarea' => array('name' => 'field'),
  4742. 'some test data',
  4743. '/textarea',
  4744. );
  4745. $this->assertTags($result, $expected);
  4746. $result = $this->Form->textarea('user.bio');
  4747. $expected = array(
  4748. 'textarea' => array('name' => 'user[bio]'),
  4749. '/textarea',
  4750. );
  4751. $this->assertTags($result, $expected);
  4752. $this->Form->request->data = array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars');
  4753. $result = $this->Form->textarea('field');
  4754. $expected = array(
  4755. 'textarea' => array('name' => 'field'),
  4756. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  4757. '/textarea',
  4758. );
  4759. $this->assertTags($result, $expected);
  4760. $this->Form->request->data = [
  4761. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  4762. ];
  4763. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  4764. $expected = array(
  4765. 'textarea' => array('name' => 'Model[field]'),
  4766. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  4767. '/textarea',
  4768. );
  4769. $this->assertTags($result, $expected);
  4770. $result = $this->Form->textarea('0.OtherModel.field');
  4771. $expected = array(
  4772. 'textarea' => array('name' => '0[OtherModel][field]'),
  4773. '/textarea'
  4774. );
  4775. $this->assertTags($result, $expected);
  4776. }
  4777. /**
  4778. * testTextAreaWithStupidCharacters method
  4779. *
  4780. * test text area with non-ascii characters
  4781. *
  4782. * @return void
  4783. */
  4784. public function testTextAreaWithStupidCharacters() {
  4785. $result = $this->Form->textarea('Post.content', [
  4786. 'value' => "GREAT®",
  4787. 'rows' => '15',
  4788. 'cols' => '75'
  4789. ]);
  4790. $expected = [
  4791. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  4792. 'GREAT®',
  4793. '/textarea',
  4794. ];
  4795. $this->assertTags($result, $expected);
  4796. }
  4797. /**
  4798. * testHiddenField method
  4799. *
  4800. * @return void
  4801. */
  4802. public function testHiddenField() {
  4803. $this->article['errors'] = [
  4804. 'field' => true
  4805. ];
  4806. $this->Form->request->data['field'] = 'test';
  4807. $this->Form->create($this->article);
  4808. $result = $this->Form->hidden('field', array('id' => 'theID'));
  4809. $this->assertTags($result, array(
  4810. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test'))
  4811. );
  4812. $result = $this->Form->hidden('field', ['value' => 'my value']);
  4813. $expected = [
  4814. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  4815. ];
  4816. $this->assertTags($result, $expected);
  4817. }
  4818. /**
  4819. * testFileUploadField method
  4820. *
  4821. * @return void
  4822. */
  4823. public function testFileUploadField() {
  4824. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  4825. $result = $this->Form->file('Model.upload');
  4826. $this->assertTags($result, $expected);
  4827. $this->Form->request->data['Model']['upload'] = [
  4828. 'name' => '', 'type' => '', 'tmp_name' => '',
  4829. 'error' => 4, 'size' => 0
  4830. ];
  4831. $result = $this->Form->file('Model.upload');
  4832. $this->assertTags($result, $expected);
  4833. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  4834. $result = $this->Form->file('Model.upload');
  4835. $this->assertTags($result, $expected);
  4836. }
  4837. /**
  4838. * test File upload input on a model not used in create();
  4839. *
  4840. * @return void
  4841. */
  4842. public function testFileUploadOnOtherModel() {
  4843. $this->Form->create($this->article, array('type' => 'file'));
  4844. $result = $this->Form->file('ValidateProfile.city');
  4845. $expected = array(
  4846. 'input' => array('type' => 'file', 'name' => 'ValidateProfile[city]')
  4847. );
  4848. $this->assertTags($result, $expected);
  4849. }
  4850. /**
  4851. * testButton method
  4852. *
  4853. * @return void
  4854. */
  4855. public function testButton() {
  4856. $result = $this->Form->button('Hi');
  4857. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  4858. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  4859. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  4860. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  4861. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  4862. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  4863. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  4864. $result = $this->Form->button('No type', array('type' => false));
  4865. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  4866. $result = $this->Form->button('Upload Text', array(
  4867. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  4868. 'escape' => false
  4869. ));
  4870. $this->assertNotRegExp('/\&039/', $result);
  4871. }
  4872. /**
  4873. * Test that button() makes unlocked fields by default.
  4874. *
  4875. * @return void
  4876. */
  4877. public function testButtonUnlockedByDefault() {
  4878. $this->Form->request->params['_csrfToken'] = 'secured';
  4879. $this->Form->button('Save', array('name' => 'save'));
  4880. $this->Form->button('Clear');
  4881. $result = $this->Form->unlockField();
  4882. $this->assertEquals(array('save'), $result);
  4883. }
  4884. /**
  4885. * testPostButton method
  4886. *
  4887. * @return void
  4888. */
  4889. public function testPostButton() {
  4890. $result = $this->Form->postButton('Hi', '/controller/action');
  4891. $this->assertTags($result, array(
  4892. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  4893. 'div' => array('style' => 'display:none;'),
  4894. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4895. '/div',
  4896. 'button' => array('type' => 'submit'),
  4897. 'Hi',
  4898. '/button',
  4899. '/form'
  4900. ));
  4901. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  4902. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  4903. }
  4904. /**
  4905. * Test using postButton with N dimensional data.
  4906. *
  4907. * @return void
  4908. */
  4909. public function testPostButtonNestedData() {
  4910. $data = array(
  4911. 'one' => array(
  4912. 'two' => array(
  4913. 3, 4, 5
  4914. )
  4915. )
  4916. );
  4917. $result = $this->Form->postButton('Send', '/', array('data' => $data));
  4918. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  4919. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  4920. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  4921. }
  4922. /**
  4923. * Test that postButton adds _Token fields.
  4924. *
  4925. * @return void
  4926. */
  4927. public function testSecurePostButton() {
  4928. $this->Form->request->params['_csrfToken'] = 'testkey';
  4929. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  4930. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  4931. $expected = array(
  4932. 'form' => array(
  4933. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  4934. ),
  4935. array('div' => array('style' => 'display:none;')),
  4936. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  4937. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  4938. '/div',
  4939. 'button' => array('type' => 'submit'),
  4940. 'Delete',
  4941. '/button',
  4942. array('div' => array('style' => 'display:none;')),
  4943. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  4944. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  4945. '/div',
  4946. '/form',
  4947. );
  4948. $this->assertTags($result, $expected);
  4949. }
  4950. /**
  4951. * testPostLink method
  4952. *
  4953. * @return void
  4954. */
  4955. public function testPostLink() {
  4956. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  4957. $this->assertTags($result, array(
  4958. 'form' => array(
  4959. 'method' => 'post', 'action' => '/posts/delete/1',
  4960. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4961. ),
  4962. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4963. '/form',
  4964. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4965. 'Delete',
  4966. '/a'
  4967. ));
  4968. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  4969. $this->assertTags($result, array(
  4970. 'form' => array(
  4971. 'method' => 'post', 'action' => '/posts/delete/1',
  4972. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4973. ),
  4974. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  4975. '/form',
  4976. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4977. 'Delete',
  4978. '/a'
  4979. ));
  4980. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  4981. $this->assertTags($result, array(
  4982. 'form' => array(
  4983. 'method' => 'post', 'action' => '/posts/delete/1',
  4984. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4985. ),
  4986. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4987. '/form',
  4988. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  4989. 'Delete',
  4990. '/a'
  4991. ));
  4992. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  4993. $this->assertTags($result, array(
  4994. 'form' => array(
  4995. 'method' => 'post', 'action' => '/posts/delete/1',
  4996. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4997. ),
  4998. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4999. '/form',
  5000. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;&#039;Confirm&#039; this \\\\&quot;deletion\\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5001. 'Delete',
  5002. '/a'
  5003. ));
  5004. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5005. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  5006. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  5007. $this->assertTags($result, array(
  5008. 'form' => array(
  5009. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  5010. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5011. ),
  5012. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5013. '/form',
  5014. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5015. 'Delete',
  5016. '/a'
  5017. ));
  5018. $result = $this->Form->postLink(
  5019. '',
  5020. array('controller' => 'items', 'action' => 'delete', 10),
  5021. array('class' => 'btn btn-danger', 'escape' => false),
  5022. 'Confirm thing'
  5023. );
  5024. $this->assertTags($result, array(
  5025. 'form' => array(
  5026. 'method' => 'post', 'action' => '/items/delete/10',
  5027. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5028. ),
  5029. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5030. '/form',
  5031. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5032. '/a'
  5033. ));
  5034. }
  5035. /**
  5036. * Test using postLink with N dimensional data.
  5037. *
  5038. * @return void
  5039. */
  5040. public function testPostLinkNestedData() {
  5041. $data = array(
  5042. 'one' => array(
  5043. 'two' => array(
  5044. 3, 4, 5
  5045. )
  5046. )
  5047. );
  5048. $result = $this->Form->postLink('Send', '/', array('data' => $data));
  5049. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  5050. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  5051. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  5052. }
  5053. /**
  5054. * test creating postLinks after a GET form.
  5055. *
  5056. * @return void
  5057. */
  5058. public function testPostLinkAfterGetForm() {
  5059. $this->Form->request->params['_csrfToken'] = 'testkey';
  5060. $this->Form->request->params['_Token'] = 'val';
  5061. $this->Form->create($this->article, array('type' => 'get'));
  5062. $this->Form->end();
  5063. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5064. $this->assertTags($result, array(
  5065. 'form' => array(
  5066. 'method' => 'post', 'action' => '/posts/delete/1',
  5067. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5068. ),
  5069. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5070. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  5071. 'div' => array('style' => 'display:none;'),
  5072. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  5073. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  5074. '/div',
  5075. '/form',
  5076. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5077. 'Delete',
  5078. '/a'
  5079. ));
  5080. }
  5081. /**
  5082. * Test that postLink adds form tags to view block
  5083. *
  5084. * @return void
  5085. */
  5086. public function testPostLinkFormBuffer() {
  5087. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('inline' => false));
  5088. $this->assertTags($result, array(
  5089. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5090. 'Delete',
  5091. '/a'
  5092. ));
  5093. $result = $this->View->fetch('postLink');
  5094. $this->assertTags($result, array(
  5095. 'form' => array(
  5096. 'method' => 'post', 'action' => '/posts/delete/1',
  5097. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5098. ),
  5099. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5100. '/form'
  5101. ));
  5102. $result = $this->Form->postLink('Delete', '/posts/delete/2',
  5103. array('inline' => false, 'method' => 'DELETE')
  5104. );
  5105. $this->assertTags($result, array(
  5106. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5107. 'Delete',
  5108. '/a'
  5109. ));
  5110. $result = $this->View->fetch('postLink');
  5111. $this->assertTags($result, array(
  5112. 'form' => array(
  5113. 'method' => 'post', 'action' => '/posts/delete/1',
  5114. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5115. ),
  5116. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5117. '/form',
  5118. array(
  5119. 'form' => array(
  5120. 'method' => 'post', 'action' => '/posts/delete/2',
  5121. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5122. ),
  5123. ),
  5124. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE')),
  5125. '/form'
  5126. ));
  5127. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => 'foobar'));
  5128. $this->assertTags($result, array(
  5129. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5130. 'Delete',
  5131. '/a'
  5132. ));
  5133. $result = $this->View->fetch('foobar');
  5134. $this->assertTags($result, array(
  5135. 'form' => array(
  5136. 'method' => 'post', 'action' => '/posts/delete/1',
  5137. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5138. ),
  5139. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5140. '/form'
  5141. ));
  5142. }
  5143. /**
  5144. * testSubmitButton method
  5145. *
  5146. * @return void
  5147. */
  5148. public function testSubmitButton() {
  5149. $result = $this->Form->submit('');
  5150. $expected = array(
  5151. 'div' => array('class' => 'submit'),
  5152. 'input' => array('type' => 'submit', 'value' => ''),
  5153. '/div'
  5154. );
  5155. $this->assertTags($result, $expected);
  5156. $result = $this->Form->submit('Test Submit');
  5157. $expected = array(
  5158. 'div' => array('class' => 'submit'),
  5159. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5160. '/div'
  5161. );
  5162. $this->assertTags($result, $expected);
  5163. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5164. $expected = array(
  5165. 'span' => array('class' => 'submit'),
  5166. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5167. '/span'
  5168. );
  5169. $this->assertTags($result, $expected);
  5170. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5171. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5172. $this->assertTags($result, $expected);
  5173. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5174. $expected = array(
  5175. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5176. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5177. '/div'
  5178. );
  5179. $this->assertTags($result, $expected);
  5180. $result = $this->Form->submit('Next >');
  5181. $expected = array(
  5182. 'div' => array('class' => 'submit'),
  5183. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5184. '/div'
  5185. );
  5186. $this->assertTags($result, $expected);
  5187. $result = $this->Form->submit('Next >', array('escape' => false));
  5188. $expected = array(
  5189. 'div' => array('class' => 'submit'),
  5190. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5191. '/div'
  5192. );
  5193. $this->assertTags($result, $expected);
  5194. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5195. $expected = array(
  5196. 'div' => array('class' => 'submit'),
  5197. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5198. '/div'
  5199. );
  5200. $this->assertTags($result, $expected);
  5201. $before = '--before--';
  5202. $after = '--after--';
  5203. $result = $this->Form->submit('Test', array('before' => $before));
  5204. $expected = array(
  5205. 'div' => array('class' => 'submit'),
  5206. '--before--',
  5207. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5208. '/div'
  5209. );
  5210. $this->assertTags($result, $expected);
  5211. $result = $this->Form->submit('Test', array('after' => $after));
  5212. $expected = array(
  5213. 'div' => array('class' => 'submit'),
  5214. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5215. '--after--',
  5216. '/div'
  5217. );
  5218. $this->assertTags($result, $expected);
  5219. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5220. $expected = array(
  5221. 'div' => array('class' => 'submit'),
  5222. '--before--',
  5223. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5224. '--after--',
  5225. '/div'
  5226. );
  5227. $this->assertTags($result, $expected);
  5228. }
  5229. /**
  5230. * test image submit types.
  5231. *
  5232. * @return void
  5233. */
  5234. public function testSubmitImage() {
  5235. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5236. $expected = array(
  5237. 'div' => array('class' => 'submit'),
  5238. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5239. '/div'
  5240. );
  5241. $this->assertTags($result, $expected);
  5242. $result = $this->Form->submit('/relative/cake.power.gif');
  5243. $expected = array(
  5244. 'div' => array('class' => 'submit'),
  5245. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5246. '/div'
  5247. );
  5248. $this->assertTags($result, $expected);
  5249. $result = $this->Form->submit('cake.power.gif');
  5250. $expected = array(
  5251. 'div' => array('class' => 'submit'),
  5252. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5253. '/div'
  5254. );
  5255. $this->assertTags($result, $expected);
  5256. $result = $this->Form->submit('Not.an.image');
  5257. $expected = array(
  5258. 'div' => array('class' => 'submit'),
  5259. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5260. '/div'
  5261. );
  5262. $this->assertTags($result, $expected);
  5263. $after = '--after--';
  5264. $before = '--before--';
  5265. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5266. $expected = array(
  5267. 'div' => array('class' => 'submit'),
  5268. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5269. '--after--',
  5270. '/div'
  5271. );
  5272. $this->assertTags($result, $expected);
  5273. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5274. $expected = array(
  5275. 'div' => array('class' => 'submit'),
  5276. '--before--',
  5277. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5278. '/div'
  5279. );
  5280. $this->assertTags($result, $expected);
  5281. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5282. $expected = array(
  5283. 'div' => array('class' => 'submit'),
  5284. '--before--',
  5285. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5286. '--after--',
  5287. '/div'
  5288. );
  5289. $this->assertTags($result, $expected);
  5290. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  5291. $expected = array(
  5292. 'div' => array('class' => 'submit'),
  5293. '--before--',
  5294. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5295. '--after--',
  5296. '/div'
  5297. );
  5298. $this->assertTags($result, $expected);
  5299. }
  5300. /**
  5301. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5302. * be submitted at a time.
  5303. *
  5304. * @return void
  5305. */
  5306. public function testSubmitUnlockedByDefault() {
  5307. $this->Form->request->params['_Token'] = 'secured';
  5308. $this->Form->submit('Go go');
  5309. $this->Form->submit('Save', array('name' => 'save'));
  5310. $result = $this->Form->unlockField();
  5311. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5312. }
  5313. /**
  5314. * Test submit image with timestamps.
  5315. *
  5316. * @return void
  5317. */
  5318. public function testSubmitImageTimestamp() {
  5319. Configure::write('Asset.timestamp', 'force');
  5320. $result = $this->Form->submit('cake.power.gif');
  5321. $expected = array(
  5322. 'div' => array('class' => 'submit'),
  5323. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  5324. '/div'
  5325. );
  5326. $this->assertTags($result, $expected);
  5327. }
  5328. /**
  5329. * test that datetime() works with GET style forms.
  5330. *
  5331. * @return void
  5332. */
  5333. public function testDateTimeWithGetForms() {
  5334. extract($this->dateRegex);
  5335. $this->Form->create($this->article, array('type' => 'get'));
  5336. $result = $this->Form->datetime('created');
  5337. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  5338. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  5339. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  5340. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  5341. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  5342. $this->assertContains('name="created[meridian]"', $result, 'meridian name attribute is wrong.');
  5343. }
  5344. /**
  5345. * testForMagicInputNonExistingNorValidated method
  5346. *
  5347. * @return void
  5348. */
  5349. public function testForMagicInputNonExistingNorValidated() {
  5350. $this->markTestIncomplete('Need to revisit once models work again.');
  5351. $encoding = strtolower(Configure::read('App.encoding'));
  5352. $result = $this->Form->create('Contact');
  5353. $expected = array(
  5354. 'form' => array(
  5355. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5356. 'accept-charset' => $encoding
  5357. ),
  5358. 'div' => array('style' => 'display:none;'),
  5359. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5360. '/div'
  5361. );
  5362. $this->assertTags($result, $expected);
  5363. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  5364. $expected = array(
  5365. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  5366. 'Non Existing Nor Validated',
  5367. '/label',
  5368. 'input' => array(
  5369. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  5370. 'id' => 'ContactNonExistingNorValidated'
  5371. )
  5372. );
  5373. $this->assertTags($result, $expected);
  5374. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  5375. 'div' => false, 'value' => 'my value'
  5376. ));
  5377. $expected = array(
  5378. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  5379. 'Non Existing Nor Validated',
  5380. '/label',
  5381. 'input' => array(
  5382. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  5383. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  5384. )
  5385. );
  5386. $this->assertTags($result, $expected);
  5387. $this->Form->request->data = array(
  5388. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  5389. ));
  5390. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  5391. $expected = array(
  5392. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  5393. 'Non Existing Nor Validated',
  5394. '/label',
  5395. 'input' => array(
  5396. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  5397. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  5398. )
  5399. );
  5400. $this->assertTags($result, $expected);
  5401. }
  5402. /**
  5403. * testFormMagicInputLabel method
  5404. *
  5405. * @return void
  5406. */
  5407. public function testFormMagicInputLabel() {
  5408. $this->markTestIncomplete('Need to revisit once models work again.');
  5409. TableRegistry::get('Contacts', [
  5410. 'className' => __NAMESPACE__ . '\ContactsTable'
  5411. ]);
  5412. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  5413. $this->Form->templates(['groupContainer' => '{{content}}']);
  5414. $result = $this->Form->input('Contacts.name', array('label' => 'My label'));
  5415. $expected = array(
  5416. 'label' => array('for' => 'contacts-name'),
  5417. 'My label',
  5418. '/label',
  5419. 'input' => array(
  5420. 'type' => 'text', 'name' => 'Contacts[name]',
  5421. 'id' => 'contacts-name', 'maxlength' => '255'
  5422. )
  5423. );
  5424. $this->assertTags($result, $expected);
  5425. $result = $this->Form->input('name', array(
  5426. 'label' => array('class' => 'mandatory')
  5427. ));
  5428. $expected = array(
  5429. 'label' => array('for' => 'name', 'class' => 'mandatory'),
  5430. 'Name',
  5431. '/label',
  5432. 'input' => array(
  5433. 'type' => 'text', 'name' => 'name',
  5434. 'id' => 'name', 'maxlength' => '255'
  5435. )
  5436. );
  5437. $this->assertTags($result, $expected);
  5438. $result = $this->Form->input('name', array(
  5439. 'div' => false,
  5440. 'label' => array('class' => 'mandatory', 'text' => 'My label')
  5441. ));
  5442. $expected = array(
  5443. 'label' => array('for' => 'name', 'class' => 'mandatory'),
  5444. 'My label',
  5445. '/label',
  5446. 'input' => array(
  5447. 'type' => 'text', 'name' => 'name',
  5448. 'id' => 'name', 'maxlength' => '255'
  5449. )
  5450. );
  5451. $this->assertTags($result, $expected);
  5452. $result = $this->Form->input('Contact.name', array(
  5453. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  5454. ));
  5455. $expected = array(
  5456. 'label' => array('for' => 'my_id'),
  5457. 'Name',
  5458. '/label',
  5459. 'input' => array(
  5460. 'type' => 'text', 'name' => 'Contact[name]',
  5461. 'id' => 'my_id', 'maxlength' => '255'
  5462. )
  5463. );
  5464. $this->assertTags($result, $expected);
  5465. $result = $this->Form->input('1.id');
  5466. $this->assertTags($result, array('input' => array(
  5467. 'type' => 'hidden', 'name' => 'Contact[1][id]',
  5468. 'id' => 'Contact1Id'
  5469. )));
  5470. $result = $this->Form->input("1.name");
  5471. $expected = array(
  5472. 'div' => array('class' => 'input text'),
  5473. 'label' => array('for' => 'Contact1Name'),
  5474. 'Name',
  5475. '/label',
  5476. 'input' => array(
  5477. 'type' => 'text', 'name' => 'Contact[1][name]',
  5478. 'id' => 'Contact1Name', 'maxlength' => '255'
  5479. ),
  5480. '/div'
  5481. );
  5482. $this->assertTags($result, $expected);
  5483. $result = $this->Form->input('Contact.1.id');
  5484. $this->assertTags($result, array(
  5485. 'input' => array(
  5486. 'type' => 'hidden', 'name' => 'Contact[1][id]',
  5487. 'id' => 'Contact1Id'
  5488. )
  5489. ));
  5490. $result = $this->Form->input("Model.1.name");
  5491. $expected = array(
  5492. 'div' => array('class' => 'input text'),
  5493. 'label' => array('for' => 'Model1Name'),
  5494. 'Name',
  5495. '/label',
  5496. 'input' => array(
  5497. 'type' => 'text', 'name' => 'Model[1][name]',
  5498. 'id' => 'Model1Name'
  5499. ),
  5500. '/div'
  5501. );
  5502. $this->assertTags($result, $expected);
  5503. }
  5504. /**
  5505. * testFormEnd method
  5506. *
  5507. * @return void
  5508. */
  5509. public function testFormEnd() {
  5510. $this->assertEquals('</form>', $this->Form->end());
  5511. }
  5512. /**
  5513. * testDbLessModel method
  5514. *
  5515. * @return void
  5516. */
  5517. public function testDbLessModel() {
  5518. $this->markTestIncomplete('Need to revisit once models work again.');
  5519. $this->Form->create('TestMail');
  5520. $result = $this->Form->input('name');
  5521. $expected = array(
  5522. 'div' => array('class' => 'input text'),
  5523. 'label' => array('for' => 'TestMailName'),
  5524. 'Name',
  5525. '/label',
  5526. 'input' => array(
  5527. 'name' => 'TestMail[name]', 'type' => 'text',
  5528. 'id' => 'TestMailName'
  5529. ),
  5530. '/div'
  5531. );
  5532. $this->assertTags($result, $expected);
  5533. $this->Form->create('TestMail');
  5534. $result = $this->Form->input('name');
  5535. $expected = array(
  5536. 'div' => array('class' => 'input text'),
  5537. 'label' => array('for' => 'TestMailName'),
  5538. 'Name',
  5539. '/label',
  5540. 'input' => array(
  5541. 'name' => 'TestMail[name]', 'type' => 'text',
  5542. 'id' => 'TestMailName'
  5543. ),
  5544. '/div'
  5545. );
  5546. $this->assertTags($result, $expected);
  5547. }
  5548. /**
  5549. * Test the generation of fields for a multi record form.
  5550. *
  5551. * @return void
  5552. */
  5553. public function testMultiRecordForm() {
  5554. $this->markTestIncomplete('Need to revisit once models work again.');
  5555. $this->Form->create('ValidateProfile');
  5556. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  5557. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  5558. $expected = array(
  5559. 'div' => array('class' => 'input textarea'),
  5560. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  5561. 'Name',
  5562. '/label',
  5563. 'textarea' => array(
  5564. 'id' => 'ValidateProfile1ValidateItem2Name',
  5565. 'name' => 'ValidateProfile[1][ValidateItem][2][name]',
  5566. 'cols' => 30,
  5567. 'rows' => 6
  5568. ),
  5569. 'Value',
  5570. '/textarea',
  5571. '/div'
  5572. );
  5573. $this->assertTags($result, $expected);
  5574. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  5575. $expected = array(
  5576. 'div' => array('class' => 'input date'),
  5577. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  5578. 'Created',
  5579. '/label',
  5580. array('select' => array(
  5581. 'name' => 'ValidateProfile[1][ValidateItem][2][created][month]',
  5582. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  5583. )
  5584. ),
  5585. array('option' => array('value' => '')), '/option',
  5586. $this->dateRegex['monthsRegex'],
  5587. '/select', '-',
  5588. array('select' => array(
  5589. 'name' => 'ValidateProfile[1][ValidateItem][2][created][day]',
  5590. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  5591. )
  5592. ),
  5593. array('option' => array('value' => '')), '/option',
  5594. $this->dateRegex['daysRegex'],
  5595. '/select', '-',
  5596. array('select' => array(
  5597. 'name' => 'ValidateProfile[1][ValidateItem][2][created][year]',
  5598. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  5599. )
  5600. ),
  5601. array('option' => array('value' => '')), '/option',
  5602. $this->dateRegex['yearsRegex'],
  5603. '/select',
  5604. '/div'
  5605. );
  5606. $this->assertTags($result, $expected);
  5607. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  5608. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  5609. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  5610. $expected = array(
  5611. 'div' => array('class' => 'input select error'),
  5612. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  5613. 'Profile',
  5614. '/label',
  5615. 'select' => array(
  5616. 'name' => 'ValidateProfile[1][ValidateItem][2][profile_id]',
  5617. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  5618. 'class' => 'form-error'
  5619. ),
  5620. '/select',
  5621. array('div' => array('class' => 'error-message')),
  5622. 'Error',
  5623. '/div',
  5624. '/div'
  5625. );
  5626. $this->assertTags($result, $expected);
  5627. }
  5628. /**
  5629. * test the correct display of multi-record form validation errors.
  5630. *
  5631. * @return void
  5632. */
  5633. public function testMultiRecordFormValidationErrors() {
  5634. $this->markTestIncomplete('Need to revisit once models work again.');
  5635. $this->Form->create('ValidateProfile');
  5636. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  5637. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  5638. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  5639. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  5640. $result = $this->Form->error('ValidateProfile.2.city');
  5641. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  5642. $result = $this->Form->error('2.city');
  5643. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  5644. }
  5645. /**
  5646. * test the correct display of multi-record form validation errors.
  5647. *
  5648. * @return void
  5649. */
  5650. public function testSaveManyRecordFormValidationErrors() {
  5651. $this->markTestIncomplete('Need to revisit once models work again.');
  5652. $this->Form->create('ValidateUser');
  5653. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  5654. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  5655. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  5656. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  5657. $result = $this->Form->error('ValidateUser.0.city');
  5658. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  5659. }
  5660. /**
  5661. * test that some html5 inputs + FormHelper::__call() work
  5662. *
  5663. * @return void
  5664. */
  5665. public function testHtml5Inputs() {
  5666. $result = $this->Form->email('User.email');
  5667. $expected = array(
  5668. 'input' => array('type' => 'email', 'name' => 'User[email]')
  5669. );
  5670. $this->assertTags($result, $expected);
  5671. $result = $this->Form->search('User.query');
  5672. $expected = array(
  5673. 'input' => array('type' => 'search', 'name' => 'User[query]')
  5674. );
  5675. $this->assertTags($result, $expected);
  5676. $result = $this->Form->search('User.query', array('value' => 'test'));
  5677. $expected = array(
  5678. 'input' => array('type' => 'search', 'name' => 'User[query]', 'value' => 'test')
  5679. );
  5680. $this->assertTags($result, $expected);
  5681. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  5682. $expected = array(
  5683. 'input' => array('type' => 'text', 'name' => 'User[query]', 'value' => 'test')
  5684. );
  5685. $this->assertTags($result, $expected);
  5686. }
  5687. /**
  5688. * Test accessing html5 inputs through input().
  5689. *
  5690. * @return void
  5691. */
  5692. public function testHtml5InputWithInput() {
  5693. $this->Form->create();
  5694. $this->Form->templates(['groupContainer' => '{{content}}']);
  5695. $result = $this->Form->input('website', array(
  5696. 'type' => 'url',
  5697. 'val' => 'http://domain.tld',
  5698. 'label' => false
  5699. ));
  5700. $expected = array(
  5701. 'input' => array('type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld')
  5702. );
  5703. $this->assertTags($result, $expected);
  5704. }
  5705. /**
  5706. * Test errors when field name is missing.
  5707. *
  5708. * @expectedException \Cake\Error\Exception
  5709. * @return void
  5710. */
  5711. public function testHtml5InputException() {
  5712. $this->Form->email();
  5713. }
  5714. /**
  5715. * Tests that formhelper sets required attributes.
  5716. *
  5717. * @return void
  5718. */
  5719. public function testRequiredAttribute() {
  5720. $this->article['required'] = [
  5721. 'title' => true,
  5722. 'body' => false,
  5723. ];
  5724. $this->Form->create($this->article);
  5725. $result = $this->Form->input('title');
  5726. $expected = array(
  5727. 'div' => array('class' => 'input text required'),
  5728. 'label' => array('for' => 'title'),
  5729. 'Title',
  5730. '/label',
  5731. 'input' => array(
  5732. 'type' => 'text',
  5733. 'name' => 'title',
  5734. 'id' => 'title',
  5735. 'required' => 'required',
  5736. ),
  5737. '/div'
  5738. );
  5739. $this->assertTags($result, $expected);
  5740. $result = $this->Form->input('title', ['required' => false]);
  5741. $this->assertNotContains('required', $result);
  5742. $result = $this->Form->input('body');
  5743. $expected = array(
  5744. 'div' => array('class' => 'input text'),
  5745. 'label' => array('for' => 'body'),
  5746. 'Body',
  5747. '/label',
  5748. 'input' => array(
  5749. 'type' => 'text',
  5750. 'name' => 'body',
  5751. 'id' => 'body',
  5752. ),
  5753. '/div'
  5754. );
  5755. $this->assertTags($result, $expected);
  5756. $result = $this->Form->input('body', ['required' => true]);
  5757. $this->assertContains('required', $result);
  5758. }
  5759. }