FormHelperTest.php 258 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc.
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc.
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.View.Helper
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('ClassRegistry', 'Utility');
  20. App::uses('Controller', 'Controller');
  21. App::uses('View', 'View');
  22. App::uses('Model', 'Model');
  23. App::uses('Security', 'Utility');
  24. App::uses('CakeRequest', 'Network');
  25. App::uses('HtmlHelper', 'View/Helper');
  26. App::uses('FormHelper', 'View/Helper');
  27. App::uses('Router', 'Routing');
  28. /**
  29. * ContactTestController class
  30. *
  31. * @package cake
  32. * @package Cake.Test.Case.View.Helper
  33. */
  34. class ContactTestController extends Controller {
  35. /**
  36. * name property
  37. *
  38. * @var string 'ContactTest'
  39. */
  40. public $name = 'ContactTest';
  41. /**
  42. * uses property
  43. *
  44. * @var mixed null
  45. */
  46. public $uses = null;
  47. }
  48. /**
  49. * Contact class
  50. *
  51. * @package cake
  52. * @package Cake.Test.Case.View.Helper
  53. */
  54. class Contact extends CakeTestModel {
  55. /**
  56. * primaryKey property
  57. *
  58. * @var string 'id'
  59. */
  60. public $primaryKey = 'id';
  61. /**
  62. * useTable property
  63. *
  64. * @var bool false
  65. */
  66. public $useTable = false;
  67. /**
  68. * name property
  69. *
  70. * @var string 'Contact'
  71. */
  72. public $name = 'Contact';
  73. /**
  74. * Default schema
  75. *
  76. * @var array
  77. */
  78. protected $_schema = array(
  79. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  80. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  81. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  82. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  83. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  84. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  85. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  86. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  87. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  88. );
  89. /**
  90. * validate property
  91. *
  92. * @var array
  93. */
  94. public $validate = array(
  95. 'non_existing' => array(),
  96. 'idontexist' => array(),
  97. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  98. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  99. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  100. 'imrequiredonboth' => array(
  101. 'required' => array('rule' => 'alphaNumeric'),
  102. ),
  103. 'string_required' => 'notEmpty',
  104. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  105. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  106. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  107. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  108. 'imalsonotrequired' => array(
  109. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  110. 'between' => array('rule' => array('between', 5, 30)),
  111. ),
  112. 'imalsonotrequired2' => array(
  113. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  114. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  115. ),
  116. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  117. 'iamrequiredalways' => array(
  118. 'email' => array('rule' => 'email'),
  119. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  120. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  121. ),
  122. );
  123. /**
  124. * schema method
  125. *
  126. * @return void
  127. */
  128. public function setSchema($schema) {
  129. $this->_schema = $schema;
  130. }
  131. /**
  132. * hasAndBelongsToMany property
  133. *
  134. * @var array
  135. */
  136. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  137. /**
  138. * hasAndBelongsToMany property
  139. *
  140. * @var array
  141. */
  142. public $belongsTo = array('User' => array('className' => 'UserForm'));
  143. }
  144. /**
  145. * ContactTagsContact class
  146. *
  147. * @package cake
  148. * @package Cake.Test.Case.View.Helper
  149. */
  150. class ContactTagsContact extends CakeTestModel {
  151. /**
  152. * useTable property
  153. *
  154. * @var bool false
  155. */
  156. public $useTable = false;
  157. /**
  158. * name property
  159. *
  160. * @var string 'Contact'
  161. */
  162. public $name = 'ContactTagsContact';
  163. /**
  164. * Default schema
  165. *
  166. * @var array
  167. */
  168. protected $_schema = array(
  169. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  170. 'contact_tag_id' => array(
  171. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  172. )
  173. );
  174. /**
  175. * schema method
  176. *
  177. * @return void
  178. */
  179. public function setSchema($schema) {
  180. $this->_schema = $schema;
  181. }
  182. }
  183. /**
  184. * ContactNonStandardPk class
  185. *
  186. * @package cake
  187. * @package Cake.Test.Case.View.Helper
  188. */
  189. class ContactNonStandardPk extends Contact {
  190. /**
  191. * primaryKey property
  192. *
  193. * @var string 'pk'
  194. */
  195. public $primaryKey = 'pk';
  196. /**
  197. * name property
  198. *
  199. * @var string 'ContactNonStandardPk'
  200. */
  201. public $name = 'ContactNonStandardPk';
  202. /**
  203. * schema method
  204. *
  205. * @return void
  206. */
  207. public function schema($field = false) {
  208. $this->_schema = parent::schema();
  209. $this->_schema['pk'] = $this->_schema['id'];
  210. unset($this->_schema['id']);
  211. return $this->_schema;
  212. }
  213. }
  214. /**
  215. * ContactTag class
  216. *
  217. * @package cake
  218. * @package Cake.Test.Case.View.Helper
  219. */
  220. class ContactTag extends Model {
  221. /**
  222. * useTable property
  223. *
  224. * @var bool false
  225. */
  226. public $useTable = false;
  227. /**
  228. * schema definition
  229. *
  230. * @var array
  231. */
  232. protected $_schema = array(
  233. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  234. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  235. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  236. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  237. );
  238. }
  239. /**
  240. * UserForm class
  241. *
  242. * @package cake
  243. * @package Cake.Test.Case.View.Helper
  244. */
  245. class UserForm extends CakeTestModel {
  246. /**
  247. * useTable property
  248. *
  249. * @var bool false
  250. */
  251. public $useTable = false;
  252. /**
  253. * primaryKey property
  254. *
  255. * @var string 'id'
  256. */
  257. public $primaryKey = 'id';
  258. /**
  259. * name property
  260. *
  261. * @var string 'UserForm'
  262. */
  263. public $name = 'UserForm';
  264. /**
  265. * hasMany property
  266. *
  267. * @var array
  268. */
  269. public $hasMany = array(
  270. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  271. ));
  272. /**
  273. * schema definition
  274. *
  275. * @var array
  276. */
  277. protected $_schema = array(
  278. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  279. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  280. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  281. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  282. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  283. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  284. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  285. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  286. );
  287. }
  288. /**
  289. * OpenidUrl class
  290. *
  291. * @package cake
  292. * @package Cake.Test.Case.View.Helper
  293. */
  294. class OpenidUrl extends CakeTestModel {
  295. /**
  296. * useTable property
  297. *
  298. * @var bool false
  299. */
  300. public $useTable = false;
  301. /**
  302. * primaryKey property
  303. *
  304. * @var string 'id'
  305. */
  306. public $primaryKey = 'id';
  307. /**
  308. * name property
  309. *
  310. * @var string 'OpenidUrl'
  311. */
  312. public $name = 'OpenidUrl';
  313. /**
  314. * belongsTo property
  315. *
  316. * @var array
  317. */
  318. public $belongsTo = array('UserForm' => array(
  319. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  320. ));
  321. /**
  322. * validate property
  323. *
  324. * @var array
  325. */
  326. public $validate = array('openid_not_registered' => array());
  327. /**
  328. * schema method
  329. *
  330. * @var array
  331. */
  332. protected $_schema = array(
  333. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  334. 'user_form_id' => array(
  335. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  336. ),
  337. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  338. );
  339. /**
  340. * beforeValidate method
  341. *
  342. * @return void
  343. */
  344. public function beforeValidate($options = array()) {
  345. $this->invalidate('openid_not_registered');
  346. return true;
  347. }
  348. }
  349. /**
  350. * ValidateUser class
  351. *
  352. * @package cake
  353. * @package Cake.Test.Case.View.Helper
  354. */
  355. class ValidateUser extends CakeTestModel {
  356. /**
  357. * primaryKey property
  358. *
  359. * @var string 'id'
  360. */
  361. public $primaryKey = 'id';
  362. /**
  363. * useTable property
  364. *
  365. * @var bool false
  366. */
  367. public $useTable = false;
  368. /**
  369. * name property
  370. *
  371. * @var string 'ValidateUser'
  372. */
  373. public $name = 'ValidateUser';
  374. /**
  375. * hasOne property
  376. *
  377. * @var array
  378. */
  379. public $hasOne = array('ValidateProfile' => array(
  380. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  381. ));
  382. /**
  383. * schema method
  384. *
  385. * @var array
  386. */
  387. protected $_schema = array(
  388. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  389. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  390. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  391. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  392. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  393. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  394. );
  395. /**
  396. * beforeValidate method
  397. *
  398. * @return void
  399. */
  400. public function beforeValidate($options = array()) {
  401. $this->invalidate('email');
  402. return false;
  403. }
  404. }
  405. /**
  406. * ValidateProfile class
  407. *
  408. * @package cake
  409. * @package Cake.Test.Case.View.Helper
  410. */
  411. class ValidateProfile extends CakeTestModel {
  412. /**
  413. * primaryKey property
  414. *
  415. * @var string 'id'
  416. */
  417. public $primaryKey = 'id';
  418. /**
  419. * useTable property
  420. *
  421. * @var bool false
  422. */
  423. public $useTable = false;
  424. /**
  425. * schema property
  426. *
  427. * @var array
  428. */
  429. protected $_schema = array(
  430. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  431. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  432. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  433. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  434. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  435. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  436. );
  437. /**
  438. * name property
  439. *
  440. * @var string 'ValidateProfile'
  441. */
  442. public $name = 'ValidateProfile';
  443. /**
  444. * hasOne property
  445. *
  446. * @var array
  447. */
  448. public $hasOne = array('ValidateItem' => array(
  449. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  450. ));
  451. /**
  452. * belongsTo property
  453. *
  454. * @var array
  455. */
  456. public $belongsTo = array('ValidateUser' => array(
  457. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  458. ));
  459. /**
  460. * beforeValidate method
  461. *
  462. * @return void
  463. */
  464. public function beforeValidate($options = array()) {
  465. $this->invalidate('full_name');
  466. $this->invalidate('city');
  467. return false;
  468. }
  469. }
  470. /**
  471. * ValidateItem class
  472. *
  473. * @package cake
  474. * @package Cake.Test.Case.View.Helper
  475. */
  476. class ValidateItem extends CakeTestModel {
  477. /**
  478. * primaryKey property
  479. *
  480. * @var string 'id'
  481. */
  482. public $primaryKey = 'id';
  483. /**
  484. * useTable property
  485. *
  486. * @var bool false
  487. */
  488. public $useTable = false;
  489. /**
  490. * name property
  491. *
  492. * @var string 'ValidateItem'
  493. */
  494. public $name = 'ValidateItem';
  495. /**
  496. * schema property
  497. *
  498. * @var array
  499. */
  500. protected $_schema = array(
  501. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  502. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  503. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  504. 'description' => array(
  505. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  506. ),
  507. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  508. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  509. );
  510. /**
  511. * belongsTo property
  512. *
  513. * @var array
  514. */
  515. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  516. /**
  517. * beforeValidate method
  518. *
  519. * @return void
  520. */
  521. public function beforeValidate($options = array()) {
  522. $this->invalidate('description');
  523. return false;
  524. }
  525. }
  526. /**
  527. * TestMail class
  528. *
  529. * @package cake
  530. * @package Cake.Test.Case.View.Helper
  531. */
  532. class TestMail extends CakeTestModel {
  533. /**
  534. * primaryKey property
  535. *
  536. * @var string 'id'
  537. */
  538. public $primaryKey = 'id';
  539. /**
  540. * useTable property
  541. *
  542. * @var bool false
  543. */
  544. public $useTable = false;
  545. /**
  546. * name property
  547. *
  548. * @var string 'TestMail'
  549. */
  550. public $name = 'TestMail';
  551. }
  552. /**
  553. * FormHelperTest class
  554. *
  555. * @package cake
  556. * @subpackage Cake.Test.Case.View.Helper
  557. * @property FormHelper $Form
  558. */
  559. class FormHelperTest extends CakeTestCase {
  560. /**
  561. * Fixtures to be used
  562. *
  563. * @var array
  564. */
  565. public $fixtures = array('core.post');
  566. /**
  567. * Do not load the fixtures by default
  568. *
  569. * @var boolean
  570. */
  571. public $autoFixtures = false;
  572. /**
  573. * setUp method
  574. *
  575. * @return void
  576. */
  577. public function setUp() {
  578. parent::setUp();
  579. Configure::write('App.base', '');
  580. $this->Controller = new ContactTestController();
  581. $this->View = new View($this->Controller);
  582. $this->Form = new FormHelper($this->View);
  583. $this->Form->Html = new HtmlHelper($this->View);
  584. $this->Form->request = new CakeRequest('contacts/add', false);
  585. $this->Form->request->here = '/contacts/add';
  586. $this->Form->request['action'] = 'add';
  587. $this->Form->request->webroot = '';
  588. $this->Form->request->base = '';
  589. ClassRegistry::addObject('Contact', new Contact());
  590. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  591. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  592. ClassRegistry::addObject('User', new UserForm());
  593. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  594. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  595. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  596. $this->oldSalt = Configure::read('Security.salt');
  597. $this->dateRegex = array(
  598. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  599. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  600. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  601. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  602. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  603. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  604. );
  605. Configure::write('Security.salt', 'foo!');
  606. }
  607. /**
  608. * tearDown method
  609. *
  610. * @return void
  611. */
  612. public function tearDown() {
  613. parent::tearDown();
  614. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  615. Configure::write('Security.salt', $this->oldSalt);
  616. }
  617. /**
  618. * testFormCreateWithSecurity method
  619. *
  620. * Test form->create() with security key.
  621. *
  622. * @return void
  623. */
  624. public function testCreateWithSecurity() {
  625. $this->Form->request['_Token'] = array('key' => 'testKey');
  626. $encoding = strtolower(Configure::read('App.encoding'));
  627. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  628. $expected = array(
  629. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  630. 'div' => array('style' => 'display:none;'),
  631. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  632. array('input' => array(
  633. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  634. )),
  635. '/div'
  636. );
  637. $this->assertTags($result, $expected);
  638. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  639. $expected['form']['id'] = 'MyForm';
  640. $this->assertTags($result, $expected);
  641. }
  642. /**
  643. * test that create() clears the fields property so it starts fresh
  644. *
  645. * @return void
  646. */
  647. public function testCreateClearingFields() {
  648. $this->Form->fields = array('model_id');
  649. $this->Form->create('Contact');
  650. $this->assertEquals(array(), $this->Form->fields);
  651. }
  652. /**
  653. * Tests form hash generation with model-less data
  654. *
  655. * @return void
  656. */
  657. public function testValidateHashNoModel() {
  658. $this->Form->request['_Token'] = array('key' => 'foo');
  659. $result = $this->Form->secure(array('anything'));
  660. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  661. }
  662. /**
  663. * Tests that models with identical field names get resolved properly
  664. *
  665. * @return void
  666. */
  667. public function testDuplicateFieldNameResolution() {
  668. $result = $this->Form->create('ValidateUser');
  669. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  670. $result = $this->Form->input('ValidateItem.name');
  671. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  672. $result = $this->Form->input('ValidateUser.name');
  673. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  674. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  675. $this->assertRegExp('/type="text"/', $result);
  676. $result = $this->Form->input('ValidateItem.name');
  677. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  678. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  679. $this->assertRegExp('/<textarea/', $result);
  680. $result = $this->Form->input('name');
  681. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  682. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  683. $this->assertRegExp('/type="text"/', $result);
  684. }
  685. /**
  686. * Tests that hidden fields generated for checkboxes don't get locked
  687. *
  688. * @return void
  689. */
  690. public function testNoCheckboxLocking() {
  691. $this->Form->request['_Token'] = array('key' => 'foo');
  692. $this->assertSame(array(), $this->Form->fields);
  693. $this->Form->checkbox('check', array('value' => '1'));
  694. $this->assertSame($this->Form->fields, array('check'));
  695. }
  696. /**
  697. * testFormSecurityFields method
  698. *
  699. * Test generation of secure form hash generation.
  700. *
  701. * @return void
  702. */
  703. public function testFormSecurityFields() {
  704. $key = 'testKey';
  705. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  706. $this->Form->request['_Token'] = array('key' => $key);
  707. $result = $this->Form->secure($fields);
  708. $expected = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  709. $expected .= ':' . 'Model.valid';
  710. $expected = array(
  711. 'div' => array('style' => 'display:none;'),
  712. array('input' => array(
  713. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  714. 'value' => urlencode($expected), 'id' => 'preg:/TokenFields\d+/'
  715. )),
  716. array('input' => array(
  717. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  718. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  719. )),
  720. '/div'
  721. );
  722. $this->assertTags($result, $expected);
  723. }
  724. /**
  725. * Tests correct generation of number fields for double and float fields
  726. *
  727. * @return void
  728. */
  729. public function testTextFieldGenerationForFloats() {
  730. $model = ClassRegistry::getObject('Contact');
  731. $model->setSchema(array('foo' => array(
  732. 'type' => 'float',
  733. 'null' => false,
  734. 'default' => null,
  735. 'length' => null
  736. )));
  737. $this->Form->create('Contact');
  738. $result = $this->Form->input('foo');
  739. $expected = array(
  740. 'div' => array('class' => 'input number'),
  741. 'label' => array('for' => 'ContactFoo'),
  742. 'Foo',
  743. '/label',
  744. array('input' => array(
  745. 'type' => 'number',
  746. 'name' => 'data[Contact][foo]',
  747. 'id' => 'ContactFoo',
  748. 'step' => 'any'
  749. )),
  750. '/div'
  751. );
  752. $this->assertTags($result, $expected);
  753. $result = $this->Form->input('foo', array('step' => 0.5));
  754. $expected = array(
  755. 'div' => array('class' => 'input number'),
  756. 'label' => array('for' => 'ContactFoo'),
  757. 'Foo',
  758. '/label',
  759. array('input' => array(
  760. 'type' => 'number',
  761. 'name' => 'data[Contact][foo]',
  762. 'id' => 'ContactFoo',
  763. 'step' => '0.5'
  764. )),
  765. '/div'
  766. );
  767. $this->assertTags($result, $expected);
  768. }
  769. /**
  770. * Tests correct generation of number fields for integer fields
  771. *
  772. * @access public
  773. * @return void
  774. */
  775. public function testTextFieldTypeNumberGenerationForIntegers() {
  776. $model = ClassRegistry::getObject('Contact');
  777. $model->setSchema(array('foo' => array(
  778. 'type' => 'integer',
  779. 'null' => false,
  780. 'default' => null,
  781. 'length' => null
  782. )));
  783. $this->Form->create('Contact');
  784. $result = $this->Form->input('foo');
  785. $expected = array(
  786. 'div' => array('class' => 'input number'),
  787. 'label' => array('for' => 'ContactFoo'),
  788. 'Foo',
  789. '/label',
  790. array('input' => array(
  791. 'type' => 'number', 'name' => 'data[Contact][foo]',
  792. 'id' => 'ContactFoo'
  793. )),
  794. '/div'
  795. );
  796. $this->assertTags($result, $expected);
  797. }
  798. /**
  799. * testFormSecurityMultipleFields method
  800. *
  801. * Test secure() with multiple row form. Ensure hash is correct.
  802. *
  803. * @return void
  804. */
  805. public function testFormSecurityMultipleFields() {
  806. $key = 'testKey';
  807. $fields = array(
  808. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  809. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  810. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  811. );
  812. $this->Form->request['_Token'] = array('key' => $key);
  813. $result = $this->Form->secure($fields);
  814. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  815. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  816. $expected = array(
  817. 'div' => array('style' => 'display:none;'),
  818. array('input' => array(
  819. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  820. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  821. )),
  822. array('input' => array(
  823. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  824. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  825. )),
  826. '/div'
  827. );
  828. $this->assertTags($result, $expected);
  829. }
  830. /**
  831. * testFormSecurityMultipleSubmitButtons
  832. *
  833. * test form submit generation and ensure that _Token is only created on end()
  834. *
  835. * @return void
  836. */
  837. public function testFormSecurityMultipleSubmitButtons() {
  838. $key = 'testKey';
  839. $this->Form->request['_Token'] = array('key' => $key);
  840. $this->Form->create('Addresses');
  841. $this->Form->input('Address.title');
  842. $this->Form->input('Address.first_name');
  843. $result = $this->Form->submit('Save', array('name' => 'save'));
  844. $expected = array(
  845. 'div' => array('class' => 'submit'),
  846. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  847. '/div',
  848. );
  849. $this->assertTags($result, $expected);
  850. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  851. $expected = array(
  852. 'div' => array('class' => 'submit'),
  853. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  854. '/div',
  855. );
  856. $this->assertTags($result, $expected);
  857. $result = $this->Form->end(null);
  858. $expected = array(
  859. 'div' => array('style' => 'display:none;'),
  860. array('input' => array(
  861. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  862. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  863. )),
  864. array('input' => array(
  865. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  866. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  867. )),
  868. '/div'
  869. );
  870. $this->assertTags($result, $expected);
  871. }
  872. /**
  873. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  874. *
  875. * @return void
  876. */
  877. public function testSecurityButtonNestedNamed() {
  878. $key = 'testKey';
  879. $this->Form->request['_Token'] = array('key' => $key);
  880. $this->Form->create('Addresses');
  881. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  882. $result = $this->Form->unlockField();
  883. $this->assertEquals(array('Address.button'), $result);
  884. }
  885. /**
  886. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  887. *
  888. * @return void
  889. */
  890. public function testSecuritySubmitNestedNamed() {
  891. $key = 'testKey';
  892. $this->Form->request['_Token'] = array('key' => $key);
  893. $this->Form->create('Addresses');
  894. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  895. $result = $this->Form->unlockField();
  896. $this->assertEquals(array('Address.button'), $result);
  897. }
  898. /**
  899. * Test that the correct fields are unlocked for image submits with no names.
  900. *
  901. * @return void
  902. */
  903. public function testSecuritySubmitImageNoName() {
  904. $key = 'testKey';
  905. $this->Form->request['_Token'] = array('key' => $key);
  906. $this->Form->create('User');
  907. $result = $this->Form->submit('save.png');
  908. $expected = array(
  909. 'div' => array('class' => 'submit'),
  910. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  911. '/div'
  912. );
  913. $this->assertTags($result, $expected);
  914. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  915. }
  916. /**
  917. * Test that the correct fields are unlocked for image submits with names.
  918. *
  919. * @return void
  920. */
  921. public function testSecuritySubmitImageName() {
  922. $key = 'testKey';
  923. $this->Form->request['_Token'] = array('key' => $key);
  924. $this->Form->create('User');
  925. $result = $this->Form->submit('save.png', array('name' => 'test'));
  926. $expected = array(
  927. 'div' => array('class' => 'submit'),
  928. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  929. '/div'
  930. );
  931. $this->assertTags($result, $expected);
  932. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  933. }
  934. /**
  935. * testFormSecurityMultipleInputFields method
  936. *
  937. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  938. *
  939. * @return void
  940. */
  941. public function testFormSecurityMultipleInputFields() {
  942. $key = 'testKey';
  943. $this->Form->request['_Token'] = array('key' => $key);
  944. $this->Form->create('Addresses');
  945. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  946. $this->Form->input('Addresses.0.title');
  947. $this->Form->input('Addresses.0.first_name');
  948. $this->Form->input('Addresses.0.last_name');
  949. $this->Form->input('Addresses.0.address');
  950. $this->Form->input('Addresses.0.city');
  951. $this->Form->input('Addresses.0.phone');
  952. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  953. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  954. $this->Form->input('Addresses.1.title');
  955. $this->Form->input('Addresses.1.first_name');
  956. $this->Form->input('Addresses.1.last_name');
  957. $this->Form->input('Addresses.1.address');
  958. $this->Form->input('Addresses.1.city');
  959. $this->Form->input('Addresses.1.phone');
  960. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  961. $result = $this->Form->secure($this->Form->fields);
  962. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  963. $expected = array(
  964. 'div' => array('style' => 'display:none;'),
  965. array('input' => array(
  966. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  967. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  968. )),
  969. array('input' => array(
  970. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  971. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  972. )),
  973. '/div'
  974. );
  975. $this->assertTags($result, $expected);
  976. }
  977. /**
  978. * Test form security with Model.field.0 style inputs
  979. *
  980. * @return void
  981. */
  982. public function testFormSecurityArrayFields() {
  983. $key = 'testKey';
  984. $this->Form->request->params['_Token']['key'] = $key;
  985. $this->Form->create('Address');
  986. $this->Form->input('Address.primary.1');
  987. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  988. $this->Form->input('Address.secondary.1.0');
  989. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  990. }
  991. /**
  992. * testFormSecurityMultipleInputDisabledFields method
  993. *
  994. * test secure form generation with multiple records and disabled fields.
  995. *
  996. * @return void
  997. */
  998. public function testFormSecurityMultipleInputDisabledFields() {
  999. $key = 'testKey';
  1000. $this->Form->request->params['_Token'] = array(
  1001. 'key' => $key,
  1002. 'unlockedFields' => array('first_name', 'address')
  1003. );
  1004. $this->Form->create();
  1005. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1006. $this->Form->input('Addresses.0.title');
  1007. $this->Form->input('Addresses.0.first_name');
  1008. $this->Form->input('Addresses.0.last_name');
  1009. $this->Form->input('Addresses.0.address');
  1010. $this->Form->input('Addresses.0.city');
  1011. $this->Form->input('Addresses.0.phone');
  1012. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1013. $this->Form->input('Addresses.1.title');
  1014. $this->Form->input('Addresses.1.first_name');
  1015. $this->Form->input('Addresses.1.last_name');
  1016. $this->Form->input('Addresses.1.address');
  1017. $this->Form->input('Addresses.1.city');
  1018. $this->Form->input('Addresses.1.phone');
  1019. $result = $this->Form->secure($this->Form->fields);
  1020. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  1021. $expected = array(
  1022. 'div' => array('style' => 'display:none;'),
  1023. array('input' => array(
  1024. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1025. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1026. )),
  1027. array('input' => array(
  1028. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1029. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1030. )),
  1031. '/div'
  1032. );
  1033. $this->assertTags($result, $expected);
  1034. }
  1035. /**
  1036. * testFormSecurityInputDisabledFields method
  1037. *
  1038. * Test single record form with disabled fields.
  1039. *
  1040. * @return void
  1041. */
  1042. public function testFormSecurityInputUnlockedFields() {
  1043. $key = 'testKey';
  1044. $this->Form->request['_Token'] = array(
  1045. 'key' => $key,
  1046. 'unlockedFields' => array('first_name', 'address')
  1047. );
  1048. $this->Form->create();
  1049. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1050. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1051. $this->Form->input('Addresses.title');
  1052. $this->Form->input('Addresses.first_name');
  1053. $this->Form->input('Addresses.last_name');
  1054. $this->Form->input('Addresses.address');
  1055. $this->Form->input('Addresses.city');
  1056. $this->Form->input('Addresses.phone');
  1057. $result = $this->Form->fields;
  1058. $expected = array(
  1059. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1060. 'Addresses.city', 'Addresses.phone'
  1061. );
  1062. $this->assertEquals($expected, $result);
  1063. $result = $this->Form->secure($expected);
  1064. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1065. $expected = array(
  1066. 'div' => array('style' => 'display:none;'),
  1067. array('input' => array(
  1068. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1069. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1070. )),
  1071. array('input' => array(
  1072. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1073. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1074. )),
  1075. '/div'
  1076. );
  1077. $this->assertTags($result, $expected);
  1078. }
  1079. /**
  1080. * test securing inputs with custom name attributes.
  1081. *
  1082. * @return void
  1083. */
  1084. public function testFormSecureWithCustomNameAttribute() {
  1085. $this->Form->request->params['_Token']['key'] = 'testKey';
  1086. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1087. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1088. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1089. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1090. }
  1091. /**
  1092. * testFormSecuredInput method
  1093. *
  1094. * Test generation of entire secure form, assertions made on input() output.
  1095. *
  1096. * @return void
  1097. */
  1098. public function testFormSecuredInput() {
  1099. $this->Form->request['_Token'] = array('key' => 'testKey');
  1100. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1101. $encoding = strtolower(Configure::read('App.encoding'));
  1102. $expected = array(
  1103. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1104. 'div' => array('style' => 'display:none;'),
  1105. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1106. array('input' => array(
  1107. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1108. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1109. )),
  1110. '/div'
  1111. );
  1112. $this->assertTags($result, $expected);
  1113. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1114. $expected = array(
  1115. 'div' => array('class' => 'input text'),
  1116. 'label' => array('for' => 'UserFormPublished'),
  1117. 'Published',
  1118. '/label',
  1119. array('input' => array(
  1120. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1121. 'id' => 'UserFormPublished'
  1122. )),
  1123. '/div'
  1124. );
  1125. $this->assertTags($result, $expected);
  1126. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1127. $expected = array(
  1128. 'div' => array('class' => 'input text'),
  1129. 'label' => array('for' => 'UserFormOther'),
  1130. 'Other',
  1131. '/label',
  1132. array('input' => array(
  1133. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1134. 'id' => 'UserFormOther'
  1135. )),
  1136. '/div'
  1137. );
  1138. $this->assertTags($result, $expected);
  1139. $result = $this->Form->hidden('UserForm.stuff');
  1140. $expected = array('input' => array(
  1141. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1142. 'id' => 'UserFormStuff'
  1143. ));
  1144. $this->assertTags($result, $expected);
  1145. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1146. $expected = array('input' => array(
  1147. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1148. 'value' => '0', 'id' => 'UserFormHidden'
  1149. ));
  1150. $this->assertTags($result, $expected);
  1151. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1152. $expected = array(
  1153. 'div' => array('class' => 'input checkbox'),
  1154. array('input' => array(
  1155. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1156. 'value' => '0', 'id' => 'UserFormSomething_'
  1157. )),
  1158. array('input' => array(
  1159. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1160. 'value' => '1', 'id' => 'UserFormSomething'
  1161. )),
  1162. 'label' => array('for' => 'UserFormSomething'),
  1163. 'Something',
  1164. '/label',
  1165. '/div'
  1166. );
  1167. $this->assertTags($result, $expected);
  1168. $result = $this->Form->fields;
  1169. $expected = array(
  1170. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1171. 'UserForm.hidden' => '0', 'UserForm.something'
  1172. );
  1173. $this->assertEquals($expected, $result);
  1174. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1175. $result = $this->Form->secure($this->Form->fields);
  1176. $expected = array(
  1177. 'div' => array('style' => 'display:none;'),
  1178. array('input' => array(
  1179. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1180. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1181. )),
  1182. array('input' => array(
  1183. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1184. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1185. )),
  1186. '/div'
  1187. );
  1188. $this->assertTags($result, $expected);
  1189. }
  1190. /**
  1191. * Tests that the correct keys are added to the field hash index
  1192. *
  1193. * @return void
  1194. */
  1195. public function testFormSecuredFileInput() {
  1196. $this->Form->request['_Token'] = array('key' => 'testKey');
  1197. $this->assertEquals(array(), $this->Form->fields);
  1198. $result = $this->Form->file('Attachment.file');
  1199. $expected = array(
  1200. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1201. 'Attachment.file.error', 'Attachment.file.size'
  1202. );
  1203. $this->assertEquals($expected, $this->Form->fields);
  1204. }
  1205. /**
  1206. * test that multiple selects keys are added to field hash
  1207. *
  1208. * @return void
  1209. */
  1210. public function testFormSecuredMultipleSelect() {
  1211. $this->Form->request['_Token'] = array('key' => 'testKey');
  1212. $this->assertEquals(array(), $this->Form->fields);
  1213. $options = array('1' => 'one', '2' => 'two');
  1214. $this->Form->select('Model.select', $options);
  1215. $expected = array('Model.select');
  1216. $this->assertEquals($expected, $this->Form->fields);
  1217. $this->Form->fields = array();
  1218. $this->Form->select('Model.select', $options, array('multiple' => true));
  1219. $this->assertEquals($expected, $this->Form->fields);
  1220. }
  1221. /**
  1222. * testFormSecuredRadio method
  1223. *
  1224. * @return void
  1225. */
  1226. public function testFormSecuredRadio() {
  1227. $this->Form->request['_Token'] = array('key' => 'testKey');
  1228. $this->assertEquals(array(), $this->Form->fields);
  1229. $options = array('1' => 'option1', '2' => 'option2');
  1230. $this->Form->radio('Test.test', $options);
  1231. $expected = array('Test.test');
  1232. $this->assertEquals($expected, $this->Form->fields);
  1233. }
  1234. /**
  1235. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1236. * hashing.
  1237. *
  1238. * @return void
  1239. */
  1240. public function testFormSecuredAndDisabled() {
  1241. $this->Form->request['_Token'] = array('key' => 'testKey');
  1242. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1243. $this->Form->text('Model.text', array('disabled' => true));
  1244. $this->Form->password('Model.text', array('disabled' => true));
  1245. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1246. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1247. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1248. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1249. $this->Form->month('Model.month', array('disabled' => true));
  1250. $this->Form->day('Model.day', array('disabled' => true));
  1251. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1252. $this->Form->minute('Model.minute', array('disabled' => true));
  1253. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1254. $expected = array(
  1255. 'Model.radio' => ''
  1256. );
  1257. $this->assertEquals($expected, $this->Form->fields);
  1258. }
  1259. /**
  1260. * testDisableSecurityUsingForm method
  1261. *
  1262. * @return void
  1263. */
  1264. public function testDisableSecurityUsingForm() {
  1265. $this->Form->request['_Token'] = array(
  1266. 'key' => 'testKey',
  1267. 'disabledFields' => array()
  1268. );
  1269. $this->Form->create();
  1270. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1271. $this->Form->input('Addresses.title');
  1272. $this->Form->input('Addresses.first_name', array('secure' => false));
  1273. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1274. $this->Form->input('Addresses.zip', array(
  1275. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1276. ));
  1277. $result = $this->Form->fields;
  1278. $expected = array(
  1279. 'Addresses.id' => '123456', 'Addresses.title',
  1280. );
  1281. $this->assertEquals($expected, $result);
  1282. }
  1283. /**
  1284. * test disableField
  1285. *
  1286. * @return void
  1287. */
  1288. public function testUnlockFieldAddsToList() {
  1289. $this->Form->request['_Token'] = array(
  1290. 'key' => 'testKey',
  1291. 'unlockedFields' => array()
  1292. );
  1293. $this->Form->create('Contact');
  1294. $this->Form->unlockField('Contact.name');
  1295. $this->Form->text('Contact.name');
  1296. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1297. $this->assertEquals(array(), $this->Form->fields);
  1298. }
  1299. /**
  1300. * test unlockField removing from fields array.
  1301. *
  1302. * @return void
  1303. */
  1304. public function testUnlockFieldRemovingFromFields() {
  1305. $this->Form->request['_Token'] = array(
  1306. 'key' => 'testKey',
  1307. 'unlockedFields' => array()
  1308. );
  1309. $this->Form->create('Contact');
  1310. $this->Form->hidden('Contact.id', array('value' => 1));
  1311. $this->Form->text('Contact.name');
  1312. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1313. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1314. $this->Form->unlockField('Contact.name');
  1315. $this->Form->unlockField('Contact.id');
  1316. $this->assertEquals(array(), $this->Form->fields);
  1317. }
  1318. /**
  1319. * testTagIsInvalid method
  1320. *
  1321. * @return void
  1322. */
  1323. public function testTagIsInvalid() {
  1324. $Contact = ClassRegistry::getObject('Contact');
  1325. $Contact->validationErrors[0]['email'] = array('Please provide an email');
  1326. $this->Form->setEntity('Contact.0.email');
  1327. $result = $this->Form->tagIsInvalid();
  1328. $expected = array('Please provide an email');
  1329. $this->assertEquals($expected, $result);
  1330. $this->Form->setEntity('Contact.1.email');
  1331. $result = $this->Form->tagIsInvalid();
  1332. $expected = false;
  1333. $this->assertSame($expected, $result);
  1334. $this->Form->setEntity('Contact.0.name');
  1335. $result = $this->Form->tagIsInvalid();
  1336. $expected = false;
  1337. $this->assertSame($expected, $result);
  1338. }
  1339. /**
  1340. * testPasswordValidation method
  1341. *
  1342. * test validation errors on password input.
  1343. *
  1344. * @return void
  1345. */
  1346. public function testPasswordValidation() {
  1347. $Contact = ClassRegistry::getObject('Contact');
  1348. $Contact->validationErrors['password'] = array('Please provide a password');
  1349. $result = $this->Form->input('Contact.password');
  1350. $expected = array(
  1351. 'div' => array('class' => 'input password error'),
  1352. 'label' => array('for' => 'ContactPassword'),
  1353. 'Password',
  1354. '/label',
  1355. 'input' => array(
  1356. 'type' => 'password', 'name' => 'data[Contact][password]',
  1357. 'id' => 'ContactPassword', 'class' => 'form-error'
  1358. ),
  1359. array('div' => array('class' => 'error-message')),
  1360. 'Please provide a password',
  1361. '/div',
  1362. '/div'
  1363. );
  1364. $this->assertTags($result, $expected);
  1365. }
  1366. /**
  1367. * testEmptyErrorValidation method
  1368. *
  1369. * test validation error div when validation message is an empty string
  1370. *
  1371. * @access public
  1372. * @return void
  1373. */
  1374. public function testEmptyErrorValidation() {
  1375. $this->Form->validationErrors['Contact']['password'] = '';
  1376. $result = $this->Form->input('Contact.password');
  1377. $expected = array(
  1378. 'div' => array('class' => 'input password error'),
  1379. 'label' => array('for' => 'ContactPassword'),
  1380. 'Password',
  1381. '/label',
  1382. 'input' => array(
  1383. 'type' => 'password', 'name' => 'data[Contact][password]',
  1384. 'id' => 'ContactPassword', 'class' => 'form-error'
  1385. ),
  1386. array('div' => array('class' => 'error-message')),
  1387. array(),
  1388. '/div',
  1389. '/div'
  1390. );
  1391. $this->assertTags($result, $expected);
  1392. }
  1393. /**
  1394. * testEmptyInputErrorValidation method
  1395. *
  1396. * test validation error div when validation message is overridden by an empty string when calling input()
  1397. *
  1398. * @access public
  1399. * @return void
  1400. */
  1401. public function testEmptyInputErrorValidation() {
  1402. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1403. $result = $this->Form->input('Contact.password', array('error' => ''));
  1404. $expected = array(
  1405. 'div' => array('class' => 'input password error'),
  1406. 'label' => array('for' => 'ContactPassword'),
  1407. 'Password',
  1408. '/label',
  1409. 'input' => array(
  1410. 'type' => 'password', 'name' => 'data[Contact][password]',
  1411. 'id' => 'ContactPassword', 'class' => 'form-error'
  1412. ),
  1413. array('div' => array('class' => 'error-message')),
  1414. array(),
  1415. '/div',
  1416. '/div'
  1417. );
  1418. $this->assertTags($result, $expected);
  1419. }
  1420. /**
  1421. * testFormValidationAssociated method
  1422. *
  1423. * test display of form errors in conjunction with model::validates.
  1424. *
  1425. * @return void
  1426. */
  1427. public function testFormValidationAssociated() {
  1428. $this->UserForm = ClassRegistry::getObject('UserForm');
  1429. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1430. $data = array(
  1431. 'UserForm' => array('name' => 'user'),
  1432. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1433. );
  1434. $result = $this->UserForm->OpenidUrl->create($data);
  1435. $this->assertFalse(empty($result));
  1436. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1437. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1438. $encoding = strtolower(Configure::read('App.encoding'));
  1439. $expected = array(
  1440. 'form' => array(
  1441. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1442. 'accept-charset' => $encoding
  1443. ),
  1444. 'div' => array('style' => 'display:none;'),
  1445. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1446. '/div'
  1447. );
  1448. $this->assertTags($result, $expected);
  1449. $result = $this->Form->error(
  1450. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1451. );
  1452. $this->assertEquals('Error, not registered', $result);
  1453. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1454. }
  1455. /**
  1456. * testFormValidationAssociatedFirstLevel method
  1457. *
  1458. * test form error display with associated model.
  1459. *
  1460. * @return void
  1461. */
  1462. public function testFormValidationAssociatedFirstLevel() {
  1463. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1464. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1465. $data = array(
  1466. 'ValidateUser' => array('name' => 'mariano'),
  1467. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1468. );
  1469. $result = $this->ValidateUser->create($data);
  1470. $this->assertFalse(empty($result));
  1471. $this->assertFalse($this->ValidateUser->validates());
  1472. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1473. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1474. $encoding = strtolower(Configure::read('App.encoding'));
  1475. $expected = array(
  1476. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1477. 'div' => array('style' => 'display:none;'),
  1478. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1479. '/div'
  1480. );
  1481. $this->assertTags($result, $expected);
  1482. $result = $this->Form->error(
  1483. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1484. );
  1485. $this->assertEquals('Invalid email', $result);
  1486. $result = $this->Form->error(
  1487. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1488. );
  1489. $this->assertEquals('Invalid name', $result);
  1490. $result = $this->Form->error(
  1491. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1492. );
  1493. $this->assertEquals('Invalid city', $result);
  1494. unset($this->ValidateUser->ValidateProfile);
  1495. unset($this->ValidateUser);
  1496. }
  1497. /**
  1498. * testFormValidationAssociatedSecondLevel method
  1499. *
  1500. * test form error display with associated model.
  1501. *
  1502. * @return void
  1503. */
  1504. public function testFormValidationAssociatedSecondLevel() {
  1505. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1506. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1507. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1508. $data = array(
  1509. 'ValidateUser' => array('name' => 'mariano'),
  1510. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1511. 'ValidateItem' => array('name' => 'Item')
  1512. );
  1513. $result = $this->ValidateUser->create($data);
  1514. $this->assertFalse(empty($result));
  1515. $this->assertFalse($this->ValidateUser->validates());
  1516. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1517. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1518. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1519. $encoding = strtolower(Configure::read('App.encoding'));
  1520. $expected = array(
  1521. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1522. 'div' => array('style' => 'display:none;'),
  1523. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1524. '/div'
  1525. );
  1526. $this->assertTags($result, $expected);
  1527. $result = $this->Form->error(
  1528. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1529. );
  1530. $this->assertEquals('Invalid email', $result);
  1531. $result = $this->Form->error(
  1532. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1533. );
  1534. $this->assertEquals('Invalid name', $result);
  1535. $result = $this->Form->error(
  1536. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1537. );
  1538. $result = $this->Form->error(
  1539. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1540. );
  1541. $this->assertEquals('Invalid description', $result);
  1542. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1543. unset($this->ValidateUser->ValidateProfile);
  1544. unset($this->ValidateUser);
  1545. }
  1546. /**
  1547. * testFormValidationMultiRecord method
  1548. *
  1549. * test form error display with multiple records.
  1550. *
  1551. * @return void
  1552. */
  1553. public function testFormValidationMultiRecord() {
  1554. $Contact = ClassRegistry::getObject('Contact');
  1555. $Contact->validationErrors[2] = array(
  1556. 'name' => array('This field cannot be left blank')
  1557. );
  1558. $result = $this->Form->input('Contact.2.name');
  1559. $expected = array(
  1560. 'div' => array('class' => 'input text error'),
  1561. 'label' => array('for' => 'Contact2Name'),
  1562. 'Name',
  1563. '/label',
  1564. 'input' => array(
  1565. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1566. 'class' => 'form-error', 'maxlength' => 255
  1567. ),
  1568. array('div' => array('class' => 'error-message')),
  1569. 'This field cannot be left blank',
  1570. '/div',
  1571. '/div'
  1572. );
  1573. $this->assertTags($result, $expected);
  1574. }
  1575. /**
  1576. * testMultipleInputValidation method
  1577. *
  1578. * test multiple record form validation error display.
  1579. *
  1580. * @return void
  1581. */
  1582. public function testMultipleInputValidation() {
  1583. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1584. $Address->validationErrors[0] = array(
  1585. 'title' => array('This field cannot be empty'),
  1586. 'first_name' => array('This field cannot be empty')
  1587. );
  1588. $Address->validationErrors[1] = array(
  1589. 'last_name' => array('You must have a last name')
  1590. );
  1591. $this->Form->create();
  1592. $result = $this->Form->input('Address.0.title');
  1593. $expected = array(
  1594. 'div' => array('class'),
  1595. 'label' => array('for'),
  1596. 'preg:/[^<]+/',
  1597. '/label',
  1598. 'input' => array(
  1599. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1600. ),
  1601. array('div' => array('class' => 'error-message')),
  1602. 'This field cannot be empty',
  1603. '/div',
  1604. '/div'
  1605. );
  1606. $this->assertTags($result, $expected);
  1607. $result = $this->Form->input('Address.0.first_name');
  1608. $expected = array(
  1609. 'div' => array('class'),
  1610. 'label' => array('for'),
  1611. 'preg:/[^<]+/',
  1612. '/label',
  1613. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1614. array('div' => array('class' => 'error-message')),
  1615. 'This field cannot be empty',
  1616. '/div',
  1617. '/div'
  1618. );
  1619. $this->assertTags($result, $expected);
  1620. $result = $this->Form->input('Address.0.last_name');
  1621. $expected = array(
  1622. 'div' => array('class'),
  1623. 'label' => array('for'),
  1624. 'preg:/[^<]+/',
  1625. '/label',
  1626. 'input' => array('type' => 'text', 'name', 'id'),
  1627. '/div'
  1628. );
  1629. $this->assertTags($result, $expected);
  1630. $result = $this->Form->input('Address.1.last_name');
  1631. $expected = array(
  1632. 'div' => array('class'),
  1633. 'label' => array('for'),
  1634. 'preg:/[^<]+/',
  1635. '/label',
  1636. 'input' => array(
  1637. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1638. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1639. ),
  1640. array('div' => array('class' => 'error-message')),
  1641. 'You must have a last name',
  1642. '/div',
  1643. '/div'
  1644. );
  1645. $this->assertTags($result, $expected);
  1646. }
  1647. /**
  1648. * testInput method
  1649. *
  1650. * Test various incarnations of input().
  1651. *
  1652. * @return void
  1653. */
  1654. public function testInput() {
  1655. $result = $this->Form->input('ValidateUser.balance');
  1656. $expected = array(
  1657. 'div' => array('class'),
  1658. 'label' => array('for'),
  1659. 'Balance',
  1660. '/label',
  1661. 'input' => array('name', 'type' => 'number', 'maxlength' => 8, 'id'),
  1662. '/div',
  1663. );
  1664. $this->assertTags($result, $expected);
  1665. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1666. $expected = array(
  1667. 'div' => array('class' => 'input text'),
  1668. 'label' => array('for' => 'custom'),
  1669. 'Email',
  1670. '/label',
  1671. array('input' => array(
  1672. 'type' => 'text', 'name' => 'data[Contact][email]',
  1673. 'id' => 'custom', 'maxlength' => 255
  1674. )),
  1675. '/div'
  1676. );
  1677. $this->assertTags($result, $expected);
  1678. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1679. $expected = array(
  1680. '<div',
  1681. 'label' => array('for' => 'ContactEmail'),
  1682. 'Email',
  1683. '/label',
  1684. array('input' => array(
  1685. 'type' => 'text', 'name' => 'data[Contact][email]',
  1686. 'id' => 'ContactEmail', 'maxlength' => 255
  1687. )),
  1688. '/div'
  1689. );
  1690. $this->assertTags($result, $expected);
  1691. $result = $this->Form->hidden('Contact.idontexist');
  1692. $expected = array('input' => array(
  1693. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1694. 'id' => 'ContactIdontexist'
  1695. ));
  1696. $this->assertTags($result, $expected);
  1697. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1698. $expected = array(
  1699. 'div' => array('class' => 'input text'),
  1700. 'label' => array('for' => 'ContactEmail'),
  1701. 'Email',
  1702. '/label',
  1703. array('input' => array(
  1704. 'type' => 'text', 'name' => 'data[Contact][email]',
  1705. 'id' => 'ContactEmail'
  1706. )),
  1707. '/div'
  1708. );
  1709. $this->assertTags($result, $expected);
  1710. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1711. $expected = array(
  1712. 'div' => array('class' => 'input text'),
  1713. 'label' => array('for' => 'Contact5Email'),
  1714. 'Email',
  1715. '/label',
  1716. array('input' => array(
  1717. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1718. 'id' => 'Contact5Email'
  1719. )),
  1720. '/div'
  1721. );
  1722. $this->assertTags($result, $expected);
  1723. $result = $this->Form->input('Contact.password');
  1724. $expected = array(
  1725. 'div' => array('class' => 'input password'),
  1726. 'label' => array('for' => 'ContactPassword'),
  1727. 'Password',
  1728. '/label',
  1729. array('input' => array(
  1730. 'type' => 'password', 'name' => 'data[Contact][password]',
  1731. 'id' => 'ContactPassword'
  1732. )),
  1733. '/div'
  1734. );
  1735. $this->assertTags($result, $expected);
  1736. $result = $this->Form->input('Contact.email', array(
  1737. 'type' => 'file', 'class' => 'textbox'
  1738. ));
  1739. $expected = array(
  1740. 'div' => array('class' => 'input file'),
  1741. 'label' => array('for' => 'ContactEmail'),
  1742. 'Email',
  1743. '/label',
  1744. array('input' => array(
  1745. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1746. 'id' => 'ContactEmail'
  1747. )),
  1748. '/div'
  1749. );
  1750. $this->assertTags($result, $expected);
  1751. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1752. $result = $this->Form->input('Contact.phone');
  1753. $expected = array(
  1754. 'div' => array('class' => 'input text'),
  1755. 'label' => array('for' => 'ContactPhone'),
  1756. 'Phone',
  1757. '/label',
  1758. array('input' => array(
  1759. 'type' => 'text', 'name' => 'data[Contact][phone]',
  1760. 'value' => 'Hello &amp; World &gt; weird chars',
  1761. 'id' => 'ContactPhone', 'maxlength' => 255
  1762. )),
  1763. '/div'
  1764. );
  1765. $this->assertTags($result, $expected);
  1766. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1767. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1768. $expected = array(
  1769. 'div' => array('class' => 'input text'),
  1770. 'label' => array('for' => 'myId'),
  1771. 'Field',
  1772. '/label',
  1773. 'input' => array(
  1774. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1775. 'value' => 'My value', 'id' => 'myId'
  1776. ),
  1777. '/div'
  1778. );
  1779. $this->assertTags($result, $expected);
  1780. unset($this->Form->request->data);
  1781. $Contact = ClassRegistry::getObject('Contact');
  1782. $Contact->validationErrors['field'] = array('Badness!');
  1783. $result = $this->Form->input('Contact.field');
  1784. $expected = array(
  1785. 'div' => array('class' => 'input text error'),
  1786. 'label' => array('for' => 'ContactField'),
  1787. 'Field',
  1788. '/label',
  1789. 'input' => array(
  1790. 'type' => 'text', 'name' => 'data[Contact][field]',
  1791. 'id' => 'ContactField', 'class' => 'form-error'
  1792. ),
  1793. array('div' => array('class' => 'error-message')),
  1794. 'Badness!',
  1795. '/div',
  1796. '/div'
  1797. );
  1798. $this->assertTags($result, $expected);
  1799. $result = $this->Form->input('Contact.field', array(
  1800. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1801. ));
  1802. $expected = array(
  1803. 'label' => array('for' => 'ContactField'),
  1804. 'Field',
  1805. '/label',
  1806. 'input' => array(
  1807. 'type' => 'text', 'name' => 'data[Contact][field]',
  1808. 'id' => 'ContactField', 'class' => 'form-error'
  1809. ),
  1810. array('span' => array('class' => 'error-message')),
  1811. 'Badness!',
  1812. '/span'
  1813. );
  1814. $this->assertTags($result, $expected);
  1815. $result = $this->Form->input('Contact.field', array(
  1816. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1817. ));
  1818. $expected = array(
  1819. 'div' => array('class' => 'input text error'),
  1820. 'label' => array('for' => 'ContactField'),
  1821. 'Field',
  1822. '/label',
  1823. 'input' => array(
  1824. 'type' => 'text', 'name' => 'data[Contact][field]',
  1825. 'id' => 'ContactField', 'class' => 'form-error'
  1826. ),
  1827. array('div' => array('class' => 'error')),
  1828. 'Badness!',
  1829. '/div'
  1830. );
  1831. $this->assertTags($result, $expected);
  1832. $result = $this->Form->input('Contact.field', array(
  1833. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1834. ));
  1835. $expected = array(
  1836. 'span' => array('class' => 'input text error'),
  1837. 'label' => array('for' => 'ContactField'),
  1838. 'Field',
  1839. '/label',
  1840. 'input' => array(
  1841. 'type' => 'text', 'name' => 'data[Contact][field]',
  1842. 'id' => 'ContactField', 'class' => 'form-error'
  1843. ),
  1844. 'Badness!',
  1845. '/span'
  1846. );
  1847. $this->assertTags($result, $expected);
  1848. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1849. $expected = array(
  1850. 'div' => array('class' => 'input text error'),
  1851. 'label' => array('for' => 'ContactField'),
  1852. 'Field',
  1853. '/label',
  1854. 'input' => array(
  1855. 'type' => 'text', 'name' => 'data[Contact][field]',
  1856. 'id' => 'ContactField', 'class' => 'form-error'
  1857. ),
  1858. 'A message to you, Rudy',
  1859. array('div' => array('class' => 'error-message')),
  1860. 'Badness!',
  1861. '/div',
  1862. '/div'
  1863. );
  1864. $this->assertTags($result, $expected);
  1865. $this->Form->setEntity(null);
  1866. $this->Form->setEntity('Contact.field');
  1867. $result = $this->Form->input('Contact.field', array(
  1868. 'after' => 'A message to you, Rudy', 'error' => false
  1869. ));
  1870. $expected = array(
  1871. 'div' => array('class' => 'input text'),
  1872. 'label' => array('for' => 'ContactField'),
  1873. 'Field',
  1874. '/label',
  1875. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1876. 'A message to you, Rudy',
  1877. '/div'
  1878. );
  1879. $this->assertTags($result, $expected);
  1880. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1881. $expected = array(
  1882. 'div' => array('class' => 'input text'),
  1883. 'label' => array('for' => 'ObjectField'),
  1884. 'Field',
  1885. '/label',
  1886. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1887. 'A message to you, Rudy',
  1888. '/div'
  1889. );
  1890. $this->assertTags($result, $expected);
  1891. $Contact->validationErrors['field'] = array('minLength');
  1892. $result = $this->Form->input('Contact.field', array(
  1893. 'error' => array(
  1894. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1895. 'maxLength' => 'login too large'
  1896. )
  1897. ));
  1898. $expected = array(
  1899. 'div' => array('class' => 'input text error'),
  1900. 'label' => array('for' => 'ContactField'),
  1901. 'Field',
  1902. '/label',
  1903. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1904. array('div' => array('class' => 'error-message')),
  1905. 'Le login doit contenir au moins 2 caractères',
  1906. '/div',
  1907. '/div'
  1908. );
  1909. $this->assertTags($result, $expected);
  1910. $Contact->validationErrors['field'] = array('maxLength');
  1911. $result = $this->Form->input('Contact.field', array(
  1912. 'error' => array(
  1913. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1914. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1915. 'maxLength' => 'login too large',
  1916. )
  1917. ));
  1918. $expected = array(
  1919. 'div' => array('class' => 'input text error'),
  1920. 'label' => array('for' => 'ContactField'),
  1921. 'Field',
  1922. '/label',
  1923. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1924. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1925. 'login too large',
  1926. '/span',
  1927. '/div'
  1928. );
  1929. $this->assertTags($result, $expected);
  1930. }
  1931. /**
  1932. * Test that inputs with 0 can be created.
  1933. *
  1934. * @return void
  1935. */
  1936. public function testInputZero() {
  1937. $this->Form->create('User');
  1938. $result = $this->Form->input('0');
  1939. $expected = array(
  1940. 'div' => array('class' => 'input text'),
  1941. 'label' => array('for' => 'User0'), '/label',
  1942. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  1943. '/div'
  1944. );
  1945. $this->assertTags($result, $expected);
  1946. }
  1947. /**
  1948. * test input() with checkbox creation
  1949. *
  1950. * @return void
  1951. */
  1952. public function testInputCheckbox() {
  1953. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1954. $expected = array(
  1955. 'div' => array('class' => 'input checkbox'),
  1956. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1957. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1958. '/div'
  1959. );
  1960. $this->assertTags($result, $expected);
  1961. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1962. $expected = array(
  1963. 'div' => array('class' => 'input checkbox'),
  1964. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1965. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1966. '/div'
  1967. );
  1968. $this->assertTags($result, $expected);
  1969. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1970. $expected = array(
  1971. 'div' => array('class' => 'input checkbox'),
  1972. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1973. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1974. '/div'
  1975. );
  1976. $this->assertTags($result, $expected);
  1977. }
  1978. /**
  1979. * test form->input() with time types.
  1980. *
  1981. */
  1982. public function testInputTime() {
  1983. extract($this->dateRegex);
  1984. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1985. $result = explode(':', $result);
  1986. $this->assertRegExp('/option value="23"/', $result[0]);
  1987. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1988. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1989. $result = explode(':', $result);
  1990. $this->assertRegExp('/option value="23"/', $result[0]);
  1991. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1992. $result = $this->Form->input('Model.field', array(
  1993. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  1994. ));
  1995. $result = explode(':', $result);
  1996. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1997. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1998. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1999. $result = $this->Form->input('Model.field', array(
  2000. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2001. ));
  2002. $result = explode(':', $result);
  2003. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2004. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2005. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2006. $result = $this->Form->input('prueba', array(
  2007. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  2008. 'maxYear' => date('Y') + 1 ,'interval' => 15
  2009. ));
  2010. $result = explode(':', $result);
  2011. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2012. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2013. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2014. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2015. $result = $this->Form->input('Random.start_time', array(
  2016. 'type' => 'time',
  2017. 'selected' => '18:15'
  2018. ));
  2019. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2020. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2021. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2022. }
  2023. /**
  2024. * Test interval + selected near the hour roll over.
  2025. *
  2026. * @return void
  2027. */
  2028. public function testTimeSelectedWithInterval() {
  2029. $result = $this->Form->input('Model.start_time', array(
  2030. 'type' => 'time',
  2031. 'interval' => 15,
  2032. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2033. ));
  2034. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2035. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2036. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2037. $result = $this->Form->input('Model.start_time', array(
  2038. 'type' => 'time',
  2039. 'interval' => 15,
  2040. 'selected' => '2012-10-23 15:57:00'
  2041. ));
  2042. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2043. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2044. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2045. $result = $this->Form->input('Model.start_time', array(
  2046. 'timeFormat' => 24,
  2047. 'type' => 'time',
  2048. 'interval' => 15,
  2049. 'selected' => '15:57'
  2050. ));
  2051. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2052. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2053. $result = $this->Form->input('Model.start_time', array(
  2054. 'timeFormat' => 24,
  2055. 'type' => 'time',
  2056. 'interval' => 15,
  2057. 'selected' => '23:57'
  2058. ));
  2059. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2060. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2061. $result = $this->Form->input('Model.created', array(
  2062. 'timeFormat' => 24,
  2063. 'type' => 'datetime',
  2064. 'interval' => 15,
  2065. 'selected' => '2012-09-30 23:56'
  2066. ));
  2067. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2068. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2069. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2070. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2071. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2072. }
  2073. /**
  2074. * test form->input() with datetime, date and time types
  2075. *
  2076. * @return void
  2077. */
  2078. public function testInputDatetime() {
  2079. extract($this->dateRegex);
  2080. $result = $this->Form->input('prueba', array(
  2081. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2082. 'maxYear' => date('Y') + 1, 'interval' => 15
  2083. ));
  2084. $result = explode(':', $result);
  2085. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2086. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2087. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2088. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2089. //related to ticket #5013
  2090. $result = $this->Form->input('Contact.date', array(
  2091. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2092. ));
  2093. $this->assertRegExp('/class="customClass"/', $result);
  2094. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2095. $result = $this->Form->input('Contact.date', array(
  2096. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2097. ));
  2098. $this->assertRegExp('/id="customIdDay"/', $result);
  2099. $this->assertRegExp('/id="customIdMonth"/', $result);
  2100. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2101. $result = $this->Form->input('Model.field', array(
  2102. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2103. ));
  2104. $this->assertRegExp('/id="customIDDay"/', $result);
  2105. $this->assertRegExp('/id="customIDHour"/', $result);
  2106. $result = explode('</select><select', $result);
  2107. $result = explode(':', $result[1]);
  2108. $this->assertRegExp('/option value="23"/', $result[0]);
  2109. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2110. $result = $this->Form->input('Model.field', array(
  2111. 'type' => 'datetime', 'timeFormat' => 12
  2112. ));
  2113. $result = explode('</select><select', $result);
  2114. $result = explode(':', $result[1]);
  2115. $this->assertRegExp('/option value="12"/', $result[0]);
  2116. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2117. $this->Form->request->data = array('Contact' => array('created' => null));
  2118. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2119. $expected = array(
  2120. 'div' => array('class' => 'input date'),
  2121. 'label' => array('for' => 'ContactCreatedMonth'),
  2122. 'Created',
  2123. '/label',
  2124. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2125. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2126. $monthsRegex,
  2127. '/select', '-',
  2128. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2129. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2130. $daysRegex,
  2131. '/select', '-',
  2132. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2133. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2134. $yearsRegex,
  2135. '/select',
  2136. '/div'
  2137. );
  2138. $this->assertTags($result, $expected);
  2139. $this->Form->request->data = array('Contact' => array('created' => null));
  2140. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2141. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2142. $this->Form->request->data = array('Contact' => array('created' => null));
  2143. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2144. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2145. $result = $this->Form->input('Contact.created', array(
  2146. 'type' => 'date',
  2147. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2148. 'timeFormat' => 'NONE'
  2149. ));
  2150. $this->assertRegExp('/for\="created-month"/', $result);
  2151. }
  2152. /**
  2153. * Test generating checkboxes in a loop.
  2154. *
  2155. * @return void
  2156. */
  2157. public function testInputCheckboxesInLoop() {
  2158. for ($i = 1; $i < 5; $i++) {
  2159. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2160. $expected = array(
  2161. 'div' => array('class' => 'input checkbox'),
  2162. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2163. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2164. 'label' => array('for' => "Contact{$i}Email"),
  2165. 'Email',
  2166. '/label',
  2167. '/div'
  2168. );
  2169. $this->assertTags($result, $expected);
  2170. }
  2171. }
  2172. /**
  2173. * test input name with leading integer, ensure attributes are generated correctly.
  2174. *
  2175. * @return void
  2176. */
  2177. public function testInputWithLeadingInteger() {
  2178. $result = $this->Form->text('0.Node.title');
  2179. $expected = array(
  2180. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2181. );
  2182. $this->assertTags($result, $expected);
  2183. }
  2184. /**
  2185. * test form->input() with select type inputs.
  2186. *
  2187. * @return void
  2188. */
  2189. public function testInputSelectType() {
  2190. $result = $this->Form->input('email', array(
  2191. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2192. );
  2193. $expected = array(
  2194. 'div' => array('class' => 'input select'),
  2195. 'label' => array('for' => 'email'),
  2196. 'Email',
  2197. '/label',
  2198. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2199. array('option' => array('value' => '')),
  2200. '/option',
  2201. array('option' => array('value' => 'è')),
  2202. 'Firést',
  2203. '/option',
  2204. array('option' => array('value' => 'é')),
  2205. 'Secoènd',
  2206. '/option',
  2207. '/select',
  2208. '/div'
  2209. );
  2210. $this->assertTags($result, $expected);
  2211. $result = $this->Form->input('email', array(
  2212. 'options' => array('First', 'Second'), 'empty' => true)
  2213. );
  2214. $expected = array(
  2215. 'div' => array('class' => 'input select'),
  2216. 'label' => array('for' => 'email'),
  2217. 'Email',
  2218. '/label',
  2219. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2220. array('option' => array('value' => '')),
  2221. '/option',
  2222. array('option' => array('value' => '0')),
  2223. 'First',
  2224. '/option',
  2225. array('option' => array('value' => '1')),
  2226. 'Second',
  2227. '/option',
  2228. '/select',
  2229. '/div'
  2230. );
  2231. $this->assertTags($result, $expected);
  2232. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2233. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2234. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2235. $expected = array(
  2236. 'div' => array('class' => 'input select'),
  2237. 'label' => array('for' => 'ModelUserId'),
  2238. 'User',
  2239. '/label',
  2240. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2241. array('option' => array('value' => '')),
  2242. '/option',
  2243. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2244. 'good',
  2245. '/option',
  2246. array('option' => array('value' => 'other')),
  2247. 'bad',
  2248. '/option',
  2249. '/select',
  2250. '/div'
  2251. );
  2252. $this->assertTags($result, $expected);
  2253. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2254. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2255. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2256. $expected = array(
  2257. 'div' => array('class' => 'input select'),
  2258. 'label' => array('for' => 'ThingUserId'),
  2259. 'User',
  2260. '/label',
  2261. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2262. array('option' => array('value' => '')),
  2263. 'Some Empty',
  2264. '/option',
  2265. array('option' => array('value' => 'value')),
  2266. 'good',
  2267. '/option',
  2268. array('option' => array('value' => 'other')),
  2269. 'bad',
  2270. '/option',
  2271. '/select',
  2272. '/div'
  2273. );
  2274. $this->assertTags($result, $expected);
  2275. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2276. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2277. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2278. $expected = array(
  2279. 'div' => array('class' => 'input select'),
  2280. 'label' => array('for' => 'ThingUserId'),
  2281. 'User',
  2282. '/label',
  2283. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2284. array('option' => array('value' => '')),
  2285. 'Some Empty',
  2286. '/option',
  2287. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2288. 'good',
  2289. '/option',
  2290. array('option' => array('value' => 'other')),
  2291. 'bad',
  2292. '/option',
  2293. '/select',
  2294. '/div'
  2295. );
  2296. $this->assertTags($result, $expected);
  2297. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2298. $this->Form->request->data = array('User' => array('User' => array('value')));
  2299. $result = $this->Form->input('User.User', array('empty' => true));
  2300. $expected = array(
  2301. 'div' => array('class' => 'input select'),
  2302. 'label' => array('for' => 'UserUser'),
  2303. 'User',
  2304. '/label',
  2305. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2306. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2307. array('option' => array('value' => '')),
  2308. '/option',
  2309. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2310. 'good',
  2311. '/option',
  2312. array('option' => array('value' => 'other')),
  2313. 'bad',
  2314. '/option',
  2315. '/select',
  2316. '/div'
  2317. );
  2318. $this->assertTags($result, $expected);
  2319. $this->Form->data = array();
  2320. $result = $this->Form->input('Publisher.id', array(
  2321. 'label' => 'Publisher',
  2322. 'type' => 'select',
  2323. 'multiple' => 'checkbox',
  2324. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2325. ));
  2326. $expected = array(
  2327. array('div' => array('class' => 'input select')),
  2328. array('label' => array('for' => 'PublisherId')),
  2329. 'Publisher',
  2330. '/label',
  2331. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2332. array('div' => array('class' => 'checkbox')),
  2333. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2334. array('label' => array('for' => 'PublisherIdValue1')),
  2335. 'Label 1',
  2336. '/label',
  2337. '/div',
  2338. array('div' => array('class' => 'checkbox')),
  2339. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2340. array('label' => array('for' => 'PublisherIdValue2')),
  2341. 'Label 2',
  2342. '/label',
  2343. '/div',
  2344. '/div'
  2345. );
  2346. $this->assertTags($result, $expected);
  2347. }
  2348. /**
  2349. * test that input() and a non standard primary key makes a hidden input by default.
  2350. *
  2351. * @return void
  2352. */
  2353. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2354. $this->Form->create('User');
  2355. $this->Form->fieldset = array(
  2356. 'User' => array(
  2357. 'fields' => array(
  2358. 'model_id' => array('type' => 'integer')
  2359. ),
  2360. 'validates' => array(),
  2361. 'key' => 'model_id'
  2362. )
  2363. );
  2364. $result = $this->Form->input('model_id');
  2365. $expected = array(
  2366. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2367. );
  2368. $this->assertTags($result, $expected);
  2369. }
  2370. /**
  2371. * test that overriding the magic select type widget is possible
  2372. *
  2373. * @return void
  2374. */
  2375. public function testInputOverridingMagicSelectType() {
  2376. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2377. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2378. $expected = array(
  2379. 'div' => array('class' => 'input text'),
  2380. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2381. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2382. '/div'
  2383. );
  2384. $this->assertTags($result, $expected);
  2385. //Check that magic types still work for plural/singular vars
  2386. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2387. $result = $this->Form->input('Model.type');
  2388. $expected = array(
  2389. 'div' => array('class' => 'input select'),
  2390. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2391. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2392. array('option' => array('value' => 'value')), 'good', '/option',
  2393. array('option' => array('value' => 'other')), 'bad', '/option',
  2394. '/select',
  2395. '/div'
  2396. );
  2397. $this->assertTags($result, $expected);
  2398. }
  2399. /**
  2400. * Test that magic input() selects are created for type=number
  2401. *
  2402. * @return void
  2403. */
  2404. public function testInputMagicSelectForTypeNumber() {
  2405. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2406. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2407. $result = $this->Form->input('ValidateUser.balance');
  2408. $expected = array(
  2409. 'div' => array('class' => 'input select'),
  2410. 'label' => array('for' => 'ValidateUserBalance'),
  2411. 'Balance',
  2412. '/label',
  2413. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2414. array('option' => array('value' => '0')),
  2415. 'nothing',
  2416. '/option',
  2417. array('option' => array('value' => '1', 'selected' => 'selected')),
  2418. 'some',
  2419. '/option',
  2420. array('option' => array('value' => '100')),
  2421. 'a lot',
  2422. '/option',
  2423. '/select',
  2424. '/div'
  2425. );
  2426. $this->assertTags($result, $expected);
  2427. }
  2428. /**
  2429. * Test that magic input() selects can easily be converted into radio types without error.
  2430. *
  2431. * @return void
  2432. */
  2433. public function testInputMagicSelectChangeToRadio() {
  2434. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2435. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2436. $this->assertRegExp('/input type="radio"/', $result);
  2437. }
  2438. /**
  2439. * fields with the same name as the model should work.
  2440. *
  2441. * @return void
  2442. */
  2443. public function testInputWithMatchingFieldAndModelName() {
  2444. $this->Form->create('User');
  2445. $this->Form->fieldset = array(
  2446. 'User' => array(
  2447. 'fields' => array(
  2448. 'User' => array('type' => 'text')
  2449. ),
  2450. 'validates' => array(),
  2451. 'key' => 'id'
  2452. )
  2453. );
  2454. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2455. $result = $this->Form->input('User', array('type' => 'text'));
  2456. $expected = array(
  2457. 'div' => array('class' => 'input text'),
  2458. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2459. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2460. '/div'
  2461. );
  2462. $this->assertTags($result, $expected);
  2463. }
  2464. /**
  2465. * testFormInputs method
  2466. *
  2467. * test correct results from form::inputs().
  2468. *
  2469. * @return void
  2470. */
  2471. public function testFormInputs() {
  2472. $this->Form->create('Contact');
  2473. $result = $this->Form->inputs('The Legend');
  2474. $expected = array(
  2475. '<fieldset',
  2476. '<legend',
  2477. 'The Legend',
  2478. '/legend',
  2479. '*/fieldset',
  2480. );
  2481. $this->assertTags($result, $expected);
  2482. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2483. $expected = array(
  2484. 'fieldset' => array('class' => 'classy-stuff'),
  2485. '<legend',
  2486. 'Field of Dreams',
  2487. '/legend',
  2488. '*/fieldset'
  2489. );
  2490. $this->assertTags($result, $expected);
  2491. $this->Form->create('Contact');
  2492. $this->Form->request['prefix'] = 'admin';
  2493. $this->Form->request['action'] = 'admin_edit';
  2494. $result = $this->Form->inputs();
  2495. $expected = array(
  2496. '<fieldset',
  2497. '<legend',
  2498. 'Edit Contact',
  2499. '/legend',
  2500. '*/fieldset',
  2501. );
  2502. $this->assertTags($result, $expected);
  2503. $this->Form->create('Contact');
  2504. $result = $this->Form->inputs(false);
  2505. $expected = array(
  2506. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2507. array('div' => array('class' => 'input text')),
  2508. '*/div',
  2509. array('div' => array('class' => 'input text')),
  2510. '*/div',
  2511. array('div' => array('class' => 'input text')),
  2512. '*/div',
  2513. array('div' => array('class' => 'input password')),
  2514. '*/div',
  2515. array('div' => array('class' => 'input date')),
  2516. '*/div',
  2517. array('div' => array('class' => 'input date')),
  2518. '*/div',
  2519. array('div' => array('class' => 'input datetime')),
  2520. '*/div',
  2521. array('div' => array('class' => 'input number')),
  2522. '*/div',
  2523. array('div' => array('class' => 'input select')),
  2524. '*/div',
  2525. );
  2526. $this->assertTags($result, $expected);
  2527. $this->Form->create('Contact');
  2528. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2529. $expected = array(
  2530. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2531. array('div' => array('class' => 'input text')),
  2532. '*/div',
  2533. array('div' => array('class' => 'input text')),
  2534. '*/div',
  2535. array('div' => array('class' => 'input text')),
  2536. '*/div',
  2537. array('div' => array('class' => 'input password')),
  2538. '*/div',
  2539. array('div' => array('class' => 'input date')),
  2540. '*/div',
  2541. array('div' => array('class' => 'input date')),
  2542. '*/div',
  2543. array('div' => array('class' => 'input datetime')),
  2544. '*/div',
  2545. array('div' => array('class' => 'input number')),
  2546. '*/div',
  2547. array('div' => array('class' => 'input select')),
  2548. '*/div',
  2549. );
  2550. $this->assertTags($result, $expected);
  2551. $this->Form->create('Contact');
  2552. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2553. $expected = array(
  2554. 'fieldset' => array(),
  2555. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2556. array('div' => array('class' => 'input text')),
  2557. '*/div',
  2558. array('div' => array('class' => 'input text')),
  2559. '*/div',
  2560. array('div' => array('class' => 'input text')),
  2561. '*/div',
  2562. array('div' => array('class' => 'input password')),
  2563. '*/div',
  2564. array('div' => array('class' => 'input date')),
  2565. '*/div',
  2566. array('div' => array('class' => 'input date')),
  2567. '*/div',
  2568. array('div' => array('class' => 'input datetime')),
  2569. '*/div',
  2570. array('div' => array('class' => 'input number')),
  2571. '*/div',
  2572. array('div' => array('class' => 'input select')),
  2573. '*/div',
  2574. '/fieldset'
  2575. );
  2576. $this->assertTags($result, $expected);
  2577. $this->Form->create('Contact');
  2578. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2579. $expected = array(
  2580. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2581. array('div' => array('class' => 'input text')),
  2582. '*/div',
  2583. array('div' => array('class' => 'input text')),
  2584. '*/div',
  2585. array('div' => array('class' => 'input text')),
  2586. '*/div',
  2587. array('div' => array('class' => 'input password')),
  2588. '*/div',
  2589. array('div' => array('class' => 'input date')),
  2590. '*/div',
  2591. array('div' => array('class' => 'input date')),
  2592. '*/div',
  2593. array('div' => array('class' => 'input datetime')),
  2594. '*/div',
  2595. array('div' => array('class' => 'input number')),
  2596. '*/div',
  2597. array('div' => array('class' => 'input select')),
  2598. '*/div',
  2599. );
  2600. $this->assertTags($result, $expected);
  2601. $this->Form->create('Contact');
  2602. $result = $this->Form->inputs('Hello');
  2603. $expected = array(
  2604. 'fieldset' => array(),
  2605. 'legend' => array(),
  2606. 'Hello',
  2607. '/legend',
  2608. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2609. array('div' => array('class' => 'input text')),
  2610. '*/div',
  2611. array('div' => array('class' => 'input text')),
  2612. '*/div',
  2613. array('div' => array('class' => 'input text')),
  2614. '*/div',
  2615. array('div' => array('class' => 'input password')),
  2616. '*/div',
  2617. array('div' => array('class' => 'input date')),
  2618. '*/div',
  2619. array('div' => array('class' => 'input date')),
  2620. '*/div',
  2621. array('div' => array('class' => 'input datetime')),
  2622. '*/div',
  2623. array('div' => array('class' => 'input number')),
  2624. '*/div',
  2625. array('div' => array('class' => 'input select')),
  2626. '*/div',
  2627. '/fieldset'
  2628. );
  2629. $this->assertTags($result, $expected);
  2630. $this->Form->create('Contact');
  2631. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2632. $expected = array(
  2633. 'fieldset' => array(),
  2634. 'legend' => array(),
  2635. 'Hello',
  2636. '/legend',
  2637. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2638. array('div' => array('class' => 'input text')),
  2639. '*/div',
  2640. array('div' => array('class' => 'input text')),
  2641. '*/div',
  2642. array('div' => array('class' => 'input text')),
  2643. '*/div',
  2644. array('div' => array('class' => 'input password')),
  2645. '*/div',
  2646. array('div' => array('class' => 'input date')),
  2647. '*/div',
  2648. array('div' => array('class' => 'input date')),
  2649. '*/div',
  2650. array('div' => array('class' => 'input datetime')),
  2651. '*/div',
  2652. array('div' => array('class' => 'input number')),
  2653. '*/div',
  2654. array('div' => array('class' => 'input select')),
  2655. '*/div',
  2656. '/fieldset'
  2657. );
  2658. $this->assertTags($result, $expected);
  2659. }
  2660. /**
  2661. * testSelectAsCheckbox method
  2662. *
  2663. * test multi-select widget with checkbox formatting.
  2664. *
  2665. * @return void
  2666. */
  2667. public function testSelectAsCheckbox() {
  2668. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2669. $expected = array(
  2670. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2671. array('div' => array('class' => 'checkbox')),
  2672. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2673. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2674. 'first',
  2675. '/label',
  2676. '/div',
  2677. array('div' => array('class' => 'checkbox')),
  2678. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2679. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2680. 'second',
  2681. '/label',
  2682. '/div',
  2683. array('div' => array('class' => 'checkbox')),
  2684. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2685. array('label' => array('for' => 'ModelMultiField2')),
  2686. 'third',
  2687. '/label',
  2688. '/div',
  2689. );
  2690. $this->assertTags($result, $expected);
  2691. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2692. $expected = array(
  2693. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2694. array('div' => array('class' => 'checkbox')),
  2695. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2696. array('label' => array('for' => 'ModelMultiField12')),
  2697. 'half',
  2698. '/label',
  2699. '/div',
  2700. );
  2701. $this->assertTags($result, $expected);
  2702. }
  2703. /**
  2704. * testLabel method
  2705. *
  2706. * test label generation.
  2707. *
  2708. * @return void
  2709. */
  2710. public function testLabel() {
  2711. $this->Form->text('Person.name');
  2712. $result = $this->Form->label();
  2713. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2714. $this->Form->text('Person.name');
  2715. $result = $this->Form->label();
  2716. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2717. $result = $this->Form->label('Person.first_name');
  2718. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2719. $result = $this->Form->label('Person.first_name', 'Your first name');
  2720. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2721. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2722. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2723. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2724. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2725. $result = $this->Form->label('Person.first_name', '');
  2726. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2727. $result = $this->Form->label('Person.2.name', '');
  2728. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2729. }
  2730. /**
  2731. * testTextbox method
  2732. *
  2733. * test textbox element generation
  2734. *
  2735. * @return void
  2736. */
  2737. public function testTextbox() {
  2738. $result = $this->Form->text('Model.field');
  2739. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2740. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2741. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2742. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2743. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2744. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2745. $result = $this->Form->text('Model.text');
  2746. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2747. $Contact = ClassRegistry::getObject('Contact');
  2748. $Contact->validationErrors['text'] = array(true);
  2749. $this->Form->request->data['Contact']['text'] = 'test';
  2750. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2751. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2752. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2753. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2754. $expected = array(
  2755. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2756. );
  2757. $this->assertTags($result, $expected);
  2758. }
  2759. /**
  2760. * testDefaultValue method
  2761. *
  2762. * Test default value setting
  2763. *
  2764. * @return void
  2765. */
  2766. public function testDefaultValue() {
  2767. $this->Form->request->data['Model']['field'] = 'test';
  2768. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2769. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  2770. unset($this->Form->request->data['Model']['field']);
  2771. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2772. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  2773. }
  2774. /**
  2775. * testCheckboxDefaultValue method
  2776. *
  2777. * Test default value setting on checkbox() method
  2778. *
  2779. * @return void
  2780. */
  2781. public function testCheckboxDefaultValue() {
  2782. $this->Form->request->data['Model']['field'] = false;
  2783. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2784. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2785. unset($this->Form->request->data['Model']['field']);
  2786. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2787. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2788. $this->Form->request->data['Model']['field'] = true;
  2789. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2790. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2791. unset($this->Form->request->data['Model']['field']);
  2792. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2793. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2794. }
  2795. /**
  2796. * testError method
  2797. *
  2798. * Test field error generation
  2799. *
  2800. * @return void
  2801. */
  2802. public function testError() {
  2803. $Contact = ClassRegistry::getObject('Contact');
  2804. $Contact->validationErrors['field'] = array(1);
  2805. $result = $this->Form->error('Contact.field');
  2806. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  2807. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2808. $this->assertEquals('Error in field Field', $result);
  2809. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2810. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2811. $this->assertEquals('This field contains invalid input', $result);
  2812. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2813. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  2814. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  2815. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  2816. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  2817. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  2818. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2819. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  2820. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2821. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  2822. $this->assertEquals('<strong>Badness!</strong>', $result);
  2823. $Contact->validationErrors['field'] = array("email");
  2824. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  2825. $expected = array(
  2826. 'div' => array('class' => 'field-error'),
  2827. 'No good!',
  2828. '/div'
  2829. );
  2830. $this->assertTags($result, $expected);
  2831. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  2832. $result = $this->Form->error('Contact.field', array(
  2833. 'notEmpty' => 'Cannot be empty',
  2834. 'email' => 'No good!'
  2835. ));
  2836. $expected = array(
  2837. 'div' => array('class' => 'error-message'),
  2838. 'ul' => array(),
  2839. '<li', 'Cannot be empty', '/li',
  2840. '<li', 'No good!', '/li',
  2841. '<li', 'Something else', '/li',
  2842. '/ul',
  2843. '/div'
  2844. );
  2845. $this->assertTags($result, $expected);
  2846. /** Testing error messages list options **/
  2847. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  2848. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  2849. $expected = array(
  2850. 'div' => array('class' => 'error-message'),
  2851. 'ol' => array(),
  2852. '<li', 'notEmpty', '/li',
  2853. '<li', 'email', '/li',
  2854. '/ol',
  2855. '/div'
  2856. );
  2857. $this->assertTags($result, $expected);
  2858. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  2859. $expected = array(
  2860. 'div' => array('class' => 'error-message'),
  2861. 'ol' => array(),
  2862. '<li', 'notEmpty', '/li',
  2863. '<li', 'email', '/li',
  2864. '/ol',
  2865. '/div'
  2866. );
  2867. $this->assertTags($result, $expected);
  2868. $result = $this->Form->error('Contact.field', null, array(
  2869. 'listOptions' => array(
  2870. 'class' => 'ul-class',
  2871. 'itemOptions' => array(
  2872. 'class' => 'li-class'
  2873. )
  2874. )
  2875. ));
  2876. $expected = array(
  2877. 'div' => array('class' => 'error-message'),
  2878. 'ul' => array('class' => 'ul-class'),
  2879. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  2880. array('li' => array('class' => 'li-class')), 'email', '/li',
  2881. '/ul',
  2882. '/div'
  2883. );
  2884. $this->assertTags($result, $expected);
  2885. }
  2886. /**
  2887. * test error options when using form->input();
  2888. *
  2889. * @return void
  2890. */
  2891. public function testInputErrorEscape() {
  2892. $this->Form->create('ValidateProfile');
  2893. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  2894. $ValidateProfile->validationErrors['city'] = array('required<br>');
  2895. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
  2896. $this->assertRegExp('/required&lt;br&gt;/', $result);
  2897. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => false))));
  2898. $this->assertRegExp('/required<br>/', $result);
  2899. }
  2900. /**
  2901. * testPassword method
  2902. *
  2903. * Test password element generation
  2904. *
  2905. * @return void
  2906. */
  2907. public function testPassword() {
  2908. $Contact = ClassRegistry::getObject('Contact');
  2909. $result = $this->Form->password('Contact.field');
  2910. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  2911. $Contact->validationErrors['passwd'] = 1;
  2912. $this->Form->request->data['Contact']['passwd'] = 'test';
  2913. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  2914. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2915. }
  2916. /**
  2917. * testRadio method
  2918. *
  2919. * Test radio element set generation
  2920. *
  2921. * @return void
  2922. */
  2923. public function testRadio() {
  2924. $result = $this->Form->radio('Model.field', array('option A'));
  2925. $expected = array(
  2926. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2927. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2928. 'label' => array('for' => 'ModelField0'),
  2929. 'option A',
  2930. '/label'
  2931. );
  2932. $this->assertTags($result, $expected);
  2933. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  2934. $expected = array(
  2935. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2936. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  2937. 'label' => array('for' => 'ModelField12'),
  2938. 'half',
  2939. '/label'
  2940. );
  2941. $this->assertTags($result, $expected);
  2942. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  2943. $expected = array(
  2944. 'fieldset' => array(),
  2945. 'legend' => array(),
  2946. 'Field',
  2947. '/legend',
  2948. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2949. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2950. array('label' => array('for' => 'ModelField0')),
  2951. 'option A',
  2952. '/label',
  2953. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2954. array('label' => array('for' => 'ModelField1')),
  2955. 'option B',
  2956. '/label',
  2957. '/fieldset'
  2958. );
  2959. $this->assertTags($result, $expected);
  2960. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  2961. $expected = array(
  2962. 'fieldset' => array(),
  2963. 'legend' => array(),
  2964. 'Field',
  2965. '/legend',
  2966. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2967. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2968. array('label' => array('for' => 'ModelField0')),
  2969. 'option A',
  2970. '/label',
  2971. 'br' => array(),
  2972. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2973. array('label' => array('for' => 'ModelField1')),
  2974. 'option B',
  2975. '/label',
  2976. '/fieldset'
  2977. );
  2978. $this->assertTags($result, $expected);
  2979. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2980. $expected = array(
  2981. 'div' => array('class' => 'input radio'),
  2982. 'fieldset' => array(),
  2983. 'legend' => array(),
  2984. 'Legend title',
  2985. '/legend',
  2986. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2987. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2988. array('label' => array('for' => 'NewsletterSubscribe0')),
  2989. 'Unsubscribe',
  2990. '/label',
  2991. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2992. array('label' => array('for' => 'NewsletterSubscribe1')),
  2993. 'Subscribe',
  2994. '/label',
  2995. '/fieldset',
  2996. '/div'
  2997. );
  2998. $this->assertTags($result, $expected);
  2999. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3000. $expected = array(
  3001. 'div' => array('class' => 'input radio'),
  3002. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3003. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3004. array('label' => array('for' => 'NewsletterSubscribe0')),
  3005. 'Unsubscribe',
  3006. '/label',
  3007. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3008. array('label' => array('for' => 'NewsletterSubscribe1')),
  3009. 'Subscribe',
  3010. '/label',
  3011. '/div'
  3012. );
  3013. $this->assertTags($result, $expected);
  3014. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3015. $expected = array(
  3016. 'div' => array('class' => 'input radio'),
  3017. 'fieldset' => array(),
  3018. 'legend' => array(),
  3019. 'Legend title',
  3020. '/legend',
  3021. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3022. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3023. 'Unsubscribe',
  3024. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3025. 'Subscribe',
  3026. '/fieldset',
  3027. '/div'
  3028. );
  3029. $this->assertTags($result, $expected);
  3030. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3031. $expected = array(
  3032. 'div' => array('class' => 'input radio'),
  3033. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3034. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3035. 'Unsubscribe',
  3036. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3037. 'Subscribe',
  3038. '/div'
  3039. );
  3040. $this->assertTags($result, $expected);
  3041. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'value' => '1', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3042. $expected = array(
  3043. 'div' => array('class' => 'input radio'),
  3044. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3045. 'Unsubscribe',
  3046. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1', 'checked' => 'checked')),
  3047. 'Subscribe',
  3048. '/div'
  3049. );
  3050. $this->assertTags($result, $expected);
  3051. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3052. $expected = array(
  3053. 'fieldset' => array(),
  3054. 'legend' => array(),
  3055. 'Gender',
  3056. '/legend',
  3057. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3058. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3059. array('label' => array('for' => 'EmployeeGenderMale')),
  3060. 'Male',
  3061. '/label',
  3062. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3063. array('label' => array('for' => 'EmployeeGenderFemale')),
  3064. 'Female',
  3065. '/label',
  3066. '/fieldset',
  3067. );
  3068. $this->assertTags($result, $expected);
  3069. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3070. $expected = array(
  3071. 'fieldset' => array(),
  3072. 'legend' => array(),
  3073. 'Gender',
  3074. '/legend',
  3075. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3076. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3077. array('label' => array('for' => 'OfficerGenderMale')),
  3078. 'Male',
  3079. '/label',
  3080. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3081. array('label' => array('for' => 'OfficerGenderFemale')),
  3082. 'Female',
  3083. '/label',
  3084. '/fieldset',
  3085. );
  3086. $this->assertTags($result, $expected);
  3087. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3088. $expected = array(
  3089. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3090. array('input' => array('type' => 'radio', 'name' => 'data[Contact][1][imrequired]', 'value' => '0', 'id' => 'Contact1Imrequired0')),
  3091. 'label' => array('for' => 'Contact1Imrequired0'),
  3092. 'option A',
  3093. '/label'
  3094. );
  3095. $this->assertTags($result, $expected);
  3096. $result = $this->Form->radio('Model.1.field', array('option A'));
  3097. $expected = array(
  3098. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3099. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3100. 'label' => array('for' => 'Model1Field0'),
  3101. 'option A',
  3102. '/label'
  3103. );
  3104. $this->assertTags($result, $expected);
  3105. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3106. $expected = array(
  3107. 'fieldset' => array(),
  3108. 'legend' => array(),
  3109. 'Field',
  3110. '/legend',
  3111. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3112. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3113. array('label' => array('for' => 'ModelField0')),
  3114. 'option A',
  3115. '/label',
  3116. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3117. array('label' => array('for' => 'ModelField1')),
  3118. 'option B',
  3119. '/label',
  3120. '/fieldset'
  3121. );
  3122. $this->assertTags($result, $expected);
  3123. $result = $this->Form->radio(
  3124. 'Model.field',
  3125. array('option A', 'option B'),
  3126. array('between' => 'I am between')
  3127. );
  3128. $expected = array(
  3129. 'fieldset' => array(),
  3130. 'legend' => array(),
  3131. 'Field',
  3132. '/legend',
  3133. 'I am between',
  3134. 'input' => array(
  3135. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3136. 'value' => '', 'id' => 'ModelField_'
  3137. ),
  3138. array('input' => array(
  3139. 'type' => 'radio', 'name' => 'data[Model][field]',
  3140. 'value' => '0', 'id' => 'ModelField0'
  3141. )),
  3142. array('label' => array('for' => 'ModelField0')),
  3143. 'option A',
  3144. '/label',
  3145. array('input' => array(
  3146. 'type' => 'radio', 'name' => 'data[Model][field]',
  3147. 'value' => '1', 'id' => 'ModelField1'
  3148. )),
  3149. array('label' => array('for' => 'ModelField1')),
  3150. 'option B',
  3151. '/label',
  3152. '/fieldset'
  3153. );
  3154. $this->assertTags($result, $expected);
  3155. }
  3156. /**
  3157. * Test that radios with a 0 value are selected under the correct conditions.
  3158. *
  3159. * @return void
  3160. */
  3161. public function testRadioOptionWithZeroValue() {
  3162. $expected = array(
  3163. 'fieldset' => array(),
  3164. 'legend' => array(),
  3165. 'Field',
  3166. '/legend',
  3167. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3168. array('label' => array('for' => 'ModelField1')),
  3169. 'Yes',
  3170. '/label',
  3171. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3172. array('label' => array('for' => 'ModelField0')),
  3173. 'No',
  3174. '/label',
  3175. '/fieldset'
  3176. );
  3177. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3178. $this->assertTags($result, $expected);
  3179. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3180. $this->assertTags($result, $expected);
  3181. $expected = array(
  3182. 'fieldset' => array(),
  3183. 'legend' => array(),
  3184. 'Field',
  3185. '/legend',
  3186. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3187. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3188. array('label' => array('for' => 'ModelField1')),
  3189. 'Yes',
  3190. '/label',
  3191. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3192. array('label' => array('for' => 'ModelField0')),
  3193. 'No',
  3194. '/label',
  3195. '/fieldset'
  3196. );
  3197. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3198. $this->assertTags($result, $expected);
  3199. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3200. $this->assertTags($result, $expected);
  3201. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3202. $this->assertTags($result, $expected);
  3203. }
  3204. /**
  3205. * test disabled radio options
  3206. *
  3207. * @return void
  3208. */
  3209. public function testRadioDisabled() {
  3210. $result = $this->Form->radio(
  3211. 'Model.field',
  3212. array('option A', 'option B'),
  3213. array('disabled' => array('option A'), 'value' => '0')
  3214. );
  3215. $expected = array(
  3216. 'fieldset' => array(),
  3217. 'legend' => array(),
  3218. 'Field',
  3219. '/legend',
  3220. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3221. array('label' => array('for' => 'ModelField0')),
  3222. 'option A',
  3223. '/label',
  3224. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3225. array('label' => array('for' => 'ModelField1')),
  3226. 'option B',
  3227. '/label',
  3228. '/fieldset'
  3229. );
  3230. $this->assertTags($result, $expected);
  3231. $result = $this->Form->radio(
  3232. 'Model.field',
  3233. array('option A', 'option B'),
  3234. array('disabled' => true, 'value' => '0')
  3235. );
  3236. $expected = array(
  3237. 'fieldset' => array(),
  3238. 'legend' => array(),
  3239. 'Field',
  3240. '/legend',
  3241. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3242. array('label' => array('for' => 'ModelField0')),
  3243. 'option A',
  3244. '/label',
  3245. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3246. array('label' => array('for' => 'ModelField1')),
  3247. 'option B',
  3248. '/label',
  3249. '/fieldset'
  3250. );
  3251. $this->assertTags($result, $expected);
  3252. $result = $this->Form->radio(
  3253. 'Model.field',
  3254. array('option A', 'option B'),
  3255. array('disabled' => 'disabled', 'value' => '0')
  3256. );
  3257. $expected = array(
  3258. 'fieldset' => array(),
  3259. 'legend' => array(),
  3260. 'Field',
  3261. '/legend',
  3262. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3263. array('label' => array('for' => 'ModelField0')),
  3264. 'option A',
  3265. '/label',
  3266. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3267. array('label' => array('for' => 'ModelField1')),
  3268. 'option B',
  3269. '/label',
  3270. '/fieldset'
  3271. );
  3272. $this->assertTags($result, $expected);
  3273. }
  3274. /**
  3275. * test disabling the hidden input for radio buttons
  3276. *
  3277. * @return void
  3278. */
  3279. public function testRadioHiddenInputDisabling() {
  3280. $result = $this->Form->input('Model.1.field', array(
  3281. 'type' => 'radio',
  3282. 'options' => array('option A'),
  3283. 'hiddenField' => false
  3284. )
  3285. );
  3286. $expected = array(
  3287. 'div' => array('class' => 'input radio'),
  3288. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3289. 'label' => array('for' => 'Model1Field0'),
  3290. 'option A',
  3291. '/label',
  3292. '/div'
  3293. );
  3294. $this->assertTags($result, $expected);
  3295. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3296. $expected = array(
  3297. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3298. 'label' => array('for' => 'Model1Field0'),
  3299. 'option A',
  3300. '/label'
  3301. );
  3302. $this->assertTags($result, $expected);
  3303. }
  3304. /**
  3305. * test adding an empty option for radio buttons
  3306. *
  3307. * @return void
  3308. */
  3309. public function testRadioAddEmptyOption() {
  3310. $result = $this->Form->input('Model.1.field', array(
  3311. 'type' => 'radio',
  3312. 'options' => array('option A'),
  3313. 'empty' => true,
  3314. 'hiddenField' => false
  3315. ));
  3316. $expected = array(
  3317. 'div' => array('class' => 'input radio'),
  3318. 'fieldset' => array(),
  3319. 'legend' => array(),
  3320. 'Field',
  3321. '/legend',
  3322. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3323. array('label' => array('for' => 'Model1Field')),
  3324. __('empty'),
  3325. '/label',
  3326. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3327. array('label' => array('for' => 'Model1Field0')),
  3328. 'option A',
  3329. '/label',
  3330. '/fieldset',
  3331. '/div'
  3332. );
  3333. $this->assertTags($result, $expected);
  3334. $result = $this->Form->input('Model.1.field', array(
  3335. 'type' => 'radio',
  3336. 'options' => array('option A'),
  3337. 'empty' => 'CustomEmptyLabel',
  3338. 'hiddenField' => false
  3339. ));
  3340. $expected = array(
  3341. 'div' => array('class' => 'input radio'),
  3342. 'fieldset' => array(),
  3343. 'legend' => array(),
  3344. 'Field',
  3345. '/legend',
  3346. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3347. array('label' => array('for' => 'Model1Field')),
  3348. 'CustomEmptyLabel',
  3349. '/label',
  3350. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3351. array('label' => array('for' => 'Model1Field0')),
  3352. 'option A',
  3353. '/label',
  3354. '/fieldset',
  3355. '/div'
  3356. );
  3357. $this->assertTags($result, $expected);
  3358. $result = $this->Form->input('Model.1.field', array(
  3359. 'type' => 'radio',
  3360. 'options' => array('option A'),
  3361. 'empty' => false,
  3362. 'hiddenField' => false
  3363. ));
  3364. $this->assertTextNotContains('"Model1Field"', $result);
  3365. }
  3366. /**
  3367. * testSelect method
  3368. *
  3369. * Test select element generation.
  3370. *
  3371. * @return void
  3372. */
  3373. public function testSelect() {
  3374. $result = $this->Form->select('Model.field', array());
  3375. $expected = array(
  3376. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3377. array('option' => array('value' => '')),
  3378. '/option',
  3379. '/select'
  3380. );
  3381. $this->assertTags($result, $expected);
  3382. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3383. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3384. $expected = array(
  3385. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3386. array('option' => array('value' => '')),
  3387. '/option',
  3388. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3389. 'good',
  3390. '/option',
  3391. array('option' => array('value' => 'other')),
  3392. 'bad',
  3393. '/option',
  3394. '/select'
  3395. );
  3396. $this->assertTags($result, $expected);
  3397. $this->Form->request->data = array();
  3398. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3399. $expected = array(
  3400. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3401. array('option' => array('value' => '')),
  3402. '/option',
  3403. array('option' => array('value' => 'value')),
  3404. 'good',
  3405. '/option',
  3406. array('option' => array('value' => 'other')),
  3407. 'bad',
  3408. '/option',
  3409. '/select'
  3410. );
  3411. $this->assertTags($result, $expected);
  3412. $result = $this->Form->select(
  3413. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3414. array('empty' => false)
  3415. );
  3416. $expected = array(
  3417. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3418. array('option' => array('value' => 'first')),
  3419. 'first &quot;html&quot; &lt;chars&gt;',
  3420. '/option',
  3421. array('option' => array('value' => 'second')),
  3422. 'value',
  3423. '/option',
  3424. '/select'
  3425. );
  3426. $this->assertTags($result, $expected);
  3427. $result = $this->Form->select(
  3428. 'Model.field',
  3429. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3430. array('escape' => false, 'empty' => false)
  3431. );
  3432. $expected = array(
  3433. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3434. array('option' => array('value' => 'first')),
  3435. 'first "html" <chars>',
  3436. '/option',
  3437. array('option' => array('value' => 'second')),
  3438. 'value',
  3439. '/option',
  3440. '/select'
  3441. );
  3442. $this->assertTags($result, $expected);
  3443. $options = array(
  3444. array('value' => 'first', 'name' => 'First'),
  3445. array('value' => 'first', 'name' => 'Another First'),
  3446. );
  3447. $result = $this->Form->select(
  3448. 'Model.field',
  3449. $options,
  3450. array('escape' => false, 'empty' => false)
  3451. );
  3452. $expected = array(
  3453. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3454. array('option' => array('value' => 'first')),
  3455. 'First',
  3456. '/option',
  3457. array('option' => array('value' => 'first')),
  3458. 'Another First',
  3459. '/option',
  3460. '/select'
  3461. );
  3462. $this->assertTags($result, $expected);
  3463. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3464. $result = $this->Form->select(
  3465. 'Model.contact_id',
  3466. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3467. array('escape' => false, 'empty' => 'pick something')
  3468. );
  3469. $expected = array(
  3470. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3471. array('option' => array('value' => '')), 'pick something', '/option',
  3472. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3473. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3474. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3475. '/select'
  3476. );
  3477. $this->assertTags($result, $expected);
  3478. $this->Form->request->data['Model']['field'] = 0;
  3479. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3480. $expected = array(
  3481. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3482. array('option' => array('value' => '')), '/option',
  3483. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3484. array('option' => array('value' => '1')), 'Yes', '/option',
  3485. '/select'
  3486. );
  3487. $this->assertTags($result, $expected);
  3488. }
  3489. /**
  3490. * test that select() with optiongroups listens to the escape param.
  3491. *
  3492. * @return void
  3493. */
  3494. public function testSelectOptionGroupEscaping() {
  3495. $options = array(
  3496. '>< Key' => array(
  3497. 1 => 'One',
  3498. 2 => 'Two'
  3499. )
  3500. );
  3501. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3502. $expected = array(
  3503. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3504. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3505. array('option' => array('value' => '1')), 'One', '/option',
  3506. array('option' => array('value' => '2')), 'Two', '/option',
  3507. '/optgroup',
  3508. '/select'
  3509. );
  3510. $this->assertTags($result, $expected);
  3511. $options = array(
  3512. '>< Key' => array(
  3513. 1 => 'One',
  3514. 2 => 'Two'
  3515. )
  3516. );
  3517. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3518. $expected = array(
  3519. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3520. 'optgroup' => array('label' => '>< Key'),
  3521. array('option' => array('value' => '1')), 'One', '/option',
  3522. array('option' => array('value' => '2')), 'Two', '/option',
  3523. '/optgroup',
  3524. '/select'
  3525. );
  3526. $this->assertTags($result, $expected);
  3527. }
  3528. /**
  3529. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3530. *
  3531. * @return void
  3532. */
  3533. public function testSelectWithNullAttributes() {
  3534. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3535. $expected = array(
  3536. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3537. array('option' => array('value' => '0')),
  3538. 'first',
  3539. '/option',
  3540. array('option' => array('value' => '1')),
  3541. 'second',
  3542. '/option',
  3543. '/select'
  3544. );
  3545. $this->assertTags($result, $expected);
  3546. }
  3547. /**
  3548. * testNestedSelect method
  3549. *
  3550. * test select element generation with optgroups
  3551. *
  3552. * @return void
  3553. */
  3554. public function testNestedSelect() {
  3555. $result = $this->Form->select(
  3556. 'Model.field',
  3557. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3558. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3559. )), array('empty' => false)
  3560. );
  3561. $expected = array(
  3562. 'select' => array('name' => 'data[Model][field]',
  3563. 'id' => 'ModelField'),
  3564. array('option' => array('value' => 1)),
  3565. 'One',
  3566. '/option',
  3567. array('option' => array('value' => 2)),
  3568. 'Two',
  3569. '/option',
  3570. array('optgroup' => array('label' => 'Three')),
  3571. array('option' => array('value' => 4)),
  3572. 'Four',
  3573. '/option',
  3574. array('option' => array('value' => 5)),
  3575. 'Five',
  3576. '/option',
  3577. '/optgroup',
  3578. '/select'
  3579. );
  3580. $this->assertTags($result, $expected);
  3581. $result = $this->Form->select(
  3582. 'Model.field',
  3583. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3584. array('showParents' => true, 'empty' => false)
  3585. );
  3586. $expected = array(
  3587. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3588. array('option' => array('value' => 1)),
  3589. 'One',
  3590. '/option',
  3591. array('option' => array('value' => 2)),
  3592. 'Two',
  3593. '/option',
  3594. array('optgroup' => array('label' => 'Three')),
  3595. array('option' => array('value' => 3)),
  3596. 'Three',
  3597. '/option',
  3598. array('option' => array('value' => 4)),
  3599. 'Four',
  3600. '/option',
  3601. '/optgroup',
  3602. '/select'
  3603. );
  3604. $this->assertTags($result, $expected);
  3605. }
  3606. /**
  3607. * testSelectMultiple method
  3608. *
  3609. * test generation of multiple select elements
  3610. *
  3611. * @return void
  3612. */
  3613. public function testSelectMultiple() {
  3614. $options = array('first', 'second', 'third');
  3615. $result = $this->Form->select(
  3616. 'Model.multi_field', $options, array('multiple' => true)
  3617. );
  3618. $expected = array(
  3619. 'input' => array(
  3620. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3621. ),
  3622. 'select' => array(
  3623. 'name' => 'data[Model][multi_field][]',
  3624. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3625. ),
  3626. array('option' => array('value' => '0')),
  3627. 'first',
  3628. '/option',
  3629. array('option' => array('value' => '1')),
  3630. 'second',
  3631. '/option',
  3632. array('option' => array('value' => '2')),
  3633. 'third',
  3634. '/option',
  3635. '/select'
  3636. );
  3637. $this->assertTags($result, $expected);
  3638. $result = $this->Form->select(
  3639. 'Model.multi_field', $options, array('multiple' => 'multiple')
  3640. );
  3641. $expected = array(
  3642. 'input' => array(
  3643. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3644. ),
  3645. 'select' => array(
  3646. 'name' => 'data[Model][multi_field][]',
  3647. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3648. ),
  3649. array('option' => array('value' => '0')),
  3650. 'first',
  3651. '/option',
  3652. array('option' => array('value' => '1')),
  3653. 'second',
  3654. '/option',
  3655. array('option' => array('value' => '2')),
  3656. 'third',
  3657. '/option',
  3658. '/select'
  3659. );
  3660. $this->assertTags($result, $expected);
  3661. $result = $this->Form->select(
  3662. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  3663. );
  3664. $expected = array(
  3665. 'input' => array(
  3666. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3667. ),
  3668. 'select' => array(
  3669. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  3670. 'multiple' => 'multiple'
  3671. ),
  3672. array('option' => array('value' => '0', 'selected' => 'selected')),
  3673. 'first',
  3674. '/option',
  3675. array('option' => array('value' => '1', 'selected' => 'selected')),
  3676. 'second',
  3677. '/option',
  3678. array('option' => array('value' => '2')),
  3679. 'third',
  3680. '/option',
  3681. '/select'
  3682. );
  3683. $this->assertTags($result, $expected);
  3684. $result = $this->Form->select(
  3685. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  3686. );
  3687. $expected = array(
  3688. 'select' => array(
  3689. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  3690. ),
  3691. array('option' => array('value' => '0', 'selected' => 'selected')),
  3692. 'first',
  3693. '/option',
  3694. array('option' => array('value' => '1', 'selected' => 'selected')),
  3695. 'second',
  3696. '/option',
  3697. array('option' => array('value' => '2')),
  3698. 'third',
  3699. '/option',
  3700. '/select'
  3701. );
  3702. $this->assertTags($result, $expected);
  3703. }
  3704. /**
  3705. * test generation of habtm select boxes.
  3706. *
  3707. * @return void
  3708. */
  3709. public function testHabtmSelectBox() {
  3710. $this->View->viewVars['contactTags'] = array(
  3711. 1 => 'blue',
  3712. 2 => 'red',
  3713. 3 => 'green'
  3714. );
  3715. $this->Form->request->data = array(
  3716. 'Contact' => array(),
  3717. 'ContactTag' => array(
  3718. array(
  3719. 'id' => 1,
  3720. 'name' => 'blue'
  3721. ),
  3722. array(
  3723. 'id' => 3,
  3724. 'name' => 'green'
  3725. )
  3726. )
  3727. );
  3728. $this->Form->create('Contact');
  3729. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3730. $expected = array(
  3731. 'input' => array(
  3732. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3733. ),
  3734. 'select' => array(
  3735. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  3736. 'multiple' => 'multiple'
  3737. ),
  3738. array('option' => array('value' => '1', 'selected' => 'selected')),
  3739. 'blue',
  3740. '/option',
  3741. array('option' => array('value' => '2')),
  3742. 'red',
  3743. '/option',
  3744. array('option' => array('value' => '3', 'selected' => 'selected')),
  3745. 'green',
  3746. '/option',
  3747. '/select'
  3748. );
  3749. $this->assertTags($result, $expected);
  3750. }
  3751. /**
  3752. * test generation of multi select elements in checkbox format
  3753. *
  3754. * @return void
  3755. */
  3756. public function testSelectMultipleCheckboxes() {
  3757. $result = $this->Form->select(
  3758. 'Model.multi_field',
  3759. array('first', 'second', 'third'),
  3760. array('multiple' => 'checkbox')
  3761. );
  3762. $expected = array(
  3763. 'input' => array(
  3764. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3765. ),
  3766. array('div' => array('class' => 'checkbox')),
  3767. array('input' => array(
  3768. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3769. 'value' => '0', 'id' => 'ModelMultiField0'
  3770. )),
  3771. array('label' => array('for' => 'ModelMultiField0')),
  3772. 'first',
  3773. '/label',
  3774. '/div',
  3775. array('div' => array('class' => 'checkbox')),
  3776. array('input' => array(
  3777. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3778. 'value' => '1', 'id' => 'ModelMultiField1'
  3779. )),
  3780. array('label' => array('for' => 'ModelMultiField1')),
  3781. 'second',
  3782. '/label',
  3783. '/div',
  3784. array('div' => array('class' => 'checkbox')),
  3785. array('input' => array(
  3786. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3787. 'value' => '2', 'id' => 'ModelMultiField2'
  3788. )),
  3789. array('label' => array('for' => 'ModelMultiField2')),
  3790. 'third',
  3791. '/label',
  3792. '/div'
  3793. );
  3794. $this->assertTags($result, $expected);
  3795. $result = $this->Form->select(
  3796. 'Model.multi_field',
  3797. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3798. array('multiple' => 'checkbox')
  3799. );
  3800. $expected = array(
  3801. 'input' => array(
  3802. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3803. ),
  3804. array('div' => array('class' => 'checkbox')),
  3805. array('input' => array(
  3806. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3807. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  3808. )),
  3809. array('label' => array('for' => 'ModelMultiFieldA')),
  3810. 'first',
  3811. '/label',
  3812. '/div',
  3813. array('div' => array('class' => 'checkbox')),
  3814. array('input' => array(
  3815. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3816. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  3817. )),
  3818. array('label' => array('for' => 'ModelMultiFieldB')),
  3819. 'second',
  3820. '/label',
  3821. '/div',
  3822. array('div' => array('class' => 'checkbox')),
  3823. array('input' => array(
  3824. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3825. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  3826. )),
  3827. array('label' => array('for' => 'ModelMultiFieldC')),
  3828. 'third',
  3829. '/label',
  3830. '/div'
  3831. );
  3832. $this->assertTags($result, $expected);
  3833. $result = $this->Form->select(
  3834. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  3835. );
  3836. $expected = array(
  3837. 'input' => array(
  3838. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3839. ),
  3840. array('div' => array('class' => 'checkbox')),
  3841. array('input' => array(
  3842. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3843. 'value' => '1', 'id' => 'ModelMultiField1'
  3844. )),
  3845. array('label' => array('for' => 'ModelMultiField1')),
  3846. 'first',
  3847. '/label',
  3848. '/div'
  3849. );
  3850. $this->assertTags($result, $expected);
  3851. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3852. $result = $this->Form->select(
  3853. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3854. );
  3855. $expected = array(
  3856. 'input' => array(
  3857. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3858. ),
  3859. array('div' => array('class' => 'checkbox')),
  3860. array('input' => array(
  3861. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3862. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  3863. )),
  3864. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  3865. 'first',
  3866. '/label',
  3867. '/div',
  3868. array('div' => array('class' => 'checkbox')),
  3869. array('input' => array(
  3870. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3871. 'value' => 'Array', 'id' => 'ModelTagsArray'
  3872. )),
  3873. array('label' => array('for' => 'ModelTagsArray')),
  3874. 'Array',
  3875. '/label',
  3876. '/div'
  3877. );
  3878. $this->assertTags($result, $expected);
  3879. }
  3880. /**
  3881. * test multiple checkboxes with div styles.
  3882. *
  3883. * @return void
  3884. */
  3885. public function testSelectMultipleCheckboxDiv() {
  3886. $result = $this->Form->select(
  3887. 'Model.tags',
  3888. array('first', 'second'),
  3889. array('multiple' => 'checkbox', 'class' => 'my-class')
  3890. );
  3891. $expected = array(
  3892. 'input' => array(
  3893. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3894. ),
  3895. array('div' => array('class' => 'my-class')),
  3896. array('input' => array(
  3897. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3898. 'value' => '0', 'id' => 'ModelTags0'
  3899. )),
  3900. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  3901. '/div',
  3902. array('div' => array('class' => 'my-class')),
  3903. array('input' => array(
  3904. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3905. 'value' => '1', 'id' => 'ModelTags1'
  3906. )),
  3907. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  3908. '/div'
  3909. );
  3910. $this->assertTags($result, $expected);
  3911. $result = $this->Form->input('Model.tags', array(
  3912. 'options' => array('first', 'second'),
  3913. 'multiple' => 'checkbox',
  3914. 'class' => 'my-class',
  3915. 'div' => false,
  3916. 'label' => false
  3917. ));
  3918. $this->assertTags($result, $expected);
  3919. $Contact = ClassRegistry::getObject('Contact');
  3920. $Contact->validationErrors['tags'] = 'Select atleast one option';
  3921. $result = $this->Form->input('Contact.tags', array(
  3922. 'options' => array('one'),
  3923. 'multiple' => 'checkbox',
  3924. 'label' => false,
  3925. 'div' => false
  3926. ));
  3927. $expected = array(
  3928. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3929. array('div' => array('class' => 'checkbox form-error')),
  3930. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3931. array('label' => array('for' => 'ContactTags0')),
  3932. 'one',
  3933. '/label',
  3934. '/div'
  3935. );
  3936. $this->assertTags($result, $expected);
  3937. $result = $this->Form->input('Contact.tags', array(
  3938. 'options' => array('one'),
  3939. 'multiple' => 'checkbox',
  3940. 'class' => 'mycheckbox',
  3941. 'label' => false,
  3942. 'div' => false
  3943. ));
  3944. $expected = array(
  3945. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3946. array('div' => array('class' => 'mycheckbox form-error')),
  3947. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3948. array('label' => array('for' => 'ContactTags0')),
  3949. 'one',
  3950. '/label',
  3951. '/div'
  3952. );
  3953. $this->assertTags($result, $expected);
  3954. }
  3955. /**
  3956. * Checks the security hash array generated for multiple-input checkbox elements
  3957. *
  3958. * @return void
  3959. */
  3960. public function testSelectMultipleCheckboxSecurity() {
  3961. $this->Form->request['_Token'] = array('key' => 'testKey');
  3962. $this->assertEquals(array(), $this->Form->fields);
  3963. $result = $this->Form->select(
  3964. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  3965. array('multiple' => 'checkbox')
  3966. );
  3967. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  3968. $result = $this->Form->secure($this->Form->fields);
  3969. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  3970. $this->assertRegExp('/"' . $key . '"/', $result);
  3971. }
  3972. /**
  3973. * Multiple select elements should always be secured as they always participate
  3974. * in the POST data.
  3975. *
  3976. * @return void
  3977. */
  3978. public function testSelectMultipleSecureWithNoOptions() {
  3979. $this->Form->request['_Token'] = array('key' => 'testkey');
  3980. $this->assertEquals(array(), $this->Form->fields);
  3981. $result = $this->Form->select(
  3982. 'Model.select',
  3983. array(),
  3984. array('multiple' => true)
  3985. );
  3986. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3987. }
  3988. /**
  3989. * When a select box has no options it should not be added to the fields list
  3990. * as it always fail post validation.
  3991. *
  3992. * @return void
  3993. */
  3994. public function testSelectNoSecureWithNoOptions() {
  3995. $this->Form->request['_Token'] = array('key' => 'testkey');
  3996. $this->assertEquals(array(), $this->Form->fields);
  3997. $this->Form->select(
  3998. 'Model.select',
  3999. array()
  4000. );
  4001. $this->assertEquals(array(), $this->Form->fields);
  4002. $this->Form->select(
  4003. 'Model.select',
  4004. array(),
  4005. array('empty' => true)
  4006. );
  4007. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4008. }
  4009. /**
  4010. * testInputMultipleCheckboxes method
  4011. *
  4012. * test input() resulting in multi select elements being generated.
  4013. *
  4014. * @return void
  4015. */
  4016. public function testInputMultipleCheckboxes() {
  4017. $result = $this->Form->input('Model.multi_field', array(
  4018. 'options' => array('first', 'second', 'third'),
  4019. 'multiple' => 'checkbox'
  4020. ));
  4021. $expected = array(
  4022. array('div' => array('class' => 'input select')),
  4023. array('label' => array('for' => 'ModelMultiField')),
  4024. 'Multi Field',
  4025. '/label',
  4026. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4027. array('div' => array('class' => 'checkbox')),
  4028. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4029. array('label' => array('for' => 'ModelMultiField0')),
  4030. 'first',
  4031. '/label',
  4032. '/div',
  4033. array('div' => array('class' => 'checkbox')),
  4034. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4035. array('label' => array('for' => 'ModelMultiField1')),
  4036. 'second',
  4037. '/label',
  4038. '/div',
  4039. array('div' => array('class' => 'checkbox')),
  4040. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4041. array('label' => array('for' => 'ModelMultiField2')),
  4042. 'third',
  4043. '/label',
  4044. '/div',
  4045. '/div'
  4046. );
  4047. $this->assertTags($result, $expected);
  4048. $result = $this->Form->input('Model.multi_field', array(
  4049. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4050. 'multiple' => 'checkbox'
  4051. ));
  4052. $expected = array(
  4053. array('div' => array('class' => 'input select')),
  4054. array('label' => array('for' => 'ModelMultiField')),
  4055. 'Multi Field',
  4056. '/label',
  4057. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4058. array('div' => array('class' => 'checkbox')),
  4059. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4060. array('label' => array('for' => 'ModelMultiFieldA')),
  4061. 'first',
  4062. '/label',
  4063. '/div',
  4064. array('div' => array('class' => 'checkbox')),
  4065. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4066. array('label' => array('for' => 'ModelMultiFieldB')),
  4067. 'second',
  4068. '/label',
  4069. '/div',
  4070. array('div' => array('class' => 'checkbox')),
  4071. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4072. array('label' => array('for' => 'ModelMultiFieldC')),
  4073. 'third',
  4074. '/label',
  4075. '/div',
  4076. '/div'
  4077. );
  4078. $this->assertTags($result, $expected);
  4079. $result = $this->Form->input('Model.multi_field', array(
  4080. 'options' => array('1' => 'first'),
  4081. 'multiple' => 'checkbox',
  4082. 'label' => false,
  4083. 'div' => false
  4084. ));
  4085. $expected = array(
  4086. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4087. array('div' => array('class' => 'checkbox')),
  4088. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4089. array('label' => array('for' => 'ModelMultiField1')),
  4090. 'first',
  4091. '/label',
  4092. '/div'
  4093. );
  4094. $this->assertTags($result, $expected);
  4095. $result = $this->Form->input('Model.multi_field', array(
  4096. 'options' => array('2' => 'second'),
  4097. 'multiple' => 'checkbox',
  4098. 'label' => false,
  4099. 'div' => false
  4100. ));
  4101. $expected = array(
  4102. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4103. array('div' => array('class' => 'checkbox')),
  4104. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4105. array('label' => array('for' => 'ModelMultiField2')),
  4106. 'second',
  4107. '/label',
  4108. '/div'
  4109. );
  4110. $this->assertTags($result, $expected);
  4111. }
  4112. /**
  4113. * testSelectHiddenFieldOmission method
  4114. *
  4115. * test that select() with 'hiddenField' => false omits the hidden field
  4116. *
  4117. * @return void
  4118. */
  4119. public function testSelectHiddenFieldOmission() {
  4120. $result = $this->Form->select('Model.multi_field',
  4121. array('first', 'second'),
  4122. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4123. );
  4124. $expected = array(
  4125. array('div' => array('class' => 'checkbox')),
  4126. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4127. array('label' => array('for' => 'ModelMultiField0')),
  4128. 'first',
  4129. '/label',
  4130. '/div',
  4131. array('div' => array('class' => 'checkbox')),
  4132. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4133. array('label' => array('for' => 'ModelMultiField1')),
  4134. 'second',
  4135. '/label',
  4136. '/div'
  4137. );
  4138. $this->assertTags($result, $expected);
  4139. $result = $this->Form->input('Model.multi_field', array(
  4140. 'options' => array('first', 'second'),
  4141. 'multiple' => 'checkbox',
  4142. 'hiddenField' => false
  4143. ));
  4144. $expected = array(
  4145. array('div' => array('class' => 'input select')),
  4146. array('label' => array('for' => 'ModelMultiField')),
  4147. 'Multi Field',
  4148. '/label',
  4149. array('div' => array('class' => 'checkbox')),
  4150. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4151. array('label' => array('for' => 'ModelMultiField0')),
  4152. 'first',
  4153. '/label',
  4154. '/div',
  4155. array('div' => array('class' => 'checkbox')),
  4156. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4157. array('label' => array('for' => 'ModelMultiField1')),
  4158. 'second',
  4159. '/label',
  4160. '/div',
  4161. '/div'
  4162. );
  4163. $this->assertTags($result, $expected);
  4164. }
  4165. /**
  4166. * test that select() with multiple = checkbox works with overriding name attribute.
  4167. *
  4168. * @return void
  4169. */
  4170. public function testSelectCheckboxMultipleOverrideName() {
  4171. $result = $this->Form->input('category', array(
  4172. 'type' => 'select',
  4173. 'multiple' => 'checkbox',
  4174. 'name' => 'data[fish]',
  4175. 'options' => array('1', '2'),
  4176. 'div' => false,
  4177. 'label' => false,
  4178. ));
  4179. $expected = array(
  4180. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4181. array('div' => array('class' => 'checkbox')),
  4182. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4183. array('label' => array('for' => 'Category0')), '1', '/label',
  4184. '/div',
  4185. array('div' => array('class' => 'checkbox')),
  4186. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4187. array('label' => array('for' => 'Category1')), '2', '/label',
  4188. '/div'
  4189. );
  4190. $this->assertTags($result, $expected);
  4191. }
  4192. /**
  4193. * Test that 'id' overrides all the checkbox id's as well.
  4194. *
  4195. * @return void
  4196. */
  4197. public function testSelectCheckboxMultipleId() {
  4198. $result = $this->Form->select(
  4199. 'Model.multi_field',
  4200. array('first', 'second', 'third'),
  4201. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4202. );
  4203. $expected = array(
  4204. 'input' => array(
  4205. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4206. ),
  4207. array('div' => array('class' => 'checkbox')),
  4208. array('input' => array(
  4209. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4210. 'value' => '0', 'id' => 'CustomId0'
  4211. )),
  4212. array('label' => array('for' => 'CustomId0')),
  4213. 'first',
  4214. '/label',
  4215. '/div',
  4216. array('div' => array('class' => 'checkbox')),
  4217. array('input' => array(
  4218. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4219. 'value' => '1', 'id' => 'CustomId1'
  4220. )),
  4221. array('label' => array('for' => 'CustomId1')),
  4222. 'second',
  4223. '/label',
  4224. '/div',
  4225. array('div' => array('class' => 'checkbox')),
  4226. array('input' => array(
  4227. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4228. 'value' => '2', 'id' => 'CustomId2'
  4229. )),
  4230. array('label' => array('for' => 'CustomId2')),
  4231. 'third',
  4232. '/label',
  4233. '/div'
  4234. );
  4235. $this->assertTags($result, $expected);
  4236. }
  4237. /**
  4238. * testCheckbox method
  4239. *
  4240. * Test generation of checkboxes
  4241. *
  4242. * @return void
  4243. */
  4244. public function testCheckbox() {
  4245. $result = $this->Form->checkbox('Model.field');
  4246. $expected = array(
  4247. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4248. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4249. );
  4250. $this->assertTags($result, $expected);
  4251. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4252. $expected = array(
  4253. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4254. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4255. );
  4256. $this->assertTags($result, $expected);
  4257. $Contact = ClassRegistry::getObject('Contact');
  4258. $Contact->validationErrors['field'] = 1;
  4259. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4260. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4261. $expected = array(
  4262. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4263. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4264. );
  4265. $this->assertTags($result, $expected);
  4266. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4267. $expected = array(
  4268. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4269. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4270. );
  4271. $this->assertTags($result, $expected);
  4272. $this->Form->request->data['Contact']['field'] = '';
  4273. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4274. $expected = array(
  4275. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4276. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4277. );
  4278. $this->assertTags($result, $expected);
  4279. $Contact->validationErrors = array();
  4280. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4281. $expected = array(
  4282. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4283. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4284. );
  4285. $this->assertTags($result, $expected);
  4286. $this->Form->request->data['Contact']['published'] = 1;
  4287. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4288. $expected = array(
  4289. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4290. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4291. );
  4292. $this->assertTags($result, $expected);
  4293. $this->Form->request->data['Contact']['published'] = 0;
  4294. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4295. $expected = array(
  4296. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4297. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4298. );
  4299. $this->assertTags($result, $expected);
  4300. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4301. $expected = array(
  4302. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4303. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4304. );
  4305. $this->assertTags($result, $expected);
  4306. $result = $this->Form->checkbox('CustomField.1.value');
  4307. $expected = array(
  4308. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4309. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4310. );
  4311. $this->assertTags($result, $expected);
  4312. }
  4313. /**
  4314. * test checkbox() with a custom name attribute
  4315. *
  4316. * @return void
  4317. */
  4318. public function testCheckboxCustomNameAttribute() {
  4319. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4320. $expected = array(
  4321. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4322. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4323. );
  4324. $this->assertTags($result, $expected);
  4325. }
  4326. /**
  4327. * test the checked option for checkboxes.
  4328. *
  4329. * @return void
  4330. */
  4331. public function testCheckboxCheckedOption() {
  4332. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4333. $expected = array(
  4334. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4335. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4336. );
  4337. $this->assertTags($result, $expected);
  4338. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4339. $expected = array(
  4340. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4341. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4342. );
  4343. $this->assertTags($result, $expected);
  4344. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4345. $expected = array(
  4346. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4347. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4348. );
  4349. $this->assertTags($result, $expected);
  4350. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4351. $expected = array(
  4352. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4353. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4354. );
  4355. $this->assertTags($result, $expected);
  4356. $this->Form->request->data['Model']['field'] = 1;
  4357. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4358. $expected = array(
  4359. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4360. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4361. );
  4362. $this->assertTags($result, $expected);
  4363. }
  4364. /**
  4365. * Test that disabled attribute works on both the checkbox and hidden input.
  4366. *
  4367. * @return void
  4368. */
  4369. public function testCheckboxDisabling() {
  4370. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4371. $expected = array(
  4372. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4373. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4374. );
  4375. $this->assertTags($result, $expected);
  4376. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4377. $expected = array(
  4378. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4379. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4380. );
  4381. $this->assertTags($result, $expected);
  4382. }
  4383. /**
  4384. * Test that the hidden input for checkboxes can be omitted or set to a
  4385. * specific value.
  4386. *
  4387. * @return void
  4388. */
  4389. public function testCheckboxHiddenField() {
  4390. $result = $this->Form->input('UserForm.something', array(
  4391. 'type' => 'checkbox',
  4392. 'hiddenField' => false
  4393. ));
  4394. $expected = array(
  4395. 'div' => array('class' => 'input checkbox'),
  4396. array('input' => array(
  4397. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4398. 'value' => '1', 'id' => 'UserFormSomething'
  4399. )),
  4400. 'label' => array('for' => 'UserFormSomething'),
  4401. 'Something',
  4402. '/label',
  4403. '/div'
  4404. );
  4405. $this->assertTags($result, $expected);
  4406. $result = $this->Form->input('UserForm.something', array(
  4407. 'type' => 'checkbox',
  4408. 'value' => 'Y',
  4409. 'hiddenField' => 'N',
  4410. ));
  4411. $expected = array(
  4412. 'div' => array('class' => 'input checkbox'),
  4413. array('input' => array(
  4414. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  4415. 'value' => 'N', 'id' => 'UserFormSomething_'
  4416. )),
  4417. array('input' => array(
  4418. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4419. 'value' => 'Y', 'id' => 'UserFormSomething'
  4420. )),
  4421. 'label' => array('for' => 'UserFormSomething'),
  4422. 'Something',
  4423. '/label',
  4424. '/div'
  4425. );
  4426. $this->assertTags($result, $expected);
  4427. }
  4428. /**
  4429. * testDateTime method
  4430. *
  4431. * Test generation of date/time select elements
  4432. *
  4433. * @return void
  4434. */
  4435. public function testDateTime() {
  4436. extract($this->dateRegex);
  4437. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4438. $now = strtotime('now');
  4439. $expected = array(
  4440. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4441. $daysRegex,
  4442. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4443. date('j', $now),
  4444. '/option',
  4445. '*/select',
  4446. '-',
  4447. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4448. $monthsRegex,
  4449. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4450. date('F', $now),
  4451. '/option',
  4452. '*/select',
  4453. '-',
  4454. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4455. $yearsRegex,
  4456. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4457. date('Y', $now),
  4458. '/option',
  4459. '*/select',
  4460. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4461. $hoursRegex,
  4462. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4463. date('g', $now),
  4464. '/option',
  4465. '*/select',
  4466. ':',
  4467. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4468. $minutesRegex,
  4469. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4470. date('i', $now),
  4471. '/option',
  4472. '*/select',
  4473. ' ',
  4474. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4475. $meridianRegex,
  4476. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4477. date('a', $now),
  4478. '/option',
  4479. '*/select'
  4480. );
  4481. $this->assertTags($result, $expected);
  4482. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4483. $expected = array(
  4484. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4485. $daysRegex,
  4486. array('option' => array('value' => '')),
  4487. '/option',
  4488. '*/select',
  4489. '-',
  4490. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4491. $monthsRegex,
  4492. array('option' => array('value' => '')),
  4493. '/option',
  4494. '*/select',
  4495. '-',
  4496. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4497. $yearsRegex,
  4498. array('option' => array('value' => '')),
  4499. '/option',
  4500. '*/select',
  4501. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4502. $hoursRegex,
  4503. array('option' => array('value' => '')),
  4504. '/option',
  4505. '*/select',
  4506. ':',
  4507. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4508. $minutesRegex,
  4509. array('option' => array('value' => '')),
  4510. '/option',
  4511. '*/select',
  4512. ' ',
  4513. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4514. $meridianRegex,
  4515. array('option' => array('value' => '')),
  4516. '/option',
  4517. '*/select'
  4518. );
  4519. $this->assertTags($result, $expected);
  4520. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4521. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  4522. $this->assertTags($result, $expected);
  4523. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4524. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  4525. $this->assertTags($result, $expected);
  4526. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4527. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  4528. $expected = array(
  4529. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4530. $daysRegex,
  4531. array('option' => array('value' => '')),
  4532. '/option',
  4533. '*/select',
  4534. '-',
  4535. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4536. $monthsRegex,
  4537. array('option' => array('value' => '')),
  4538. '/option',
  4539. '*/select',
  4540. '-',
  4541. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4542. $yearsRegex,
  4543. array('option' => array('value' => '')),
  4544. '/option',
  4545. '*/select',
  4546. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4547. $hoursRegex,
  4548. array('option' => array('value' => '')),
  4549. '/option',
  4550. '*/select',
  4551. ':',
  4552. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4553. $minutesRegex,
  4554. array('option' => array('value' => '')),
  4555. '/option',
  4556. array('option' => array('value' => '00')),
  4557. '00',
  4558. '/option',
  4559. array('option' => array('value' => '05')),
  4560. '05',
  4561. '/option',
  4562. array('option' => array('value' => '10')),
  4563. '10',
  4564. '/option',
  4565. '*/select',
  4566. ' ',
  4567. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4568. $meridianRegex,
  4569. array('option' => array('value' => '')),
  4570. '/option',
  4571. '*/select'
  4572. );
  4573. $this->assertTags($result, $expected);
  4574. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4575. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4576. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4577. $this->Form->request->data['Contact']['data'] = null;
  4578. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4579. $expected = array(
  4580. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4581. $daysRegex,
  4582. array('option' => array('value' => '')),
  4583. '/option',
  4584. '*/select',
  4585. '-',
  4586. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4587. $monthsRegex,
  4588. array('option' => array('value' => '')),
  4589. '/option',
  4590. '*/select',
  4591. '-',
  4592. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4593. $yearsRegex,
  4594. array('option' => array('value' => '')),
  4595. '/option',
  4596. '*/select',
  4597. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4598. $hoursRegex,
  4599. array('option' => array('value' => '')),
  4600. '/option',
  4601. '*/select',
  4602. ':',
  4603. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4604. $minutesRegex,
  4605. array('option' => array('value' => '')),
  4606. '/option',
  4607. '*/select',
  4608. ' ',
  4609. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4610. $meridianRegex,
  4611. array('option' => array('value' => '')),
  4612. '/option',
  4613. '*/select'
  4614. );
  4615. $this->assertTags($result, $expected);
  4616. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4617. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  4618. $now = strtotime($this->Form->data['Model']['field']);
  4619. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  4620. $expected = array(
  4621. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4622. $daysRegex,
  4623. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4624. date('j', $now),
  4625. '/option',
  4626. '*/select',
  4627. '-',
  4628. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4629. $monthsRegex,
  4630. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4631. date('F', $now),
  4632. '/option',
  4633. '*/select',
  4634. '-',
  4635. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  4636. $yearsRegex,
  4637. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4638. date('Y', $now),
  4639. '/option',
  4640. '*/select',
  4641. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4642. $hoursRegex,
  4643. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4644. date('g', $now),
  4645. '/option',
  4646. '*/select',
  4647. ':',
  4648. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4649. $minutesRegex,
  4650. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4651. date('i', $now),
  4652. '/option',
  4653. '*/select',
  4654. ' ',
  4655. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  4656. $meridianRegex,
  4657. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4658. date('a', $now),
  4659. '/option',
  4660. '*/select'
  4661. );
  4662. $this->assertTags($result, $expected);
  4663. $selected = strtotime('2008-10-26 12:33:00');
  4664. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  4665. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  4666. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  4667. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  4668. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  4669. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  4670. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  4671. $this->Form->create('Contact');
  4672. $result = $this->Form->input('published');
  4673. $now = strtotime('now');
  4674. $expected = array(
  4675. 'div' => array('class' => 'input date'),
  4676. 'label' => array('for' => 'ContactPublishedMonth'),
  4677. 'Published',
  4678. '/label',
  4679. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4680. $monthsRegex,
  4681. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4682. date('F', $now),
  4683. '/option',
  4684. '*/select',
  4685. '-',
  4686. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4687. $daysRegex,
  4688. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4689. date('j', $now),
  4690. '/option',
  4691. '*/select',
  4692. '-',
  4693. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4694. $yearsRegex,
  4695. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4696. date('Y', $now),
  4697. '/option',
  4698. '*/select',
  4699. '/div'
  4700. );
  4701. $this->assertTags($result, $expected);
  4702. $result = $this->Form->input('published2', array('type' => 'date'));
  4703. $now = strtotime('now');
  4704. $expected = array(
  4705. 'div' => array('class' => 'input date'),
  4706. 'label' => array('for' => 'ContactPublished2Month'),
  4707. 'Published2',
  4708. '/label',
  4709. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  4710. $monthsRegex,
  4711. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4712. date('F', $now),
  4713. '/option',
  4714. '*/select',
  4715. '-',
  4716. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  4717. $daysRegex,
  4718. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4719. date('j', $now),
  4720. '/option',
  4721. '*/select',
  4722. '-',
  4723. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  4724. $yearsRegex,
  4725. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4726. date('Y', $now),
  4727. '/option',
  4728. '*/select',
  4729. '/div'
  4730. );
  4731. $this->assertTags($result, $expected);
  4732. $this->Form->create('Contact');
  4733. $result = $this->Form->input('published', array('monthNames' => false));
  4734. $now = strtotime('now');
  4735. $expected = array(
  4736. 'div' => array('class' => 'input date'),
  4737. 'label' => array('for' => 'ContactPublishedMonth'),
  4738. 'Published',
  4739. '/label',
  4740. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4741. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4742. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4743. date('m', $now),
  4744. '/option',
  4745. '*/select',
  4746. '-',
  4747. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4748. $daysRegex,
  4749. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4750. date('j', $now),
  4751. '/option',
  4752. '*/select',
  4753. '-',
  4754. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4755. $yearsRegex,
  4756. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4757. date('Y', $now),
  4758. '/option',
  4759. '*/select',
  4760. '/div'
  4761. );
  4762. $this->assertTags($result, $expected);
  4763. $result = $this->Form->input('published', array('type' => 'time'));
  4764. $now = strtotime('now');
  4765. $expected = array(
  4766. 'div' => array('class' => 'input time'),
  4767. 'label' => array('for' => 'ContactPublishedHour'),
  4768. 'Published',
  4769. '/label',
  4770. array('select' => array('name' => 'data[Contact][published][hour]', 'id' => 'ContactPublishedHour')),
  4771. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4772. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4773. date('g', $now),
  4774. '/option',
  4775. '*/select',
  4776. ':',
  4777. );
  4778. $this->assertTags($result, $expected);
  4779. $result = $this->Form->input('published', array(
  4780. 'timeFormat' => 24,
  4781. 'interval' => 5,
  4782. 'selected' => strtotime('2009-09-03 13:37:00'),
  4783. 'type' => 'datetime'
  4784. ));
  4785. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4786. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  4787. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  4788. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  4789. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  4790. $this->assertNoErrors();
  4791. $this->Form->request->data['Contact'] = array(
  4792. 'date' => array(
  4793. 'day' => '',
  4794. 'month' => '',
  4795. 'year' => '',
  4796. 'hour' => '',
  4797. 'min' => '',
  4798. 'meridian' => ''
  4799. )
  4800. );
  4801. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4802. }
  4803. /**
  4804. * test that datetime() and default values work.
  4805. *
  4806. * @return void
  4807. */
  4808. public function testDatetimeWithDefault() {
  4809. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  4810. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4811. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4812. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4813. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  4814. 'default' => '2009-06-01 11:15:30'
  4815. ));
  4816. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4817. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4818. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4819. }
  4820. /**
  4821. * test that bogus non-date time data doesn't cause errors.
  4822. *
  4823. * @return void
  4824. */
  4825. public function testDateTimeWithBogusData() {
  4826. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  4827. $this->assertNotRegExp('/selected="selected">\d/', $result);
  4828. }
  4829. /**
  4830. * testDateTimeEmptyAsArray
  4831. *
  4832. * @return void
  4833. */
  4834. public function testDateTimeEmptyAsArray() {
  4835. $result = $this->Form->dateTime('Contact.date',
  4836. 'DMY',
  4837. '12',
  4838. array(
  4839. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  4840. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  4841. )
  4842. )
  4843. );
  4844. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4845. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4846. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4847. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4848. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4849. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4850. $result = $this->Form->dateTime('Contact.date',
  4851. 'DMY',
  4852. '12',
  4853. array(
  4854. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4855. )
  4856. );
  4857. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4858. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4859. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4860. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  4861. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  4862. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  4863. }
  4864. /**
  4865. * testFormDateTimeMulti method
  4866. *
  4867. * test multiple datetime element generation
  4868. *
  4869. * @return void
  4870. */
  4871. public function testFormDateTimeMulti() {
  4872. extract($this->dateRegex);
  4873. $result = $this->Form->dateTime('Contact.1.updated');
  4874. $expected = array(
  4875. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  4876. $daysRegex,
  4877. array('option' => array('value' => '')),
  4878. '/option',
  4879. '*/select',
  4880. '-',
  4881. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  4882. $monthsRegex,
  4883. array('option' => array('value' => '')),
  4884. '/option',
  4885. '*/select',
  4886. '-',
  4887. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  4888. $yearsRegex,
  4889. array('option' => array('value' => '')),
  4890. '/option',
  4891. '*/select',
  4892. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  4893. $hoursRegex,
  4894. array('option' => array('value' => '')),
  4895. '/option',
  4896. '*/select',
  4897. ':',
  4898. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  4899. $minutesRegex,
  4900. array('option' => array('value' => '')),
  4901. '/option',
  4902. '*/select',
  4903. ' ',
  4904. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  4905. $meridianRegex,
  4906. array('option' => array('value' => '')),
  4907. '/option',
  4908. '*/select'
  4909. );
  4910. $this->assertTags($result, $expected);
  4911. $result = $this->Form->dateTime('Contact.2.updated');
  4912. $expected = array(
  4913. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  4914. $daysRegex,
  4915. array('option' => array('value' => '')),
  4916. '/option',
  4917. '*/select',
  4918. '-',
  4919. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  4920. $monthsRegex,
  4921. array('option' => array('value' => '')),
  4922. '/option',
  4923. '*/select',
  4924. '-',
  4925. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  4926. $yearsRegex,
  4927. array('option' => array('value' => '')),
  4928. '/option',
  4929. '*/select',
  4930. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  4931. $hoursRegex,
  4932. array('option' => array('value' => '')),
  4933. '/option',
  4934. '*/select',
  4935. ':',
  4936. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  4937. $minutesRegex,
  4938. array('option' => array('value' => '')),
  4939. '/option',
  4940. '*/select',
  4941. ' ',
  4942. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  4943. $meridianRegex,
  4944. array('option' => array('value' => '')),
  4945. '/option',
  4946. '*/select'
  4947. );
  4948. $this->assertTags($result, $expected);
  4949. }
  4950. /**
  4951. * When changing the date format, the label should always focus the first select box when
  4952. * clicked.
  4953. *
  4954. * @return void
  4955. */
  4956. public function testDateTimeLabelIdMatchesFirstInput() {
  4957. $result = $this->Form->input('Model.date', array('type' => 'date'));
  4958. $this->assertContains('label for="ModelDateMonth"', $result);
  4959. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  4960. $this->assertContains('label for="ModelDateDay"', $result);
  4961. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  4962. $this->assertContains('label for="ModelDateYear"', $result);
  4963. }
  4964. /**
  4965. * testMonth method
  4966. *
  4967. * @return void
  4968. */
  4969. public function testMonth() {
  4970. $result = $this->Form->month('Model.field');
  4971. $expected = array(
  4972. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4973. array('option' => array('value' => '')),
  4974. '/option',
  4975. array('option' => array('value' => '01')),
  4976. date('F', strtotime('2008-01-01 00:00:00')),
  4977. '/option',
  4978. array('option' => array('value' => '02')),
  4979. date('F', strtotime('2008-02-01 00:00:00')),
  4980. '/option',
  4981. '*/select',
  4982. );
  4983. $this->assertTags($result, $expected);
  4984. $result = $this->Form->month('Model.field', array('empty' => true));
  4985. $expected = array(
  4986. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4987. array('option' => array('value' => '')),
  4988. '/option',
  4989. array('option' => array('value' => '01')),
  4990. date('F', strtotime('2008-01-01 00:00:00')),
  4991. '/option',
  4992. array('option' => array('value' => '02')),
  4993. date('F', strtotime('2008-02-01 00:00:00')),
  4994. '/option',
  4995. '*/select',
  4996. );
  4997. $this->assertTags($result, $expected);
  4998. $result = $this->Form->month('Model.field', array('monthNames' => false));
  4999. $expected = array(
  5000. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5001. array('option' => array('value' => '')),
  5002. '/option',
  5003. array('option' => array('value' => '01')),
  5004. '01',
  5005. '/option',
  5006. array('option' => array('value' => '02')),
  5007. '02',
  5008. '/option',
  5009. '*/select',
  5010. );
  5011. $this->assertTags($result, $expected);
  5012. $monthNames = array(
  5013. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5014. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5015. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5016. $expected = array(
  5017. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5018. array('option' => array('value' => '')),
  5019. '/option',
  5020. array('option' => array('value' => '01')),
  5021. 'Jan',
  5022. '/option',
  5023. array('option' => array('value' => '02')),
  5024. 'Feb',
  5025. '/option',
  5026. '*/select',
  5027. );
  5028. $this->assertTags($result, $expected);
  5029. }
  5030. /**
  5031. * testDay method
  5032. *
  5033. * @return void
  5034. */
  5035. public function testDay() {
  5036. extract($this->dateRegex);
  5037. $result = $this->Form->day('Model.field', array('value' => false));
  5038. $expected = array(
  5039. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5040. array('option' => array('value' => '')),
  5041. '/option',
  5042. array('option' => array('value' => '01')),
  5043. '1',
  5044. '/option',
  5045. array('option' => array('value' => '02')),
  5046. '2',
  5047. '/option',
  5048. $daysRegex,
  5049. '/select',
  5050. );
  5051. $this->assertTags($result, $expected);
  5052. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5053. $result = $this->Form->day('Model.field');
  5054. $expected = array(
  5055. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5056. array('option' => array('value' => '')),
  5057. '/option',
  5058. array('option' => array('value' => '01')),
  5059. '1',
  5060. '/option',
  5061. array('option' => array('value' => '02')),
  5062. '2',
  5063. '/option',
  5064. $daysRegex,
  5065. array('option' => array('value' => '10', 'selected' => 'selected')),
  5066. '10',
  5067. '/option',
  5068. $daysRegex,
  5069. '/select',
  5070. );
  5071. $this->assertTags($result, $expected);
  5072. $this->Form->request->data['Model']['field'] = '';
  5073. $result = $this->Form->day('Model.field', array('value' => '10'));
  5074. $expected = array(
  5075. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5076. array('option' => array('value' => '')),
  5077. '/option',
  5078. array('option' => array('value' => '01')),
  5079. '1',
  5080. '/option',
  5081. array('option' => array('value' => '02')),
  5082. '2',
  5083. '/option',
  5084. $daysRegex,
  5085. array('option' => array('value' => '10', 'selected' => 'selected')),
  5086. '10',
  5087. '/option',
  5088. $daysRegex,
  5089. '/select',
  5090. );
  5091. $this->assertTags($result, $expected);
  5092. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5093. $result = $this->Form->day('Model.field', array('value' => true));
  5094. $expected = array(
  5095. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5096. array('option' => array('value' => '')),
  5097. '/option',
  5098. array('option' => array('value' => '01')),
  5099. '1',
  5100. '/option',
  5101. array('option' => array('value' => '02')),
  5102. '2',
  5103. '/option',
  5104. $daysRegex,
  5105. array('option' => array('value' => '10', 'selected' => 'selected')),
  5106. '10',
  5107. '/option',
  5108. $daysRegex,
  5109. '/select',
  5110. );
  5111. $this->assertTags($result, $expected);
  5112. }
  5113. /**
  5114. * testMinute method
  5115. *
  5116. * @return void
  5117. */
  5118. public function testMinute() {
  5119. extract($this->dateRegex);
  5120. $result = $this->Form->minute('Model.field');
  5121. $expected = array(
  5122. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5123. array('option' => array('value' => '')),
  5124. '/option',
  5125. array('option' => array('value' => '00')),
  5126. '00',
  5127. '/option',
  5128. array('option' => array('value' => '01')),
  5129. '01',
  5130. '/option',
  5131. array('option' => array('value' => '02')),
  5132. '02',
  5133. '/option',
  5134. $minutesRegex,
  5135. '/select',
  5136. );
  5137. $this->assertTags($result, $expected);
  5138. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5139. $result = $this->Form->minute('Model.field');
  5140. $expected = array(
  5141. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5142. array('option' => array('value' => '')),
  5143. '/option',
  5144. array('option' => array('value' => '00')),
  5145. '00',
  5146. '/option',
  5147. array('option' => array('value' => '01')),
  5148. '01',
  5149. '/option',
  5150. array('option' => array('value' => '02')),
  5151. '02',
  5152. '/option',
  5153. $minutesRegex,
  5154. array('option' => array('value' => '12', 'selected' => 'selected')),
  5155. '12',
  5156. '/option',
  5157. $minutesRegex,
  5158. '/select',
  5159. );
  5160. $this->assertTags($result, $expected);
  5161. $this->Form->request->data['Model']['field'] = '';
  5162. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5163. $expected = array(
  5164. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5165. array('option' => array('value' => '')),
  5166. '/option',
  5167. array('option' => array('value' => '00')),
  5168. '00',
  5169. '/option',
  5170. array('option' => array('value' => '05')),
  5171. '05',
  5172. '/option',
  5173. array('option' => array('value' => '10')),
  5174. '10',
  5175. '/option',
  5176. $minutesRegex,
  5177. '/select',
  5178. );
  5179. $this->assertTags($result, $expected);
  5180. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5181. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5182. $expected = array(
  5183. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5184. array('option' => array('value' => '')),
  5185. '/option',
  5186. array('option' => array('value' => '00')),
  5187. '00',
  5188. '/option',
  5189. array('option' => array('value' => '05')),
  5190. '05',
  5191. '/option',
  5192. array('option' => array('value' => '10', 'selected' => 'selected')),
  5193. '10',
  5194. '/option',
  5195. $minutesRegex,
  5196. '/select',
  5197. );
  5198. $this->assertTags($result, $expected);
  5199. }
  5200. /**
  5201. * testHour method
  5202. *
  5203. * @return void
  5204. */
  5205. public function testHour() {
  5206. extract($this->dateRegex);
  5207. $result = $this->Form->hour('Model.field', false);
  5208. $expected = array(
  5209. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5210. array('option' => array('value' => '')),
  5211. '/option',
  5212. array('option' => array('value' => '01')),
  5213. '1',
  5214. '/option',
  5215. array('option' => array('value' => '02')),
  5216. '2',
  5217. '/option',
  5218. $hoursRegex,
  5219. '/select',
  5220. );
  5221. $this->assertTags($result, $expected);
  5222. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5223. $result = $this->Form->hour('Model.field', false);
  5224. $expected = array(
  5225. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5226. array('option' => array('value' => '')),
  5227. '/option',
  5228. array('option' => array('value' => '01')),
  5229. '1',
  5230. '/option',
  5231. array('option' => array('value' => '02')),
  5232. '2',
  5233. '/option',
  5234. $hoursRegex,
  5235. array('option' => array('value' => '12', 'selected' => 'selected')),
  5236. '12',
  5237. '/option',
  5238. '/select',
  5239. );
  5240. $this->assertTags($result, $expected);
  5241. $this->Form->request->data['Model']['field'] = '';
  5242. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5243. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5244. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  5245. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5246. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5247. $result = $this->Form->hour('Model.field', true);
  5248. $expected = array(
  5249. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5250. array('option' => array('value' => '')),
  5251. '/option',
  5252. array('option' => array('value' => '00', 'selected' => 'selected')),
  5253. '0',
  5254. '/option',
  5255. array('option' => array('value' => '01')),
  5256. '1',
  5257. '/option',
  5258. array('option' => array('value' => '02')),
  5259. '2',
  5260. '/option',
  5261. $hoursRegex,
  5262. '/select',
  5263. );
  5264. $this->assertTags($result, $expected);
  5265. unset($this->Form->request->data['Model']['field']);
  5266. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5267. $thisHour = date('H');
  5268. $optValue = date('G');
  5269. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5270. }
  5271. /**
  5272. * testYear method
  5273. *
  5274. * @return void
  5275. */
  5276. public function testYear() {
  5277. $result = $this->Form->year('Model.field', 2006, 2007);
  5278. $expected = array(
  5279. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5280. array('option' => array('value' => '')),
  5281. '/option',
  5282. array('option' => array('value' => '2007')),
  5283. '2007',
  5284. '/option',
  5285. array('option' => array('value' => '2006')),
  5286. '2006',
  5287. '/option',
  5288. '/select',
  5289. );
  5290. $this->assertTags($result, $expected);
  5291. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5292. $expected = array(
  5293. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5294. array('option' => array('value' => '')),
  5295. '/option',
  5296. array('option' => array('value' => '2006')),
  5297. '2006',
  5298. '/option',
  5299. array('option' => array('value' => '2007')),
  5300. '2007',
  5301. '/option',
  5302. '/select',
  5303. );
  5304. $this->assertTags($result, $expected);
  5305. $this->request->data['Contact']['published'] = '';
  5306. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5307. $expected = array(
  5308. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5309. array('option' => array('value' => '')),
  5310. '/option',
  5311. array('option' => array('value' => '2007')),
  5312. '2007',
  5313. '/option',
  5314. array('option' => array('value' => '2006')),
  5315. '2006',
  5316. '/option',
  5317. '/select',
  5318. );
  5319. $this->assertTags($result, $expected);
  5320. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5321. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5322. $expected = array(
  5323. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5324. array('option' => array('value' => '2007')),
  5325. '2007',
  5326. '/option',
  5327. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5328. '2006',
  5329. '/option',
  5330. '/select',
  5331. );
  5332. $this->assertTags($result, $expected);
  5333. $this->Form->request->data['Contact']['published'] = '';
  5334. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5335. $expected = array(
  5336. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5337. array('option' => array('value' => '')),
  5338. '/option',
  5339. array('option' => array('value' => '2007')),
  5340. '2007',
  5341. '/option',
  5342. array('option' => array('value' => '2006')),
  5343. '2006',
  5344. '/option',
  5345. '/select',
  5346. );
  5347. $this->assertTags($result, $expected);
  5348. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5349. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5350. $expected = array(
  5351. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5352. array('option' => array('value' => '2007')),
  5353. '2007',
  5354. '/option',
  5355. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5356. '2006',
  5357. '/option',
  5358. '/select',
  5359. );
  5360. $this->assertTags($result, $expected);
  5361. $this->Form->request->data['Contact']['published'] = '';
  5362. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5363. $expected = array(
  5364. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5365. array('option' => array('value' => '')),
  5366. '/option',
  5367. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5368. '2007',
  5369. '/option',
  5370. array('option' => array('value' => '2006')),
  5371. '2006',
  5372. '/option',
  5373. '/select',
  5374. );
  5375. $this->assertTags($result, $expected);
  5376. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5377. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5378. $expected = array(
  5379. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5380. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5381. '2007',
  5382. '/option',
  5383. array('option' => array('value' => '2006')),
  5384. '2006',
  5385. '/option',
  5386. '/select',
  5387. );
  5388. $this->assertTags($result, $expected);
  5389. $this->Form->request->data['Contact']['published'] = '';
  5390. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5391. $expected = array(
  5392. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5393. array('option' => array('value' => '2008')),
  5394. '2008',
  5395. '/option',
  5396. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5397. '2007',
  5398. '/option',
  5399. array('option' => array('value' => '2006')),
  5400. '2006',
  5401. '/option',
  5402. '/select',
  5403. );
  5404. $this->assertTags($result, $expected);
  5405. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5406. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5407. $expected = array(
  5408. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5409. array('option' => array('value' => '2008')),
  5410. '2008',
  5411. '/option',
  5412. array('option' => array('value' => '2007')),
  5413. '2007',
  5414. '/option',
  5415. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5416. '2006',
  5417. '/option',
  5418. '/select',
  5419. );
  5420. $this->assertTags($result, $expected);
  5421. $this->Form->request->data = array();
  5422. $this->Form->create('Contact');
  5423. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5424. $expected = array(
  5425. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5426. array('option' => array('value' => '2008')),
  5427. '2008',
  5428. '/option',
  5429. array('option' => array('value' => '2007')),
  5430. '2007',
  5431. '/option',
  5432. array('option' => array('value' => '2006')),
  5433. '2006',
  5434. '/option',
  5435. '/select',
  5436. );
  5437. $this->assertTags($result, $expected);
  5438. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  5439. $this->assertContains('data[Contact][published][year]', $result);
  5440. }
  5441. /**
  5442. * testTextArea method
  5443. *
  5444. * @return void
  5445. */
  5446. public function testTextArea() {
  5447. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  5448. $result = $this->Form->textarea('Model.field');
  5449. $expected = array(
  5450. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5451. 'some test data',
  5452. '/textarea',
  5453. );
  5454. $this->assertTags($result, $expected);
  5455. $result = $this->Form->textarea('Model.tmp');
  5456. $expected = array(
  5457. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  5458. '/textarea',
  5459. );
  5460. $this->assertTags($result, $expected);
  5461. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5462. $result = $this->Form->textarea('Model.field');
  5463. $expected = array(
  5464. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5465. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5466. '/textarea',
  5467. );
  5468. $this->assertTags($result, $expected);
  5469. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5470. $result = $this->Form->textarea('Model.field', array('escape' => false));
  5471. $expected = array(
  5472. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5473. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5474. '/textarea',
  5475. );
  5476. $this->assertTags($result, $expected);
  5477. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  5478. $result = $this->Form->textarea('Model.0.OtherModel.field');
  5479. $expected = array(
  5480. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  5481. '/textarea'
  5482. );
  5483. $this->assertTags($result, $expected);
  5484. }
  5485. /**
  5486. * testTextAreaWithStupidCharacters method
  5487. *
  5488. * test text area with non-ascii characters
  5489. *
  5490. * @return void
  5491. */
  5492. public function testTextAreaWithStupidCharacters() {
  5493. $this->loadFixtures('Post');
  5494. $result = $this->Form->input('Post.content', array(
  5495. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  5496. ));
  5497. $expected = array(
  5498. 'div' => array('class' => 'input text'),
  5499. 'label' => array('for' => 'PostContent'),
  5500. 'Current Text',
  5501. '/label',
  5502. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  5503. 'GREAT®',
  5504. '/textarea',
  5505. '/div'
  5506. );
  5507. $this->assertTags($result, $expected);
  5508. }
  5509. /**
  5510. * testHiddenField method
  5511. *
  5512. * @return void
  5513. */
  5514. public function testHiddenField() {
  5515. $Contact = ClassRegistry::getObject('Contact');
  5516. $Contact->validationErrors['field'] = 1;
  5517. $this->Form->request->data['Contact']['field'] = 'test';
  5518. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  5519. $this->assertTags($result, array(
  5520. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  5521. );
  5522. }
  5523. /**
  5524. * testFileUploadField method
  5525. *
  5526. * @return void
  5527. */
  5528. public function testFileUploadField() {
  5529. $result = $this->Form->file('Model.upload');
  5530. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5531. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  5532. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  5533. $expected = array(
  5534. 'div' => array('class' => 'input file'),
  5535. 'label' => array('for' => 'ModelUpload'),
  5536. 'Upload',
  5537. '/label',
  5538. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  5539. '/div'
  5540. );
  5541. $this->assertTags($result, $expected);
  5542. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  5543. $result = $this->Form->file('Model.upload');
  5544. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5545. }
  5546. /**
  5547. * test File upload input on a model not used in create();
  5548. *
  5549. * @return void
  5550. */
  5551. public function testFileUploadOnOtherModel() {
  5552. $this->Form->create('ValidateUser', array('type' => 'file'));
  5553. $result = $this->Form->file('ValidateProfile.city');
  5554. $expected = array(
  5555. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  5556. );
  5557. $this->assertTags($result, $expected);
  5558. }
  5559. /**
  5560. * testButton method
  5561. *
  5562. * @return void
  5563. */
  5564. public function testButton() {
  5565. $result = $this->Form->button('Hi');
  5566. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5567. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5568. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5569. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5570. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5571. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5572. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5573. $result = $this->Form->button('No type', array('type' => false));
  5574. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5575. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  5576. $this->assertNotRegExp('/\&039/', $result);
  5577. }
  5578. /**
  5579. * Test that button() makes unlocked fields by default.
  5580. *
  5581. * @return void
  5582. */
  5583. public function testButtonUnlockedByDefault() {
  5584. $this->Form->request->params['_Token']['key'] = 'secured';
  5585. $this->Form->button('Save', array('name' => 'save'));
  5586. $this->Form->button('Clear');
  5587. $result = $this->Form->unlockField();
  5588. $this->assertEquals(array('save'), $result);
  5589. }
  5590. /**
  5591. * testPostButton method
  5592. *
  5593. * @return void
  5594. */
  5595. public function testPostButton() {
  5596. $result = $this->Form->postButton('Hi', '/controller/action');
  5597. $this->assertTags($result, array(
  5598. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5599. 'div' => array('style' => 'display:none;'),
  5600. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5601. '/div',
  5602. 'button' => array('type' => 'submit'),
  5603. 'Hi',
  5604. '/button',
  5605. '/form'
  5606. ));
  5607. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5608. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  5609. }
  5610. /**
  5611. * Test that postButton adds _Token fields.
  5612. *
  5613. * @return void
  5614. */
  5615. public function testSecurePostButton() {
  5616. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5617. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5618. $expected = array(
  5619. 'form' => array(
  5620. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5621. ),
  5622. array('div' => array('style' => 'display:none;')),
  5623. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5624. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5625. '/div',
  5626. 'button' => array('type' => 'submit'),
  5627. 'Delete',
  5628. '/button',
  5629. array('div' => array('style' => 'display:none;')),
  5630. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5631. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5632. '/div',
  5633. '/form',
  5634. );
  5635. $this->assertTags($result, $expected);
  5636. }
  5637. /**
  5638. * testPostLink method
  5639. *
  5640. * @return void
  5641. */
  5642. public function testPostLink() {
  5643. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5644. $this->assertTags($result, array(
  5645. 'form' => array(
  5646. 'method' => 'post', 'action' => '/posts/delete/1',
  5647. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5648. ),
  5649. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5650. '/form',
  5651. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5652. 'Delete',
  5653. '/a'
  5654. ));
  5655. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5656. $this->assertTags($result, array(
  5657. 'form' => array(
  5658. 'method' => 'post', 'action' => '/posts/delete/1',
  5659. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5660. ),
  5661. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5662. '/form',
  5663. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5664. 'Delete',
  5665. '/a'
  5666. ));
  5667. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5668. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  5669. }
  5670. /**
  5671. * Test that postLink adds _Token fields.
  5672. *
  5673. * @return void
  5674. */
  5675. public function testSecurePostLink() {
  5676. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5677. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5678. $expected = array(
  5679. 'form' => array(
  5680. 'method' => 'post', 'action' => '/posts/delete/1',
  5681. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5682. ),
  5683. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5684. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5685. 'div' => array('style' => 'display:none;'),
  5686. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5687. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5688. '/div',
  5689. '/form',
  5690. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5691. 'Delete',
  5692. '/a'
  5693. );
  5694. $this->assertTags($result, $expected);
  5695. }
  5696. /**
  5697. * testSubmitButton method
  5698. *
  5699. * @return void
  5700. */
  5701. public function testSubmitButton() {
  5702. $result = $this->Form->submit('');
  5703. $expected = array(
  5704. 'div' => array('class' => 'submit'),
  5705. 'input' => array('type' => 'submit', 'value' => ''),
  5706. '/div'
  5707. );
  5708. $this->assertTags($result, $expected);
  5709. $result = $this->Form->submit('Test Submit');
  5710. $expected = array(
  5711. 'div' => array('class' => 'submit'),
  5712. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5713. '/div'
  5714. );
  5715. $this->assertTags($result, $expected);
  5716. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5717. $expected = array(
  5718. 'span' => array('class' => 'submit'),
  5719. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5720. '/span'
  5721. );
  5722. $this->assertTags($result, $expected);
  5723. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5724. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5725. $this->assertTags($result, $expected);
  5726. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5727. $expected = array(
  5728. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5729. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5730. '/div'
  5731. );
  5732. $this->assertTags($result, $expected);
  5733. $result = $this->Form->submit('Next >');
  5734. $expected = array(
  5735. 'div' => array('class' => 'submit'),
  5736. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5737. '/div'
  5738. );
  5739. $this->assertTags($result, $expected);
  5740. $result = $this->Form->submit('Next >', array('escape' => false));
  5741. $expected = array(
  5742. 'div' => array('class' => 'submit'),
  5743. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5744. '/div'
  5745. );
  5746. $this->assertTags($result, $expected);
  5747. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5748. $expected = array(
  5749. 'div' => array('class' => 'submit'),
  5750. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5751. '/div'
  5752. );
  5753. $this->assertTags($result, $expected);
  5754. $before = '--before--';
  5755. $after = '--after--';
  5756. $result = $this->Form->submit('Test', array('before' => $before));
  5757. $expected = array(
  5758. 'div' => array('class' => 'submit'),
  5759. '--before--',
  5760. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5761. '/div'
  5762. );
  5763. $this->assertTags($result, $expected);
  5764. $result = $this->Form->submit('Test', array('after' => $after));
  5765. $expected = array(
  5766. 'div' => array('class' => 'submit'),
  5767. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5768. '--after--',
  5769. '/div'
  5770. );
  5771. $this->assertTags($result, $expected);
  5772. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5773. $expected = array(
  5774. 'div' => array('class' => 'submit'),
  5775. '--before--',
  5776. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5777. '--after--',
  5778. '/div'
  5779. );
  5780. $this->assertTags($result, $expected);
  5781. }
  5782. /**
  5783. * test image submit types.
  5784. *
  5785. * @return void
  5786. */
  5787. public function testSubmitImage() {
  5788. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5789. $expected = array(
  5790. 'div' => array('class' => 'submit'),
  5791. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5792. '/div'
  5793. );
  5794. $this->assertTags($result, $expected);
  5795. $result = $this->Form->submit('/relative/cake.power.gif');
  5796. $expected = array(
  5797. 'div' => array('class' => 'submit'),
  5798. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5799. '/div'
  5800. );
  5801. $this->assertTags($result, $expected);
  5802. $result = $this->Form->submit('cake.power.gif');
  5803. $expected = array(
  5804. 'div' => array('class' => 'submit'),
  5805. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5806. '/div'
  5807. );
  5808. $this->assertTags($result, $expected);
  5809. $result = $this->Form->submit('Not.an.image');
  5810. $expected = array(
  5811. 'div' => array('class' => 'submit'),
  5812. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5813. '/div'
  5814. );
  5815. $this->assertTags($result, $expected);
  5816. $after = '--after--';
  5817. $before = '--before--';
  5818. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5819. $expected = array(
  5820. 'div' => array('class' => 'submit'),
  5821. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5822. '--after--',
  5823. '/div'
  5824. );
  5825. $this->assertTags($result, $expected);
  5826. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5827. $expected = array(
  5828. 'div' => array('class' => 'submit'),
  5829. '--before--',
  5830. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5831. '/div'
  5832. );
  5833. $this->assertTags($result, $expected);
  5834. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5835. $expected = array(
  5836. 'div' => array('class' => 'submit'),
  5837. '--before--',
  5838. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5839. '--after--',
  5840. '/div'
  5841. );
  5842. $this->assertTags($result, $expected);
  5843. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  5844. $expected = array(
  5845. 'div' => array('class' => 'submit'),
  5846. '--before--',
  5847. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5848. '--after--',
  5849. '/div'
  5850. );
  5851. $this->assertTags($result, $expected);
  5852. }
  5853. /**
  5854. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5855. * be submitted at a time.
  5856. *
  5857. * @return void
  5858. */
  5859. public function testSubmitUnlockedByDefault() {
  5860. $this->Form->request->params['_Token']['key'] = 'secured';
  5861. $this->Form->submit('Go go');
  5862. $this->Form->submit('Save', array('name' => 'save'));
  5863. $result = $this->Form->unlockField();
  5864. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5865. }
  5866. /**
  5867. * Test submit image with timestamps.
  5868. *
  5869. * @return void
  5870. */
  5871. public function testSubmitImageTimestamp() {
  5872. Configure::write('Asset.timestamp', 'force');
  5873. $result = $this->Form->submit('cake.power.gif');
  5874. $expected = array(
  5875. 'div' => array('class' => 'submit'),
  5876. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  5877. '/div'
  5878. );
  5879. $this->assertTags($result, $expected);
  5880. }
  5881. /**
  5882. * test the create() method
  5883. *
  5884. * @return void
  5885. */
  5886. public function testCreate() {
  5887. $result = $this->Form->create('Contact');
  5888. $encoding = strtolower(Configure::read('App.encoding'));
  5889. $expected = array(
  5890. 'form' => array(
  5891. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5892. 'accept-charset' => $encoding
  5893. ),
  5894. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  5895. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5896. '/div'
  5897. );
  5898. $this->assertTags($result, $expected);
  5899. $result = $this->Form->create('Contact', array('type' => 'GET'));
  5900. $expected = array('form' => array(
  5901. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5902. 'accept-charset' => $encoding
  5903. ));
  5904. $this->assertTags($result, $expected);
  5905. $result = $this->Form->create('Contact', array('type' => 'get'));
  5906. $expected = array('form' => array(
  5907. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5908. 'accept-charset' => $encoding
  5909. ));
  5910. $this->assertTags($result, $expected);
  5911. $result = $this->Form->create('Contact', array('type' => 'put'));
  5912. $expected = array(
  5913. 'form' => array(
  5914. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5915. 'accept-charset' => $encoding
  5916. ),
  5917. 'div' => array('style' => 'display:none;'),
  5918. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5919. '/div'
  5920. );
  5921. $this->assertTags($result, $expected);
  5922. $result = $this->Form->create('Contact', array('type' => 'file'));
  5923. $expected = array(
  5924. 'form' => array(
  5925. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5926. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5927. ),
  5928. 'div' => array('style' => 'display:none;'),
  5929. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5930. '/div'
  5931. );
  5932. $this->assertTags($result, $expected);
  5933. $this->Form->request->data['Contact']['id'] = 1;
  5934. $this->Form->request->here = '/contacts/edit/1';
  5935. $this->Form->request['action'] = 'edit';
  5936. $result = $this->Form->create('Contact');
  5937. $expected = array(
  5938. 'form' => array(
  5939. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5940. 'accept-charset' => $encoding
  5941. ),
  5942. 'div' => array('style' => 'display:none;'),
  5943. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5944. '/div'
  5945. );
  5946. $this->assertTags($result, $expected);
  5947. $this->Form->request->data['Contact']['id'] = 1;
  5948. $this->Form->request->here = '/contacts/edit/1';
  5949. $this->Form->request['action'] = 'edit';
  5950. $result = $this->Form->create('Contact', array('type' => 'file'));
  5951. $expected = array(
  5952. 'form' => array(
  5953. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5954. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5955. ),
  5956. 'div' => array('style' => 'display:none;'),
  5957. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5958. '/div'
  5959. );
  5960. $this->assertTags($result, $expected);
  5961. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  5962. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  5963. $expected = array(
  5964. 'form' => array(
  5965. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  5966. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  5967. ),
  5968. 'div' => array('style' => 'display:none;'),
  5969. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5970. '/div'
  5971. );
  5972. $this->assertTags($result, $expected);
  5973. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  5974. $expected = array(
  5975. 'form' => array(
  5976. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  5977. 'accept-charset' => $encoding
  5978. ),
  5979. 'div' => array('style' => 'display:none;'),
  5980. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5981. '/div'
  5982. );
  5983. $this->assertTags($result, $expected);
  5984. $this->Form->request['action'] = 'add';
  5985. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  5986. $expected = array(
  5987. 'form' => array(
  5988. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  5989. 'accept-charset' => $encoding
  5990. ),
  5991. 'div' => array('style' => 'display:none;'),
  5992. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5993. '/div'
  5994. );
  5995. $this->assertTags($result, $expected);
  5996. $result = $this->Form->create('User', array('action' => 'login'));
  5997. $expected = array(
  5998. 'form' => array(
  5999. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  6000. 'accept-charset' => $encoding
  6001. ),
  6002. 'div' => array('style' => 'display:none;'),
  6003. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6004. '/div'
  6005. );
  6006. $this->assertTags($result, $expected);
  6007. $result = $this->Form->create('User', array('url' => '/users/login'));
  6008. $expected = array(
  6009. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  6010. 'div' => array('style' => 'display:none;'),
  6011. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6012. '/div'
  6013. );
  6014. $this->assertTags($result, $expected);
  6015. $this->Form->request['controller'] = 'pages';
  6016. $result = $this->Form->create('User', array('action' => 'signup'));
  6017. $expected = array(
  6018. 'form' => array(
  6019. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  6020. 'accept-charset' => $encoding
  6021. ),
  6022. 'div' => array('style' => 'display:none;'),
  6023. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6024. '/div'
  6025. );
  6026. $this->assertTags($result, $expected);
  6027. $this->Form->request->data = array();
  6028. $this->Form->request['controller'] = 'contacts';
  6029. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6030. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  6031. $expected = array(
  6032. 'form' => array(
  6033. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  6034. 'accept-charset' => 'utf-8'
  6035. ),
  6036. 'div' => array('style' => 'display:none;'),
  6037. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6038. '/div'
  6039. );
  6040. $this->assertTags($result, $expected);
  6041. }
  6042. /**
  6043. * Test the onsubmit option for create()
  6044. *
  6045. * @return void
  6046. */
  6047. public function testCreateOnSubmit() {
  6048. $this->Form->request->data = array();
  6049. $this->Form->request['controller'] = 'contacts';
  6050. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6051. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  6052. $expected = array(
  6053. 'form' => array(
  6054. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  6055. 'accept-charset' => 'utf-8'
  6056. ),
  6057. 'div' => array('style' => 'display:none;'),
  6058. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6059. '/div'
  6060. );
  6061. $this->assertTags($result, $expected);
  6062. $this->Form->request->data = array();
  6063. $this->Form->request['controller'] = 'contacts';
  6064. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6065. $result = $this->Form->create(array(
  6066. 'url' => array('action' => 'index', 'param'),
  6067. 'default' => false,
  6068. 'onsubmit' => 'someFunction();'
  6069. ));
  6070. $expected = array(
  6071. 'form' => array(
  6072. 'id' => 'ContactAddForm', 'method' => 'post',
  6073. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6074. 'action' => '/contacts/index/param',
  6075. 'accept-charset' => 'utf-8'
  6076. ),
  6077. 'div' => array('style' => 'display:none;'),
  6078. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6079. '/div'
  6080. );
  6081. $this->assertTags($result, $expected);
  6082. }
  6083. /**
  6084. * test create() with automatic url generation
  6085. *
  6086. * @return void
  6087. */
  6088. public function testCreateAutoUrl() {
  6089. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6090. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6091. $this->Form->request->base = '/base_url';
  6092. $result = $this->Form->create('Contact');
  6093. $expected = array(
  6094. 'form' => array(
  6095. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6096. 'accept-charset' => 'utf-8'
  6097. ),
  6098. 'div' => array('style' => 'display:none;'),
  6099. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6100. '/div'
  6101. );
  6102. $this->assertTags($result, $expected);
  6103. $this->Form->request['action'] = 'delete';
  6104. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6105. $this->Form->request->base = '/base_url';
  6106. $result = $this->Form->create('Contact');
  6107. $expected = array(
  6108. 'form' => array(
  6109. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6110. 'accept-charset' => 'utf-8'
  6111. ),
  6112. 'div' => array('style' => 'display:none;'),
  6113. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6114. '/div'
  6115. );
  6116. $this->assertTags($result, $expected);
  6117. }
  6118. /**
  6119. * test create() with a custom route
  6120. *
  6121. * @return void
  6122. */
  6123. public function testCreateCustomRoute() {
  6124. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6125. $encoding = strtolower(Configure::read('App.encoding'));
  6126. $result = $this->Form->create('User', array('action' => 'login'));
  6127. $expected = array(
  6128. 'form' => array(
  6129. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6130. 'accept-charset' => $encoding
  6131. ),
  6132. 'div' => array('style' => 'display:none;'),
  6133. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6134. '/div'
  6135. );
  6136. $this->assertTags($result, $expected);
  6137. }
  6138. /**
  6139. * test that inputDefaults are stored and used.
  6140. *
  6141. * @return void
  6142. */
  6143. public function testCreateWithInputDefaults() {
  6144. $this->Form->create('User', array(
  6145. 'inputDefaults' => array(
  6146. 'div' => false,
  6147. 'label' => false,
  6148. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6149. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6150. )
  6151. ));
  6152. $result = $this->Form->input('username');
  6153. $expected = array(
  6154. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6155. );
  6156. $this->assertTags($result, $expected);
  6157. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6158. $expected = array(
  6159. 'div' => array('class' => 'input text'),
  6160. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6161. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6162. '/div'
  6163. );
  6164. $this->assertTags($result, $expected);
  6165. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  6166. $expected = array(
  6167. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6168. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  6169. );
  6170. $this->assertTags($result, $expected);
  6171. $this->Form->create('User', array(
  6172. 'inputDefaults' => array(
  6173. 'div' => false,
  6174. 'label' => array('class' => 'nice', 'for' => 'changed'),
  6175. )
  6176. ));
  6177. $result = $this->Form->input('username', array('div' => true));
  6178. $expected = array(
  6179. 'div' => array('class' => 'input text'),
  6180. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  6181. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6182. '/div'
  6183. );
  6184. $this->assertTags($result, $expected);
  6185. }
  6186. /**
  6187. * test automatic accept-charset overriding
  6188. *
  6189. * @return void
  6190. */
  6191. public function testCreateWithAcceptCharset() {
  6192. $result = $this->Form->create('UserForm', array(
  6193. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  6194. )
  6195. );
  6196. $expected = array(
  6197. 'form' => array(
  6198. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6199. 'accept-charset' => 'iso-8859-1'
  6200. ),
  6201. 'div' => array('style' => 'display:none;'),
  6202. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6203. '/div'
  6204. );
  6205. $this->assertTags($result, $expected);
  6206. }
  6207. /**
  6208. * Test base form url when url param is passed with multiple parameters (&)
  6209. *
  6210. */
  6211. public function testCreateQuerystringrequest() {
  6212. $encoding = strtolower(Configure::read('App.encoding'));
  6213. $result = $this->Form->create('Contact', array(
  6214. 'type' => 'post',
  6215. 'escape' => false,
  6216. 'url' => array(
  6217. 'controller' => 'controller',
  6218. 'action' => 'action',
  6219. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6220. )
  6221. ));
  6222. $expected = array(
  6223. 'form' => array(
  6224. 'id' => 'ContactAddForm',
  6225. 'method' => 'post',
  6226. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6227. 'accept-charset' => $encoding
  6228. ),
  6229. 'div' => array('style' => 'display:none;'),
  6230. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6231. '/div'
  6232. );
  6233. $this->assertTags($result, $expected);
  6234. $result = $this->Form->create('Contact', array(
  6235. 'type' => 'post',
  6236. 'url' => array(
  6237. 'controller' => 'controller',
  6238. 'action' => 'action',
  6239. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6240. )
  6241. ));
  6242. $expected = array(
  6243. 'form' => array(
  6244. 'id' => 'ContactAddForm',
  6245. 'method' => 'post',
  6246. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6247. 'accept-charset' => $encoding
  6248. ),
  6249. 'div' => array('style' => 'display:none;'),
  6250. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6251. '/div'
  6252. );
  6253. $this->assertTags($result, $expected);
  6254. }
  6255. /**
  6256. * test that create() doesn't cause errors by multiple id's being in the primary key
  6257. * as could happen with multiple select or checkboxes.
  6258. *
  6259. * @return void
  6260. */
  6261. public function testCreateWithMultipleIdInData() {
  6262. $encoding = strtolower(Configure::read('App.encoding'));
  6263. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6264. $result = $this->Form->create('Contact');
  6265. $expected = array(
  6266. 'form' => array(
  6267. 'id' => 'ContactAddForm',
  6268. 'method' => 'post',
  6269. 'action' => '/contacts/add',
  6270. 'accept-charset' => $encoding
  6271. ),
  6272. 'div' => array('style' => 'display:none;'),
  6273. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6274. '/div'
  6275. );
  6276. $this->assertTags($result, $expected);
  6277. }
  6278. /**
  6279. * test that create() doesn't add in extra passed params.
  6280. *
  6281. * @return void
  6282. */
  6283. public function testCreatePassedArgs() {
  6284. $encoding = strtolower(Configure::read('App.encoding'));
  6285. $this->Form->request->data['Contact']['id'] = 1;
  6286. $result = $this->Form->create('Contact', array(
  6287. 'type' => 'post',
  6288. 'escape' => false,
  6289. 'url' => array(
  6290. 'action' => 'edit',
  6291. 'myparam'
  6292. )
  6293. ));
  6294. $expected = array(
  6295. 'form' => array(
  6296. 'id' => 'ContactAddForm',
  6297. 'method' => 'post',
  6298. 'action' => '/contacts/edit/myparam',
  6299. 'accept-charset' => $encoding
  6300. ),
  6301. 'div' => array('style' => 'display:none;'),
  6302. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6303. '/div'
  6304. );
  6305. $this->assertTags($result, $expected);
  6306. }
  6307. /**
  6308. * test creating a get form, and get form inputs.
  6309. *
  6310. * @return void
  6311. */
  6312. public function testGetFormCreate() {
  6313. $encoding = strtolower(Configure::read('App.encoding'));
  6314. $result = $this->Form->create('Contact', array('type' => 'get'));
  6315. $this->assertTags($result, array('form' => array(
  6316. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6317. 'accept-charset' => $encoding
  6318. )));
  6319. $result = $this->Form->text('Contact.name');
  6320. $this->assertTags($result, array('input' => array(
  6321. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6322. )));
  6323. $result = $this->Form->password('password');
  6324. $this->assertTags($result, array('input' => array(
  6325. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6326. )));
  6327. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6328. $result = $this->Form->text('user_form');
  6329. $this->assertTags($result, array('input' => array(
  6330. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6331. )));
  6332. }
  6333. /**
  6334. * test get form, and inputs when the model param is false
  6335. *
  6336. * @return void
  6337. */
  6338. public function testGetFormWithFalseModel() {
  6339. $encoding = strtolower(Configure::read('App.encoding'));
  6340. $this->Form->request['controller'] = 'contact_test';
  6341. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6342. $expected = array('form' => array(
  6343. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6344. 'accept-charset' => $encoding
  6345. ));
  6346. $this->assertTags($result, $expected);
  6347. $result = $this->Form->text('reason');
  6348. $expected = array(
  6349. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6350. );
  6351. $this->assertTags($result, $expected);
  6352. }
  6353. /**
  6354. * test that datetime() works with GET style forms.
  6355. *
  6356. * @return void
  6357. */
  6358. public function testDateTimeWithGetForms() {
  6359. extract($this->dateRegex);
  6360. $this->Form->create('Contact', array('type' => 'get'));
  6361. $result = $this->Form->datetime('created');
  6362. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6363. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6364. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6365. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6366. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6367. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6368. }
  6369. /**
  6370. * testEditFormWithData method
  6371. *
  6372. * test auto populating form elements from submitted data.
  6373. *
  6374. * @return void
  6375. */
  6376. public function testEditFormWithData() {
  6377. $this->Form->request->data = array('Person' => array(
  6378. 'id' => 1,
  6379. 'first_name' => 'Nate',
  6380. 'last_name' => 'Abele',
  6381. 'email' => 'nate@example.com'
  6382. ));
  6383. $this->Form->request->addParams(array(
  6384. 'models' => array('Person'),
  6385. 'controller' => 'people',
  6386. 'action' => 'add'
  6387. ));
  6388. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6389. $this->Form->create();
  6390. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6391. $expected = array(
  6392. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6393. 'select' => array(
  6394. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6395. ),
  6396. array('option' => array('value' => 1)), 'Nate', '/option',
  6397. array('option' => array('value' => 2)), 'Garrett', '/option',
  6398. array('option' => array('value' => 3)), 'Larry', '/option',
  6399. '/select'
  6400. );
  6401. $this->assertTags($result, $expected);
  6402. }
  6403. /**
  6404. * Test that required fields are created for various types of validation.
  6405. *
  6406. * @return void
  6407. */
  6408. public function testFormInputRequiredDetection() {
  6409. $this->Form->create('Contact');
  6410. $result = $this->Form->input('Contact.non_existing');
  6411. $expected = array(
  6412. 'div' => array('class' => 'input text'),
  6413. 'label' => array('for' => 'ContactNonExisting'),
  6414. 'Non Existing',
  6415. '/label',
  6416. 'input' => array(
  6417. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  6418. 'id' => 'ContactNonExisting'
  6419. ),
  6420. '/div'
  6421. );
  6422. $this->assertTags($result, $expected);
  6423. $result = $this->Form->input('Contact.imrequired');
  6424. $expected = array(
  6425. 'div' => array('class' => 'input text required'),
  6426. 'label' => array('for' => 'ContactImrequired'),
  6427. 'Imrequired',
  6428. '/label',
  6429. 'input' => array(
  6430. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  6431. 'id' => 'ContactImrequired'
  6432. ),
  6433. '/div'
  6434. );
  6435. $this->assertTags($result, $expected);
  6436. $result = $this->Form->input('Contact.imalsorequired');
  6437. $expected = array(
  6438. 'div' => array('class' => 'input text required'),
  6439. 'label' => array('for' => 'ContactImalsorequired'),
  6440. 'Imalsorequired',
  6441. '/label',
  6442. 'input' => array(
  6443. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  6444. 'id' => 'ContactImalsorequired'
  6445. ),
  6446. '/div'
  6447. );
  6448. $this->assertTags($result, $expected);
  6449. $result = $this->Form->input('Contact.imrequiredtoo');
  6450. $expected = array(
  6451. 'div' => array('class' => 'input text required'),
  6452. 'label' => array('for' => 'ContactImrequiredtoo'),
  6453. 'Imrequiredtoo',
  6454. '/label',
  6455. 'input' => array(
  6456. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  6457. 'id' => 'ContactImrequiredtoo'
  6458. ),
  6459. '/div'
  6460. );
  6461. $this->assertTags($result, $expected);
  6462. $result = $this->Form->input('Contact.required_one');
  6463. $expected = array(
  6464. 'div' => array('class' => 'input text required'),
  6465. 'label' => array('for' => 'ContactRequiredOne'),
  6466. 'Required One',
  6467. '/label',
  6468. 'input' => array(
  6469. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  6470. 'id' => 'ContactRequiredOne'
  6471. ),
  6472. '/div'
  6473. );
  6474. $this->assertTags($result, $expected);
  6475. $result = $this->Form->input('Contact.string_required');
  6476. $expected = array(
  6477. 'div' => array('class' => 'input text required'),
  6478. 'label' => array('for' => 'ContactStringRequired'),
  6479. 'String Required',
  6480. '/label',
  6481. 'input' => array(
  6482. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  6483. 'id' => 'ContactStringRequired'
  6484. ),
  6485. '/div'
  6486. );
  6487. $this->assertTags($result, $expected);
  6488. $result = $this->Form->input('Contact.imnotrequired');
  6489. $expected = array(
  6490. 'div' => array('class' => 'input text'),
  6491. 'label' => array('for' => 'ContactImnotrequired'),
  6492. 'Imnotrequired',
  6493. '/label',
  6494. 'input' => array(
  6495. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  6496. 'id' => 'ContactImnotrequired'
  6497. ),
  6498. '/div'
  6499. );
  6500. $this->assertTags($result, $expected);
  6501. $result = $this->Form->input('Contact.imalsonotrequired');
  6502. $expected = array(
  6503. 'div' => array('class' => 'input text'),
  6504. 'label' => array('for' => 'ContactImalsonotrequired'),
  6505. 'Imalsonotrequired',
  6506. '/label',
  6507. 'input' => array(
  6508. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  6509. 'id' => 'ContactImalsonotrequired'
  6510. ),
  6511. '/div'
  6512. );
  6513. $this->assertTags($result, $expected);
  6514. $result = $this->Form->input('Contact.imalsonotrequired2');
  6515. $expected = array(
  6516. 'div' => array('class' => 'input text'),
  6517. 'label' => array('for' => 'ContactImalsonotrequired2'),
  6518. 'Imalsonotrequired2',
  6519. '/label',
  6520. 'input' => array(
  6521. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  6522. 'id' => 'ContactImalsonotrequired2'
  6523. ),
  6524. '/div'
  6525. );
  6526. $this->assertTags($result, $expected);
  6527. $result = $this->Form->input('Contact.imnotrequiredeither');
  6528. $expected = array(
  6529. 'div' => array('class' => 'input text'),
  6530. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6531. 'Imnotrequiredeither',
  6532. '/label',
  6533. 'input' => array(
  6534. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  6535. 'id' => 'ContactImnotrequiredeither'
  6536. ),
  6537. '/div'
  6538. );
  6539. $this->assertTags($result, $expected);
  6540. $result = $this->Form->input('Contact.iamrequiredalways');
  6541. $expected = array(
  6542. 'div' => array('class' => 'input text required'),
  6543. 'label' => array('for' => 'ContactIamrequiredalways'),
  6544. 'Iamrequiredalways',
  6545. '/label',
  6546. 'input' => array(
  6547. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  6548. 'id' => 'ContactIamrequiredalways'
  6549. ),
  6550. '/div'
  6551. );
  6552. $this->assertTags($result, $expected);
  6553. }
  6554. /**
  6555. * testFormMagicInput method
  6556. *
  6557. * @return void
  6558. */
  6559. public function testFormMagicInput() {
  6560. $encoding = strtolower(Configure::read('App.encoding'));
  6561. $result = $this->Form->create('Contact');
  6562. $expected = array(
  6563. 'form' => array(
  6564. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6565. 'accept-charset' => $encoding
  6566. ),
  6567. 'div' => array('style' => 'display:none;'),
  6568. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6569. '/div'
  6570. );
  6571. $this->assertTags($result, $expected);
  6572. $result = $this->Form->input('name');
  6573. $expected = array(
  6574. 'div' => array('class' => 'input text'),
  6575. 'label' => array('for' => 'ContactName'),
  6576. 'Name',
  6577. '/label',
  6578. 'input' => array(
  6579. 'type' => 'text', 'name' => 'data[Contact][name]',
  6580. 'id' => 'ContactName', 'maxlength' => '255'
  6581. ),
  6582. '/div'
  6583. );
  6584. $this->assertTags($result, $expected);
  6585. $result = $this->Form->input('non_existing_field_in_contact_model');
  6586. $expected = array(
  6587. 'div' => array('class' => 'input text'),
  6588. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  6589. 'Non Existing Field In Contact Model',
  6590. '/label',
  6591. 'input' => array(
  6592. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  6593. 'id' => 'ContactNonExistingFieldInContactModel'
  6594. ),
  6595. '/div'
  6596. );
  6597. $this->assertTags($result, $expected);
  6598. $result = $this->Form->input('Address.street');
  6599. $expected = array(
  6600. 'div' => array('class' => 'input text'),
  6601. 'label' => array('for' => 'AddressStreet'),
  6602. 'Street',
  6603. '/label',
  6604. 'input' => array(
  6605. 'type' => 'text', 'name' => 'data[Address][street]',
  6606. 'id' => 'AddressStreet'
  6607. ),
  6608. '/div'
  6609. );
  6610. $this->assertTags($result, $expected);
  6611. $result = $this->Form->input('Address.non_existing_field_in_model');
  6612. $expected = array(
  6613. 'div' => array('class' => 'input text'),
  6614. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  6615. 'Non Existing Field In Model',
  6616. '/label',
  6617. 'input' => array(
  6618. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  6619. 'id' => 'AddressNonExistingFieldInModel'
  6620. ),
  6621. '/div'
  6622. );
  6623. $this->assertTags($result, $expected);
  6624. $result = $this->Form->input('name', array('div' => false));
  6625. $expected = array(
  6626. 'label' => array('for' => 'ContactName'),
  6627. 'Name',
  6628. '/label',
  6629. 'input' => array(
  6630. 'type' => 'text', 'name' => 'data[Contact][name]',
  6631. 'id' => 'ContactName', 'maxlength' => '255'
  6632. )
  6633. );
  6634. $this->assertTags($result, $expected);
  6635. extract($this->dateRegex);
  6636. $now = strtotime('now');
  6637. $result = $this->Form->input('Contact.published', array('div' => false));
  6638. $expected = array(
  6639. 'label' => array('for' => 'ContactPublishedMonth'),
  6640. 'Published',
  6641. '/label',
  6642. array('select' => array(
  6643. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  6644. )),
  6645. $monthsRegex,
  6646. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6647. date('F', $now),
  6648. '/option',
  6649. '*/select',
  6650. '-',
  6651. array('select' => array(
  6652. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  6653. )),
  6654. $daysRegex,
  6655. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6656. date('j', $now),
  6657. '/option',
  6658. '*/select',
  6659. '-',
  6660. array('select' => array(
  6661. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  6662. )),
  6663. $yearsRegex,
  6664. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6665. date('Y', $now),
  6666. '*/select'
  6667. );
  6668. $this->assertTags($result, $expected);
  6669. $result = $this->Form->input('Contact.updated', array('div' => false));
  6670. $expected = array(
  6671. 'label' => array('for' => 'ContactUpdatedMonth'),
  6672. 'Updated',
  6673. '/label',
  6674. array('select' => array(
  6675. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  6676. )),
  6677. $monthsRegex,
  6678. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6679. date('F', $now),
  6680. '/option',
  6681. '*/select',
  6682. '-',
  6683. array('select' => array(
  6684. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  6685. )),
  6686. $daysRegex,
  6687. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6688. date('j', $now),
  6689. '/option',
  6690. '*/select',
  6691. '-',
  6692. array('select' => array(
  6693. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  6694. )),
  6695. $yearsRegex,
  6696. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6697. date('Y', $now),
  6698. '*/select'
  6699. );
  6700. $this->assertTags($result, $expected);
  6701. $result = $this->Form->input('UserForm.stuff');
  6702. $expected = array(
  6703. 'div' => array('class' => 'input text'),
  6704. 'label' => array('for' => 'UserFormStuff'),
  6705. 'Stuff',
  6706. '/label',
  6707. 'input' => array(
  6708. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  6709. 'id' => 'UserFormStuff', 'maxlength' => 10
  6710. ),
  6711. '/div'
  6712. );
  6713. $this->assertTags($result, $expected);
  6714. }
  6715. /**
  6716. * testForMagicInputNonExistingNorValidated method
  6717. *
  6718. * @return void
  6719. */
  6720. public function testForMagicInputNonExistingNorValidated() {
  6721. $encoding = strtolower(Configure::read('App.encoding'));
  6722. $result = $this->Form->create('Contact');
  6723. $expected = array(
  6724. 'form' => array(
  6725. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6726. 'accept-charset' => $encoding
  6727. ),
  6728. 'div' => array('style' => 'display:none;'),
  6729. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6730. '/div'
  6731. );
  6732. $this->assertTags($result, $expected);
  6733. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6734. $expected = array(
  6735. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6736. 'Non Existing Nor Validated',
  6737. '/label',
  6738. 'input' => array(
  6739. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6740. 'id' => 'ContactNonExistingNorValidated'
  6741. )
  6742. );
  6743. $this->assertTags($result, $expected);
  6744. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6745. 'div' => false, 'value' => 'my value'
  6746. ));
  6747. $expected = array(
  6748. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6749. 'Non Existing Nor Validated',
  6750. '/label',
  6751. 'input' => array(
  6752. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6753. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6754. )
  6755. );
  6756. $this->assertTags($result, $expected);
  6757. $this->Form->request->data = array(
  6758. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6759. ));
  6760. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6761. $expected = array(
  6762. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6763. 'Non Existing Nor Validated',
  6764. '/label',
  6765. 'input' => array(
  6766. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6767. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6768. )
  6769. );
  6770. $this->assertTags($result, $expected);
  6771. }
  6772. /**
  6773. * testFormMagicInputLabel method
  6774. *
  6775. * @return void
  6776. */
  6777. public function testFormMagicInputLabel() {
  6778. $encoding = strtolower(Configure::read('App.encoding'));
  6779. $result = $this->Form->create('Contact');
  6780. $expected = array(
  6781. 'form' => array(
  6782. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6783. 'accept-charset' => $encoding
  6784. ),
  6785. 'div' => array('style' => 'display:none;'),
  6786. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6787. '/div'
  6788. );
  6789. $this->assertTags($result, $expected);
  6790. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  6791. $this->assertTags($result, array('input' => array(
  6792. 'name' => 'data[Contact][name]', 'type' => 'text',
  6793. 'id' => 'ContactName', 'maxlength' => '255')
  6794. ));
  6795. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  6796. $expected = array(
  6797. 'label' => array('for' => 'ContactName'),
  6798. 'My label',
  6799. '/label',
  6800. 'input' => array(
  6801. 'type' => 'text', 'name' => 'data[Contact][name]',
  6802. 'id' => 'ContactName', 'maxlength' => '255'
  6803. )
  6804. );
  6805. $this->assertTags($result, $expected);
  6806. $result = $this->Form->input('Contact.name', array(
  6807. 'div' => false, 'label' => array('class' => 'mandatory')
  6808. ));
  6809. $expected = array(
  6810. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6811. 'Name',
  6812. '/label',
  6813. 'input' => array(
  6814. 'type' => 'text', 'name' => 'data[Contact][name]',
  6815. 'id' => 'ContactName', 'maxlength' => '255'
  6816. )
  6817. );
  6818. $this->assertTags($result, $expected);
  6819. $result = $this->Form->input('Contact.name', array(
  6820. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  6821. ));
  6822. $expected = array(
  6823. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6824. 'My label',
  6825. '/label',
  6826. 'input' => array(
  6827. 'type' => 'text', 'name' => 'data[Contact][name]',
  6828. 'id' => 'ContactName', 'maxlength' => '255'
  6829. )
  6830. );
  6831. $this->assertTags($result, $expected);
  6832. $result = $this->Form->input('Contact.name', array(
  6833. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  6834. ));
  6835. $expected = array(
  6836. 'label' => array('for' => 'my_id'),
  6837. 'Name',
  6838. '/label',
  6839. 'input' => array(
  6840. 'type' => 'text', 'name' => 'data[Contact][name]',
  6841. 'id' => 'my_id', 'maxlength' => '255'
  6842. )
  6843. );
  6844. $this->assertTags($result, $expected);
  6845. $result = $this->Form->input('1.id');
  6846. $this->assertTags($result, array('input' => array(
  6847. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6848. 'id' => 'Contact1Id'
  6849. )));
  6850. $result = $this->Form->input("1.name");
  6851. $expected = array(
  6852. 'div' => array('class' => 'input text'),
  6853. 'label' => array('for' => 'Contact1Name'),
  6854. 'Name',
  6855. '/label',
  6856. 'input' => array(
  6857. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  6858. 'id' => 'Contact1Name', 'maxlength' => '255'
  6859. ),
  6860. '/div'
  6861. );
  6862. $this->assertTags($result, $expected);
  6863. $result = $this->Form->input('Contact.1.id');
  6864. $this->assertTags($result, array(
  6865. 'input' => array(
  6866. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6867. 'id' => 'Contact1Id'
  6868. )
  6869. ));
  6870. $result = $this->Form->input("Model.1.name");
  6871. $expected = array(
  6872. 'div' => array('class' => 'input text'),
  6873. 'label' => array('for' => 'Model1Name'),
  6874. 'Name',
  6875. '/label',
  6876. 'input' => array(
  6877. 'type' => 'text', 'name' => 'data[Model][1][name]',
  6878. 'id' => 'Model1Name'
  6879. ),
  6880. '/div'
  6881. );
  6882. $this->assertTags($result, $expected);
  6883. }
  6884. /**
  6885. * testFormEnd method
  6886. *
  6887. * @return void
  6888. */
  6889. public function testFormEnd() {
  6890. $this->assertEquals('</form>', $this->Form->end());
  6891. $result = $this->Form->end('');
  6892. $expected = array(
  6893. 'div' => array('class' => 'submit'),
  6894. 'input' => array('type' => 'submit', 'value' => ''),
  6895. '/div',
  6896. '/form'
  6897. );
  6898. $this->assertTags($result, $expected);
  6899. $result = $this->Form->end(array('label' => ''));
  6900. $expected = array(
  6901. 'div' => array('class' => 'submit'),
  6902. 'input' => array('type' => 'submit', 'value' => ''),
  6903. '/div',
  6904. '/form'
  6905. );
  6906. $this->assertTags($result, $expected);
  6907. $result = $this->Form->end('save');
  6908. $expected = array(
  6909. 'div' => array('class' => 'submit'),
  6910. 'input' => array('type' => 'submit', 'value' => 'save'),
  6911. '/div',
  6912. '/form'
  6913. );
  6914. $this->assertTags($result, $expected);
  6915. $result = $this->Form->end(array('label' => 'save'));
  6916. $expected = array(
  6917. 'div' => array('class' => 'submit'),
  6918. 'input' => array('type' => 'submit', 'value' => 'save'),
  6919. '/div',
  6920. '/form'
  6921. );
  6922. $this->assertTags($result, $expected);
  6923. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  6924. $expected = array(
  6925. 'div' => array('class' => 'submit'),
  6926. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6927. '/div',
  6928. '/form'
  6929. );
  6930. $this->assertTags($result, $expected);
  6931. $result = $this->Form->end(array('name' => 'Whatever'));
  6932. $expected = array(
  6933. 'div' => array('class' => 'submit'),
  6934. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  6935. '/div',
  6936. '/form'
  6937. );
  6938. $this->assertTags($result, $expected);
  6939. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  6940. $expected = array(
  6941. 'div' => array('class' => 'good'),
  6942. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6943. '/div',
  6944. '/form'
  6945. );
  6946. $this->assertTags($result, $expected);
  6947. $result = $this->Form->end(array(
  6948. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  6949. ));
  6950. $expected = array(
  6951. 'div' => array('class' => 'good'),
  6952. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6953. '/div',
  6954. '/form'
  6955. );
  6956. $this->assertTags($result, $expected);
  6957. }
  6958. /**
  6959. * testMultipleFormWithIdFields method
  6960. *
  6961. * @return void
  6962. */
  6963. public function testMultipleFormWithIdFields() {
  6964. $this->Form->create('UserForm');
  6965. $result = $this->Form->input('id');
  6966. $this->assertTags($result, array('input' => array(
  6967. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  6968. )));
  6969. $result = $this->Form->input('ValidateItem.id');
  6970. $this->assertTags($result, array('input' => array(
  6971. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  6972. 'id' => 'ValidateItemId'
  6973. )));
  6974. $result = $this->Form->input('ValidateUser.id');
  6975. $this->assertTags($result, array('input' => array(
  6976. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  6977. 'id' => 'ValidateUserId'
  6978. )));
  6979. }
  6980. /**
  6981. * testDbLessModel method
  6982. *
  6983. * @return void
  6984. */
  6985. public function testDbLessModel() {
  6986. $this->Form->create('TestMail');
  6987. $result = $this->Form->input('name');
  6988. $expected = array(
  6989. 'div' => array('class' => 'input text'),
  6990. 'label' => array('for' => 'TestMailName'),
  6991. 'Name',
  6992. '/label',
  6993. 'input' => array(
  6994. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6995. 'id' => 'TestMailName'
  6996. ),
  6997. '/div'
  6998. );
  6999. $this->assertTags($result, $expected);
  7000. ClassRegistry::init('TestMail');
  7001. $this->Form->create('TestMail');
  7002. $result = $this->Form->input('name');
  7003. $expected = array(
  7004. 'div' => array('class' => 'input text'),
  7005. 'label' => array('for' => 'TestMailName'),
  7006. 'Name',
  7007. '/label',
  7008. 'input' => array(
  7009. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7010. 'id' => 'TestMailName'
  7011. ),
  7012. '/div'
  7013. );
  7014. $this->assertTags($result, $expected);
  7015. }
  7016. /**
  7017. * testBrokenness method
  7018. *
  7019. * @return void
  7020. */
  7021. public function testBrokenness() {
  7022. /*
  7023. * #4 This test has two parents and four children. By default (as of r7117) both
  7024. * parents are show but the first parent is missing a child. This is the inconsistency
  7025. * in the default behaviour - one parent has all children, the other does not - dependent
  7026. * on the data values.
  7027. */
  7028. $result = $this->Form->select('Model.field', array(
  7029. 'Fred' => array(
  7030. 'freds_son_1' => 'Fred',
  7031. 'freds_son_2' => 'Freddie'
  7032. ),
  7033. 'Bert' => array(
  7034. 'berts_son_1' => 'Albert',
  7035. 'berts_son_2' => 'Bertie')
  7036. ),
  7037. array('showParents' => true, 'empty' => false)
  7038. );
  7039. $expected = array(
  7040. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7041. array('optgroup' => array('label' => 'Fred')),
  7042. array('option' => array('value' => 'freds_son_1')),
  7043. 'Fred',
  7044. '/option',
  7045. array('option' => array('value' => 'freds_son_2')),
  7046. 'Freddie',
  7047. '/option',
  7048. '/optgroup',
  7049. array('optgroup' => array('label' => 'Bert')),
  7050. array('option' => array('value' => 'berts_son_1')),
  7051. 'Albert',
  7052. '/option',
  7053. array('option' => array('value' => 'berts_son_2')),
  7054. 'Bertie',
  7055. '/option',
  7056. '/optgroup',
  7057. '/select'
  7058. );
  7059. $this->assertTags($result, $expected);
  7060. /*
  7061. * #2 This is structurally identical to the test above (#1) - only the parent name has
  7062. * changed, so we should expect the same select list data, just with a different name
  7063. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  7064. * This is where data corruption can occur, because when a select value is missing from
  7065. * a list a form will substitute the first value in the list - without the user knowing.
  7066. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  7067. * affect the availability of 3 => 'Three' as a valid option.
  7068. */
  7069. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  7070. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  7071. ));
  7072. $result = $this->Form->select(
  7073. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  7074. );
  7075. $expected = array(
  7076. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7077. array('option' => array('value' => 1)),
  7078. 'One',
  7079. '/option',
  7080. array('option' => array('value' => 2)),
  7081. 'Two',
  7082. '/option',
  7083. array('optgroup' => array('label' => 'Three')),
  7084. array('option' => array('value' => 3)),
  7085. 'Three',
  7086. '/option',
  7087. array('option' => array('value' => 4)),
  7088. 'Four',
  7089. '/option',
  7090. array('option' => array('value' => 5)),
  7091. 'Five',
  7092. '/option',
  7093. '/optgroup',
  7094. '/select'
  7095. );
  7096. $this->assertTags($result, $expected);
  7097. }
  7098. /**
  7099. * Test the generation of fields for a multi record form.
  7100. *
  7101. * @return void
  7102. */
  7103. public function testMultiRecordForm() {
  7104. $this->Form->create('ValidateProfile');
  7105. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  7106. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  7107. $expected = array(
  7108. 'div' => array('class' => 'input textarea'),
  7109. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  7110. 'Name',
  7111. '/label',
  7112. 'textarea' => array(
  7113. 'id' => 'ValidateProfile1ValidateItem2Name',
  7114. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  7115. 'cols' => 30,
  7116. 'rows' => 6
  7117. ),
  7118. 'Value',
  7119. '/textarea',
  7120. '/div'
  7121. );
  7122. $this->assertTags($result, $expected);
  7123. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
  7124. $expected = array(
  7125. 'div' => array('class' => 'input date'),
  7126. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7127. 'Created',
  7128. '/label',
  7129. array('select' => array(
  7130. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  7131. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7132. )
  7133. ),
  7134. array('option' => array('value' => '')), '/option',
  7135. $this->dateRegex['monthsRegex'],
  7136. '/select', '-',
  7137. array('select' => array(
  7138. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  7139. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7140. )
  7141. ),
  7142. array('option' => array('value' => '')), '/option',
  7143. $this->dateRegex['daysRegex'],
  7144. '/select', '-',
  7145. array('select' => array(
  7146. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  7147. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7148. )
  7149. ),
  7150. array('option' => array('value' => '')), '/option',
  7151. $this->dateRegex['yearsRegex'],
  7152. '/select',
  7153. '/div'
  7154. );
  7155. $this->assertTags($result, $expected);
  7156. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7157. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7158. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7159. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7160. $expected = array(
  7161. 'div' => array('class' => 'input select error'),
  7162. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7163. 'Profile',
  7164. '/label',
  7165. 'select' => array(
  7166. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7167. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7168. 'class' => 'form-error'
  7169. ),
  7170. '/select',
  7171. array('div' => array('class' => 'error-message')),
  7172. 'Error',
  7173. '/div',
  7174. '/div'
  7175. );
  7176. $this->assertTags($result, $expected);
  7177. }
  7178. /**
  7179. * test the correct display of multi-record form validation errors.
  7180. *
  7181. * @return void
  7182. */
  7183. public function testMultiRecordFormValidationErrors() {
  7184. $this->Form->create('ValidateProfile');
  7185. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7186. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7187. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7188. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7189. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7190. $result = $this->Form->error('ValidateProfile.2.city');
  7191. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7192. $result = $this->Form->error('2.city');
  7193. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7194. }
  7195. /**
  7196. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7197. *
  7198. * @return void
  7199. */
  7200. public function testInputTemplate() {
  7201. $result = $this->Form->input('Contact.email', array(
  7202. 'type' => 'text', 'format' => array('input')
  7203. ));
  7204. $expected = array(
  7205. 'div' => array('class' => 'input text'),
  7206. 'input' => array(
  7207. 'type' => 'text', 'name' => 'data[Contact][email]',
  7208. 'id' => 'ContactEmail'
  7209. ),
  7210. '/div'
  7211. );
  7212. $this->assertTags($result, $expected);
  7213. $result = $this->Form->input('Contact.email', array(
  7214. 'type' => 'text', 'format' => array('input', 'label'),
  7215. 'label' => '<em>Email (required)</em>'
  7216. ));
  7217. $expected = array(
  7218. 'div' => array('class' => 'input text'),
  7219. array('input' => array(
  7220. 'type' => 'text', 'name' => 'data[Contact][email]',
  7221. 'id' => 'ContactEmail'
  7222. )),
  7223. 'label' => array('for' => 'ContactEmail'),
  7224. 'em' => array(),
  7225. 'Email (required)',
  7226. '/em',
  7227. '/label',
  7228. '/div'
  7229. );
  7230. $this->assertTags($result, $expected);
  7231. $result = $this->Form->input('Contact.email', array(
  7232. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7233. 'between' => '<div>Something in the middle</div>',
  7234. 'after' => '<span>Some text at the end</span>'
  7235. ));
  7236. $expected = array(
  7237. 'div' => array('class' => 'input text'),
  7238. array('input' => array(
  7239. 'type' => 'text', 'name' => 'data[Contact][email]',
  7240. 'id' => 'ContactEmail'
  7241. )),
  7242. array('div' => array()),
  7243. 'Something in the middle',
  7244. '/div',
  7245. 'label' => array('for' => 'ContactEmail'),
  7246. 'Email',
  7247. '/label',
  7248. 'span' => array(),
  7249. 'Some text at the end',
  7250. '/span',
  7251. '/div'
  7252. );
  7253. $this->assertTags($result, $expected);
  7254. $result = $this->Form->input('Contact.method', array(
  7255. 'type' => 'radio',
  7256. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7257. 'between' => 'I am between',
  7258. ));
  7259. $expected = array(
  7260. 'div' => array('class' => 'input radio'),
  7261. 'fieldset' => array(),
  7262. 'legend' => array(),
  7263. 'Method',
  7264. '/legend',
  7265. 'I am between',
  7266. 'input' => array(
  7267. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  7268. 'value' => '', 'id' => 'ContactMethod_'
  7269. ),
  7270. array('input' => array(
  7271. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7272. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7273. )),
  7274. array('label' => array('for' => 'ContactMethodEmail')),
  7275. 'Email',
  7276. '/label',
  7277. array('input' => array(
  7278. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7279. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7280. )),
  7281. array('label' => array('for' => 'ContactMethodPigeon')),
  7282. 'Pigeon',
  7283. '/label',
  7284. '/fieldset',
  7285. '/div',
  7286. );
  7287. $this->assertTags($result, $expected);
  7288. }
  7289. /**
  7290. * test that some html5 inputs + FormHelper::__call() work
  7291. *
  7292. * @return void
  7293. */
  7294. public function testHtml5Inputs() {
  7295. $result = $this->Form->email('User.email');
  7296. $expected = array(
  7297. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  7298. );
  7299. $this->assertTags($result, $expected);
  7300. $result = $this->Form->search('User.query');
  7301. $expected = array(
  7302. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  7303. );
  7304. $this->assertTags($result, $expected);
  7305. $result = $this->Form->search('User.query', array('value' => 'test'));
  7306. $expected = array(
  7307. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7308. );
  7309. $this->assertTags($result, $expected);
  7310. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7311. $expected = array(
  7312. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7313. );
  7314. $this->assertTags($result, $expected);
  7315. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  7316. $expected = array(
  7317. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7318. );
  7319. $this->assertTags($result, $expected);
  7320. }
  7321. /**
  7322. *
  7323. * @expectedException CakeException
  7324. * @return void
  7325. */
  7326. public function testHtml5InputException() {
  7327. $this->Form->email();
  7328. }
  7329. /**
  7330. * Tests that a model can be loaded from the model names passed in the request object
  7331. *
  7332. * @return void
  7333. */
  7334. public function testIntrospectModelFromRequest() {
  7335. $this->loadFixtures('Post');
  7336. App::build(array(
  7337. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  7338. ));
  7339. CakePlugin::load('TestPlugin');
  7340. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  7341. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  7342. $this->Form->create('TestPluginPost');
  7343. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  7344. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  7345. CakePlugin::unload();
  7346. App::build();
  7347. }
  7348. /**
  7349. * Tests that it is possible to set the validation errors directly in the helper for a field
  7350. *
  7351. * @return void
  7352. */
  7353. public function testCustomValidationErrors() {
  7354. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7355. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7356. $this->assertEquals('Badness!', $result);
  7357. }
  7358. /**
  7359. * Tests that the 'on' key validates as expected on create
  7360. *
  7361. * @return void
  7362. */
  7363. public function testRequiredOnCreate() {
  7364. $this->Form->create('Contact');
  7365. $result = $this->Form->input('Contact.imrequiredonupdate');
  7366. $expected = array(
  7367. 'div' => array('class' => 'input text'),
  7368. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7369. 'Imrequiredonupdate',
  7370. '/label',
  7371. 'input' => array(
  7372. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7373. 'id' => 'ContactImrequiredonupdate'
  7374. ),
  7375. '/div'
  7376. );
  7377. $this->assertTags($result, $expected);
  7378. $result = $this->Form->input('Contact.imrequiredoncreate');
  7379. $expected = array(
  7380. 'div' => array('class' => 'input text required'),
  7381. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7382. 'Imrequiredoncreate',
  7383. '/label',
  7384. 'input' => array(
  7385. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7386. 'id' => 'ContactImrequiredoncreate'
  7387. ),
  7388. '/div'
  7389. );
  7390. $this->assertTags($result, $expected);
  7391. $result = $this->Form->input('Contact.imrequiredonboth');
  7392. $expected = array(
  7393. 'div' => array('class' => 'input text required'),
  7394. 'label' => array('for' => 'ContactImrequiredonboth'),
  7395. 'Imrequiredonboth',
  7396. '/label',
  7397. 'input' => array(
  7398. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7399. 'id' => 'ContactImrequiredonboth'
  7400. ),
  7401. '/div'
  7402. );
  7403. $this->assertTags($result, $expected);
  7404. $result = $this->Form->input('Contact.imrequired');
  7405. $expected = array(
  7406. 'div' => array('class' => 'input text required'),
  7407. 'label' => array('for' => 'ContactImrequired'),
  7408. 'Imrequired',
  7409. '/label',
  7410. 'input' => array(
  7411. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7412. 'id' => 'ContactImrequired'
  7413. ),
  7414. '/div'
  7415. );
  7416. $this->assertTags($result, $expected);
  7417. }
  7418. /**
  7419. * Tests that the 'on' key validates as expected on update
  7420. *
  7421. * @return void
  7422. */
  7423. public function testRequiredOnUpdate() {
  7424. $this->Form->request->data['Contact']['id'] = 1;
  7425. $this->Form->create('Contact');
  7426. $result = $this->Form->input('Contact.imrequiredonupdate');
  7427. $expected = array(
  7428. 'div' => array('class' => 'input text required'),
  7429. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7430. 'Imrequiredonupdate',
  7431. '/label',
  7432. 'input' => array(
  7433. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7434. 'id' => 'ContactImrequiredonupdate'
  7435. ),
  7436. '/div'
  7437. );
  7438. $this->assertTags($result, $expected);
  7439. $result = $this->Form->input('Contact.imrequiredoncreate');
  7440. $expected = array(
  7441. 'div' => array('class' => 'input text'),
  7442. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7443. 'Imrequiredoncreate',
  7444. '/label',
  7445. 'input' => array(
  7446. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7447. 'id' => 'ContactImrequiredoncreate'
  7448. ),
  7449. '/div'
  7450. );
  7451. $this->assertTags($result, $expected);
  7452. $result = $this->Form->input('Contact.imrequiredonboth');
  7453. $expected = array(
  7454. 'div' => array('class' => 'input text required'),
  7455. 'label' => array('for' => 'ContactImrequiredonboth'),
  7456. 'Imrequiredonboth',
  7457. '/label',
  7458. 'input' => array(
  7459. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7460. 'id' => 'ContactImrequiredonboth'
  7461. ),
  7462. '/div'
  7463. );
  7464. $this->assertTags($result, $expected);
  7465. $result = $this->Form->input('Contact.imrequired');
  7466. $expected = array(
  7467. 'div' => array('class' => 'input text required'),
  7468. 'label' => array('for' => 'ContactImrequired'),
  7469. 'Imrequired',
  7470. '/label',
  7471. 'input' => array(
  7472. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7473. 'id' => 'ContactImrequired'
  7474. ),
  7475. '/div'
  7476. );
  7477. $this->assertTags($result, $expected);
  7478. }
  7479. /**
  7480. * Test inputDefaults setter and getter
  7481. *
  7482. * @return void
  7483. */
  7484. public function testInputDefaults() {
  7485. $this->Form->create('Contact');
  7486. $this->Form->inputDefaults(array(
  7487. 'label' => false,
  7488. 'div' => array(
  7489. 'style' => 'color: #000;'
  7490. )
  7491. ));
  7492. $result = $this->Form->input('Contact.field1');
  7493. $expected = array(
  7494. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  7495. 'input' => array(
  7496. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7497. 'id' => 'ContactField1'
  7498. ),
  7499. '/div'
  7500. );
  7501. $this->assertTags($result, $expected);
  7502. $this->Form->inputDefaults(array(
  7503. 'div' => false,
  7504. 'label' => 'Label',
  7505. ));
  7506. $result = $this->Form->input('Contact.field1');
  7507. $expected = array(
  7508. 'label' => array('for' => 'ContactField1'),
  7509. 'Label',
  7510. '/label',
  7511. 'input' => array(
  7512. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7513. 'id' => 'ContactField1'
  7514. ),
  7515. );
  7516. $this->assertTags($result, $expected);
  7517. $this->Form->inputDefaults(array(
  7518. 'label' => false,
  7519. ), true);
  7520. $result = $this->Form->input('Contact.field1');
  7521. $expected = array(
  7522. 'input' => array(
  7523. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7524. 'id' => 'ContactField1'
  7525. ),
  7526. );
  7527. $this->assertTags($result, $expected);
  7528. $result = $this->Form->inputDefaults();
  7529. $expected = array(
  7530. 'div' => false,
  7531. 'label' => false,
  7532. );
  7533. $this->assertEqual($result, $expected);
  7534. }
  7535. }