FormHelperTest.php 252 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc.
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc.
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package Cake.Test.Case.View.Helper
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('ClassRegistry', 'Utility');
  20. App::uses('Controller', 'Controller');
  21. App::uses('View', 'View');
  22. App::uses('Model', 'Model');
  23. App::uses('Security', 'Utility');
  24. App::uses('CakeRequest', 'Network');
  25. App::uses('HtmlHelper', 'View/Helper');
  26. App::uses('FormHelper', 'View/Helper');
  27. App::uses('Router', 'Routing');
  28. /**
  29. * ContactTestController class
  30. *
  31. * @package cake
  32. * @package Cake.Test.Case.View.Helper
  33. */
  34. class ContactTestController extends Controller {
  35. /**
  36. * name property
  37. *
  38. * @var string 'ContactTest'
  39. */
  40. public $name = 'ContactTest';
  41. /**
  42. * uses property
  43. *
  44. * @var mixed null
  45. */
  46. public $uses = null;
  47. }
  48. /**
  49. * Contact class
  50. *
  51. * @package cake
  52. * @package Cake.Test.Case.View.Helper
  53. */
  54. class Contact extends CakeTestModel {
  55. /**
  56. * primaryKey property
  57. *
  58. * @var string 'id'
  59. */
  60. public $primaryKey = 'id';
  61. /**
  62. * useTable property
  63. *
  64. * @var bool false
  65. */
  66. public $useTable = false;
  67. /**
  68. * name property
  69. *
  70. * @var string 'Contact'
  71. */
  72. public $name = 'Contact';
  73. /**
  74. * Default schema
  75. *
  76. * @var array
  77. */
  78. protected $_schema = array(
  79. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  80. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  81. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  82. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  83. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  84. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  85. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  86. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  87. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  88. );
  89. /**
  90. * validate property
  91. *
  92. * @var array
  93. */
  94. public $validate = array(
  95. 'non_existing' => array(),
  96. 'idontexist' => array(),
  97. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  98. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  99. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  100. 'imrequiredonboth' => array(
  101. 'required' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  102. 'check' => array('rule' => 'alphaNumeric')
  103. ),
  104. 'string_required' => 'notEmpty',
  105. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  106. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  107. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  108. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  109. 'imalsonotrequired' => array(
  110. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  111. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  112. ),
  113. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  114. );
  115. /**
  116. * schema method
  117. *
  118. * @return void
  119. */
  120. public function setSchema($schema) {
  121. $this->_schema = $schema;
  122. }
  123. /**
  124. * hasAndBelongsToMany property
  125. *
  126. * @var array
  127. */
  128. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  129. /**
  130. * hasAndBelongsToMany property
  131. *
  132. * @var array
  133. */
  134. public $belongsTo = array('User' => array('className' => 'UserForm'));
  135. }
  136. /**
  137. * ContactTagsContact class
  138. *
  139. * @package cake
  140. * @package Cake.Test.Case.View.Helper
  141. */
  142. class ContactTagsContact extends CakeTestModel {
  143. /**
  144. * useTable property
  145. *
  146. * @var bool false
  147. */
  148. public $useTable = false;
  149. /**
  150. * name property
  151. *
  152. * @var string 'Contact'
  153. */
  154. public $name = 'ContactTagsContact';
  155. /**
  156. * Default schema
  157. *
  158. * @var array
  159. */
  160. protected $_schema = array(
  161. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  162. 'contact_tag_id' => array(
  163. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  164. )
  165. );
  166. /**
  167. * schema method
  168. *
  169. * @return void
  170. */
  171. public function setSchema($schema) {
  172. $this->_schema = $schema;
  173. }
  174. }
  175. /**
  176. * ContactNonStandardPk class
  177. *
  178. * @package cake
  179. * @package Cake.Test.Case.View.Helper
  180. */
  181. class ContactNonStandardPk extends Contact {
  182. /**
  183. * primaryKey property
  184. *
  185. * @var string 'pk'
  186. */
  187. public $primaryKey = 'pk';
  188. /**
  189. * name property
  190. *
  191. * @var string 'ContactNonStandardPk'
  192. */
  193. public $name = 'ContactNonStandardPk';
  194. /**
  195. * schema method
  196. *
  197. * @return void
  198. */
  199. public function schema($field = false) {
  200. $this->_schema = parent::schema();
  201. $this->_schema['pk'] = $this->_schema['id'];
  202. unset($this->_schema['id']);
  203. return $this->_schema;
  204. }
  205. }
  206. /**
  207. * ContactTag class
  208. *
  209. * @package cake
  210. * @package Cake.Test.Case.View.Helper
  211. */
  212. class ContactTag extends Model {
  213. /**
  214. * useTable property
  215. *
  216. * @var bool false
  217. */
  218. public $useTable = false;
  219. /**
  220. * schema definition
  221. *
  222. * @var array
  223. */
  224. protected $_schema = array(
  225. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  226. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  227. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  228. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  229. );
  230. }
  231. /**
  232. * UserForm class
  233. *
  234. * @package cake
  235. * @package Cake.Test.Case.View.Helper
  236. */
  237. class UserForm extends CakeTestModel {
  238. /**
  239. * useTable property
  240. *
  241. * @var bool false
  242. */
  243. public $useTable = false;
  244. /**
  245. * primaryKey property
  246. *
  247. * @var string 'id'
  248. */
  249. public $primaryKey = 'id';
  250. /**
  251. * name property
  252. *
  253. * @var string 'UserForm'
  254. */
  255. public $name = 'UserForm';
  256. /**
  257. * hasMany property
  258. *
  259. * @var array
  260. */
  261. public $hasMany = array(
  262. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  263. ));
  264. /**
  265. * schema definition
  266. *
  267. * @var array
  268. */
  269. protected $_schema = array(
  270. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  271. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  272. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  273. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  274. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  275. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  276. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  277. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  278. );
  279. }
  280. /**
  281. * OpenidUrl class
  282. *
  283. * @package cake
  284. * @package Cake.Test.Case.View.Helper
  285. */
  286. class OpenidUrl extends CakeTestModel {
  287. /**
  288. * useTable property
  289. *
  290. * @var bool false
  291. */
  292. public $useTable = false;
  293. /**
  294. * primaryKey property
  295. *
  296. * @var string 'id'
  297. */
  298. public $primaryKey = 'id';
  299. /**
  300. * name property
  301. *
  302. * @var string 'OpenidUrl'
  303. */
  304. public $name = 'OpenidUrl';
  305. /**
  306. * belongsTo property
  307. *
  308. * @var array
  309. */
  310. public $belongsTo = array('UserForm' => array(
  311. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  312. ));
  313. /**
  314. * validate property
  315. *
  316. * @var array
  317. */
  318. public $validate = array('openid_not_registered' => array());
  319. /**
  320. * schema method
  321. *
  322. * @var array
  323. */
  324. protected $_schema = array(
  325. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  326. 'user_form_id' => array(
  327. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  328. ),
  329. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  330. );
  331. /**
  332. * beforeValidate method
  333. *
  334. * @return void
  335. */
  336. public function beforeValidate($options = array()) {
  337. $this->invalidate('openid_not_registered');
  338. return true;
  339. }
  340. }
  341. /**
  342. * ValidateUser class
  343. *
  344. * @package cake
  345. * @package Cake.Test.Case.View.Helper
  346. */
  347. class ValidateUser extends CakeTestModel {
  348. /**
  349. * primaryKey property
  350. *
  351. * @var string 'id'
  352. */
  353. public $primaryKey = 'id';
  354. /**
  355. * useTable property
  356. *
  357. * @var bool false
  358. */
  359. public $useTable = false;
  360. /**
  361. * name property
  362. *
  363. * @var string 'ValidateUser'
  364. */
  365. public $name = 'ValidateUser';
  366. /**
  367. * hasOne property
  368. *
  369. * @var array
  370. */
  371. public $hasOne = array('ValidateProfile' => array(
  372. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  373. ));
  374. /**
  375. * schema method
  376. *
  377. * @var array
  378. */
  379. protected $_schema = array(
  380. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  381. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  382. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  383. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  384. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  385. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  386. );
  387. /**
  388. * beforeValidate method
  389. *
  390. * @return void
  391. */
  392. public function beforeValidate($options = array()) {
  393. $this->invalidate('email');
  394. return false;
  395. }
  396. }
  397. /**
  398. * ValidateProfile class
  399. *
  400. * @package cake
  401. * @package Cake.Test.Case.View.Helper
  402. */
  403. class ValidateProfile extends CakeTestModel {
  404. /**
  405. * primaryKey property
  406. *
  407. * @var string 'id'
  408. */
  409. public $primaryKey = 'id';
  410. /**
  411. * useTable property
  412. *
  413. * @var bool false
  414. */
  415. public $useTable = false;
  416. /**
  417. * schema property
  418. *
  419. * @var array
  420. */
  421. protected $_schema = array(
  422. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  423. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  424. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  425. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  426. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  427. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  428. );
  429. /**
  430. * name property
  431. *
  432. * @var string 'ValidateProfile'
  433. */
  434. public $name = 'ValidateProfile';
  435. /**
  436. * hasOne property
  437. *
  438. * @var array
  439. */
  440. public $hasOne = array('ValidateItem' => array(
  441. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  442. ));
  443. /**
  444. * belongsTo property
  445. *
  446. * @var array
  447. */
  448. public $belongsTo = array('ValidateUser' => array(
  449. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  450. ));
  451. /**
  452. * beforeValidate method
  453. *
  454. * @return void
  455. */
  456. public function beforeValidate($options = array()) {
  457. $this->invalidate('full_name');
  458. $this->invalidate('city');
  459. return false;
  460. }
  461. }
  462. /**
  463. * ValidateItem class
  464. *
  465. * @package cake
  466. * @package Cake.Test.Case.View.Helper
  467. */
  468. class ValidateItem extends CakeTestModel {
  469. /**
  470. * primaryKey property
  471. *
  472. * @var string 'id'
  473. */
  474. public $primaryKey = 'id';
  475. /**
  476. * useTable property
  477. *
  478. * @var bool false
  479. */
  480. public $useTable = false;
  481. /**
  482. * name property
  483. *
  484. * @var string 'ValidateItem'
  485. */
  486. public $name = 'ValidateItem';
  487. /**
  488. * schema property
  489. *
  490. * @var array
  491. */
  492. protected $_schema = array(
  493. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  494. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  495. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  496. 'description' => array(
  497. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  498. ),
  499. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  500. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  501. );
  502. /**
  503. * belongsTo property
  504. *
  505. * @var array
  506. */
  507. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  508. /**
  509. * beforeValidate method
  510. *
  511. * @return void
  512. */
  513. public function beforeValidate($options = array()) {
  514. $this->invalidate('description');
  515. return false;
  516. }
  517. }
  518. /**
  519. * TestMail class
  520. *
  521. * @package cake
  522. * @package Cake.Test.Case.View.Helper
  523. */
  524. class TestMail extends CakeTestModel {
  525. /**
  526. * primaryKey property
  527. *
  528. * @var string 'id'
  529. */
  530. public $primaryKey = 'id';
  531. /**
  532. * useTable property
  533. *
  534. * @var bool false
  535. */
  536. public $useTable = false;
  537. /**
  538. * name property
  539. *
  540. * @var string 'TestMail'
  541. */
  542. public $name = 'TestMail';
  543. }
  544. /**
  545. * FormHelperTest class
  546. *
  547. * @package cake
  548. * @subpackage Cake.Test.Case.View.Helper
  549. * @property FormHelper $Form
  550. */
  551. class FormHelperTest extends CakeTestCase {
  552. /**
  553. * Fixtures to be used
  554. *
  555. * @var array
  556. */
  557. public $fixtures = array('core.post');
  558. /**
  559. * Do not load the fixtures by default
  560. *
  561. * @var boolean
  562. */
  563. public $autoFixtures = false;
  564. /**
  565. * setUp method
  566. *
  567. * @return void
  568. */
  569. public function setUp() {
  570. parent::setUp();
  571. Configure::write('App.base', '');
  572. $this->Controller = new ContactTestController();
  573. $this->View = new View($this->Controller);
  574. $this->Form = new FormHelper($this->View);
  575. $this->Form->Html = new HtmlHelper($this->View);
  576. $this->Form->request = new CakeRequest('contacts/add', false);
  577. $this->Form->request->here = '/contacts/add';
  578. $this->Form->request['action'] = 'add';
  579. $this->Form->request->webroot = '';
  580. $this->Form->request->base = '';
  581. ClassRegistry::addObject('Contact', new Contact());
  582. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  583. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  584. ClassRegistry::addObject('User', new UserForm());
  585. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  586. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  587. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  588. $this->oldSalt = Configure::read('Security.salt');
  589. $this->dateRegex = array(
  590. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  591. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  592. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  593. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  594. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  595. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  596. );
  597. Configure::write('Security.salt', 'foo!');
  598. }
  599. /**
  600. * tearDown method
  601. *
  602. * @return void
  603. */
  604. public function tearDown() {
  605. parent::tearDown();
  606. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  607. Configure::write('Security.salt', $this->oldSalt);
  608. }
  609. /**
  610. * testFormCreateWithSecurity method
  611. *
  612. * Test form->create() with security key.
  613. *
  614. * @return void
  615. */
  616. public function testCreateWithSecurity() {
  617. $this->Form->request['_Token'] = array('key' => 'testKey');
  618. $encoding = strtolower(Configure::read('App.encoding'));
  619. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  620. $expected = array(
  621. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  622. 'div' => array('style' => 'display:none;'),
  623. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  624. array('input' => array(
  625. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  626. )),
  627. '/div'
  628. );
  629. $this->assertTags($result, $expected);
  630. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  631. $expected['form']['id'] = 'MyForm';
  632. $this->assertTags($result, $expected);
  633. }
  634. /**
  635. * test that create() clears the fields property so it starts fresh
  636. *
  637. * @return void
  638. */
  639. public function testCreateClearingFields() {
  640. $this->Form->fields = array('model_id');
  641. $this->Form->create('Contact');
  642. $this->assertEquals($this->Form->fields, array());
  643. }
  644. /**
  645. * Tests form hash generation with model-less data
  646. *
  647. * @return void
  648. */
  649. public function testValidateHashNoModel() {
  650. $this->Form->request['_Token'] = array('key' => 'foo');
  651. $result = $this->Form->secure(array('anything'));
  652. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  653. }
  654. /**
  655. * Tests that models with identical field names get resolved properly
  656. *
  657. * @return void
  658. */
  659. public function testDuplicateFieldNameResolution() {
  660. $result = $this->Form->create('ValidateUser');
  661. $this->assertEquals($this->Form->entity(), array('ValidateUser'));
  662. $result = $this->Form->input('ValidateItem.name');
  663. $this->assertEquals($this->Form->entity(), array('ValidateItem', 'name'));
  664. $result = $this->Form->input('ValidateUser.name');
  665. $this->assertEquals($this->Form->entity(), array('ValidateUser', 'name'));
  666. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  667. $this->assertRegExp('/type="text"/', $result);
  668. $result = $this->Form->input('ValidateItem.name');
  669. $this->assertEquals($this->Form->entity(), array('ValidateItem', 'name'));
  670. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  671. $this->assertRegExp('/<textarea/', $result);
  672. $result = $this->Form->input('name');
  673. $this->assertEquals($this->Form->entity(), array('ValidateUser', 'name'));
  674. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  675. $this->assertRegExp('/type="text"/', $result);
  676. }
  677. /**
  678. * Tests that hidden fields generated for checkboxes don't get locked
  679. *
  680. * @return void
  681. */
  682. public function testNoCheckboxLocking() {
  683. $this->Form->request['_Token'] = array('key' => 'foo');
  684. $this->assertSame($this->Form->fields, array());
  685. $this->Form->checkbox('check', array('value' => '1'));
  686. $this->assertSame($this->Form->fields, array('check'));
  687. }
  688. /**
  689. * testFormSecurityFields method
  690. *
  691. * Test generation of secure form hash generation.
  692. *
  693. * @return void
  694. */
  695. public function testFormSecurityFields() {
  696. $key = 'testKey';
  697. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  698. $this->Form->request['_Token'] = array('key' => $key);
  699. $result = $this->Form->secure($fields);
  700. $expected = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  701. $expected .= ':' . 'Model.valid';
  702. $expected = array(
  703. 'div' => array('style' => 'display:none;'),
  704. array('input' => array(
  705. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  706. 'value' => urlencode($expected), 'id' => 'preg:/TokenFields\d+/'
  707. )),
  708. array('input' => array(
  709. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  710. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  711. )),
  712. '/div'
  713. );
  714. $this->assertTags($result, $expected);
  715. }
  716. /**
  717. * Tests correct generation of number fields for double and float fields
  718. *
  719. * @return void
  720. */
  721. public function testTextFieldGenerationForFloats() {
  722. $model = ClassRegistry::getObject('Contact');
  723. $model->setSchema(array('foo' => array(
  724. 'type' => 'float',
  725. 'null' => false,
  726. 'default' => null,
  727. 'length' => null
  728. )));
  729. $this->Form->create('Contact');
  730. $result = $this->Form->input('foo');
  731. $expected = array(
  732. 'div' => array('class' => 'input number'),
  733. 'label' => array('for' => 'ContactFoo'),
  734. 'Foo',
  735. '/label',
  736. array('input' => array(
  737. 'type' => 'number',
  738. 'name' => 'data[Contact][foo]',
  739. 'id' => 'ContactFoo',
  740. 'step' => 'any'
  741. )),
  742. '/div'
  743. );
  744. $this->assertTags($result, $expected);
  745. $result = $this->Form->input('foo', array('step' => 0.5));
  746. $expected = array(
  747. 'div' => array('class' => 'input number'),
  748. 'label' => array('for' => 'ContactFoo'),
  749. 'Foo',
  750. '/label',
  751. array('input' => array(
  752. 'type' => 'number',
  753. 'name' => 'data[Contact][foo]',
  754. 'id' => 'ContactFoo',
  755. 'step' => '0.5'
  756. )),
  757. '/div'
  758. );
  759. $this->assertTags($result, $expected);
  760. }
  761. /**
  762. * Tests correct generation of number fields for integer fields
  763. *
  764. * @access public
  765. * @return void
  766. */
  767. public function testTextFieldTypeNumberGenerationForIntegers() {
  768. $model = ClassRegistry::getObject('Contact');
  769. $model->setSchema(array('foo' => array(
  770. 'type' => 'integer',
  771. 'null' => false,
  772. 'default' => null,
  773. 'length' => null
  774. )));
  775. $this->Form->create('Contact');
  776. $result = $this->Form->input('foo');
  777. $expected = array(
  778. 'div' => array('class' => 'input number'),
  779. 'label' => array('for' => 'ContactFoo'),
  780. 'Foo',
  781. '/label',
  782. array('input' => array(
  783. 'type' => 'number', 'name' => 'data[Contact][foo]',
  784. 'id' => 'ContactFoo'
  785. )),
  786. '/div'
  787. );
  788. $this->assertTags($result, $expected);
  789. }
  790. /**
  791. * testFormSecurityMultipleFields method
  792. *
  793. * Test secure() with multiple row form. Ensure hash is correct.
  794. *
  795. * @return void
  796. */
  797. public function testFormSecurityMultipleFields() {
  798. $key = 'testKey';
  799. $fields = array(
  800. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  801. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  802. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  803. );
  804. $this->Form->request['_Token'] = array('key' => $key);
  805. $result = $this->Form->secure($fields);
  806. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  807. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  808. $expected = array(
  809. 'div' => array('style' => 'display:none;'),
  810. array('input' => array(
  811. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  812. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  813. )),
  814. array('input' => array(
  815. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  816. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  817. )),
  818. '/div'
  819. );
  820. $this->assertTags($result, $expected);
  821. }
  822. /**
  823. * testFormSecurityMultipleSubmitButtons
  824. *
  825. * test form submit generation and ensure that _Token is only created on end()
  826. *
  827. * @return void
  828. */
  829. public function testFormSecurityMultipleSubmitButtons() {
  830. $key = 'testKey';
  831. $this->Form->request['_Token'] = array('key' => $key);
  832. $this->Form->create('Addresses');
  833. $this->Form->input('Address.title');
  834. $this->Form->input('Address.first_name');
  835. $result = $this->Form->submit('Save', array('name' => 'save'));
  836. $expected = array(
  837. 'div' => array('class' => 'submit'),
  838. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  839. '/div',
  840. );
  841. $this->assertTags($result, $expected);
  842. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  843. $expected = array(
  844. 'div' => array('class' => 'submit'),
  845. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  846. '/div',
  847. );
  848. $this->assertTags($result, $expected);
  849. $result = $this->Form->end(null);
  850. $expected = array(
  851. 'div' => array('style' => 'display:none;'),
  852. array('input' => array(
  853. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  854. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  855. )),
  856. array('input' => array(
  857. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  858. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  859. )),
  860. '/div'
  861. );
  862. $this->assertTags($result, $expected);
  863. }
  864. /**
  865. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  866. *
  867. * @return void
  868. */
  869. public function testSecurityButtonNestedNamed() {
  870. $key = 'testKey';
  871. $this->Form->request['_Token'] = array('key' => $key);
  872. $this->Form->create('Addresses');
  873. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  874. $result = $this->Form->unlockField();
  875. $this->assertEquals(array('Address.button'), $result);
  876. }
  877. /**
  878. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  879. *
  880. * @return void
  881. */
  882. public function testSecuritySubmitNestedNamed() {
  883. $key = 'testKey';
  884. $this->Form->request['_Token'] = array('key' => $key);
  885. $this->Form->create('Addresses');
  886. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  887. $result = $this->Form->unlockField();
  888. $this->assertEquals(array('Address.button'), $result);
  889. }
  890. /**
  891. * Test that the correct fields are unlocked for image submits with no names.
  892. *
  893. * @return void
  894. */
  895. public function testSecuritySubmitImageNoName() {
  896. $key = 'testKey';
  897. $this->Form->request['_Token'] = array('key' => $key);
  898. $this->Form->create('User');
  899. $result = $this->Form->submit('save.png');
  900. $expected = array(
  901. 'div' => array('class' => 'submit'),
  902. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  903. '/div'
  904. );
  905. $this->assertTags($result, $expected);
  906. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  907. }
  908. /**
  909. * Test that the correct fields are unlocked for image submits with names.
  910. *
  911. * @return void
  912. */
  913. public function testSecuritySubmitImageName() {
  914. $key = 'testKey';
  915. $this->Form->request['_Token'] = array('key' => $key);
  916. $this->Form->create('User');
  917. $result = $this->Form->submit('save.png', array('name' => 'test'));
  918. $expected = array(
  919. 'div' => array('class' => 'submit'),
  920. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  921. '/div'
  922. );
  923. $this->assertTags($result, $expected);
  924. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  925. }
  926. /**
  927. * testFormSecurityMultipleInputFields method
  928. *
  929. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  930. *
  931. * @return void
  932. */
  933. public function testFormSecurityMultipleInputFields() {
  934. $key = 'testKey';
  935. $this->Form->request['_Token'] = array('key' => $key);
  936. $this->Form->create('Addresses');
  937. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  938. $this->Form->input('Addresses.0.title');
  939. $this->Form->input('Addresses.0.first_name');
  940. $this->Form->input('Addresses.0.last_name');
  941. $this->Form->input('Addresses.0.address');
  942. $this->Form->input('Addresses.0.city');
  943. $this->Form->input('Addresses.0.phone');
  944. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  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. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  953. $result = $this->Form->secure($this->Form->fields);
  954. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  955. $expected = array(
  956. 'div' => array('style' => 'display:none;'),
  957. array('input' => array(
  958. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  959. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  960. )),
  961. array('input' => array(
  962. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  963. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  964. )),
  965. '/div'
  966. );
  967. $this->assertTags($result, $expected);
  968. }
  969. /**
  970. * Test form security with Model.field.0 style inputs
  971. *
  972. * @return void
  973. */
  974. public function testFormSecurityArrayFields() {
  975. $key = 'testKey';
  976. $this->Form->request->params['_Token']['key'] = $key;
  977. $this->Form->create('Address');
  978. $this->Form->input('Address.primary.1');
  979. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  980. $this->Form->input('Address.secondary.1.0');
  981. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  982. }
  983. /**
  984. * testFormSecurityMultipleInputDisabledFields method
  985. *
  986. * test secure form generation with multiple records and disabled fields.
  987. *
  988. * @return void
  989. */
  990. public function testFormSecurityMultipleInputDisabledFields() {
  991. $key = 'testKey';
  992. $this->Form->request->params['_Token'] = array(
  993. 'key' => $key,
  994. 'unlockedFields' => array('first_name', 'address')
  995. );
  996. $this->Form->create();
  997. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  998. $this->Form->input('Addresses.0.title');
  999. $this->Form->input('Addresses.0.first_name');
  1000. $this->Form->input('Addresses.0.last_name');
  1001. $this->Form->input('Addresses.0.address');
  1002. $this->Form->input('Addresses.0.city');
  1003. $this->Form->input('Addresses.0.phone');
  1004. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1005. $this->Form->input('Addresses.1.title');
  1006. $this->Form->input('Addresses.1.first_name');
  1007. $this->Form->input('Addresses.1.last_name');
  1008. $this->Form->input('Addresses.1.address');
  1009. $this->Form->input('Addresses.1.city');
  1010. $this->Form->input('Addresses.1.phone');
  1011. $result = $this->Form->secure($this->Form->fields);
  1012. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  1013. $expected = array(
  1014. 'div' => array('style' => 'display:none;'),
  1015. array('input' => array(
  1016. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1017. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1018. )),
  1019. array('input' => array(
  1020. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1021. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1022. )),
  1023. '/div'
  1024. );
  1025. $this->assertTags($result, $expected);
  1026. }
  1027. /**
  1028. * testFormSecurityInputDisabledFields method
  1029. *
  1030. * Test single record form with disabled fields.
  1031. *
  1032. * @return void
  1033. */
  1034. public function testFormSecurityInputUnlockedFields() {
  1035. $key = 'testKey';
  1036. $this->Form->request['_Token'] = array(
  1037. 'key' => $key,
  1038. 'unlockedFields' => array('first_name', 'address')
  1039. );
  1040. $this->Form->create();
  1041. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1042. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1043. $this->Form->input('Addresses.title');
  1044. $this->Form->input('Addresses.first_name');
  1045. $this->Form->input('Addresses.last_name');
  1046. $this->Form->input('Addresses.address');
  1047. $this->Form->input('Addresses.city');
  1048. $this->Form->input('Addresses.phone');
  1049. $result = $this->Form->fields;
  1050. $expected = array(
  1051. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1052. 'Addresses.city', 'Addresses.phone'
  1053. );
  1054. $this->assertEquals($expected, $result);
  1055. $result = $this->Form->secure($expected);
  1056. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1057. $expected = array(
  1058. 'div' => array('style' => 'display:none;'),
  1059. array('input' => array(
  1060. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1061. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1062. )),
  1063. array('input' => array(
  1064. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1065. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1066. )),
  1067. '/div'
  1068. );
  1069. $this->assertTags($result, $expected);
  1070. }
  1071. /**
  1072. * test securing inputs with custom name attributes.
  1073. *
  1074. * @return void
  1075. */
  1076. public function testFormSecureWithCustomNameAttribute() {
  1077. $this->Form->request->params['_Token']['key'] = 'testKey';
  1078. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1079. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1080. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1081. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1082. }
  1083. /**
  1084. * testFormSecuredInput method
  1085. *
  1086. * Test generation of entire secure form, assertions made on input() output.
  1087. *
  1088. * @return void
  1089. */
  1090. public function testFormSecuredInput() {
  1091. $this->Form->request['_Token'] = array('key' => 'testKey');
  1092. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1093. $encoding = strtolower(Configure::read('App.encoding'));
  1094. $expected = array(
  1095. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1096. 'div' => array('style' => 'display:none;'),
  1097. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1098. array('input' => array(
  1099. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1100. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1101. )),
  1102. '/div'
  1103. );
  1104. $this->assertTags($result, $expected);
  1105. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1106. $expected = array(
  1107. 'div' => array('class' => 'input text'),
  1108. 'label' => array('for' => 'UserFormPublished'),
  1109. 'Published',
  1110. '/label',
  1111. array('input' => array(
  1112. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1113. 'id' => 'UserFormPublished'
  1114. )),
  1115. '/div'
  1116. );
  1117. $this->assertTags($result, $expected);
  1118. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1119. $expected = array(
  1120. 'div' => array('class' => 'input text'),
  1121. 'label' => array('for' => 'UserFormOther'),
  1122. 'Other',
  1123. '/label',
  1124. array('input' => array(
  1125. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1126. 'id' => 'UserFormOther'
  1127. )),
  1128. '/div'
  1129. );
  1130. $this->assertTags($result, $expected);
  1131. $result = $this->Form->hidden('UserForm.stuff');
  1132. $expected = array('input' => array(
  1133. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1134. 'id' => 'UserFormStuff'
  1135. ));
  1136. $this->assertTags($result, $expected);
  1137. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1138. $expected = array('input' => array(
  1139. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1140. 'value' => '0', 'id' => 'UserFormHidden'
  1141. ));
  1142. $this->assertTags($result, $expected);
  1143. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1144. $expected = array(
  1145. 'div' => array('class' => 'input checkbox'),
  1146. array('input' => array(
  1147. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1148. 'value' => '0', 'id' => 'UserFormSomething_'
  1149. )),
  1150. array('input' => array(
  1151. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1152. 'value' => '1', 'id' => 'UserFormSomething'
  1153. )),
  1154. 'label' => array('for' => 'UserFormSomething'),
  1155. 'Something',
  1156. '/label',
  1157. '/div'
  1158. );
  1159. $this->assertTags($result, $expected);
  1160. $result = $this->Form->fields;
  1161. $expected = array(
  1162. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1163. 'UserForm.hidden' => '0', 'UserForm.something'
  1164. );
  1165. $this->assertEquals($expected, $result);
  1166. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1167. $result = $this->Form->secure($this->Form->fields);
  1168. $expected = array(
  1169. 'div' => array('style' => 'display:none;'),
  1170. array('input' => array(
  1171. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1172. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1173. )),
  1174. array('input' => array(
  1175. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1176. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1177. )),
  1178. '/div'
  1179. );
  1180. $this->assertTags($result, $expected);
  1181. }
  1182. /**
  1183. * Tests that the correct keys are added to the field hash index
  1184. *
  1185. * @return void
  1186. */
  1187. public function testFormSecuredFileInput() {
  1188. $this->Form->request['_Token'] = array('key' => 'testKey');
  1189. $this->assertEquals($this->Form->fields, array());
  1190. $result = $this->Form->file('Attachment.file');
  1191. $expected = array(
  1192. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1193. 'Attachment.file.error', 'Attachment.file.size'
  1194. );
  1195. $this->assertEquals($this->Form->fields, $expected);
  1196. }
  1197. /**
  1198. * test that multiple selects keys are added to field hash
  1199. *
  1200. * @return void
  1201. */
  1202. public function testFormSecuredMultipleSelect() {
  1203. $this->Form->request['_Token'] = array('key' => 'testKey');
  1204. $this->assertEquals($this->Form->fields, array());
  1205. $options = array('1' => 'one', '2' => 'two');
  1206. $this->Form->select('Model.select', $options);
  1207. $expected = array('Model.select');
  1208. $this->assertEquals($this->Form->fields, $expected);
  1209. $this->Form->fields = array();
  1210. $this->Form->select('Model.select', $options, array('multiple' => true));
  1211. $this->assertEquals($this->Form->fields, $expected);
  1212. }
  1213. /**
  1214. * testFormSecuredRadio method
  1215. *
  1216. * @return void
  1217. */
  1218. public function testFormSecuredRadio() {
  1219. $this->Form->request['_Token'] = array('key' => 'testKey');
  1220. $this->assertEquals($this->Form->fields, array());
  1221. $options = array('1' => 'option1', '2' => 'option2');
  1222. $this->Form->radio('Test.test', $options);
  1223. $expected = array('Test.test');
  1224. $this->assertEquals($this->Form->fields, $expected);
  1225. }
  1226. /**
  1227. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1228. * hashing.
  1229. *
  1230. * @return void
  1231. */
  1232. public function testFormSecuredAndDisabled() {
  1233. $this->Form->request['_Token'] = array('key' => 'testKey');
  1234. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1235. $this->Form->text('Model.text', array('disabled' => true));
  1236. $this->Form->password('Model.text', array('disabled' => true));
  1237. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1238. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1239. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1240. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1241. $this->Form->month('Model.month', array('disabled' => true));
  1242. $this->Form->day('Model.day', array('disabled' => true));
  1243. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1244. $this->Form->minute('Model.minute', array('disabled' => true));
  1245. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1246. $expected = array(
  1247. 'Model.radio' => ''
  1248. );
  1249. $this->assertEquals($expected, $this->Form->fields);
  1250. }
  1251. /**
  1252. * testDisableSecurityUsingForm method
  1253. *
  1254. * @return void
  1255. */
  1256. public function testDisableSecurityUsingForm() {
  1257. $this->Form->request['_Token'] = array(
  1258. 'key' => 'testKey',
  1259. 'disabledFields' => array()
  1260. );
  1261. $this->Form->create();
  1262. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1263. $this->Form->input('Addresses.title');
  1264. $this->Form->input('Addresses.first_name', array('secure' => false));
  1265. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1266. $this->Form->input('Addresses.zip', array(
  1267. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1268. ));
  1269. $result = $this->Form->fields;
  1270. $expected = array(
  1271. 'Addresses.id' => '123456', 'Addresses.title',
  1272. );
  1273. $this->assertEquals($expected, $result);
  1274. }
  1275. /**
  1276. * test disableField
  1277. *
  1278. * @return void
  1279. */
  1280. public function testUnlockFieldAddsToList() {
  1281. $this->Form->request['_Token'] = array(
  1282. 'key' => 'testKey',
  1283. 'unlockedFields' => array()
  1284. );
  1285. $this->Form->create('Contact');
  1286. $this->Form->unlockField('Contact.name');
  1287. $this->Form->text('Contact.name');
  1288. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1289. $this->assertEquals(array(), $this->Form->fields);
  1290. }
  1291. /**
  1292. * test unlockField removing from fields array.
  1293. *
  1294. * @return void
  1295. */
  1296. public function testUnlockFieldRemovingFromFields() {
  1297. $this->Form->request['_Token'] = array(
  1298. 'key' => 'testKey',
  1299. 'unlockedFields' => array()
  1300. );
  1301. $this->Form->create('Contact');
  1302. $this->Form->hidden('Contact.id', array('value' => 1));
  1303. $this->Form->text('Contact.name');
  1304. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1305. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1306. $this->Form->unlockField('Contact.name');
  1307. $this->Form->unlockField('Contact.id');
  1308. $this->assertEquals(array(), $this->Form->fields);
  1309. }
  1310. /**
  1311. * testTagIsInvalid method
  1312. *
  1313. * @return void
  1314. */
  1315. public function testTagIsInvalid() {
  1316. $Contact = ClassRegistry::getObject('Contact');
  1317. $Contact->validationErrors[0]['email'] = array('Please provide an email');
  1318. $this->Form->setEntity('Contact.0.email');
  1319. $result = $this->Form->tagIsInvalid();
  1320. $expected = array('Please provide an email');
  1321. $this->assertEquals($expected, $result);
  1322. $this->Form->setEntity('Contact.1.email');
  1323. $result = $this->Form->tagIsInvalid();
  1324. $expected = false;
  1325. $this->assertIdentical($expected, $result);
  1326. $this->Form->setEntity('Contact.0.name');
  1327. $result = $this->Form->tagIsInvalid();
  1328. $expected = false;
  1329. $this->assertIdentical($expected, $result);
  1330. }
  1331. /**
  1332. * testPasswordValidation method
  1333. *
  1334. * test validation errors on password input.
  1335. *
  1336. * @return void
  1337. */
  1338. public function testPasswordValidation() {
  1339. $Contact = ClassRegistry::getObject('Contact');
  1340. $Contact->validationErrors['password'] = array('Please provide a password');
  1341. $result = $this->Form->input('Contact.password');
  1342. $expected = array(
  1343. 'div' => array('class' => 'input password error'),
  1344. 'label' => array('for' => 'ContactPassword'),
  1345. 'Password',
  1346. '/label',
  1347. 'input' => array(
  1348. 'type' => 'password', 'name' => 'data[Contact][password]',
  1349. 'id' => 'ContactPassword', 'class' => 'form-error'
  1350. ),
  1351. array('div' => array('class' => 'error-message')),
  1352. 'Please provide a password',
  1353. '/div',
  1354. '/div'
  1355. );
  1356. $this->assertTags($result, $expected);
  1357. }
  1358. /**
  1359. * testEmptyErrorValidation method
  1360. *
  1361. * test validation error div when validation message is an empty string
  1362. *
  1363. * @access public
  1364. * @return void
  1365. */
  1366. public function testEmptyErrorValidation() {
  1367. $this->Form->validationErrors['Contact']['password'] = '';
  1368. $result = $this->Form->input('Contact.password');
  1369. $expected = array(
  1370. 'div' => array('class' => 'input password error'),
  1371. 'label' => array('for' => 'ContactPassword'),
  1372. 'Password',
  1373. '/label',
  1374. 'input' => array(
  1375. 'type' => 'password', 'name' => 'data[Contact][password]',
  1376. 'id' => 'ContactPassword', 'class' => 'form-error'
  1377. ),
  1378. array('div' => array('class' => 'error-message')),
  1379. array(),
  1380. '/div',
  1381. '/div'
  1382. );
  1383. $this->assertTags($result, $expected);
  1384. }
  1385. /**
  1386. * testEmptyInputErrorValidation method
  1387. *
  1388. * test validation error div when validation message is overridden by an empty string when calling input()
  1389. *
  1390. * @access public
  1391. * @return void
  1392. */
  1393. public function testEmptyInputErrorValidation() {
  1394. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1395. $result = $this->Form->input('Contact.password', array('error' => ''));
  1396. $expected = array(
  1397. 'div' => array('class' => 'input password error'),
  1398. 'label' => array('for' => 'ContactPassword'),
  1399. 'Password',
  1400. '/label',
  1401. 'input' => array(
  1402. 'type' => 'password', 'name' => 'data[Contact][password]',
  1403. 'id' => 'ContactPassword', 'class' => 'form-error'
  1404. ),
  1405. array('div' => array('class' => 'error-message')),
  1406. array(),
  1407. '/div',
  1408. '/div'
  1409. );
  1410. $this->assertTags($result, $expected);
  1411. }
  1412. /**
  1413. * testFormValidationAssociated method
  1414. *
  1415. * test display of form errors in conjunction with model::validates.
  1416. *
  1417. * @return void
  1418. */
  1419. public function testFormValidationAssociated() {
  1420. $this->UserForm = ClassRegistry::getObject('UserForm');
  1421. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1422. $data = array(
  1423. 'UserForm' => array('name' => 'user'),
  1424. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1425. );
  1426. $result = $this->UserForm->OpenidUrl->create($data);
  1427. $this->assertFalse(empty($result));
  1428. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1429. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1430. $encoding = strtolower(Configure::read('App.encoding'));
  1431. $expected = array(
  1432. 'form' => array(
  1433. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1434. 'accept-charset' => $encoding
  1435. ),
  1436. 'div' => array('style' => 'display:none;'),
  1437. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1438. '/div'
  1439. );
  1440. $this->assertTags($result, $expected);
  1441. $result = $this->Form->error(
  1442. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1443. );
  1444. $this->assertEquals($result, 'Error, not registered');
  1445. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1446. }
  1447. /**
  1448. * testFormValidationAssociatedFirstLevel method
  1449. *
  1450. * test form error display with associated model.
  1451. *
  1452. * @return void
  1453. */
  1454. public function testFormValidationAssociatedFirstLevel() {
  1455. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1456. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1457. $data = array(
  1458. 'ValidateUser' => array('name' => 'mariano'),
  1459. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1460. );
  1461. $result = $this->ValidateUser->create($data);
  1462. $this->assertFalse(empty($result));
  1463. $this->assertFalse($this->ValidateUser->validates());
  1464. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1465. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1466. $encoding = strtolower(Configure::read('App.encoding'));
  1467. $expected = array(
  1468. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1469. 'div' => array('style' => 'display:none;'),
  1470. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1471. '/div'
  1472. );
  1473. $this->assertTags($result, $expected);
  1474. $result = $this->Form->error(
  1475. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1476. );
  1477. $this->assertEquals($result, 'Invalid email');
  1478. $result = $this->Form->error(
  1479. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1480. );
  1481. $this->assertEquals($result, 'Invalid name');
  1482. $result = $this->Form->error(
  1483. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1484. );
  1485. $this->assertEquals($result, 'Invalid city');
  1486. unset($this->ValidateUser->ValidateProfile);
  1487. unset($this->ValidateUser);
  1488. }
  1489. /**
  1490. * testFormValidationAssociatedSecondLevel method
  1491. *
  1492. * test form error display with associated model.
  1493. *
  1494. * @return void
  1495. */
  1496. public function testFormValidationAssociatedSecondLevel() {
  1497. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1498. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1499. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1500. $data = array(
  1501. 'ValidateUser' => array('name' => 'mariano'),
  1502. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1503. 'ValidateItem' => array('name' => 'Item')
  1504. );
  1505. $result = $this->ValidateUser->create($data);
  1506. $this->assertFalse(empty($result));
  1507. $this->assertFalse($this->ValidateUser->validates());
  1508. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1509. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1510. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1511. $encoding = strtolower(Configure::read('App.encoding'));
  1512. $expected = array(
  1513. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1514. 'div' => array('style' => 'display:none;'),
  1515. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1516. '/div'
  1517. );
  1518. $this->assertTags($result, $expected);
  1519. $result = $this->Form->error(
  1520. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1521. );
  1522. $this->assertEquals($result, 'Invalid email');
  1523. $result = $this->Form->error(
  1524. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1525. );
  1526. $this->assertEquals($result, 'Invalid name');
  1527. $result = $this->Form->error(
  1528. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1529. );
  1530. $result = $this->Form->error(
  1531. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1532. );
  1533. $this->assertEquals($result, 'Invalid description');
  1534. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1535. unset($this->ValidateUser->ValidateProfile);
  1536. unset($this->ValidateUser);
  1537. }
  1538. /**
  1539. * testFormValidationMultiRecord method
  1540. *
  1541. * test form error display with multiple records.
  1542. *
  1543. * @return void
  1544. */
  1545. public function testFormValidationMultiRecord() {
  1546. $Contact = ClassRegistry::getObject('Contact');
  1547. $Contact->validationErrors[2] = array(
  1548. 'name' => array('This field cannot be left blank')
  1549. );
  1550. $result = $this->Form->input('Contact.2.name');
  1551. $expected = array(
  1552. 'div' => array('class' => 'input text error'),
  1553. 'label' => array('for' => 'Contact2Name'),
  1554. 'Name',
  1555. '/label',
  1556. 'input' => array(
  1557. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1558. 'class' => 'form-error', 'maxlength' => 255
  1559. ),
  1560. array('div' => array('class' => 'error-message')),
  1561. 'This field cannot be left blank',
  1562. '/div',
  1563. '/div'
  1564. );
  1565. $this->assertTags($result, $expected);
  1566. }
  1567. /**
  1568. * testMultipleInputValidation method
  1569. *
  1570. * test multiple record form validation error display.
  1571. *
  1572. * @return void
  1573. */
  1574. public function testMultipleInputValidation() {
  1575. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1576. $Address->validationErrors[0] = array(
  1577. 'title' => array('This field cannot be empty'),
  1578. 'first_name' => array('This field cannot be empty')
  1579. );
  1580. $Address->validationErrors[1] = array(
  1581. 'last_name' => array('You must have a last name')
  1582. );
  1583. $this->Form->create();
  1584. $result = $this->Form->input('Address.0.title');
  1585. $expected = array(
  1586. 'div' => array('class'),
  1587. 'label' => array('for'),
  1588. 'preg:/[^<]+/',
  1589. '/label',
  1590. 'input' => array(
  1591. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1592. ),
  1593. array('div' => array('class' => 'error-message')),
  1594. 'This field cannot be empty',
  1595. '/div',
  1596. '/div'
  1597. );
  1598. $this->assertTags($result, $expected);
  1599. $result = $this->Form->input('Address.0.first_name');
  1600. $expected = array(
  1601. 'div' => array('class'),
  1602. 'label' => array('for'),
  1603. 'preg:/[^<]+/',
  1604. '/label',
  1605. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1606. array('div' => array('class' => 'error-message')),
  1607. 'This field cannot be empty',
  1608. '/div',
  1609. '/div'
  1610. );
  1611. $this->assertTags($result, $expected);
  1612. $result = $this->Form->input('Address.0.last_name');
  1613. $expected = array(
  1614. 'div' => array('class'),
  1615. 'label' => array('for'),
  1616. 'preg:/[^<]+/',
  1617. '/label',
  1618. 'input' => array('type' => 'text', 'name', 'id'),
  1619. '/div'
  1620. );
  1621. $this->assertTags($result, $expected);
  1622. $result = $this->Form->input('Address.1.last_name');
  1623. $expected = array(
  1624. 'div' => array('class'),
  1625. 'label' => array('for'),
  1626. 'preg:/[^<]+/',
  1627. '/label',
  1628. 'input' => array(
  1629. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1630. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1631. ),
  1632. array('div' => array('class' => 'error-message')),
  1633. 'You must have a last name',
  1634. '/div',
  1635. '/div'
  1636. );
  1637. $this->assertTags($result, $expected);
  1638. }
  1639. /**
  1640. * testInput method
  1641. *
  1642. * Test various incarnations of input().
  1643. *
  1644. * @return void
  1645. */
  1646. public function testInput() {
  1647. $result = $this->Form->input('ValidateUser.balance');
  1648. $expected = array(
  1649. 'div' => array('class'),
  1650. 'label' => array('for'),
  1651. 'Balance',
  1652. '/label',
  1653. 'input' => array('name', 'type' => 'number', 'maxlength' => 8, 'id'),
  1654. '/div',
  1655. );
  1656. $this->assertTags($result, $expected);
  1657. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1658. $expected = array(
  1659. 'div' => array('class' => 'input text'),
  1660. 'label' => array('for' => 'custom'),
  1661. 'Email',
  1662. '/label',
  1663. array('input' => array(
  1664. 'type' => 'text', 'name' => 'data[Contact][email]',
  1665. 'id' => 'custom', 'maxlength' => 255
  1666. )),
  1667. '/div'
  1668. );
  1669. $this->assertTags($result, $expected);
  1670. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1671. $expected = array(
  1672. '<div',
  1673. 'label' => array('for' => 'ContactEmail'),
  1674. 'Email',
  1675. '/label',
  1676. array('input' => array(
  1677. 'type' => 'text', 'name' => 'data[Contact][email]',
  1678. 'id' => 'ContactEmail', 'maxlength' => 255
  1679. )),
  1680. '/div'
  1681. );
  1682. $this->assertTags($result, $expected);
  1683. $result = $this->Form->hidden('Contact.idontexist');
  1684. $expected = array('input' => array(
  1685. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1686. 'id' => 'ContactIdontexist'
  1687. ));
  1688. $this->assertTags($result, $expected);
  1689. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1690. $expected = array(
  1691. 'div' => array('class' => 'input text'),
  1692. 'label' => array('for' => 'ContactEmail'),
  1693. 'Email',
  1694. '/label',
  1695. array('input' => array(
  1696. 'type' => 'text', 'name' => 'data[Contact][email]',
  1697. 'id' => 'ContactEmail'
  1698. )),
  1699. '/div'
  1700. );
  1701. $this->assertTags($result, $expected);
  1702. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1703. $expected = array(
  1704. 'div' => array('class' => 'input text'),
  1705. 'label' => array('for' => 'Contact5Email'),
  1706. 'Email',
  1707. '/label',
  1708. array('input' => array(
  1709. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1710. 'id' => 'Contact5Email'
  1711. )),
  1712. '/div'
  1713. );
  1714. $this->assertTags($result, $expected);
  1715. $result = $this->Form->input('Contact.password');
  1716. $expected = array(
  1717. 'div' => array('class' => 'input password'),
  1718. 'label' => array('for' => 'ContactPassword'),
  1719. 'Password',
  1720. '/label',
  1721. array('input' => array(
  1722. 'type' => 'password', 'name' => 'data[Contact][password]',
  1723. 'id' => 'ContactPassword'
  1724. )),
  1725. '/div'
  1726. );
  1727. $this->assertTags($result, $expected);
  1728. $result = $this->Form->input('Contact.email', array(
  1729. 'type' => 'file', 'class' => 'textbox'
  1730. ));
  1731. $expected = array(
  1732. 'div' => array('class' => 'input file'),
  1733. 'label' => array('for' => 'ContactEmail'),
  1734. 'Email',
  1735. '/label',
  1736. array('input' => array(
  1737. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1738. 'id' => 'ContactEmail'
  1739. )),
  1740. '/div'
  1741. );
  1742. $this->assertTags($result, $expected);
  1743. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1744. $result = $this->Form->input('Contact.phone');
  1745. $expected = array(
  1746. 'div' => array('class' => 'input text'),
  1747. 'label' => array('for' => 'ContactPhone'),
  1748. 'Phone',
  1749. '/label',
  1750. array('input' => array(
  1751. 'type' => 'text', 'name' => 'data[Contact][phone]',
  1752. 'value' => 'Hello &amp; World &gt; weird chars',
  1753. 'id' => 'ContactPhone', 'maxlength' => 255
  1754. )),
  1755. '/div'
  1756. );
  1757. $this->assertTags($result, $expected);
  1758. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1759. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1760. $expected = array(
  1761. 'div' => array('class' => 'input text'),
  1762. 'label' => array('for' => 'myId'),
  1763. 'Field',
  1764. '/label',
  1765. 'input' => array(
  1766. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1767. 'value' => 'My value', 'id' => 'myId'
  1768. ),
  1769. '/div'
  1770. );
  1771. $this->assertTags($result, $expected);
  1772. unset($this->Form->request->data);
  1773. $Contact = ClassRegistry::getObject('Contact');
  1774. $Contact->validationErrors['field'] = array('Badness!');
  1775. $result = $this->Form->input('Contact.field');
  1776. $expected = array(
  1777. 'div' => array('class' => 'input text error'),
  1778. 'label' => array('for' => 'ContactField'),
  1779. 'Field',
  1780. '/label',
  1781. 'input' => array(
  1782. 'type' => 'text', 'name' => 'data[Contact][field]',
  1783. 'id' => 'ContactField', 'class' => 'form-error'
  1784. ),
  1785. array('div' => array('class' => 'error-message')),
  1786. 'Badness!',
  1787. '/div',
  1788. '/div'
  1789. );
  1790. $this->assertTags($result, $expected);
  1791. $result = $this->Form->input('Contact.field', array(
  1792. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1793. ));
  1794. $expected = array(
  1795. 'label' => array('for' => 'ContactField'),
  1796. 'Field',
  1797. '/label',
  1798. 'input' => array(
  1799. 'type' => 'text', 'name' => 'data[Contact][field]',
  1800. 'id' => 'ContactField', 'class' => 'form-error'
  1801. ),
  1802. array('span' => array('class' => 'error-message')),
  1803. 'Badness!',
  1804. '/span'
  1805. );
  1806. $this->assertTags($result, $expected);
  1807. $result = $this->Form->input('Contact.field', array(
  1808. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1809. ));
  1810. $expected = array(
  1811. 'div' => array('class' => 'input text error'),
  1812. 'label' => array('for' => 'ContactField'),
  1813. 'Field',
  1814. '/label',
  1815. 'input' => array(
  1816. 'type' => 'text', 'name' => 'data[Contact][field]',
  1817. 'id' => 'ContactField', 'class' => 'form-error'
  1818. ),
  1819. array('div' => array('class' => 'error')),
  1820. 'Badness!',
  1821. '/div'
  1822. );
  1823. $this->assertTags($result, $expected);
  1824. $result = $this->Form->input('Contact.field', array(
  1825. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1826. ));
  1827. $expected = array(
  1828. 'span' => array('class' => 'input text error'),
  1829. 'label' => array('for' => 'ContactField'),
  1830. 'Field',
  1831. '/label',
  1832. 'input' => array(
  1833. 'type' => 'text', 'name' => 'data[Contact][field]',
  1834. 'id' => 'ContactField', 'class' => 'form-error'
  1835. ),
  1836. 'Badness!',
  1837. '/span'
  1838. );
  1839. $this->assertTags($result, $expected);
  1840. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1841. $expected = array(
  1842. 'div' => array('class' => 'input text error'),
  1843. 'label' => array('for' => 'ContactField'),
  1844. 'Field',
  1845. '/label',
  1846. 'input' => array(
  1847. 'type' => 'text', 'name' => 'data[Contact][field]',
  1848. 'id' => 'ContactField', 'class' => 'form-error'
  1849. ),
  1850. 'A message to you, Rudy',
  1851. array('div' => array('class' => 'error-message')),
  1852. 'Badness!',
  1853. '/div',
  1854. '/div'
  1855. );
  1856. $this->assertTags($result, $expected);
  1857. $this->Form->setEntity(null);
  1858. $this->Form->setEntity('Contact.field');
  1859. $result = $this->Form->input('Contact.field', array(
  1860. 'after' => 'A message to you, Rudy', 'error' => false
  1861. ));
  1862. $expected = array(
  1863. 'div' => array('class' => 'input text'),
  1864. 'label' => array('for' => 'ContactField'),
  1865. 'Field',
  1866. '/label',
  1867. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1868. 'A message to you, Rudy',
  1869. '/div'
  1870. );
  1871. $this->assertTags($result, $expected);
  1872. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1873. $expected = array(
  1874. 'div' => array('class' => 'input text'),
  1875. 'label' => array('for' => 'ObjectField'),
  1876. 'Field',
  1877. '/label',
  1878. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1879. 'A message to you, Rudy',
  1880. '/div'
  1881. );
  1882. $this->assertTags($result, $expected);
  1883. $Contact->validationErrors['field'] = array('minLength');
  1884. $result = $this->Form->input('Contact.field', array(
  1885. 'error' => array(
  1886. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1887. 'maxLength' => 'login too large'
  1888. )
  1889. ));
  1890. $expected = array(
  1891. 'div' => array('class' => 'input text error'),
  1892. 'label' => array('for' => 'ContactField'),
  1893. 'Field',
  1894. '/label',
  1895. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1896. array('div' => array('class' => 'error-message')),
  1897. 'Le login doit contenir au moins 2 caractères',
  1898. '/div',
  1899. '/div'
  1900. );
  1901. $this->assertTags($result, $expected);
  1902. $Contact->validationErrors['field'] = array('maxLength');
  1903. $result = $this->Form->input('Contact.field', array(
  1904. 'error' => array(
  1905. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1906. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1907. 'maxLength' => 'login too large',
  1908. )
  1909. ));
  1910. $expected = array(
  1911. 'div' => array('class' => 'input text error'),
  1912. 'label' => array('for' => 'ContactField'),
  1913. 'Field',
  1914. '/label',
  1915. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1916. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1917. 'login too large',
  1918. '/span',
  1919. '/div'
  1920. );
  1921. $this->assertTags($result, $expected);
  1922. }
  1923. /**
  1924. * test input() with checkbox creation
  1925. *
  1926. * @return void
  1927. */
  1928. public function testInputCheckbox() {
  1929. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1930. $expected = array(
  1931. 'div' => array('class' => 'input checkbox'),
  1932. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1933. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1934. '/div'
  1935. );
  1936. $this->assertTags($result, $expected);
  1937. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1938. $expected = array(
  1939. 'div' => array('class' => 'input checkbox'),
  1940. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1941. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1942. '/div'
  1943. );
  1944. $this->assertTags($result, $expected);
  1945. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1946. $expected = array(
  1947. 'div' => array('class' => 'input checkbox'),
  1948. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1949. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1950. '/div'
  1951. );
  1952. $this->assertTags($result, $expected);
  1953. }
  1954. /**
  1955. * test form->input() with time types.
  1956. *
  1957. */
  1958. public function testInputTime() {
  1959. extract($this->dateRegex);
  1960. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1961. $result = explode(':', $result);
  1962. $this->assertRegExp('/option value="23"/', $result[0]);
  1963. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1964. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1965. $result = explode(':', $result);
  1966. $this->assertRegExp('/option value="23"/', $result[0]);
  1967. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1968. $result = $this->Form->input('Model.field', array(
  1969. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  1970. ));
  1971. $result = explode(':', $result);
  1972. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1973. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1974. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1975. $result = $this->Form->input('Model.field', array(
  1976. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  1977. ));
  1978. $result = explode(':', $result);
  1979. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1980. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1981. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1982. $result = $this->Form->input('prueba', array(
  1983. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  1984. 'maxYear' => date('Y') + 1 ,'interval' => 15
  1985. ));
  1986. $result = explode(':', $result);
  1987. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1988. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1989. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1990. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  1991. $result = $this->Form->input('Random.start_time', array(
  1992. 'type' => 'time',
  1993. 'selected' => '18:15'
  1994. ));
  1995. $this->assertRegExp('#<option value="06"[^>]*>6</option>#', $result);
  1996. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result);
  1997. $this->assertRegExp('#<option value="pm"[^>]*>pm</option>#', $result);
  1998. }
  1999. /**
  2000. * test form->input() with datetime, date and time types
  2001. *
  2002. * @return void
  2003. */
  2004. public function testInputDatetime() {
  2005. extract($this->dateRegex);
  2006. $result = $this->Form->input('prueba', array(
  2007. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2008. 'maxYear' => date('Y') + 1, 'interval' => 15
  2009. ));
  2010. $result = explode(':', $result);
  2011. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2012. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2013. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2014. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2015. //related to ticket #5013
  2016. $result = $this->Form->input('Contact.date', array(
  2017. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2018. ));
  2019. $this->assertRegExp('/class="customClass"/', $result);
  2020. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2021. $result = $this->Form->input('Contact.date', array(
  2022. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2023. ));
  2024. $this->assertRegExp('/id="customIdDay"/', $result);
  2025. $this->assertRegExp('/id="customIdMonth"/', $result);
  2026. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2027. $result = $this->Form->input('Model.field', array(
  2028. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2029. ));
  2030. $this->assertRegExp('/id="customIDDay"/', $result);
  2031. $this->assertRegExp('/id="customIDHour"/', $result);
  2032. $result = explode('</select><select', $result);
  2033. $result = explode(':', $result[1]);
  2034. $this->assertRegExp('/option value="23"/', $result[0]);
  2035. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2036. $result = $this->Form->input('Model.field', array(
  2037. 'type' => 'datetime', 'timeFormat' => 12
  2038. ));
  2039. $result = explode('</select><select', $result);
  2040. $result = explode(':', $result[1]);
  2041. $this->assertRegExp('/option value="12"/', $result[0]);
  2042. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2043. $this->Form->request->data = array('Contact' => array('created' => null));
  2044. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2045. $expected = array(
  2046. 'div' => array('class' => 'input date'),
  2047. 'label' => array('for' => 'ContactCreatedMonth'),
  2048. 'Created',
  2049. '/label',
  2050. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2051. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2052. $monthsRegex,
  2053. '/select', '-',
  2054. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2055. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2056. $daysRegex,
  2057. '/select', '-',
  2058. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2059. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2060. $yearsRegex,
  2061. '/select',
  2062. '/div'
  2063. );
  2064. $this->assertTags($result, $expected);
  2065. $this->Form->request->data = array('Contact' => array('created' => null));
  2066. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2067. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2068. $this->Form->request->data = array('Contact' => array('created' => null));
  2069. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2070. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2071. $result = $this->Form->input('Contact.created', array(
  2072. 'type' => 'date',
  2073. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2074. 'timeFormat' => 'NONE'
  2075. ));
  2076. $this->assertRegExp('/for\="created-month"/', $result);
  2077. }
  2078. /**
  2079. * Test generating checkboxes in a loop.
  2080. *
  2081. * @return void
  2082. */
  2083. public function testInputCheckboxesInLoop() {
  2084. for ($i = 1; $i < 5; $i++) {
  2085. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2086. $expected = array(
  2087. 'div' => array('class' => 'input checkbox'),
  2088. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2089. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2090. 'label' => array('for' => "Contact{$i}Email"),
  2091. 'Email',
  2092. '/label',
  2093. '/div'
  2094. );
  2095. $this->assertTags($result, $expected);
  2096. }
  2097. }
  2098. /**
  2099. * test input name with leading integer, ensure attributes are generated correctly.
  2100. *
  2101. * @return void
  2102. */
  2103. public function testInputWithLeadingInteger() {
  2104. $result = $this->Form->text('0.Node.title');
  2105. $expected = array(
  2106. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2107. );
  2108. $this->assertTags($result, $expected);
  2109. }
  2110. /**
  2111. * test form->input() with select type inputs.
  2112. *
  2113. * @return void
  2114. */
  2115. public function testInputSelectType() {
  2116. $result = $this->Form->input('email', array(
  2117. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2118. );
  2119. $expected = array(
  2120. 'div' => array('class' => 'input select'),
  2121. 'label' => array('for' => 'email'),
  2122. 'Email',
  2123. '/label',
  2124. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2125. array('option' => array('value' => '')),
  2126. '/option',
  2127. array('option' => array('value' => 'è')),
  2128. 'Firést',
  2129. '/option',
  2130. array('option' => array('value' => 'é')),
  2131. 'Secoènd',
  2132. '/option',
  2133. '/select',
  2134. '/div'
  2135. );
  2136. $this->assertTags($result, $expected);
  2137. $result = $this->Form->input('email', array(
  2138. 'options' => array('First', 'Second'), 'empty' => true)
  2139. );
  2140. $expected = array(
  2141. 'div' => array('class' => 'input select'),
  2142. 'label' => array('for' => 'email'),
  2143. 'Email',
  2144. '/label',
  2145. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2146. array('option' => array('value' => '')),
  2147. '/option',
  2148. array('option' => array('value' => '0')),
  2149. 'First',
  2150. '/option',
  2151. array('option' => array('value' => '1')),
  2152. 'Second',
  2153. '/option',
  2154. '/select',
  2155. '/div'
  2156. );
  2157. $this->assertTags($result, $expected);
  2158. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2159. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2160. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2161. $expected = array(
  2162. 'div' => array('class' => 'input select'),
  2163. 'label' => array('for' => 'ModelUserId'),
  2164. 'User',
  2165. '/label',
  2166. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2167. array('option' => array('value' => '')),
  2168. '/option',
  2169. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2170. 'good',
  2171. '/option',
  2172. array('option' => array('value' => 'other')),
  2173. 'bad',
  2174. '/option',
  2175. '/select',
  2176. '/div'
  2177. );
  2178. $this->assertTags($result, $expected);
  2179. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2180. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2181. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2182. $expected = array(
  2183. 'div' => array('class' => 'input select'),
  2184. 'label' => array('for' => 'ThingUserId'),
  2185. 'User',
  2186. '/label',
  2187. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2188. array('option' => array('value' => '')),
  2189. 'Some Empty',
  2190. '/option',
  2191. array('option' => array('value' => 'value')),
  2192. 'good',
  2193. '/option',
  2194. array('option' => array('value' => 'other')),
  2195. 'bad',
  2196. '/option',
  2197. '/select',
  2198. '/div'
  2199. );
  2200. $this->assertTags($result, $expected);
  2201. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2202. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2203. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2204. $expected = array(
  2205. 'div' => array('class' => 'input select'),
  2206. 'label' => array('for' => 'ThingUserId'),
  2207. 'User',
  2208. '/label',
  2209. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2210. array('option' => array('value' => '')),
  2211. 'Some Empty',
  2212. '/option',
  2213. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2214. 'good',
  2215. '/option',
  2216. array('option' => array('value' => 'other')),
  2217. 'bad',
  2218. '/option',
  2219. '/select',
  2220. '/div'
  2221. );
  2222. $this->assertTags($result, $expected);
  2223. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2224. $this->Form->request->data = array('User' => array('User' => array('value')));
  2225. $result = $this->Form->input('User.User', array('empty' => true));
  2226. $expected = array(
  2227. 'div' => array('class' => 'input select'),
  2228. 'label' => array('for' => 'UserUser'),
  2229. 'User',
  2230. '/label',
  2231. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2232. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2233. array('option' => array('value' => '')),
  2234. '/option',
  2235. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2236. 'good',
  2237. '/option',
  2238. array('option' => array('value' => 'other')),
  2239. 'bad',
  2240. '/option',
  2241. '/select',
  2242. '/div'
  2243. );
  2244. $this->assertTags($result, $expected);
  2245. $this->Form->data = array();
  2246. $result = $this->Form->input('Publisher.id', array(
  2247. 'label' => 'Publisher',
  2248. 'type' => 'select',
  2249. 'multiple' => 'checkbox',
  2250. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2251. ));
  2252. $expected = array(
  2253. array('div' => array('class' => 'input select')),
  2254. array('label' => array('for' => 'PublisherId')),
  2255. 'Publisher',
  2256. '/label',
  2257. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2258. array('div' => array('class' => 'checkbox')),
  2259. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2260. array('label' => array('for' => 'PublisherIdValue1')),
  2261. 'Label 1',
  2262. '/label',
  2263. '/div',
  2264. array('div' => array('class' => 'checkbox')),
  2265. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2266. array('label' => array('for' => 'PublisherIdValue2')),
  2267. 'Label 2',
  2268. '/label',
  2269. '/div',
  2270. '/div'
  2271. );
  2272. $this->assertTags($result, $expected);
  2273. }
  2274. /**
  2275. * test that input() and a non standard primary key makes a hidden input by default.
  2276. *
  2277. * @return void
  2278. */
  2279. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2280. $this->Form->create('User');
  2281. $this->Form->fieldset = array(
  2282. 'User' => array(
  2283. 'fields' => array(
  2284. 'model_id' => array('type' => 'integer')
  2285. ),
  2286. 'validates' => array(),
  2287. 'key' => 'model_id'
  2288. )
  2289. );
  2290. $result = $this->Form->input('model_id');
  2291. $expected = array(
  2292. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2293. );
  2294. $this->assertTags($result, $expected);
  2295. }
  2296. /**
  2297. * test that overriding the magic select type widget is possible
  2298. *
  2299. * @return void
  2300. */
  2301. public function testInputOverridingMagicSelectType() {
  2302. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2303. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2304. $expected = array(
  2305. 'div' => array('class' => 'input text'),
  2306. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2307. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2308. '/div'
  2309. );
  2310. $this->assertTags($result, $expected);
  2311. //Check that magic types still work for plural/singular vars
  2312. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2313. $result = $this->Form->input('Model.type');
  2314. $expected = array(
  2315. 'div' => array('class' => 'input select'),
  2316. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2317. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2318. array('option' => array('value' => 'value')), 'good', '/option',
  2319. array('option' => array('value' => 'other')), 'bad', '/option',
  2320. '/select',
  2321. '/div'
  2322. );
  2323. $this->assertTags($result, $expected);
  2324. }
  2325. /**
  2326. * Test that magic input() selects can easily be converted into radio types without error.
  2327. *
  2328. * @return void
  2329. */
  2330. public function testInputMagicSelectChangeToRadio() {
  2331. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2332. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2333. $this->assertRegExp('/input type="radio"/', $result);
  2334. }
  2335. /**
  2336. * fields with the same name as the model should work.
  2337. *
  2338. * @return void
  2339. */
  2340. public function testInputWithMatchingFieldAndModelName() {
  2341. $this->Form->create('User');
  2342. $this->Form->fieldset = array(
  2343. 'User' => array(
  2344. 'fields' => array(
  2345. 'User' => array('type' => 'text')
  2346. ),
  2347. 'validates' => array(),
  2348. 'key' => 'id'
  2349. )
  2350. );
  2351. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2352. $result = $this->Form->input('User', array('type' => 'text'));
  2353. $expected = array(
  2354. 'div' => array('class' => 'input text'),
  2355. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2356. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2357. '/div'
  2358. );
  2359. $this->assertTags($result, $expected);
  2360. }
  2361. /**
  2362. * testFormInputs method
  2363. *
  2364. * test correct results from form::inputs().
  2365. *
  2366. * @return void
  2367. */
  2368. public function testFormInputs() {
  2369. $this->Form->create('Contact');
  2370. $result = $this->Form->inputs('The Legend');
  2371. $expected = array(
  2372. '<fieldset',
  2373. '<legend',
  2374. 'The Legend',
  2375. '/legend',
  2376. '*/fieldset',
  2377. );
  2378. $this->assertTags($result, $expected);
  2379. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2380. $expected = array(
  2381. 'fieldset' => array('class' => 'classy-stuff'),
  2382. '<legend',
  2383. 'Field of Dreams',
  2384. '/legend',
  2385. '*/fieldset'
  2386. );
  2387. $this->assertTags($result, $expected);
  2388. $this->Form->create('Contact');
  2389. $this->Form->request['prefix'] = 'admin';
  2390. $this->Form->request['action'] = 'admin_edit';
  2391. $result = $this->Form->inputs();
  2392. $expected = array(
  2393. '<fieldset',
  2394. '<legend',
  2395. 'Edit Contact',
  2396. '/legend',
  2397. '*/fieldset',
  2398. );
  2399. $this->assertTags($result, $expected);
  2400. $this->Form->create('Contact');
  2401. $result = $this->Form->inputs(false);
  2402. $expected = array(
  2403. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2404. array('div' => array('class' => 'input text')),
  2405. '*/div',
  2406. array('div' => array('class' => 'input text')),
  2407. '*/div',
  2408. array('div' => array('class' => 'input text')),
  2409. '*/div',
  2410. array('div' => array('class' => 'input password')),
  2411. '*/div',
  2412. array('div' => array('class' => 'input date')),
  2413. '*/div',
  2414. array('div' => array('class' => 'input date')),
  2415. '*/div',
  2416. array('div' => array('class' => 'input datetime')),
  2417. '*/div',
  2418. array('div' => array('class' => 'input number')),
  2419. '*/div',
  2420. array('div' => array('class' => 'input select')),
  2421. '*/div',
  2422. );
  2423. $this->assertTags($result, $expected);
  2424. $this->Form->create('Contact');
  2425. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2426. $expected = array(
  2427. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2428. array('div' => array('class' => 'input text')),
  2429. '*/div',
  2430. array('div' => array('class' => 'input text')),
  2431. '*/div',
  2432. array('div' => array('class' => 'input text')),
  2433. '*/div',
  2434. array('div' => array('class' => 'input password')),
  2435. '*/div',
  2436. array('div' => array('class' => 'input date')),
  2437. '*/div',
  2438. array('div' => array('class' => 'input date')),
  2439. '*/div',
  2440. array('div' => array('class' => 'input datetime')),
  2441. '*/div',
  2442. array('div' => array('class' => 'input number')),
  2443. '*/div',
  2444. array('div' => array('class' => 'input select')),
  2445. '*/div',
  2446. );
  2447. $this->assertTags($result, $expected);
  2448. $this->Form->create('Contact');
  2449. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2450. $expected = array(
  2451. 'fieldset' => array(),
  2452. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2453. array('div' => array('class' => 'input text')),
  2454. '*/div',
  2455. array('div' => array('class' => 'input text')),
  2456. '*/div',
  2457. array('div' => array('class' => 'input text')),
  2458. '*/div',
  2459. array('div' => array('class' => 'input password')),
  2460. '*/div',
  2461. array('div' => array('class' => 'input date')),
  2462. '*/div',
  2463. array('div' => array('class' => 'input date')),
  2464. '*/div',
  2465. array('div' => array('class' => 'input datetime')),
  2466. '*/div',
  2467. array('div' => array('class' => 'input number')),
  2468. '*/div',
  2469. array('div' => array('class' => 'input select')),
  2470. '*/div',
  2471. '/fieldset'
  2472. );
  2473. $this->assertTags($result, $expected);
  2474. $this->Form->create('Contact');
  2475. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2476. $expected = array(
  2477. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2478. array('div' => array('class' => 'input text')),
  2479. '*/div',
  2480. array('div' => array('class' => 'input text')),
  2481. '*/div',
  2482. array('div' => array('class' => 'input text')),
  2483. '*/div',
  2484. array('div' => array('class' => 'input password')),
  2485. '*/div',
  2486. array('div' => array('class' => 'input date')),
  2487. '*/div',
  2488. array('div' => array('class' => 'input date')),
  2489. '*/div',
  2490. array('div' => array('class' => 'input datetime')),
  2491. '*/div',
  2492. array('div' => array('class' => 'input number')),
  2493. '*/div',
  2494. array('div' => array('class' => 'input select')),
  2495. '*/div',
  2496. );
  2497. $this->assertTags($result, $expected);
  2498. $this->Form->create('Contact');
  2499. $result = $this->Form->inputs('Hello');
  2500. $expected = array(
  2501. 'fieldset' => array(),
  2502. 'legend' => array(),
  2503. 'Hello',
  2504. '/legend',
  2505. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2506. array('div' => array('class' => 'input text')),
  2507. '*/div',
  2508. array('div' => array('class' => 'input text')),
  2509. '*/div',
  2510. array('div' => array('class' => 'input text')),
  2511. '*/div',
  2512. array('div' => array('class' => 'input password')),
  2513. '*/div',
  2514. array('div' => array('class' => 'input date')),
  2515. '*/div',
  2516. array('div' => array('class' => 'input date')),
  2517. '*/div',
  2518. array('div' => array('class' => 'input datetime')),
  2519. '*/div',
  2520. array('div' => array('class' => 'input number')),
  2521. '*/div',
  2522. array('div' => array('class' => 'input select')),
  2523. '*/div',
  2524. '/fieldset'
  2525. );
  2526. $this->assertTags($result, $expected);
  2527. $this->Form->create('Contact');
  2528. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2529. $expected = array(
  2530. 'fieldset' => array(),
  2531. 'legend' => array(),
  2532. 'Hello',
  2533. '/legend',
  2534. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2535. array('div' => array('class' => 'input text')),
  2536. '*/div',
  2537. array('div' => array('class' => 'input text')),
  2538. '*/div',
  2539. array('div' => array('class' => 'input text')),
  2540. '*/div',
  2541. array('div' => array('class' => 'input password')),
  2542. '*/div',
  2543. array('div' => array('class' => 'input date')),
  2544. '*/div',
  2545. array('div' => array('class' => 'input date')),
  2546. '*/div',
  2547. array('div' => array('class' => 'input datetime')),
  2548. '*/div',
  2549. array('div' => array('class' => 'input number')),
  2550. '*/div',
  2551. array('div' => array('class' => 'input select')),
  2552. '*/div',
  2553. '/fieldset'
  2554. );
  2555. $this->assertTags($result, $expected);
  2556. }
  2557. /**
  2558. * testSelectAsCheckbox method
  2559. *
  2560. * test multi-select widget with checkbox formatting.
  2561. *
  2562. * @return void
  2563. */
  2564. public function testSelectAsCheckbox() {
  2565. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2566. $expected = array(
  2567. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2568. array('div' => array('class' => 'checkbox')),
  2569. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2570. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2571. 'first',
  2572. '/label',
  2573. '/div',
  2574. array('div' => array('class' => 'checkbox')),
  2575. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2576. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2577. 'second',
  2578. '/label',
  2579. '/div',
  2580. array('div' => array('class' => 'checkbox')),
  2581. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2582. array('label' => array('for' => 'ModelMultiField2')),
  2583. 'third',
  2584. '/label',
  2585. '/div',
  2586. );
  2587. $this->assertTags($result, $expected);
  2588. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2589. $expected = array(
  2590. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2591. array('div' => array('class' => 'checkbox')),
  2592. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2593. array('label' => array('for' => 'ModelMultiField12')),
  2594. 'half',
  2595. '/label',
  2596. '/div',
  2597. );
  2598. $this->assertTags($result, $expected);
  2599. }
  2600. /**
  2601. * testLabel method
  2602. *
  2603. * test label generation.
  2604. *
  2605. * @return void
  2606. */
  2607. public function testLabel() {
  2608. $this->Form->text('Person.name');
  2609. $result = $this->Form->label();
  2610. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2611. $this->Form->text('Person.name');
  2612. $result = $this->Form->label();
  2613. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2614. $result = $this->Form->label('Person.first_name');
  2615. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2616. $result = $this->Form->label('Person.first_name', 'Your first name');
  2617. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2618. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2619. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2620. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2621. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2622. $result = $this->Form->label('Person.first_name', '');
  2623. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2624. $result = $this->Form->label('Person.2.name', '');
  2625. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2626. }
  2627. /**
  2628. * testTextbox method
  2629. *
  2630. * test textbox element generation
  2631. *
  2632. * @return void
  2633. */
  2634. public function testTextbox() {
  2635. $result = $this->Form->text('Model.field');
  2636. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2637. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2638. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2639. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2640. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2641. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2642. $result = $this->Form->text('Model.text');
  2643. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2644. $Contact = ClassRegistry::getObject('Contact');
  2645. $Contact->validationErrors['text'] = array(true);
  2646. $this->Form->request->data['Contact']['text'] = 'test';
  2647. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2648. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2649. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2650. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2651. $expected = array(
  2652. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2653. );
  2654. $this->assertTags($result, $expected);
  2655. }
  2656. /**
  2657. * testDefaultValue method
  2658. *
  2659. * Test default value setting
  2660. *
  2661. * @return void
  2662. */
  2663. public function testDefaultValue() {
  2664. $this->Form->request->data['Model']['field'] = 'test';
  2665. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2666. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  2667. unset($this->Form->request->data['Model']['field']);
  2668. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2669. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  2670. }
  2671. /**
  2672. * testCheckboxDefaultValue method
  2673. *
  2674. * Test default value setting on checkbox() method
  2675. *
  2676. * @return void
  2677. */
  2678. public function testCheckboxDefaultValue() {
  2679. $this->Form->request->data['Model']['field'] = false;
  2680. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2681. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2682. unset($this->Form->request->data['Model']['field']);
  2683. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2684. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2685. $this->Form->request->data['Model']['field'] = true;
  2686. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2687. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2688. unset($this->Form->request->data['Model']['field']);
  2689. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2690. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2691. }
  2692. /**
  2693. * testError method
  2694. *
  2695. * Test field error generation
  2696. *
  2697. * @return void
  2698. */
  2699. public function testError() {
  2700. $Contact = ClassRegistry::getObject('Contact');
  2701. $Contact->validationErrors['field'] = array(1);
  2702. $result = $this->Form->error('Contact.field');
  2703. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  2704. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2705. $this->assertEquals($result, 'Error in field Field');
  2706. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2707. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2708. $this->assertEquals($result, 'This field contains invalid input');
  2709. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2710. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  2711. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  2712. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  2713. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  2714. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  2715. $this->assertEquals($result, '&lt;strong&gt;Badness!&lt;/strong&gt;');
  2716. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  2717. $this->assertEquals($result, '&lt;strong&gt;Badness!&lt;/strong&gt;');
  2718. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  2719. $this->assertEquals($result, '<strong>Badness!</strong>');
  2720. $Contact->validationErrors['field'] = array("email");
  2721. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  2722. $expected = array(
  2723. 'div' => array('class' => 'field-error'),
  2724. 'No good!',
  2725. '/div'
  2726. );
  2727. $this->assertTags($result, $expected);
  2728. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  2729. $result = $this->Form->error('Contact.field', array(
  2730. 'notEmpty' => 'Cannot be empty',
  2731. 'email' => 'No good!'
  2732. ));
  2733. $expected = array(
  2734. 'div' => array('class' => 'error-message'),
  2735. 'ul' => array(),
  2736. '<li', 'Cannot be empty', '/li',
  2737. '<li', 'No good!', '/li',
  2738. '<li', 'Something else', '/li',
  2739. '/ul',
  2740. '/div'
  2741. );
  2742. $this->assertTags($result, $expected);
  2743. /** Testing error messages list options **/
  2744. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  2745. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  2746. $expected = array(
  2747. 'div' => array('class' => 'error-message'),
  2748. 'ol' => array(),
  2749. '<li', 'notEmpty', '/li',
  2750. '<li', 'email', '/li',
  2751. '/ol',
  2752. '/div'
  2753. );
  2754. $this->assertTags($result, $expected);
  2755. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  2756. $expected = array(
  2757. 'div' => array('class' => 'error-message'),
  2758. 'ol' => array(),
  2759. '<li', 'notEmpty', '/li',
  2760. '<li', 'email', '/li',
  2761. '/ol',
  2762. '/div'
  2763. );
  2764. $this->assertTags($result, $expected);
  2765. $result = $this->Form->error('Contact.field', null, array(
  2766. 'listOptions' => array(
  2767. 'class' => 'ul-class',
  2768. 'itemOptions' => array(
  2769. 'class' => 'li-class'
  2770. )
  2771. )
  2772. ));
  2773. $expected = array(
  2774. 'div' => array('class' => 'error-message'),
  2775. 'ul' => array('class' => 'ul-class'),
  2776. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  2777. array('li' => array('class' => 'li-class')), 'email', '/li',
  2778. '/ul',
  2779. '/div'
  2780. );
  2781. $this->assertTags($result, $expected);
  2782. }
  2783. /**
  2784. * test error options when using form->input();
  2785. *
  2786. * @return void
  2787. */
  2788. public function testInputErrorEscape() {
  2789. $this->Form->create('ValidateProfile');
  2790. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  2791. $ValidateProfile->validationErrors['city'] = array('required<br>');
  2792. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
  2793. $this->assertRegExp('/required&lt;br&gt;/', $result);
  2794. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => false))));
  2795. $this->assertRegExp('/required<br>/', $result);
  2796. }
  2797. /**
  2798. * testPassword method
  2799. *
  2800. * Test password element generation
  2801. *
  2802. * @return void
  2803. */
  2804. public function testPassword() {
  2805. $Contact = ClassRegistry::getObject('Contact');
  2806. $result = $this->Form->password('Contact.field');
  2807. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  2808. $Contact->validationErrors['passwd'] = 1;
  2809. $this->Form->request->data['Contact']['passwd'] = 'test';
  2810. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  2811. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2812. }
  2813. /**
  2814. * testRadio method
  2815. *
  2816. * Test radio element set generation
  2817. *
  2818. * @return void
  2819. */
  2820. public function testRadio() {
  2821. $result = $this->Form->radio('Model.field', array('option A'));
  2822. $expected = array(
  2823. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2824. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2825. 'label' => array('for' => 'ModelField0'),
  2826. 'option A',
  2827. '/label'
  2828. );
  2829. $this->assertTags($result, $expected);
  2830. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  2831. $expected = array(
  2832. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2833. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  2834. 'label' => array('for' => 'ModelField12'),
  2835. 'half',
  2836. '/label'
  2837. );
  2838. $this->assertTags($result, $expected);
  2839. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  2840. $expected = array(
  2841. 'fieldset' => array(),
  2842. 'legend' => array(),
  2843. 'Field',
  2844. '/legend',
  2845. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2846. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2847. array('label' => array('for' => 'ModelField0')),
  2848. 'option A',
  2849. '/label',
  2850. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2851. array('label' => array('for' => 'ModelField1')),
  2852. 'option B',
  2853. '/label',
  2854. '/fieldset'
  2855. );
  2856. $this->assertTags($result, $expected);
  2857. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  2858. $expected = array(
  2859. 'fieldset' => array(),
  2860. 'legend' => array(),
  2861. 'Field',
  2862. '/legend',
  2863. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2864. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2865. array('label' => array('for' => 'ModelField0')),
  2866. 'option A',
  2867. '/label',
  2868. 'br' => array(),
  2869. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2870. array('label' => array('for' => 'ModelField1')),
  2871. 'option B',
  2872. '/label',
  2873. '/fieldset'
  2874. );
  2875. $this->assertTags($result, $expected);
  2876. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  2877. $expected = array(
  2878. 'fieldset' => array(),
  2879. 'legend' => array(),
  2880. 'Field',
  2881. '/legend',
  2882. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'checked' => 'checked')),
  2883. array('label' => array('for' => 'ModelField1')),
  2884. 'Yes',
  2885. '/label',
  2886. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2887. array('label' => array('for' => 'ModelField0')),
  2888. 'No',
  2889. '/label',
  2890. '/fieldset'
  2891. );
  2892. $this->assertTags($result, $expected);
  2893. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  2894. $expected = array(
  2895. 'fieldset' => array(),
  2896. 'legend' => array(),
  2897. 'Field',
  2898. '/legend',
  2899. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2900. array('label' => array('for' => 'ModelField1')),
  2901. 'Yes',
  2902. '/label',
  2903. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  2904. array('label' => array('for' => 'ModelField0')),
  2905. 'No',
  2906. '/label',
  2907. '/fieldset'
  2908. );
  2909. $this->assertTags($result, $expected);
  2910. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  2911. $expected = array(
  2912. 'fieldset' => array(),
  2913. 'legend' => array(),
  2914. 'Field',
  2915. '/legend',
  2916. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2917. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2918. array('label' => array('for' => 'ModelField1')),
  2919. 'Yes',
  2920. '/label',
  2921. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2922. array('label' => array('for' => 'ModelField0')),
  2923. 'No',
  2924. '/label',
  2925. '/fieldset'
  2926. );
  2927. $this->assertTags($result, $expected);
  2928. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  2929. $expected = array(
  2930. 'fieldset' => array(),
  2931. 'legend' => array(),
  2932. 'Field',
  2933. '/legend',
  2934. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2935. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2936. array('label' => array('for' => 'ModelField1')),
  2937. 'Yes',
  2938. '/label',
  2939. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2940. array('label' => array('for' => 'ModelField0')),
  2941. 'No',
  2942. '/label',
  2943. '/fieldset'
  2944. );
  2945. $this->assertTags($result, $expected);
  2946. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2947. $expected = array(
  2948. 'div' => array('class' => 'input radio'),
  2949. 'fieldset' => array(),
  2950. 'legend' => array(),
  2951. 'Legend title',
  2952. '/legend',
  2953. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2954. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2955. array('label' => array('for' => 'NewsletterSubscribe0')),
  2956. 'Unsubscribe',
  2957. '/label',
  2958. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2959. array('label' => array('for' => 'NewsletterSubscribe1')),
  2960. 'Subscribe',
  2961. '/label',
  2962. '/fieldset',
  2963. '/div'
  2964. );
  2965. $this->assertTags($result, $expected);
  2966. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2967. $expected = array(
  2968. 'div' => array('class' => 'input radio'),
  2969. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2970. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2971. array('label' => array('for' => 'NewsletterSubscribe0')),
  2972. 'Unsubscribe',
  2973. '/label',
  2974. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2975. array('label' => array('for' => 'NewsletterSubscribe1')),
  2976. 'Subscribe',
  2977. '/label',
  2978. '/div'
  2979. );
  2980. $this->assertTags($result, $expected);
  2981. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2982. $expected = array(
  2983. 'div' => array('class' => 'input radio'),
  2984. 'fieldset' => array(),
  2985. 'legend' => array(),
  2986. 'Legend title',
  2987. '/legend',
  2988. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2989. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2990. 'Unsubscribe',
  2991. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2992. 'Subscribe',
  2993. '/fieldset',
  2994. '/div'
  2995. );
  2996. $this->assertTags($result, $expected);
  2997. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2998. $expected = array(
  2999. 'div' => array('class' => 'input radio'),
  3000. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3001. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3002. 'Unsubscribe',
  3003. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3004. 'Subscribe',
  3005. '/div'
  3006. );
  3007. $this->assertTags($result, $expected);
  3008. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'value' => '1', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3009. $expected = array(
  3010. 'div' => array('class' => 'input radio'),
  3011. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3012. 'Unsubscribe',
  3013. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1', 'checked' => 'checked')),
  3014. 'Subscribe',
  3015. '/div'
  3016. );
  3017. $this->assertTags($result, $expected);
  3018. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3019. $expected = array(
  3020. 'fieldset' => array(),
  3021. 'legend' => array(),
  3022. 'Gender',
  3023. '/legend',
  3024. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3025. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3026. array('label' => array('for' => 'EmployeeGenderMale')),
  3027. 'Male',
  3028. '/label',
  3029. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3030. array('label' => array('for' => 'EmployeeGenderFemale')),
  3031. 'Female',
  3032. '/label',
  3033. '/fieldset',
  3034. );
  3035. $this->assertTags($result, $expected);
  3036. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3037. $expected = array(
  3038. 'fieldset' => array(),
  3039. 'legend' => array(),
  3040. 'Gender',
  3041. '/legend',
  3042. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3043. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3044. array('label' => array('for' => 'OfficerGenderMale')),
  3045. 'Male',
  3046. '/label',
  3047. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3048. array('label' => array('for' => 'OfficerGenderFemale')),
  3049. 'Female',
  3050. '/label',
  3051. '/fieldset',
  3052. );
  3053. $this->assertTags($result, $expected);
  3054. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3055. $expected = array(
  3056. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3057. array('input' => array('type' => 'radio', 'name' => 'data[Contact][1][imrequired]', 'value' => '0', 'id' => 'Contact1Imrequired0')),
  3058. 'label' => array('for' => 'Contact1Imrequired0'),
  3059. 'option A',
  3060. '/label'
  3061. );
  3062. $this->assertTags($result, $expected);
  3063. $result = $this->Form->radio('Model.1.field', array('option A'));
  3064. $expected = array(
  3065. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3066. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3067. 'label' => array('for' => 'Model1Field0'),
  3068. 'option A',
  3069. '/label'
  3070. );
  3071. $this->assertTags($result, $expected);
  3072. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3073. $expected = array(
  3074. 'fieldset' => array(),
  3075. 'legend' => array(),
  3076. 'Field',
  3077. '/legend',
  3078. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3079. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3080. array('label' => array('for' => 'ModelField0')),
  3081. 'option A',
  3082. '/label',
  3083. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3084. array('label' => array('for' => 'ModelField1')),
  3085. 'option B',
  3086. '/label',
  3087. '/fieldset'
  3088. );
  3089. $this->assertTags($result, $expected);
  3090. $result = $this->Form->radio(
  3091. 'Model.field',
  3092. array('option A', 'option B'),
  3093. array('between' => 'I am between')
  3094. );
  3095. $expected = array(
  3096. 'fieldset' => array(),
  3097. 'legend' => array(),
  3098. 'Field',
  3099. '/legend',
  3100. 'I am between',
  3101. 'input' => array(
  3102. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3103. 'value' => '', 'id' => 'ModelField_'
  3104. ),
  3105. array('input' => array(
  3106. 'type' => 'radio', 'name' => 'data[Model][field]',
  3107. 'value' => '0', 'id' => 'ModelField0'
  3108. )),
  3109. array('label' => array('for' => 'ModelField0')),
  3110. 'option A',
  3111. '/label',
  3112. array('input' => array(
  3113. 'type' => 'radio', 'name' => 'data[Model][field]',
  3114. 'value' => '1', 'id' => 'ModelField1'
  3115. )),
  3116. array('label' => array('for' => 'ModelField1')),
  3117. 'option B',
  3118. '/label',
  3119. '/fieldset'
  3120. );
  3121. $this->assertTags($result, $expected);
  3122. }
  3123. /**
  3124. * test disabled radio options
  3125. *
  3126. * @return void
  3127. */
  3128. public function testRadioDisabled() {
  3129. $result = $this->Form->radio(
  3130. 'Model.field',
  3131. array('option A', 'option B'),
  3132. array('disabled' => array('option A'), 'value' => 'option A')
  3133. );
  3134. $expected = array(
  3135. 'fieldset' => array(),
  3136. 'legend' => array(),
  3137. 'Field',
  3138. '/legend',
  3139. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3140. array('label' => array('for' => 'ModelField0')),
  3141. 'option A',
  3142. '/label',
  3143. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3144. array('label' => array('for' => 'ModelField1')),
  3145. 'option B',
  3146. '/label',
  3147. '/fieldset'
  3148. );
  3149. $this->assertTags($result, $expected);
  3150. $result = $this->Form->radio(
  3151. 'Model.field',
  3152. array('option A', 'option B'),
  3153. array('disabled' => true, 'value' => 'option A')
  3154. );
  3155. $expected = array(
  3156. 'fieldset' => array(),
  3157. 'legend' => array(),
  3158. 'Field',
  3159. '/legend',
  3160. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3161. array('label' => array('for' => 'ModelField0')),
  3162. 'option A',
  3163. '/label',
  3164. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3165. array('label' => array('for' => 'ModelField1')),
  3166. 'option B',
  3167. '/label',
  3168. '/fieldset'
  3169. );
  3170. $this->assertTags($result, $expected);
  3171. $result = $this->Form->radio(
  3172. 'Model.field',
  3173. array('option A', 'option B'),
  3174. array('disabled' => 'disabled', 'value' => 'option A')
  3175. );
  3176. $expected = array(
  3177. 'fieldset' => array(),
  3178. 'legend' => array(),
  3179. 'Field',
  3180. '/legend',
  3181. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3182. array('label' => array('for' => 'ModelField0')),
  3183. 'option A',
  3184. '/label',
  3185. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3186. array('label' => array('for' => 'ModelField1')),
  3187. 'option B',
  3188. '/label',
  3189. '/fieldset'
  3190. );
  3191. $this->assertTags($result, $expected);
  3192. }
  3193. /**
  3194. * test disabling the hidden input for radio buttons
  3195. *
  3196. * @return void
  3197. */
  3198. public function testRadioHiddenInputDisabling() {
  3199. $result = $this->Form->input('Model.1.field', array(
  3200. 'type' => 'radio',
  3201. 'options' => array('option A'),
  3202. 'hiddenField' => false
  3203. )
  3204. );
  3205. $expected = array(
  3206. 'div' => array('class' => 'input radio'),
  3207. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3208. 'label' => array('for' => 'Model1Field0'),
  3209. 'option A',
  3210. '/label',
  3211. '/div'
  3212. );
  3213. $this->assertTags($result, $expected);
  3214. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3215. $expected = array(
  3216. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3217. 'label' => array('for' => 'Model1Field0'),
  3218. 'option A',
  3219. '/label'
  3220. );
  3221. $this->assertTags($result, $expected);
  3222. }
  3223. /**
  3224. * testSelect method
  3225. *
  3226. * Test select element generation.
  3227. *
  3228. * @return void
  3229. */
  3230. public function testSelect() {
  3231. $result = $this->Form->select('Model.field', array());
  3232. $expected = array(
  3233. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3234. array('option' => array('value' => '')),
  3235. '/option',
  3236. '/select'
  3237. );
  3238. $this->assertTags($result, $expected);
  3239. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3240. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3241. $expected = array(
  3242. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3243. array('option' => array('value' => '')),
  3244. '/option',
  3245. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3246. 'good',
  3247. '/option',
  3248. array('option' => array('value' => 'other')),
  3249. 'bad',
  3250. '/option',
  3251. '/select'
  3252. );
  3253. $this->assertTags($result, $expected);
  3254. $this->Form->request->data = array();
  3255. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3256. $expected = array(
  3257. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3258. array('option' => array('value' => '')),
  3259. '/option',
  3260. array('option' => array('value' => 'value')),
  3261. 'good',
  3262. '/option',
  3263. array('option' => array('value' => 'other')),
  3264. 'bad',
  3265. '/option',
  3266. '/select'
  3267. );
  3268. $this->assertTags($result, $expected);
  3269. $result = $this->Form->select(
  3270. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3271. array('empty' => false)
  3272. );
  3273. $expected = array(
  3274. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3275. array('option' => array('value' => 'first')),
  3276. 'first &quot;html&quot; &lt;chars&gt;',
  3277. '/option',
  3278. array('option' => array('value' => 'second')),
  3279. 'value',
  3280. '/option',
  3281. '/select'
  3282. );
  3283. $this->assertTags($result, $expected);
  3284. $result = $this->Form->select(
  3285. 'Model.field',
  3286. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3287. array('escape' => false, 'empty' => false)
  3288. );
  3289. $expected = array(
  3290. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3291. array('option' => array('value' => 'first')),
  3292. 'first "html" <chars>',
  3293. '/option',
  3294. array('option' => array('value' => 'second')),
  3295. 'value',
  3296. '/option',
  3297. '/select'
  3298. );
  3299. $this->assertTags($result, $expected);
  3300. $options = array(
  3301. array('value' => 'first', 'name' => 'First'),
  3302. array('value' => 'first', 'name' => 'Another First'),
  3303. );
  3304. $result = $this->Form->select(
  3305. 'Model.field',
  3306. $options,
  3307. array('escape' => false, 'empty' => false)
  3308. );
  3309. $expected = array(
  3310. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3311. array('option' => array('value' => 'first')),
  3312. 'First',
  3313. '/option',
  3314. array('option' => array('value' => 'first')),
  3315. 'Another First',
  3316. '/option',
  3317. '/select'
  3318. );
  3319. $this->assertTags($result, $expected);
  3320. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3321. $result = $this->Form->select(
  3322. 'Model.contact_id',
  3323. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3324. array('escape' => false, 'empty' => 'pick something')
  3325. );
  3326. $expected = array(
  3327. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3328. array('option' => array('value' => '')), 'pick something', '/option',
  3329. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3330. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3331. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3332. '/select'
  3333. );
  3334. $this->assertTags($result, $expected);
  3335. $this->Form->request->data['Model']['field'] = 0;
  3336. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3337. $expected = array(
  3338. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3339. array('option' => array('value' => '')), '/option',
  3340. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3341. array('option' => array('value' => '1')), 'Yes', '/option',
  3342. '/select'
  3343. );
  3344. $this->assertTags($result, $expected);
  3345. }
  3346. /**
  3347. * test that select() with optiongroups listens to the escape param.
  3348. *
  3349. * @return void
  3350. */
  3351. public function testSelectOptionGroupEscaping() {
  3352. $options = array(
  3353. '>< Key' => array(
  3354. 1 => 'One',
  3355. 2 => 'Two'
  3356. )
  3357. );
  3358. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3359. $expected = array(
  3360. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3361. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3362. array('option' => array('value' => '1')), 'One', '/option',
  3363. array('option' => array('value' => '2')), 'Two', '/option',
  3364. '/optgroup',
  3365. '/select'
  3366. );
  3367. $this->assertTags($result, $expected);
  3368. $options = array(
  3369. '>< Key' => array(
  3370. 1 => 'One',
  3371. 2 => 'Two'
  3372. )
  3373. );
  3374. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3375. $expected = array(
  3376. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3377. 'optgroup' => array('label' => '>< Key'),
  3378. array('option' => array('value' => '1')), 'One', '/option',
  3379. array('option' => array('value' => '2')), 'Two', '/option',
  3380. '/optgroup',
  3381. '/select'
  3382. );
  3383. $this->assertTags($result, $expected);
  3384. }
  3385. /**
  3386. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3387. *
  3388. * @return void
  3389. */
  3390. public function testSelectWithNullAttributes() {
  3391. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3392. $expected = array(
  3393. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3394. array('option' => array('value' => '0')),
  3395. 'first',
  3396. '/option',
  3397. array('option' => array('value' => '1')),
  3398. 'second',
  3399. '/option',
  3400. '/select'
  3401. );
  3402. $this->assertTags($result, $expected);
  3403. }
  3404. /**
  3405. * testNestedSelect method
  3406. *
  3407. * test select element generation with optgroups
  3408. *
  3409. * @return void
  3410. */
  3411. public function testNestedSelect() {
  3412. $result = $this->Form->select(
  3413. 'Model.field',
  3414. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3415. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3416. )), array('empty' => false)
  3417. );
  3418. $expected = array(
  3419. 'select' => array('name' => 'data[Model][field]',
  3420. 'id' => 'ModelField'),
  3421. array('option' => array('value' => 1)),
  3422. 'One',
  3423. '/option',
  3424. array('option' => array('value' => 2)),
  3425. 'Two',
  3426. '/option',
  3427. array('optgroup' => array('label' => 'Three')),
  3428. array('option' => array('value' => 4)),
  3429. 'Four',
  3430. '/option',
  3431. array('option' => array('value' => 5)),
  3432. 'Five',
  3433. '/option',
  3434. '/optgroup',
  3435. '/select'
  3436. );
  3437. $this->assertTags($result, $expected);
  3438. $result = $this->Form->select(
  3439. 'Model.field',
  3440. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3441. array('showParents' => true, 'empty' => false)
  3442. );
  3443. $expected = array(
  3444. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3445. array('option' => array('value' => 1)),
  3446. 'One',
  3447. '/option',
  3448. array('option' => array('value' => 2)),
  3449. 'Two',
  3450. '/option',
  3451. array('optgroup' => array('label' => 'Three')),
  3452. array('option' => array('value' => 3)),
  3453. 'Three',
  3454. '/option',
  3455. array('option' => array('value' => 4)),
  3456. 'Four',
  3457. '/option',
  3458. '/optgroup',
  3459. '/select'
  3460. );
  3461. $this->assertTags($result, $expected);
  3462. }
  3463. /**
  3464. * testSelectMultiple method
  3465. *
  3466. * test generation of multiple select elements
  3467. *
  3468. * @return void
  3469. */
  3470. public function testSelectMultiple() {
  3471. $options = array('first', 'second', 'third');
  3472. $result = $this->Form->select(
  3473. 'Model.multi_field', $options, array('multiple' => true)
  3474. );
  3475. $expected = array(
  3476. 'input' => array(
  3477. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3478. ),
  3479. 'select' => array(
  3480. 'name' => 'data[Model][multi_field][]',
  3481. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3482. ),
  3483. array('option' => array('value' => '0')),
  3484. 'first',
  3485. '/option',
  3486. array('option' => array('value' => '1')),
  3487. 'second',
  3488. '/option',
  3489. array('option' => array('value' => '2')),
  3490. 'third',
  3491. '/option',
  3492. '/select'
  3493. );
  3494. $this->assertTags($result, $expected);
  3495. $result = $this->Form->select(
  3496. 'Model.multi_field', $options, array('multiple' => 'multiple')
  3497. );
  3498. $expected = array(
  3499. 'input' => array(
  3500. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3501. ),
  3502. 'select' => array(
  3503. 'name' => 'data[Model][multi_field][]',
  3504. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3505. ),
  3506. array('option' => array('value' => '0')),
  3507. 'first',
  3508. '/option',
  3509. array('option' => array('value' => '1')),
  3510. 'second',
  3511. '/option',
  3512. array('option' => array('value' => '2')),
  3513. 'third',
  3514. '/option',
  3515. '/select'
  3516. );
  3517. $this->assertTags($result, $expected);
  3518. $result = $this->Form->select(
  3519. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  3520. );
  3521. $expected = array(
  3522. 'input' => array(
  3523. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3524. ),
  3525. 'select' => array(
  3526. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  3527. 'multiple' => 'multiple'
  3528. ),
  3529. array('option' => array('value' => '0', 'selected' => 'selected')),
  3530. 'first',
  3531. '/option',
  3532. array('option' => array('value' => '1', 'selected' => 'selected')),
  3533. 'second',
  3534. '/option',
  3535. array('option' => array('value' => '2')),
  3536. 'third',
  3537. '/option',
  3538. '/select'
  3539. );
  3540. $this->assertTags($result, $expected);
  3541. $result = $this->Form->select(
  3542. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  3543. );
  3544. $expected = array(
  3545. 'select' => array(
  3546. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  3547. ),
  3548. array('option' => array('value' => '0', 'selected' => 'selected')),
  3549. 'first',
  3550. '/option',
  3551. array('option' => array('value' => '1', 'selected' => 'selected')),
  3552. 'second',
  3553. '/option',
  3554. array('option' => array('value' => '2')),
  3555. 'third',
  3556. '/option',
  3557. '/select'
  3558. );
  3559. $this->assertTags($result, $expected);
  3560. }
  3561. /**
  3562. * test generation of habtm select boxes.
  3563. *
  3564. * @return void
  3565. */
  3566. public function testHabtmSelectBox() {
  3567. $this->View->viewVars['contactTags'] = array(
  3568. 1 => 'blue',
  3569. 2 => 'red',
  3570. 3 => 'green'
  3571. );
  3572. $this->Form->request->data = array(
  3573. 'Contact' => array(),
  3574. 'ContactTag' => array(
  3575. array(
  3576. 'id' => 1,
  3577. 'name' => 'blue'
  3578. ),
  3579. array(
  3580. 'id' => 3,
  3581. 'name' => 'green'
  3582. )
  3583. )
  3584. );
  3585. $this->Form->create('Contact');
  3586. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3587. $expected = array(
  3588. 'input' => array(
  3589. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3590. ),
  3591. 'select' => array(
  3592. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  3593. 'multiple' => 'multiple'
  3594. ),
  3595. array('option' => array('value' => '1', 'selected' => 'selected')),
  3596. 'blue',
  3597. '/option',
  3598. array('option' => array('value' => '2')),
  3599. 'red',
  3600. '/option',
  3601. array('option' => array('value' => '3', 'selected' => 'selected')),
  3602. 'green',
  3603. '/option',
  3604. '/select'
  3605. );
  3606. $this->assertTags($result, $expected);
  3607. }
  3608. /**
  3609. * test generation of multi select elements in checkbox format
  3610. *
  3611. * @return void
  3612. */
  3613. public function testSelectMultipleCheckboxes() {
  3614. $result = $this->Form->select(
  3615. 'Model.multi_field',
  3616. array('first', 'second', 'third'),
  3617. array('multiple' => 'checkbox')
  3618. );
  3619. $expected = array(
  3620. 'input' => array(
  3621. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3622. ),
  3623. array('div' => array('class' => 'checkbox')),
  3624. array('input' => array(
  3625. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3626. 'value' => '0', 'id' => 'ModelMultiField0'
  3627. )),
  3628. array('label' => array('for' => 'ModelMultiField0')),
  3629. 'first',
  3630. '/label',
  3631. '/div',
  3632. array('div' => array('class' => 'checkbox')),
  3633. array('input' => array(
  3634. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3635. 'value' => '1', 'id' => 'ModelMultiField1'
  3636. )),
  3637. array('label' => array('for' => 'ModelMultiField1')),
  3638. 'second',
  3639. '/label',
  3640. '/div',
  3641. array('div' => array('class' => 'checkbox')),
  3642. array('input' => array(
  3643. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3644. 'value' => '2', 'id' => 'ModelMultiField2'
  3645. )),
  3646. array('label' => array('for' => 'ModelMultiField2')),
  3647. 'third',
  3648. '/label',
  3649. '/div'
  3650. );
  3651. $this->assertTags($result, $expected);
  3652. $result = $this->Form->select(
  3653. 'Model.multi_field',
  3654. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3655. array('multiple' => 'checkbox')
  3656. );
  3657. $expected = array(
  3658. 'input' => array(
  3659. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3660. ),
  3661. array('div' => array('class' => 'checkbox')),
  3662. array('input' => array(
  3663. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3664. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  3665. )),
  3666. array('label' => array('for' => 'ModelMultiFieldA')),
  3667. 'first',
  3668. '/label',
  3669. '/div',
  3670. array('div' => array('class' => 'checkbox')),
  3671. array('input' => array(
  3672. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3673. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  3674. )),
  3675. array('label' => array('for' => 'ModelMultiFieldB')),
  3676. 'second',
  3677. '/label',
  3678. '/div',
  3679. array('div' => array('class' => 'checkbox')),
  3680. array('input' => array(
  3681. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3682. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  3683. )),
  3684. array('label' => array('for' => 'ModelMultiFieldC')),
  3685. 'third',
  3686. '/label',
  3687. '/div'
  3688. );
  3689. $this->assertTags($result, $expected);
  3690. $result = $this->Form->select(
  3691. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  3692. );
  3693. $expected = array(
  3694. 'input' => array(
  3695. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3696. ),
  3697. array('div' => array('class' => 'checkbox')),
  3698. array('input' => array(
  3699. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3700. 'value' => '1', 'id' => 'ModelMultiField1'
  3701. )),
  3702. array('label' => array('for' => 'ModelMultiField1')),
  3703. 'first',
  3704. '/label',
  3705. '/div'
  3706. );
  3707. $this->assertTags($result, $expected);
  3708. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3709. $result = $this->Form->select(
  3710. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3711. );
  3712. $expected = array(
  3713. 'input' => array(
  3714. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3715. ),
  3716. array('div' => array('class' => 'checkbox')),
  3717. array('input' => array(
  3718. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3719. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  3720. )),
  3721. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  3722. 'first',
  3723. '/label',
  3724. '/div',
  3725. array('div' => array('class' => 'checkbox')),
  3726. array('input' => array(
  3727. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3728. 'value' => 'Array', 'id' => 'ModelTagsArray'
  3729. )),
  3730. array('label' => array('for' => 'ModelTagsArray')),
  3731. 'Array',
  3732. '/label',
  3733. '/div'
  3734. );
  3735. $this->assertTags($result, $expected);
  3736. }
  3737. /**
  3738. * test multiple checkboxes with div styles.
  3739. *
  3740. * @return void
  3741. */
  3742. public function testSelectMultipleCheckboxDiv() {
  3743. $result = $this->Form->select(
  3744. 'Model.tags',
  3745. array('first', 'second'),
  3746. array('multiple' => 'checkbox', 'class' => 'my-class')
  3747. );
  3748. $expected = array(
  3749. 'input' => array(
  3750. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3751. ),
  3752. array('div' => array('class' => 'my-class')),
  3753. array('input' => array(
  3754. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3755. 'value' => '0', 'id' => 'ModelTags0'
  3756. )),
  3757. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  3758. '/div',
  3759. array('div' => array('class' => 'my-class')),
  3760. array('input' => array(
  3761. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3762. 'value' => '1', 'id' => 'ModelTags1'
  3763. )),
  3764. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  3765. '/div'
  3766. );
  3767. $this->assertTags($result, $expected);
  3768. $result = $this->Form->input('Model.tags', array(
  3769. 'options' => array('first', 'second'),
  3770. 'multiple' => 'checkbox',
  3771. 'class' => 'my-class',
  3772. 'div' => false,
  3773. 'label' => false
  3774. ));
  3775. $this->assertTags($result, $expected);
  3776. $Contact = ClassRegistry::getObject('Contact');
  3777. $Contact->validationErrors['tags'] = 'Select atleast one option';
  3778. $result = $this->Form->input('Contact.tags', array(
  3779. 'options' => array('one'),
  3780. 'multiple' => 'checkbox',
  3781. 'label' => false,
  3782. 'div' => false
  3783. ));
  3784. $expected = array(
  3785. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3786. array('div' => array('class' => 'checkbox form-error')),
  3787. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3788. array('label' => array('for' => 'ContactTags0')),
  3789. 'one',
  3790. '/label',
  3791. '/div'
  3792. );
  3793. $this->assertTags($result, $expected);
  3794. $result = $this->Form->input('Contact.tags', array(
  3795. 'options' => array('one'),
  3796. 'multiple' => 'checkbox',
  3797. 'class' => 'mycheckbox',
  3798. 'label' => false,
  3799. 'div' => false
  3800. ));
  3801. $expected = array(
  3802. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3803. array('div' => array('class' => 'mycheckbox form-error')),
  3804. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3805. array('label' => array('for' => 'ContactTags0')),
  3806. 'one',
  3807. '/label',
  3808. '/div'
  3809. );
  3810. $this->assertTags($result, $expected);
  3811. }
  3812. /**
  3813. * Checks the security hash array generated for multiple-input checkbox elements
  3814. *
  3815. * @return void
  3816. */
  3817. public function testSelectMultipleCheckboxSecurity() {
  3818. $this->Form->request['_Token'] = array('key' => 'testKey');
  3819. $this->assertEquals($this->Form->fields, array());
  3820. $result = $this->Form->select(
  3821. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  3822. array('multiple' => 'checkbox')
  3823. );
  3824. $this->assertEquals($this->Form->fields, array('Model.multi_field'));
  3825. $result = $this->Form->secure($this->Form->fields);
  3826. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  3827. $this->assertRegExp('/"' . $key . '"/', $result);
  3828. }
  3829. /**
  3830. * testInputMultipleCheckboxes method
  3831. *
  3832. * test input() resulting in multi select elements being generated.
  3833. *
  3834. * @return void
  3835. */
  3836. public function testInputMultipleCheckboxes() {
  3837. $result = $this->Form->input('Model.multi_field', array(
  3838. 'options' => array('first', 'second', 'third'),
  3839. 'multiple' => 'checkbox'
  3840. ));
  3841. $expected = array(
  3842. array('div' => array('class' => 'input select')),
  3843. array('label' => array('for' => 'ModelMultiField')),
  3844. 'Multi Field',
  3845. '/label',
  3846. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3847. array('div' => array('class' => 'checkbox')),
  3848. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  3849. array('label' => array('for' => 'ModelMultiField0')),
  3850. 'first',
  3851. '/label',
  3852. '/div',
  3853. array('div' => array('class' => 'checkbox')),
  3854. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  3855. array('label' => array('for' => 'ModelMultiField1')),
  3856. 'second',
  3857. '/label',
  3858. '/div',
  3859. array('div' => array('class' => 'checkbox')),
  3860. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  3861. array('label' => array('for' => 'ModelMultiField2')),
  3862. 'third',
  3863. '/label',
  3864. '/div',
  3865. '/div'
  3866. );
  3867. $this->assertTags($result, $expected);
  3868. $result = $this->Form->input('Model.multi_field', array(
  3869. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3870. 'multiple' => 'checkbox'
  3871. ));
  3872. $expected = array(
  3873. array('div' => array('class' => 'input select')),
  3874. array('label' => array('for' => 'ModelMultiField')),
  3875. 'Multi Field',
  3876. '/label',
  3877. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3878. array('div' => array('class' => 'checkbox')),
  3879. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  3880. array('label' => array('for' => 'ModelMultiFieldA')),
  3881. 'first',
  3882. '/label',
  3883. '/div',
  3884. array('div' => array('class' => 'checkbox')),
  3885. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  3886. array('label' => array('for' => 'ModelMultiFieldB')),
  3887. 'second',
  3888. '/label',
  3889. '/div',
  3890. array('div' => array('class' => 'checkbox')),
  3891. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  3892. array('label' => array('for' => 'ModelMultiFieldC')),
  3893. 'third',
  3894. '/label',
  3895. '/div',
  3896. '/div'
  3897. );
  3898. $this->assertTags($result, $expected);
  3899. $result = $this->Form->input('Model.multi_field', array(
  3900. 'options' => array('1' => 'first'),
  3901. 'multiple' => 'checkbox',
  3902. 'label' => false,
  3903. 'div' => false
  3904. ));
  3905. $expected = array(
  3906. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3907. array('div' => array('class' => 'checkbox')),
  3908. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  3909. array('label' => array('for' => 'ModelMultiField1')),
  3910. 'first',
  3911. '/label',
  3912. '/div'
  3913. );
  3914. $this->assertTags($result, $expected);
  3915. $result = $this->Form->input('Model.multi_field', array(
  3916. 'options' => array('2' => 'second'),
  3917. 'multiple' => 'checkbox',
  3918. 'label' => false,
  3919. 'div' => false
  3920. ));
  3921. $expected = array(
  3922. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3923. array('div' => array('class' => 'checkbox')),
  3924. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  3925. array('label' => array('for' => 'ModelMultiField2')),
  3926. 'second',
  3927. '/label',
  3928. '/div'
  3929. );
  3930. $this->assertTags($result, $expected);
  3931. }
  3932. /**
  3933. * testSelectHiddenFieldOmission method
  3934. *
  3935. * test that select() with 'hiddenField' => false omits the hidden field
  3936. *
  3937. * @return void
  3938. */
  3939. public function testSelectHiddenFieldOmission() {
  3940. $result = $this->Form->select('Model.multi_field',
  3941. array('first', 'second'),
  3942. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  3943. );
  3944. $expected = array(
  3945. array('div' => array('class' => 'checkbox')),
  3946. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  3947. array('label' => array('for' => 'ModelMultiField0')),
  3948. 'first',
  3949. '/label',
  3950. '/div',
  3951. array('div' => array('class' => 'checkbox')),
  3952. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  3953. array('label' => array('for' => 'ModelMultiField1')),
  3954. 'second',
  3955. '/label',
  3956. '/div'
  3957. );
  3958. $this->assertTags($result, $expected);
  3959. $result = $this->Form->input('Model.multi_field', array(
  3960. 'options' => array('first', 'second'),
  3961. 'multiple' => 'checkbox',
  3962. 'hiddenField' => false
  3963. ));
  3964. $expected = array(
  3965. array('div' => array('class' => 'input select')),
  3966. array('label' => array('for' => 'ModelMultiField')),
  3967. 'Multi Field',
  3968. '/label',
  3969. array('div' => array('class' => 'checkbox')),
  3970. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  3971. array('label' => array('for' => 'ModelMultiField0')),
  3972. 'first',
  3973. '/label',
  3974. '/div',
  3975. array('div' => array('class' => 'checkbox')),
  3976. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  3977. array('label' => array('for' => 'ModelMultiField1')),
  3978. 'second',
  3979. '/label',
  3980. '/div',
  3981. '/div'
  3982. );
  3983. $this->assertTags($result, $expected);
  3984. }
  3985. /**
  3986. * test that select() with multiple = checkbox works with overriding name attribute.
  3987. *
  3988. * @return void
  3989. */
  3990. public function testSelectCheckboxMultipleOverrideName() {
  3991. $result = $this->Form->input('category', array(
  3992. 'type' => 'select',
  3993. 'multiple' => 'checkbox',
  3994. 'name' => 'data[fish]',
  3995. 'options' => array('1', '2'),
  3996. 'div' => false,
  3997. 'label' => false,
  3998. ));
  3999. $expected = array(
  4000. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4001. array('div' => array('class' => 'checkbox')),
  4002. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4003. array('label' => array('for' => 'Category0')), '1', '/label',
  4004. '/div',
  4005. array('div' => array('class' => 'checkbox')),
  4006. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4007. array('label' => array('for' => 'Category1')), '2', '/label',
  4008. '/div'
  4009. );
  4010. $this->assertTags($result, $expected);
  4011. }
  4012. /**
  4013. * Test that 'id' overrides all the checkbox id's as well.
  4014. *
  4015. * @return void
  4016. */
  4017. public function testSelectCheckboxMultipleId() {
  4018. $result = $this->Form->select(
  4019. 'Model.multi_field',
  4020. array('first', 'second', 'third'),
  4021. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4022. );
  4023. $expected = array(
  4024. 'input' => array(
  4025. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4026. ),
  4027. array('div' => array('class' => 'checkbox')),
  4028. array('input' => array(
  4029. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4030. 'value' => '0', 'id' => 'CustomId0'
  4031. )),
  4032. array('label' => array('for' => 'CustomId0')),
  4033. 'first',
  4034. '/label',
  4035. '/div',
  4036. array('div' => array('class' => 'checkbox')),
  4037. array('input' => array(
  4038. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4039. 'value' => '1', 'id' => 'CustomId1'
  4040. )),
  4041. array('label' => array('for' => 'CustomId1')),
  4042. 'second',
  4043. '/label',
  4044. '/div',
  4045. array('div' => array('class' => 'checkbox')),
  4046. array('input' => array(
  4047. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4048. 'value' => '2', 'id' => 'CustomId2'
  4049. )),
  4050. array('label' => array('for' => 'CustomId2')),
  4051. 'third',
  4052. '/label',
  4053. '/div'
  4054. );
  4055. $this->assertTags($result, $expected);
  4056. }
  4057. /**
  4058. * testCheckbox method
  4059. *
  4060. * Test generation of checkboxes
  4061. *
  4062. * @return void
  4063. */
  4064. public function testCheckbox() {
  4065. $result = $this->Form->checkbox('Model.field');
  4066. $expected = array(
  4067. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4068. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4069. );
  4070. $this->assertTags($result, $expected);
  4071. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4072. $expected = array(
  4073. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4074. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4075. );
  4076. $this->assertTags($result, $expected);
  4077. $Contact = ClassRegistry::getObject('Contact');
  4078. $Contact->validationErrors['field'] = 1;
  4079. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4080. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4081. $expected = array(
  4082. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4083. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4084. );
  4085. $this->assertTags($result, $expected);
  4086. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4087. $expected = array(
  4088. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4089. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4090. );
  4091. $this->assertTags($result, $expected);
  4092. $this->Form->request->data['Contact']['field'] = '';
  4093. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4094. $expected = array(
  4095. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4096. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4097. );
  4098. $this->assertTags($result, $expected);
  4099. $Contact->validationErrors = array();
  4100. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4101. $expected = array(
  4102. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4103. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4104. );
  4105. $this->assertTags($result, $expected);
  4106. $this->Form->request->data['Contact']['published'] = 1;
  4107. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4108. $expected = array(
  4109. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4110. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4111. );
  4112. $this->assertTags($result, $expected);
  4113. $this->Form->request->data['Contact']['published'] = 0;
  4114. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4115. $expected = array(
  4116. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4117. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4118. );
  4119. $this->assertTags($result, $expected);
  4120. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4121. $expected = array(
  4122. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4123. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4124. );
  4125. $this->assertTags($result, $expected);
  4126. $result = $this->Form->checkbox('CustomField.1.value');
  4127. $expected = array(
  4128. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4129. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4130. );
  4131. $this->assertTags($result, $expected);
  4132. }
  4133. /**
  4134. * test checkbox() with a custom name attribute
  4135. *
  4136. * @return void
  4137. */
  4138. public function testCheckboxCustomNameAttribute() {
  4139. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4140. $expected = array(
  4141. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4142. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4143. );
  4144. $this->assertTags($result, $expected);
  4145. }
  4146. /**
  4147. * test the checked option for checkboxes.
  4148. *
  4149. * @return void
  4150. */
  4151. public function testCheckboxCheckedOption() {
  4152. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4153. $expected = array(
  4154. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4155. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4156. );
  4157. $this->assertTags($result, $expected);
  4158. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4159. $expected = array(
  4160. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4161. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4162. );
  4163. $this->assertTags($result, $expected);
  4164. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4165. $expected = array(
  4166. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4167. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4168. );
  4169. $this->assertTags($result, $expected);
  4170. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4171. $expected = array(
  4172. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4173. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4174. );
  4175. $this->assertTags($result, $expected);
  4176. $this->Form->request->data['Model']['field'] = 1;
  4177. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4178. $expected = array(
  4179. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4180. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4181. );
  4182. $this->assertTags($result, $expected);
  4183. }
  4184. /**
  4185. * Test that disabled attribute works on both the checkbox and hidden input.
  4186. *
  4187. * @return void
  4188. */
  4189. public function testCheckboxDisabling() {
  4190. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4191. $expected = array(
  4192. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4193. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4194. );
  4195. $this->assertTags($result, $expected);
  4196. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4197. $expected = array(
  4198. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4199. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4200. );
  4201. $this->assertTags($result, $expected);
  4202. }
  4203. /**
  4204. * Test that the hidden input for checkboxes can be omitted or set to a
  4205. * specific value.
  4206. *
  4207. * @return void
  4208. */
  4209. public function testCheckboxHiddenField() {
  4210. $result = $this->Form->input('UserForm.something', array(
  4211. 'type' => 'checkbox',
  4212. 'hiddenField' => false
  4213. ));
  4214. $expected = array(
  4215. 'div' => array('class' => 'input checkbox'),
  4216. array('input' => array(
  4217. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4218. 'value' => '1', 'id' => 'UserFormSomething'
  4219. )),
  4220. 'label' => array('for' => 'UserFormSomething'),
  4221. 'Something',
  4222. '/label',
  4223. '/div'
  4224. );
  4225. $this->assertTags($result, $expected);
  4226. $result = $this->Form->input('UserForm.something', array(
  4227. 'type' => 'checkbox',
  4228. 'value' => 'Y',
  4229. 'hiddenField' => 'N',
  4230. ));
  4231. $expected = array(
  4232. 'div' => array('class' => 'input checkbox'),
  4233. array('input' => array(
  4234. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  4235. 'value' => 'N', 'id' => 'UserFormSomething_'
  4236. )),
  4237. array('input' => array(
  4238. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4239. 'value' => 'Y', 'id' => 'UserFormSomething'
  4240. )),
  4241. 'label' => array('for' => 'UserFormSomething'),
  4242. 'Something',
  4243. '/label',
  4244. '/div'
  4245. );
  4246. $this->assertTags($result, $expected);
  4247. }
  4248. /**
  4249. * testDateTime method
  4250. *
  4251. * Test generation of date/time select elements
  4252. *
  4253. * @return void
  4254. */
  4255. public function testDateTime() {
  4256. extract($this->dateRegex);
  4257. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4258. $now = strtotime('now');
  4259. $expected = array(
  4260. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4261. $daysRegex,
  4262. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4263. date('j', $now),
  4264. '/option',
  4265. '*/select',
  4266. '-',
  4267. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4268. $monthsRegex,
  4269. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4270. date('F', $now),
  4271. '/option',
  4272. '*/select',
  4273. '-',
  4274. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4275. $yearsRegex,
  4276. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4277. date('Y', $now),
  4278. '/option',
  4279. '*/select',
  4280. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4281. $hoursRegex,
  4282. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4283. date('g', $now),
  4284. '/option',
  4285. '*/select',
  4286. ':',
  4287. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4288. $minutesRegex,
  4289. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4290. date('i', $now),
  4291. '/option',
  4292. '*/select',
  4293. ' ',
  4294. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4295. $meridianRegex,
  4296. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4297. date('a', $now),
  4298. '/option',
  4299. '*/select'
  4300. );
  4301. $this->assertTags($result, $expected);
  4302. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4303. $expected = array(
  4304. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4305. $daysRegex,
  4306. array('option' => array('value' => '')),
  4307. '/option',
  4308. '*/select',
  4309. '-',
  4310. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4311. $monthsRegex,
  4312. array('option' => array('value' => '')),
  4313. '/option',
  4314. '*/select',
  4315. '-',
  4316. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4317. $yearsRegex,
  4318. array('option' => array('value' => '')),
  4319. '/option',
  4320. '*/select',
  4321. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4322. $hoursRegex,
  4323. array('option' => array('value' => '')),
  4324. '/option',
  4325. '*/select',
  4326. ':',
  4327. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4328. $minutesRegex,
  4329. array('option' => array('value' => '')),
  4330. '/option',
  4331. '*/select',
  4332. ' ',
  4333. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4334. $meridianRegex,
  4335. array('option' => array('value' => '')),
  4336. '/option',
  4337. '*/select'
  4338. );
  4339. $this->assertTags($result, $expected);
  4340. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4341. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  4342. $expected = array(
  4343. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4344. $daysRegex,
  4345. array('option' => array('value' => '')),
  4346. '/option',
  4347. '*/select',
  4348. '-',
  4349. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4350. $monthsRegex,
  4351. array('option' => array('value' => '')),
  4352. '/option',
  4353. '*/select',
  4354. '-',
  4355. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4356. $yearsRegex,
  4357. array('option' => array('value' => '')),
  4358. '/option',
  4359. '*/select',
  4360. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4361. $hoursRegex,
  4362. array('option' => array('value' => '')),
  4363. '/option',
  4364. '*/select',
  4365. ':',
  4366. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4367. $minutesRegex,
  4368. array('option' => array('value' => '')),
  4369. '/option',
  4370. '*/select',
  4371. ' ',
  4372. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4373. $meridianRegex,
  4374. array('option' => array('value' => '')),
  4375. '/option',
  4376. '*/select'
  4377. );
  4378. $this->assertTags($result, $expected);
  4379. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4380. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  4381. $expected = array(
  4382. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4383. $daysRegex,
  4384. array('option' => array('value' => '')),
  4385. '/option',
  4386. '*/select',
  4387. '-',
  4388. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4389. $monthsRegex,
  4390. array('option' => array('value' => '')),
  4391. '/option',
  4392. '*/select',
  4393. '-',
  4394. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4395. $yearsRegex,
  4396. array('option' => array('value' => '')),
  4397. '/option',
  4398. '*/select',
  4399. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4400. $hoursRegex,
  4401. array('option' => array('value' => '')),
  4402. '/option',
  4403. '*/select',
  4404. ':',
  4405. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4406. $minutesRegex,
  4407. array('option' => array('value' => '')),
  4408. '/option',
  4409. '*/select',
  4410. ' ',
  4411. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4412. $meridianRegex,
  4413. array('option' => array('value' => '')),
  4414. '/option',
  4415. '*/select'
  4416. );
  4417. $this->assertTags($result, $expected);
  4418. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4419. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  4420. $expected = array(
  4421. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4422. $daysRegex,
  4423. array('option' => array('value' => '')),
  4424. '/option',
  4425. '*/select',
  4426. '-',
  4427. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4428. $monthsRegex,
  4429. array('option' => array('value' => '')),
  4430. '/option',
  4431. '*/select',
  4432. '-',
  4433. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4434. $yearsRegex,
  4435. array('option' => array('value' => '')),
  4436. '/option',
  4437. '*/select',
  4438. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4439. $hoursRegex,
  4440. array('option' => array('value' => '')),
  4441. '/option',
  4442. '*/select',
  4443. ':',
  4444. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4445. $minutesRegex,
  4446. array('option' => array('value' => '')),
  4447. '/option',
  4448. array('option' => array('value' => '00')),
  4449. '00',
  4450. '/option',
  4451. array('option' => array('value' => '05')),
  4452. '05',
  4453. '/option',
  4454. array('option' => array('value' => '10')),
  4455. '10',
  4456. '/option',
  4457. '*/select',
  4458. ' ',
  4459. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4460. $meridianRegex,
  4461. array('option' => array('value' => '')),
  4462. '/option',
  4463. '*/select'
  4464. );
  4465. $this->assertTags($result, $expected);
  4466. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4467. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4468. $expected = array(
  4469. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4470. $daysRegex,
  4471. array('option' => array('value' => '')),
  4472. '/option',
  4473. '*/select',
  4474. '-',
  4475. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4476. $monthsRegex,
  4477. array('option' => array('value' => '')),
  4478. '/option',
  4479. '*/select',
  4480. '-',
  4481. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4482. $yearsRegex,
  4483. array('option' => array('value' => '')),
  4484. '/option',
  4485. '*/select',
  4486. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4487. $hoursRegex,
  4488. array('option' => array('value' => '')),
  4489. '/option',
  4490. '*/select',
  4491. ':',
  4492. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4493. $minutesRegex,
  4494. array('option' => array('value' => '')),
  4495. '/option',
  4496. array('option' => array('value' => '00')),
  4497. '00',
  4498. '/option',
  4499. array('option' => array('value' => '05')),
  4500. '05',
  4501. '/option',
  4502. array('option' => array('value' => '10')),
  4503. '10',
  4504. '/option',
  4505. '*/select',
  4506. ' ',
  4507. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4508. $meridianRegex,
  4509. array('option' => array('value' => '')),
  4510. '/option',
  4511. '*/select'
  4512. );
  4513. $this->assertTags($result, $expected);
  4514. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4515. $this->Form->request->data['Contact']['data'] = null;
  4516. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4517. $expected = array(
  4518. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4519. $daysRegex,
  4520. array('option' => array('value' => '')),
  4521. '/option',
  4522. '*/select',
  4523. '-',
  4524. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4525. $monthsRegex,
  4526. array('option' => array('value' => '')),
  4527. '/option',
  4528. '*/select',
  4529. '-',
  4530. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4531. $yearsRegex,
  4532. array('option' => array('value' => '')),
  4533. '/option',
  4534. '*/select',
  4535. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4536. $hoursRegex,
  4537. array('option' => array('value' => '')),
  4538. '/option',
  4539. '*/select',
  4540. ':',
  4541. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4542. $minutesRegex,
  4543. array('option' => array('value' => '')),
  4544. '/option',
  4545. '*/select',
  4546. ' ',
  4547. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4548. $meridianRegex,
  4549. array('option' => array('value' => '')),
  4550. '/option',
  4551. '*/select'
  4552. );
  4553. $this->assertTags($result, $expected);
  4554. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4555. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  4556. $now = strtotime($this->Form->data['Model']['field']);
  4557. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  4558. $expected = array(
  4559. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4560. $daysRegex,
  4561. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4562. date('j', $now),
  4563. '/option',
  4564. '*/select',
  4565. '-',
  4566. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4567. $monthsRegex,
  4568. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4569. date('F', $now),
  4570. '/option',
  4571. '*/select',
  4572. '-',
  4573. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  4574. $yearsRegex,
  4575. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4576. date('Y', $now),
  4577. '/option',
  4578. '*/select',
  4579. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4580. $hoursRegex,
  4581. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4582. date('g', $now),
  4583. '/option',
  4584. '*/select',
  4585. ':',
  4586. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4587. $minutesRegex,
  4588. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4589. date('i', $now),
  4590. '/option',
  4591. '*/select',
  4592. ' ',
  4593. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  4594. $meridianRegex,
  4595. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4596. date('a', $now),
  4597. '/option',
  4598. '*/select'
  4599. );
  4600. $this->assertTags($result, $expected);
  4601. $selected = strtotime('2008-10-26 12:33:00');
  4602. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  4603. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  4604. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  4605. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  4606. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  4607. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  4608. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  4609. $this->Form->create('Contact');
  4610. $result = $this->Form->input('published');
  4611. $now = strtotime('now');
  4612. $expected = array(
  4613. 'div' => array('class' => 'input date'),
  4614. 'label' => array('for' => 'ContactPublishedMonth'),
  4615. 'Published',
  4616. '/label',
  4617. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4618. $monthsRegex,
  4619. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4620. date('F', $now),
  4621. '/option',
  4622. '*/select',
  4623. '-',
  4624. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4625. $daysRegex,
  4626. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4627. date('j', $now),
  4628. '/option',
  4629. '*/select',
  4630. '-',
  4631. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4632. $yearsRegex,
  4633. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4634. date('Y', $now),
  4635. '/option',
  4636. '*/select',
  4637. '/div'
  4638. );
  4639. $this->assertTags($result, $expected);
  4640. $result = $this->Form->input('published2', array('type' => 'date'));
  4641. $now = strtotime('now');
  4642. $expected = array(
  4643. 'div' => array('class' => 'input date'),
  4644. 'label' => array('for' => 'ContactPublished2Month'),
  4645. 'Published2',
  4646. '/label',
  4647. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  4648. $monthsRegex,
  4649. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4650. date('F', $now),
  4651. '/option',
  4652. '*/select',
  4653. '-',
  4654. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  4655. $daysRegex,
  4656. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4657. date('j', $now),
  4658. '/option',
  4659. '*/select',
  4660. '-',
  4661. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  4662. $yearsRegex,
  4663. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4664. date('Y', $now),
  4665. '/option',
  4666. '*/select',
  4667. '/div'
  4668. );
  4669. $this->assertTags($result, $expected);
  4670. $this->Form->create('Contact');
  4671. $result = $this->Form->input('published', array('monthNames' => false));
  4672. $now = strtotime('now');
  4673. $expected = array(
  4674. 'div' => array('class' => 'input date'),
  4675. 'label' => array('for' => 'ContactPublishedMonth'),
  4676. 'Published',
  4677. '/label',
  4678. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4679. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4680. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4681. date('m', $now),
  4682. '/option',
  4683. '*/select',
  4684. '-',
  4685. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4686. $daysRegex,
  4687. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4688. date('j', $now),
  4689. '/option',
  4690. '*/select',
  4691. '-',
  4692. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4693. $yearsRegex,
  4694. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4695. date('Y', $now),
  4696. '/option',
  4697. '*/select',
  4698. '/div'
  4699. );
  4700. $this->assertTags($result, $expected);
  4701. $result = $this->Form->input('published', array('type' => 'time'));
  4702. $now = strtotime('now');
  4703. $expected = array(
  4704. 'div' => array('class' => 'input time'),
  4705. 'label' => array('for' => 'ContactPublishedHour'),
  4706. 'Published',
  4707. '/label',
  4708. array('select' => array('name' => 'data[Contact][published][hour]', 'id' => 'ContactPublishedHour')),
  4709. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4710. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4711. date('g', $now),
  4712. '/option',
  4713. '*/select',
  4714. ':',
  4715. );
  4716. $this->assertTags($result, $expected);
  4717. $result = $this->Form->input('published', array(
  4718. 'timeFormat' => 24,
  4719. 'interval' => 5,
  4720. 'selected' => strtotime('2009-09-03 13:37:00'),
  4721. 'type' => 'datetime'
  4722. ));
  4723. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4724. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  4725. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  4726. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  4727. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  4728. $this->assertNoErrors();
  4729. $this->Form->request->data['Contact'] = array(
  4730. 'date' => array(
  4731. 'day' => '',
  4732. 'month' => '',
  4733. 'year' => '',
  4734. 'hour' => '',
  4735. 'min' => '',
  4736. 'meridian' => ''
  4737. )
  4738. );
  4739. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4740. }
  4741. /**
  4742. * test that datetime() and default values work.
  4743. *
  4744. * @return void
  4745. */
  4746. public function testDatetimeWithDefault() {
  4747. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  4748. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4749. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4750. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4751. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  4752. 'default' => '2009-06-01 11:15:30'
  4753. ));
  4754. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4755. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4756. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4757. }
  4758. /**
  4759. * test that bogus non-date time data doesn't cause errors.
  4760. *
  4761. * @return void
  4762. */
  4763. public function testDateTimeWithBogusData() {
  4764. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  4765. $this->assertNotRegExp('/selected="selected">\d/', $result);
  4766. }
  4767. /**
  4768. * testFormDateTimeMulti method
  4769. *
  4770. * test multiple datetime element generation
  4771. *
  4772. * @return void
  4773. */
  4774. public function testFormDateTimeMulti() {
  4775. extract($this->dateRegex);
  4776. $result = $this->Form->dateTime('Contact.1.updated');
  4777. $expected = array(
  4778. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  4779. $daysRegex,
  4780. array('option' => array('value' => '')),
  4781. '/option',
  4782. '*/select',
  4783. '-',
  4784. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  4785. $monthsRegex,
  4786. array('option' => array('value' => '')),
  4787. '/option',
  4788. '*/select',
  4789. '-',
  4790. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  4791. $yearsRegex,
  4792. array('option' => array('value' => '')),
  4793. '/option',
  4794. '*/select',
  4795. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  4796. $hoursRegex,
  4797. array('option' => array('value' => '')),
  4798. '/option',
  4799. '*/select',
  4800. ':',
  4801. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  4802. $minutesRegex,
  4803. array('option' => array('value' => '')),
  4804. '/option',
  4805. '*/select',
  4806. ' ',
  4807. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  4808. $meridianRegex,
  4809. array('option' => array('value' => '')),
  4810. '/option',
  4811. '*/select'
  4812. );
  4813. $this->assertTags($result, $expected);
  4814. $result = $this->Form->dateTime('Contact.2.updated');
  4815. $expected = array(
  4816. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  4817. $daysRegex,
  4818. array('option' => array('value' => '')),
  4819. '/option',
  4820. '*/select',
  4821. '-',
  4822. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  4823. $monthsRegex,
  4824. array('option' => array('value' => '')),
  4825. '/option',
  4826. '*/select',
  4827. '-',
  4828. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  4829. $yearsRegex,
  4830. array('option' => array('value' => '')),
  4831. '/option',
  4832. '*/select',
  4833. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  4834. $hoursRegex,
  4835. array('option' => array('value' => '')),
  4836. '/option',
  4837. '*/select',
  4838. ':',
  4839. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  4840. $minutesRegex,
  4841. array('option' => array('value' => '')),
  4842. '/option',
  4843. '*/select',
  4844. ' ',
  4845. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  4846. $meridianRegex,
  4847. array('option' => array('value' => '')),
  4848. '/option',
  4849. '*/select'
  4850. );
  4851. $this->assertTags($result, $expected);
  4852. }
  4853. /**
  4854. * When changing the date format, the label should always focus the first select box when
  4855. * clicked.
  4856. *
  4857. * @return void
  4858. */
  4859. public function testDateTimeLabelIdMatchesFirstInput() {
  4860. $result = $this->Form->input('Model.date', array('type' => 'date'));
  4861. $this->assertContains('label for="ModelDateMonth"', $result);
  4862. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  4863. $this->assertContains('label for="ModelDateDay"', $result);
  4864. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  4865. $this->assertContains('label for="ModelDateYear"', $result);
  4866. }
  4867. /**
  4868. * testMonth method
  4869. *
  4870. * @return void
  4871. */
  4872. public function testMonth() {
  4873. $result = $this->Form->month('Model.field');
  4874. $expected = array(
  4875. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4876. array('option' => array('value' => '')),
  4877. '/option',
  4878. array('option' => array('value' => '01')),
  4879. date('F', strtotime('2008-01-01 00:00:00')),
  4880. '/option',
  4881. array('option' => array('value' => '02')),
  4882. date('F', strtotime('2008-02-01 00:00:00')),
  4883. '/option',
  4884. '*/select',
  4885. );
  4886. $this->assertTags($result, $expected);
  4887. $result = $this->Form->month('Model.field', array('empty' => true));
  4888. $expected = array(
  4889. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4890. array('option' => array('value' => '')),
  4891. '/option',
  4892. array('option' => array('value' => '01')),
  4893. date('F', strtotime('2008-01-01 00:00:00')),
  4894. '/option',
  4895. array('option' => array('value' => '02')),
  4896. date('F', strtotime('2008-02-01 00:00:00')),
  4897. '/option',
  4898. '*/select',
  4899. );
  4900. $this->assertTags($result, $expected);
  4901. $result = $this->Form->month('Model.field', array('monthNames' => false));
  4902. $expected = array(
  4903. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4904. array('option' => array('value' => '')),
  4905. '/option',
  4906. array('option' => array('value' => '01')),
  4907. '01',
  4908. '/option',
  4909. array('option' => array('value' => '02')),
  4910. '02',
  4911. '/option',
  4912. '*/select',
  4913. );
  4914. $this->assertTags($result, $expected);
  4915. $monthNames = array(
  4916. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  4917. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  4918. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  4919. $expected = array(
  4920. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4921. array('option' => array('value' => '')),
  4922. '/option',
  4923. array('option' => array('value' => '01')),
  4924. 'Jan',
  4925. '/option',
  4926. array('option' => array('value' => '02')),
  4927. 'Feb',
  4928. '/option',
  4929. '*/select',
  4930. );
  4931. $this->assertTags($result, $expected);
  4932. }
  4933. /**
  4934. * testDay method
  4935. *
  4936. * @return void
  4937. */
  4938. public function testDay() {
  4939. extract($this->dateRegex);
  4940. $result = $this->Form->day('Model.field', array('value' => false));
  4941. $expected = array(
  4942. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4943. array('option' => array('value' => '')),
  4944. '/option',
  4945. array('option' => array('value' => '01')),
  4946. '1',
  4947. '/option',
  4948. array('option' => array('value' => '02')),
  4949. '2',
  4950. '/option',
  4951. $daysRegex,
  4952. '/select',
  4953. );
  4954. $this->assertTags($result, $expected);
  4955. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  4956. $result = $this->Form->day('Model.field');
  4957. $expected = array(
  4958. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4959. array('option' => array('value' => '')),
  4960. '/option',
  4961. array('option' => array('value' => '01')),
  4962. '1',
  4963. '/option',
  4964. array('option' => array('value' => '02')),
  4965. '2',
  4966. '/option',
  4967. $daysRegex,
  4968. array('option' => array('value' => '10', 'selected' => 'selected')),
  4969. '10',
  4970. '/option',
  4971. $daysRegex,
  4972. '/select',
  4973. );
  4974. $this->assertTags($result, $expected);
  4975. $this->Form->request->data['Model']['field'] = '';
  4976. $result = $this->Form->day('Model.field', array('value' => '10'));
  4977. $expected = array(
  4978. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4979. array('option' => array('value' => '')),
  4980. '/option',
  4981. array('option' => array('value' => '01')),
  4982. '1',
  4983. '/option',
  4984. array('option' => array('value' => '02')),
  4985. '2',
  4986. '/option',
  4987. $daysRegex,
  4988. array('option' => array('value' => '10', 'selected' => 'selected')),
  4989. '10',
  4990. '/option',
  4991. $daysRegex,
  4992. '/select',
  4993. );
  4994. $this->assertTags($result, $expected);
  4995. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  4996. $result = $this->Form->day('Model.field', array('value' => true));
  4997. $expected = array(
  4998. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4999. array('option' => array('value' => '')),
  5000. '/option',
  5001. array('option' => array('value' => '01')),
  5002. '1',
  5003. '/option',
  5004. array('option' => array('value' => '02')),
  5005. '2',
  5006. '/option',
  5007. $daysRegex,
  5008. array('option' => array('value' => '10', 'selected' => 'selected')),
  5009. '10',
  5010. '/option',
  5011. $daysRegex,
  5012. '/select',
  5013. );
  5014. $this->assertTags($result, $expected);
  5015. }
  5016. /**
  5017. * testMinute method
  5018. *
  5019. * @return void
  5020. */
  5021. public function testMinute() {
  5022. extract($this->dateRegex);
  5023. $result = $this->Form->minute('Model.field');
  5024. $expected = array(
  5025. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5026. array('option' => array('value' => '')),
  5027. '/option',
  5028. array('option' => array('value' => '00')),
  5029. '00',
  5030. '/option',
  5031. array('option' => array('value' => '01')),
  5032. '01',
  5033. '/option',
  5034. array('option' => array('value' => '02')),
  5035. '02',
  5036. '/option',
  5037. $minutesRegex,
  5038. '/select',
  5039. );
  5040. $this->assertTags($result, $expected);
  5041. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5042. $result = $this->Form->minute('Model.field');
  5043. $expected = array(
  5044. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5045. array('option' => array('value' => '')),
  5046. '/option',
  5047. array('option' => array('value' => '00')),
  5048. '00',
  5049. '/option',
  5050. array('option' => array('value' => '01')),
  5051. '01',
  5052. '/option',
  5053. array('option' => array('value' => '02')),
  5054. '02',
  5055. '/option',
  5056. $minutesRegex,
  5057. array('option' => array('value' => '12', 'selected' => 'selected')),
  5058. '12',
  5059. '/option',
  5060. $minutesRegex,
  5061. '/select',
  5062. );
  5063. $this->assertTags($result, $expected);
  5064. $this->Form->request->data['Model']['field'] = '';
  5065. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5066. $expected = array(
  5067. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5068. array('option' => array('value' => '')),
  5069. '/option',
  5070. array('option' => array('value' => '00')),
  5071. '00',
  5072. '/option',
  5073. array('option' => array('value' => '05')),
  5074. '05',
  5075. '/option',
  5076. array('option' => array('value' => '10')),
  5077. '10',
  5078. '/option',
  5079. $minutesRegex,
  5080. '/select',
  5081. );
  5082. $this->assertTags($result, $expected);
  5083. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5084. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5085. $expected = array(
  5086. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5087. array('option' => array('value' => '')),
  5088. '/option',
  5089. array('option' => array('value' => '00')),
  5090. '00',
  5091. '/option',
  5092. array('option' => array('value' => '05')),
  5093. '05',
  5094. '/option',
  5095. array('option' => array('value' => '10', 'selected' => 'selected')),
  5096. '10',
  5097. '/option',
  5098. $minutesRegex,
  5099. '/select',
  5100. );
  5101. $this->assertTags($result, $expected);
  5102. }
  5103. /**
  5104. * testHour method
  5105. *
  5106. * @return void
  5107. */
  5108. public function testHour() {
  5109. extract($this->dateRegex);
  5110. $result = $this->Form->hour('Model.field', false);
  5111. $expected = array(
  5112. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5113. array('option' => array('value' => '')),
  5114. '/option',
  5115. array('option' => array('value' => '01')),
  5116. '1',
  5117. '/option',
  5118. array('option' => array('value' => '02')),
  5119. '2',
  5120. '/option',
  5121. $hoursRegex,
  5122. '/select',
  5123. );
  5124. $this->assertTags($result, $expected);
  5125. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5126. $result = $this->Form->hour('Model.field', false);
  5127. $expected = array(
  5128. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5129. array('option' => array('value' => '')),
  5130. '/option',
  5131. array('option' => array('value' => '01')),
  5132. '1',
  5133. '/option',
  5134. array('option' => array('value' => '02')),
  5135. '2',
  5136. '/option',
  5137. $hoursRegex,
  5138. array('option' => array('value' => '12', 'selected' => 'selected')),
  5139. '12',
  5140. '/option',
  5141. '/select',
  5142. );
  5143. $this->assertTags($result, $expected);
  5144. $this->Form->request->data['Model']['field'] = '';
  5145. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5146. $expected = array(
  5147. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5148. array('option' => array('value' => '')),
  5149. '/option',
  5150. array('option' => array('value' => '00')),
  5151. '0',
  5152. '/option',
  5153. array('option' => array('value' => '01')),
  5154. '1',
  5155. '/option',
  5156. array('option' => array('value' => '02')),
  5157. '2',
  5158. '/option',
  5159. $hoursRegex,
  5160. array('option' => array('value' => '23', 'selected' => 'selected')),
  5161. '23',
  5162. '/option',
  5163. '/select',
  5164. );
  5165. $this->assertTags($result, $expected);
  5166. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5167. $result = $this->Form->hour('Model.field', true);
  5168. $expected = array(
  5169. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5170. array('option' => array('value' => '')),
  5171. '/option',
  5172. array('option' => array('value' => '00', 'selected' => 'selected')),
  5173. '0',
  5174. '/option',
  5175. array('option' => array('value' => '01')),
  5176. '1',
  5177. '/option',
  5178. array('option' => array('value' => '02')),
  5179. '2',
  5180. '/option',
  5181. $hoursRegex,
  5182. '/select',
  5183. );
  5184. $this->assertTags($result, $expected);
  5185. unset($this->Form->request->data['Model']['field']);
  5186. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5187. $thisHour = date('H');
  5188. $optValue = date('G');
  5189. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5190. }
  5191. /**
  5192. * testYear method
  5193. *
  5194. * @return void
  5195. */
  5196. public function testYear() {
  5197. $result = $this->Form->year('Model.field', 2006, 2007);
  5198. $expected = array(
  5199. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5200. array('option' => array('value' => '')),
  5201. '/option',
  5202. array('option' => array('value' => '2007')),
  5203. '2007',
  5204. '/option',
  5205. array('option' => array('value' => '2006')),
  5206. '2006',
  5207. '/option',
  5208. '/select',
  5209. );
  5210. $this->assertTags($result, $expected);
  5211. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5212. $expected = array(
  5213. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5214. array('option' => array('value' => '')),
  5215. '/option',
  5216. array('option' => array('value' => '2006')),
  5217. '2006',
  5218. '/option',
  5219. array('option' => array('value' => '2007')),
  5220. '2007',
  5221. '/option',
  5222. '/select',
  5223. );
  5224. $this->assertTags($result, $expected);
  5225. $this->request->data['Contact']['published'] = '';
  5226. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5227. $expected = array(
  5228. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5229. array('option' => array('value' => '')),
  5230. '/option',
  5231. array('option' => array('value' => '2007')),
  5232. '2007',
  5233. '/option',
  5234. array('option' => array('value' => '2006')),
  5235. '2006',
  5236. '/option',
  5237. '/select',
  5238. );
  5239. $this->assertTags($result, $expected);
  5240. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5241. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5242. $expected = array(
  5243. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5244. array('option' => array('value' => '2007')),
  5245. '2007',
  5246. '/option',
  5247. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5248. '2006',
  5249. '/option',
  5250. '/select',
  5251. );
  5252. $this->assertTags($result, $expected);
  5253. $this->Form->request->data['Contact']['published'] = '';
  5254. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5255. $expected = array(
  5256. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5257. array('option' => array('value' => '')),
  5258. '/option',
  5259. array('option' => array('value' => '2007')),
  5260. '2007',
  5261. '/option',
  5262. array('option' => array('value' => '2006')),
  5263. '2006',
  5264. '/option',
  5265. '/select',
  5266. );
  5267. $this->assertTags($result, $expected);
  5268. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5269. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5270. $expected = array(
  5271. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5272. array('option' => array('value' => '2007')),
  5273. '2007',
  5274. '/option',
  5275. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5276. '2006',
  5277. '/option',
  5278. '/select',
  5279. );
  5280. $this->assertTags($result, $expected);
  5281. $this->Form->request->data['Contact']['published'] = '';
  5282. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5283. $expected = array(
  5284. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5285. array('option' => array('value' => '')),
  5286. '/option',
  5287. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5288. '2007',
  5289. '/option',
  5290. array('option' => array('value' => '2006')),
  5291. '2006',
  5292. '/option',
  5293. '/select',
  5294. );
  5295. $this->assertTags($result, $expected);
  5296. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5297. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5298. $expected = array(
  5299. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5300. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5301. '2007',
  5302. '/option',
  5303. array('option' => array('value' => '2006')),
  5304. '2006',
  5305. '/option',
  5306. '/select',
  5307. );
  5308. $this->assertTags($result, $expected);
  5309. $this->Form->request->data['Contact']['published'] = '';
  5310. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5311. $expected = array(
  5312. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5313. array('option' => array('value' => '2008')),
  5314. '2008',
  5315. '/option',
  5316. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5317. '2007',
  5318. '/option',
  5319. array('option' => array('value' => '2006')),
  5320. '2006',
  5321. '/option',
  5322. '/select',
  5323. );
  5324. $this->assertTags($result, $expected);
  5325. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5326. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5327. $expected = array(
  5328. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5329. array('option' => array('value' => '2008')),
  5330. '2008',
  5331. '/option',
  5332. array('option' => array('value' => '2007')),
  5333. '2007',
  5334. '/option',
  5335. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5336. '2006',
  5337. '/option',
  5338. '/select',
  5339. );
  5340. $this->assertTags($result, $expected);
  5341. $this->Form->request->data = array();
  5342. $this->Form->create('Contact');
  5343. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5344. $expected = array(
  5345. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5346. array('option' => array('value' => '2008')),
  5347. '2008',
  5348. '/option',
  5349. array('option' => array('value' => '2007')),
  5350. '2007',
  5351. '/option',
  5352. array('option' => array('value' => '2006')),
  5353. '2006',
  5354. '/option',
  5355. '/select',
  5356. );
  5357. $this->assertTags($result, $expected);
  5358. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  5359. $this->assertContains('data[Contact][published][year]', $result);
  5360. }
  5361. /**
  5362. * testTextArea method
  5363. *
  5364. * @return void
  5365. */
  5366. public function testTextArea() {
  5367. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  5368. $result = $this->Form->textarea('Model.field');
  5369. $expected = array(
  5370. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5371. 'some test data',
  5372. '/textarea',
  5373. );
  5374. $this->assertTags($result, $expected);
  5375. $result = $this->Form->textarea('Model.tmp');
  5376. $expected = array(
  5377. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  5378. '/textarea',
  5379. );
  5380. $this->assertTags($result, $expected);
  5381. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5382. $result = $this->Form->textarea('Model.field');
  5383. $expected = array(
  5384. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5385. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5386. '/textarea',
  5387. );
  5388. $this->assertTags($result, $expected);
  5389. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5390. $result = $this->Form->textarea('Model.field', array('escape' => false));
  5391. $expected = array(
  5392. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5393. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5394. '/textarea',
  5395. );
  5396. $this->assertTags($result, $expected);
  5397. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  5398. $result = $this->Form->textarea('Model.0.OtherModel.field');
  5399. $expected = array(
  5400. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  5401. '/textarea'
  5402. );
  5403. $this->assertTags($result, $expected);
  5404. }
  5405. /**
  5406. * testTextAreaWithStupidCharacters method
  5407. *
  5408. * test text area with non-ascii characters
  5409. *
  5410. * @return void
  5411. */
  5412. public function testTextAreaWithStupidCharacters() {
  5413. $this->loadFixtures('Post');
  5414. $result = $this->Form->input('Post.content', array(
  5415. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  5416. ));
  5417. $expected = array(
  5418. 'div' => array('class' => 'input text'),
  5419. 'label' => array('for' => 'PostContent'),
  5420. 'Current Text',
  5421. '/label',
  5422. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  5423. 'GREAT®',
  5424. '/textarea',
  5425. '/div'
  5426. );
  5427. $this->assertTags($result, $expected);
  5428. }
  5429. /**
  5430. * testHiddenField method
  5431. *
  5432. * @return void
  5433. */
  5434. public function testHiddenField() {
  5435. $Contact = ClassRegistry::getObject('Contact');
  5436. $Contact->validationErrors['field'] = 1;
  5437. $this->Form->request->data['Contact']['field'] = 'test';
  5438. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  5439. $this->assertTags($result, array(
  5440. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  5441. );
  5442. }
  5443. /**
  5444. * testFileUploadField method
  5445. *
  5446. * @return void
  5447. */
  5448. public function testFileUploadField() {
  5449. $result = $this->Form->file('Model.upload');
  5450. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5451. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  5452. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  5453. $expected = array(
  5454. 'div' => array('class' => 'input file'),
  5455. 'label' => array('for' => 'ModelUpload'),
  5456. 'Upload',
  5457. '/label',
  5458. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  5459. '/div'
  5460. );
  5461. $this->assertTags($result, $expected);
  5462. }
  5463. /**
  5464. * test File upload input on a model not used in create();
  5465. *
  5466. * @return void
  5467. */
  5468. public function testFileUploadOnOtherModel() {
  5469. $this->Form->create('ValidateUser', array('type' => 'file'));
  5470. $result = $this->Form->file('ValidateProfile.city');
  5471. $expected = array(
  5472. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  5473. );
  5474. $this->assertTags($result, $expected);
  5475. }
  5476. /**
  5477. * testButton method
  5478. *
  5479. * @return void
  5480. */
  5481. public function testButton() {
  5482. $result = $this->Form->button('Hi');
  5483. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5484. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5485. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5486. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5487. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5488. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5489. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5490. $result = $this->Form->button('No type', array('type' => false));
  5491. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5492. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  5493. $this->assertNotRegExp('/\&039/', $result);
  5494. }
  5495. /**
  5496. * Test that button() makes unlocked fields by default.
  5497. *
  5498. * @return void
  5499. */
  5500. public function testButtonUnlockedByDefault() {
  5501. $this->Form->request->params['_Token']['key'] = 'secured';
  5502. $this->Form->button('Save', array('name' => 'save'));
  5503. $this->Form->button('Clear');
  5504. $result = $this->Form->unlockField();
  5505. $this->assertEquals(array('save'), $result);
  5506. }
  5507. /**
  5508. * testPostButton method
  5509. *
  5510. * @return void
  5511. */
  5512. public function testPostButton() {
  5513. $result = $this->Form->postButton('Hi', '/controller/action');
  5514. $this->assertTags($result, array(
  5515. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'style' => 'display:none;'),
  5516. 'div' => array('style' => 'display:none;'),
  5517. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5518. '/div',
  5519. 'button' => array('type' => 'submit'),
  5520. 'Hi',
  5521. '/button',
  5522. '/form'
  5523. ));
  5524. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5525. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  5526. }
  5527. /**
  5528. * Test that postButton adds _Token fields.
  5529. *
  5530. * @return void
  5531. */
  5532. public function testSecurePostButton() {
  5533. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5534. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5535. $expected = array(
  5536. 'form' => array(
  5537. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5538. 'style' => 'display:none;'
  5539. ),
  5540. array('div' => array('style' => 'display:none;')),
  5541. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5542. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5543. '/div',
  5544. 'button' => array('type' => 'submit'),
  5545. 'Delete',
  5546. '/button',
  5547. array('div' => array('style' => 'display:none;')),
  5548. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5549. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5550. '/div',
  5551. '/form',
  5552. );
  5553. $this->assertTags($result, $expected);
  5554. }
  5555. /**
  5556. * testPostLink method
  5557. *
  5558. * @return void
  5559. */
  5560. public function testPostLink() {
  5561. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5562. $this->assertTags($result, array(
  5563. 'form' => array(
  5564. 'method' => 'post', 'action' => '/posts/delete/1',
  5565. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5566. ),
  5567. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5568. '/form',
  5569. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5570. 'Delete',
  5571. '/a'
  5572. ));
  5573. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5574. $this->assertTags($result, array(
  5575. 'form' => array(
  5576. 'method' => 'post', 'action' => '/posts/delete/1',
  5577. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5578. ),
  5579. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5580. '/form',
  5581. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5582. 'Delete',
  5583. '/a'
  5584. ));
  5585. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5586. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  5587. }
  5588. /**
  5589. * Test that postLink adds _Token fields.
  5590. *
  5591. * @return void
  5592. */
  5593. public function testSecurePostLink() {
  5594. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5595. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5596. $expected = array(
  5597. 'form' => array(
  5598. 'method' => 'post', 'action' => '/posts/delete/1',
  5599. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5600. ),
  5601. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5602. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5603. 'div' => array('style' => 'display:none;'),
  5604. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5605. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5606. '/div',
  5607. '/form',
  5608. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5609. 'Delete',
  5610. '/a'
  5611. );
  5612. $this->assertTags($result, $expected);
  5613. }
  5614. /**
  5615. * testSubmitButton method
  5616. *
  5617. * @return void
  5618. */
  5619. public function testSubmitButton() {
  5620. $result = $this->Form->submit('');
  5621. $expected = array(
  5622. 'div' => array('class' => 'submit'),
  5623. 'input' => array('type' => 'submit', 'value' => ''),
  5624. '/div'
  5625. );
  5626. $this->assertTags($result, $expected);
  5627. $result = $this->Form->submit('Test Submit');
  5628. $expected = array(
  5629. 'div' => array('class' => 'submit'),
  5630. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5631. '/div'
  5632. );
  5633. $this->assertTags($result, $expected);
  5634. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5635. $expected = array(
  5636. 'span' => array('class' => 'submit'),
  5637. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5638. '/span'
  5639. );
  5640. $this->assertTags($result, $expected);
  5641. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5642. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5643. $this->assertTags($result, $expected);
  5644. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5645. $expected = array(
  5646. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5647. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5648. '/div'
  5649. );
  5650. $this->assertTags($result, $expected);
  5651. $result = $this->Form->submit('Next >');
  5652. $expected = array(
  5653. 'div' => array('class' => 'submit'),
  5654. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5655. '/div'
  5656. );
  5657. $this->assertTags($result, $expected);
  5658. $result = $this->Form->submit('Next >', array('escape' => false));
  5659. $expected = array(
  5660. 'div' => array('class' => 'submit'),
  5661. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5662. '/div'
  5663. );
  5664. $this->assertTags($result, $expected);
  5665. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5666. $expected = array(
  5667. 'div' => array('class' => 'submit'),
  5668. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5669. '/div'
  5670. );
  5671. $this->assertTags($result, $expected);
  5672. $before = '--before--';
  5673. $after = '--after--';
  5674. $result = $this->Form->submit('Test', array('before' => $before));
  5675. $expected = array(
  5676. 'div' => array('class' => 'submit'),
  5677. '--before--',
  5678. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5679. '/div'
  5680. );
  5681. $this->assertTags($result, $expected);
  5682. $result = $this->Form->submit('Test', array('after' => $after));
  5683. $expected = array(
  5684. 'div' => array('class' => 'submit'),
  5685. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5686. '--after--',
  5687. '/div'
  5688. );
  5689. $this->assertTags($result, $expected);
  5690. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5691. $expected = array(
  5692. 'div' => array('class' => 'submit'),
  5693. '--before--',
  5694. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5695. '--after--',
  5696. '/div'
  5697. );
  5698. $this->assertTags($result, $expected);
  5699. }
  5700. /**
  5701. * test image submit types.
  5702. *
  5703. * @return void
  5704. */
  5705. public function testSubmitImage() {
  5706. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5707. $expected = array(
  5708. 'div' => array('class' => 'submit'),
  5709. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5710. '/div'
  5711. );
  5712. $this->assertTags($result, $expected);
  5713. $result = $this->Form->submit('/relative/cake.power.gif');
  5714. $expected = array(
  5715. 'div' => array('class' => 'submit'),
  5716. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5717. '/div'
  5718. );
  5719. $this->assertTags($result, $expected);
  5720. $result = $this->Form->submit('cake.power.gif');
  5721. $expected = array(
  5722. 'div' => array('class' => 'submit'),
  5723. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5724. '/div'
  5725. );
  5726. $this->assertTags($result, $expected);
  5727. $result = $this->Form->submit('Not.an.image');
  5728. $expected = array(
  5729. 'div' => array('class' => 'submit'),
  5730. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5731. '/div'
  5732. );
  5733. $this->assertTags($result, $expected);
  5734. $after = '--after--';
  5735. $before = '--before--';
  5736. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5737. $expected = array(
  5738. 'div' => array('class' => 'submit'),
  5739. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5740. '--after--',
  5741. '/div'
  5742. );
  5743. $this->assertTags($result, $expected);
  5744. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5745. $expected = array(
  5746. 'div' => array('class' => 'submit'),
  5747. '--before--',
  5748. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5749. '/div'
  5750. );
  5751. $this->assertTags($result, $expected);
  5752. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5753. $expected = array(
  5754. 'div' => array('class' => 'submit'),
  5755. '--before--',
  5756. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5757. '--after--',
  5758. '/div'
  5759. );
  5760. $this->assertTags($result, $expected);
  5761. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  5762. $expected = array(
  5763. 'div' => array('class' => 'submit'),
  5764. '--before--',
  5765. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5766. '--after--',
  5767. '/div'
  5768. );
  5769. $this->assertTags($result, $expected);
  5770. }
  5771. /**
  5772. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5773. * be submitted at a time.
  5774. *
  5775. * @return void
  5776. */
  5777. public function testSubmitUnlockedByDefault() {
  5778. $this->Form->request->params['_Token']['key'] = 'secured';
  5779. $this->Form->submit('Go go');
  5780. $this->Form->submit('Save', array('name' => 'save'));
  5781. $result = $this->Form->unlockField();
  5782. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5783. }
  5784. /**
  5785. * Test submit image with timestamps.
  5786. *
  5787. * @return void
  5788. */
  5789. public function testSubmitImageTimestamp() {
  5790. Configure::write('Asset.timestamp', 'force');
  5791. $result = $this->Form->submit('cake.power.gif');
  5792. $expected = array(
  5793. 'div' => array('class' => 'submit'),
  5794. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  5795. '/div'
  5796. );
  5797. $this->assertTags($result, $expected);
  5798. }
  5799. /**
  5800. * test the create() method
  5801. *
  5802. * @return void
  5803. */
  5804. public function testCreate() {
  5805. $result = $this->Form->create('Contact');
  5806. $encoding = strtolower(Configure::read('App.encoding'));
  5807. $expected = array(
  5808. 'form' => array(
  5809. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5810. 'accept-charset' => $encoding
  5811. ),
  5812. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  5813. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5814. '/div'
  5815. );
  5816. $this->assertTags($result, $expected);
  5817. $result = $this->Form->create('Contact', array('type' => 'GET'));
  5818. $expected = array('form' => array(
  5819. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5820. 'accept-charset' => $encoding
  5821. ));
  5822. $this->assertTags($result, $expected);
  5823. $result = $this->Form->create('Contact', array('type' => 'get'));
  5824. $expected = array('form' => array(
  5825. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5826. 'accept-charset' => $encoding
  5827. ));
  5828. $this->assertTags($result, $expected);
  5829. $result = $this->Form->create('Contact', array('type' => 'put'));
  5830. $expected = array(
  5831. 'form' => array(
  5832. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5833. 'accept-charset' => $encoding
  5834. ),
  5835. 'div' => array('style' => 'display:none;'),
  5836. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5837. '/div'
  5838. );
  5839. $this->assertTags($result, $expected);
  5840. $result = $this->Form->create('Contact', array('type' => 'file'));
  5841. $expected = array(
  5842. 'form' => array(
  5843. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5844. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5845. ),
  5846. 'div' => array('style' => 'display:none;'),
  5847. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5848. '/div'
  5849. );
  5850. $this->assertTags($result, $expected);
  5851. $this->Form->request->data['Contact']['id'] = 1;
  5852. $this->Form->request->here = '/contacts/edit/1';
  5853. $this->Form->request['action'] = 'edit';
  5854. $result = $this->Form->create('Contact');
  5855. $expected = array(
  5856. 'form' => array(
  5857. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5858. 'accept-charset' => $encoding
  5859. ),
  5860. 'div' => array('style' => 'display:none;'),
  5861. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5862. '/div'
  5863. );
  5864. $this->assertTags($result, $expected);
  5865. $this->Form->request->data['Contact']['id'] = 1;
  5866. $this->Form->request->here = '/contacts/edit/1';
  5867. $this->Form->request['action'] = 'edit';
  5868. $result = $this->Form->create('Contact', array('type' => 'file'));
  5869. $expected = array(
  5870. 'form' => array(
  5871. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5872. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5873. ),
  5874. 'div' => array('style' => 'display:none;'),
  5875. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5876. '/div'
  5877. );
  5878. $this->assertTags($result, $expected);
  5879. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  5880. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  5881. $expected = array(
  5882. 'form' => array(
  5883. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  5884. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  5885. ),
  5886. 'div' => array('style' => 'display:none;'),
  5887. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5888. '/div'
  5889. );
  5890. $this->assertTags($result, $expected);
  5891. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  5892. $expected = array(
  5893. 'form' => array(
  5894. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  5895. 'accept-charset' => $encoding
  5896. ),
  5897. 'div' => array('style' => 'display:none;'),
  5898. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5899. '/div'
  5900. );
  5901. $this->assertTags($result, $expected);
  5902. $this->Form->request['action'] = 'add';
  5903. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  5904. $expected = array(
  5905. 'form' => array(
  5906. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  5907. 'accept-charset' => $encoding
  5908. ),
  5909. 'div' => array('style' => 'display:none;'),
  5910. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5911. '/div'
  5912. );
  5913. $this->assertTags($result, $expected);
  5914. $result = $this->Form->create('User', array('action' => 'login'));
  5915. $expected = array(
  5916. 'form' => array(
  5917. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  5918. 'accept-charset' => $encoding
  5919. ),
  5920. 'div' => array('style' => 'display:none;'),
  5921. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5922. '/div'
  5923. );
  5924. $this->assertTags($result, $expected);
  5925. $result = $this->Form->create('User', array('url' => '/users/login'));
  5926. $expected = array(
  5927. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  5928. 'div' => array('style' => 'display:none;'),
  5929. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5930. '/div'
  5931. );
  5932. $this->assertTags($result, $expected);
  5933. $this->Form->request['controller'] = 'pages';
  5934. $result = $this->Form->create('User', array('action' => 'signup'));
  5935. $expected = array(
  5936. 'form' => array(
  5937. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  5938. 'accept-charset' => $encoding
  5939. ),
  5940. 'div' => array('style' => 'display:none;'),
  5941. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5942. '/div'
  5943. );
  5944. $this->assertTags($result, $expected);
  5945. $this->Form->request->data = array();
  5946. $this->Form->request['controller'] = 'contacts';
  5947. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  5948. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  5949. $expected = array(
  5950. 'form' => array(
  5951. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  5952. 'accept-charset' => 'utf-8'
  5953. ),
  5954. 'div' => array('style' => 'display:none;'),
  5955. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5956. '/div'
  5957. );
  5958. $this->assertTags($result, $expected);
  5959. }
  5960. /**
  5961. * Test the onsubmit option for create()
  5962. *
  5963. * @return void
  5964. */
  5965. public function testCreateOnSubmit() {
  5966. $this->Form->request->data = array();
  5967. $this->Form->request['controller'] = 'contacts';
  5968. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  5969. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  5970. $expected = array(
  5971. 'form' => array(
  5972. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  5973. 'accept-charset' => 'utf-8'
  5974. ),
  5975. 'div' => array('style' => 'display:none;'),
  5976. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5977. '/div'
  5978. );
  5979. $this->assertTags($result, $expected);
  5980. $this->Form->request->data = array();
  5981. $this->Form->request['controller'] = 'contacts';
  5982. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  5983. $result = $this->Form->create(array(
  5984. 'url' => array('action' => 'index', 'param'),
  5985. 'default' => false,
  5986. 'onsubmit' => 'someFunction();'
  5987. ));
  5988. $expected = array(
  5989. 'form' => array(
  5990. 'id' => 'ContactAddForm', 'method' => 'post',
  5991. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  5992. 'action' => '/contacts/index/param',
  5993. 'accept-charset' => 'utf-8'
  5994. ),
  5995. 'div' => array('style' => 'display:none;'),
  5996. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5997. '/div'
  5998. );
  5999. $this->assertTags($result, $expected);
  6000. }
  6001. /**
  6002. * test create() with automatic url generation
  6003. *
  6004. * @return void
  6005. */
  6006. public function testCreateAutoUrl() {
  6007. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6008. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6009. $this->Form->request->base = '/base_url';
  6010. $result = $this->Form->create('Contact');
  6011. $expected = array(
  6012. 'form' => array(
  6013. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6014. 'accept-charset' => 'utf-8'
  6015. ),
  6016. 'div' => array('style' => 'display:none;'),
  6017. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6018. '/div'
  6019. );
  6020. $this->assertTags($result, $expected);
  6021. $this->Form->request['action'] = 'delete';
  6022. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6023. $this->Form->request->base = '/base_url';
  6024. $result = $this->Form->create('Contact');
  6025. $expected = array(
  6026. 'form' => array(
  6027. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6028. 'accept-charset' => 'utf-8'
  6029. ),
  6030. 'div' => array('style' => 'display:none;'),
  6031. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6032. '/div'
  6033. );
  6034. $this->assertTags($result, $expected);
  6035. }
  6036. /**
  6037. * test create() with a custom route
  6038. *
  6039. * @return void
  6040. */
  6041. public function testCreateCustomRoute() {
  6042. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6043. $encoding = strtolower(Configure::read('App.encoding'));
  6044. $result = $this->Form->create('User', array('action' => 'login'));
  6045. $expected = array(
  6046. 'form' => array(
  6047. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6048. 'accept-charset' => $encoding
  6049. ),
  6050. 'div' => array('style' => 'display:none;'),
  6051. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6052. '/div'
  6053. );
  6054. $this->assertTags($result, $expected);
  6055. }
  6056. /**
  6057. * test that inputDefaults are stored and used.
  6058. *
  6059. * @return void
  6060. */
  6061. public function testCreateWithInputDefaults() {
  6062. $this->Form->create('User', array(
  6063. 'inputDefaults' => array(
  6064. 'div' => false,
  6065. 'label' => false,
  6066. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error'))
  6067. )
  6068. ));
  6069. $result = $this->Form->input('username');
  6070. $expected = array(
  6071. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6072. );
  6073. $this->assertTags($result, $expected);
  6074. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6075. $expected = array(
  6076. 'div' => array('class' => 'input text'),
  6077. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6078. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6079. '/div'
  6080. );
  6081. $this->assertTags($result, $expected);
  6082. $User = ClassRegistry::getObject('User');
  6083. $User->validationErrors['username'] = array('empty');
  6084. $result = $this->Form->input('username', array('div' => true, 'label' => 'username', 'error' => array('empty' => __('Required'))));
  6085. $expected = array(
  6086. 'div' => array('class' => 'input text error'),
  6087. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6088. 'input' => array('class' => 'form-error', 'type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6089. 'small' => array('class' => 'error'), 'Required', '/small',
  6090. '/div'
  6091. );
  6092. $this->assertTags($result, $expected);
  6093. }
  6094. /**
  6095. * test automatic accept-charset overriding
  6096. *
  6097. * @return void
  6098. */
  6099. public function testCreateWithAcceptCharset() {
  6100. $result = $this->Form->create('UserForm', array(
  6101. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  6102. )
  6103. );
  6104. $expected = array(
  6105. 'form' => array(
  6106. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6107. 'accept-charset' => 'iso-8859-1'
  6108. ),
  6109. 'div' => array('style' => 'display:none;'),
  6110. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6111. '/div'
  6112. );
  6113. $this->assertTags($result, $expected);
  6114. }
  6115. /**
  6116. * Test base form url when url param is passed with multiple parameters (&)
  6117. *
  6118. */
  6119. public function testCreateQuerystringrequest() {
  6120. $encoding = strtolower(Configure::read('App.encoding'));
  6121. $result = $this->Form->create('Contact', array(
  6122. 'type' => 'post',
  6123. 'escape' => false,
  6124. 'url' => array(
  6125. 'controller' => 'controller',
  6126. 'action' => 'action',
  6127. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6128. )
  6129. ));
  6130. $expected = array(
  6131. 'form' => array(
  6132. 'id' => 'ContactAddForm',
  6133. 'method' => 'post',
  6134. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6135. 'accept-charset' => $encoding
  6136. ),
  6137. 'div' => array('style' => 'display:none;'),
  6138. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6139. '/div'
  6140. );
  6141. $this->assertTags($result, $expected);
  6142. $result = $this->Form->create('Contact', array(
  6143. 'type' => 'post',
  6144. 'url' => array(
  6145. 'controller' => 'controller',
  6146. 'action' => 'action',
  6147. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6148. )
  6149. ));
  6150. $expected = array(
  6151. 'form' => array(
  6152. 'id' => 'ContactAddForm',
  6153. 'method' => 'post',
  6154. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6155. 'accept-charset' => $encoding
  6156. ),
  6157. 'div' => array('style' => 'display:none;'),
  6158. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6159. '/div'
  6160. );
  6161. $this->assertTags($result, $expected);
  6162. }
  6163. /**
  6164. * test that create() doesn't cause errors by multiple id's being in the primary key
  6165. * as could happen with multiple select or checkboxes.
  6166. *
  6167. * @return void
  6168. */
  6169. public function testCreateWithMultipleIdInData() {
  6170. $encoding = strtolower(Configure::read('App.encoding'));
  6171. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6172. $result = $this->Form->create('Contact');
  6173. $expected = array(
  6174. 'form' => array(
  6175. 'id' => 'ContactAddForm',
  6176. 'method' => 'post',
  6177. 'action' => '/contacts/add',
  6178. 'accept-charset' => $encoding
  6179. ),
  6180. 'div' => array('style' => 'display:none;'),
  6181. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6182. '/div'
  6183. );
  6184. $this->assertTags($result, $expected);
  6185. }
  6186. /**
  6187. * test that create() doesn't add in extra passed params.
  6188. *
  6189. * @return void
  6190. */
  6191. public function testCreatePassedArgs() {
  6192. $encoding = strtolower(Configure::read('App.encoding'));
  6193. $this->Form->request->data['Contact']['id'] = 1;
  6194. $result = $this->Form->create('Contact', array(
  6195. 'type' => 'post',
  6196. 'escape' => false,
  6197. 'url' => array(
  6198. 'action' => 'edit',
  6199. 'myparam'
  6200. )
  6201. ));
  6202. $expected = array(
  6203. 'form' => array(
  6204. 'id' => 'ContactAddForm',
  6205. 'method' => 'post',
  6206. 'action' => '/contacts/edit/myparam',
  6207. 'accept-charset' => $encoding
  6208. ),
  6209. 'div' => array('style' => 'display:none;'),
  6210. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6211. '/div'
  6212. );
  6213. $this->assertTags($result, $expected);
  6214. }
  6215. /**
  6216. * test creating a get form, and get form inputs.
  6217. *
  6218. * @return void
  6219. */
  6220. public function testGetFormCreate() {
  6221. $encoding = strtolower(Configure::read('App.encoding'));
  6222. $result = $this->Form->create('Contact', array('type' => 'get'));
  6223. $this->assertTags($result, array('form' => array(
  6224. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6225. 'accept-charset' => $encoding
  6226. )));
  6227. $result = $this->Form->text('Contact.name');
  6228. $this->assertTags($result, array('input' => array(
  6229. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6230. )));
  6231. $result = $this->Form->password('password');
  6232. $this->assertTags($result, array('input' => array(
  6233. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6234. )));
  6235. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6236. $result = $this->Form->text('user_form');
  6237. $this->assertTags($result, array('input' => array(
  6238. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6239. )));
  6240. }
  6241. /**
  6242. * test get form, and inputs when the model param is false
  6243. *
  6244. * @return void
  6245. */
  6246. public function testGetFormWithFalseModel() {
  6247. $encoding = strtolower(Configure::read('App.encoding'));
  6248. $this->Form->request['controller'] = 'contact_test';
  6249. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6250. $expected = array('form' => array(
  6251. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6252. 'accept-charset' => $encoding
  6253. ));
  6254. $this->assertTags($result, $expected);
  6255. $result = $this->Form->text('reason');
  6256. $expected = array(
  6257. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6258. );
  6259. $this->assertTags($result, $expected);
  6260. }
  6261. /**
  6262. * test that datetime() works with GET style forms.
  6263. *
  6264. * @return void
  6265. */
  6266. public function testDateTimeWithGetForms() {
  6267. extract($this->dateRegex);
  6268. $this->Form->create('Contact', array('type' => 'get'));
  6269. $result = $this->Form->datetime('created');
  6270. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6271. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6272. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6273. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6274. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6275. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6276. }
  6277. /**
  6278. * testEditFormWithData method
  6279. *
  6280. * test auto populating form elements from submitted data.
  6281. *
  6282. * @return void
  6283. */
  6284. public function testEditFormWithData() {
  6285. $this->Form->request->data = array('Person' => array(
  6286. 'id' => 1,
  6287. 'first_name' => 'Nate',
  6288. 'last_name' => 'Abele',
  6289. 'email' => 'nate@example.com'
  6290. ));
  6291. $this->Form->request->addParams(array(
  6292. 'models' => array('Person'),
  6293. 'controller' => 'people',
  6294. 'action' => 'add'
  6295. ));
  6296. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6297. $this->Form->create();
  6298. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6299. $expected = array(
  6300. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6301. 'select' => array(
  6302. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6303. ),
  6304. array('option' => array('value' => 1)), 'Nate', '/option',
  6305. array('option' => array('value' => 2)), 'Garrett', '/option',
  6306. array('option' => array('value' => 3)), 'Larry', '/option',
  6307. '/select'
  6308. );
  6309. $this->assertTags($result, $expected);
  6310. }
  6311. /**
  6312. * Test that required fields are created for various types of validation.
  6313. *
  6314. * @return void
  6315. */
  6316. public function testFormInputRequiredDetection() {
  6317. $this->Form->create('Contact');
  6318. $result = $this->Form->input('Contact.non_existing');
  6319. $expected = array(
  6320. 'div' => array('class' => 'input text required'),
  6321. 'label' => array('for' => 'ContactNonExisting'),
  6322. 'Non Existing',
  6323. '/label',
  6324. 'input' => array(
  6325. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  6326. 'id' => 'ContactNonExisting'
  6327. ),
  6328. '/div'
  6329. );
  6330. $this->assertTags($result, $expected);
  6331. $result = $this->Form->input('Contact.imrequired');
  6332. $expected = array(
  6333. 'div' => array('class' => 'input text required'),
  6334. 'label' => array('for' => 'ContactImrequired'),
  6335. 'Imrequired',
  6336. '/label',
  6337. 'input' => array(
  6338. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  6339. 'id' => 'ContactImrequired'
  6340. ),
  6341. '/div'
  6342. );
  6343. $this->assertTags($result, $expected);
  6344. $result = $this->Form->input('Contact.imalsorequired');
  6345. $expected = array(
  6346. 'div' => array('class' => 'input text required'),
  6347. 'label' => array('for' => 'ContactImalsorequired'),
  6348. 'Imalsorequired',
  6349. '/label',
  6350. 'input' => array(
  6351. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  6352. 'id' => 'ContactImalsorequired'
  6353. ),
  6354. '/div'
  6355. );
  6356. $this->assertTags($result, $expected);
  6357. $result = $this->Form->input('Contact.imrequiredtoo');
  6358. $expected = array(
  6359. 'div' => array('class' => 'input text required'),
  6360. 'label' => array('for' => 'ContactImrequiredtoo'),
  6361. 'Imrequiredtoo',
  6362. '/label',
  6363. 'input' => array(
  6364. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  6365. 'id' => 'ContactImrequiredtoo'
  6366. ),
  6367. '/div'
  6368. );
  6369. $this->assertTags($result, $expected);
  6370. $result = $this->Form->input('Contact.required_one');
  6371. $expected = array(
  6372. 'div' => array('class' => 'input text required'),
  6373. 'label' => array('for' => 'ContactRequiredOne'),
  6374. 'Required One',
  6375. '/label',
  6376. 'input' => array(
  6377. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  6378. 'id' => 'ContactRequiredOne'
  6379. ),
  6380. '/div'
  6381. );
  6382. $this->assertTags($result, $expected);
  6383. $result = $this->Form->input('Contact.string_required');
  6384. $expected = array(
  6385. 'div' => array('class' => 'input text required'),
  6386. 'label' => array('for' => 'ContactStringRequired'),
  6387. 'String Required',
  6388. '/label',
  6389. 'input' => array(
  6390. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  6391. 'id' => 'ContactStringRequired'
  6392. ),
  6393. '/div'
  6394. );
  6395. $this->assertTags($result, $expected);
  6396. $result = $this->Form->input('Contact.imnotrequired');
  6397. $expected = array(
  6398. 'div' => array('class' => 'input text'),
  6399. 'label' => array('for' => 'ContactImnotrequired'),
  6400. 'Imnotrequired',
  6401. '/label',
  6402. 'input' => array(
  6403. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  6404. 'id' => 'ContactImnotrequired'
  6405. ),
  6406. '/div'
  6407. );
  6408. $this->assertTags($result, $expected);
  6409. $result = $this->Form->input('Contact.imalsonotrequired');
  6410. $expected = array(
  6411. 'div' => array('class' => 'input text'),
  6412. 'label' => array('for' => 'ContactImalsonotrequired'),
  6413. 'Imalsonotrequired',
  6414. '/label',
  6415. 'input' => array(
  6416. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  6417. 'id' => 'ContactImalsonotrequired'
  6418. ),
  6419. '/div'
  6420. );
  6421. $this->assertTags($result, $expected);
  6422. $result = $this->Form->input('Contact.imnotrequiredeither');
  6423. $expected = array(
  6424. 'div' => array('class' => 'input text'),
  6425. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6426. 'Imnotrequiredeither',
  6427. '/label',
  6428. 'input' => array(
  6429. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  6430. 'id' => 'ContactImnotrequiredeither'
  6431. ),
  6432. '/div'
  6433. );
  6434. $this->assertTags($result, $expected);
  6435. }
  6436. /**
  6437. * testFormMagicInput method
  6438. *
  6439. * @return void
  6440. */
  6441. public function testFormMagicInput() {
  6442. $encoding = strtolower(Configure::read('App.encoding'));
  6443. $result = $this->Form->create('Contact');
  6444. $expected = array(
  6445. 'form' => array(
  6446. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6447. 'accept-charset' => $encoding
  6448. ),
  6449. 'div' => array('style' => 'display:none;'),
  6450. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6451. '/div'
  6452. );
  6453. $this->assertTags($result, $expected);
  6454. $result = $this->Form->input('name');
  6455. $expected = array(
  6456. 'div' => array('class' => 'input text'),
  6457. 'label' => array('for' => 'ContactName'),
  6458. 'Name',
  6459. '/label',
  6460. 'input' => array(
  6461. 'type' => 'text', 'name' => 'data[Contact][name]',
  6462. 'id' => 'ContactName', 'maxlength' => '255'
  6463. ),
  6464. '/div'
  6465. );
  6466. $this->assertTags($result, $expected);
  6467. $result = $this->Form->input('non_existing_field_in_contact_model');
  6468. $expected = array(
  6469. 'div' => array('class' => 'input text'),
  6470. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  6471. 'Non Existing Field In Contact Model',
  6472. '/label',
  6473. 'input' => array(
  6474. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  6475. 'id' => 'ContactNonExistingFieldInContactModel'
  6476. ),
  6477. '/div'
  6478. );
  6479. $this->assertTags($result, $expected);
  6480. $result = $this->Form->input('Address.street');
  6481. $expected = array(
  6482. 'div' => array('class' => 'input text'),
  6483. 'label' => array('for' => 'AddressStreet'),
  6484. 'Street',
  6485. '/label',
  6486. 'input' => array(
  6487. 'type' => 'text', 'name' => 'data[Address][street]',
  6488. 'id' => 'AddressStreet'
  6489. ),
  6490. '/div'
  6491. );
  6492. $this->assertTags($result, $expected);
  6493. $result = $this->Form->input('Address.non_existing_field_in_model');
  6494. $expected = array(
  6495. 'div' => array('class' => 'input text'),
  6496. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  6497. 'Non Existing Field In Model',
  6498. '/label',
  6499. 'input' => array(
  6500. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  6501. 'id' => 'AddressNonExistingFieldInModel'
  6502. ),
  6503. '/div'
  6504. );
  6505. $this->assertTags($result, $expected);
  6506. $result = $this->Form->input('name', array('div' => false));
  6507. $expected = array(
  6508. 'label' => array('for' => 'ContactName'),
  6509. 'Name',
  6510. '/label',
  6511. 'input' => array(
  6512. 'type' => 'text', 'name' => 'data[Contact][name]',
  6513. 'id' => 'ContactName', 'maxlength' => '255'
  6514. )
  6515. );
  6516. $this->assertTags($result, $expected);
  6517. extract($this->dateRegex);
  6518. $now = strtotime('now');
  6519. $result = $this->Form->input('Contact.published', array('div' => false));
  6520. $expected = array(
  6521. 'label' => array('for' => 'ContactPublishedMonth'),
  6522. 'Published',
  6523. '/label',
  6524. array('select' => array(
  6525. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  6526. )),
  6527. $monthsRegex,
  6528. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6529. date('F', $now),
  6530. '/option',
  6531. '*/select',
  6532. '-',
  6533. array('select' => array(
  6534. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  6535. )),
  6536. $daysRegex,
  6537. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6538. date('j', $now),
  6539. '/option',
  6540. '*/select',
  6541. '-',
  6542. array('select' => array(
  6543. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  6544. )),
  6545. $yearsRegex,
  6546. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6547. date('Y', $now),
  6548. '*/select'
  6549. );
  6550. $this->assertTags($result, $expected);
  6551. $result = $this->Form->input('Contact.updated', array('div' => false));
  6552. $expected = array(
  6553. 'label' => array('for' => 'ContactUpdatedMonth'),
  6554. 'Updated',
  6555. '/label',
  6556. array('select' => array(
  6557. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  6558. )),
  6559. $monthsRegex,
  6560. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6561. date('F', $now),
  6562. '/option',
  6563. '*/select',
  6564. '-',
  6565. array('select' => array(
  6566. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  6567. )),
  6568. $daysRegex,
  6569. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6570. date('j', $now),
  6571. '/option',
  6572. '*/select',
  6573. '-',
  6574. array('select' => array(
  6575. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  6576. )),
  6577. $yearsRegex,
  6578. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6579. date('Y', $now),
  6580. '*/select'
  6581. );
  6582. $this->assertTags($result, $expected);
  6583. $result = $this->Form->input('UserForm.stuff');
  6584. $expected = array(
  6585. 'div' => array('class' => 'input text'),
  6586. 'label' => array('for' => 'UserFormStuff'),
  6587. 'Stuff',
  6588. '/label',
  6589. 'input' => array(
  6590. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  6591. 'id' => 'UserFormStuff', 'maxlength' => 10
  6592. ),
  6593. '/div'
  6594. );
  6595. $this->assertTags($result, $expected);
  6596. }
  6597. /**
  6598. * testForMagicInputNonExistingNorValidated method
  6599. *
  6600. * @return void
  6601. */
  6602. public function testForMagicInputNonExistingNorValidated() {
  6603. $encoding = strtolower(Configure::read('App.encoding'));
  6604. $result = $this->Form->create('Contact');
  6605. $expected = array(
  6606. 'form' => array(
  6607. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6608. 'accept-charset' => $encoding
  6609. ),
  6610. 'div' => array('style' => 'display:none;'),
  6611. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6612. '/div'
  6613. );
  6614. $this->assertTags($result, $expected);
  6615. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6616. $expected = array(
  6617. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6618. 'Non Existing Nor Validated',
  6619. '/label',
  6620. 'input' => array(
  6621. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6622. 'id' => 'ContactNonExistingNorValidated'
  6623. )
  6624. );
  6625. $this->assertTags($result, $expected);
  6626. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6627. 'div' => false, 'value' => 'my value'
  6628. ));
  6629. $expected = array(
  6630. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6631. 'Non Existing Nor Validated',
  6632. '/label',
  6633. 'input' => array(
  6634. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6635. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6636. )
  6637. );
  6638. $this->assertTags($result, $expected);
  6639. $this->Form->request->data = array(
  6640. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6641. ));
  6642. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6643. $expected = array(
  6644. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6645. 'Non Existing Nor Validated',
  6646. '/label',
  6647. 'input' => array(
  6648. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6649. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6650. )
  6651. );
  6652. $this->assertTags($result, $expected);
  6653. }
  6654. /**
  6655. * testFormMagicInputLabel method
  6656. *
  6657. * @return void
  6658. */
  6659. public function testFormMagicInputLabel() {
  6660. $encoding = strtolower(Configure::read('App.encoding'));
  6661. $result = $this->Form->create('Contact');
  6662. $expected = array(
  6663. 'form' => array(
  6664. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6665. 'accept-charset' => $encoding
  6666. ),
  6667. 'div' => array('style' => 'display:none;'),
  6668. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6669. '/div'
  6670. );
  6671. $this->assertTags($result, $expected);
  6672. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  6673. $this->assertTags($result, array('input' => array(
  6674. 'name' => 'data[Contact][name]', 'type' => 'text',
  6675. 'id' => 'ContactName', 'maxlength' => '255')
  6676. ));
  6677. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  6678. $expected = array(
  6679. 'label' => array('for' => 'ContactName'),
  6680. 'My label',
  6681. '/label',
  6682. 'input' => array(
  6683. 'type' => 'text', 'name' => 'data[Contact][name]',
  6684. 'id' => 'ContactName', 'maxlength' => '255'
  6685. )
  6686. );
  6687. $this->assertTags($result, $expected);
  6688. $result = $this->Form->input('Contact.name', array(
  6689. 'div' => false, 'label' => array('class' => 'mandatory')
  6690. ));
  6691. $expected = array(
  6692. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6693. 'Name',
  6694. '/label',
  6695. 'input' => array(
  6696. 'type' => 'text', 'name' => 'data[Contact][name]',
  6697. 'id' => 'ContactName', 'maxlength' => '255'
  6698. )
  6699. );
  6700. $this->assertTags($result, $expected);
  6701. $result = $this->Form->input('Contact.name', array(
  6702. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  6703. ));
  6704. $expected = array(
  6705. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6706. 'My label',
  6707. '/label',
  6708. 'input' => array(
  6709. 'type' => 'text', 'name' => 'data[Contact][name]',
  6710. 'id' => 'ContactName', 'maxlength' => '255'
  6711. )
  6712. );
  6713. $this->assertTags($result, $expected);
  6714. $result = $this->Form->input('Contact.name', array(
  6715. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  6716. ));
  6717. $expected = array(
  6718. 'label' => array('for' => 'my_id'),
  6719. 'Name',
  6720. '/label',
  6721. 'input' => array(
  6722. 'type' => 'text', 'name' => 'data[Contact][name]',
  6723. 'id' => 'my_id', 'maxlength' => '255'
  6724. )
  6725. );
  6726. $this->assertTags($result, $expected);
  6727. $result = $this->Form->input('1.id');
  6728. $this->assertTags($result, array('input' => array(
  6729. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6730. 'id' => 'Contact1Id'
  6731. )));
  6732. $result = $this->Form->input("1.name");
  6733. $expected = array(
  6734. 'div' => array('class' => 'input text'),
  6735. 'label' => array('for' => 'Contact1Name'),
  6736. 'Name',
  6737. '/label',
  6738. 'input' => array(
  6739. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  6740. 'id' => 'Contact1Name', 'maxlength' => '255'
  6741. ),
  6742. '/div'
  6743. );
  6744. $this->assertTags($result, $expected);
  6745. $result = $this->Form->input('Contact.1.id');
  6746. $this->assertTags($result, array(
  6747. 'input' => array(
  6748. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6749. 'id' => 'Contact1Id'
  6750. )
  6751. ));
  6752. $result = $this->Form->input("Model.1.name");
  6753. $expected = array(
  6754. 'div' => array('class' => 'input text'),
  6755. 'label' => array('for' => 'Model1Name'),
  6756. 'Name',
  6757. '/label',
  6758. 'input' => array(
  6759. 'type' => 'text', 'name' => 'data[Model][1][name]',
  6760. 'id' => 'Model1Name'
  6761. ),
  6762. '/div'
  6763. );
  6764. $this->assertTags($result, $expected);
  6765. }
  6766. /**
  6767. * testFormEnd method
  6768. *
  6769. * @return void
  6770. */
  6771. public function testFormEnd() {
  6772. $this->assertEquals($this->Form->end(), '</form>');
  6773. $result = $this->Form->end('');
  6774. $expected = array(
  6775. 'div' => array('class' => 'submit'),
  6776. 'input' => array('type' => 'submit', 'value' => ''),
  6777. '/div',
  6778. '/form'
  6779. );
  6780. $this->assertTags($result, $expected);
  6781. $result = $this->Form->end(array('label' => ''));
  6782. $expected = array(
  6783. 'div' => array('class' => 'submit'),
  6784. 'input' => array('type' => 'submit', 'value' => ''),
  6785. '/div',
  6786. '/form'
  6787. );
  6788. $this->assertTags($result, $expected);
  6789. $result = $this->Form->end('save');
  6790. $expected = array(
  6791. 'div' => array('class' => 'submit'),
  6792. 'input' => array('type' => 'submit', 'value' => 'save'),
  6793. '/div',
  6794. '/form'
  6795. );
  6796. $this->assertTags($result, $expected);
  6797. $result = $this->Form->end(array('label' => 'save'));
  6798. $expected = array(
  6799. 'div' => array('class' => 'submit'),
  6800. 'input' => array('type' => 'submit', 'value' => 'save'),
  6801. '/div',
  6802. '/form'
  6803. );
  6804. $this->assertTags($result, $expected);
  6805. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  6806. $expected = array(
  6807. 'div' => array('class' => 'submit'),
  6808. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6809. '/div',
  6810. '/form'
  6811. );
  6812. $this->assertTags($result, $expected);
  6813. $result = $this->Form->end(array('name' => 'Whatever'));
  6814. $expected = array(
  6815. 'div' => array('class' => 'submit'),
  6816. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  6817. '/div',
  6818. '/form'
  6819. );
  6820. $this->assertTags($result, $expected);
  6821. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  6822. $expected = array(
  6823. 'div' => array('class' => 'good'),
  6824. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6825. '/div',
  6826. '/form'
  6827. );
  6828. $this->assertTags($result, $expected);
  6829. $result = $this->Form->end(array(
  6830. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  6831. ));
  6832. $expected = array(
  6833. 'div' => array('class' => 'good'),
  6834. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6835. '/div',
  6836. '/form'
  6837. );
  6838. $this->assertTags($result, $expected);
  6839. }
  6840. /**
  6841. * testMultipleFormWithIdFields method
  6842. *
  6843. * @return void
  6844. */
  6845. public function testMultipleFormWithIdFields() {
  6846. $this->Form->create('UserForm');
  6847. $result = $this->Form->input('id');
  6848. $this->assertTags($result, array('input' => array(
  6849. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  6850. )));
  6851. $result = $this->Form->input('ValidateItem.id');
  6852. $this->assertTags($result, array('input' => array(
  6853. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  6854. 'id' => 'ValidateItemId'
  6855. )));
  6856. $result = $this->Form->input('ValidateUser.id');
  6857. $this->assertTags($result, array('input' => array(
  6858. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  6859. 'id' => 'ValidateUserId'
  6860. )));
  6861. }
  6862. /**
  6863. * testDbLessModel method
  6864. *
  6865. * @return void
  6866. */
  6867. public function testDbLessModel() {
  6868. $this->Form->create('TestMail');
  6869. $result = $this->Form->input('name');
  6870. $expected = array(
  6871. 'div' => array('class' => 'input text'),
  6872. 'label' => array('for' => 'TestMailName'),
  6873. 'Name',
  6874. '/label',
  6875. 'input' => array(
  6876. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6877. 'id' => 'TestMailName'
  6878. ),
  6879. '/div'
  6880. );
  6881. $this->assertTags($result, $expected);
  6882. ClassRegistry::init('TestMail');
  6883. $this->Form->create('TestMail');
  6884. $result = $this->Form->input('name');
  6885. $expected = array(
  6886. 'div' => array('class' => 'input text'),
  6887. 'label' => array('for' => 'TestMailName'),
  6888. 'Name',
  6889. '/label',
  6890. 'input' => array(
  6891. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6892. 'id' => 'TestMailName'
  6893. ),
  6894. '/div'
  6895. );
  6896. $this->assertTags($result, $expected);
  6897. }
  6898. /**
  6899. * testBrokenness method
  6900. *
  6901. * @return void
  6902. */
  6903. public function testBrokenness() {
  6904. /*
  6905. * #4 This test has two parents and four children. By default (as of r7117) both
  6906. * parents are show but the first parent is missing a child. This is the inconsistency
  6907. * in the default behaviour - one parent has all children, the other does not - dependent
  6908. * on the data values.
  6909. */
  6910. $result = $this->Form->select('Model.field', array(
  6911. 'Fred' => array(
  6912. 'freds_son_1' => 'Fred',
  6913. 'freds_son_2' => 'Freddie'
  6914. ),
  6915. 'Bert' => array(
  6916. 'berts_son_1' => 'Albert',
  6917. 'berts_son_2' => 'Bertie')
  6918. ),
  6919. array('showParents' => true, 'empty' => false)
  6920. );
  6921. $expected = array(
  6922. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6923. array('optgroup' => array('label' => 'Fred')),
  6924. array('option' => array('value' => 'freds_son_1')),
  6925. 'Fred',
  6926. '/option',
  6927. array('option' => array('value' => 'freds_son_2')),
  6928. 'Freddie',
  6929. '/option',
  6930. '/optgroup',
  6931. array('optgroup' => array('label' => 'Bert')),
  6932. array('option' => array('value' => 'berts_son_1')),
  6933. 'Albert',
  6934. '/option',
  6935. array('option' => array('value' => 'berts_son_2')),
  6936. 'Bertie',
  6937. '/option',
  6938. '/optgroup',
  6939. '/select'
  6940. );
  6941. $this->assertTags($result, $expected);
  6942. /*
  6943. * #2 This is structurally identical to the test above (#1) - only the parent name has
  6944. * changed, so we should expect the same select list data, just with a different name
  6945. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  6946. * This is where data corruption can occur, because when a select value is missing from
  6947. * a list a form will substitute the first value in the list - without the user knowing.
  6948. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  6949. * affect the availability of 3 => 'Three' as a valid option.
  6950. */
  6951. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  6952. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  6953. ));
  6954. $result = $this->Form->select(
  6955. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  6956. );
  6957. $expected = array(
  6958. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6959. array('option' => array('value' => 1)),
  6960. 'One',
  6961. '/option',
  6962. array('option' => array('value' => 2)),
  6963. 'Two',
  6964. '/option',
  6965. array('optgroup' => array('label' => 'Three')),
  6966. array('option' => array('value' => 3)),
  6967. 'Three',
  6968. '/option',
  6969. array('option' => array('value' => 4)),
  6970. 'Four',
  6971. '/option',
  6972. array('option' => array('value' => 5)),
  6973. 'Five',
  6974. '/option',
  6975. '/optgroup',
  6976. '/select'
  6977. );
  6978. $this->assertTags($result, $expected);
  6979. }
  6980. /**
  6981. * Test the generation of fields for a multi record form.
  6982. *
  6983. * @return void
  6984. */
  6985. public function testMultiRecordForm() {
  6986. $this->Form->create('ValidateProfile');
  6987. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  6988. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  6989. $expected = array(
  6990. 'div' => array('class' => 'input textarea'),
  6991. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  6992. 'Name',
  6993. '/label',
  6994. 'textarea' => array(
  6995. 'id' => 'ValidateProfile1ValidateItem2Name',
  6996. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  6997. 'cols' => 30,
  6998. 'rows' => 6
  6999. ),
  7000. 'Value',
  7001. '/textarea',
  7002. '/div'
  7003. );
  7004. $this->assertTags($result, $expected);
  7005. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
  7006. $expected = array(
  7007. 'div' => array('class' => 'input date'),
  7008. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7009. 'Created',
  7010. '/label',
  7011. array('select' => array(
  7012. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  7013. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7014. )
  7015. ),
  7016. array('option' => array('value' => '')), '/option',
  7017. $this->dateRegex['monthsRegex'],
  7018. '/select', '-',
  7019. array('select' => array(
  7020. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  7021. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7022. )
  7023. ),
  7024. array('option' => array('value' => '')), '/option',
  7025. $this->dateRegex['daysRegex'],
  7026. '/select', '-',
  7027. array('select' => array(
  7028. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  7029. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7030. )
  7031. ),
  7032. array('option' => array('value' => '')), '/option',
  7033. $this->dateRegex['yearsRegex'],
  7034. '/select',
  7035. '/div'
  7036. );
  7037. $this->assertTags($result, $expected);
  7038. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7039. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7040. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7041. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7042. $expected = array(
  7043. 'div' => array('class' => 'input select error'),
  7044. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7045. 'Profile',
  7046. '/label',
  7047. 'select' => array(
  7048. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7049. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7050. 'class' => 'form-error'
  7051. ),
  7052. '/select',
  7053. array('div' => array('class' => 'error-message')),
  7054. 'Error',
  7055. '/div',
  7056. '/div'
  7057. );
  7058. $this->assertTags($result, $expected);
  7059. }
  7060. /**
  7061. * test the correct display of multi-record form validation errors.
  7062. *
  7063. * @return void
  7064. */
  7065. public function testMultiRecordFormValidationErrors() {
  7066. $this->Form->create('ValidateProfile');
  7067. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7068. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7069. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7070. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7071. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7072. $result = $this->Form->error('ValidateProfile.2.city');
  7073. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7074. $result = $this->Form->error('2.city');
  7075. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7076. }
  7077. /**
  7078. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7079. *
  7080. * @return void
  7081. */
  7082. public function testInputTemplate() {
  7083. $result = $this->Form->input('Contact.email', array(
  7084. 'type' => 'text', 'format' => array('input')
  7085. ));
  7086. $expected = array(
  7087. 'div' => array('class' => 'input text'),
  7088. 'input' => array(
  7089. 'type' => 'text', 'name' => 'data[Contact][email]',
  7090. 'id' => 'ContactEmail'
  7091. ),
  7092. '/div'
  7093. );
  7094. $this->assertTags($result, $expected);
  7095. $result = $this->Form->input('Contact.email', array(
  7096. 'type' => 'text', 'format' => array('input', 'label'),
  7097. 'label' => '<em>Email (required)</em>'
  7098. ));
  7099. $expected = array(
  7100. 'div' => array('class' => 'input text'),
  7101. array('input' => array(
  7102. 'type' => 'text', 'name' => 'data[Contact][email]',
  7103. 'id' => 'ContactEmail'
  7104. )),
  7105. 'label' => array('for' => 'ContactEmail'),
  7106. 'em' => array(),
  7107. 'Email (required)',
  7108. '/em',
  7109. '/label',
  7110. '/div'
  7111. );
  7112. $this->assertTags($result, $expected);
  7113. $result = $this->Form->input('Contact.email', array(
  7114. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7115. 'between' => '<div>Something in the middle</div>',
  7116. 'after' => '<span>Some text at the end</span>'
  7117. ));
  7118. $expected = array(
  7119. 'div' => array('class' => 'input text'),
  7120. array('input' => array(
  7121. 'type' => 'text', 'name' => 'data[Contact][email]',
  7122. 'id' => 'ContactEmail'
  7123. )),
  7124. array('div' => array()),
  7125. 'Something in the middle',
  7126. '/div',
  7127. 'label' => array('for' => 'ContactEmail'),
  7128. 'Email',
  7129. '/label',
  7130. 'span' => array(),
  7131. 'Some text at the end',
  7132. '/span',
  7133. '/div'
  7134. );
  7135. $this->assertTags($result, $expected);
  7136. $result = $this->Form->input('Contact.method', array(
  7137. 'type' => 'radio',
  7138. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7139. 'between' => 'I am between',
  7140. ));
  7141. $expected = array(
  7142. 'div' => array('class' => 'input radio'),
  7143. 'fieldset' => array(),
  7144. 'legend' => array(),
  7145. 'Method',
  7146. '/legend',
  7147. 'I am between',
  7148. 'input' => array(
  7149. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  7150. 'value' => '', 'id' => 'ContactMethod_'
  7151. ),
  7152. array('input' => array(
  7153. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7154. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7155. )),
  7156. array('label' => array('for' => 'ContactMethodEmail')),
  7157. 'Email',
  7158. '/label',
  7159. array('input' => array(
  7160. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7161. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7162. )),
  7163. array('label' => array('for' => 'ContactMethodPigeon')),
  7164. 'Pigeon',
  7165. '/label',
  7166. '/fieldset',
  7167. '/div',
  7168. );
  7169. $this->assertTags($result, $expected);
  7170. }
  7171. /**
  7172. * test that some html5 inputs + FormHelper::__call() work
  7173. *
  7174. * @return void
  7175. */
  7176. public function testHtml5Inputs() {
  7177. $result = $this->Form->email('User.email');
  7178. $expected = array(
  7179. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  7180. );
  7181. $this->assertTags($result, $expected);
  7182. $result = $this->Form->search('User.query');
  7183. $expected = array(
  7184. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  7185. );
  7186. $this->assertTags($result, $expected);
  7187. $result = $this->Form->search('User.query', array('value' => 'test'));
  7188. $expected = array(
  7189. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7190. );
  7191. $this->assertTags($result, $expected);
  7192. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7193. $expected = array(
  7194. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7195. );
  7196. $this->assertTags($result, $expected);
  7197. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  7198. $expected = array(
  7199. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7200. );
  7201. $this->assertTags($result, $expected);
  7202. }
  7203. /**
  7204. *
  7205. * @expectedException CakeException
  7206. * @return void
  7207. */
  7208. public function testHtml5InputException() {
  7209. $this->Form->email();
  7210. }
  7211. /**
  7212. * Tests that a model can be loaded from the model names passed in the request object
  7213. *
  7214. * @return void
  7215. */
  7216. public function testIntrospectModelFromRequest() {
  7217. $this->loadFixtures('Post');
  7218. App::build(array(
  7219. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  7220. ));
  7221. CakePlugin::load('TestPlugin');
  7222. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  7223. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  7224. $this->Form->create('TestPluginPost');
  7225. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  7226. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  7227. CakePlugin::unload();
  7228. App::build();
  7229. }
  7230. /**
  7231. * Tests that it is possible to set the validation errors directly in the helper for a field
  7232. *
  7233. * @return void
  7234. */
  7235. public function testCustomValidationErrors() {
  7236. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7237. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7238. $this->assertEquals('Badness!', $result);
  7239. }
  7240. /**
  7241. * Tests that the 'on' key validates as expected on create
  7242. *
  7243. * @return void
  7244. */
  7245. public function testRequiredOnCreate() {
  7246. $this->Form->create('Contact');
  7247. $result = $this->Form->input('Contact.imrequiredonupdate');
  7248. $expected = array(
  7249. 'div' => array('class' => 'input text'),
  7250. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7251. 'Imrequiredonupdate',
  7252. '/label',
  7253. 'input' => array(
  7254. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7255. 'id' => 'ContactImrequiredonupdate'
  7256. ),
  7257. '/div'
  7258. );
  7259. $this->assertTags($result, $expected);
  7260. $result = $this->Form->input('Contact.imrequiredoncreate');
  7261. $expected = array(
  7262. 'div' => array('class' => 'input text required'),
  7263. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7264. 'Imrequiredoncreate',
  7265. '/label',
  7266. 'input' => array(
  7267. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7268. 'id' => 'ContactImrequiredoncreate'
  7269. ),
  7270. '/div'
  7271. );
  7272. $this->assertTags($result, $expected);
  7273. $result = $this->Form->input('Contact.imrequiredonboth');
  7274. $expected = array(
  7275. 'div' => array('class' => 'input text required'),
  7276. 'label' => array('for' => 'ContactImrequiredonboth'),
  7277. 'Imrequiredonboth',
  7278. '/label',
  7279. 'input' => array(
  7280. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7281. 'id' => 'ContactImrequiredonboth'
  7282. ),
  7283. '/div'
  7284. );
  7285. $this->assertTags($result, $expected);
  7286. $result = $this->Form->input('Contact.imrequired');
  7287. $expected = array(
  7288. 'div' => array('class' => 'input text required'),
  7289. 'label' => array('for' => 'ContactImrequired'),
  7290. 'Imrequired',
  7291. '/label',
  7292. 'input' => array(
  7293. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7294. 'id' => 'ContactImrequired'
  7295. ),
  7296. '/div'
  7297. );
  7298. $this->assertTags($result, $expected);
  7299. }
  7300. /**
  7301. * Tests that the 'on' key validates as expected on update
  7302. *
  7303. * @return void
  7304. */
  7305. public function testRequiredOnUpdate() {
  7306. $this->Form->request->data['Contact']['id'] = 1;
  7307. $this->Form->create('Contact');
  7308. $result = $this->Form->input('Contact.imrequiredonupdate');
  7309. $expected = array(
  7310. 'div' => array('class' => 'input text required'),
  7311. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7312. 'Imrequiredonupdate',
  7313. '/label',
  7314. 'input' => array(
  7315. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7316. 'id' => 'ContactImrequiredonupdate'
  7317. ),
  7318. '/div'
  7319. );
  7320. $this->assertTags($result, $expected);
  7321. $result = $this->Form->input('Contact.imrequiredoncreate');
  7322. $expected = array(
  7323. 'div' => array('class' => 'input text'),
  7324. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7325. 'Imrequiredoncreate',
  7326. '/label',
  7327. 'input' => array(
  7328. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7329. 'id' => 'ContactImrequiredoncreate'
  7330. ),
  7331. '/div'
  7332. );
  7333. $this->assertTags($result, $expected);
  7334. $result = $this->Form->input('Contact.imrequiredonboth');
  7335. $expected = array(
  7336. 'div' => array('class' => 'input text required'),
  7337. 'label' => array('for' => 'ContactImrequiredonboth'),
  7338. 'Imrequiredonboth',
  7339. '/label',
  7340. 'input' => array(
  7341. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7342. 'id' => 'ContactImrequiredonboth'
  7343. ),
  7344. '/div'
  7345. );
  7346. $this->assertTags($result, $expected);
  7347. $result = $this->Form->input('Contact.imrequired');
  7348. $expected = array(
  7349. 'div' => array('class' => 'input text required'),
  7350. 'label' => array('for' => 'ContactImrequired'),
  7351. 'Imrequired',
  7352. '/label',
  7353. 'input' => array(
  7354. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7355. 'id' => 'ContactImrequired'
  7356. ),
  7357. '/div'
  7358. );
  7359. $this->assertTags($result, $expected);
  7360. }
  7361. }