FormHelperTest.php 285 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966
  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 (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  16. * @package Cake.Test.Case.View.Helper
  17. * @since CakePHP(tm) v 1.2.0.4206
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. App::uses('ClassRegistry', 'Utility');
  21. App::uses('Controller', 'Controller');
  22. App::uses('View', 'View');
  23. App::uses('Model', 'Model');
  24. App::uses('Security', 'Utility');
  25. App::uses('CakeRequest', 'Network');
  26. App::uses('HtmlHelper', 'View/Helper');
  27. App::uses('FormHelper', 'View/Helper');
  28. App::uses('Router', 'Routing');
  29. /**
  30. * ContactTestController class
  31. *
  32. * @package cake
  33. * @package Cake.Test.Case.View.Helper
  34. */
  35. class ContactTestController extends Controller {
  36. /**
  37. * uses property
  38. *
  39. * @var mixed null
  40. */
  41. public $uses = null;
  42. }
  43. /**
  44. * Contact class
  45. *
  46. * @package cake
  47. * @package Cake.Test.Case.View.Helper
  48. */
  49. class Contact extends CakeTestModel {
  50. /**
  51. * useTable property
  52. *
  53. * @var bool false
  54. */
  55. public $useTable = false;
  56. /**
  57. * Default schema
  58. *
  59. * @var array
  60. */
  61. protected $_schema = array(
  62. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  63. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  64. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  65. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  66. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  67. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  68. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  69. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  70. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  71. );
  72. /**
  73. * validate property
  74. *
  75. * @var array
  76. */
  77. public $validate = array(
  78. 'non_existing' => array(),
  79. 'idontexist' => array(),
  80. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  81. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  82. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  83. 'imrequiredonboth' => array(
  84. 'required' => array('rule' => 'alphaNumeric'),
  85. ),
  86. 'string_required' => 'notEmpty',
  87. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  88. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  89. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  90. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  91. 'imalsonotrequired' => array(
  92. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  93. 'between' => array('rule' => array('between', 5, 30)),
  94. ),
  95. 'imalsonotrequired2' => array(
  96. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  97. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  98. ),
  99. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  100. 'iamrequiredalways' => array(
  101. 'email' => array('rule' => 'email'),
  102. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  103. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  104. ),
  105. 'boolean_field' => array('rule' => 'boolean')
  106. );
  107. /**
  108. * schema method
  109. *
  110. * @return void
  111. */
  112. public function setSchema($schema) {
  113. $this->_schema = $schema;
  114. }
  115. /**
  116. * hasAndBelongsToMany property
  117. *
  118. * @var array
  119. */
  120. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  121. /**
  122. * hasAndBelongsToMany property
  123. *
  124. * @var array
  125. */
  126. public $belongsTo = array('User' => array('className' => 'UserForm'));
  127. }
  128. /**
  129. * ContactTagsContact class
  130. *
  131. * @package cake
  132. * @package Cake.Test.Case.View.Helper
  133. */
  134. class ContactTagsContact extends CakeTestModel {
  135. /**
  136. * useTable property
  137. *
  138. * @var bool false
  139. */
  140. public $useTable = false;
  141. /**
  142. * Default schema
  143. *
  144. * @var array
  145. */
  146. protected $_schema = array(
  147. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  148. 'contact_tag_id' => array(
  149. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  150. )
  151. );
  152. /**
  153. * schema method
  154. *
  155. * @return void
  156. */
  157. public function setSchema($schema) {
  158. $this->_schema = $schema;
  159. }
  160. }
  161. /**
  162. * ContactNonStandardPk class
  163. *
  164. * @package cake
  165. * @package Cake.Test.Case.View.Helper
  166. */
  167. class ContactNonStandardPk extends Contact {
  168. /**
  169. * primaryKey property
  170. *
  171. * @var string 'pk'
  172. */
  173. public $primaryKey = 'pk';
  174. /**
  175. * schema method
  176. *
  177. * @return void
  178. */
  179. public function schema($field = false) {
  180. $this->_schema = parent::schema();
  181. $this->_schema['pk'] = $this->_schema['id'];
  182. unset($this->_schema['id']);
  183. return $this->_schema;
  184. }
  185. }
  186. /**
  187. * ContactTag class
  188. *
  189. * @package cake
  190. * @package Cake.Test.Case.View.Helper
  191. */
  192. class ContactTag extends Model {
  193. /**
  194. * useTable property
  195. *
  196. * @var bool false
  197. */
  198. public $useTable = false;
  199. /**
  200. * schema definition
  201. *
  202. * @var array
  203. */
  204. protected $_schema = array(
  205. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  206. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  207. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  208. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  209. );
  210. }
  211. /**
  212. * UserForm class
  213. *
  214. * @package cake
  215. * @package Cake.Test.Case.View.Helper
  216. */
  217. class UserForm extends CakeTestModel {
  218. /**
  219. * useTable property
  220. *
  221. * @var bool false
  222. */
  223. public $useTable = false;
  224. /**
  225. * hasMany property
  226. *
  227. * @var array
  228. */
  229. public $hasMany = array(
  230. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  231. ));
  232. /**
  233. * schema definition
  234. *
  235. * @var array
  236. */
  237. protected $_schema = array(
  238. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  239. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  240. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  241. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  242. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  243. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  244. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  245. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  246. );
  247. }
  248. /**
  249. * OpenidUrl class
  250. *
  251. * @package cake
  252. * @package Cake.Test.Case.View.Helper
  253. */
  254. class OpenidUrl extends CakeTestModel {
  255. /**
  256. * useTable property
  257. *
  258. * @var bool false
  259. */
  260. public $useTable = false;
  261. /**
  262. * belongsTo property
  263. *
  264. * @var array
  265. */
  266. public $belongsTo = array('UserForm' => array(
  267. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  268. ));
  269. /**
  270. * validate property
  271. *
  272. * @var array
  273. */
  274. public $validate = array('openid_not_registered' => array());
  275. /**
  276. * schema method
  277. *
  278. * @var array
  279. */
  280. protected $_schema = array(
  281. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  282. 'user_form_id' => array(
  283. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  284. ),
  285. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  286. );
  287. /**
  288. * beforeValidate method
  289. *
  290. * @return void
  291. */
  292. public function beforeValidate($options = array()) {
  293. $this->invalidate('openid_not_registered');
  294. return true;
  295. }
  296. }
  297. /**
  298. * ValidateUser class
  299. *
  300. * @package cake
  301. * @package Cake.Test.Case.View.Helper
  302. */
  303. class ValidateUser extends CakeTestModel {
  304. /**
  305. * useTable property
  306. *
  307. * @var bool false
  308. */
  309. public $useTable = false;
  310. /**
  311. * hasOne property
  312. *
  313. * @var array
  314. */
  315. public $hasOne = array('ValidateProfile' => array(
  316. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  317. ));
  318. /**
  319. * schema method
  320. *
  321. * @var array
  322. */
  323. protected $_schema = array(
  324. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  325. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  326. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  327. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  328. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  329. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  330. );
  331. /**
  332. * beforeValidate method
  333. *
  334. * @return void
  335. */
  336. public function beforeValidate($options = array()) {
  337. $this->invalidate('email');
  338. return false;
  339. }
  340. }
  341. /**
  342. * ValidateProfile class
  343. *
  344. * @package cake
  345. * @package Cake.Test.Case.View.Helper
  346. */
  347. class ValidateProfile extends CakeTestModel {
  348. /**
  349. * useTable property
  350. *
  351. * @var bool false
  352. */
  353. public $useTable = false;
  354. /**
  355. * schema property
  356. *
  357. * @var array
  358. */
  359. protected $_schema = array(
  360. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  361. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  362. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  363. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  364. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  365. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  366. );
  367. /**
  368. * hasOne property
  369. *
  370. * @var array
  371. */
  372. public $hasOne = array('ValidateItem' => array(
  373. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  374. ));
  375. /**
  376. * belongsTo property
  377. *
  378. * @var array
  379. */
  380. public $belongsTo = array('ValidateUser' => array(
  381. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  382. ));
  383. /**
  384. * beforeValidate method
  385. *
  386. * @return void
  387. */
  388. public function beforeValidate($options = array()) {
  389. $this->invalidate('full_name');
  390. $this->invalidate('city');
  391. return false;
  392. }
  393. }
  394. /**
  395. * ValidateItem class
  396. *
  397. * @package cake
  398. * @package Cake.Test.Case.View.Helper
  399. */
  400. class ValidateItem extends CakeTestModel {
  401. /**
  402. * useTable property
  403. *
  404. * @var bool false
  405. */
  406. public $useTable = false;
  407. /**
  408. * schema property
  409. *
  410. * @var array
  411. */
  412. protected $_schema = array(
  413. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  414. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  415. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  416. 'description' => array(
  417. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  418. ),
  419. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  420. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  421. );
  422. /**
  423. * belongsTo property
  424. *
  425. * @var array
  426. */
  427. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  428. /**
  429. * beforeValidate method
  430. *
  431. * @return void
  432. */
  433. public function beforeValidate($options = array()) {
  434. $this->invalidate('description');
  435. return false;
  436. }
  437. }
  438. /**
  439. * TestMail class
  440. *
  441. * @package cake
  442. * @package Cake.Test.Case.View.Helper
  443. */
  444. class TestMail extends CakeTestModel {
  445. /**
  446. * useTable property
  447. *
  448. * @var bool false
  449. */
  450. public $useTable = false;
  451. }
  452. /**
  453. * FormHelperTest class
  454. *
  455. * @package cake
  456. * @subpackage Cake.Test.Case.View.Helper
  457. * @property FormHelper $Form
  458. */
  459. class FormHelperTest extends CakeTestCase {
  460. /**
  461. * Fixtures to be used
  462. *
  463. * @var array
  464. */
  465. public $fixtures = array('core.post');
  466. /**
  467. * Do not load the fixtures by default
  468. *
  469. * @var boolean
  470. */
  471. public $autoFixtures = false;
  472. /**
  473. * setUp method
  474. *
  475. * @return void
  476. */
  477. public function setUp() {
  478. parent::setUp();
  479. Configure::write('App.base', '');
  480. Configure::delete('Asset');
  481. $this->Controller = new ContactTestController();
  482. $this->View = new View($this->Controller);
  483. $this->Form = new FormHelper($this->View);
  484. $this->Form->Html = new HtmlHelper($this->View);
  485. $this->Form->request = new CakeRequest('contacts/add', false);
  486. $this->Form->request->here = '/contacts/add';
  487. $this->Form->request['action'] = 'add';
  488. $this->Form->request->webroot = '';
  489. $this->Form->request->base = '';
  490. ClassRegistry::addObject('Contact', new Contact());
  491. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  492. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  493. ClassRegistry::addObject('User', new UserForm());
  494. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  495. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  496. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  497. $this->oldSalt = Configure::read('Security.salt');
  498. $this->dateRegex = array(
  499. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  500. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  501. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  502. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  503. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  504. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  505. );
  506. Configure::write('Security.salt', 'foo!');
  507. }
  508. /**
  509. * tearDown method
  510. *
  511. * @return void
  512. */
  513. public function tearDown() {
  514. parent::tearDown();
  515. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  516. Configure::write('Security.salt', $this->oldSalt);
  517. }
  518. /**
  519. * testFormCreateWithSecurity method
  520. *
  521. * Test form->create() with security key.
  522. *
  523. * @return void
  524. */
  525. public function testCreateWithSecurity() {
  526. $this->Form->request['_Token'] = array('key' => 'testKey');
  527. $encoding = strtolower(Configure::read('App.encoding'));
  528. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  529. $expected = array(
  530. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  531. 'div' => array('style' => 'display:none;'),
  532. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  533. array('input' => array(
  534. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  535. )),
  536. '/div'
  537. );
  538. $this->assertTags($result, $expected);
  539. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  540. $expected['form']['id'] = 'MyForm';
  541. $this->assertTags($result, $expected);
  542. }
  543. /**
  544. * testFormCreateGetNoSecurity method
  545. *
  546. * Test form->create() with no security key as its a get form
  547. *
  548. * @return void
  549. */
  550. public function testCreateEndGetNoSecurity() {
  551. $this->Form->request['_Token'] = array('key' => 'testKey');
  552. $encoding = strtolower(Configure::read('App.encoding'));
  553. $result = $this->Form->create('Contact', array('type' => 'get', 'url' => '/contacts/add'));
  554. $this->assertNotContains('Token', $result);
  555. $result = $this->Form->end('Save');
  556. $this->assertNotContains('Token', $result);
  557. }
  558. /**
  559. * test that create() clears the fields property so it starts fresh
  560. *
  561. * @return void
  562. */
  563. public function testCreateClearingFields() {
  564. $this->Form->fields = array('model_id');
  565. $this->Form->create('Contact');
  566. $this->assertEquals(array(), $this->Form->fields);
  567. }
  568. /**
  569. * Tests form hash generation with model-less data
  570. *
  571. * @return void
  572. */
  573. public function testValidateHashNoModel() {
  574. $this->Form->request['_Token'] = array('key' => 'foo');
  575. $result = $this->Form->secure(array('anything'));
  576. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  577. }
  578. /**
  579. * Tests that models with identical field names get resolved properly
  580. *
  581. * @return void
  582. */
  583. public function testDuplicateFieldNameResolution() {
  584. $result = $this->Form->create('ValidateUser');
  585. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  586. $result = $this->Form->input('ValidateItem.name');
  587. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  588. $result = $this->Form->input('ValidateUser.name');
  589. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  590. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  591. $this->assertRegExp('/type="text"/', $result);
  592. $result = $this->Form->input('ValidateItem.name');
  593. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  594. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  595. $this->assertRegExp('/<textarea/', $result);
  596. $result = $this->Form->input('name');
  597. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  598. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  599. $this->assertRegExp('/type="text"/', $result);
  600. }
  601. /**
  602. * Tests that hidden fields generated for checkboxes don't get locked
  603. *
  604. * @return void
  605. */
  606. public function testNoCheckboxLocking() {
  607. $this->Form->request['_Token'] = array('key' => 'foo');
  608. $this->assertSame(array(), $this->Form->fields);
  609. $this->Form->checkbox('check', array('value' => '1'));
  610. $this->assertSame($this->Form->fields, array('check'));
  611. }
  612. /**
  613. * testFormSecurityFields method
  614. *
  615. * Test generation of secure form hash generation.
  616. *
  617. * @return void
  618. */
  619. public function testFormSecurityFields() {
  620. $key = 'testKey';
  621. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  622. $this->Form->request['_Token'] = array('key' => $key);
  623. $result = $this->Form->secure($fields);
  624. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  625. $hash .= ':' . 'Model.valid';
  626. $hash = urlencode($hash);
  627. $expected = array(
  628. 'div' => array('style' => 'display:none;'),
  629. array('input' => array(
  630. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  631. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  632. )),
  633. array('input' => array(
  634. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  635. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  636. )),
  637. '/div'
  638. );
  639. $this->assertTags($result, $expected);
  640. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  641. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  642. $result = $this->Form->secure($fields);
  643. $expected = array(
  644. 'div' => array('class' => 'hidden'),
  645. array('input' => array(
  646. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  647. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  648. )),
  649. array('input' => array(
  650. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  651. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  652. )),
  653. '/div'
  654. );
  655. $this->assertTags($result, $expected);
  656. }
  657. /**
  658. * Tests correct generation of number fields for double and float fields
  659. *
  660. * @return void
  661. */
  662. public function testTextFieldGenerationForFloats() {
  663. $model = ClassRegistry::getObject('Contact');
  664. $model->setSchema(array('foo' => array(
  665. 'type' => 'float',
  666. 'null' => false,
  667. 'default' => null,
  668. 'length' => 10
  669. )));
  670. $this->Form->create('Contact');
  671. $result = $this->Form->input('foo');
  672. $expected = array(
  673. 'div' => array('class' => 'input number'),
  674. 'label' => array('for' => 'ContactFoo'),
  675. 'Foo',
  676. '/label',
  677. array('input' => array(
  678. 'type' => 'number',
  679. 'name' => 'data[Contact][foo]',
  680. 'id' => 'ContactFoo',
  681. 'step' => 'any'
  682. )),
  683. '/div'
  684. );
  685. $this->assertTags($result, $expected);
  686. $result = $this->Form->input('foo', array('step' => 0.5));
  687. $expected = array(
  688. 'div' => array('class' => 'input number'),
  689. 'label' => array('for' => 'ContactFoo'),
  690. 'Foo',
  691. '/label',
  692. array('input' => array(
  693. 'type' => 'number',
  694. 'name' => 'data[Contact][foo]',
  695. 'id' => 'ContactFoo',
  696. 'step' => '0.5'
  697. )),
  698. '/div'
  699. );
  700. $this->assertTags($result, $expected);
  701. }
  702. /**
  703. * Tests correct generation of number fields for integer fields
  704. *
  705. * @access public
  706. * @return void
  707. */
  708. public function testTextFieldTypeNumberGenerationForIntegers() {
  709. $model = ClassRegistry::getObject('Contact');
  710. $model->setSchema(array('foo' => array(
  711. 'type' => 'integer',
  712. 'null' => false,
  713. 'default' => null,
  714. 'length' => null
  715. )));
  716. $this->Form->create('Contact');
  717. $result = $this->Form->input('foo');
  718. $expected = array(
  719. 'div' => array('class' => 'input number'),
  720. 'label' => array('for' => 'ContactFoo'),
  721. 'Foo',
  722. '/label',
  723. array('input' => array(
  724. 'type' => 'number', 'name' => 'data[Contact][foo]',
  725. 'id' => 'ContactFoo'
  726. )),
  727. '/div'
  728. );
  729. $this->assertTags($result, $expected);
  730. }
  731. /**
  732. * testFormSecurityMultipleFields method
  733. *
  734. * Test secure() with multiple row form. Ensure hash is correct.
  735. *
  736. * @return void
  737. */
  738. public function testFormSecurityMultipleFields() {
  739. $key = 'testKey';
  740. $fields = array(
  741. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  742. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  743. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  744. );
  745. $this->Form->request['_Token'] = array('key' => $key);
  746. $result = $this->Form->secure($fields);
  747. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  748. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  749. $expected = array(
  750. 'div' => array('style' => 'display:none;'),
  751. array('input' => array(
  752. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  753. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  754. )),
  755. array('input' => array(
  756. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  757. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  758. )),
  759. '/div'
  760. );
  761. $this->assertTags($result, $expected);
  762. }
  763. /**
  764. * testFormSecurityMultipleSubmitButtons
  765. *
  766. * test form submit generation and ensure that _Token is only created on end()
  767. *
  768. * @return void
  769. */
  770. public function testFormSecurityMultipleSubmitButtons() {
  771. $key = 'testKey';
  772. $this->Form->request['_Token'] = array('key' => $key);
  773. $this->Form->create('Addresses');
  774. $this->Form->input('Address.title');
  775. $this->Form->input('Address.first_name');
  776. $result = $this->Form->submit('Save', array('name' => 'save'));
  777. $expected = array(
  778. 'div' => array('class' => 'submit'),
  779. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  780. '/div',
  781. );
  782. $this->assertTags($result, $expected);
  783. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  784. $expected = array(
  785. 'div' => array('class' => 'submit'),
  786. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  787. '/div',
  788. );
  789. $this->assertTags($result, $expected);
  790. $result = $this->Form->end(null);
  791. $expected = array(
  792. 'div' => array('style' => 'display:none;'),
  793. array('input' => array(
  794. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  795. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  796. )),
  797. array('input' => array(
  798. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  799. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  800. )),
  801. '/div'
  802. );
  803. $this->assertTags($result, $expected);
  804. }
  805. /**
  806. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  807. *
  808. * @return void
  809. */
  810. public function testSecurityButtonNestedNamed() {
  811. $key = 'testKey';
  812. $this->Form->request['_Token'] = array('key' => $key);
  813. $this->Form->create('Addresses');
  814. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  815. $result = $this->Form->unlockField();
  816. $this->assertEquals(array('Address.button'), $result);
  817. }
  818. /**
  819. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  820. *
  821. * @return void
  822. */
  823. public function testSecuritySubmitNestedNamed() {
  824. $key = 'testKey';
  825. $this->Form->request['_Token'] = array('key' => $key);
  826. $this->Form->create('Addresses');
  827. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  828. $result = $this->Form->unlockField();
  829. $this->assertEquals(array('Address.button'), $result);
  830. }
  831. /**
  832. * Test that the correct fields are unlocked for image submits with no names.
  833. *
  834. * @return void
  835. */
  836. public function testSecuritySubmitImageNoName() {
  837. $key = 'testKey';
  838. $this->Form->request['_Token'] = array('key' => $key);
  839. $this->Form->create('User');
  840. $result = $this->Form->submit('save.png');
  841. $expected = array(
  842. 'div' => array('class' => 'submit'),
  843. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  844. '/div'
  845. );
  846. $this->assertTags($result, $expected);
  847. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  848. }
  849. /**
  850. * Test that the correct fields are unlocked for image submits with names.
  851. *
  852. * @return void
  853. */
  854. public function testSecuritySubmitImageName() {
  855. $key = 'testKey';
  856. $this->Form->request['_Token'] = array('key' => $key);
  857. $this->Form->create('User');
  858. $result = $this->Form->submit('save.png', array('name' => 'test'));
  859. $expected = array(
  860. 'div' => array('class' => 'submit'),
  861. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  862. '/div'
  863. );
  864. $this->assertTags($result, $expected);
  865. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  866. }
  867. /**
  868. * testFormSecurityMultipleInputFields method
  869. *
  870. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  871. *
  872. * @return void
  873. */
  874. public function testFormSecurityMultipleInputFields() {
  875. $key = 'testKey';
  876. $this->Form->request['_Token'] = array('key' => $key);
  877. $this->Form->create('Addresses');
  878. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  879. $this->Form->input('Addresses.0.title');
  880. $this->Form->input('Addresses.0.first_name');
  881. $this->Form->input('Addresses.0.last_name');
  882. $this->Form->input('Addresses.0.address');
  883. $this->Form->input('Addresses.0.city');
  884. $this->Form->input('Addresses.0.phone');
  885. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  886. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  887. $this->Form->input('Addresses.1.title');
  888. $this->Form->input('Addresses.1.first_name');
  889. $this->Form->input('Addresses.1.last_name');
  890. $this->Form->input('Addresses.1.address');
  891. $this->Form->input('Addresses.1.city');
  892. $this->Form->input('Addresses.1.phone');
  893. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  894. $result = $this->Form->secure($this->Form->fields);
  895. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  896. $expected = array(
  897. 'div' => array('style' => 'display:none;'),
  898. array('input' => array(
  899. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  900. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  901. )),
  902. array('input' => array(
  903. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  904. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  905. )),
  906. '/div'
  907. );
  908. $this->assertTags($result, $expected);
  909. }
  910. /**
  911. * Test form security with Model.field.0 style inputs
  912. *
  913. * @return void
  914. */
  915. public function testFormSecurityArrayFields() {
  916. $key = 'testKey';
  917. $this->Form->request->params['_Token']['key'] = $key;
  918. $this->Form->create('Address');
  919. $this->Form->input('Address.primary.1');
  920. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  921. $this->Form->input('Address.secondary.1.0');
  922. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  923. }
  924. /**
  925. * testFormSecurityMultipleInputDisabledFields method
  926. *
  927. * test secure form generation with multiple records and disabled fields.
  928. *
  929. * @return void
  930. */
  931. public function testFormSecurityMultipleInputDisabledFields() {
  932. $key = 'testKey';
  933. $this->Form->request->params['_Token'] = array(
  934. 'key' => $key,
  935. 'unlockedFields' => array('first_name', 'address')
  936. );
  937. $this->Form->create();
  938. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  939. $this->Form->input('Addresses.0.title');
  940. $this->Form->input('Addresses.0.first_name');
  941. $this->Form->input('Addresses.0.last_name');
  942. $this->Form->input('Addresses.0.address');
  943. $this->Form->input('Addresses.0.city');
  944. $this->Form->input('Addresses.0.phone');
  945. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  946. $this->Form->input('Addresses.1.title');
  947. $this->Form->input('Addresses.1.first_name');
  948. $this->Form->input('Addresses.1.last_name');
  949. $this->Form->input('Addresses.1.address');
  950. $this->Form->input('Addresses.1.city');
  951. $this->Form->input('Addresses.1.phone');
  952. $result = $this->Form->secure($this->Form->fields);
  953. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  954. $expected = array(
  955. 'div' => array('style' => 'display:none;'),
  956. array('input' => array(
  957. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  958. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  959. )),
  960. array('input' => array(
  961. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  962. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  963. )),
  964. '/div'
  965. );
  966. $this->assertTags($result, $expected);
  967. }
  968. /**
  969. * testFormSecurityInputDisabledFields method
  970. *
  971. * Test single record form with disabled fields.
  972. *
  973. * @return void
  974. */
  975. public function testFormSecurityInputUnlockedFields() {
  976. $key = 'testKey';
  977. $this->Form->request['_Token'] = array(
  978. 'key' => $key,
  979. 'unlockedFields' => array('first_name', 'address')
  980. );
  981. $this->Form->create();
  982. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  983. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  984. $this->Form->input('Addresses.title');
  985. $this->Form->input('Addresses.first_name');
  986. $this->Form->input('Addresses.last_name');
  987. $this->Form->input('Addresses.address');
  988. $this->Form->input('Addresses.city');
  989. $this->Form->input('Addresses.phone');
  990. $result = $this->Form->fields;
  991. $expected = array(
  992. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  993. 'Addresses.city', 'Addresses.phone'
  994. );
  995. $this->assertEquals($expected, $result);
  996. $result = $this->Form->secure($expected);
  997. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  998. $expected = array(
  999. 'div' => array('style' => 'display:none;'),
  1000. array('input' => array(
  1001. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1002. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1003. )),
  1004. array('input' => array(
  1005. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1006. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1007. )),
  1008. '/div'
  1009. );
  1010. $this->assertTags($result, $expected);
  1011. }
  1012. /**
  1013. * test securing inputs with custom name attributes.
  1014. *
  1015. * @return void
  1016. */
  1017. public function testFormSecureWithCustomNameAttribute() {
  1018. $this->Form->request->params['_Token']['key'] = 'testKey';
  1019. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1020. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1021. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1022. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1023. }
  1024. /**
  1025. * testFormSecuredInput method
  1026. *
  1027. * Test generation of entire secure form, assertions made on input() output.
  1028. *
  1029. * @return void
  1030. */
  1031. public function testFormSecuredInput() {
  1032. $this->Form->request['_Token'] = array('key' => 'testKey');
  1033. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1034. $encoding = strtolower(Configure::read('App.encoding'));
  1035. $expected = array(
  1036. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1037. 'div' => array('style' => 'display:none;'),
  1038. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1039. array('input' => array(
  1040. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1041. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1042. )),
  1043. '/div'
  1044. );
  1045. $this->assertTags($result, $expected);
  1046. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1047. $expected = array(
  1048. 'div' => array('class' => 'input text'),
  1049. 'label' => array('for' => 'UserFormPublished'),
  1050. 'Published',
  1051. '/label',
  1052. array('input' => array(
  1053. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1054. 'id' => 'UserFormPublished'
  1055. )),
  1056. '/div'
  1057. );
  1058. $this->assertTags($result, $expected);
  1059. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1060. $expected = array(
  1061. 'div' => array('class' => 'input text'),
  1062. 'label' => array('for' => 'UserFormOther'),
  1063. 'Other',
  1064. '/label',
  1065. array('input' => array(
  1066. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1067. 'id' => 'UserFormOther'
  1068. )),
  1069. '/div'
  1070. );
  1071. $this->assertTags($result, $expected);
  1072. $result = $this->Form->hidden('UserForm.stuff');
  1073. $expected = array(
  1074. 'input' => array(
  1075. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1076. 'id' => 'UserFormStuff'
  1077. ));
  1078. $this->assertTags($result, $expected);
  1079. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1080. $expected = array('input' => array(
  1081. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1082. 'value' => '0', 'id' => 'UserFormHidden'
  1083. ));
  1084. $this->assertTags($result, $expected);
  1085. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1086. $expected = array(
  1087. 'div' => array('class' => 'input checkbox'),
  1088. array('input' => array(
  1089. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1090. 'value' => '0', 'id' => 'UserFormSomething_'
  1091. )),
  1092. array('input' => array(
  1093. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1094. 'value' => '1', 'id' => 'UserFormSomething'
  1095. )),
  1096. 'label' => array('for' => 'UserFormSomething'),
  1097. 'Something',
  1098. '/label',
  1099. '/div'
  1100. );
  1101. $this->assertTags($result, $expected);
  1102. $result = $this->Form->fields;
  1103. $expected = array(
  1104. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1105. 'UserForm.hidden' => '0', 'UserForm.something'
  1106. );
  1107. $this->assertEquals($expected, $result);
  1108. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1109. $result = $this->Form->secure($this->Form->fields);
  1110. $expected = array(
  1111. 'div' => array('style' => 'display:none;'),
  1112. array('input' => array(
  1113. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1114. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1115. )),
  1116. array('input' => array(
  1117. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1118. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1119. )),
  1120. '/div'
  1121. );
  1122. $this->assertTags($result, $expected);
  1123. }
  1124. /**
  1125. * Test secured inputs with custom names.
  1126. *
  1127. * @return void
  1128. */
  1129. public function testSecuredInputCustomName() {
  1130. $this->Form->request['_Token'] = array('key' => 'testKey');
  1131. $this->assertEquals(array(), $this->Form->fields);
  1132. $this->Form->input('text_input', array(
  1133. 'name' => 'data[Option][General.default_role]',
  1134. ));
  1135. $expected = array('Option.General.default_role');
  1136. $this->assertEquals($expected, $this->Form->fields);
  1137. $this->Form->input('select_box', array(
  1138. 'name' => 'data[Option][General.select_role]',
  1139. 'type' => 'select',
  1140. 'options' => array(1, 2),
  1141. ));
  1142. $expected = array('Option.General.default_role', 'Option.General.select_role');
  1143. $this->assertEquals($expected, $this->Form->fields);
  1144. }
  1145. /**
  1146. * Tests that the correct keys are added to the field hash index
  1147. *
  1148. * @return void
  1149. */
  1150. public function testFormSecuredFileInput() {
  1151. $this->Form->request['_Token'] = array('key' => 'testKey');
  1152. $this->assertEquals(array(), $this->Form->fields);
  1153. $this->Form->file('Attachment.file');
  1154. $expected = array(
  1155. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1156. 'Attachment.file.error', 'Attachment.file.size'
  1157. );
  1158. $this->assertEquals($expected, $this->Form->fields);
  1159. }
  1160. /**
  1161. * test that multiple selects keys are added to field hash
  1162. *
  1163. * @return void
  1164. */
  1165. public function testFormSecuredMultipleSelect() {
  1166. $this->Form->request['_Token'] = array('key' => 'testKey');
  1167. $this->assertEquals(array(), $this->Form->fields);
  1168. $options = array('1' => 'one', '2' => 'two');
  1169. $this->Form->select('Model.select', $options);
  1170. $expected = array('Model.select');
  1171. $this->assertEquals($expected, $this->Form->fields);
  1172. $this->Form->fields = array();
  1173. $this->Form->select('Model.select', $options, array('multiple' => true));
  1174. $this->assertEquals($expected, $this->Form->fields);
  1175. }
  1176. /**
  1177. * testFormSecuredRadio method
  1178. *
  1179. * @return void
  1180. */
  1181. public function testFormSecuredRadio() {
  1182. $this->Form->request['_Token'] = array('key' => 'testKey');
  1183. $this->assertEquals(array(), $this->Form->fields);
  1184. $options = array('1' => 'option1', '2' => 'option2');
  1185. $this->Form->radio('Test.test', $options);
  1186. $expected = array('Test.test');
  1187. $this->assertEquals($expected, $this->Form->fields);
  1188. }
  1189. /**
  1190. * Test that when disabled is in a list based attribute array it works.
  1191. *
  1192. * @return void
  1193. */
  1194. public function testFormSecuredAndDisabledNotAssoc() {
  1195. $this->Form->request['_Token'] = array('key' => 'testKey');
  1196. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1197. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1198. $this->Form->text('Model.text', array('disabled'));
  1199. $this->Form->textarea('Model.textarea', array('disabled'));
  1200. $this->Form->password('Model.password', array('disabled'));
  1201. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1202. $expected = array(
  1203. 'Model.radio' => ''
  1204. );
  1205. $this->assertEquals($expected, $this->Form->fields);
  1206. }
  1207. /**
  1208. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1209. * hashing.
  1210. *
  1211. * @return void
  1212. */
  1213. public function testFormSecuredAndDisabled() {
  1214. $this->Form->request['_Token'] = array('key' => 'testKey');
  1215. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1216. $this->Form->text('Model.text', array('disabled' => true));
  1217. $this->Form->password('Model.text', array('disabled' => true));
  1218. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1219. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1220. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1221. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1222. $this->Form->month('Model.month', array('disabled' => true));
  1223. $this->Form->day('Model.day', array('disabled' => true));
  1224. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1225. $this->Form->minute('Model.minute', array('disabled' => true));
  1226. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1227. $expected = array(
  1228. 'Model.radio' => ''
  1229. );
  1230. $this->assertEquals($expected, $this->Form->fields);
  1231. }
  1232. /**
  1233. * testDisableSecurityUsingForm method
  1234. *
  1235. * @return void
  1236. */
  1237. public function testDisableSecurityUsingForm() {
  1238. $this->Form->request['_Token'] = array(
  1239. 'key' => 'testKey',
  1240. 'disabledFields' => array()
  1241. );
  1242. $this->Form->create();
  1243. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1244. $this->Form->input('Addresses.title');
  1245. $this->Form->input('Addresses.first_name', array('secure' => false));
  1246. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1247. $this->Form->input('Addresses.zip', array(
  1248. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1249. ));
  1250. $result = $this->Form->fields;
  1251. $expected = array(
  1252. 'Addresses.id' => '123456', 'Addresses.title',
  1253. );
  1254. $this->assertEquals($expected, $result);
  1255. }
  1256. /**
  1257. * test disableField
  1258. *
  1259. * @return void
  1260. */
  1261. public function testUnlockFieldAddsToList() {
  1262. $this->Form->request['_Token'] = array(
  1263. 'key' => 'testKey',
  1264. 'unlockedFields' => array()
  1265. );
  1266. $this->Form->create('Contact');
  1267. $this->Form->unlockField('Contact.name');
  1268. $this->Form->text('Contact.name');
  1269. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1270. $this->assertEquals(array(), $this->Form->fields);
  1271. }
  1272. /**
  1273. * test unlockField removing from fields array.
  1274. *
  1275. * @return void
  1276. */
  1277. public function testUnlockFieldRemovingFromFields() {
  1278. $this->Form->request['_Token'] = array(
  1279. 'key' => 'testKey',
  1280. 'unlockedFields' => array()
  1281. );
  1282. $this->Form->create('Contact');
  1283. $this->Form->hidden('Contact.id', array('value' => 1));
  1284. $this->Form->text('Contact.name');
  1285. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1286. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1287. $this->Form->unlockField('Contact.name');
  1288. $this->Form->unlockField('Contact.id');
  1289. $this->assertEquals(array(), $this->Form->fields);
  1290. }
  1291. /**
  1292. * testTagIsInvalid method
  1293. *
  1294. * @return void
  1295. */
  1296. public function testTagIsInvalid() {
  1297. $Contact = ClassRegistry::getObject('Contact');
  1298. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1299. $this->Form->setEntity('Contact.0.email');
  1300. $result = $this->Form->tagIsInvalid();
  1301. $this->assertEquals($expected, $result);
  1302. $this->Form->setEntity('Contact.1.email');
  1303. $result = $this->Form->tagIsInvalid();
  1304. $this->assertFalse($result);
  1305. $this->Form->setEntity('Contact.0.name');
  1306. $result = $this->Form->tagIsInvalid();
  1307. $this->assertFalse($result);
  1308. }
  1309. /**
  1310. * Test tagIsInvalid with validation errors from a saveMany
  1311. *
  1312. * @return void
  1313. */
  1314. public function testTagIsInvalidSaveMany() {
  1315. $Contact = ClassRegistry::getObject('Contact');
  1316. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1317. $this->Form->create('Contact');
  1318. $this->Form->setEntity('0.email');
  1319. $result = $this->Form->tagIsInvalid();
  1320. $this->assertEquals($expected, $result);
  1321. $this->Form->setEntity('0.Contact.email');
  1322. $result = $this->Form->tagIsInvalid();
  1323. $this->assertEquals($expected, $result);
  1324. }
  1325. /**
  1326. * Test validation errors.
  1327. *
  1328. * @return void
  1329. */
  1330. public function testPasswordValidation() {
  1331. $Contact = ClassRegistry::getObject('Contact');
  1332. $Contact->validationErrors['password'] = array('Please provide a password');
  1333. $result = $this->Form->input('Contact.password');
  1334. $expected = array(
  1335. 'div' => array('class' => 'input password error'),
  1336. 'label' => array('for' => 'ContactPassword'),
  1337. 'Password',
  1338. '/label',
  1339. 'input' => array(
  1340. 'type' => 'password', 'name' => 'data[Contact][password]',
  1341. 'id' => 'ContactPassword', 'class' => 'form-error'
  1342. ),
  1343. array('div' => array('class' => 'error-message')),
  1344. 'Please provide a password',
  1345. '/div',
  1346. '/div'
  1347. );
  1348. $this->assertTags($result, $expected);
  1349. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  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. '/div'
  1360. );
  1361. $this->assertTags($result, $expected);
  1362. }
  1363. /**
  1364. * Test validation errors, when validation message is an empty string.
  1365. *
  1366. * @access public
  1367. * @return void
  1368. */
  1369. public function testEmptyErrorValidation() {
  1370. $this->Form->validationErrors['Contact']['password'] = '';
  1371. $result = $this->Form->input('Contact.password');
  1372. $expected = array(
  1373. 'div' => array('class' => 'input password error'),
  1374. 'label' => array('for' => 'ContactPassword'),
  1375. 'Password',
  1376. '/label',
  1377. 'input' => array(
  1378. 'type' => 'password', 'name' => 'data[Contact][password]',
  1379. 'id' => 'ContactPassword', 'class' => 'form-error'
  1380. ),
  1381. array('div' => array('class' => 'error-message')),
  1382. array(),
  1383. '/div',
  1384. '/div'
  1385. );
  1386. $this->assertTags($result, $expected);
  1387. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1388. $expected = array(
  1389. 'div' => array('class' => 'input password error'),
  1390. 'label' => array('for' => 'ContactPassword'),
  1391. 'Password',
  1392. '/label',
  1393. 'input' => array(
  1394. 'type' => 'password', 'name' => 'data[Contact][password]',
  1395. 'id' => 'ContactPassword', 'class' => 'form-error'
  1396. ),
  1397. '/div'
  1398. );
  1399. $this->assertTags($result, $expected);
  1400. }
  1401. /**
  1402. * Test validation errors, when calling input() overriding validation message by an empty string.
  1403. *
  1404. * @access public
  1405. * @return void
  1406. */
  1407. public function testEmptyInputErrorValidation() {
  1408. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1409. $result = $this->Form->input('Contact.password', array('error' => ''));
  1410. $expected = array(
  1411. 'div' => array('class' => 'input password error'),
  1412. 'label' => array('for' => 'ContactPassword'),
  1413. 'Password',
  1414. '/label',
  1415. 'input' => array(
  1416. 'type' => 'password', 'name' => 'data[Contact][password]',
  1417. 'id' => 'ContactPassword', 'class' => 'form-error'
  1418. ),
  1419. array('div' => array('class' => 'error-message')),
  1420. array(),
  1421. '/div',
  1422. '/div'
  1423. );
  1424. $this->assertTags($result, $expected);
  1425. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1426. $expected = array(
  1427. 'div' => array('class' => 'input password error'),
  1428. 'label' => array('for' => 'ContactPassword'),
  1429. 'Password',
  1430. '/label',
  1431. 'input' => array(
  1432. 'type' => 'password', 'name' => 'data[Contact][password]',
  1433. 'id' => 'ContactPassword', 'class' => 'form-error'
  1434. ),
  1435. '/div'
  1436. );
  1437. $this->assertTags($result, $expected);
  1438. }
  1439. /**
  1440. * testFormValidationAssociated method
  1441. *
  1442. * test display of form errors in conjunction with model::validates.
  1443. *
  1444. * @return void
  1445. */
  1446. public function testFormValidationAssociated() {
  1447. $this->UserForm = ClassRegistry::getObject('UserForm');
  1448. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1449. $data = array(
  1450. 'UserForm' => array('name' => 'user'),
  1451. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1452. );
  1453. $result = $this->UserForm->OpenidUrl->create($data);
  1454. $this->assertFalse(empty($result));
  1455. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1456. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1457. $encoding = strtolower(Configure::read('App.encoding'));
  1458. $expected = array(
  1459. 'form' => array(
  1460. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1461. 'accept-charset' => $encoding
  1462. ),
  1463. 'div' => array('style' => 'display:none;'),
  1464. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1465. '/div'
  1466. );
  1467. $this->assertTags($result, $expected);
  1468. $result = $this->Form->error(
  1469. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1470. );
  1471. $this->assertEquals('Error, not registered', $result);
  1472. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1473. }
  1474. /**
  1475. * testFormValidationAssociatedFirstLevel method
  1476. *
  1477. * test form error display with associated model.
  1478. *
  1479. * @return void
  1480. */
  1481. public function testFormValidationAssociatedFirstLevel() {
  1482. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1483. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1484. $data = array(
  1485. 'ValidateUser' => array('name' => 'mariano'),
  1486. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1487. );
  1488. $result = $this->ValidateUser->create($data);
  1489. $this->assertFalse(empty($result));
  1490. $this->assertFalse($this->ValidateUser->validates());
  1491. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1492. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1493. $encoding = strtolower(Configure::read('App.encoding'));
  1494. $expected = array(
  1495. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1496. 'div' => array('style' => 'display:none;'),
  1497. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1498. '/div'
  1499. );
  1500. $this->assertTags($result, $expected);
  1501. $result = $this->Form->error(
  1502. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1503. );
  1504. $this->assertEquals('Invalid email', $result);
  1505. $result = $this->Form->error(
  1506. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1507. );
  1508. $this->assertEquals('Invalid name', $result);
  1509. $result = $this->Form->error(
  1510. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1511. );
  1512. $this->assertEquals('Invalid city', $result);
  1513. unset($this->ValidateUser->ValidateProfile);
  1514. unset($this->ValidateUser);
  1515. }
  1516. /**
  1517. * testFormValidationAssociatedSecondLevel method
  1518. *
  1519. * test form error display with associated model.
  1520. *
  1521. * @return void
  1522. */
  1523. public function testFormValidationAssociatedSecondLevel() {
  1524. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1525. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1526. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1527. $data = array(
  1528. 'ValidateUser' => array('name' => 'mariano'),
  1529. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1530. 'ValidateItem' => array('name' => 'Item')
  1531. );
  1532. $result = $this->ValidateUser->create($data);
  1533. $this->assertFalse(empty($result));
  1534. $this->assertFalse($this->ValidateUser->validates());
  1535. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1536. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1537. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1538. $encoding = strtolower(Configure::read('App.encoding'));
  1539. $expected = array(
  1540. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1541. 'div' => array('style' => 'display:none;'),
  1542. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1543. '/div'
  1544. );
  1545. $this->assertTags($result, $expected);
  1546. $result = $this->Form->error(
  1547. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1548. );
  1549. $this->assertEquals('Invalid email', $result);
  1550. $result = $this->Form->error(
  1551. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1552. );
  1553. $this->assertEquals('Invalid name', $result);
  1554. $result = $this->Form->error(
  1555. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1556. );
  1557. $result = $this->Form->error(
  1558. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1559. );
  1560. $this->assertEquals('Invalid description', $result);
  1561. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1562. unset($this->ValidateUser->ValidateProfile);
  1563. unset($this->ValidateUser);
  1564. }
  1565. /**
  1566. * testFormValidationMultiRecord method
  1567. *
  1568. * test form error display with multiple records.
  1569. *
  1570. * @return void
  1571. */
  1572. public function testFormValidationMultiRecord() {
  1573. $Contact = ClassRegistry::getObject('Contact');
  1574. $Contact->validationErrors[2] = array(
  1575. 'name' => array('This field cannot be left blank')
  1576. );
  1577. $result = $this->Form->input('Contact.2.name');
  1578. $expected = array(
  1579. 'div' => array('class' => 'input text error'),
  1580. 'label' => array('for' => 'Contact2Name'),
  1581. 'Name',
  1582. '/label',
  1583. 'input' => array(
  1584. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1585. 'class' => 'form-error', 'maxlength' => 255
  1586. ),
  1587. array('div' => array('class' => 'error-message')),
  1588. 'This field cannot be left blank',
  1589. '/div',
  1590. '/div'
  1591. );
  1592. $this->assertTags($result, $expected);
  1593. }
  1594. /**
  1595. * testMultipleInputValidation method
  1596. *
  1597. * test multiple record form validation error display.
  1598. *
  1599. * @return void
  1600. */
  1601. public function testMultipleInputValidation() {
  1602. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1603. $Address->validationErrors[0] = array(
  1604. 'title' => array('This field cannot be empty'),
  1605. 'first_name' => array('This field cannot be empty')
  1606. );
  1607. $Address->validationErrors[1] = array(
  1608. 'last_name' => array('You must have a last name')
  1609. );
  1610. $this->Form->create();
  1611. $result = $this->Form->input('Address.0.title');
  1612. $expected = array(
  1613. 'div' => array('class'),
  1614. 'label' => array('for'),
  1615. 'preg:/[^<]+/',
  1616. '/label',
  1617. 'input' => array(
  1618. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1619. ),
  1620. array('div' => array('class' => 'error-message')),
  1621. 'This field cannot be empty',
  1622. '/div',
  1623. '/div'
  1624. );
  1625. $this->assertTags($result, $expected);
  1626. $result = $this->Form->input('Address.0.first_name');
  1627. $expected = array(
  1628. 'div' => array('class'),
  1629. 'label' => array('for'),
  1630. 'preg:/[^<]+/',
  1631. '/label',
  1632. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1633. array('div' => array('class' => 'error-message')),
  1634. 'This field cannot be empty',
  1635. '/div',
  1636. '/div'
  1637. );
  1638. $this->assertTags($result, $expected);
  1639. $result = $this->Form->input('Address.0.last_name');
  1640. $expected = array(
  1641. 'div' => array('class'),
  1642. 'label' => array('for'),
  1643. 'preg:/[^<]+/',
  1644. '/label',
  1645. 'input' => array('type' => 'text', 'name', 'id'),
  1646. '/div'
  1647. );
  1648. $this->assertTags($result, $expected);
  1649. $result = $this->Form->input('Address.1.last_name');
  1650. $expected = array(
  1651. 'div' => array('class'),
  1652. 'label' => array('for'),
  1653. 'preg:/[^<]+/',
  1654. '/label',
  1655. 'input' => array(
  1656. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1657. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1658. ),
  1659. array('div' => array('class' => 'error-message')),
  1660. 'You must have a last name',
  1661. '/div',
  1662. '/div'
  1663. );
  1664. $this->assertTags($result, $expected);
  1665. }
  1666. /**
  1667. * testInput method
  1668. *
  1669. * Test various incarnations of input().
  1670. *
  1671. * @return void
  1672. */
  1673. public function testInput() {
  1674. $result = $this->Form->input('ValidateUser.balance');
  1675. $expected = array(
  1676. 'div' => array('class'),
  1677. 'label' => array('for'),
  1678. 'Balance',
  1679. '/label',
  1680. 'input' => array('name', 'type' => 'number', 'id'),
  1681. '/div',
  1682. );
  1683. $this->assertTags($result, $expected);
  1684. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1685. $expected = array(
  1686. 'div' => array('class' => 'input email'),
  1687. 'label' => array('for' => 'custom'),
  1688. 'Email',
  1689. '/label',
  1690. array('input' => array(
  1691. 'type' => 'email', 'name' => 'data[Contact][email]',
  1692. 'id' => 'custom', 'maxlength' => 255
  1693. )),
  1694. '/div'
  1695. );
  1696. $this->assertTags($result, $expected);
  1697. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1698. $expected = array(
  1699. '<div',
  1700. 'label' => array('for' => 'ContactEmail'),
  1701. 'Email',
  1702. '/label',
  1703. array('input' => array(
  1704. 'type' => 'email', 'name' => 'data[Contact][email]',
  1705. 'id' => 'ContactEmail', 'maxlength' => 255
  1706. )),
  1707. '/div'
  1708. );
  1709. $this->assertTags($result, $expected);
  1710. $result = $this->Form->hidden('Contact.idontexist');
  1711. $expected = array('input' => array(
  1712. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1713. 'id' => 'ContactIdontexist'
  1714. ));
  1715. $this->assertTags($result, $expected);
  1716. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1717. $expected = array(
  1718. 'div' => array('class' => 'input text'),
  1719. 'label' => array('for' => 'ContactEmail'),
  1720. 'Email',
  1721. '/label',
  1722. array('input' => array(
  1723. 'type' => 'text', 'name' => 'data[Contact][email]',
  1724. 'id' => 'ContactEmail'
  1725. )),
  1726. '/div'
  1727. );
  1728. $this->assertTags($result, $expected);
  1729. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1730. $expected = array(
  1731. 'div' => array('class' => 'input text'),
  1732. 'label' => array('for' => 'Contact5Email'),
  1733. 'Email',
  1734. '/label',
  1735. array('input' => array(
  1736. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1737. 'id' => 'Contact5Email'
  1738. )),
  1739. '/div'
  1740. );
  1741. $this->assertTags($result, $expected);
  1742. $result = $this->Form->input('Contact.password');
  1743. $expected = array(
  1744. 'div' => array('class' => 'input password'),
  1745. 'label' => array('for' => 'ContactPassword'),
  1746. 'Password',
  1747. '/label',
  1748. array('input' => array(
  1749. 'type' => 'password', 'name' => 'data[Contact][password]',
  1750. 'id' => 'ContactPassword'
  1751. )),
  1752. '/div'
  1753. );
  1754. $this->assertTags($result, $expected);
  1755. $result = $this->Form->input('Contact.email', array(
  1756. 'type' => 'file', 'class' => 'textbox'
  1757. ));
  1758. $expected = array(
  1759. 'div' => array('class' => 'input file'),
  1760. 'label' => array('for' => 'ContactEmail'),
  1761. 'Email',
  1762. '/label',
  1763. array('input' => array(
  1764. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1765. 'id' => 'ContactEmail'
  1766. )),
  1767. '/div'
  1768. );
  1769. $this->assertTags($result, $expected);
  1770. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1771. $result = $this->Form->input('Contact.phone');
  1772. $expected = array(
  1773. 'div' => array('class' => 'input tel'),
  1774. 'label' => array('for' => 'ContactPhone'),
  1775. 'Phone',
  1776. '/label',
  1777. array('input' => array(
  1778. 'type' => 'tel', 'name' => 'data[Contact][phone]',
  1779. 'value' => 'Hello &amp; World &gt; weird chars',
  1780. 'id' => 'ContactPhone', 'maxlength' => 255
  1781. )),
  1782. '/div'
  1783. );
  1784. $this->assertTags($result, $expected);
  1785. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1786. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1787. $expected = array(
  1788. 'div' => array('class' => 'input text'),
  1789. 'label' => array('for' => 'myId'),
  1790. 'Field',
  1791. '/label',
  1792. 'input' => array(
  1793. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1794. 'value' => 'My value', 'id' => 'myId'
  1795. ),
  1796. '/div'
  1797. );
  1798. $this->assertTags($result, $expected);
  1799. unset($this->Form->request->data);
  1800. $Contact = ClassRegistry::getObject('Contact');
  1801. $Contact->validationErrors['field'] = array('Badness!');
  1802. $result = $this->Form->input('Contact.field');
  1803. $expected = array(
  1804. 'div' => array('class' => 'input text error'),
  1805. 'label' => array('for' => 'ContactField'),
  1806. 'Field',
  1807. '/label',
  1808. 'input' => array(
  1809. 'type' => 'text', 'name' => 'data[Contact][field]',
  1810. 'id' => 'ContactField', 'class' => 'form-error'
  1811. ),
  1812. array('div' => array('class' => 'error-message')),
  1813. 'Badness!',
  1814. '/div',
  1815. '/div'
  1816. );
  1817. $this->assertTags($result, $expected);
  1818. $result = $this->Form->input('Contact.field', array(
  1819. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1820. ));
  1821. $expected = array(
  1822. 'label' => array('for' => 'ContactField'),
  1823. 'Field',
  1824. '/label',
  1825. 'input' => array(
  1826. 'type' => 'text', 'name' => 'data[Contact][field]',
  1827. 'id' => 'ContactField', 'class' => 'form-error'
  1828. ),
  1829. array('span' => array('class' => 'error-message')),
  1830. 'Badness!',
  1831. '/span'
  1832. );
  1833. $this->assertTags($result, $expected);
  1834. $result = $this->Form->input('Contact.field', array(
  1835. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1836. ));
  1837. $expected = array(
  1838. 'div' => array('class' => 'input text error'),
  1839. 'label' => array('for' => 'ContactField'),
  1840. 'Field',
  1841. '/label',
  1842. 'input' => array(
  1843. 'type' => 'text', 'name' => 'data[Contact][field]',
  1844. 'id' => 'ContactField', 'class' => 'form-error'
  1845. ),
  1846. array('div' => array('class' => 'error')),
  1847. 'Badness!',
  1848. '/div'
  1849. );
  1850. $this->assertTags($result, $expected);
  1851. $result = $this->Form->input('Contact.field', array(
  1852. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1853. ));
  1854. $expected = array(
  1855. 'span' => array('class' => 'input text error'),
  1856. 'label' => array('for' => 'ContactField'),
  1857. 'Field',
  1858. '/label',
  1859. 'input' => array(
  1860. 'type' => 'text', 'name' => 'data[Contact][field]',
  1861. 'id' => 'ContactField', 'class' => 'form-error'
  1862. ),
  1863. 'Badness!',
  1864. '/span'
  1865. );
  1866. $this->assertTags($result, $expected);
  1867. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1868. $expected = array(
  1869. 'div' => array('class' => 'input text error'),
  1870. 'label' => array('for' => 'ContactField'),
  1871. 'Field',
  1872. '/label',
  1873. 'input' => array(
  1874. 'type' => 'text', 'name' => 'data[Contact][field]',
  1875. 'id' => 'ContactField', 'class' => 'form-error'
  1876. ),
  1877. 'A message to you, Rudy',
  1878. array('div' => array('class' => 'error-message')),
  1879. 'Badness!',
  1880. '/div',
  1881. '/div'
  1882. );
  1883. $this->assertTags($result, $expected);
  1884. $this->Form->setEntity(null);
  1885. $this->Form->setEntity('Contact.field');
  1886. $result = $this->Form->input('Contact.field', array(
  1887. 'after' => 'A message to you, Rudy', 'error' => false
  1888. ));
  1889. $expected = array(
  1890. 'div' => array('class' => 'input text'),
  1891. 'label' => array('for' => 'ContactField'),
  1892. 'Field',
  1893. '/label',
  1894. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1895. 'A message to you, Rudy',
  1896. '/div'
  1897. );
  1898. $this->assertTags($result, $expected);
  1899. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1900. $expected = array(
  1901. 'div' => array('class' => 'input text'),
  1902. 'label' => array('for' => 'ObjectField'),
  1903. 'Field',
  1904. '/label',
  1905. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1906. 'A message to you, Rudy',
  1907. '/div'
  1908. );
  1909. $this->assertTags($result, $expected);
  1910. $Contact->validationErrors['field'] = array('minLength');
  1911. $result = $this->Form->input('Contact.field', array(
  1912. 'error' => array(
  1913. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1914. 'maxLength' => 'login too large'
  1915. )
  1916. ));
  1917. $expected = array(
  1918. 'div' => array('class' => 'input text error'),
  1919. 'label' => array('for' => 'ContactField'),
  1920. 'Field',
  1921. '/label',
  1922. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1923. array('div' => array('class' => 'error-message')),
  1924. 'Le login doit contenir au moins 2 caractères',
  1925. '/div',
  1926. '/div'
  1927. );
  1928. $this->assertTags($result, $expected);
  1929. $Contact->validationErrors['field'] = array('maxLength');
  1930. $result = $this->Form->input('Contact.field', array(
  1931. 'error' => array(
  1932. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1933. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1934. 'maxLength' => 'login too large',
  1935. )
  1936. ));
  1937. $expected = array(
  1938. 'div' => array('class' => 'input text error'),
  1939. 'label' => array('for' => 'ContactField'),
  1940. 'Field',
  1941. '/label',
  1942. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1943. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1944. 'login too large',
  1945. '/span',
  1946. '/div'
  1947. );
  1948. $this->assertTags($result, $expected);
  1949. }
  1950. /**
  1951. * Test that inputs with 0 can be created.
  1952. *
  1953. * @return void
  1954. */
  1955. public function testInputZero() {
  1956. $this->Form->create('User');
  1957. $result = $this->Form->input('0');
  1958. $expected = array(
  1959. 'div' => array('class' => 'input text'),
  1960. 'label' => array('for' => 'User0'), '/label',
  1961. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  1962. '/div'
  1963. );
  1964. $this->assertTags($result, $expected);
  1965. }
  1966. /**
  1967. * test input() with checkbox creation
  1968. *
  1969. * @return void
  1970. */
  1971. public function testInputCheckbox() {
  1972. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1973. $expected = array(
  1974. 'div' => array('class' => 'input checkbox'),
  1975. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1976. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1977. '/div'
  1978. );
  1979. $this->assertTags($result, $expected);
  1980. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1981. $expected = array(
  1982. 'div' => array('class' => 'input checkbox'),
  1983. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1984. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1985. '/div'
  1986. );
  1987. $this->assertTags($result, $expected);
  1988. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1989. $expected = array(
  1990. 'div' => array('class' => 'input checkbox'),
  1991. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1992. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1993. '/div'
  1994. );
  1995. $this->assertTags($result, $expected);
  1996. $result = $this->Form->input('User.disabled', array(
  1997. 'label' => 'Disabled',
  1998. 'type' => 'checkbox',
  1999. 'data-foo' => 'disabled'
  2000. ));
  2001. $expected = array(
  2002. 'div' => array('class' => 'input checkbox'),
  2003. 'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
  2004. array('input' => array(
  2005. 'type' => 'checkbox',
  2006. 'name' => 'data[User][disabled]',
  2007. 'value' => '1',
  2008. 'id' => 'UserDisabled',
  2009. 'data-foo' => 'disabled'
  2010. )),
  2011. 'label' => array('for' => 'UserDisabled'),
  2012. 'Disabled',
  2013. '/label',
  2014. '/div'
  2015. );
  2016. $this->assertTags($result, $expected);
  2017. }
  2018. /**
  2019. * test form->input() with time types.
  2020. *
  2021. */
  2022. public function testInputTime() {
  2023. extract($this->dateRegex);
  2024. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2025. $result = explode(':', $result);
  2026. $this->assertRegExp('/option value="23"/', $result[0]);
  2027. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2028. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2029. $result = explode(':', $result);
  2030. $this->assertRegExp('/option value="23"/', $result[0]);
  2031. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2032. $result = $this->Form->input('Model.field', array(
  2033. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2034. ));
  2035. $result = explode(':', $result);
  2036. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2037. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2038. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2039. $result = $this->Form->input('Model.field', array(
  2040. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2041. ));
  2042. $result = explode(':', $result);
  2043. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2044. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2045. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2046. $result = $this->Form->input('prueba', array(
  2047. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  2048. 'maxYear' => date('Y') + 1 ,'interval' => 15
  2049. ));
  2050. $result = explode(':', $result);
  2051. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2052. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2053. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2054. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2055. $result = $this->Form->input('Random.start_time', array(
  2056. 'type' => 'time',
  2057. 'selected' => '18:15'
  2058. ));
  2059. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2060. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2061. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2062. $result = $this->Form->input('published', array('type' => 'time'));
  2063. $now = strtotime('now');
  2064. $this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
  2065. $now = strtotime('2013-03-09 00:42:21');
  2066. $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
  2067. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2068. $this->assertContains('<option value="42" selected="selected">42</option>', $result);
  2069. }
  2070. /**
  2071. * Test interval + selected near the hour roll over.
  2072. *
  2073. * @return void
  2074. */
  2075. public function testTimeSelectedWithInterval() {
  2076. $result = $this->Form->input('Model.start_time', array(
  2077. 'type' => 'time',
  2078. 'interval' => 15,
  2079. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2080. ));
  2081. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2082. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2083. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2084. $result = $this->Form->input('Model.start_time', array(
  2085. 'type' => 'time',
  2086. 'interval' => 15,
  2087. 'selected' => '2012-10-23 15:57:00'
  2088. ));
  2089. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2090. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2091. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2092. $result = $this->Form->input('Model.start_time', array(
  2093. 'timeFormat' => 24,
  2094. 'type' => 'time',
  2095. 'interval' => 15,
  2096. 'selected' => '15:57'
  2097. ));
  2098. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2099. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2100. $result = $this->Form->input('Model.start_time', array(
  2101. 'timeFormat' => 24,
  2102. 'type' => 'time',
  2103. 'interval' => 15,
  2104. 'selected' => '23:57'
  2105. ));
  2106. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2107. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2108. $result = $this->Form->input('Model.created', array(
  2109. 'timeFormat' => 24,
  2110. 'type' => 'datetime',
  2111. 'interval' => 15,
  2112. 'selected' => '2012-09-30 23:56'
  2113. ));
  2114. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2115. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2116. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2117. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2118. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2119. }
  2120. /**
  2121. * Test interval & timeFormat = 12
  2122. *
  2123. * @return void
  2124. */
  2125. public function testInputTimeWithIntervalAnd12HourFormat() {
  2126. $result = $this->Form->input('Model.start_time', array(
  2127. 'type' => 'time',
  2128. 'timeFormat' => 12,
  2129. 'interval' => 5,
  2130. 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
  2131. ));
  2132. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2133. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2134. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2135. $result = $this->Form->input('Model.start_time', array(
  2136. 'type' => 'time',
  2137. 'timeFormat' => '12',
  2138. 'interval' => 5,
  2139. 'selected' => '2013-04-19 16:30:00'
  2140. ));
  2141. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2142. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2143. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2144. $result = $this->Form->input('Model.start_time', array(
  2145. 'type' => 'time',
  2146. 'timeFormat' => '12',
  2147. 'interval' => 10,
  2148. 'selected' => '2013-05-19 00:33:00'
  2149. ));
  2150. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2151. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2152. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2153. $result = $this->Form->input('Model.start_time', array(
  2154. 'type' => 'time',
  2155. 'timeFormat' => '12',
  2156. 'interval' => 10,
  2157. 'selected' => '2013-05-19 13:33:00'
  2158. ));
  2159. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2160. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2161. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2162. $result = $this->Form->input('Model.start_time', array(
  2163. 'type' => 'time',
  2164. 'timeFormat' => '12',
  2165. 'interval' => 10,
  2166. 'selected' => '2013-05-19 01:33:00'
  2167. ));
  2168. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2169. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2170. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2171. }
  2172. /**
  2173. * test form->input() with datetime, date and time types
  2174. *
  2175. * @return void
  2176. */
  2177. public function testInputDatetime() {
  2178. extract($this->dateRegex);
  2179. $result = $this->Form->input('prueba', array(
  2180. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2181. 'maxYear' => date('Y') + 1, 'interval' => 15
  2182. ));
  2183. $result = explode(':', $result);
  2184. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2185. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2186. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2187. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2188. //related to ticket #5013
  2189. $result = $this->Form->input('Contact.date', array(
  2190. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2191. ));
  2192. $this->assertRegExp('/class="customClass"/', $result);
  2193. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2194. $result = $this->Form->input('Contact.date', array(
  2195. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2196. ));
  2197. $this->assertRegExp('/id="customIdDay"/', $result);
  2198. $this->assertRegExp('/id="customIdMonth"/', $result);
  2199. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2200. $result = $this->Form->input('Model.field', array(
  2201. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2202. ));
  2203. $this->assertRegExp('/id="customIDDay"/', $result);
  2204. $this->assertRegExp('/id="customIDHour"/', $result);
  2205. $result = explode('</select><select', $result);
  2206. $result = explode(':', $result[1]);
  2207. $this->assertRegExp('/option value="23"/', $result[0]);
  2208. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2209. $result = $this->Form->input('Model.field', array(
  2210. 'type' => 'datetime', 'timeFormat' => 12
  2211. ));
  2212. $result = explode('</select><select', $result);
  2213. $result = explode(':', $result[1]);
  2214. $this->assertRegExp('/option value="12"/', $result[0]);
  2215. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2216. $this->Form->request->data = array('Contact' => array('created' => null));
  2217. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2218. $expected = array(
  2219. 'div' => array('class' => 'input date'),
  2220. 'label' => array('for' => 'ContactCreatedMonth'),
  2221. 'Created',
  2222. '/label',
  2223. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2224. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2225. $monthsRegex,
  2226. '/select', '-',
  2227. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2228. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2229. $daysRegex,
  2230. '/select', '-',
  2231. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2232. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2233. $yearsRegex,
  2234. '/select',
  2235. '/div'
  2236. );
  2237. $this->assertTags($result, $expected);
  2238. $this->Form->request->data = array('Contact' => array('created' => null));
  2239. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2240. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2241. $this->Form->request->data = array('Contact' => array('created' => null));
  2242. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2243. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2244. $result = $this->Form->input('Contact.created', array(
  2245. 'type' => 'date',
  2246. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2247. 'timeFormat' => 'NONE'
  2248. ));
  2249. $this->assertRegExp('/for\="created-month"/', $result);
  2250. }
  2251. /**
  2252. * Test generating checkboxes in a loop.
  2253. *
  2254. * @return void
  2255. */
  2256. public function testInputCheckboxesInLoop() {
  2257. for ($i = 1; $i < 5; $i++) {
  2258. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2259. $expected = array(
  2260. 'div' => array('class' => 'input checkbox'),
  2261. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2262. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2263. 'label' => array('for' => "Contact{$i}Email"),
  2264. 'Email',
  2265. '/label',
  2266. '/div'
  2267. );
  2268. $this->assertTags($result, $expected);
  2269. }
  2270. }
  2271. /**
  2272. * Test generating checkboxes with disabled elements.
  2273. *
  2274. * @return void
  2275. */
  2276. public function testInputCheckboxWithDisabledElements() {
  2277. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2278. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2279. $expected = array(
  2280. array('div' => array('class' => 'input select')),
  2281. array('label' => array('for' => "ContactMultiple")),
  2282. 'Multiple',
  2283. '/label',
  2284. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2285. array('div' => array('class' => 'checkbox')),
  2286. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2287. array('label' => array('for' => "ContactMultiple1")),
  2288. 'One',
  2289. '/label',
  2290. '/div',
  2291. array('div' => array('class' => 'checkbox')),
  2292. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2293. array('label' => array('for' => "ContactMultiple2")),
  2294. 'Two',
  2295. '/label',
  2296. '/div',
  2297. array('div' => array('class' => 'checkbox')),
  2298. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2299. array('label' => array('for' => "ContactMultiple3")),
  2300. 'Three',
  2301. '/label',
  2302. '/div',
  2303. '/div'
  2304. );
  2305. $this->assertTags($result, $expected);
  2306. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2307. $this->assertTags($result, $expected);
  2308. $disabled = array('2', 3);
  2309. $expected = array(
  2310. array('div' => array('class' => 'input select')),
  2311. array('label' => array('for' => "ContactMultiple")),
  2312. 'Multiple',
  2313. '/label',
  2314. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2315. array('div' => array('class' => 'checkbox')),
  2316. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2317. array('label' => array('for' => "ContactMultiple1")),
  2318. 'One',
  2319. '/label',
  2320. '/div',
  2321. array('div' => array('class' => 'checkbox')),
  2322. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2323. array('label' => array('for' => "ContactMultiple2")),
  2324. 'Two',
  2325. '/label',
  2326. '/div',
  2327. array('div' => array('class' => 'checkbox')),
  2328. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2329. array('label' => array('for' => "ContactMultiple3")),
  2330. 'Three',
  2331. '/label',
  2332. '/div',
  2333. '/div'
  2334. );
  2335. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2336. $this->assertTags($result, $expected);
  2337. // make sure 50 does only disable 50, and not 50f5c0cf
  2338. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2339. $disabled = array(50);
  2340. $expected = array(
  2341. array('div' => array('class' => 'input select')),
  2342. array('label' => array('for' => "ContactMultiple")),
  2343. 'Multiple',
  2344. '/label',
  2345. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2346. array('div' => array('class' => 'checkbox')),
  2347. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2348. array('label' => array('for' => "ContactMultiple50")),
  2349. 'Fifty',
  2350. '/label',
  2351. '/div',
  2352. array('div' => array('class' => 'checkbox')),
  2353. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2354. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2355. 'Stringy',
  2356. '/label',
  2357. '/div',
  2358. '/div'
  2359. );
  2360. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2361. $this->assertTags($result, $expected);
  2362. }
  2363. /**
  2364. * test input name with leading integer, ensure attributes are generated correctly.
  2365. *
  2366. * @return void
  2367. */
  2368. public function testInputWithLeadingInteger() {
  2369. $result = $this->Form->text('0.Node.title');
  2370. $expected = array(
  2371. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2372. );
  2373. $this->assertTags($result, $expected);
  2374. }
  2375. /**
  2376. * test form->input() with select type inputs.
  2377. *
  2378. * @return void
  2379. */
  2380. public function testInputSelectType() {
  2381. $result = $this->Form->input('email', array(
  2382. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2383. );
  2384. $expected = array(
  2385. 'div' => array('class' => 'input select'),
  2386. 'label' => array('for' => 'email'),
  2387. 'Email',
  2388. '/label',
  2389. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2390. array('option' => array('value' => '')),
  2391. '/option',
  2392. array('option' => array('value' => 'è')),
  2393. 'Firést',
  2394. '/option',
  2395. array('option' => array('value' => 'é')),
  2396. 'Secoènd',
  2397. '/option',
  2398. '/select',
  2399. '/div'
  2400. );
  2401. $this->assertTags($result, $expected);
  2402. $result = $this->Form->input('email', array(
  2403. 'options' => array('First', 'Second'), 'empty' => true)
  2404. );
  2405. $expected = array(
  2406. 'div' => array('class' => 'input select'),
  2407. 'label' => array('for' => 'email'),
  2408. 'Email',
  2409. '/label',
  2410. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2411. array('option' => array('value' => '')),
  2412. '/option',
  2413. array('option' => array('value' => '0')),
  2414. 'First',
  2415. '/option',
  2416. array('option' => array('value' => '1')),
  2417. 'Second',
  2418. '/option',
  2419. '/select',
  2420. '/div'
  2421. );
  2422. $this->assertTags($result, $expected);
  2423. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2424. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2425. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2426. $expected = array(
  2427. 'div' => array('class' => 'input select'),
  2428. 'label' => array('for' => 'ModelUserId'),
  2429. 'User',
  2430. '/label',
  2431. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2432. array('option' => array('value' => '')),
  2433. '/option',
  2434. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2435. 'good',
  2436. '/option',
  2437. array('option' => array('value' => 'other')),
  2438. 'bad',
  2439. '/option',
  2440. '/select',
  2441. '/div'
  2442. );
  2443. $this->assertTags($result, $expected);
  2444. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2445. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2446. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2447. $expected = array(
  2448. 'div' => array('class' => 'input select'),
  2449. 'label' => array('for' => 'ThingUserId'),
  2450. 'User',
  2451. '/label',
  2452. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2453. array('option' => array('value' => '')),
  2454. 'Some Empty',
  2455. '/option',
  2456. array('option' => array('value' => 'value')),
  2457. 'good',
  2458. '/option',
  2459. array('option' => array('value' => 'other')),
  2460. 'bad',
  2461. '/option',
  2462. '/select',
  2463. '/div'
  2464. );
  2465. $this->assertTags($result, $expected);
  2466. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2467. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2468. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2469. $expected = array(
  2470. 'div' => array('class' => 'input select'),
  2471. 'label' => array('for' => 'ThingUserId'),
  2472. 'User',
  2473. '/label',
  2474. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2475. array('option' => array('value' => '')),
  2476. 'Some Empty',
  2477. '/option',
  2478. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2479. 'good',
  2480. '/option',
  2481. array('option' => array('value' => 'other')),
  2482. 'bad',
  2483. '/option',
  2484. '/select',
  2485. '/div'
  2486. );
  2487. $this->assertTags($result, $expected);
  2488. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2489. $this->Form->request->data = array('User' => array('User' => array('value')));
  2490. $result = $this->Form->input('User.User', array('empty' => true));
  2491. $expected = array(
  2492. 'div' => array('class' => 'input select'),
  2493. 'label' => array('for' => 'UserUser'),
  2494. 'User',
  2495. '/label',
  2496. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2497. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2498. array('option' => array('value' => '')),
  2499. '/option',
  2500. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2501. 'good',
  2502. '/option',
  2503. array('option' => array('value' => 'other')),
  2504. 'bad',
  2505. '/option',
  2506. '/select',
  2507. '/div'
  2508. );
  2509. $this->assertTags($result, $expected);
  2510. $this->Form->data = array();
  2511. $result = $this->Form->input('Publisher.id', array(
  2512. 'label' => 'Publisher',
  2513. 'type' => 'select',
  2514. 'multiple' => 'checkbox',
  2515. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2516. ));
  2517. $expected = array(
  2518. array('div' => array('class' => 'input select')),
  2519. array('label' => array('for' => 'PublisherId')),
  2520. 'Publisher',
  2521. '/label',
  2522. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2523. array('div' => array('class' => 'checkbox')),
  2524. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2525. array('label' => array('for' => 'PublisherIdValue1')),
  2526. 'Label 1',
  2527. '/label',
  2528. '/div',
  2529. array('div' => array('class' => 'checkbox')),
  2530. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2531. array('label' => array('for' => 'PublisherIdValue2')),
  2532. 'Label 2',
  2533. '/label',
  2534. '/div',
  2535. '/div'
  2536. );
  2537. $this->assertTags($result, $expected);
  2538. }
  2539. /**
  2540. * test that input() and a non standard primary key makes a hidden input by default.
  2541. *
  2542. * @return void
  2543. */
  2544. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2545. $this->Form->create('User');
  2546. $this->Form->fieldset = array(
  2547. 'User' => array(
  2548. 'fields' => array(
  2549. 'model_id' => array('type' => 'integer')
  2550. ),
  2551. 'validates' => array(),
  2552. 'key' => 'model_id'
  2553. )
  2554. );
  2555. $result = $this->Form->input('model_id');
  2556. $expected = array(
  2557. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2558. );
  2559. $this->assertTags($result, $expected);
  2560. }
  2561. /**
  2562. * test that overriding the magic select type widget is possible
  2563. *
  2564. * @return void
  2565. */
  2566. public function testInputOverridingMagicSelectType() {
  2567. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2568. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2569. $expected = array(
  2570. 'div' => array('class' => 'input text'),
  2571. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2572. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2573. '/div'
  2574. );
  2575. $this->assertTags($result, $expected);
  2576. //Check that magic types still work for plural/singular vars
  2577. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2578. $result = $this->Form->input('Model.type');
  2579. $expected = array(
  2580. 'div' => array('class' => 'input select'),
  2581. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2582. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2583. array('option' => array('value' => 'value')), 'good', '/option',
  2584. array('option' => array('value' => 'other')), 'bad', '/option',
  2585. '/select',
  2586. '/div'
  2587. );
  2588. $this->assertTags($result, $expected);
  2589. }
  2590. /**
  2591. * Test that magic input() selects are created for type=number
  2592. *
  2593. * @return void
  2594. */
  2595. public function testInputMagicSelectForTypeNumber() {
  2596. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2597. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2598. $result = $this->Form->input('ValidateUser.balance');
  2599. $expected = array(
  2600. 'div' => array('class' => 'input select'),
  2601. 'label' => array('for' => 'ValidateUserBalance'),
  2602. 'Balance',
  2603. '/label',
  2604. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2605. array('option' => array('value' => '0')),
  2606. 'nothing',
  2607. '/option',
  2608. array('option' => array('value' => '1', 'selected' => 'selected')),
  2609. 'some',
  2610. '/option',
  2611. array('option' => array('value' => '100')),
  2612. 'a lot',
  2613. '/option',
  2614. '/select',
  2615. '/div'
  2616. );
  2617. $this->assertTags($result, $expected);
  2618. }
  2619. /**
  2620. * Test that magic input() selects can easily be converted into radio types without error.
  2621. *
  2622. * @return void
  2623. */
  2624. public function testInputMagicSelectChangeToRadio() {
  2625. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2626. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2627. $this->assertRegExp('/input type="radio"/', $result);
  2628. }
  2629. /**
  2630. * fields with the same name as the model should work.
  2631. *
  2632. * @return void
  2633. */
  2634. public function testInputWithMatchingFieldAndModelName() {
  2635. $this->Form->create('User');
  2636. $this->Form->fieldset = array(
  2637. 'User' => array(
  2638. 'fields' => array(
  2639. 'User' => array('type' => 'text')
  2640. ),
  2641. 'validates' => array(),
  2642. 'key' => 'id'
  2643. )
  2644. );
  2645. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2646. $result = $this->Form->input('User', array('type' => 'text'));
  2647. $expected = array(
  2648. 'div' => array('class' => 'input text'),
  2649. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2650. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2651. '/div'
  2652. );
  2653. $this->assertTags($result, $expected);
  2654. }
  2655. /**
  2656. * testFormInputs method
  2657. *
  2658. * test correct results from form::inputs().
  2659. *
  2660. * @return void
  2661. */
  2662. public function testFormInputs() {
  2663. $this->Form->create('Contact');
  2664. $result = $this->Form->inputs('The Legend');
  2665. $expected = array(
  2666. '<fieldset',
  2667. '<legend',
  2668. 'The Legend',
  2669. '/legend',
  2670. '*/fieldset',
  2671. );
  2672. $this->assertTags($result, $expected);
  2673. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2674. $expected = array(
  2675. 'fieldset' => array('class' => 'classy-stuff'),
  2676. '<legend',
  2677. 'Field of Dreams',
  2678. '/legend',
  2679. '*/fieldset'
  2680. );
  2681. $this->assertTags($result, $expected);
  2682. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2683. $this->assertTags($result, $expected);
  2684. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2685. $this->assertTags($result, $expected);
  2686. $this->Form->create('Contact');
  2687. $this->Form->request['prefix'] = 'admin';
  2688. $this->Form->request['action'] = 'admin_edit';
  2689. $result = $this->Form->inputs();
  2690. $expected = array(
  2691. '<fieldset',
  2692. '<legend',
  2693. 'Edit Contact',
  2694. '/legend',
  2695. '*/fieldset',
  2696. );
  2697. $this->assertTags($result, $expected);
  2698. $this->Form->create('Contact');
  2699. $result = $this->Form->inputs(false);
  2700. $expected = array(
  2701. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2702. array('div' => array('class' => 'input text')),
  2703. '*/div',
  2704. array('div' => array('class' => 'input email')),
  2705. '*/div',
  2706. array('div' => array('class' => 'input tel')),
  2707. '*/div',
  2708. array('div' => array('class' => 'input password')),
  2709. '*/div',
  2710. array('div' => array('class' => 'input date')),
  2711. '*/div',
  2712. array('div' => array('class' => 'input date')),
  2713. '*/div',
  2714. array('div' => array('class' => 'input datetime')),
  2715. '*/div',
  2716. array('div' => array('class' => 'input number')),
  2717. '*/div',
  2718. array('div' => array('class' => 'input select')),
  2719. '*/div',
  2720. );
  2721. $this->assertTags($result, $expected);
  2722. $this->Form->create('Contact');
  2723. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2724. $expected = array(
  2725. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2726. array('div' => array('class' => 'input text')),
  2727. '*/div',
  2728. array('div' => array('class' => 'input email')),
  2729. '*/div',
  2730. array('div' => array('class' => 'input tel')),
  2731. '*/div',
  2732. array('div' => array('class' => 'input password')),
  2733. '*/div',
  2734. array('div' => array('class' => 'input date')),
  2735. '*/div',
  2736. array('div' => array('class' => 'input date')),
  2737. '*/div',
  2738. array('div' => array('class' => 'input datetime')),
  2739. '*/div',
  2740. array('div' => array('class' => 'input number')),
  2741. '*/div',
  2742. array('div' => array('class' => 'input select')),
  2743. '*/div',
  2744. );
  2745. $this->assertTags($result, $expected);
  2746. $this->Form->create('Contact');
  2747. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2748. $this->assertTags($result, $expected);
  2749. $this->Form->create('Contact');
  2750. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2751. $expected = array(
  2752. 'fieldset' => array(),
  2753. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2754. array('div' => array('class' => 'input text')),
  2755. '*/div',
  2756. array('div' => array('class' => 'input email')),
  2757. '*/div',
  2758. array('div' => array('class' => 'input tel')),
  2759. '*/div',
  2760. array('div' => array('class' => 'input password')),
  2761. '*/div',
  2762. array('div' => array('class' => 'input date')),
  2763. '*/div',
  2764. array('div' => array('class' => 'input date')),
  2765. '*/div',
  2766. array('div' => array('class' => 'input datetime')),
  2767. '*/div',
  2768. array('div' => array('class' => 'input number')),
  2769. '*/div',
  2770. array('div' => array('class' => 'input select')),
  2771. '*/div',
  2772. '/fieldset'
  2773. );
  2774. $this->assertTags($result, $expected);
  2775. $this->Form->create('Contact');
  2776. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2777. $expected = array(
  2778. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2779. array('div' => array('class' => 'input text')),
  2780. '*/div',
  2781. array('div' => array('class' => 'input email')),
  2782. '*/div',
  2783. array('div' => array('class' => 'input tel')),
  2784. '*/div',
  2785. array('div' => array('class' => 'input password')),
  2786. '*/div',
  2787. array('div' => array('class' => 'input date')),
  2788. '*/div',
  2789. array('div' => array('class' => 'input date')),
  2790. '*/div',
  2791. array('div' => array('class' => 'input datetime')),
  2792. '*/div',
  2793. array('div' => array('class' => 'input number')),
  2794. '*/div',
  2795. array('div' => array('class' => 'input select')),
  2796. '*/div',
  2797. );
  2798. $this->assertTags($result, $expected);
  2799. $this->Form->create('Contact');
  2800. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2801. $this->assertTags($result, $expected);
  2802. $this->Form->create('Contact');
  2803. $result = $this->Form->inputs('Hello');
  2804. $expected = array(
  2805. 'fieldset' => array(),
  2806. 'legend' => array(),
  2807. 'Hello',
  2808. '/legend',
  2809. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2810. array('div' => array('class' => 'input text')),
  2811. '*/div',
  2812. array('div' => array('class' => 'input email')),
  2813. '*/div',
  2814. array('div' => array('class' => 'input tel')),
  2815. '*/div',
  2816. array('div' => array('class' => 'input password')),
  2817. '*/div',
  2818. array('div' => array('class' => 'input date')),
  2819. '*/div',
  2820. array('div' => array('class' => 'input date')),
  2821. '*/div',
  2822. array('div' => array('class' => 'input datetime')),
  2823. '*/div',
  2824. array('div' => array('class' => 'input number')),
  2825. '*/div',
  2826. array('div' => array('class' => 'input select')),
  2827. '*/div',
  2828. '/fieldset'
  2829. );
  2830. $this->assertTags($result, $expected);
  2831. $this->Form->create('Contact');
  2832. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2833. $expected = array(
  2834. 'fieldset' => array(),
  2835. 'legend' => array(),
  2836. 'Hello',
  2837. '/legend',
  2838. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2839. array('div' => array('class' => 'input text')),
  2840. '*/div',
  2841. array('div' => array('class' => 'input email')),
  2842. '*/div',
  2843. array('div' => array('class' => 'input tel')),
  2844. '*/div',
  2845. array('div' => array('class' => 'input password')),
  2846. '*/div',
  2847. array('div' => array('class' => 'input date')),
  2848. '*/div',
  2849. array('div' => array('class' => 'input date')),
  2850. '*/div',
  2851. array('div' => array('class' => 'input datetime')),
  2852. '*/div',
  2853. array('div' => array('class' => 'input number')),
  2854. '*/div',
  2855. array('div' => array('class' => 'input select')),
  2856. '*/div',
  2857. '/fieldset'
  2858. );
  2859. $this->assertTags($result, $expected);
  2860. $this->Form->create('Contact');
  2861. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2862. $this->assertTags($result, $expected);
  2863. $this->Form->end();
  2864. $this->Form->create(false);
  2865. $expected = array(
  2866. 'fieldset' => array(),
  2867. array('div' => array('class' => 'input text')),
  2868. 'label' => array('for' => 'foo'),
  2869. 'Foo',
  2870. '/label',
  2871. 'input' => array('type' => 'text', 'name' => 'data[foo]', 'id' => 'foo'),
  2872. '*/div',
  2873. '/fieldset'
  2874. );
  2875. $result = $this->Form->inputs(
  2876. array('foo' => array('type' => 'text')),
  2877. array(),
  2878. array('legend' => false)
  2879. );
  2880. $this->assertTags($result, $expected);
  2881. }
  2882. /**
  2883. * Tests inputs() works with plugin models
  2884. *
  2885. * @return void
  2886. */
  2887. public function testInputsPluginModel() {
  2888. $this->loadFixtures('Post');
  2889. App::build(array(
  2890. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  2891. ));
  2892. CakePlugin::load('TestPlugin');
  2893. $this->Form->request['models'] = array(
  2894. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  2895. );
  2896. $this->Form->create('TestPlugin.TestPluginPost');
  2897. $result = $this->Form->inputs();
  2898. $this->assertContains('data[TestPluginPost][id]', $result);
  2899. $this->assertContains('data[TestPluginPost][author_id]', $result);
  2900. $this->assertContains('data[TestPluginPost][title]', $result);
  2901. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  2902. $this->assertFalse(ClassRegistry::isKeySet('TestPlugin'));
  2903. $this->assertEquals('TestPluginPost', $this->Form->model());
  2904. }
  2905. /**
  2906. * testSelectAsCheckbox method
  2907. *
  2908. * test multi-select widget with checkbox formatting.
  2909. *
  2910. * @return void
  2911. */
  2912. public function testSelectAsCheckbox() {
  2913. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2914. $expected = array(
  2915. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2916. array('div' => array('class' => 'checkbox')),
  2917. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2918. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2919. 'first',
  2920. '/label',
  2921. '/div',
  2922. array('div' => array('class' => 'checkbox')),
  2923. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2924. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2925. 'second',
  2926. '/label',
  2927. '/div',
  2928. array('div' => array('class' => 'checkbox')),
  2929. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2930. array('label' => array('for' => 'ModelMultiField2')),
  2931. 'third',
  2932. '/label',
  2933. '/div',
  2934. );
  2935. $this->assertTags($result, $expected);
  2936. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2937. $expected = array(
  2938. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2939. array('div' => array('class' => 'checkbox')),
  2940. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2941. array('label' => array('for' => 'ModelMultiField12')),
  2942. 'half',
  2943. '/label',
  2944. '/div',
  2945. );
  2946. $this->assertTags($result, $expected);
  2947. }
  2948. /**
  2949. * testLabel method
  2950. *
  2951. * test label generation.
  2952. *
  2953. * @return void
  2954. */
  2955. public function testLabel() {
  2956. $this->Form->text('Person.name');
  2957. $result = $this->Form->label();
  2958. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2959. $this->Form->text('Person.name');
  2960. $result = $this->Form->label();
  2961. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2962. $result = $this->Form->label('Person.first_name');
  2963. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2964. $result = $this->Form->label('Person.first_name', 'Your first name');
  2965. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2966. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2967. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2968. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2969. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2970. $result = $this->Form->label('Person.first_name', '');
  2971. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2972. $result = $this->Form->label('Person.2.name', '');
  2973. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2974. }
  2975. /**
  2976. * testTextbox method
  2977. *
  2978. * test textbox element generation
  2979. *
  2980. * @return void
  2981. */
  2982. public function testTextbox() {
  2983. $result = $this->Form->text('Model.field');
  2984. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2985. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2986. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2987. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2988. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2989. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2990. $result = $this->Form->text('Model.text');
  2991. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2992. $Contact = ClassRegistry::getObject('Contact');
  2993. $Contact->validationErrors['text'] = array(true);
  2994. $this->Form->request->data['Contact']['text'] = 'test';
  2995. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2996. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2997. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2998. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2999. $expected = array(
  3000. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  3001. );
  3002. $this->assertTags($result, $expected);
  3003. }
  3004. /**
  3005. * testDefaultValue method
  3006. *
  3007. * Test default value setting
  3008. *
  3009. * @return void
  3010. */
  3011. public function testDefaultValue() {
  3012. $this->Form->request->data['Model']['field'] = 'test';
  3013. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3014. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  3015. unset($this->Form->request->data['Model']['field']);
  3016. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3017. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  3018. }
  3019. /**
  3020. * testCheckboxDefaultValue method
  3021. *
  3022. * Test default value setting on checkbox() method
  3023. *
  3024. * @return void
  3025. */
  3026. public function testCheckboxDefaultValue() {
  3027. $this->Form->request->data['Model']['field'] = false;
  3028. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3029. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3030. unset($this->Form->request->data['Model']['field']);
  3031. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3032. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3033. $this->Form->request->data['Model']['field'] = true;
  3034. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3035. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3036. unset($this->Form->request->data['Model']['field']);
  3037. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3038. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3039. }
  3040. /**
  3041. * testError method
  3042. *
  3043. * Test field error generation
  3044. *
  3045. * @return void
  3046. */
  3047. public function testError() {
  3048. $Contact = ClassRegistry::getObject('Contact');
  3049. $Contact->validationErrors['field'] = array(1);
  3050. $result = $this->Form->error('Contact.field');
  3051. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  3052. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3053. $this->assertEquals('Error in field Field', $result);
  3054. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3055. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3056. $this->assertEquals('This field contains invalid input', $result);
  3057. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3058. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  3059. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  3060. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  3061. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  3062. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  3063. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3064. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  3065. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3066. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  3067. $this->assertEquals('<strong>Badness!</strong>', $result);
  3068. $Contact->validationErrors['field'] = array("email");
  3069. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  3070. $expected = array(
  3071. 'div' => array('class' => 'field-error'),
  3072. 'No good!',
  3073. '/div'
  3074. );
  3075. $this->assertTags($result, $expected);
  3076. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  3077. $result = $this->Form->error('Contact.field', array(
  3078. 'notEmpty' => 'Cannot be empty',
  3079. 'email' => 'No good!'
  3080. ));
  3081. $expected = array(
  3082. 'div' => array('class' => 'error-message'),
  3083. 'ul' => array(),
  3084. '<li', 'Cannot be empty', '/li',
  3085. '<li', 'No good!', '/li',
  3086. '<li', 'Something else', '/li',
  3087. '/ul',
  3088. '/div'
  3089. );
  3090. $this->assertTags($result, $expected);
  3091. // Testing error messages list options
  3092. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  3093. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  3094. $expected = array(
  3095. 'div' => array('class' => 'error-message'),
  3096. 'ol' => array(),
  3097. '<li', 'notEmpty', '/li',
  3098. '<li', 'email', '/li',
  3099. '/ol',
  3100. '/div'
  3101. );
  3102. $this->assertTags($result, $expected);
  3103. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  3104. $expected = array(
  3105. 'div' => array('class' => 'error-message'),
  3106. 'ol' => array(),
  3107. '<li', 'notEmpty', '/li',
  3108. '<li', 'email', '/li',
  3109. '/ol',
  3110. '/div'
  3111. );
  3112. $this->assertTags($result, $expected);
  3113. $result = $this->Form->error('Contact.field', null, array(
  3114. 'listOptions' => array(
  3115. 'class' => 'ul-class',
  3116. 'itemOptions' => array(
  3117. 'class' => 'li-class'
  3118. )
  3119. )
  3120. ));
  3121. $expected = array(
  3122. 'div' => array('class' => 'error-message'),
  3123. 'ul' => array('class' => 'ul-class'),
  3124. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  3125. array('li' => array('class' => 'li-class')), 'email', '/li',
  3126. '/ul',
  3127. '/div'
  3128. );
  3129. $this->assertTags($result, $expected);
  3130. }
  3131. /**
  3132. * test error options when using form->input();
  3133. *
  3134. * @return void
  3135. */
  3136. public function testInputErrorEscape() {
  3137. $this->Form->create('ValidateProfile');
  3138. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  3139. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3140. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
  3141. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3142. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
  3143. $this->assertRegExp('/required<br>/', $result);
  3144. }
  3145. /**
  3146. * testPassword method
  3147. *
  3148. * Test password element generation
  3149. *
  3150. * @return void
  3151. */
  3152. public function testPassword() {
  3153. $Contact = ClassRegistry::getObject('Contact');
  3154. $result = $this->Form->password('Contact.field');
  3155. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  3156. $Contact->validationErrors['passwd'] = 1;
  3157. $this->Form->request->data['Contact']['passwd'] = 'test';
  3158. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3159. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3160. }
  3161. /**
  3162. * testRadio method
  3163. *
  3164. * Test radio element set generation
  3165. *
  3166. * @return void
  3167. */
  3168. public function testRadio() {
  3169. $result = $this->Form->radio('Model.field', array('option A'));
  3170. $expected = array(
  3171. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3172. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3173. 'label' => array('for' => 'ModelField0'),
  3174. 'option A',
  3175. '/label'
  3176. );
  3177. $this->assertTags($result, $expected);
  3178. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3179. $expected = array(
  3180. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3181. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  3182. 'label' => array('for' => 'ModelField12'),
  3183. 'half',
  3184. '/label'
  3185. );
  3186. $this->assertTags($result, $expected);
  3187. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3188. $expected = array(
  3189. 'fieldset' => array(),
  3190. 'legend' => array(),
  3191. 'Field',
  3192. '/legend',
  3193. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3194. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3195. array('label' => array('for' => 'ModelField0')),
  3196. 'option A',
  3197. '/label',
  3198. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3199. array('label' => array('for' => 'ModelField1')),
  3200. 'option B',
  3201. '/label',
  3202. '/fieldset'
  3203. );
  3204. $this->assertTags($result, $expected);
  3205. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3206. $expected = array(
  3207. 'fieldset' => array(),
  3208. 'legend' => array(),
  3209. 'Field',
  3210. '/legend',
  3211. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3212. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3213. array('label' => array('for' => 'ModelField0')),
  3214. 'option A',
  3215. '/label',
  3216. 'br' => array(),
  3217. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3218. array('label' => array('for' => 'ModelField1')),
  3219. 'option B',
  3220. '/label',
  3221. '/fieldset'
  3222. );
  3223. $this->assertTags($result, $expected);
  3224. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3225. $expected = array(
  3226. 'fieldset' => array(),
  3227. 'legend' => array(),
  3228. 'Gender',
  3229. '/legend',
  3230. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3231. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3232. array('label' => array('for' => 'EmployeeGenderMale')),
  3233. 'Male',
  3234. '/label',
  3235. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3236. array('label' => array('for' => 'EmployeeGenderFemale')),
  3237. 'Female',
  3238. '/label',
  3239. '/fieldset',
  3240. );
  3241. $this->assertTags($result, $expected);
  3242. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3243. $expected = array(
  3244. 'fieldset' => array(),
  3245. 'legend' => array(),
  3246. 'Gender',
  3247. '/legend',
  3248. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3249. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3250. array('label' => array('for' => 'OfficerGenderMale')),
  3251. 'Male',
  3252. '/label',
  3253. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3254. array('label' => array('for' => 'OfficerGenderFemale')),
  3255. 'Female',
  3256. '/label',
  3257. '/fieldset',
  3258. );
  3259. $this->assertTags($result, $expected);
  3260. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3261. $expected = array(
  3262. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3263. array('input' => array(
  3264. 'type' => 'radio',
  3265. 'name' => 'data[Contact][1][imrequired]',
  3266. 'value' => '0',
  3267. 'id' => 'Contact1Imrequired0',
  3268. 'required' => 'required'
  3269. )),
  3270. 'label' => array('for' => 'Contact1Imrequired0'),
  3271. 'option A',
  3272. '/label'
  3273. );
  3274. $this->assertTags($result, $expected);
  3275. $result = $this->Form->radio('Model.1.field', array('option A'));
  3276. $expected = array(
  3277. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3278. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3279. 'label' => array('for' => 'Model1Field0'),
  3280. 'option A',
  3281. '/label'
  3282. );
  3283. $this->assertTags($result, $expected);
  3284. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3285. $expected = array(
  3286. 'fieldset' => array(),
  3287. 'legend' => array(),
  3288. 'Field',
  3289. '/legend',
  3290. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3291. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3292. array('label' => array('for' => 'ModelField0')),
  3293. 'option A',
  3294. '/label',
  3295. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3296. array('label' => array('for' => 'ModelField1')),
  3297. 'option B',
  3298. '/label',
  3299. '/fieldset'
  3300. );
  3301. $this->assertTags($result, $expected);
  3302. }
  3303. /**
  3304. * Test radio inputs with between as string or array. Also ensure
  3305. * that an array with less between elements works.
  3306. *
  3307. * @return void
  3308. */
  3309. public function testRadioBetween() {
  3310. $result = $this->Form->radio(
  3311. 'Model.field',
  3312. array('option A', 'option B'),
  3313. array('between' => 'I am between')
  3314. );
  3315. $expected = array(
  3316. 'fieldset' => array(),
  3317. 'legend' => array(),
  3318. 'Field',
  3319. '/legend',
  3320. 'I am between',
  3321. 'input' => array(
  3322. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3323. 'value' => '', 'id' => 'ModelField_'
  3324. ),
  3325. array('input' => array(
  3326. 'type' => 'radio', 'name' => 'data[Model][field]',
  3327. 'value' => '0', 'id' => 'ModelField0'
  3328. )),
  3329. array('label' => array('for' => 'ModelField0')),
  3330. 'option A',
  3331. '/label',
  3332. array('input' => array(
  3333. 'type' => 'radio', 'name' => 'data[Model][field]',
  3334. 'value' => '1', 'id' => 'ModelField1'
  3335. )),
  3336. array('label' => array('for' => 'ModelField1')),
  3337. 'option B',
  3338. '/label',
  3339. '/fieldset'
  3340. );
  3341. $this->assertTags($result, $expected);
  3342. $result = $this->Form->radio(
  3343. 'Model.field',
  3344. array('option A', 'option B', 'option C'),
  3345. array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
  3346. );
  3347. $expected = array(
  3348. 'fieldset' => array(),
  3349. 'legend' => array(),
  3350. 'Field',
  3351. '/legend',
  3352. 'input' => array(
  3353. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3354. 'value' => '', 'id' => 'ModelField_'
  3355. ),
  3356. array('input' => array(
  3357. 'type' => 'radio', 'name' => 'data[Model][field]',
  3358. 'value' => '0', 'id' => 'ModelField0'
  3359. )),
  3360. array('label' => array('for' => 'ModelField0')),
  3361. 'option A',
  3362. '/label',
  3363. 'between A',
  3364. '--separator--',
  3365. array('input' => array(
  3366. 'type' => 'radio', 'name' => 'data[Model][field]',
  3367. 'value' => '1', 'id' => 'ModelField1'
  3368. )),
  3369. array('label' => array('for' => 'ModelField1')),
  3370. 'option B',
  3371. '/label',
  3372. 'between B',
  3373. '--separator--',
  3374. array('input' => array(
  3375. 'type' => 'radio', 'name' => 'data[Model][field]',
  3376. 'value' => '2', 'id' => 'ModelField2'
  3377. )),
  3378. array('label' => array('for' => 'ModelField2')),
  3379. 'option C',
  3380. '/label',
  3381. 'between C',
  3382. '/fieldset'
  3383. );
  3384. $this->assertTags($result, $expected);
  3385. $result = $this->Form->input('Model.field', array(
  3386. 'options' => array('1' => 'first', '2' => 'second'),
  3387. 'type' => 'radio',
  3388. 'before' => '--before--',
  3389. 'after' => '--after--',
  3390. 'separator' => '--separator--',
  3391. 'between' => array('--between first--', '--between second--')
  3392. ));
  3393. $expected = array(
  3394. 'div' => array('class' => 'input radio'),
  3395. '--before--',
  3396. 'fieldset' => array(),
  3397. 'legend' => array(),
  3398. 'Field',
  3399. '/legend',
  3400. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3401. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3402. array('label' => array('for' => 'ModelField1')),
  3403. 'first',
  3404. '/label',
  3405. '--between first--',
  3406. '--separator--',
  3407. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3408. array('label' => array('for' => 'ModelField2')),
  3409. 'second',
  3410. '/label',
  3411. '--between second--',
  3412. '/fieldset',
  3413. '--after--',
  3414. '/div'
  3415. );
  3416. $this->assertTags($result, $expected);
  3417. $result = $this->Form->input('Model.field', array(
  3418. 'options' => array('1' => 'first', '2' => 'second'),
  3419. 'type' => 'radio',
  3420. 'before' => '--before--',
  3421. 'after' => '--after--',
  3422. 'separator' => '--separator--',
  3423. 'between' => array('--between first--')
  3424. ));
  3425. $expected = array(
  3426. 'div' => array('class' => 'input radio'),
  3427. '--before--',
  3428. 'fieldset' => array(),
  3429. 'legend' => array(),
  3430. 'Field',
  3431. '/legend',
  3432. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3433. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3434. array('label' => array('for' => 'ModelField1')),
  3435. 'first',
  3436. '/label',
  3437. '--between first--',
  3438. '--separator--',
  3439. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3440. array('label' => array('for' => 'ModelField2')),
  3441. 'second',
  3442. '/label',
  3443. '/fieldset',
  3444. '--after--',
  3445. '/div'
  3446. );
  3447. $this->assertTags($result, $expected);
  3448. }
  3449. /**
  3450. * Test that radios with a 0 value are selected under the correct conditions.
  3451. * Also ensure that values that are booleanish are handled correctly.
  3452. *
  3453. * @return void
  3454. */
  3455. public function testRadioOptionWithBooleanishValues() {
  3456. $expected = array(
  3457. 'fieldset' => array(),
  3458. 'legend' => array(),
  3459. 'Field',
  3460. '/legend',
  3461. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3462. array('label' => array('for' => 'ModelField1')),
  3463. 'Yes',
  3464. '/label',
  3465. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3466. array('label' => array('for' => 'ModelField0')),
  3467. 'No',
  3468. '/label',
  3469. '/fieldset'
  3470. );
  3471. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3472. $this->assertTags($result, $expected);
  3473. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3474. $this->assertTags($result, $expected);
  3475. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3476. $this->assertTags($result, $expected);
  3477. $expected = array(
  3478. 'fieldset' => array(),
  3479. 'legend' => array(),
  3480. 'Field',
  3481. '/legend',
  3482. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3483. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3484. array('label' => array('for' => 'ModelField1')),
  3485. 'Yes',
  3486. '/label',
  3487. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3488. array('label' => array('for' => 'ModelField0')),
  3489. 'No',
  3490. '/label',
  3491. '/fieldset'
  3492. );
  3493. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3494. $this->assertTags($result, $expected);
  3495. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3496. $this->assertTags($result, $expected);
  3497. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3498. $this->assertTags($result, $expected);
  3499. $expected = array(
  3500. 'fieldset' => array(),
  3501. 'legend' => array(),
  3502. 'Field',
  3503. '/legend',
  3504. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3505. array('label' => array('for' => 'ModelField1')),
  3506. 'Yes',
  3507. '/label',
  3508. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3509. array('label' => array('for' => 'ModelField0')),
  3510. 'No',
  3511. '/label',
  3512. '/fieldset'
  3513. );
  3514. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3515. $this->assertTags($result, $expected);
  3516. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3517. $this->assertTags($result, $expected);
  3518. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3519. $this->assertTags($result, $expected);
  3520. }
  3521. /**
  3522. * test disabled radio options
  3523. *
  3524. * @return void
  3525. */
  3526. public function testRadioDisabled() {
  3527. $result = $this->Form->radio(
  3528. 'Model.field',
  3529. array('option A', 'option B'),
  3530. array('disabled' => array(0), 'value' => '0')
  3531. );
  3532. $expected = array(
  3533. 'fieldset' => array(),
  3534. 'legend' => array(),
  3535. 'Field',
  3536. '/legend',
  3537. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3538. array('label' => array('for' => 'ModelField0')),
  3539. 'option A',
  3540. '/label',
  3541. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3542. array('label' => array('for' => 'ModelField1')),
  3543. 'option B',
  3544. '/label',
  3545. '/fieldset'
  3546. );
  3547. $this->assertTags($result, $expected);
  3548. $result = $this->Form->radio(
  3549. 'Model.field',
  3550. array('option A', 'option B'),
  3551. array('disabled' => true, 'value' => '0')
  3552. );
  3553. $expected = array(
  3554. 'fieldset' => array(),
  3555. 'legend' => array(),
  3556. 'Field',
  3557. '/legend',
  3558. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3559. array('label' => array('for' => 'ModelField0')),
  3560. 'option A',
  3561. '/label',
  3562. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3563. array('label' => array('for' => 'ModelField1')),
  3564. 'option B',
  3565. '/label',
  3566. '/fieldset'
  3567. );
  3568. $this->assertTags($result, $expected);
  3569. $result = $this->Form->radio(
  3570. 'Model.field',
  3571. array('option A', 'option B'),
  3572. array('disabled' => 'disabled', 'value' => '0')
  3573. );
  3574. $expected = array(
  3575. 'fieldset' => array(),
  3576. 'legend' => array(),
  3577. 'Field',
  3578. '/legend',
  3579. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3580. array('label' => array('for' => 'ModelField0')),
  3581. 'option A',
  3582. '/label',
  3583. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3584. array('label' => array('for' => 'ModelField1')),
  3585. 'option B',
  3586. '/label',
  3587. '/fieldset'
  3588. );
  3589. $this->assertTags($result, $expected);
  3590. $result = $this->Form->input('Model.field', array(
  3591. 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
  3592. 'type' => 'radio',
  3593. 'disabled' => array(2, '3x'),
  3594. ));
  3595. $expected = array(
  3596. 'div' => array('class' => 'input radio'),
  3597. 'fieldset' => array(),
  3598. 'legend' => array(),
  3599. 'Field',
  3600. '/legend',
  3601. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3602. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3603. array('label' => array('for' => 'ModelField1')),
  3604. 'first',
  3605. '/label',
  3606. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
  3607. array('label' => array('for' => 'ModelField2')),
  3608. 'second',
  3609. '/label',
  3610. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2x', 'id' => 'ModelField2x')),
  3611. array('label' => array('for' => 'ModelField2x')),
  3612. '2x',
  3613. '/label',
  3614. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '3', 'id' => 'ModelField3')),
  3615. array('label' => array('for' => 'ModelField3')),
  3616. 'third',
  3617. '/label',
  3618. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
  3619. array('label' => array('for' => 'ModelField3x')),
  3620. '3x',
  3621. '/label',
  3622. '/fieldset',
  3623. '/div'
  3624. );
  3625. $this->assertTags($result, $expected);
  3626. }
  3627. /**
  3628. * test disabling the hidden input for radio buttons
  3629. *
  3630. * @return void
  3631. */
  3632. public function testRadioHiddenInputDisabling() {
  3633. $result = $this->Form->input('Model.1.field', array(
  3634. 'type' => 'radio',
  3635. 'options' => array('option A'),
  3636. 'hiddenField' => false
  3637. )
  3638. );
  3639. $expected = array(
  3640. 'div' => array('class' => 'input radio'),
  3641. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3642. 'label' => array('for' => 'Model1Field0'),
  3643. 'option A',
  3644. '/label',
  3645. '/div'
  3646. );
  3647. $this->assertTags($result, $expected);
  3648. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3649. $expected = array(
  3650. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3651. 'label' => array('for' => 'Model1Field0'),
  3652. 'option A',
  3653. '/label'
  3654. );
  3655. $this->assertTags($result, $expected);
  3656. }
  3657. /**
  3658. * test adding an empty option for radio buttons
  3659. *
  3660. * @return void
  3661. */
  3662. public function testRadioAddEmptyOption() {
  3663. $result = $this->Form->input('Model.1.field', array(
  3664. 'type' => 'radio',
  3665. 'options' => array('option A'),
  3666. 'empty' => true,
  3667. 'hiddenField' => false
  3668. ));
  3669. $expected = array(
  3670. 'div' => array('class' => 'input radio'),
  3671. 'fieldset' => array(),
  3672. 'legend' => array(),
  3673. 'Field',
  3674. '/legend',
  3675. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3676. array('label' => array('for' => 'Model1Field')),
  3677. __('empty'),
  3678. '/label',
  3679. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3680. array('label' => array('for' => 'Model1Field0')),
  3681. 'option A',
  3682. '/label',
  3683. '/fieldset',
  3684. '/div'
  3685. );
  3686. $this->assertTags($result, $expected);
  3687. $result = $this->Form->input('Model.1.field', array(
  3688. 'type' => 'radio',
  3689. 'options' => array('option A'),
  3690. 'empty' => 'CustomEmptyLabel',
  3691. 'hiddenField' => false
  3692. ));
  3693. $expected = array(
  3694. 'div' => array('class' => 'input radio'),
  3695. 'fieldset' => array(),
  3696. 'legend' => array(),
  3697. 'Field',
  3698. '/legend',
  3699. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3700. array('label' => array('for' => 'Model1Field')),
  3701. 'CustomEmptyLabel',
  3702. '/label',
  3703. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3704. array('label' => array('for' => 'Model1Field0')),
  3705. 'option A',
  3706. '/label',
  3707. '/fieldset',
  3708. '/div'
  3709. );
  3710. $this->assertTags($result, $expected);
  3711. $result = $this->Form->input('Model.1.field', array(
  3712. 'type' => 'radio',
  3713. 'options' => array('option A'),
  3714. 'empty' => false,
  3715. 'hiddenField' => false
  3716. ));
  3717. $this->assertTextNotContains('"Model1Field"', $result);
  3718. }
  3719. /**
  3720. * testSelect method
  3721. *
  3722. * Test select element generation.
  3723. *
  3724. * @return void
  3725. */
  3726. public function testSelect() {
  3727. $result = $this->Form->select('Model.field', array());
  3728. $expected = array(
  3729. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3730. array('option' => array('value' => '')),
  3731. '/option',
  3732. '/select'
  3733. );
  3734. $this->assertTags($result, $expected);
  3735. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3736. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3737. $expected = array(
  3738. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3739. array('option' => array('value' => '')),
  3740. '/option',
  3741. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3742. 'good',
  3743. '/option',
  3744. array('option' => array('value' => 'other')),
  3745. 'bad',
  3746. '/option',
  3747. '/select'
  3748. );
  3749. $this->assertTags($result, $expected);
  3750. $this->Form->request->data = array();
  3751. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3752. $expected = array(
  3753. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3754. array('option' => array('value' => '')),
  3755. '/option',
  3756. array('option' => array('value' => 'value')),
  3757. 'good',
  3758. '/option',
  3759. array('option' => array('value' => 'other')),
  3760. 'bad',
  3761. '/option',
  3762. '/select'
  3763. );
  3764. $this->assertTags($result, $expected);
  3765. $result = $this->Form->select(
  3766. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3767. array('empty' => false)
  3768. );
  3769. $expected = array(
  3770. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3771. array('option' => array('value' => 'first')),
  3772. 'first &quot;html&quot; &lt;chars&gt;',
  3773. '/option',
  3774. array('option' => array('value' => 'second')),
  3775. 'value',
  3776. '/option',
  3777. '/select'
  3778. );
  3779. $this->assertTags($result, $expected);
  3780. $result = $this->Form->select(
  3781. 'Model.field',
  3782. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3783. array('escape' => false, 'empty' => false)
  3784. );
  3785. $expected = array(
  3786. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3787. array('option' => array('value' => 'first')),
  3788. 'first "html" <chars>',
  3789. '/option',
  3790. array('option' => array('value' => 'second')),
  3791. 'value',
  3792. '/option',
  3793. '/select'
  3794. );
  3795. $this->assertTags($result, $expected);
  3796. $options = array(
  3797. array('value' => 'first', 'name' => 'First'),
  3798. array('value' => 'first', 'name' => 'Another First'),
  3799. );
  3800. $result = $this->Form->select(
  3801. 'Model.field',
  3802. $options,
  3803. array('escape' => false, 'empty' => false)
  3804. );
  3805. $expected = array(
  3806. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3807. array('option' => array('value' => 'first')),
  3808. 'First',
  3809. '/option',
  3810. array('option' => array('value' => 'first')),
  3811. 'Another First',
  3812. '/option',
  3813. '/select'
  3814. );
  3815. $this->assertTags($result, $expected);
  3816. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3817. $result = $this->Form->select(
  3818. 'Model.contact_id',
  3819. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3820. array('escape' => false, 'empty' => 'pick something')
  3821. );
  3822. $expected = array(
  3823. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3824. array('option' => array('value' => '')), 'pick something', '/option',
  3825. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3826. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3827. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3828. '/select'
  3829. );
  3830. $this->assertTags($result, $expected);
  3831. $this->Form->request->data['Model']['field'] = 0;
  3832. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3833. $expected = array(
  3834. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3835. array('option' => array('value' => '')), '/option',
  3836. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3837. array('option' => array('value' => '1')), 'Yes', '/option',
  3838. '/select'
  3839. );
  3840. $this->assertTags($result, $expected);
  3841. $this->Form->request->data['Model']['field'] = 50;
  3842. $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
  3843. $expected = array(
  3844. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3845. array('option' => array('value' => '')), '/option',
  3846. array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
  3847. array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
  3848. '/select'
  3849. );
  3850. $this->assertTags($result, $expected);
  3851. $result = $this->Form->select('Contact.required_one', array('option A'));
  3852. $expected = array(
  3853. 'select' => array(
  3854. 'name' => 'data[Contact][required_one]',
  3855. 'id' => 'ContactRequiredOne',
  3856. 'required' => 'required'
  3857. ),
  3858. array('option' => array('value' => '')), '/option',
  3859. array('option' => array('value' => '0')), 'option A', '/option',
  3860. '/select'
  3861. );
  3862. $this->assertTags($result, $expected);
  3863. $result = $this->Form->select('Contact.required_one', array('option A'), array('disabled' => true));
  3864. $expected = array(
  3865. 'select' => array(
  3866. 'name' => 'data[Contact][required_one]',
  3867. 'id' => 'ContactRequiredOne',
  3868. 'disabled' => 'disabled'
  3869. ),
  3870. array('option' => array('value' => '', 'disabled' => 'disabled')), '/option',
  3871. array('option' => array('value' => '0', 'disabled' => 'disabled')), 'option A', '/option',
  3872. '/select'
  3873. );
  3874. $this->assertTags($result, $expected);
  3875. }
  3876. /**
  3877. * test that select() with optiongroups listens to the escape param.
  3878. *
  3879. * @return void
  3880. */
  3881. public function testSelectOptionGroupEscaping() {
  3882. $options = array(
  3883. '>< Key' => array(
  3884. 1 => 'One',
  3885. 2 => 'Two'
  3886. )
  3887. );
  3888. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3889. $expected = array(
  3890. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3891. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3892. array('option' => array('value' => '1')), 'One', '/option',
  3893. array('option' => array('value' => '2')), 'Two', '/option',
  3894. '/optgroup',
  3895. '/select'
  3896. );
  3897. $this->assertTags($result, $expected);
  3898. $options = array(
  3899. '>< Key' => array(
  3900. 1 => 'One',
  3901. 2 => 'Two'
  3902. )
  3903. );
  3904. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3905. $expected = array(
  3906. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3907. 'optgroup' => array('label' => '>< Key'),
  3908. array('option' => array('value' => '1')), 'One', '/option',
  3909. array('option' => array('value' => '2')), 'Two', '/option',
  3910. '/optgroup',
  3911. '/select'
  3912. );
  3913. $this->assertTags($result, $expected);
  3914. }
  3915. /**
  3916. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3917. *
  3918. * @return void
  3919. */
  3920. public function testSelectWithNullAttributes() {
  3921. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3922. $expected = array(
  3923. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3924. array('option' => array('value' => '0')),
  3925. 'first',
  3926. '/option',
  3927. array('option' => array('value' => '1')),
  3928. 'second',
  3929. '/option',
  3930. '/select'
  3931. );
  3932. $this->assertTags($result, $expected);
  3933. }
  3934. /**
  3935. * testNestedSelect method
  3936. *
  3937. * test select element generation with optgroups
  3938. *
  3939. * @return void
  3940. */
  3941. public function testNestedSelect() {
  3942. $result = $this->Form->select(
  3943. 'Model.field',
  3944. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3945. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3946. )), array('empty' => false)
  3947. );
  3948. $expected = array(
  3949. 'select' => array('name' => 'data[Model][field]',
  3950. 'id' => 'ModelField'),
  3951. array('option' => array('value' => 1)),
  3952. 'One',
  3953. '/option',
  3954. array('option' => array('value' => 2)),
  3955. 'Two',
  3956. '/option',
  3957. array('optgroup' => array('label' => 'Three')),
  3958. array('option' => array('value' => 4)),
  3959. 'Four',
  3960. '/option',
  3961. array('option' => array('value' => 5)),
  3962. 'Five',
  3963. '/option',
  3964. '/optgroup',
  3965. '/select'
  3966. );
  3967. $this->assertTags($result, $expected);
  3968. $result = $this->Form->select(
  3969. 'Model.field',
  3970. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3971. array('showParents' => true, 'empty' => false)
  3972. );
  3973. $expected = array(
  3974. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3975. array('option' => array('value' => 1)),
  3976. 'One',
  3977. '/option',
  3978. array('option' => array('value' => 2)),
  3979. 'Two',
  3980. '/option',
  3981. array('optgroup' => array('label' => 'Three')),
  3982. array('option' => array('value' => 3)),
  3983. 'Three',
  3984. '/option',
  3985. array('option' => array('value' => 4)),
  3986. 'Four',
  3987. '/option',
  3988. '/optgroup',
  3989. '/select'
  3990. );
  3991. $this->assertTags($result, $expected);
  3992. }
  3993. /**
  3994. * testSelectMultiple method
  3995. *
  3996. * test generation of multiple select elements
  3997. *
  3998. * @return void
  3999. */
  4000. public function testSelectMultiple() {
  4001. $options = array('first', 'second', 'third');
  4002. $result = $this->Form->select(
  4003. 'Model.multi_field', $options, array('multiple' => true)
  4004. );
  4005. $expected = array(
  4006. 'input' => array(
  4007. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4008. ),
  4009. 'select' => array(
  4010. 'name' => 'data[Model][multi_field][]',
  4011. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4012. ),
  4013. array('option' => array('value' => '0')),
  4014. 'first',
  4015. '/option',
  4016. array('option' => array('value' => '1')),
  4017. 'second',
  4018. '/option',
  4019. array('option' => array('value' => '2')),
  4020. 'third',
  4021. '/option',
  4022. '/select'
  4023. );
  4024. $this->assertTags($result, $expected);
  4025. $result = $this->Form->select(
  4026. 'Model.multi_field', $options, array('multiple' => 'multiple')
  4027. );
  4028. $expected = array(
  4029. 'input' => array(
  4030. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4031. ),
  4032. 'select' => array(
  4033. 'name' => 'data[Model][multi_field][]',
  4034. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4035. ),
  4036. array('option' => array('value' => '0')),
  4037. 'first',
  4038. '/option',
  4039. array('option' => array('value' => '1')),
  4040. 'second',
  4041. '/option',
  4042. array('option' => array('value' => '2')),
  4043. 'third',
  4044. '/option',
  4045. '/select'
  4046. );
  4047. $this->assertTags($result, $expected);
  4048. $result = $this->Form->select(
  4049. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  4050. );
  4051. $expected = array(
  4052. 'input' => array(
  4053. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4054. ),
  4055. 'select' => array(
  4056. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  4057. 'multiple' => 'multiple'
  4058. ),
  4059. array('option' => array('value' => '0', 'selected' => 'selected')),
  4060. 'first',
  4061. '/option',
  4062. array('option' => array('value' => '1', 'selected' => 'selected')),
  4063. 'second',
  4064. '/option',
  4065. array('option' => array('value' => '2')),
  4066. 'third',
  4067. '/option',
  4068. '/select'
  4069. );
  4070. $this->assertTags($result, $expected);
  4071. $result = $this->Form->select(
  4072. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  4073. );
  4074. $expected = array(
  4075. 'select' => array(
  4076. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  4077. ),
  4078. array('option' => array('value' => '0', 'selected' => 'selected')),
  4079. 'first',
  4080. '/option',
  4081. array('option' => array('value' => '1', 'selected' => 'selected')),
  4082. 'second',
  4083. '/option',
  4084. array('option' => array('value' => '2')),
  4085. 'third',
  4086. '/option',
  4087. '/select'
  4088. );
  4089. $this->assertTags($result, $expected);
  4090. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4091. $selected = array('2', '3x');
  4092. $result = $this->Form->select(
  4093. 'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
  4094. );
  4095. $expected = array(
  4096. 'input' => array(
  4097. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4098. ),
  4099. 'select' => array(
  4100. 'name' => 'data[Model][multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4101. ),
  4102. array('option' => array('value' => '1')),
  4103. 'One',
  4104. '/option',
  4105. array('option' => array('value' => '2', 'selected' => 'selected')),
  4106. 'Two',
  4107. '/option',
  4108. array('option' => array('value' => '3')),
  4109. 'Three',
  4110. '/option',
  4111. array('option' => array('value' => '3x', 'selected' => 'selected')),
  4112. 'Stringy',
  4113. '/option',
  4114. '/select'
  4115. );
  4116. $this->assertTags($result, $expected);
  4117. $result = $this->Form->select('Contact.required_one', array(
  4118. '1' => 'option A',
  4119. '2' => 'option B'
  4120. ), array('multiple' => true));
  4121. $expected = array(
  4122. 'input' => array(
  4123. 'type' => 'hidden', 'name' => 'data[Contact][required_one]', 'value' => '', 'id' => 'ContactRequiredOne_'
  4124. ),
  4125. 'select' => array(
  4126. 'name' => 'data[Contact][required_one][]',
  4127. 'id' => 'ContactRequiredOne',
  4128. 'required' => 'required',
  4129. 'multiple' => 'multiple'
  4130. ),
  4131. array('option' => array('value' => '1')), 'option A', '/option',
  4132. array('option' => array('value' => '2')), 'option B', '/option',
  4133. '/select'
  4134. );
  4135. $this->assertTags($result, $expected);
  4136. }
  4137. /**
  4138. * Test generating multiple select with disabled elements.
  4139. *
  4140. * @return void
  4141. */
  4142. public function testSelectMultipleWithDisabledElements() {
  4143. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4144. $disabled = array(2, 3);
  4145. $result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
  4146. $expected = array(
  4147. 'input' => array(
  4148. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4149. ),
  4150. 'select' => array(
  4151. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4152. ),
  4153. array('option' => array('value' => '1')),
  4154. 'One',
  4155. '/option',
  4156. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4157. 'Two',
  4158. '/option',
  4159. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4160. 'Three',
  4161. '/option',
  4162. array('option' => array('value' => '3x')),
  4163. 'Stringy',
  4164. '/option',
  4165. '/select'
  4166. );
  4167. $this->assertTags($result, $expected);
  4168. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4169. $disabled = array('2', '3x');
  4170. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4171. $expected = array(
  4172. array('div' => array('class' => 'input select')),
  4173. array('label' => array('for' => 'ContactMultiple')),
  4174. 'Multiple',
  4175. '/label',
  4176. 'input' => array(
  4177. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4178. ),
  4179. 'select' => array(
  4180. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4181. ),
  4182. array('option' => array('value' => '1')),
  4183. 'One',
  4184. '/option',
  4185. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4186. 'Two',
  4187. '/option',
  4188. array('option' => array('value' => '3')),
  4189. 'Three',
  4190. '/option',
  4191. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4192. 'Stringy',
  4193. '/option',
  4194. '/select',
  4195. '/div'
  4196. );
  4197. $this->assertTags($result, $expected);
  4198. }
  4199. /**
  4200. * test generation of habtm select boxes.
  4201. *
  4202. * @return void
  4203. */
  4204. public function testHabtmSelectBox() {
  4205. $this->View->viewVars['contactTags'] = array(
  4206. 1 => 'blue',
  4207. 2 => 'red',
  4208. 3 => 'green'
  4209. );
  4210. $this->Form->request->data = array(
  4211. 'Contact' => array(),
  4212. 'ContactTag' => array(
  4213. array(
  4214. 'id' => '1',
  4215. 'name' => 'blue'
  4216. ),
  4217. array(
  4218. 'id' => 3,
  4219. 'name' => 'green'
  4220. )
  4221. )
  4222. );
  4223. $this->Form->create('Contact');
  4224. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4225. $expected = array(
  4226. 'input' => array(
  4227. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4228. ),
  4229. 'select' => array(
  4230. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4231. 'multiple' => 'multiple'
  4232. ),
  4233. array('option' => array('value' => '1', 'selected' => 'selected')),
  4234. 'blue',
  4235. '/option',
  4236. array('option' => array('value' => '2')),
  4237. 'red',
  4238. '/option',
  4239. array('option' => array('value' => '3', 'selected' => 'selected')),
  4240. 'green',
  4241. '/option',
  4242. '/select'
  4243. );
  4244. $this->assertTags($result, $expected);
  4245. // make sure only 50 is selected, and not 50f5c0cf
  4246. $this->View->viewVars['contactTags'] = array(
  4247. '1' => 'blue',
  4248. '50f5c0cf' => 'red',
  4249. '50' => 'green'
  4250. );
  4251. $this->Form->request->data = array(
  4252. 'Contact' => array(),
  4253. 'ContactTag' => array(
  4254. array(
  4255. 'id' => 1,
  4256. 'name' => 'blue'
  4257. ),
  4258. array(
  4259. 'id' => 50,
  4260. 'name' => 'green'
  4261. )
  4262. )
  4263. );
  4264. $this->Form->create('Contact');
  4265. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4266. $expected = array(
  4267. 'input' => array(
  4268. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4269. ),
  4270. 'select' => array(
  4271. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4272. 'multiple' => 'multiple'
  4273. ),
  4274. array('option' => array('value' => '1', 'selected' => 'selected')),
  4275. 'blue',
  4276. '/option',
  4277. array('option' => array('value' => '50f5c0cf')),
  4278. 'red',
  4279. '/option',
  4280. array('option' => array('value' => '50', 'selected' => 'selected')),
  4281. 'green',
  4282. '/option',
  4283. '/select'
  4284. );
  4285. $this->assertTags($result, $expected);
  4286. }
  4287. /**
  4288. * test generation of multi select elements in checkbox format
  4289. *
  4290. * @return void
  4291. */
  4292. public function testSelectMultipleCheckboxes() {
  4293. $result = $this->Form->select(
  4294. 'Model.multi_field',
  4295. array('first', 'second', 'third'),
  4296. array('multiple' => 'checkbox')
  4297. );
  4298. $expected = array(
  4299. 'input' => array(
  4300. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4301. ),
  4302. array('div' => array('class' => 'checkbox')),
  4303. array('input' => array(
  4304. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4305. 'value' => '0', 'id' => 'ModelMultiField0'
  4306. )),
  4307. array('label' => array('for' => 'ModelMultiField0')),
  4308. 'first',
  4309. '/label',
  4310. '/div',
  4311. array('div' => array('class' => 'checkbox')),
  4312. array('input' => array(
  4313. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4314. 'value' => '1', 'id' => 'ModelMultiField1'
  4315. )),
  4316. array('label' => array('for' => 'ModelMultiField1')),
  4317. 'second',
  4318. '/label',
  4319. '/div',
  4320. array('div' => array('class' => 'checkbox')),
  4321. array('input' => array(
  4322. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4323. 'value' => '2', 'id' => 'ModelMultiField2'
  4324. )),
  4325. array('label' => array('for' => 'ModelMultiField2')),
  4326. 'third',
  4327. '/label',
  4328. '/div'
  4329. );
  4330. $this->assertTags($result, $expected);
  4331. $result = $this->Form->select(
  4332. 'Model.multi_field',
  4333. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4334. array('multiple' => 'checkbox')
  4335. );
  4336. $expected = array(
  4337. 'input' => array(
  4338. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4339. ),
  4340. array('div' => array('class' => 'checkbox')),
  4341. array('input' => array(
  4342. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4343. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  4344. )),
  4345. array('label' => array('for' => 'ModelMultiFieldA')),
  4346. 'first',
  4347. '/label',
  4348. '/div',
  4349. array('div' => array('class' => 'checkbox')),
  4350. array('input' => array(
  4351. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4352. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  4353. )),
  4354. array('label' => array('for' => 'ModelMultiFieldB')),
  4355. 'second',
  4356. '/label',
  4357. '/div',
  4358. array('div' => array('class' => 'checkbox')),
  4359. array('input' => array(
  4360. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4361. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  4362. )),
  4363. array('label' => array('for' => 'ModelMultiFieldC')),
  4364. 'third',
  4365. '/label',
  4366. '/div'
  4367. );
  4368. $this->assertTags($result, $expected);
  4369. $result = $this->Form->select(
  4370. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  4371. );
  4372. $expected = array(
  4373. 'input' => array(
  4374. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4375. ),
  4376. array('div' => array('class' => 'checkbox')),
  4377. array('input' => array(
  4378. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4379. 'value' => '1', 'id' => 'ModelMultiField1'
  4380. )),
  4381. array('label' => array('for' => 'ModelMultiField1')),
  4382. 'first',
  4383. '/label',
  4384. '/div'
  4385. );
  4386. $this->assertTags($result, $expected);
  4387. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4388. $result = $this->Form->select(
  4389. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4390. );
  4391. $expected = array(
  4392. 'input' => array(
  4393. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4394. ),
  4395. array('div' => array('class' => 'checkbox')),
  4396. array('input' => array(
  4397. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4398. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  4399. )),
  4400. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  4401. 'first',
  4402. '/label',
  4403. '/div',
  4404. array('div' => array('class' => 'checkbox')),
  4405. array('input' => array(
  4406. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4407. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4408. )),
  4409. array('label' => array('for' => 'ModelTagsArray')),
  4410. 'Array',
  4411. '/label',
  4412. '/div'
  4413. );
  4414. $this->assertTags($result, $expected);
  4415. }
  4416. /**
  4417. * test multiple checkboxes with div styles.
  4418. *
  4419. * @return void
  4420. */
  4421. public function testSelectMultipleCheckboxDiv() {
  4422. $result = $this->Form->select(
  4423. 'Model.tags',
  4424. array('first', 'second'),
  4425. array('multiple' => 'checkbox', 'class' => 'my-class')
  4426. );
  4427. $expected = array(
  4428. 'input' => array(
  4429. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4430. ),
  4431. array('div' => array('class' => 'my-class')),
  4432. array('input' => array(
  4433. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4434. 'value' => '0', 'id' => 'ModelTags0'
  4435. )),
  4436. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4437. '/div',
  4438. array('div' => array('class' => 'my-class')),
  4439. array('input' => array(
  4440. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4441. 'value' => '1', 'id' => 'ModelTags1'
  4442. )),
  4443. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4444. '/div'
  4445. );
  4446. $this->assertTags($result, $expected);
  4447. $result = $this->Form->input('Model.tags', array(
  4448. 'options' => array('first', 'second'),
  4449. 'multiple' => 'checkbox',
  4450. 'class' => 'my-class',
  4451. 'div' => false,
  4452. 'label' => false
  4453. ));
  4454. $this->assertTags($result, $expected);
  4455. $Contact = ClassRegistry::getObject('Contact');
  4456. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4457. $result = $this->Form->input('Contact.tags', array(
  4458. 'options' => array('one'),
  4459. 'multiple' => 'checkbox',
  4460. 'label' => false,
  4461. 'div' => false
  4462. ));
  4463. $expected = array(
  4464. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4465. array('div' => array('class' => 'checkbox form-error')),
  4466. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4467. array('label' => array('for' => 'ContactTags0')),
  4468. 'one',
  4469. '/label',
  4470. '/div'
  4471. );
  4472. $this->assertTags($result, $expected);
  4473. $result = $this->Form->input('Contact.tags', array(
  4474. 'options' => array('one'),
  4475. 'multiple' => 'checkbox',
  4476. 'class' => 'mycheckbox',
  4477. 'label' => false,
  4478. 'div' => false
  4479. ));
  4480. $expected = array(
  4481. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4482. array('div' => array('class' => 'mycheckbox form-error')),
  4483. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4484. array('label' => array('for' => 'ContactTags0')),
  4485. 'one',
  4486. '/label',
  4487. '/div'
  4488. );
  4489. $this->assertTags($result, $expected);
  4490. }
  4491. /**
  4492. * Checks the security hash array generated for multiple-input checkbox elements
  4493. *
  4494. * @return void
  4495. */
  4496. public function testSelectMultipleCheckboxSecurity() {
  4497. $this->Form->request['_Token'] = array('key' => 'testKey');
  4498. $this->assertEquals(array(), $this->Form->fields);
  4499. $result = $this->Form->select(
  4500. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4501. array('multiple' => 'checkbox')
  4502. );
  4503. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4504. $result = $this->Form->secure($this->Form->fields);
  4505. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4506. $this->assertRegExp('/"' . $key . '"/', $result);
  4507. }
  4508. /**
  4509. * Multiple select elements should always be secured as they always participate
  4510. * in the POST data.
  4511. *
  4512. * @return void
  4513. */
  4514. public function testSelectMultipleSecureWithNoOptions() {
  4515. $this->Form->request['_Token'] = array('key' => 'testkey');
  4516. $this->assertEquals(array(), $this->Form->fields);
  4517. $this->Form->select(
  4518. 'Model.select',
  4519. array(),
  4520. array('multiple' => true)
  4521. );
  4522. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4523. }
  4524. /**
  4525. * When a select box has no options it should not be added to the fields list
  4526. * as it always fail post validation.
  4527. *
  4528. * @return void
  4529. */
  4530. public function testSelectNoSecureWithNoOptions() {
  4531. $this->Form->request['_Token'] = array('key' => 'testkey');
  4532. $this->assertEquals(array(), $this->Form->fields);
  4533. $this->Form->select(
  4534. 'Model.select',
  4535. array()
  4536. );
  4537. $this->assertEquals(array(), $this->Form->fields);
  4538. $this->Form->select(
  4539. 'Model.select',
  4540. array(),
  4541. array('empty' => true)
  4542. );
  4543. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4544. }
  4545. /**
  4546. * testInputMultipleCheckboxes method
  4547. *
  4548. * test input() resulting in multi select elements being generated.
  4549. *
  4550. * @return void
  4551. */
  4552. public function testInputMultipleCheckboxes() {
  4553. $result = $this->Form->input('Model.multi_field', array(
  4554. 'options' => array('first', 'second', 'third'),
  4555. 'multiple' => 'checkbox'
  4556. ));
  4557. $expected = array(
  4558. array('div' => array('class' => 'input select')),
  4559. array('label' => array('for' => 'ModelMultiField')),
  4560. 'Multi Field',
  4561. '/label',
  4562. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4563. array('div' => array('class' => 'checkbox')),
  4564. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4565. array('label' => array('for' => 'ModelMultiField0')),
  4566. 'first',
  4567. '/label',
  4568. '/div',
  4569. array('div' => array('class' => 'checkbox')),
  4570. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4571. array('label' => array('for' => 'ModelMultiField1')),
  4572. 'second',
  4573. '/label',
  4574. '/div',
  4575. array('div' => array('class' => 'checkbox')),
  4576. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4577. array('label' => array('for' => 'ModelMultiField2')),
  4578. 'third',
  4579. '/label',
  4580. '/div',
  4581. '/div'
  4582. );
  4583. $this->assertTags($result, $expected);
  4584. $result = $this->Form->input('Model.multi_field', array(
  4585. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4586. 'multiple' => 'checkbox'
  4587. ));
  4588. $expected = array(
  4589. array('div' => array('class' => 'input select')),
  4590. array('label' => array('for' => 'ModelMultiField')),
  4591. 'Multi Field',
  4592. '/label',
  4593. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4594. array('div' => array('class' => 'checkbox')),
  4595. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4596. array('label' => array('for' => 'ModelMultiFieldA')),
  4597. 'first',
  4598. '/label',
  4599. '/div',
  4600. array('div' => array('class' => 'checkbox')),
  4601. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4602. array('label' => array('for' => 'ModelMultiFieldB')),
  4603. 'second',
  4604. '/label',
  4605. '/div',
  4606. array('div' => array('class' => 'checkbox')),
  4607. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4608. array('label' => array('for' => 'ModelMultiFieldC')),
  4609. 'third',
  4610. '/label',
  4611. '/div',
  4612. '/div'
  4613. );
  4614. $this->assertTags($result, $expected);
  4615. $result = $this->Form->input('Model.multi_field', array(
  4616. 'options' => array('1' => 'first'),
  4617. 'multiple' => 'checkbox',
  4618. 'label' => false,
  4619. 'div' => false
  4620. ));
  4621. $expected = array(
  4622. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4623. array('div' => array('class' => 'checkbox')),
  4624. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4625. array('label' => array('for' => 'ModelMultiField1')),
  4626. 'first',
  4627. '/label',
  4628. '/div'
  4629. );
  4630. $this->assertTags($result, $expected);
  4631. $result = $this->Form->input('Model.multi_field', array(
  4632. 'options' => array('2' => 'second'),
  4633. 'multiple' => 'checkbox',
  4634. 'label' => false,
  4635. 'div' => false
  4636. ));
  4637. $expected = array(
  4638. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4639. array('div' => array('class' => 'checkbox')),
  4640. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4641. array('label' => array('for' => 'ModelMultiField2')),
  4642. 'second',
  4643. '/label',
  4644. '/div'
  4645. );
  4646. $this->assertTags($result, $expected);
  4647. }
  4648. /**
  4649. * testSelectHiddenFieldOmission method
  4650. *
  4651. * test that select() with 'hiddenField' => false omits the hidden field
  4652. *
  4653. * @return void
  4654. */
  4655. public function testSelectHiddenFieldOmission() {
  4656. $result = $this->Form->select('Model.multi_field',
  4657. array('first', 'second'),
  4658. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4659. );
  4660. $expected = array(
  4661. array('div' => array('class' => 'checkbox')),
  4662. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4663. array('label' => array('for' => 'ModelMultiField0')),
  4664. 'first',
  4665. '/label',
  4666. '/div',
  4667. array('div' => array('class' => 'checkbox')),
  4668. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4669. array('label' => array('for' => 'ModelMultiField1')),
  4670. 'second',
  4671. '/label',
  4672. '/div'
  4673. );
  4674. $this->assertTags($result, $expected);
  4675. $result = $this->Form->input('Model.multi_field', array(
  4676. 'options' => array('first', 'second'),
  4677. 'multiple' => 'checkbox',
  4678. 'hiddenField' => false
  4679. ));
  4680. $expected = array(
  4681. array('div' => array('class' => 'input select')),
  4682. array('label' => array('for' => 'ModelMultiField')),
  4683. 'Multi Field',
  4684. '/label',
  4685. array('div' => array('class' => 'checkbox')),
  4686. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4687. array('label' => array('for' => 'ModelMultiField0')),
  4688. 'first',
  4689. '/label',
  4690. '/div',
  4691. array('div' => array('class' => 'checkbox')),
  4692. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4693. array('label' => array('for' => 'ModelMultiField1')),
  4694. 'second',
  4695. '/label',
  4696. '/div',
  4697. '/div'
  4698. );
  4699. $this->assertTags($result, $expected);
  4700. }
  4701. /**
  4702. * test that select() with multiple = checkbox works with overriding name attribute.
  4703. *
  4704. * @return void
  4705. */
  4706. public function testSelectCheckboxMultipleOverrideName() {
  4707. $result = $this->Form->input('category', array(
  4708. 'type' => 'select',
  4709. 'multiple' => 'checkbox',
  4710. 'name' => 'data[fish]',
  4711. 'options' => array('1', '2'),
  4712. 'div' => false,
  4713. 'label' => false,
  4714. ));
  4715. $expected = array(
  4716. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4717. array('div' => array('class' => 'checkbox')),
  4718. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4719. array('label' => array('for' => 'Category0')), '1', '/label',
  4720. '/div',
  4721. array('div' => array('class' => 'checkbox')),
  4722. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4723. array('label' => array('for' => 'Category1')), '2', '/label',
  4724. '/div'
  4725. );
  4726. $this->assertTags($result, $expected);
  4727. }
  4728. /**
  4729. * Test that 'id' overrides all the checkbox id's as well.
  4730. *
  4731. * @return void
  4732. */
  4733. public function testSelectCheckboxMultipleId() {
  4734. $result = $this->Form->select(
  4735. 'Model.multi_field',
  4736. array('first', 'second', 'third'),
  4737. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4738. );
  4739. $expected = array(
  4740. 'input' => array(
  4741. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4742. ),
  4743. array('div' => array('class' => 'checkbox')),
  4744. array('input' => array(
  4745. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4746. 'value' => '0', 'id' => 'CustomId0'
  4747. )),
  4748. array('label' => array('for' => 'CustomId0')),
  4749. 'first',
  4750. '/label',
  4751. '/div',
  4752. array('div' => array('class' => 'checkbox')),
  4753. array('input' => array(
  4754. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4755. 'value' => '1', 'id' => 'CustomId1'
  4756. )),
  4757. array('label' => array('for' => 'CustomId1')),
  4758. 'second',
  4759. '/label',
  4760. '/div',
  4761. array('div' => array('class' => 'checkbox')),
  4762. array('input' => array(
  4763. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4764. 'value' => '2', 'id' => 'CustomId2'
  4765. )),
  4766. array('label' => array('for' => 'CustomId2')),
  4767. 'third',
  4768. '/label',
  4769. '/div'
  4770. );
  4771. $this->assertTags($result, $expected);
  4772. }
  4773. /**
  4774. * testCheckbox method
  4775. *
  4776. * Test generation of checkboxes
  4777. *
  4778. * @return void
  4779. */
  4780. public function testCheckbox() {
  4781. $result = $this->Form->checkbox('Model.field');
  4782. $expected = array(
  4783. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4784. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4785. );
  4786. $this->assertTags($result, $expected);
  4787. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4788. $expected = array(
  4789. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4790. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4791. );
  4792. $this->assertTags($result, $expected);
  4793. $Contact = ClassRegistry::getObject('Contact');
  4794. $Contact->validationErrors['field'] = 1;
  4795. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4796. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4797. $expected = array(
  4798. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4799. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4800. );
  4801. $this->assertTags($result, $expected);
  4802. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4803. $expected = array(
  4804. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4805. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4806. );
  4807. $this->assertTags($result, $expected);
  4808. $this->Form->request->data['Contact']['field'] = '';
  4809. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4810. $expected = array(
  4811. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4812. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4813. );
  4814. $this->assertTags($result, $expected);
  4815. $Contact->validationErrors = array();
  4816. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4817. $expected = array(
  4818. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4819. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4820. );
  4821. $this->assertTags($result, $expected);
  4822. $this->Form->request->data['Contact']['published'] = 1;
  4823. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4824. $expected = array(
  4825. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4826. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4827. );
  4828. $this->assertTags($result, $expected);
  4829. $this->Form->request->data['Contact']['published'] = 0;
  4830. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4831. $expected = array(
  4832. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4833. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4834. );
  4835. $this->assertTags($result, $expected);
  4836. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4837. $expected = array(
  4838. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4839. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4840. );
  4841. $this->assertTags($result, $expected);
  4842. $result = $this->Form->checkbox('CustomField.1.value');
  4843. $expected = array(
  4844. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4845. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4846. );
  4847. $this->assertTags($result, $expected);
  4848. }
  4849. /**
  4850. * test checkbox() with a custom name attribute
  4851. *
  4852. * @return void
  4853. */
  4854. public function testCheckboxCustomNameAttribute() {
  4855. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4856. $expected = array(
  4857. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4858. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4859. );
  4860. $this->assertTags($result, $expected);
  4861. }
  4862. /**
  4863. * test the checked option for checkboxes.
  4864. *
  4865. * @return void
  4866. */
  4867. public function testCheckboxCheckedOption() {
  4868. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4869. $expected = array(
  4870. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4871. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4872. );
  4873. $this->assertTags($result, $expected);
  4874. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4875. $expected = array(
  4876. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4877. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4878. );
  4879. $this->assertTags($result, $expected);
  4880. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4881. $expected = array(
  4882. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4883. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4884. );
  4885. $this->assertTags($result, $expected);
  4886. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4887. $expected = array(
  4888. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4889. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4890. );
  4891. $this->assertTags($result, $expected);
  4892. $this->Form->request->data['Model']['field'] = 1;
  4893. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4894. $expected = array(
  4895. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4896. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4897. );
  4898. $this->assertTags($result, $expected);
  4899. }
  4900. /**
  4901. * Test that disabled attribute works on both the checkbox and hidden input.
  4902. *
  4903. * @return void
  4904. */
  4905. public function testCheckboxDisabling() {
  4906. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4907. $expected = array(
  4908. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4909. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4910. );
  4911. $this->assertTags($result, $expected);
  4912. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4913. $expected = array(
  4914. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4915. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4916. );
  4917. $this->assertTags($result, $expected);
  4918. }
  4919. /**
  4920. * Test that the hidden input for checkboxes can be omitted or set to a
  4921. * specific value.
  4922. *
  4923. * @return void
  4924. */
  4925. public function testCheckboxHiddenField() {
  4926. $result = $this->Form->input('UserForm.something', array(
  4927. 'type' => 'checkbox',
  4928. 'hiddenField' => false
  4929. ));
  4930. $expected = array(
  4931. 'div' => array('class' => 'input checkbox'),
  4932. array('input' => array(
  4933. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4934. 'value' => '1', 'id' => 'UserFormSomething'
  4935. )),
  4936. 'label' => array('for' => 'UserFormSomething'),
  4937. 'Something',
  4938. '/label',
  4939. '/div'
  4940. );
  4941. $this->assertTags($result, $expected);
  4942. $result = $this->Form->input('UserForm.something', array(
  4943. 'type' => 'checkbox',
  4944. 'value' => 'Y',
  4945. 'hiddenField' => 'N',
  4946. ));
  4947. $expected = array(
  4948. 'div' => array('class' => 'input checkbox'),
  4949. array('input' => array(
  4950. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  4951. 'value' => 'N', 'id' => 'UserFormSomething_'
  4952. )),
  4953. array('input' => array(
  4954. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4955. 'value' => 'Y', 'id' => 'UserFormSomething'
  4956. )),
  4957. 'label' => array('for' => 'UserFormSomething'),
  4958. 'Something',
  4959. '/label',
  4960. '/div'
  4961. );
  4962. $this->assertTags($result, $expected);
  4963. }
  4964. /**
  4965. * testDateTime method
  4966. *
  4967. * Test generation of date/time select elements
  4968. *
  4969. * @return void
  4970. */
  4971. public function testDateTime() {
  4972. extract($this->dateRegex);
  4973. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4974. $now = strtotime('now');
  4975. $expected = array(
  4976. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4977. $daysRegex,
  4978. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4979. date('j', $now),
  4980. '/option',
  4981. '*/select',
  4982. '-',
  4983. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4984. $monthsRegex,
  4985. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4986. date('F', $now),
  4987. '/option',
  4988. '*/select',
  4989. '-',
  4990. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4991. $yearsRegex,
  4992. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4993. date('Y', $now),
  4994. '/option',
  4995. '*/select',
  4996. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4997. $hoursRegex,
  4998. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4999. date('g', $now),
  5000. '/option',
  5001. '*/select',
  5002. ':',
  5003. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5004. $minutesRegex,
  5005. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5006. date('i', $now),
  5007. '/option',
  5008. '*/select',
  5009. ' ',
  5010. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5011. $meridianRegex,
  5012. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5013. date('a', $now),
  5014. '/option',
  5015. '*/select'
  5016. );
  5017. $this->assertTags($result, $expected);
  5018. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5019. $expected = array(
  5020. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5021. $daysRegex,
  5022. array('option' => array('value' => '')),
  5023. '/option',
  5024. '*/select',
  5025. '-',
  5026. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5027. $monthsRegex,
  5028. array('option' => array('value' => '')),
  5029. '/option',
  5030. '*/select',
  5031. '-',
  5032. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5033. $yearsRegex,
  5034. array('option' => array('value' => '')),
  5035. '/option',
  5036. '*/select',
  5037. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5038. $hoursRegex,
  5039. array('option' => array('value' => '')),
  5040. '/option',
  5041. '*/select',
  5042. ':',
  5043. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5044. $minutesRegex,
  5045. array('option' => array('value' => '')),
  5046. '/option',
  5047. '*/select',
  5048. ' ',
  5049. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5050. $meridianRegex,
  5051. array('option' => array('value' => '')),
  5052. '/option',
  5053. '*/select'
  5054. );
  5055. $this->assertTags($result, $expected);
  5056. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5057. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  5058. $this->assertTags($result, $expected);
  5059. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5060. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  5061. $this->assertTags($result, $expected);
  5062. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5063. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  5064. $expected = array(
  5065. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5066. $daysRegex,
  5067. array('option' => array('value' => '')),
  5068. '/option',
  5069. '*/select',
  5070. '-',
  5071. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5072. $monthsRegex,
  5073. array('option' => array('value' => '')),
  5074. '/option',
  5075. '*/select',
  5076. '-',
  5077. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5078. $yearsRegex,
  5079. array('option' => array('value' => '')),
  5080. '/option',
  5081. '*/select',
  5082. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5083. $hoursRegex,
  5084. array('option' => array('value' => '')),
  5085. '/option',
  5086. '*/select',
  5087. ':',
  5088. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5089. $minutesRegex,
  5090. array('option' => array('value' => '')),
  5091. '/option',
  5092. array('option' => array('value' => '00')),
  5093. '00',
  5094. '/option',
  5095. array('option' => array('value' => '05')),
  5096. '05',
  5097. '/option',
  5098. array('option' => array('value' => '10')),
  5099. '10',
  5100. '/option',
  5101. '*/select',
  5102. ' ',
  5103. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5104. $meridianRegex,
  5105. array('option' => array('value' => '')),
  5106. '/option',
  5107. '*/select'
  5108. );
  5109. $this->assertTags($result, $expected);
  5110. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5111. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5112. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5113. $this->Form->request->data['Contact']['data'] = null;
  5114. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5115. $expected = array(
  5116. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5117. $daysRegex,
  5118. array('option' => array('value' => '')),
  5119. '/option',
  5120. '*/select',
  5121. '-',
  5122. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5123. $monthsRegex,
  5124. array('option' => array('value' => '')),
  5125. '/option',
  5126. '*/select',
  5127. '-',
  5128. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5129. $yearsRegex,
  5130. array('option' => array('value' => '')),
  5131. '/option',
  5132. '*/select',
  5133. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5134. $hoursRegex,
  5135. array('option' => array('value' => '')),
  5136. '/option',
  5137. '*/select',
  5138. ':',
  5139. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5140. $minutesRegex,
  5141. array('option' => array('value' => '')),
  5142. '/option',
  5143. '*/select',
  5144. ' ',
  5145. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5146. $meridianRegex,
  5147. array('option' => array('value' => '')),
  5148. '/option',
  5149. '*/select'
  5150. );
  5151. $this->assertTags($result, $expected);
  5152. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5153. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  5154. $now = strtotime($this->Form->data['Model']['field']);
  5155. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  5156. $expected = array(
  5157. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5158. $daysRegex,
  5159. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5160. date('j', $now),
  5161. '/option',
  5162. '*/select',
  5163. '-',
  5164. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5165. $monthsRegex,
  5166. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5167. date('F', $now),
  5168. '/option',
  5169. '*/select',
  5170. '-',
  5171. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5172. $yearsRegex,
  5173. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5174. date('Y', $now),
  5175. '/option',
  5176. '*/select',
  5177. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5178. $hoursRegex,
  5179. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5180. date('g', $now),
  5181. '/option',
  5182. '*/select',
  5183. ':',
  5184. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5185. $minutesRegex,
  5186. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5187. date('i', $now),
  5188. '/option',
  5189. '*/select',
  5190. ' ',
  5191. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  5192. $meridianRegex,
  5193. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5194. date('a', $now),
  5195. '/option',
  5196. '*/select'
  5197. );
  5198. $this->assertTags($result, $expected);
  5199. $selected = strtotime('2008-10-26 12:33:00');
  5200. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  5201. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  5202. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  5203. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  5204. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  5205. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  5206. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  5207. $this->Form->create('Contact');
  5208. $result = $this->Form->input('published');
  5209. $now = strtotime('now');
  5210. $expected = array(
  5211. 'div' => array('class' => 'input date'),
  5212. 'label' => array('for' => 'ContactPublishedMonth'),
  5213. 'Published',
  5214. '/label',
  5215. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5216. $monthsRegex,
  5217. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5218. date('F', $now),
  5219. '/option',
  5220. '*/select',
  5221. '-',
  5222. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5223. $daysRegex,
  5224. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5225. date('j', $now),
  5226. '/option',
  5227. '*/select',
  5228. '-',
  5229. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5230. $yearsRegex,
  5231. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5232. date('Y', $now),
  5233. '/option',
  5234. '*/select',
  5235. '/div'
  5236. );
  5237. $this->assertTags($result, $expected);
  5238. $result = $this->Form->input('published2', array('type' => 'date'));
  5239. $now = strtotime('now');
  5240. $expected = array(
  5241. 'div' => array('class' => 'input date'),
  5242. 'label' => array('for' => 'ContactPublished2Month'),
  5243. 'Published2',
  5244. '/label',
  5245. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  5246. $monthsRegex,
  5247. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5248. date('F', $now),
  5249. '/option',
  5250. '*/select',
  5251. '-',
  5252. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  5253. $daysRegex,
  5254. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5255. date('j', $now),
  5256. '/option',
  5257. '*/select',
  5258. '-',
  5259. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  5260. $yearsRegex,
  5261. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5262. date('Y', $now),
  5263. '/option',
  5264. '*/select',
  5265. '/div'
  5266. );
  5267. $this->assertTags($result, $expected);
  5268. $this->Form->create('Contact');
  5269. $result = $this->Form->input('published', array('monthNames' => false));
  5270. $now = strtotime('now');
  5271. $expected = array(
  5272. 'div' => array('class' => 'input date'),
  5273. 'label' => array('for' => 'ContactPublishedMonth'),
  5274. 'Published',
  5275. '/label',
  5276. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5277. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5278. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5279. date('m', $now),
  5280. '/option',
  5281. '*/select',
  5282. '-',
  5283. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5284. $daysRegex,
  5285. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5286. date('j', $now),
  5287. '/option',
  5288. '*/select',
  5289. '-',
  5290. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5291. $yearsRegex,
  5292. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5293. date('Y', $now),
  5294. '/option',
  5295. '*/select',
  5296. '/div'
  5297. );
  5298. $this->assertTags($result, $expected);
  5299. $result = $this->Form->input('published', array(
  5300. 'timeFormat' => 24,
  5301. 'interval' => 5,
  5302. 'selected' => strtotime('2009-09-03 13:37:00'),
  5303. 'type' => 'datetime'
  5304. ));
  5305. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5306. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  5307. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  5308. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  5309. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  5310. }
  5311. /**
  5312. * Test that empty values don't trigger errors.
  5313. *
  5314. * @return void
  5315. */
  5316. public function testDateTimeNoErrorsOnEmptyData() {
  5317. $this->Form->request->data['Contact'] = array(
  5318. 'date' => array(
  5319. 'day' => '',
  5320. 'month' => '',
  5321. 'year' => '',
  5322. 'hour' => '',
  5323. 'min' => '',
  5324. 'meridian' => ''
  5325. )
  5326. );
  5327. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5328. $this->assertNotEmpty($result);
  5329. }
  5330. /**
  5331. * test that datetime() and default values work.
  5332. *
  5333. * @return void
  5334. */
  5335. public function testDatetimeWithDefault() {
  5336. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  5337. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5338. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5339. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5340. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  5341. 'default' => '2009-06-01 11:15:30'
  5342. ));
  5343. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5344. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5345. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5346. }
  5347. /**
  5348. * test that bogus non-date time data doesn't cause errors.
  5349. *
  5350. * @return void
  5351. */
  5352. public function testDateTimeWithBogusData() {
  5353. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  5354. $this->assertNotRegExp('/selected="selected">\d/', $result);
  5355. }
  5356. /**
  5357. * testDateTimeEmptyAsArray
  5358. *
  5359. * @return void
  5360. */
  5361. public function testDateTimeEmptyAsArray() {
  5362. $result = $this->Form->dateTime('Contact.date',
  5363. 'DMY',
  5364. '12',
  5365. array(
  5366. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  5367. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  5368. )
  5369. )
  5370. );
  5371. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5372. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5373. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5374. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5375. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5376. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5377. $result = $this->Form->dateTime('Contact.date',
  5378. 'DMY',
  5379. '12',
  5380. array(
  5381. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  5382. )
  5383. );
  5384. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5385. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5386. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5387. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  5388. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  5389. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  5390. }
  5391. /**
  5392. * testFormDateTimeMulti method
  5393. *
  5394. * test multiple datetime element generation
  5395. *
  5396. * @return void
  5397. */
  5398. public function testFormDateTimeMulti() {
  5399. extract($this->dateRegex);
  5400. $result = $this->Form->dateTime('Contact.1.updated');
  5401. $expected = array(
  5402. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  5403. $daysRegex,
  5404. array('option' => array('value' => '')),
  5405. '/option',
  5406. '*/select',
  5407. '-',
  5408. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  5409. $monthsRegex,
  5410. array('option' => array('value' => '')),
  5411. '/option',
  5412. '*/select',
  5413. '-',
  5414. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  5415. $yearsRegex,
  5416. array('option' => array('value' => '')),
  5417. '/option',
  5418. '*/select',
  5419. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  5420. $hoursRegex,
  5421. array('option' => array('value' => '')),
  5422. '/option',
  5423. '*/select',
  5424. ':',
  5425. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5426. $minutesRegex,
  5427. array('option' => array('value' => '')),
  5428. '/option',
  5429. '*/select',
  5430. ' ',
  5431. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5432. $meridianRegex,
  5433. array('option' => array('value' => '')),
  5434. '/option',
  5435. '*/select'
  5436. );
  5437. $this->assertTags($result, $expected);
  5438. $result = $this->Form->dateTime('Contact.2.updated');
  5439. $expected = array(
  5440. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5441. $daysRegex,
  5442. array('option' => array('value' => '')),
  5443. '/option',
  5444. '*/select',
  5445. '-',
  5446. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5447. $monthsRegex,
  5448. array('option' => array('value' => '')),
  5449. '/option',
  5450. '*/select',
  5451. '-',
  5452. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5453. $yearsRegex,
  5454. array('option' => array('value' => '')),
  5455. '/option',
  5456. '*/select',
  5457. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5458. $hoursRegex,
  5459. array('option' => array('value' => '')),
  5460. '/option',
  5461. '*/select',
  5462. ':',
  5463. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5464. $minutesRegex,
  5465. array('option' => array('value' => '')),
  5466. '/option',
  5467. '*/select',
  5468. ' ',
  5469. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5470. $meridianRegex,
  5471. array('option' => array('value' => '')),
  5472. '/option',
  5473. '*/select'
  5474. );
  5475. $this->assertTags($result, $expected);
  5476. }
  5477. /**
  5478. * When changing the date format, the label should always focus the first select box when
  5479. * clicked.
  5480. *
  5481. * @return void
  5482. */
  5483. public function testDateTimeLabelIdMatchesFirstInput() {
  5484. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5485. $this->assertContains('label for="ModelDateMonth"', $result);
  5486. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5487. $this->assertContains('label for="ModelDateDay"', $result);
  5488. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5489. $this->assertContains('label for="ModelDateYear"', $result);
  5490. }
  5491. /**
  5492. * testMonth method
  5493. *
  5494. * @return void
  5495. */
  5496. public function testMonth() {
  5497. $result = $this->Form->month('Model.field');
  5498. $expected = array(
  5499. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5500. array('option' => array('value' => '')),
  5501. '/option',
  5502. array('option' => array('value' => '01')),
  5503. date('F', strtotime('2008-01-01 00:00:00')),
  5504. '/option',
  5505. array('option' => array('value' => '02')),
  5506. date('F', strtotime('2008-02-01 00:00:00')),
  5507. '/option',
  5508. '*/select',
  5509. );
  5510. $this->assertTags($result, $expected);
  5511. $result = $this->Form->month('Model.field', array('empty' => true));
  5512. $expected = array(
  5513. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5514. array('option' => array('value' => '')),
  5515. '/option',
  5516. array('option' => array('value' => '01')),
  5517. date('F', strtotime('2008-01-01 00:00:00')),
  5518. '/option',
  5519. array('option' => array('value' => '02')),
  5520. date('F', strtotime('2008-02-01 00:00:00')),
  5521. '/option',
  5522. '*/select',
  5523. );
  5524. $this->assertTags($result, $expected);
  5525. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5526. $expected = array(
  5527. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5528. array('option' => array('value' => '')),
  5529. '/option',
  5530. array('option' => array('value' => '01')),
  5531. '01',
  5532. '/option',
  5533. array('option' => array('value' => '02')),
  5534. '02',
  5535. '/option',
  5536. '*/select',
  5537. );
  5538. $this->assertTags($result, $expected);
  5539. $monthNames = array(
  5540. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5541. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5542. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5543. $expected = array(
  5544. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5545. array('option' => array('value' => '')),
  5546. '/option',
  5547. array('option' => array('value' => '01')),
  5548. 'Jan',
  5549. '/option',
  5550. array('option' => array('value' => '02')),
  5551. 'Feb',
  5552. '/option',
  5553. '*/select',
  5554. );
  5555. $this->assertTags($result, $expected);
  5556. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5557. $result = $this->Form->month('Project.release');
  5558. $expected = array(
  5559. array('select' => array('name' => 'data[Project][release][month]', 'id' => 'ProjectReleaseMonth')),
  5560. array('option' => array('value' => '')),
  5561. '/option',
  5562. array('option' => array('value' => '01')),
  5563. 'January',
  5564. '/option',
  5565. array('option' => array('value' => '02', 'selected' => 'selected')),
  5566. 'February',
  5567. '/option',
  5568. '*/select',
  5569. );
  5570. $this->assertTags($result, $expected);
  5571. }
  5572. /**
  5573. * testDay method
  5574. *
  5575. * @return void
  5576. */
  5577. public function testDay() {
  5578. extract($this->dateRegex);
  5579. $result = $this->Form->day('Model.field', array('value' => false));
  5580. $expected = array(
  5581. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5582. array('option' => array('value' => '')),
  5583. '/option',
  5584. array('option' => array('value' => '01')),
  5585. '1',
  5586. '/option',
  5587. array('option' => array('value' => '02')),
  5588. '2',
  5589. '/option',
  5590. $daysRegex,
  5591. '/select',
  5592. );
  5593. $this->assertTags($result, $expected);
  5594. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5595. $result = $this->Form->day('Model.field');
  5596. $expected = array(
  5597. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5598. array('option' => array('value' => '')),
  5599. '/option',
  5600. array('option' => array('value' => '01')),
  5601. '1',
  5602. '/option',
  5603. array('option' => array('value' => '02')),
  5604. '2',
  5605. '/option',
  5606. $daysRegex,
  5607. array('option' => array('value' => '10', 'selected' => 'selected')),
  5608. '10',
  5609. '/option',
  5610. $daysRegex,
  5611. '/select',
  5612. );
  5613. $this->assertTags($result, $expected);
  5614. $this->Form->request->data['Model']['field'] = '';
  5615. $result = $this->Form->day('Model.field', array('value' => '10'));
  5616. $expected = array(
  5617. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5618. array('option' => array('value' => '')),
  5619. '/option',
  5620. array('option' => array('value' => '01')),
  5621. '1',
  5622. '/option',
  5623. array('option' => array('value' => '02')),
  5624. '2',
  5625. '/option',
  5626. $daysRegex,
  5627. array('option' => array('value' => '10', 'selected' => 'selected')),
  5628. '10',
  5629. '/option',
  5630. $daysRegex,
  5631. '/select',
  5632. );
  5633. $this->assertTags($result, $expected);
  5634. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5635. $result = $this->Form->day('Model.field', array('value' => true));
  5636. $expected = array(
  5637. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5638. array('option' => array('value' => '')),
  5639. '/option',
  5640. array('option' => array('value' => '01')),
  5641. '1',
  5642. '/option',
  5643. array('option' => array('value' => '02')),
  5644. '2',
  5645. '/option',
  5646. $daysRegex,
  5647. array('option' => array('value' => '10', 'selected' => 'selected')),
  5648. '10',
  5649. '/option',
  5650. $daysRegex,
  5651. '/select',
  5652. );
  5653. $this->assertTags($result, $expected);
  5654. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5655. $result = $this->Form->day('Project.release');
  5656. $expected = array(
  5657. array('select' => array('name' => 'data[Project][release][day]', 'id' => 'ProjectReleaseDay')),
  5658. array('option' => array('value' => '')),
  5659. '/option',
  5660. array('option' => array('value' => '01')),
  5661. '1',
  5662. '/option',
  5663. array('option' => array('value' => '02')),
  5664. '2',
  5665. '/option',
  5666. $daysRegex,
  5667. array('option' => array('value' => '10', 'selected' => 'selected')),
  5668. '10',
  5669. '/option',
  5670. $daysRegex,
  5671. '/select',
  5672. );
  5673. $this->assertTags($result, $expected);
  5674. }
  5675. /**
  5676. * testMinute method
  5677. *
  5678. * @return void
  5679. */
  5680. public function testMinute() {
  5681. extract($this->dateRegex);
  5682. $result = $this->Form->minute('Model.field');
  5683. $expected = array(
  5684. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5685. array('option' => array('value' => '')),
  5686. '/option',
  5687. array('option' => array('value' => '00')),
  5688. '00',
  5689. '/option',
  5690. array('option' => array('value' => '01')),
  5691. '01',
  5692. '/option',
  5693. array('option' => array('value' => '02')),
  5694. '02',
  5695. '/option',
  5696. $minutesRegex,
  5697. '/select',
  5698. );
  5699. $this->assertTags($result, $expected);
  5700. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5701. $result = $this->Form->minute('Model.field');
  5702. $expected = array(
  5703. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5704. array('option' => array('value' => '')),
  5705. '/option',
  5706. array('option' => array('value' => '00')),
  5707. '00',
  5708. '/option',
  5709. array('option' => array('value' => '01')),
  5710. '01',
  5711. '/option',
  5712. array('option' => array('value' => '02')),
  5713. '02',
  5714. '/option',
  5715. $minutesRegex,
  5716. array('option' => array('value' => '12', 'selected' => 'selected')),
  5717. '12',
  5718. '/option',
  5719. $minutesRegex,
  5720. '/select',
  5721. );
  5722. $this->assertTags($result, $expected);
  5723. $this->Form->request->data['Model']['field'] = '';
  5724. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5725. $expected = array(
  5726. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5727. array('option' => array('value' => '')),
  5728. '/option',
  5729. array('option' => array('value' => '00')),
  5730. '00',
  5731. '/option',
  5732. array('option' => array('value' => '05')),
  5733. '05',
  5734. '/option',
  5735. array('option' => array('value' => '10')),
  5736. '10',
  5737. '/option',
  5738. $minutesRegex,
  5739. '/select',
  5740. );
  5741. $this->assertTags($result, $expected);
  5742. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5743. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5744. $expected = array(
  5745. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5746. array('option' => array('value' => '')),
  5747. '/option',
  5748. array('option' => array('value' => '00')),
  5749. '00',
  5750. '/option',
  5751. array('option' => array('value' => '05')),
  5752. '05',
  5753. '/option',
  5754. array('option' => array('value' => '10', 'selected' => 'selected')),
  5755. '10',
  5756. '/option',
  5757. $minutesRegex,
  5758. '/select',
  5759. );
  5760. $this->assertTags($result, $expected);
  5761. }
  5762. /**
  5763. * testHour method
  5764. *
  5765. * @return void
  5766. */
  5767. public function testHour() {
  5768. extract($this->dateRegex);
  5769. $result = $this->Form->hour('Model.field', false);
  5770. $expected = array(
  5771. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5772. array('option' => array('value' => '')),
  5773. '/option',
  5774. array('option' => array('value' => '01')),
  5775. '1',
  5776. '/option',
  5777. array('option' => array('value' => '02')),
  5778. '2',
  5779. '/option',
  5780. $hoursRegex,
  5781. '/select',
  5782. );
  5783. $this->assertTags($result, $expected);
  5784. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5785. $result = $this->Form->hour('Model.field', false);
  5786. $expected = array(
  5787. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5788. array('option' => array('value' => '')),
  5789. '/option',
  5790. array('option' => array('value' => '01')),
  5791. '1',
  5792. '/option',
  5793. array('option' => array('value' => '02')),
  5794. '2',
  5795. '/option',
  5796. $hoursRegex,
  5797. array('option' => array('value' => '12', 'selected' => 'selected')),
  5798. '12',
  5799. '/option',
  5800. '/select',
  5801. );
  5802. $this->assertTags($result, $expected);
  5803. $this->Form->request->data['Model']['field'] = '';
  5804. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5805. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5806. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  5807. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5808. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5809. $result = $this->Form->hour('Model.field', true);
  5810. $expected = array(
  5811. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5812. array('option' => array('value' => '')),
  5813. '/option',
  5814. array('option' => array('value' => '00', 'selected' => 'selected')),
  5815. '0',
  5816. '/option',
  5817. array('option' => array('value' => '01')),
  5818. '1',
  5819. '/option',
  5820. array('option' => array('value' => '02')),
  5821. '2',
  5822. '/option',
  5823. $hoursRegex,
  5824. '/select',
  5825. );
  5826. $this->assertTags($result, $expected);
  5827. unset($this->Form->request->data['Model']['field']);
  5828. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5829. $thisHour = date('H');
  5830. $optValue = date('G');
  5831. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5832. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  5833. $result = $this->Form->hour('Model.field', true);
  5834. $expected = array(
  5835. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5836. array('option' => array('value' => '')),
  5837. '/option',
  5838. array('option' => array('value' => '00')),
  5839. '0',
  5840. '/option',
  5841. array('option' => array('value' => '01', 'selected' => 'selected')),
  5842. '1',
  5843. '/option',
  5844. array('option' => array('value' => '02')),
  5845. '2',
  5846. '/option',
  5847. $hoursRegex,
  5848. '/select',
  5849. );
  5850. $this->assertTags($result, $expected);
  5851. }
  5852. /**
  5853. * testYear method
  5854. *
  5855. * @return void
  5856. */
  5857. public function testYear() {
  5858. $result = $this->Form->year('Model.field', 2006, 2007);
  5859. $expected = array(
  5860. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5861. array('option' => array('value' => '')),
  5862. '/option',
  5863. array('option' => array('value' => '2007')),
  5864. '2007',
  5865. '/option',
  5866. array('option' => array('value' => '2006')),
  5867. '2006',
  5868. '/option',
  5869. '/select',
  5870. );
  5871. $this->assertTags($result, $expected);
  5872. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5873. $expected = array(
  5874. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5875. array('option' => array('value' => '')),
  5876. '/option',
  5877. array('option' => array('value' => '2006')),
  5878. '2006',
  5879. '/option',
  5880. array('option' => array('value' => '2007')),
  5881. '2007',
  5882. '/option',
  5883. '/select',
  5884. );
  5885. $this->assertTags($result, $expected);
  5886. $this->request->data['Contact']['published'] = '';
  5887. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5888. $expected = array(
  5889. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5890. array('option' => array('value' => '')),
  5891. '/option',
  5892. array('option' => array('value' => '2007')),
  5893. '2007',
  5894. '/option',
  5895. array('option' => array('value' => '2006')),
  5896. '2006',
  5897. '/option',
  5898. '/select',
  5899. );
  5900. $this->assertTags($result, $expected);
  5901. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5902. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5903. $expected = array(
  5904. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5905. array('option' => array('value' => '2007')),
  5906. '2007',
  5907. '/option',
  5908. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5909. '2006',
  5910. '/option',
  5911. '/select',
  5912. );
  5913. $this->assertTags($result, $expected);
  5914. $this->Form->request->data['Contact']['published'] = '';
  5915. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5916. $expected = array(
  5917. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5918. array('option' => array('value' => '')),
  5919. '/option',
  5920. array('option' => array('value' => '2007')),
  5921. '2007',
  5922. '/option',
  5923. array('option' => array('value' => '2006')),
  5924. '2006',
  5925. '/option',
  5926. '/select',
  5927. );
  5928. $this->assertTags($result, $expected);
  5929. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5930. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5931. $expected = array(
  5932. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5933. array('option' => array('value' => '2007')),
  5934. '2007',
  5935. '/option',
  5936. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5937. '2006',
  5938. '/option',
  5939. '/select',
  5940. );
  5941. $this->assertTags($result, $expected);
  5942. $this->Form->request->data['Contact']['published'] = '';
  5943. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5944. $expected = array(
  5945. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5946. array('option' => array('value' => '')),
  5947. '/option',
  5948. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5949. '2007',
  5950. '/option',
  5951. array('option' => array('value' => '2006')),
  5952. '2006',
  5953. '/option',
  5954. '/select',
  5955. );
  5956. $this->assertTags($result, $expected);
  5957. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5958. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5959. $expected = array(
  5960. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5961. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5962. '2007',
  5963. '/option',
  5964. array('option' => array('value' => '2006')),
  5965. '2006',
  5966. '/option',
  5967. '/select',
  5968. );
  5969. $this->assertTags($result, $expected);
  5970. $this->Form->request->data['Contact']['published'] = '';
  5971. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5972. $expected = array(
  5973. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5974. array('option' => array('value' => '2008')),
  5975. '2008',
  5976. '/option',
  5977. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5978. '2007',
  5979. '/option',
  5980. array('option' => array('value' => '2006')),
  5981. '2006',
  5982. '/option',
  5983. '/select',
  5984. );
  5985. $this->assertTags($result, $expected);
  5986. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5987. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5988. $expected = array(
  5989. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5990. array('option' => array('value' => '2008')),
  5991. '2008',
  5992. '/option',
  5993. array('option' => array('value' => '2007')),
  5994. '2007',
  5995. '/option',
  5996. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5997. '2006',
  5998. '/option',
  5999. '/select',
  6000. );
  6001. $this->assertTags($result, $expected);
  6002. $this->Form->request->data = array();
  6003. $this->Form->create('Contact');
  6004. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  6005. $expected = array(
  6006. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6007. array('option' => array('value' => '2008')),
  6008. '2008',
  6009. '/option',
  6010. array('option' => array('value' => '2007')),
  6011. '2007',
  6012. '/option',
  6013. array('option' => array('value' => '2006')),
  6014. '2006',
  6015. '/option',
  6016. '/select',
  6017. );
  6018. $this->assertTags($result, $expected);
  6019. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  6020. $this->assertContains('data[Contact][published][year]', $result);
  6021. }
  6022. /**
  6023. * testYearAutoExpandRange method
  6024. *
  6025. * @return void
  6026. */
  6027. public function testYearAutoExpandRange() {
  6028. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6029. $result = $this->Form->year('User.birthday');
  6030. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6031. $result = $matches[1];
  6032. $expected = range(date('Y') + 20, 1930);
  6033. $this->assertEquals($result, $expected);
  6034. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6035. $result = $this->Form->year('Project.release');
  6036. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6037. $result = $matches[1];
  6038. $expected = range(2050, date('Y') - 20);
  6039. $this->assertEquals($result, $expected);
  6040. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6041. $result = $this->Form->year('Project.release', 1890, 1900);
  6042. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6043. $result = $matches[1];
  6044. $expected = range(1900, 1881);
  6045. $this->assertEquals($result, $expected);
  6046. }
  6047. /**
  6048. * testTextArea method
  6049. *
  6050. * @return void
  6051. */
  6052. public function testTextArea() {
  6053. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  6054. $result = $this->Form->textarea('Model.field');
  6055. $expected = array(
  6056. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6057. 'some test data',
  6058. '/textarea',
  6059. );
  6060. $this->assertTags($result, $expected);
  6061. $result = $this->Form->textarea('Model.tmp');
  6062. $expected = array(
  6063. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  6064. '/textarea',
  6065. );
  6066. $this->assertTags($result, $expected);
  6067. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6068. $result = $this->Form->textarea('Model.field');
  6069. $expected = array(
  6070. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6071. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6072. '/textarea',
  6073. );
  6074. $this->assertTags($result, $expected);
  6075. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6076. $result = $this->Form->textarea('Model.field', array('escape' => false));
  6077. $expected = array(
  6078. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6079. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6080. '/textarea',
  6081. );
  6082. $this->assertTags($result, $expected);
  6083. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  6084. $result = $this->Form->textarea('Model.0.OtherModel.field');
  6085. $expected = array(
  6086. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  6087. '/textarea'
  6088. );
  6089. $this->assertTags($result, $expected);
  6090. }
  6091. /**
  6092. * testTextAreaWithStupidCharacters method
  6093. *
  6094. * test text area with non-ascii characters
  6095. *
  6096. * @return void
  6097. */
  6098. public function testTextAreaWithStupidCharacters() {
  6099. $this->loadFixtures('Post');
  6100. $result = $this->Form->input('Post.content', array(
  6101. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  6102. ));
  6103. $expected = array(
  6104. 'div' => array('class' => 'input textarea'),
  6105. 'label' => array('for' => 'PostContent'),
  6106. 'Current Text',
  6107. '/label',
  6108. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  6109. 'GREAT®',
  6110. '/textarea',
  6111. '/div'
  6112. );
  6113. $this->assertTags($result, $expected);
  6114. }
  6115. /**
  6116. * testHiddenField method
  6117. *
  6118. * @return void
  6119. */
  6120. public function testHiddenField() {
  6121. $Contact = ClassRegistry::getObject('Contact');
  6122. $Contact->validationErrors['field'] = 1;
  6123. $this->Form->request->data['Contact']['field'] = 'test';
  6124. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  6125. $this->assertTags($result, array(
  6126. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  6127. );
  6128. }
  6129. /**
  6130. * testFileUploadField method
  6131. *
  6132. * @return void
  6133. */
  6134. public function testFileUploadField() {
  6135. $result = $this->Form->file('Model.upload');
  6136. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6137. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  6138. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  6139. $expected = array(
  6140. 'div' => array('class' => 'input file'),
  6141. 'label' => array('for' => 'ModelUpload'),
  6142. 'Upload',
  6143. '/label',
  6144. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  6145. '/div'
  6146. );
  6147. $this->assertTags($result, $expected);
  6148. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6149. $result = $this->Form->file('Model.upload');
  6150. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6151. }
  6152. /**
  6153. * test File upload input on a model not used in create();
  6154. *
  6155. * @return void
  6156. */
  6157. public function testFileUploadOnOtherModel() {
  6158. $this->Form->create('ValidateUser', array('type' => 'file'));
  6159. $result = $this->Form->file('ValidateProfile.city');
  6160. $expected = array(
  6161. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  6162. );
  6163. $this->assertTags($result, $expected);
  6164. }
  6165. /**
  6166. * testButton method
  6167. *
  6168. * @return void
  6169. */
  6170. public function testButton() {
  6171. $result = $this->Form->button('Hi');
  6172. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  6173. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  6174. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  6175. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  6176. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  6177. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  6178. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  6179. $result = $this->Form->button('No type', array('type' => false));
  6180. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  6181. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  6182. $this->assertNotRegExp('/\&039/', $result);
  6183. }
  6184. /**
  6185. * Test that button() makes unlocked fields by default.
  6186. *
  6187. * @return void
  6188. */
  6189. public function testButtonUnlockedByDefault() {
  6190. $this->Form->request->params['_Token']['key'] = 'secured';
  6191. $this->Form->button('Save', array('name' => 'save'));
  6192. $this->Form->button('Clear');
  6193. $result = $this->Form->unlockField();
  6194. $this->assertEquals(array('save'), $result);
  6195. }
  6196. /**
  6197. * testPostButton method
  6198. *
  6199. * @return void
  6200. */
  6201. public function testPostButton() {
  6202. $result = $this->Form->postButton('Hi', '/controller/action');
  6203. $this->assertTags($result, array(
  6204. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  6205. 'div' => array('style' => 'display:none;'),
  6206. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6207. '/div',
  6208. 'button' => array('type' => 'submit'),
  6209. 'Hi',
  6210. '/button',
  6211. '/form'
  6212. ));
  6213. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  6214. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  6215. }
  6216. /**
  6217. * Test that postButton adds _Token fields.
  6218. *
  6219. * @return void
  6220. */
  6221. public function testSecurePostButton() {
  6222. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6223. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6224. $expected = array(
  6225. 'form' => array(
  6226. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6227. ),
  6228. array('div' => array('style' => 'display:none;')),
  6229. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6230. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6231. '/div',
  6232. 'button' => array('type' => 'submit'),
  6233. 'Delete',
  6234. '/button',
  6235. array('div' => array('style' => 'display:none;')),
  6236. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6237. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6238. '/div',
  6239. '/form',
  6240. );
  6241. $this->assertTags($result, $expected);
  6242. }
  6243. /**
  6244. * testPostLink method
  6245. *
  6246. * @return void
  6247. */
  6248. public function testPostLink() {
  6249. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6250. $this->assertTags($result, array(
  6251. 'form' => array(
  6252. 'method' => 'post', 'action' => '/posts/delete/1',
  6253. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6254. ),
  6255. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6256. '/form',
  6257. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6258. 'Delete',
  6259. '/a'
  6260. ));
  6261. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  6262. $this->assertTags($result, array(
  6263. 'form' => array(
  6264. 'method' => 'post', 'action' => '/posts/delete/1',
  6265. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6266. ),
  6267. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  6268. '/form',
  6269. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6270. 'Delete',
  6271. '/a'
  6272. ));
  6273. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  6274. $this->assertTags($result, array(
  6275. 'form' => array(
  6276. 'method' => 'post', 'action' => '/posts/delete/1',
  6277. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6278. ),
  6279. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6280. '/form',
  6281. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6282. 'Delete',
  6283. '/a'
  6284. ));
  6285. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  6286. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  6287. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  6288. $this->assertTags($result, array(
  6289. 'form' => array(
  6290. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6291. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6292. ),
  6293. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6294. '/form',
  6295. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6296. 'Delete',
  6297. '/a'
  6298. ));
  6299. }
  6300. /**
  6301. * Test that postLink adds _Token fields.
  6302. *
  6303. * @return void
  6304. */
  6305. public function testSecurePostLink() {
  6306. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6307. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6308. $expected = array(
  6309. 'form' => array(
  6310. 'method' => 'post', 'action' => '/posts/delete/1',
  6311. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6312. ),
  6313. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6314. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6315. 'div' => array('style' => 'display:none;'),
  6316. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6317. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6318. '/div',
  6319. '/form',
  6320. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6321. 'Delete',
  6322. '/a'
  6323. );
  6324. $this->assertTags($result, $expected);
  6325. }
  6326. /**
  6327. * testSubmitButton method
  6328. *
  6329. * @return void
  6330. */
  6331. public function testSubmitButton() {
  6332. $result = $this->Form->submit('');
  6333. $expected = array(
  6334. 'div' => array('class' => 'submit'),
  6335. 'input' => array('type' => 'submit', 'value' => ''),
  6336. '/div'
  6337. );
  6338. $this->assertTags($result, $expected);
  6339. $result = $this->Form->submit('Test Submit');
  6340. $expected = array(
  6341. 'div' => array('class' => 'submit'),
  6342. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6343. '/div'
  6344. );
  6345. $this->assertTags($result, $expected);
  6346. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  6347. $expected = array(
  6348. 'span' => array('class' => 'submit'),
  6349. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6350. '/span'
  6351. );
  6352. $this->assertTags($result, $expected);
  6353. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  6354. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  6355. $this->assertTags($result, $expected);
  6356. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  6357. $expected = array(
  6358. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  6359. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6360. '/div'
  6361. );
  6362. $this->assertTags($result, $expected);
  6363. $result = $this->Form->submit('Next >');
  6364. $expected = array(
  6365. 'div' => array('class' => 'submit'),
  6366. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  6367. '/div'
  6368. );
  6369. $this->assertTags($result, $expected);
  6370. $result = $this->Form->submit('Next >', array('escape' => false));
  6371. $expected = array(
  6372. 'div' => array('class' => 'submit'),
  6373. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  6374. '/div'
  6375. );
  6376. $this->assertTags($result, $expected);
  6377. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  6378. $expected = array(
  6379. 'div' => array('class' => 'submit'),
  6380. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  6381. '/div'
  6382. );
  6383. $this->assertTags($result, $expected);
  6384. $before = '--before--';
  6385. $after = '--after--';
  6386. $result = $this->Form->submit('Test', array('before' => $before));
  6387. $expected = array(
  6388. 'div' => array('class' => 'submit'),
  6389. '--before--',
  6390. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6391. '/div'
  6392. );
  6393. $this->assertTags($result, $expected);
  6394. $result = $this->Form->submit('Test', array('after' => $after));
  6395. $expected = array(
  6396. 'div' => array('class' => 'submit'),
  6397. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6398. '--after--',
  6399. '/div'
  6400. );
  6401. $this->assertTags($result, $expected);
  6402. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  6403. $expected = array(
  6404. 'div' => array('class' => 'submit'),
  6405. '--before--',
  6406. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6407. '--after--',
  6408. '/div'
  6409. );
  6410. $this->assertTags($result, $expected);
  6411. }
  6412. /**
  6413. * test image submit types.
  6414. *
  6415. * @return void
  6416. */
  6417. public function testSubmitImage() {
  6418. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6419. $expected = array(
  6420. 'div' => array('class' => 'submit'),
  6421. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  6422. '/div'
  6423. );
  6424. $this->assertTags($result, $expected);
  6425. $result = $this->Form->submit('/relative/cake.power.gif');
  6426. $expected = array(
  6427. 'div' => array('class' => 'submit'),
  6428. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  6429. '/div'
  6430. );
  6431. $this->assertTags($result, $expected);
  6432. $result = $this->Form->submit('cake.power.gif');
  6433. $expected = array(
  6434. 'div' => array('class' => 'submit'),
  6435. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6436. '/div'
  6437. );
  6438. $this->assertTags($result, $expected);
  6439. $result = $this->Form->submit('Not.an.image');
  6440. $expected = array(
  6441. 'div' => array('class' => 'submit'),
  6442. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6443. '/div'
  6444. );
  6445. $this->assertTags($result, $expected);
  6446. $after = '--after--';
  6447. $before = '--before--';
  6448. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  6449. $expected = array(
  6450. 'div' => array('class' => 'submit'),
  6451. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6452. '--after--',
  6453. '/div'
  6454. );
  6455. $this->assertTags($result, $expected);
  6456. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  6457. $expected = array(
  6458. 'div' => array('class' => 'submit'),
  6459. '--before--',
  6460. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6461. '/div'
  6462. );
  6463. $this->assertTags($result, $expected);
  6464. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  6465. $expected = array(
  6466. 'div' => array('class' => 'submit'),
  6467. '--before--',
  6468. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6469. '--after--',
  6470. '/div'
  6471. );
  6472. $this->assertTags($result, $expected);
  6473. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  6474. $expected = array(
  6475. 'div' => array('class' => 'submit'),
  6476. '--before--',
  6477. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6478. '--after--',
  6479. '/div'
  6480. );
  6481. $this->assertTags($result, $expected);
  6482. }
  6483. /**
  6484. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6485. * be submitted at a time.
  6486. *
  6487. * @return void
  6488. */
  6489. public function testSubmitUnlockedByDefault() {
  6490. $this->Form->request->params['_Token']['key'] = 'secured';
  6491. $this->Form->submit('Go go');
  6492. $this->Form->submit('Save', array('name' => 'save'));
  6493. $result = $this->Form->unlockField();
  6494. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  6495. }
  6496. /**
  6497. * Test submit image with timestamps.
  6498. *
  6499. * @return void
  6500. */
  6501. public function testSubmitImageTimestamp() {
  6502. Configure::write('Asset.timestamp', 'force');
  6503. $result = $this->Form->submit('cake.power.gif');
  6504. $expected = array(
  6505. 'div' => array('class' => 'submit'),
  6506. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  6507. '/div'
  6508. );
  6509. $this->assertTags($result, $expected);
  6510. }
  6511. /**
  6512. * test the create() method
  6513. *
  6514. * @return void
  6515. */
  6516. public function testCreate() {
  6517. $result = $this->Form->create('Contact');
  6518. $encoding = strtolower(Configure::read('App.encoding'));
  6519. $expected = array(
  6520. 'form' => array(
  6521. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6522. 'accept-charset' => $encoding
  6523. ),
  6524. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  6525. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6526. '/div'
  6527. );
  6528. $this->assertTags($result, $expected);
  6529. $result = $this->Form->create('Contact', array('type' => 'GET'));
  6530. $expected = array('form' => array(
  6531. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6532. 'accept-charset' => $encoding
  6533. ));
  6534. $this->assertTags($result, $expected);
  6535. $result = $this->Form->create('Contact', array('type' => 'get'));
  6536. $expected = array('form' => array(
  6537. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6538. 'accept-charset' => $encoding
  6539. ));
  6540. $this->assertTags($result, $expected);
  6541. $result = $this->Form->create('Contact', array('type' => 'put'));
  6542. $expected = array(
  6543. 'form' => array(
  6544. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6545. 'accept-charset' => $encoding
  6546. ),
  6547. 'div' => array('style' => 'display:none;'),
  6548. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6549. '/div'
  6550. );
  6551. $this->assertTags($result, $expected);
  6552. $result = $this->Form->create('Contact', array('type' => 'file'));
  6553. $expected = array(
  6554. 'form' => array(
  6555. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6556. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6557. ),
  6558. 'div' => array('style' => 'display:none;'),
  6559. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6560. '/div'
  6561. );
  6562. $this->assertTags($result, $expected);
  6563. $this->Form->request->data['Contact']['id'] = 1;
  6564. $this->Form->request->here = '/contacts/edit/1';
  6565. $this->Form->request['action'] = 'edit';
  6566. $result = $this->Form->create('Contact');
  6567. $expected = array(
  6568. 'form' => array(
  6569. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6570. 'accept-charset' => $encoding
  6571. ),
  6572. 'div' => array('style' => 'display:none;'),
  6573. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6574. '/div'
  6575. );
  6576. $this->assertTags($result, $expected);
  6577. $this->Form->request->data['Contact']['id'] = 1;
  6578. $this->Form->request->here = '/contacts/edit/1';
  6579. $this->Form->request['action'] = 'edit';
  6580. $result = $this->Form->create('Contact', array('type' => 'file'));
  6581. $expected = array(
  6582. 'form' => array(
  6583. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6584. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6585. ),
  6586. 'div' => array('style' => 'display:none;'),
  6587. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6588. '/div'
  6589. );
  6590. $this->assertTags($result, $expected);
  6591. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  6592. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  6593. $expected = array(
  6594. 'form' => array(
  6595. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  6596. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  6597. ),
  6598. 'div' => array('style' => 'display:none;'),
  6599. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6600. '/div'
  6601. );
  6602. $this->assertTags($result, $expected);
  6603. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  6604. $expected = array(
  6605. 'form' => array(
  6606. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  6607. 'accept-charset' => $encoding
  6608. ),
  6609. 'div' => array('style' => 'display:none;'),
  6610. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6611. '/div'
  6612. );
  6613. $this->assertTags($result, $expected);
  6614. $this->Form->request['action'] = 'add';
  6615. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  6616. $expected = array(
  6617. 'form' => array(
  6618. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  6619. 'accept-charset' => $encoding
  6620. ),
  6621. 'div' => array('style' => 'display:none;'),
  6622. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6623. '/div'
  6624. );
  6625. $this->assertTags($result, $expected);
  6626. $result = $this->Form->create('User', array('action' => 'login'));
  6627. $expected = array(
  6628. 'form' => array(
  6629. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  6630. 'accept-charset' => $encoding
  6631. ),
  6632. 'div' => array('style' => 'display:none;'),
  6633. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6634. '/div'
  6635. );
  6636. $this->assertTags($result, $expected);
  6637. $result = $this->Form->create('User', array('url' => '/users/login'));
  6638. $expected = array(
  6639. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  6640. 'div' => array('style' => 'display:none;'),
  6641. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6642. '/div'
  6643. );
  6644. $this->assertTags($result, $expected);
  6645. $this->Form->request['controller'] = 'pages';
  6646. $result = $this->Form->create('User', array('action' => 'signup'));
  6647. $expected = array(
  6648. 'form' => array(
  6649. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  6650. 'accept-charset' => $encoding
  6651. ),
  6652. 'div' => array('style' => 'display:none;'),
  6653. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6654. '/div'
  6655. );
  6656. $this->assertTags($result, $expected);
  6657. $this->Form->request->data = array();
  6658. $this->Form->request['controller'] = 'contacts';
  6659. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6660. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  6661. $expected = array(
  6662. 'form' => array(
  6663. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  6664. 'accept-charset' => 'utf-8'
  6665. ),
  6666. 'div' => array('style' => 'display:none;'),
  6667. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6668. '/div'
  6669. );
  6670. $this->assertTags($result, $expected);
  6671. }
  6672. /**
  6673. * Test the onsubmit option for create()
  6674. *
  6675. * @return void
  6676. */
  6677. public function testCreateOnSubmit() {
  6678. $this->Form->request->data = array();
  6679. $this->Form->request['controller'] = 'contacts';
  6680. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6681. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  6682. $expected = array(
  6683. 'form' => array(
  6684. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  6685. 'accept-charset' => 'utf-8'
  6686. ),
  6687. 'div' => array('style' => 'display:none;'),
  6688. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6689. '/div'
  6690. );
  6691. $this->assertTags($result, $expected);
  6692. $this->Form->request->data = array();
  6693. $this->Form->request['controller'] = 'contacts';
  6694. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6695. $result = $this->Form->create(array(
  6696. 'url' => array('action' => 'index', 'param'),
  6697. 'default' => false,
  6698. 'onsubmit' => 'someFunction();'
  6699. ));
  6700. $expected = array(
  6701. 'form' => array(
  6702. 'id' => 'ContactAddForm', 'method' => 'post',
  6703. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6704. 'action' => '/contacts/index/param',
  6705. 'accept-charset' => 'utf-8'
  6706. ),
  6707. 'div' => array('style' => 'display:none;'),
  6708. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6709. '/div'
  6710. );
  6711. $this->assertTags($result, $expected);
  6712. }
  6713. /**
  6714. * test create() with automatic url generation
  6715. *
  6716. * @return void
  6717. */
  6718. public function testCreateAutoUrl() {
  6719. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6720. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6721. $this->Form->request->base = '/base_url';
  6722. $result = $this->Form->create('Contact');
  6723. $expected = array(
  6724. 'form' => array(
  6725. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6726. 'accept-charset' => 'utf-8'
  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. $this->Form->request['action'] = 'delete';
  6734. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6735. $this->Form->request->base = '/base_url';
  6736. $result = $this->Form->create('Contact');
  6737. $expected = array(
  6738. 'form' => array(
  6739. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6740. 'accept-charset' => 'utf-8'
  6741. ),
  6742. 'div' => array('style' => 'display:none;'),
  6743. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6744. '/div'
  6745. );
  6746. $this->assertTags($result, $expected);
  6747. }
  6748. /**
  6749. * test create() with a custom route
  6750. *
  6751. * @return void
  6752. */
  6753. public function testCreateCustomRoute() {
  6754. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6755. $encoding = strtolower(Configure::read('App.encoding'));
  6756. $result = $this->Form->create('User', array('action' => 'login'));
  6757. $expected = array(
  6758. 'form' => array(
  6759. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6760. 'accept-charset' => $encoding
  6761. ),
  6762. 'div' => array('style' => 'display:none;'),
  6763. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6764. '/div'
  6765. );
  6766. $this->assertTags($result, $expected);
  6767. }
  6768. /**
  6769. * test that inputDefaults are stored and used.
  6770. *
  6771. * @return void
  6772. */
  6773. public function testCreateWithInputDefaults() {
  6774. $this->Form->create('User', array(
  6775. 'inputDefaults' => array(
  6776. 'div' => false,
  6777. 'label' => false,
  6778. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6779. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6780. )
  6781. ));
  6782. $result = $this->Form->input('username');
  6783. $expected = array(
  6784. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6785. );
  6786. $this->assertTags($result, $expected);
  6787. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6788. $expected = array(
  6789. 'div' => array('class' => 'input text'),
  6790. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6791. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6792. '/div'
  6793. );
  6794. $this->assertTags($result, $expected);
  6795. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  6796. $expected = array(
  6797. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6798. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  6799. );
  6800. $this->assertTags($result, $expected);
  6801. $this->Form->create('User', array(
  6802. 'inputDefaults' => array(
  6803. 'div' => false,
  6804. 'label' => array('class' => 'nice', 'for' => 'changed'),
  6805. )
  6806. ));
  6807. $result = $this->Form->input('username', array('div' => true));
  6808. $expected = array(
  6809. 'div' => array('class' => 'input text'),
  6810. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  6811. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6812. '/div'
  6813. );
  6814. $this->assertTags($result, $expected);
  6815. }
  6816. /**
  6817. * test automatic accept-charset overriding
  6818. *
  6819. * @return void
  6820. */
  6821. public function testCreateWithAcceptCharset() {
  6822. $result = $this->Form->create('UserForm', array(
  6823. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  6824. )
  6825. );
  6826. $expected = array(
  6827. 'form' => array(
  6828. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6829. 'accept-charset' => 'iso-8859-1'
  6830. ),
  6831. 'div' => array('style' => 'display:none;'),
  6832. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6833. '/div'
  6834. );
  6835. $this->assertTags($result, $expected);
  6836. }
  6837. /**
  6838. * Test base form url when url param is passed with multiple parameters (&)
  6839. *
  6840. */
  6841. public function testCreateQuerystringrequest() {
  6842. $encoding = strtolower(Configure::read('App.encoding'));
  6843. $result = $this->Form->create('Contact', array(
  6844. 'type' => 'post',
  6845. 'escape' => false,
  6846. 'url' => array(
  6847. 'controller' => 'controller',
  6848. 'action' => 'action',
  6849. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6850. )
  6851. ));
  6852. $expected = array(
  6853. 'form' => array(
  6854. 'id' => 'ContactAddForm',
  6855. 'method' => 'post',
  6856. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6857. 'accept-charset' => $encoding
  6858. ),
  6859. 'div' => array('style' => 'display:none;'),
  6860. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6861. '/div'
  6862. );
  6863. $this->assertTags($result, $expected);
  6864. $result = $this->Form->create('Contact', array(
  6865. 'type' => 'post',
  6866. 'url' => array(
  6867. 'controller' => 'controller',
  6868. 'action' => 'action',
  6869. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6870. )
  6871. ));
  6872. $expected = array(
  6873. 'form' => array(
  6874. 'id' => 'ContactAddForm',
  6875. 'method' => 'post',
  6876. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6877. 'accept-charset' => $encoding
  6878. ),
  6879. 'div' => array('style' => 'display:none;'),
  6880. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6881. '/div'
  6882. );
  6883. $this->assertTags($result, $expected);
  6884. }
  6885. /**
  6886. * test that create() doesn't cause errors by multiple id's being in the primary key
  6887. * as could happen with multiple select or checkboxes.
  6888. *
  6889. * @return void
  6890. */
  6891. public function testCreateWithMultipleIdInData() {
  6892. $encoding = strtolower(Configure::read('App.encoding'));
  6893. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6894. $result = $this->Form->create('Contact');
  6895. $expected = array(
  6896. 'form' => array(
  6897. 'id' => 'ContactAddForm',
  6898. 'method' => 'post',
  6899. 'action' => '/contacts/add',
  6900. 'accept-charset' => $encoding
  6901. ),
  6902. 'div' => array('style' => 'display:none;'),
  6903. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6904. '/div'
  6905. );
  6906. $this->assertTags($result, $expected);
  6907. }
  6908. /**
  6909. * test that create() doesn't add in extra passed params.
  6910. *
  6911. * @return void
  6912. */
  6913. public function testCreatePassedArgs() {
  6914. $encoding = strtolower(Configure::read('App.encoding'));
  6915. $this->Form->request->data['Contact']['id'] = 1;
  6916. $result = $this->Form->create('Contact', array(
  6917. 'type' => 'post',
  6918. 'escape' => false,
  6919. 'url' => array(
  6920. 'action' => 'edit',
  6921. 'myparam'
  6922. )
  6923. ));
  6924. $expected = array(
  6925. 'form' => array(
  6926. 'id' => 'ContactAddForm',
  6927. 'method' => 'post',
  6928. 'action' => '/contacts/edit/myparam',
  6929. 'accept-charset' => $encoding
  6930. ),
  6931. 'div' => array('style' => 'display:none;'),
  6932. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6933. '/div'
  6934. );
  6935. $this->assertTags($result, $expected);
  6936. }
  6937. /**
  6938. * test creating a get form, and get form inputs.
  6939. *
  6940. * @return void
  6941. */
  6942. public function testGetFormCreate() {
  6943. $encoding = strtolower(Configure::read('App.encoding'));
  6944. $result = $this->Form->create('Contact', array('type' => 'get'));
  6945. $this->assertTags($result, array('form' => array(
  6946. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6947. 'accept-charset' => $encoding
  6948. )));
  6949. $result = $this->Form->text('Contact.name');
  6950. $this->assertTags($result, array('input' => array(
  6951. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6952. )));
  6953. $result = $this->Form->password('password');
  6954. $this->assertTags($result, array('input' => array(
  6955. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6956. )));
  6957. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6958. $result = $this->Form->text('user_form');
  6959. $this->assertTags($result, array('input' => array(
  6960. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6961. )));
  6962. }
  6963. /**
  6964. * test get form, and inputs when the model param is false
  6965. *
  6966. * @return void
  6967. */
  6968. public function testGetFormWithFalseModel() {
  6969. $encoding = strtolower(Configure::read('App.encoding'));
  6970. $this->Form->request['controller'] = 'contact_test';
  6971. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6972. $expected = array('form' => array(
  6973. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6974. 'accept-charset' => $encoding
  6975. ));
  6976. $this->assertTags($result, $expected);
  6977. $result = $this->Form->text('reason');
  6978. $expected = array(
  6979. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6980. );
  6981. $this->assertTags($result, $expected);
  6982. }
  6983. /**
  6984. * test that datetime() works with GET style forms.
  6985. *
  6986. * @return void
  6987. */
  6988. public function testDateTimeWithGetForms() {
  6989. extract($this->dateRegex);
  6990. $this->Form->create('Contact', array('type' => 'get'));
  6991. $result = $this->Form->datetime('created');
  6992. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6993. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6994. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6995. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6996. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6997. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6998. }
  6999. /**
  7000. * testEditFormWithData method
  7001. *
  7002. * test auto populating form elements from submitted data.
  7003. *
  7004. * @return void
  7005. */
  7006. public function testEditFormWithData() {
  7007. $this->Form->request->data = array('Person' => array(
  7008. 'id' => 1,
  7009. 'first_name' => 'Nate',
  7010. 'last_name' => 'Abele',
  7011. 'email' => 'nate@example.com'
  7012. ));
  7013. $this->Form->request->addParams(array(
  7014. 'models' => array('Person'),
  7015. 'controller' => 'people',
  7016. 'action' => 'add'
  7017. ));
  7018. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  7019. $this->Form->create();
  7020. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  7021. $expected = array(
  7022. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  7023. 'select' => array(
  7024. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  7025. ),
  7026. array('option' => array('value' => 1)), 'Nate', '/option',
  7027. array('option' => array('value' => 2)), 'Garrett', '/option',
  7028. array('option' => array('value' => 3)), 'Larry', '/option',
  7029. '/select'
  7030. );
  7031. $this->assertTags($result, $expected);
  7032. }
  7033. /**
  7034. * Test that required fields are created for various types of validation.
  7035. *
  7036. * @return void
  7037. */
  7038. public function testFormInputRequiredDetection() {
  7039. $this->Form->create('Contact');
  7040. $result = $this->Form->input('Contact.non_existing');
  7041. $expected = array(
  7042. 'div' => array('class' => 'input text'),
  7043. 'label' => array('for' => 'ContactNonExisting'),
  7044. 'Non Existing',
  7045. '/label',
  7046. 'input' => array(
  7047. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  7048. 'id' => 'ContactNonExisting'
  7049. ),
  7050. '/div'
  7051. );
  7052. $this->assertTags($result, $expected);
  7053. $result = $this->Form->input('Contact.imrequired');
  7054. $expected = array(
  7055. 'div' => array('class' => 'input text required'),
  7056. 'label' => array('for' => 'ContactImrequired'),
  7057. 'Imrequired',
  7058. '/label',
  7059. 'input' => array(
  7060. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7061. 'id' => 'ContactImrequired',
  7062. 'required' => 'required'
  7063. ),
  7064. '/div'
  7065. );
  7066. $this->assertTags($result, $expected);
  7067. $result = $this->Form->input('Contact.imalsorequired');
  7068. $expected = array(
  7069. 'div' => array('class' => 'input text required'),
  7070. 'label' => array('for' => 'ContactImalsorequired'),
  7071. 'Imalsorequired',
  7072. '/label',
  7073. 'input' => array(
  7074. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  7075. 'id' => 'ContactImalsorequired',
  7076. 'required' => 'required'
  7077. ),
  7078. '/div'
  7079. );
  7080. $this->assertTags($result, $expected);
  7081. $result = $this->Form->input('Contact.imrequiredtoo');
  7082. $expected = array(
  7083. 'div' => array('class' => 'input text required'),
  7084. 'label' => array('for' => 'ContactImrequiredtoo'),
  7085. 'Imrequiredtoo',
  7086. '/label',
  7087. 'input' => array(
  7088. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  7089. 'id' => 'ContactImrequiredtoo',
  7090. 'required' => 'required'
  7091. ),
  7092. '/div'
  7093. );
  7094. $this->assertTags($result, $expected);
  7095. $result = $this->Form->input('Contact.required_one');
  7096. $expected = array(
  7097. 'div' => array('class' => 'input text required'),
  7098. 'label' => array('for' => 'ContactRequiredOne'),
  7099. 'Required One',
  7100. '/label',
  7101. 'input' => array(
  7102. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  7103. 'id' => 'ContactRequiredOne',
  7104. 'required' => 'required'
  7105. ),
  7106. '/div'
  7107. );
  7108. $this->assertTags($result, $expected);
  7109. $result = $this->Form->input('Contact.string_required');
  7110. $expected = array(
  7111. 'div' => array('class' => 'input text required'),
  7112. 'label' => array('for' => 'ContactStringRequired'),
  7113. 'String Required',
  7114. '/label',
  7115. 'input' => array(
  7116. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  7117. 'id' => 'ContactStringRequired',
  7118. 'required' => 'required'
  7119. ),
  7120. '/div'
  7121. );
  7122. $this->assertTags($result, $expected);
  7123. $result = $this->Form->input('Contact.imnotrequired');
  7124. $expected = array(
  7125. 'div' => array('class' => 'input text'),
  7126. 'label' => array('for' => 'ContactImnotrequired'),
  7127. 'Imnotrequired',
  7128. '/label',
  7129. 'input' => array(
  7130. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  7131. 'id' => 'ContactImnotrequired'
  7132. ),
  7133. '/div'
  7134. );
  7135. $this->assertTags($result, $expected);
  7136. $result = $this->Form->input('Contact.imalsonotrequired');
  7137. $expected = array(
  7138. 'div' => array('class' => 'input text'),
  7139. 'label' => array('for' => 'ContactImalsonotrequired'),
  7140. 'Imalsonotrequired',
  7141. '/label',
  7142. 'input' => array(
  7143. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  7144. 'id' => 'ContactImalsonotrequired'
  7145. ),
  7146. '/div'
  7147. );
  7148. $this->assertTags($result, $expected);
  7149. $result = $this->Form->input('Contact.imalsonotrequired2');
  7150. $expected = array(
  7151. 'div' => array('class' => 'input text'),
  7152. 'label' => array('for' => 'ContactImalsonotrequired2'),
  7153. 'Imalsonotrequired2',
  7154. '/label',
  7155. 'input' => array(
  7156. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  7157. 'id' => 'ContactImalsonotrequired2'
  7158. ),
  7159. '/div'
  7160. );
  7161. $this->assertTags($result, $expected);
  7162. $result = $this->Form->input('Contact.imnotrequiredeither');
  7163. $expected = array(
  7164. 'div' => array('class' => 'input text'),
  7165. 'label' => array('for' => 'ContactImnotrequiredeither'),
  7166. 'Imnotrequiredeither',
  7167. '/label',
  7168. 'input' => array(
  7169. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  7170. 'id' => 'ContactImnotrequiredeither'
  7171. ),
  7172. '/div'
  7173. );
  7174. $this->assertTags($result, $expected);
  7175. $result = $this->Form->input('Contact.iamrequiredalways');
  7176. $expected = array(
  7177. 'div' => array('class' => 'input text required'),
  7178. 'label' => array('for' => 'ContactIamrequiredalways'),
  7179. 'Iamrequiredalways',
  7180. '/label',
  7181. 'input' => array(
  7182. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  7183. 'id' => 'ContactIamrequiredalways',
  7184. 'required' => 'required'
  7185. ),
  7186. '/div'
  7187. );
  7188. $this->assertTags($result, $expected);
  7189. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  7190. $expected = array(
  7191. 'div' => array('class' => 'input checkbox required'),
  7192. array('input' => array(
  7193. 'type' => 'hidden',
  7194. 'name' => 'data[Contact][boolean_field]',
  7195. 'id' => 'ContactBooleanField_',
  7196. 'value' => '0'
  7197. )),
  7198. array('input' => array(
  7199. 'type' => 'checkbox',
  7200. 'name' => 'data[Contact][boolean_field]',
  7201. 'value' => '1',
  7202. 'id' => 'ContactBooleanField'
  7203. )),
  7204. 'label' => array('for' => 'ContactBooleanField'),
  7205. 'Boolean Field',
  7206. '/label',
  7207. '/div'
  7208. );
  7209. $this->assertTags($result, $expected);
  7210. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  7211. $expected = array(
  7212. 'div' => array('class' => 'input checkbox required'),
  7213. array('input' => array(
  7214. 'type' => 'hidden',
  7215. 'name' => 'data[Contact][boolean_field]',
  7216. 'id' => 'ContactBooleanField_',
  7217. 'value' => '0'
  7218. )),
  7219. array('input' => array(
  7220. 'type' => 'checkbox',
  7221. 'name' => 'data[Contact][boolean_field]',
  7222. 'value' => '1',
  7223. 'id' => 'ContactBooleanField',
  7224. 'required' => 'required'
  7225. )),
  7226. 'label' => array('for' => 'ContactBooleanField'),
  7227. 'Boolean Field',
  7228. '/label',
  7229. '/div'
  7230. );
  7231. $this->assertTags($result, $expected);
  7232. }
  7233. /**
  7234. * testFormMagicInput method
  7235. *
  7236. * @return void
  7237. */
  7238. public function testFormMagicInput() {
  7239. $encoding = strtolower(Configure::read('App.encoding'));
  7240. $result = $this->Form->create('Contact');
  7241. $expected = array(
  7242. 'form' => array(
  7243. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7244. 'accept-charset' => $encoding
  7245. ),
  7246. 'div' => array('style' => 'display:none;'),
  7247. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7248. '/div'
  7249. );
  7250. $this->assertTags($result, $expected);
  7251. $result = $this->Form->input('name');
  7252. $expected = array(
  7253. 'div' => array('class' => 'input text'),
  7254. 'label' => array('for' => 'ContactName'),
  7255. 'Name',
  7256. '/label',
  7257. 'input' => array(
  7258. 'type' => 'text', 'name' => 'data[Contact][name]',
  7259. 'id' => 'ContactName', 'maxlength' => '255'
  7260. ),
  7261. '/div'
  7262. );
  7263. $this->assertTags($result, $expected);
  7264. $result = $this->Form->input('non_existing_field_in_contact_model');
  7265. $expected = array(
  7266. 'div' => array('class' => 'input text'),
  7267. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  7268. 'Non Existing Field In Contact Model',
  7269. '/label',
  7270. 'input' => array(
  7271. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  7272. 'id' => 'ContactNonExistingFieldInContactModel'
  7273. ),
  7274. '/div'
  7275. );
  7276. $this->assertTags($result, $expected);
  7277. $result = $this->Form->input('Address.street');
  7278. $expected = array(
  7279. 'div' => array('class' => 'input text'),
  7280. 'label' => array('for' => 'AddressStreet'),
  7281. 'Street',
  7282. '/label',
  7283. 'input' => array(
  7284. 'type' => 'text', 'name' => 'data[Address][street]',
  7285. 'id' => 'AddressStreet'
  7286. ),
  7287. '/div'
  7288. );
  7289. $this->assertTags($result, $expected);
  7290. $result = $this->Form->input('Address.non_existing_field_in_model');
  7291. $expected = array(
  7292. 'div' => array('class' => 'input text'),
  7293. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  7294. 'Non Existing Field In Model',
  7295. '/label',
  7296. 'input' => array(
  7297. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  7298. 'id' => 'AddressNonExistingFieldInModel'
  7299. ),
  7300. '/div'
  7301. );
  7302. $this->assertTags($result, $expected);
  7303. $result = $this->Form->input('name', array('div' => false));
  7304. $expected = array(
  7305. 'label' => array('for' => 'ContactName'),
  7306. 'Name',
  7307. '/label',
  7308. 'input' => array(
  7309. 'type' => 'text', 'name' => 'data[Contact][name]',
  7310. 'id' => 'ContactName', 'maxlength' => '255'
  7311. )
  7312. );
  7313. $this->assertTags($result, $expected);
  7314. extract($this->dateRegex);
  7315. $now = strtotime('now');
  7316. $result = $this->Form->input('Contact.published', array('div' => false));
  7317. $expected = array(
  7318. 'label' => array('for' => 'ContactPublishedMonth'),
  7319. 'Published',
  7320. '/label',
  7321. array('select' => array(
  7322. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  7323. )),
  7324. $monthsRegex,
  7325. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7326. date('F', $now),
  7327. '/option',
  7328. '*/select',
  7329. '-',
  7330. array('select' => array(
  7331. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  7332. )),
  7333. $daysRegex,
  7334. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7335. date('j', $now),
  7336. '/option',
  7337. '*/select',
  7338. '-',
  7339. array('select' => array(
  7340. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  7341. )),
  7342. $yearsRegex,
  7343. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7344. date('Y', $now),
  7345. '*/select'
  7346. );
  7347. $this->assertTags($result, $expected);
  7348. $result = $this->Form->input('Contact.updated', array('div' => false));
  7349. $expected = array(
  7350. 'label' => array('for' => 'ContactUpdatedMonth'),
  7351. 'Updated',
  7352. '/label',
  7353. array('select' => array(
  7354. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  7355. )),
  7356. $monthsRegex,
  7357. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7358. date('F', $now),
  7359. '/option',
  7360. '*/select',
  7361. '-',
  7362. array('select' => array(
  7363. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  7364. )),
  7365. $daysRegex,
  7366. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7367. date('j', $now),
  7368. '/option',
  7369. '*/select',
  7370. '-',
  7371. array('select' => array(
  7372. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  7373. )),
  7374. $yearsRegex,
  7375. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7376. date('Y', $now),
  7377. '*/select'
  7378. );
  7379. $this->assertTags($result, $expected);
  7380. $result = $this->Form->input('UserForm.stuff');
  7381. $expected = array(
  7382. 'div' => array('class' => 'input text'),
  7383. 'label' => array('for' => 'UserFormStuff'),
  7384. 'Stuff',
  7385. '/label',
  7386. 'input' => array(
  7387. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  7388. 'id' => 'UserFormStuff', 'maxlength' => 10
  7389. ),
  7390. '/div'
  7391. );
  7392. $this->assertTags($result, $expected);
  7393. }
  7394. /**
  7395. * testForMagicInputNonExistingNorValidated method
  7396. *
  7397. * @return void
  7398. */
  7399. public function testForMagicInputNonExistingNorValidated() {
  7400. $encoding = strtolower(Configure::read('App.encoding'));
  7401. $result = $this->Form->create('Contact');
  7402. $expected = array(
  7403. 'form' => array(
  7404. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7405. 'accept-charset' => $encoding
  7406. ),
  7407. 'div' => array('style' => 'display:none;'),
  7408. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7409. '/div'
  7410. );
  7411. $this->assertTags($result, $expected);
  7412. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7413. $expected = array(
  7414. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7415. 'Non Existing Nor Validated',
  7416. '/label',
  7417. 'input' => array(
  7418. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7419. 'id' => 'ContactNonExistingNorValidated'
  7420. )
  7421. );
  7422. $this->assertTags($result, $expected);
  7423. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  7424. 'div' => false, 'value' => 'my value'
  7425. ));
  7426. $expected = array(
  7427. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7428. 'Non Existing Nor Validated',
  7429. '/label',
  7430. 'input' => array(
  7431. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7432. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  7433. )
  7434. );
  7435. $this->assertTags($result, $expected);
  7436. $this->Form->request->data = array(
  7437. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  7438. ));
  7439. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7440. $expected = array(
  7441. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7442. 'Non Existing Nor Validated',
  7443. '/label',
  7444. 'input' => array(
  7445. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7446. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  7447. )
  7448. );
  7449. $this->assertTags($result, $expected);
  7450. }
  7451. /**
  7452. * testFormMagicInputLabel method
  7453. *
  7454. * @return void
  7455. */
  7456. public function testFormMagicInputLabel() {
  7457. $encoding = strtolower(Configure::read('App.encoding'));
  7458. $result = $this->Form->create('Contact');
  7459. $expected = array(
  7460. 'form' => array(
  7461. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7462. 'accept-charset' => $encoding
  7463. ),
  7464. 'div' => array('style' => 'display:none;'),
  7465. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7466. '/div'
  7467. );
  7468. $this->assertTags($result, $expected);
  7469. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  7470. $this->assertTags($result, array('input' => array(
  7471. 'name' => 'data[Contact][name]', 'type' => 'text',
  7472. 'id' => 'ContactName', 'maxlength' => '255')
  7473. ));
  7474. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  7475. $expected = array(
  7476. 'label' => array('for' => 'ContactName'),
  7477. 'My label',
  7478. '/label',
  7479. 'input' => array(
  7480. 'type' => 'text', 'name' => 'data[Contact][name]',
  7481. 'id' => 'ContactName', 'maxlength' => '255'
  7482. )
  7483. );
  7484. $this->assertTags($result, $expected);
  7485. $result = $this->Form->input('Contact.name', array(
  7486. 'div' => false, 'label' => array('class' => 'mandatory')
  7487. ));
  7488. $expected = array(
  7489. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7490. 'Name',
  7491. '/label',
  7492. 'input' => array(
  7493. 'type' => 'text', 'name' => 'data[Contact][name]',
  7494. 'id' => 'ContactName', 'maxlength' => '255'
  7495. )
  7496. );
  7497. $this->assertTags($result, $expected);
  7498. $result = $this->Form->input('Contact.name', array(
  7499. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  7500. ));
  7501. $expected = array(
  7502. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7503. 'My label',
  7504. '/label',
  7505. 'input' => array(
  7506. 'type' => 'text', 'name' => 'data[Contact][name]',
  7507. 'id' => 'ContactName', 'maxlength' => '255'
  7508. )
  7509. );
  7510. $this->assertTags($result, $expected);
  7511. $result = $this->Form->input('Contact.name', array(
  7512. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  7513. ));
  7514. $expected = array(
  7515. 'label' => array('for' => 'my_id'),
  7516. 'Name',
  7517. '/label',
  7518. 'input' => array(
  7519. 'type' => 'text', 'name' => 'data[Contact][name]',
  7520. 'id' => 'my_id', 'maxlength' => '255'
  7521. )
  7522. );
  7523. $this->assertTags($result, $expected);
  7524. $result = $this->Form->input('1.id');
  7525. $this->assertTags($result, array('input' => array(
  7526. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7527. 'id' => 'Contact1Id'
  7528. )));
  7529. $result = $this->Form->input("1.name");
  7530. $expected = array(
  7531. 'div' => array('class' => 'input text'),
  7532. 'label' => array('for' => 'Contact1Name'),
  7533. 'Name',
  7534. '/label',
  7535. 'input' => array(
  7536. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  7537. 'id' => 'Contact1Name', 'maxlength' => '255'
  7538. ),
  7539. '/div'
  7540. );
  7541. $this->assertTags($result, $expected);
  7542. $result = $this->Form->input('Contact.1.id');
  7543. $this->assertTags($result, array(
  7544. 'input' => array(
  7545. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7546. 'id' => 'Contact1Id'
  7547. )
  7548. ));
  7549. $result = $this->Form->input("Model.1.name");
  7550. $expected = array(
  7551. 'div' => array('class' => 'input text'),
  7552. 'label' => array('for' => 'Model1Name'),
  7553. 'Name',
  7554. '/label',
  7555. 'input' => array(
  7556. 'type' => 'text', 'name' => 'data[Model][1][name]',
  7557. 'id' => 'Model1Name'
  7558. ),
  7559. '/div'
  7560. );
  7561. $this->assertTags($result, $expected);
  7562. }
  7563. /**
  7564. * testFormEnd method
  7565. *
  7566. * @return void
  7567. */
  7568. public function testFormEnd() {
  7569. $this->assertEquals('</form>', $this->Form->end());
  7570. $result = $this->Form->end('');
  7571. $expected = array(
  7572. 'div' => array('class' => 'submit'),
  7573. 'input' => array('type' => 'submit', 'value' => ''),
  7574. '/div',
  7575. '/form'
  7576. );
  7577. $this->assertTags($result, $expected);
  7578. $result = $this->Form->end(array('label' => ''));
  7579. $expected = array(
  7580. 'div' => array('class' => 'submit'),
  7581. 'input' => array('type' => 'submit', 'value' => ''),
  7582. '/div',
  7583. '/form'
  7584. );
  7585. $this->assertTags($result, $expected);
  7586. $result = $this->Form->end('save');
  7587. $expected = array(
  7588. 'div' => array('class' => 'submit'),
  7589. 'input' => array('type' => 'submit', 'value' => 'save'),
  7590. '/div',
  7591. '/form'
  7592. );
  7593. $this->assertTags($result, $expected);
  7594. $result = $this->Form->end(array('label' => 'save'));
  7595. $expected = array(
  7596. 'div' => array('class' => 'submit'),
  7597. 'input' => array('type' => 'submit', 'value' => 'save'),
  7598. '/div',
  7599. '/form'
  7600. );
  7601. $this->assertTags($result, $expected);
  7602. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  7603. $expected = array(
  7604. 'div' => array('class' => 'submit'),
  7605. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7606. '/div',
  7607. '/form'
  7608. );
  7609. $this->assertTags($result, $expected);
  7610. $result = $this->Form->end(array('name' => 'Whatever'));
  7611. $expected = array(
  7612. 'div' => array('class' => 'submit'),
  7613. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  7614. '/div',
  7615. '/form'
  7616. );
  7617. $this->assertTags($result, $expected);
  7618. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  7619. $expected = array(
  7620. 'div' => array('class' => 'good'),
  7621. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7622. '/div',
  7623. '/form'
  7624. );
  7625. $this->assertTags($result, $expected);
  7626. $result = $this->Form->end(array(
  7627. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  7628. ));
  7629. $expected = array(
  7630. 'div' => array('class' => 'good'),
  7631. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7632. '/div',
  7633. '/form'
  7634. );
  7635. $this->assertTags($result, $expected);
  7636. }
  7637. /**
  7638. * testMultipleFormWithIdFields method
  7639. *
  7640. * @return void
  7641. */
  7642. public function testMultipleFormWithIdFields() {
  7643. $this->Form->create('UserForm');
  7644. $result = $this->Form->input('id');
  7645. $this->assertTags($result, array('input' => array(
  7646. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  7647. )));
  7648. $result = $this->Form->input('ValidateItem.id');
  7649. $this->assertTags($result, array('input' => array(
  7650. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  7651. 'id' => 'ValidateItemId'
  7652. )));
  7653. $result = $this->Form->input('ValidateUser.id');
  7654. $this->assertTags($result, array('input' => array(
  7655. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  7656. 'id' => 'ValidateUserId'
  7657. )));
  7658. }
  7659. /**
  7660. * testDbLessModel method
  7661. *
  7662. * @return void
  7663. */
  7664. public function testDbLessModel() {
  7665. $this->Form->create('TestMail');
  7666. $result = $this->Form->input('name');
  7667. $expected = array(
  7668. 'div' => array('class' => 'input text'),
  7669. 'label' => array('for' => 'TestMailName'),
  7670. 'Name',
  7671. '/label',
  7672. 'input' => array(
  7673. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7674. 'id' => 'TestMailName'
  7675. ),
  7676. '/div'
  7677. );
  7678. $this->assertTags($result, $expected);
  7679. ClassRegistry::init('TestMail');
  7680. $this->Form->create('TestMail');
  7681. $result = $this->Form->input('name');
  7682. $expected = array(
  7683. 'div' => array('class' => 'input text'),
  7684. 'label' => array('for' => 'TestMailName'),
  7685. 'Name',
  7686. '/label',
  7687. 'input' => array(
  7688. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7689. 'id' => 'TestMailName'
  7690. ),
  7691. '/div'
  7692. );
  7693. $this->assertTags($result, $expected);
  7694. }
  7695. /**
  7696. * testBrokenness method
  7697. *
  7698. * @return void
  7699. */
  7700. public function testBrokenness() {
  7701. /*
  7702. * #4 This test has two parents and four children. By default (as of r7117) both
  7703. * parents are show but the first parent is missing a child. This is the inconsistency
  7704. * in the default behaviour - one parent has all children, the other does not - dependent
  7705. * on the data values.
  7706. */
  7707. $result = $this->Form->select('Model.field', array(
  7708. 'Fred' => array(
  7709. 'freds_son_1' => 'Fred',
  7710. 'freds_son_2' => 'Freddie'
  7711. ),
  7712. 'Bert' => array(
  7713. 'berts_son_1' => 'Albert',
  7714. 'berts_son_2' => 'Bertie')
  7715. ),
  7716. array('showParents' => true, 'empty' => false)
  7717. );
  7718. $expected = array(
  7719. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7720. array('optgroup' => array('label' => 'Fred')),
  7721. array('option' => array('value' => 'freds_son_1')),
  7722. 'Fred',
  7723. '/option',
  7724. array('option' => array('value' => 'freds_son_2')),
  7725. 'Freddie',
  7726. '/option',
  7727. '/optgroup',
  7728. array('optgroup' => array('label' => 'Bert')),
  7729. array('option' => array('value' => 'berts_son_1')),
  7730. 'Albert',
  7731. '/option',
  7732. array('option' => array('value' => 'berts_son_2')),
  7733. 'Bertie',
  7734. '/option',
  7735. '/optgroup',
  7736. '/select'
  7737. );
  7738. $this->assertTags($result, $expected);
  7739. /*
  7740. * #2 This is structurally identical to the test above (#1) - only the parent name has
  7741. * changed, so we should expect the same select list data, just with a different name
  7742. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  7743. * This is where data corruption can occur, because when a select value is missing from
  7744. * a list a form will substitute the first value in the list - without the user knowing.
  7745. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  7746. * affect the availability of 3 => 'Three' as a valid option.
  7747. */
  7748. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  7749. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  7750. ));
  7751. $result = $this->Form->select(
  7752. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  7753. );
  7754. $expected = array(
  7755. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7756. array('option' => array('value' => 1)),
  7757. 'One',
  7758. '/option',
  7759. array('option' => array('value' => 2)),
  7760. 'Two',
  7761. '/option',
  7762. array('optgroup' => array('label' => 'Three')),
  7763. array('option' => array('value' => 3)),
  7764. 'Three',
  7765. '/option',
  7766. array('option' => array('value' => 4)),
  7767. 'Four',
  7768. '/option',
  7769. array('option' => array('value' => 5)),
  7770. 'Five',
  7771. '/option',
  7772. '/optgroup',
  7773. '/select'
  7774. );
  7775. $this->assertTags($result, $expected);
  7776. }
  7777. /**
  7778. * Test the generation of fields for a multi record form.
  7779. *
  7780. * @return void
  7781. */
  7782. public function testMultiRecordForm() {
  7783. $this->Form->create('ValidateProfile');
  7784. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  7785. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  7786. $expected = array(
  7787. 'div' => array('class' => 'input textarea'),
  7788. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  7789. 'Name',
  7790. '/label',
  7791. 'textarea' => array(
  7792. 'id' => 'ValidateProfile1ValidateItem2Name',
  7793. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  7794. 'cols' => 30,
  7795. 'rows' => 6
  7796. ),
  7797. 'Value',
  7798. '/textarea',
  7799. '/div'
  7800. );
  7801. $this->assertTags($result, $expected);
  7802. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  7803. $expected = array(
  7804. 'div' => array('class' => 'input date'),
  7805. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7806. 'Created',
  7807. '/label',
  7808. array('select' => array(
  7809. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  7810. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7811. )
  7812. ),
  7813. array('option' => array('value' => '')), '/option',
  7814. $this->dateRegex['monthsRegex'],
  7815. '/select', '-',
  7816. array('select' => array(
  7817. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  7818. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7819. )
  7820. ),
  7821. array('option' => array('value' => '')), '/option',
  7822. $this->dateRegex['daysRegex'],
  7823. '/select', '-',
  7824. array('select' => array(
  7825. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  7826. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7827. )
  7828. ),
  7829. array('option' => array('value' => '')), '/option',
  7830. $this->dateRegex['yearsRegex'],
  7831. '/select',
  7832. '/div'
  7833. );
  7834. $this->assertTags($result, $expected);
  7835. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7836. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7837. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7838. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7839. $expected = array(
  7840. 'div' => array('class' => 'input select error'),
  7841. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7842. 'Profile',
  7843. '/label',
  7844. 'select' => array(
  7845. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7846. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7847. 'class' => 'form-error'
  7848. ),
  7849. '/select',
  7850. array('div' => array('class' => 'error-message')),
  7851. 'Error',
  7852. '/div',
  7853. '/div'
  7854. );
  7855. $this->assertTags($result, $expected);
  7856. }
  7857. /**
  7858. * test the correct display of multi-record form validation errors.
  7859. *
  7860. * @return void
  7861. */
  7862. public function testMultiRecordFormValidationErrors() {
  7863. $this->Form->create('ValidateProfile');
  7864. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7865. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7866. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7867. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7868. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7869. $result = $this->Form->error('ValidateProfile.2.city');
  7870. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7871. $result = $this->Form->error('2.city');
  7872. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7873. }
  7874. /**
  7875. * test the correct display of multi-record form validation errors.
  7876. *
  7877. * @return void
  7878. */
  7879. public function testSaveManyRecordFormValidationErrors() {
  7880. $this->Form->create('ValidateUser');
  7881. $ValidateUser = ClassRegistry::getObject('ValidateUser');
  7882. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  7883. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  7884. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7885. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  7886. $result = $this->Form->error('ValidateUser.0.city');
  7887. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7888. }
  7889. /**
  7890. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7891. *
  7892. * @return void
  7893. */
  7894. public function testInputTemplate() {
  7895. $result = $this->Form->input('Contact.email', array(
  7896. 'type' => 'text', 'format' => array('input')
  7897. ));
  7898. $expected = array(
  7899. 'div' => array('class' => 'input text'),
  7900. 'input' => array(
  7901. 'type' => 'text', 'name' => 'data[Contact][email]',
  7902. 'id' => 'ContactEmail'
  7903. ),
  7904. '/div'
  7905. );
  7906. $this->assertTags($result, $expected);
  7907. $result = $this->Form->input('Contact.email', array(
  7908. 'type' => 'text', 'format' => array('input', 'label'),
  7909. 'label' => '<em>Email (required)</em>'
  7910. ));
  7911. $expected = array(
  7912. 'div' => array('class' => 'input text'),
  7913. array('input' => array(
  7914. 'type' => 'text', 'name' => 'data[Contact][email]',
  7915. 'id' => 'ContactEmail'
  7916. )),
  7917. 'label' => array('for' => 'ContactEmail'),
  7918. 'em' => array(),
  7919. 'Email (required)',
  7920. '/em',
  7921. '/label',
  7922. '/div'
  7923. );
  7924. $this->assertTags($result, $expected);
  7925. $result = $this->Form->input('Contact.email', array(
  7926. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7927. 'between' => '<div>Something in the middle</div>',
  7928. 'after' => '<span>Some text at the end</span>'
  7929. ));
  7930. $expected = array(
  7931. 'div' => array('class' => 'input text'),
  7932. array('input' => array(
  7933. 'type' => 'text', 'name' => 'data[Contact][email]',
  7934. 'id' => 'ContactEmail'
  7935. )),
  7936. array('div' => array()),
  7937. 'Something in the middle',
  7938. '/div',
  7939. 'label' => array('for' => 'ContactEmail'),
  7940. 'Email',
  7941. '/label',
  7942. 'span' => array(),
  7943. 'Some text at the end',
  7944. '/span',
  7945. '/div'
  7946. );
  7947. $this->assertTags($result, $expected);
  7948. $result = $this->Form->input('Contact.method', array(
  7949. 'type' => 'radio',
  7950. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7951. 'between' => 'I am between',
  7952. ));
  7953. $expected = array(
  7954. 'div' => array('class' => 'input radio'),
  7955. 'fieldset' => array(),
  7956. 'legend' => array(),
  7957. 'Method',
  7958. '/legend',
  7959. 'I am between',
  7960. 'input' => array(
  7961. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  7962. 'value' => '', 'id' => 'ContactMethod_'
  7963. ),
  7964. array('input' => array(
  7965. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7966. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7967. )),
  7968. array('label' => array('for' => 'ContactMethodEmail')),
  7969. 'Email',
  7970. '/label',
  7971. array('input' => array(
  7972. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7973. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7974. )),
  7975. array('label' => array('for' => 'ContactMethodPigeon')),
  7976. 'Pigeon',
  7977. '/label',
  7978. '/fieldset',
  7979. '/div',
  7980. );
  7981. $this->assertTags($result, $expected);
  7982. }
  7983. /**
  7984. * test that some html5 inputs + FormHelper::__call() work
  7985. *
  7986. * @return void
  7987. */
  7988. public function testHtml5Inputs() {
  7989. $result = $this->Form->email('User.email');
  7990. $expected = array(
  7991. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  7992. );
  7993. $this->assertTags($result, $expected);
  7994. $result = $this->Form->search('User.query');
  7995. $expected = array(
  7996. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  7997. );
  7998. $this->assertTags($result, $expected);
  7999. $result = $this->Form->search('User.query', array('value' => 'test'));
  8000. $expected = array(
  8001. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8002. );
  8003. $this->assertTags($result, $expected);
  8004. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  8005. $expected = array(
  8006. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8007. );
  8008. $this->assertTags($result, $expected);
  8009. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  8010. $expected = array(
  8011. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  8012. );
  8013. $this->assertTags($result, $expected);
  8014. }
  8015. /**
  8016. *
  8017. * @expectedException CakeException
  8018. * @return void
  8019. */
  8020. public function testHtml5InputException() {
  8021. $this->Form->email();
  8022. }
  8023. /**
  8024. * Tests that a model can be loaded from the model names passed in the request object
  8025. *
  8026. * @return void
  8027. */
  8028. public function testIntrospectModelFromRequest() {
  8029. $this->loadFixtures('Post');
  8030. App::build(array(
  8031. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  8032. ));
  8033. CakePlugin::load('TestPlugin');
  8034. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  8035. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  8036. $this->Form->create('TestPluginPost');
  8037. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  8038. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  8039. CakePlugin::unload();
  8040. App::build();
  8041. }
  8042. /**
  8043. * Tests that it is possible to set the validation errors directly in the helper for a field
  8044. *
  8045. * @return void
  8046. */
  8047. public function testCustomValidationErrors() {
  8048. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  8049. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  8050. $this->assertEquals('Badness!', $result);
  8051. }
  8052. /**
  8053. * Tests that the 'on' key validates as expected on create
  8054. *
  8055. * @return void
  8056. */
  8057. public function testRequiredOnCreate() {
  8058. $this->Form->create('Contact');
  8059. $result = $this->Form->input('Contact.imrequiredonupdate');
  8060. $expected = array(
  8061. 'div' => array('class' => 'input text'),
  8062. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8063. 'Imrequiredonupdate',
  8064. '/label',
  8065. 'input' => array(
  8066. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8067. 'id' => 'ContactImrequiredonupdate'
  8068. ),
  8069. '/div'
  8070. );
  8071. $this->assertTags($result, $expected);
  8072. $result = $this->Form->input('Contact.imrequiredoncreate');
  8073. $expected = array(
  8074. 'div' => array('class' => 'input text required'),
  8075. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8076. 'Imrequiredoncreate',
  8077. '/label',
  8078. 'input' => array(
  8079. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8080. 'id' => 'ContactImrequiredoncreate',
  8081. 'required' => 'required'
  8082. ),
  8083. '/div'
  8084. );
  8085. $this->assertTags($result, $expected);
  8086. $result = $this->Form->input('Contact.imrequiredonboth');
  8087. $expected = array(
  8088. 'div' => array('class' => 'input text required'),
  8089. 'label' => array('for' => 'ContactImrequiredonboth'),
  8090. 'Imrequiredonboth',
  8091. '/label',
  8092. 'input' => array(
  8093. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8094. 'id' => 'ContactImrequiredonboth',
  8095. 'required' => 'required'
  8096. ),
  8097. '/div'
  8098. );
  8099. $this->assertTags($result, $expected);
  8100. $this->Form->inputDefaults(array('required' => false));
  8101. $result = $this->Form->input('Contact.imrequired');
  8102. $expected = array(
  8103. 'div' => array('class' => 'input text'),
  8104. 'label' => array('for' => 'ContactImrequired'),
  8105. 'Imrequired',
  8106. '/label',
  8107. 'input' => array(
  8108. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8109. 'id' => 'ContactImrequired'
  8110. ),
  8111. '/div'
  8112. );
  8113. $this->assertTags($result, $expected);
  8114. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  8115. $this->assertTags($result, $expected);
  8116. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  8117. $expected = array(
  8118. 'div' => array('class' => 'input text required'),
  8119. 'label' => array('for' => 'ContactImrequired'),
  8120. 'Imrequired',
  8121. '/label',
  8122. 'input' => array(
  8123. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8124. 'id' => 'ContactImrequired'
  8125. ),
  8126. '/div'
  8127. );
  8128. $this->assertTags($result, $expected);
  8129. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  8130. $this->assertTags($result, $expected);
  8131. }
  8132. /**
  8133. * Tests that the 'on' key validates as expected on update
  8134. *
  8135. * @return void
  8136. */
  8137. public function testRequiredOnUpdate() {
  8138. $this->Form->request->data['Contact']['id'] = 1;
  8139. $this->Form->create('Contact');
  8140. $result = $this->Form->input('Contact.imrequiredonupdate');
  8141. $expected = array(
  8142. 'div' => array('class' => 'input text required'),
  8143. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8144. 'Imrequiredonupdate',
  8145. '/label',
  8146. 'input' => array(
  8147. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8148. 'id' => 'ContactImrequiredonupdate',
  8149. 'required' => 'required'
  8150. ),
  8151. '/div'
  8152. );
  8153. $this->assertTags($result, $expected);
  8154. $result = $this->Form->input('Contact.imrequiredoncreate');
  8155. $expected = array(
  8156. 'div' => array('class' => 'input text'),
  8157. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8158. 'Imrequiredoncreate',
  8159. '/label',
  8160. 'input' => array(
  8161. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8162. 'id' => 'ContactImrequiredoncreate'
  8163. ),
  8164. '/div'
  8165. );
  8166. $this->assertTags($result, $expected);
  8167. $result = $this->Form->input('Contact.imrequiredonboth');
  8168. $expected = array(
  8169. 'div' => array('class' => 'input text required'),
  8170. 'label' => array('for' => 'ContactImrequiredonboth'),
  8171. 'Imrequiredonboth',
  8172. '/label',
  8173. 'input' => array(
  8174. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8175. 'id' => 'ContactImrequiredonboth',
  8176. 'required' => 'required'
  8177. ),
  8178. '/div'
  8179. );
  8180. $this->assertTags($result, $expected);
  8181. $result = $this->Form->input('Contact.imrequired');
  8182. $expected = array(
  8183. 'div' => array('class' => 'input text required'),
  8184. 'label' => array('for' => 'ContactImrequired'),
  8185. 'Imrequired',
  8186. '/label',
  8187. 'input' => array(
  8188. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8189. 'id' => 'ContactImrequired',
  8190. 'required' => 'required'
  8191. ),
  8192. '/div'
  8193. );
  8194. $this->assertTags($result, $expected);
  8195. }
  8196. /**
  8197. * Test inputDefaults setter and getter
  8198. *
  8199. * @return void
  8200. */
  8201. public function testInputDefaults() {
  8202. $this->Form->create('Contact');
  8203. $this->Form->inputDefaults(array(
  8204. 'label' => false,
  8205. 'div' => array(
  8206. 'style' => 'color: #000;'
  8207. )
  8208. ));
  8209. $result = $this->Form->input('Contact.field1');
  8210. $expected = array(
  8211. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  8212. 'input' => array(
  8213. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8214. 'id' => 'ContactField1'
  8215. ),
  8216. '/div'
  8217. );
  8218. $this->assertTags($result, $expected);
  8219. $this->Form->inputDefaults(array(
  8220. 'div' => false,
  8221. 'label' => 'Label',
  8222. ));
  8223. $result = $this->Form->input('Contact.field1');
  8224. $expected = array(
  8225. 'label' => array('for' => 'ContactField1'),
  8226. 'Label',
  8227. '/label',
  8228. 'input' => array(
  8229. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8230. 'id' => 'ContactField1'
  8231. ),
  8232. );
  8233. $this->assertTags($result, $expected);
  8234. $this->Form->inputDefaults(array(
  8235. 'label' => false,
  8236. ), true);
  8237. $result = $this->Form->input('Contact.field1');
  8238. $expected = array(
  8239. 'input' => array(
  8240. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8241. 'id' => 'ContactField1'
  8242. ),
  8243. );
  8244. $this->assertTags($result, $expected);
  8245. $result = $this->Form->inputDefaults();
  8246. $expected = array(
  8247. 'div' => false,
  8248. 'label' => false,
  8249. );
  8250. $this->assertEqual($result, $expected);
  8251. }
  8252. }