FormHelperTest.php 266 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc.
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc.
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.View.Helper
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('ClassRegistry', 'Utility');
  20. App::uses('Controller', 'Controller');
  21. App::uses('View', 'View');
  22. App::uses('Model', 'Model');
  23. App::uses('Security', 'Utility');
  24. App::uses('CakeRequest', 'Network');
  25. App::uses('HtmlHelper', 'View/Helper');
  26. App::uses('FormHelper', 'View/Helper');
  27. App::uses('Router', 'Routing');
  28. /**
  29. * ContactTestController class
  30. *
  31. * @package cake
  32. * @package Cake.Test.Case.View.Helper
  33. */
  34. class ContactTestController extends Controller {
  35. /**
  36. * uses property
  37. *
  38. * @var mixed null
  39. */
  40. public $uses = null;
  41. }
  42. /**
  43. * Contact class
  44. *
  45. * @package cake
  46. * @package Cake.Test.Case.View.Helper
  47. */
  48. class Contact extends CakeTestModel {
  49. /**
  50. * useTable property
  51. *
  52. * @var bool false
  53. */
  54. public $useTable = false;
  55. /**
  56. * Default schema
  57. *
  58. * @var array
  59. */
  60. protected $_schema = array(
  61. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  62. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  63. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  64. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  65. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  66. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  67. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  68. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  69. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  70. );
  71. /**
  72. * validate property
  73. *
  74. * @var array
  75. */
  76. public $validate = array(
  77. 'non_existing' => array(),
  78. 'idontexist' => array(),
  79. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  80. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  81. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  82. 'imrequiredonboth' => array(
  83. 'required' => array('rule' => 'alphaNumeric'),
  84. ),
  85. 'string_required' => 'notEmpty',
  86. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  87. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  88. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  89. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  90. 'imalsonotrequired' => array(
  91. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  92. 'between' => array('rule' => array('between', 5, 30)),
  93. ),
  94. 'imalsonotrequired2' => array(
  95. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  96. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  97. ),
  98. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  99. 'iamrequiredalways' => array(
  100. 'email' => array('rule' => 'email'),
  101. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  102. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  103. ),
  104. 'boolean_field' => array('rule' => 'boolean')
  105. );
  106. /**
  107. * schema method
  108. *
  109. * @return void
  110. */
  111. public function setSchema($schema) {
  112. $this->_schema = $schema;
  113. }
  114. /**
  115. * hasAndBelongsToMany property
  116. *
  117. * @var array
  118. */
  119. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  120. /**
  121. * hasAndBelongsToMany property
  122. *
  123. * @var array
  124. */
  125. public $belongsTo = array('User' => array('className' => 'UserForm'));
  126. }
  127. /**
  128. * ContactTagsContact class
  129. *
  130. * @package cake
  131. * @package Cake.Test.Case.View.Helper
  132. */
  133. class ContactTagsContact extends CakeTestModel {
  134. /**
  135. * useTable property
  136. *
  137. * @var bool false
  138. */
  139. public $useTable = false;
  140. /**
  141. * Default schema
  142. *
  143. * @var array
  144. */
  145. protected $_schema = array(
  146. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  147. 'contact_tag_id' => array(
  148. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  149. )
  150. );
  151. /**
  152. * schema method
  153. *
  154. * @return void
  155. */
  156. public function setSchema($schema) {
  157. $this->_schema = $schema;
  158. }
  159. }
  160. /**
  161. * ContactNonStandardPk class
  162. *
  163. * @package cake
  164. * @package Cake.Test.Case.View.Helper
  165. */
  166. class ContactNonStandardPk extends Contact {
  167. /**
  168. * primaryKey property
  169. *
  170. * @var string 'pk'
  171. */
  172. public $primaryKey = 'pk';
  173. /**
  174. * schema method
  175. *
  176. * @return void
  177. */
  178. public function schema($field = false) {
  179. $this->_schema = parent::schema();
  180. $this->_schema['pk'] = $this->_schema['id'];
  181. unset($this->_schema['id']);
  182. return $this->_schema;
  183. }
  184. }
  185. /**
  186. * ContactTag class
  187. *
  188. * @package cake
  189. * @package Cake.Test.Case.View.Helper
  190. */
  191. class ContactTag extends Model {
  192. /**
  193. * useTable property
  194. *
  195. * @var bool false
  196. */
  197. public $useTable = false;
  198. /**
  199. * schema definition
  200. *
  201. * @var array
  202. */
  203. protected $_schema = array(
  204. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  205. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  206. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  207. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  208. );
  209. }
  210. /**
  211. * UserForm class
  212. *
  213. * @package cake
  214. * @package Cake.Test.Case.View.Helper
  215. */
  216. class UserForm extends CakeTestModel {
  217. /**
  218. * useTable property
  219. *
  220. * @var bool false
  221. */
  222. public $useTable = false;
  223. /**
  224. * hasMany property
  225. *
  226. * @var array
  227. */
  228. public $hasMany = array(
  229. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  230. ));
  231. /**
  232. * schema definition
  233. *
  234. * @var array
  235. */
  236. protected $_schema = array(
  237. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  238. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  239. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  240. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  241. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  242. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  243. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  244. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  245. );
  246. }
  247. /**
  248. * OpenidUrl class
  249. *
  250. * @package cake
  251. * @package Cake.Test.Case.View.Helper
  252. */
  253. class OpenidUrl extends CakeTestModel {
  254. /**
  255. * useTable property
  256. *
  257. * @var bool false
  258. */
  259. public $useTable = false;
  260. /**
  261. * belongsTo property
  262. *
  263. * @var array
  264. */
  265. public $belongsTo = array('UserForm' => array(
  266. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  267. ));
  268. /**
  269. * validate property
  270. *
  271. * @var array
  272. */
  273. public $validate = array('openid_not_registered' => array());
  274. /**
  275. * schema method
  276. *
  277. * @var array
  278. */
  279. protected $_schema = array(
  280. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  281. 'user_form_id' => array(
  282. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  283. ),
  284. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  285. );
  286. /**
  287. * beforeValidate method
  288. *
  289. * @return void
  290. */
  291. public function beforeValidate($options = array()) {
  292. $this->invalidate('openid_not_registered');
  293. return true;
  294. }
  295. }
  296. /**
  297. * ValidateUser class
  298. *
  299. * @package cake
  300. * @package Cake.Test.Case.View.Helper
  301. */
  302. class ValidateUser extends CakeTestModel {
  303. /**
  304. * useTable property
  305. *
  306. * @var bool false
  307. */
  308. public $useTable = false;
  309. /**
  310. * hasOne property
  311. *
  312. * @var array
  313. */
  314. public $hasOne = array('ValidateProfile' => array(
  315. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  316. ));
  317. /**
  318. * schema method
  319. *
  320. * @var array
  321. */
  322. protected $_schema = array(
  323. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  324. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  325. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  326. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  327. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  328. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  329. );
  330. /**
  331. * beforeValidate method
  332. *
  333. * @return void
  334. */
  335. public function beforeValidate($options = array()) {
  336. $this->invalidate('email');
  337. return false;
  338. }
  339. }
  340. /**
  341. * ValidateProfile class
  342. *
  343. * @package cake
  344. * @package Cake.Test.Case.View.Helper
  345. */
  346. class ValidateProfile extends CakeTestModel {
  347. /**
  348. * useTable property
  349. *
  350. * @var bool false
  351. */
  352. public $useTable = false;
  353. /**
  354. * schema property
  355. *
  356. * @var array
  357. */
  358. protected $_schema = array(
  359. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  360. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  361. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  362. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  363. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  364. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  365. );
  366. /**
  367. * hasOne property
  368. *
  369. * @var array
  370. */
  371. public $hasOne = array('ValidateItem' => array(
  372. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  373. ));
  374. /**
  375. * belongsTo property
  376. *
  377. * @var array
  378. */
  379. public $belongsTo = array('ValidateUser' => array(
  380. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  381. ));
  382. /**
  383. * beforeValidate method
  384. *
  385. * @return void
  386. */
  387. public function beforeValidate($options = array()) {
  388. $this->invalidate('full_name');
  389. $this->invalidate('city');
  390. return false;
  391. }
  392. }
  393. /**
  394. * ValidateItem class
  395. *
  396. * @package cake
  397. * @package Cake.Test.Case.View.Helper
  398. */
  399. class ValidateItem extends CakeTestModel {
  400. /**
  401. * useTable property
  402. *
  403. * @var bool false
  404. */
  405. public $useTable = false;
  406. /**
  407. * schema property
  408. *
  409. * @var array
  410. */
  411. protected $_schema = array(
  412. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  413. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  414. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  415. 'description' => array(
  416. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  417. ),
  418. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  419. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  420. );
  421. /**
  422. * belongsTo property
  423. *
  424. * @var array
  425. */
  426. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  427. /**
  428. * beforeValidate method
  429. *
  430. * @return void
  431. */
  432. public function beforeValidate($options = array()) {
  433. $this->invalidate('description');
  434. return false;
  435. }
  436. }
  437. /**
  438. * TestMail class
  439. *
  440. * @package cake
  441. * @package Cake.Test.Case.View.Helper
  442. */
  443. class TestMail extends CakeTestModel {
  444. /**
  445. * useTable property
  446. *
  447. * @var bool false
  448. */
  449. public $useTable = false;
  450. }
  451. /**
  452. * FormHelperTest class
  453. *
  454. * @package cake
  455. * @subpackage Cake.Test.Case.View.Helper
  456. * @property FormHelper $Form
  457. */
  458. class FormHelperTest extends CakeTestCase {
  459. /**
  460. * Fixtures to be used
  461. *
  462. * @var array
  463. */
  464. public $fixtures = array('core.post');
  465. /**
  466. * Do not load the fixtures by default
  467. *
  468. * @var boolean
  469. */
  470. public $autoFixtures = false;
  471. /**
  472. * setUp method
  473. *
  474. * @return void
  475. */
  476. public function setUp() {
  477. parent::setUp();
  478. Configure::write('App.base', '');
  479. $this->Controller = new ContactTestController();
  480. $this->View = new View($this->Controller);
  481. $this->Form = new FormHelper($this->View);
  482. $this->Form->Html = new HtmlHelper($this->View);
  483. $this->Form->request = new CakeRequest('contacts/add', false);
  484. $this->Form->request->here = '/contacts/add';
  485. $this->Form->request['action'] = 'add';
  486. $this->Form->request->webroot = '';
  487. $this->Form->request->base = '';
  488. ClassRegistry::addObject('Contact', new Contact());
  489. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  490. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  491. ClassRegistry::addObject('User', new UserForm());
  492. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  493. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  494. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  495. $this->oldSalt = Configure::read('Security.salt');
  496. $this->dateRegex = array(
  497. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  498. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  499. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  500. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  501. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  502. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  503. );
  504. Configure::write('Security.salt', 'foo!');
  505. }
  506. /**
  507. * tearDown method
  508. *
  509. * @return void
  510. */
  511. public function tearDown() {
  512. parent::tearDown();
  513. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  514. Configure::write('Security.salt', $this->oldSalt);
  515. }
  516. /**
  517. * testFormCreateWithSecurity method
  518. *
  519. * Test form->create() with security key.
  520. *
  521. * @return void
  522. */
  523. public function testCreateWithSecurity() {
  524. $this->Form->request['_Token'] = array('key' => 'testKey');
  525. $encoding = strtolower(Configure::read('App.encoding'));
  526. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  527. $expected = array(
  528. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  529. 'div' => array('style' => 'display:none;'),
  530. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  531. array('input' => array(
  532. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  533. )),
  534. '/div'
  535. );
  536. $this->assertTags($result, $expected);
  537. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  538. $expected['form']['id'] = 'MyForm';
  539. $this->assertTags($result, $expected);
  540. }
  541. /**
  542. * testFormCreateGetNoSecurity method
  543. *
  544. * Test form->create() with no security key as its a get form
  545. *
  546. * @return void
  547. */
  548. public function testCreateEndGetNoSecurity() {
  549. $this->Form->request['_Token'] = array('key' => 'testKey');
  550. $encoding = strtolower(Configure::read('App.encoding'));
  551. $result = $this->Form->create('Contact', array('type' => 'get', 'url' => '/contacts/add'));
  552. $this->assertNotContains('Token', $result);
  553. $result = $this->Form->end('Save');
  554. $this->assertNotContains('Token', $result);
  555. }
  556. /**
  557. * test that create() clears the fields property so it starts fresh
  558. *
  559. * @return void
  560. */
  561. public function testCreateClearingFields() {
  562. $this->Form->fields = array('model_id');
  563. $this->Form->create('Contact');
  564. $this->assertEquals(array(), $this->Form->fields);
  565. }
  566. /**
  567. * Tests form hash generation with model-less data
  568. *
  569. * @return void
  570. */
  571. public function testValidateHashNoModel() {
  572. $this->Form->request['_Token'] = array('key' => 'foo');
  573. $result = $this->Form->secure(array('anything'));
  574. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  575. }
  576. /**
  577. * Tests that models with identical field names get resolved properly
  578. *
  579. * @return void
  580. */
  581. public function testDuplicateFieldNameResolution() {
  582. $result = $this->Form->create('ValidateUser');
  583. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  584. $result = $this->Form->input('ValidateItem.name');
  585. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  586. $result = $this->Form->input('ValidateUser.name');
  587. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  588. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  589. $this->assertRegExp('/type="text"/', $result);
  590. $result = $this->Form->input('ValidateItem.name');
  591. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  592. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  593. $this->assertRegExp('/<textarea/', $result);
  594. $result = $this->Form->input('name');
  595. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  596. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  597. $this->assertRegExp('/type="text"/', $result);
  598. }
  599. /**
  600. * Tests that hidden fields generated for checkboxes don't get locked
  601. *
  602. * @return void
  603. */
  604. public function testNoCheckboxLocking() {
  605. $this->Form->request['_Token'] = array('key' => 'foo');
  606. $this->assertSame(array(), $this->Form->fields);
  607. $this->Form->checkbox('check', array('value' => '1'));
  608. $this->assertSame($this->Form->fields, array('check'));
  609. }
  610. /**
  611. * testFormSecurityFields method
  612. *
  613. * Test generation of secure form hash generation.
  614. *
  615. * @return void
  616. */
  617. public function testFormSecurityFields() {
  618. $key = 'testKey';
  619. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  620. $this->Form->request['_Token'] = array('key' => $key);
  621. $result = $this->Form->secure($fields);
  622. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  623. $hash .= ':' . 'Model.valid';
  624. $hash = urlencode($hash);
  625. $expected = array(
  626. 'div' => array('style' => 'display:none;'),
  627. array('input' => array(
  628. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  629. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  630. )),
  631. array('input' => array(
  632. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  633. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  634. )),
  635. '/div'
  636. );
  637. $this->assertTags($result, $expected);
  638. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  639. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  640. $result = $this->Form->secure($fields);
  641. $expected = array(
  642. 'div' => array('class' => 'hidden'),
  643. array('input' => array(
  644. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  645. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  646. )),
  647. array('input' => array(
  648. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  649. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  650. )),
  651. '/div'
  652. );
  653. $this->assertTags($result, $expected);
  654. }
  655. /**
  656. * Tests correct generation of number fields for double and float fields
  657. *
  658. * @return void
  659. */
  660. public function testTextFieldGenerationForFloats() {
  661. $model = ClassRegistry::getObject('Contact');
  662. $model->setSchema(array('foo' => array(
  663. 'type' => 'float',
  664. 'null' => false,
  665. 'default' => null,
  666. 'length' => null
  667. )));
  668. $this->Form->create('Contact');
  669. $result = $this->Form->input('foo');
  670. $expected = array(
  671. 'div' => array('class' => 'input number'),
  672. 'label' => array('for' => 'ContactFoo'),
  673. 'Foo',
  674. '/label',
  675. array('input' => array(
  676. 'type' => 'number',
  677. 'name' => 'data[Contact][foo]',
  678. 'id' => 'ContactFoo',
  679. 'step' => 'any'
  680. )),
  681. '/div'
  682. );
  683. $this->assertTags($result, $expected);
  684. $result = $this->Form->input('foo', array('step' => 0.5));
  685. $expected = array(
  686. 'div' => array('class' => 'input number'),
  687. 'label' => array('for' => 'ContactFoo'),
  688. 'Foo',
  689. '/label',
  690. array('input' => array(
  691. 'type' => 'number',
  692. 'name' => 'data[Contact][foo]',
  693. 'id' => 'ContactFoo',
  694. 'step' => '0.5'
  695. )),
  696. '/div'
  697. );
  698. $this->assertTags($result, $expected);
  699. }
  700. /**
  701. * Tests correct generation of number fields for integer fields
  702. *
  703. * @access public
  704. * @return void
  705. */
  706. public function testTextFieldTypeNumberGenerationForIntegers() {
  707. $model = ClassRegistry::getObject('Contact');
  708. $model->setSchema(array('foo' => array(
  709. 'type' => 'integer',
  710. 'null' => false,
  711. 'default' => null,
  712. 'length' => null
  713. )));
  714. $this->Form->create('Contact');
  715. $result = $this->Form->input('foo');
  716. $expected = array(
  717. 'div' => array('class' => 'input number'),
  718. 'label' => array('for' => 'ContactFoo'),
  719. 'Foo',
  720. '/label',
  721. array('input' => array(
  722. 'type' => 'number', 'name' => 'data[Contact][foo]',
  723. 'id' => 'ContactFoo'
  724. )),
  725. '/div'
  726. );
  727. $this->assertTags($result, $expected);
  728. }
  729. /**
  730. * testFormSecurityMultipleFields method
  731. *
  732. * Test secure() with multiple row form. Ensure hash is correct.
  733. *
  734. * @return void
  735. */
  736. public function testFormSecurityMultipleFields() {
  737. $key = 'testKey';
  738. $fields = array(
  739. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  740. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  741. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  742. );
  743. $this->Form->request['_Token'] = array('key' => $key);
  744. $result = $this->Form->secure($fields);
  745. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  746. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  747. $expected = array(
  748. 'div' => array('style' => 'display:none;'),
  749. array('input' => array(
  750. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  751. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  752. )),
  753. array('input' => array(
  754. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  755. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  756. )),
  757. '/div'
  758. );
  759. $this->assertTags($result, $expected);
  760. }
  761. /**
  762. * testFormSecurityMultipleSubmitButtons
  763. *
  764. * test form submit generation and ensure that _Token is only created on end()
  765. *
  766. * @return void
  767. */
  768. public function testFormSecurityMultipleSubmitButtons() {
  769. $key = 'testKey';
  770. $this->Form->request['_Token'] = array('key' => $key);
  771. $this->Form->create('Addresses');
  772. $this->Form->input('Address.title');
  773. $this->Form->input('Address.first_name');
  774. $result = $this->Form->submit('Save', array('name' => 'save'));
  775. $expected = array(
  776. 'div' => array('class' => 'submit'),
  777. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  778. '/div',
  779. );
  780. $this->assertTags($result, $expected);
  781. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  782. $expected = array(
  783. 'div' => array('class' => 'submit'),
  784. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  785. '/div',
  786. );
  787. $this->assertTags($result, $expected);
  788. $result = $this->Form->end(null);
  789. $expected = array(
  790. 'div' => array('style' => 'display:none;'),
  791. array('input' => array(
  792. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  793. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  794. )),
  795. array('input' => array(
  796. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  797. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  798. )),
  799. '/div'
  800. );
  801. $this->assertTags($result, $expected);
  802. }
  803. /**
  804. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  805. *
  806. * @return void
  807. */
  808. public function testSecurityButtonNestedNamed() {
  809. $key = 'testKey';
  810. $this->Form->request['_Token'] = array('key' => $key);
  811. $this->Form->create('Addresses');
  812. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  813. $result = $this->Form->unlockField();
  814. $this->assertEquals(array('Address.button'), $result);
  815. }
  816. /**
  817. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  818. *
  819. * @return void
  820. */
  821. public function testSecuritySubmitNestedNamed() {
  822. $key = 'testKey';
  823. $this->Form->request['_Token'] = array('key' => $key);
  824. $this->Form->create('Addresses');
  825. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  826. $result = $this->Form->unlockField();
  827. $this->assertEquals(array('Address.button'), $result);
  828. }
  829. /**
  830. * Test that the correct fields are unlocked for image submits with no names.
  831. *
  832. * @return void
  833. */
  834. public function testSecuritySubmitImageNoName() {
  835. $key = 'testKey';
  836. $this->Form->request['_Token'] = array('key' => $key);
  837. $this->Form->create('User');
  838. $result = $this->Form->submit('save.png');
  839. $expected = array(
  840. 'div' => array('class' => 'submit'),
  841. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  842. '/div'
  843. );
  844. $this->assertTags($result, $expected);
  845. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  846. }
  847. /**
  848. * Test that the correct fields are unlocked for image submits with names.
  849. *
  850. * @return void
  851. */
  852. public function testSecuritySubmitImageName() {
  853. $key = 'testKey';
  854. $this->Form->request['_Token'] = array('key' => $key);
  855. $this->Form->create('User');
  856. $result = $this->Form->submit('save.png', array('name' => 'test'));
  857. $expected = array(
  858. 'div' => array('class' => 'submit'),
  859. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  860. '/div'
  861. );
  862. $this->assertTags($result, $expected);
  863. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  864. }
  865. /**
  866. * testFormSecurityMultipleInputFields method
  867. *
  868. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  869. *
  870. * @return void
  871. */
  872. public function testFormSecurityMultipleInputFields() {
  873. $key = 'testKey';
  874. $this->Form->request['_Token'] = array('key' => $key);
  875. $this->Form->create('Addresses');
  876. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  877. $this->Form->input('Addresses.0.title');
  878. $this->Form->input('Addresses.0.first_name');
  879. $this->Form->input('Addresses.0.last_name');
  880. $this->Form->input('Addresses.0.address');
  881. $this->Form->input('Addresses.0.city');
  882. $this->Form->input('Addresses.0.phone');
  883. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  884. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  885. $this->Form->input('Addresses.1.title');
  886. $this->Form->input('Addresses.1.first_name');
  887. $this->Form->input('Addresses.1.last_name');
  888. $this->Form->input('Addresses.1.address');
  889. $this->Form->input('Addresses.1.city');
  890. $this->Form->input('Addresses.1.phone');
  891. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  892. $result = $this->Form->secure($this->Form->fields);
  893. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  894. $expected = array(
  895. 'div' => array('style' => 'display:none;'),
  896. array('input' => array(
  897. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  898. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  899. )),
  900. array('input' => array(
  901. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  902. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  903. )),
  904. '/div'
  905. );
  906. $this->assertTags($result, $expected);
  907. }
  908. /**
  909. * Test form security with Model.field.0 style inputs
  910. *
  911. * @return void
  912. */
  913. public function testFormSecurityArrayFields() {
  914. $key = 'testKey';
  915. $this->Form->request->params['_Token']['key'] = $key;
  916. $this->Form->create('Address');
  917. $this->Form->input('Address.primary.1');
  918. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  919. $this->Form->input('Address.secondary.1.0');
  920. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  921. }
  922. /**
  923. * testFormSecurityMultipleInputDisabledFields method
  924. *
  925. * test secure form generation with multiple records and disabled fields.
  926. *
  927. * @return void
  928. */
  929. public function testFormSecurityMultipleInputDisabledFields() {
  930. $key = 'testKey';
  931. $this->Form->request->params['_Token'] = array(
  932. 'key' => $key,
  933. 'unlockedFields' => array('first_name', 'address')
  934. );
  935. $this->Form->create();
  936. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  937. $this->Form->input('Addresses.0.title');
  938. $this->Form->input('Addresses.0.first_name');
  939. $this->Form->input('Addresses.0.last_name');
  940. $this->Form->input('Addresses.0.address');
  941. $this->Form->input('Addresses.0.city');
  942. $this->Form->input('Addresses.0.phone');
  943. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  944. $this->Form->input('Addresses.1.title');
  945. $this->Form->input('Addresses.1.first_name');
  946. $this->Form->input('Addresses.1.last_name');
  947. $this->Form->input('Addresses.1.address');
  948. $this->Form->input('Addresses.1.city');
  949. $this->Form->input('Addresses.1.phone');
  950. $result = $this->Form->secure($this->Form->fields);
  951. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  952. $expected = array(
  953. 'div' => array('style' => 'display:none;'),
  954. array('input' => array(
  955. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  956. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  957. )),
  958. array('input' => array(
  959. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  960. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  961. )),
  962. '/div'
  963. );
  964. $this->assertTags($result, $expected);
  965. }
  966. /**
  967. * testFormSecurityInputDisabledFields method
  968. *
  969. * Test single record form with disabled fields.
  970. *
  971. * @return void
  972. */
  973. public function testFormSecurityInputUnlockedFields() {
  974. $key = 'testKey';
  975. $this->Form->request['_Token'] = array(
  976. 'key' => $key,
  977. 'unlockedFields' => array('first_name', 'address')
  978. );
  979. $this->Form->create();
  980. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  981. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  982. $this->Form->input('Addresses.title');
  983. $this->Form->input('Addresses.first_name');
  984. $this->Form->input('Addresses.last_name');
  985. $this->Form->input('Addresses.address');
  986. $this->Form->input('Addresses.city');
  987. $this->Form->input('Addresses.phone');
  988. $result = $this->Form->fields;
  989. $expected = array(
  990. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  991. 'Addresses.city', 'Addresses.phone'
  992. );
  993. $this->assertEquals($expected, $result);
  994. $result = $this->Form->secure($expected);
  995. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  996. $expected = array(
  997. 'div' => array('style' => 'display:none;'),
  998. array('input' => array(
  999. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1000. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1001. )),
  1002. array('input' => array(
  1003. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1004. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1005. )),
  1006. '/div'
  1007. );
  1008. $this->assertTags($result, $expected);
  1009. }
  1010. /**
  1011. * test securing inputs with custom name attributes.
  1012. *
  1013. * @return void
  1014. */
  1015. public function testFormSecureWithCustomNameAttribute() {
  1016. $this->Form->request->params['_Token']['key'] = 'testKey';
  1017. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1018. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1019. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1020. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1021. }
  1022. /**
  1023. * testFormSecuredInput method
  1024. *
  1025. * Test generation of entire secure form, assertions made on input() output.
  1026. *
  1027. * @return void
  1028. */
  1029. public function testFormSecuredInput() {
  1030. $this->Form->request['_Token'] = array('key' => 'testKey');
  1031. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1032. $encoding = strtolower(Configure::read('App.encoding'));
  1033. $expected = array(
  1034. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1035. 'div' => array('style' => 'display:none;'),
  1036. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1037. array('input' => array(
  1038. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1039. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1040. )),
  1041. '/div'
  1042. );
  1043. $this->assertTags($result, $expected);
  1044. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1045. $expected = array(
  1046. 'div' => array('class' => 'input text'),
  1047. 'label' => array('for' => 'UserFormPublished'),
  1048. 'Published',
  1049. '/label',
  1050. array('input' => array(
  1051. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1052. 'id' => 'UserFormPublished'
  1053. )),
  1054. '/div'
  1055. );
  1056. $this->assertTags($result, $expected);
  1057. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1058. $expected = array(
  1059. 'div' => array('class' => 'input text'),
  1060. 'label' => array('for' => 'UserFormOther'),
  1061. 'Other',
  1062. '/label',
  1063. array('input' => array(
  1064. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1065. 'id' => 'UserFormOther'
  1066. )),
  1067. '/div'
  1068. );
  1069. $this->assertTags($result, $expected);
  1070. $result = $this->Form->hidden('UserForm.stuff');
  1071. $expected = array('input' => array(
  1072. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1073. 'id' => 'UserFormStuff'
  1074. ));
  1075. $this->assertTags($result, $expected);
  1076. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1077. $expected = array('input' => array(
  1078. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1079. 'value' => '0', 'id' => 'UserFormHidden'
  1080. ));
  1081. $this->assertTags($result, $expected);
  1082. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1083. $expected = array(
  1084. 'div' => array('class' => 'input checkbox'),
  1085. array('input' => array(
  1086. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1087. 'value' => '0', 'id' => 'UserFormSomething_'
  1088. )),
  1089. array('input' => array(
  1090. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1091. 'value' => '1', 'id' => 'UserFormSomething'
  1092. )),
  1093. 'label' => array('for' => 'UserFormSomething'),
  1094. 'Something',
  1095. '/label',
  1096. '/div'
  1097. );
  1098. $this->assertTags($result, $expected);
  1099. $result = $this->Form->fields;
  1100. $expected = array(
  1101. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1102. 'UserForm.hidden' => '0', 'UserForm.something'
  1103. );
  1104. $this->assertEquals($expected, $result);
  1105. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1106. $result = $this->Form->secure($this->Form->fields);
  1107. $expected = array(
  1108. 'div' => array('style' => 'display:none;'),
  1109. array('input' => array(
  1110. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1111. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1112. )),
  1113. array('input' => array(
  1114. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1115. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1116. )),
  1117. '/div'
  1118. );
  1119. $this->assertTags($result, $expected);
  1120. }
  1121. /**
  1122. * Tests that the correct keys are added to the field hash index
  1123. *
  1124. * @return void
  1125. */
  1126. public function testFormSecuredFileInput() {
  1127. $this->Form->request['_Token'] = array('key' => 'testKey');
  1128. $this->assertEquals(array(), $this->Form->fields);
  1129. $this->Form->file('Attachment.file');
  1130. $expected = array(
  1131. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1132. 'Attachment.file.error', 'Attachment.file.size'
  1133. );
  1134. $this->assertEquals($expected, $this->Form->fields);
  1135. }
  1136. /**
  1137. * test that multiple selects keys are added to field hash
  1138. *
  1139. * @return void
  1140. */
  1141. public function testFormSecuredMultipleSelect() {
  1142. $this->Form->request['_Token'] = array('key' => 'testKey');
  1143. $this->assertEquals(array(), $this->Form->fields);
  1144. $options = array('1' => 'one', '2' => 'two');
  1145. $this->Form->select('Model.select', $options);
  1146. $expected = array('Model.select');
  1147. $this->assertEquals($expected, $this->Form->fields);
  1148. $this->Form->fields = array();
  1149. $this->Form->select('Model.select', $options, array('multiple' => true));
  1150. $this->assertEquals($expected, $this->Form->fields);
  1151. }
  1152. /**
  1153. * testFormSecuredRadio method
  1154. *
  1155. * @return void
  1156. */
  1157. public function testFormSecuredRadio() {
  1158. $this->Form->request['_Token'] = array('key' => 'testKey');
  1159. $this->assertEquals(array(), $this->Form->fields);
  1160. $options = array('1' => 'option1', '2' => 'option2');
  1161. $this->Form->radio('Test.test', $options);
  1162. $expected = array('Test.test');
  1163. $this->assertEquals($expected, $this->Form->fields);
  1164. }
  1165. /**
  1166. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1167. * hashing.
  1168. *
  1169. * @return void
  1170. */
  1171. public function testFormSecuredAndDisabled() {
  1172. $this->Form->request['_Token'] = array('key' => 'testKey');
  1173. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1174. $this->Form->text('Model.text', array('disabled' => true));
  1175. $this->Form->password('Model.text', array('disabled' => true));
  1176. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1177. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1178. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1179. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1180. $this->Form->month('Model.month', array('disabled' => true));
  1181. $this->Form->day('Model.day', array('disabled' => true));
  1182. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1183. $this->Form->minute('Model.minute', array('disabled' => true));
  1184. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1185. $expected = array(
  1186. 'Model.radio' => ''
  1187. );
  1188. $this->assertEquals($expected, $this->Form->fields);
  1189. }
  1190. /**
  1191. * testDisableSecurityUsingForm method
  1192. *
  1193. * @return void
  1194. */
  1195. public function testDisableSecurityUsingForm() {
  1196. $this->Form->request['_Token'] = array(
  1197. 'key' => 'testKey',
  1198. 'disabledFields' => array()
  1199. );
  1200. $this->Form->create();
  1201. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1202. $this->Form->input('Addresses.title');
  1203. $this->Form->input('Addresses.first_name', array('secure' => false));
  1204. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1205. $this->Form->input('Addresses.zip', array(
  1206. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1207. ));
  1208. $result = $this->Form->fields;
  1209. $expected = array(
  1210. 'Addresses.id' => '123456', 'Addresses.title',
  1211. );
  1212. $this->assertEquals($expected, $result);
  1213. }
  1214. /**
  1215. * test disableField
  1216. *
  1217. * @return void
  1218. */
  1219. public function testUnlockFieldAddsToList() {
  1220. $this->Form->request['_Token'] = array(
  1221. 'key' => 'testKey',
  1222. 'unlockedFields' => array()
  1223. );
  1224. $this->Form->create('Contact');
  1225. $this->Form->unlockField('Contact.name');
  1226. $this->Form->text('Contact.name');
  1227. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1228. $this->assertEquals(array(), $this->Form->fields);
  1229. }
  1230. /**
  1231. * test unlockField removing from fields array.
  1232. *
  1233. * @return void
  1234. */
  1235. public function testUnlockFieldRemovingFromFields() {
  1236. $this->Form->request['_Token'] = array(
  1237. 'key' => 'testKey',
  1238. 'unlockedFields' => array()
  1239. );
  1240. $this->Form->create('Contact');
  1241. $this->Form->hidden('Contact.id', array('value' => 1));
  1242. $this->Form->text('Contact.name');
  1243. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1244. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1245. $this->Form->unlockField('Contact.name');
  1246. $this->Form->unlockField('Contact.id');
  1247. $this->assertEquals(array(), $this->Form->fields);
  1248. }
  1249. /**
  1250. * testTagIsInvalid method
  1251. *
  1252. * @return void
  1253. */
  1254. public function testTagIsInvalid() {
  1255. $Contact = ClassRegistry::getObject('Contact');
  1256. $Contact->validationErrors[0]['email'] = array('Please provide an email');
  1257. $this->Form->setEntity('Contact.0.email');
  1258. $result = $this->Form->tagIsInvalid();
  1259. $expected = array('Please provide an email');
  1260. $this->assertEquals($expected, $result);
  1261. $this->Form->setEntity('Contact.1.email');
  1262. $result = $this->Form->tagIsInvalid();
  1263. $expected = false;
  1264. $this->assertSame($expected, $result);
  1265. $this->Form->setEntity('Contact.0.name');
  1266. $result = $this->Form->tagIsInvalid();
  1267. $expected = false;
  1268. $this->assertSame($expected, $result);
  1269. }
  1270. /**
  1271. * Test validation errors.
  1272. *
  1273. * @return void
  1274. */
  1275. public function testPasswordValidation() {
  1276. $Contact = ClassRegistry::getObject('Contact');
  1277. $Contact->validationErrors['password'] = array('Please provide a password');
  1278. $result = $this->Form->input('Contact.password');
  1279. $expected = array(
  1280. 'div' => array('class' => 'input password error'),
  1281. 'label' => array('for' => 'ContactPassword'),
  1282. 'Password',
  1283. '/label',
  1284. 'input' => array(
  1285. 'type' => 'password', 'name' => 'data[Contact][password]',
  1286. 'id' => 'ContactPassword', 'class' => 'form-error'
  1287. ),
  1288. array('div' => array('class' => 'error-message')),
  1289. 'Please provide a password',
  1290. '/div',
  1291. '/div'
  1292. );
  1293. $this->assertTags($result, $expected);
  1294. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1295. $expected = array(
  1296. 'div' => array('class' => 'input password error'),
  1297. 'label' => array('for' => 'ContactPassword'),
  1298. 'Password',
  1299. '/label',
  1300. 'input' => array(
  1301. 'type' => 'password', 'name' => 'data[Contact][password]',
  1302. 'id' => 'ContactPassword', 'class' => 'form-error'
  1303. ),
  1304. '/div'
  1305. );
  1306. $this->assertTags($result, $expected);
  1307. }
  1308. /**
  1309. * Test validation errors, when validation message is an empty string.
  1310. *
  1311. * @access public
  1312. * @return void
  1313. */
  1314. public function testEmptyErrorValidation() {
  1315. $this->Form->validationErrors['Contact']['password'] = '';
  1316. $result = $this->Form->input('Contact.password');
  1317. $expected = array(
  1318. 'div' => array('class' => 'input password error'),
  1319. 'label' => array('for' => 'ContactPassword'),
  1320. 'Password',
  1321. '/label',
  1322. 'input' => array(
  1323. 'type' => 'password', 'name' => 'data[Contact][password]',
  1324. 'id' => 'ContactPassword', 'class' => 'form-error'
  1325. ),
  1326. array('div' => array('class' => 'error-message')),
  1327. array(),
  1328. '/div',
  1329. '/div'
  1330. );
  1331. $this->assertTags($result, $expected);
  1332. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1333. $expected = array(
  1334. 'div' => array('class' => 'input password error'),
  1335. 'label' => array('for' => 'ContactPassword'),
  1336. 'Password',
  1337. '/label',
  1338. 'input' => array(
  1339. 'type' => 'password', 'name' => 'data[Contact][password]',
  1340. 'id' => 'ContactPassword', 'class' => 'form-error'
  1341. ),
  1342. '/div'
  1343. );
  1344. $this->assertTags($result, $expected);
  1345. }
  1346. /**
  1347. * Test validation errors, when calling input() overriding validation message by an empty string.
  1348. *
  1349. * @access public
  1350. * @return void
  1351. */
  1352. public function testEmptyInputErrorValidation() {
  1353. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1354. $result = $this->Form->input('Contact.password', array('error' => ''));
  1355. $expected = array(
  1356. 'div' => array('class' => 'input password error'),
  1357. 'label' => array('for' => 'ContactPassword'),
  1358. 'Password',
  1359. '/label',
  1360. 'input' => array(
  1361. 'type' => 'password', 'name' => 'data[Contact][password]',
  1362. 'id' => 'ContactPassword', 'class' => 'form-error'
  1363. ),
  1364. array('div' => array('class' => 'error-message')),
  1365. array(),
  1366. '/div',
  1367. '/div'
  1368. );
  1369. $this->assertTags($result, $expected);
  1370. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1371. $expected = array(
  1372. 'div' => array('class' => 'input password error'),
  1373. 'label' => array('for' => 'ContactPassword'),
  1374. 'Password',
  1375. '/label',
  1376. 'input' => array(
  1377. 'type' => 'password', 'name' => 'data[Contact][password]',
  1378. 'id' => 'ContactPassword', 'class' => 'form-error'
  1379. ),
  1380. '/div'
  1381. );
  1382. $this->assertTags($result, $expected);
  1383. }
  1384. /**
  1385. * testFormValidationAssociated method
  1386. *
  1387. * test display of form errors in conjunction with model::validates.
  1388. *
  1389. * @return void
  1390. */
  1391. public function testFormValidationAssociated() {
  1392. $this->UserForm = ClassRegistry::getObject('UserForm');
  1393. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1394. $data = array(
  1395. 'UserForm' => array('name' => 'user'),
  1396. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1397. );
  1398. $result = $this->UserForm->OpenidUrl->create($data);
  1399. $this->assertFalse(empty($result));
  1400. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1401. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1402. $encoding = strtolower(Configure::read('App.encoding'));
  1403. $expected = array(
  1404. 'form' => array(
  1405. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1406. 'accept-charset' => $encoding
  1407. ),
  1408. 'div' => array('style' => 'display:none;'),
  1409. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1410. '/div'
  1411. );
  1412. $this->assertTags($result, $expected);
  1413. $result = $this->Form->error(
  1414. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1415. );
  1416. $this->assertEquals('Error, not registered', $result);
  1417. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1418. }
  1419. /**
  1420. * testFormValidationAssociatedFirstLevel method
  1421. *
  1422. * test form error display with associated model.
  1423. *
  1424. * @return void
  1425. */
  1426. public function testFormValidationAssociatedFirstLevel() {
  1427. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1428. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1429. $data = array(
  1430. 'ValidateUser' => array('name' => 'mariano'),
  1431. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1432. );
  1433. $result = $this->ValidateUser->create($data);
  1434. $this->assertFalse(empty($result));
  1435. $this->assertFalse($this->ValidateUser->validates());
  1436. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1437. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1438. $encoding = strtolower(Configure::read('App.encoding'));
  1439. $expected = array(
  1440. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1441. 'div' => array('style' => 'display:none;'),
  1442. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1443. '/div'
  1444. );
  1445. $this->assertTags($result, $expected);
  1446. $result = $this->Form->error(
  1447. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1448. );
  1449. $this->assertEquals('Invalid email', $result);
  1450. $result = $this->Form->error(
  1451. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1452. );
  1453. $this->assertEquals('Invalid name', $result);
  1454. $result = $this->Form->error(
  1455. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1456. );
  1457. $this->assertEquals('Invalid city', $result);
  1458. unset($this->ValidateUser->ValidateProfile);
  1459. unset($this->ValidateUser);
  1460. }
  1461. /**
  1462. * testFormValidationAssociatedSecondLevel method
  1463. *
  1464. * test form error display with associated model.
  1465. *
  1466. * @return void
  1467. */
  1468. public function testFormValidationAssociatedSecondLevel() {
  1469. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1470. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1471. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1472. $data = array(
  1473. 'ValidateUser' => array('name' => 'mariano'),
  1474. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1475. 'ValidateItem' => array('name' => 'Item')
  1476. );
  1477. $result = $this->ValidateUser->create($data);
  1478. $this->assertFalse(empty($result));
  1479. $this->assertFalse($this->ValidateUser->validates());
  1480. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1481. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1482. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1483. $encoding = strtolower(Configure::read('App.encoding'));
  1484. $expected = array(
  1485. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1486. 'div' => array('style' => 'display:none;'),
  1487. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1488. '/div'
  1489. );
  1490. $this->assertTags($result, $expected);
  1491. $result = $this->Form->error(
  1492. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1493. );
  1494. $this->assertEquals('Invalid email', $result);
  1495. $result = $this->Form->error(
  1496. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1497. );
  1498. $this->assertEquals('Invalid name', $result);
  1499. $result = $this->Form->error(
  1500. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1501. );
  1502. $result = $this->Form->error(
  1503. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1504. );
  1505. $this->assertEquals('Invalid description', $result);
  1506. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1507. unset($this->ValidateUser->ValidateProfile);
  1508. unset($this->ValidateUser);
  1509. }
  1510. /**
  1511. * testFormValidationMultiRecord method
  1512. *
  1513. * test form error display with multiple records.
  1514. *
  1515. * @return void
  1516. */
  1517. public function testFormValidationMultiRecord() {
  1518. $Contact = ClassRegistry::getObject('Contact');
  1519. $Contact->validationErrors[2] = array(
  1520. 'name' => array('This field cannot be left blank')
  1521. );
  1522. $result = $this->Form->input('Contact.2.name');
  1523. $expected = array(
  1524. 'div' => array('class' => 'input text error'),
  1525. 'label' => array('for' => 'Contact2Name'),
  1526. 'Name',
  1527. '/label',
  1528. 'input' => array(
  1529. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1530. 'class' => 'form-error', 'maxlength' => 255
  1531. ),
  1532. array('div' => array('class' => 'error-message')),
  1533. 'This field cannot be left blank',
  1534. '/div',
  1535. '/div'
  1536. );
  1537. $this->assertTags($result, $expected);
  1538. }
  1539. /**
  1540. * testMultipleInputValidation method
  1541. *
  1542. * test multiple record form validation error display.
  1543. *
  1544. * @return void
  1545. */
  1546. public function testMultipleInputValidation() {
  1547. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1548. $Address->validationErrors[0] = array(
  1549. 'title' => array('This field cannot be empty'),
  1550. 'first_name' => array('This field cannot be empty')
  1551. );
  1552. $Address->validationErrors[1] = array(
  1553. 'last_name' => array('You must have a last name')
  1554. );
  1555. $this->Form->create();
  1556. $result = $this->Form->input('Address.0.title');
  1557. $expected = array(
  1558. 'div' => array('class'),
  1559. 'label' => array('for'),
  1560. 'preg:/[^<]+/',
  1561. '/label',
  1562. 'input' => array(
  1563. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1564. ),
  1565. array('div' => array('class' => 'error-message')),
  1566. 'This field cannot be empty',
  1567. '/div',
  1568. '/div'
  1569. );
  1570. $this->assertTags($result, $expected);
  1571. $result = $this->Form->input('Address.0.first_name');
  1572. $expected = array(
  1573. 'div' => array('class'),
  1574. 'label' => array('for'),
  1575. 'preg:/[^<]+/',
  1576. '/label',
  1577. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1578. array('div' => array('class' => 'error-message')),
  1579. 'This field cannot be empty',
  1580. '/div',
  1581. '/div'
  1582. );
  1583. $this->assertTags($result, $expected);
  1584. $result = $this->Form->input('Address.0.last_name');
  1585. $expected = array(
  1586. 'div' => array('class'),
  1587. 'label' => array('for'),
  1588. 'preg:/[^<]+/',
  1589. '/label',
  1590. 'input' => array('type' => 'text', 'name', 'id'),
  1591. '/div'
  1592. );
  1593. $this->assertTags($result, $expected);
  1594. $result = $this->Form->input('Address.1.last_name');
  1595. $expected = array(
  1596. 'div' => array('class'),
  1597. 'label' => array('for'),
  1598. 'preg:/[^<]+/',
  1599. '/label',
  1600. 'input' => array(
  1601. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1602. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1603. ),
  1604. array('div' => array('class' => 'error-message')),
  1605. 'You must have a last name',
  1606. '/div',
  1607. '/div'
  1608. );
  1609. $this->assertTags($result, $expected);
  1610. }
  1611. /**
  1612. * testInput method
  1613. *
  1614. * Test various incarnations of input().
  1615. *
  1616. * @return void
  1617. */
  1618. public function testInput() {
  1619. $result = $this->Form->input('ValidateUser.balance');
  1620. $expected = array(
  1621. 'div' => array('class'),
  1622. 'label' => array('for'),
  1623. 'Balance',
  1624. '/label',
  1625. 'input' => array('name', 'type' => 'number', 'maxlength' => 8, 'id'),
  1626. '/div',
  1627. );
  1628. $this->assertTags($result, $expected);
  1629. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1630. $expected = array(
  1631. 'div' => array('class' => 'input email'),
  1632. 'label' => array('for' => 'custom'),
  1633. 'Email',
  1634. '/label',
  1635. array('input' => array(
  1636. 'type' => 'email', 'name' => 'data[Contact][email]',
  1637. 'id' => 'custom', 'maxlength' => 255
  1638. )),
  1639. '/div'
  1640. );
  1641. $this->assertTags($result, $expected);
  1642. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1643. $expected = array(
  1644. '<div',
  1645. 'label' => array('for' => 'ContactEmail'),
  1646. 'Email',
  1647. '/label',
  1648. array('input' => array(
  1649. 'type' => 'email', 'name' => 'data[Contact][email]',
  1650. 'id' => 'ContactEmail', 'maxlength' => 255
  1651. )),
  1652. '/div'
  1653. );
  1654. $this->assertTags($result, $expected);
  1655. $result = $this->Form->hidden('Contact.idontexist');
  1656. $expected = array('input' => array(
  1657. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1658. 'id' => 'ContactIdontexist'
  1659. ));
  1660. $this->assertTags($result, $expected);
  1661. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1662. $expected = array(
  1663. 'div' => array('class' => 'input text'),
  1664. 'label' => array('for' => 'ContactEmail'),
  1665. 'Email',
  1666. '/label',
  1667. array('input' => array(
  1668. 'type' => 'text', 'name' => 'data[Contact][email]',
  1669. 'id' => 'ContactEmail'
  1670. )),
  1671. '/div'
  1672. );
  1673. $this->assertTags($result, $expected);
  1674. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1675. $expected = array(
  1676. 'div' => array('class' => 'input text'),
  1677. 'label' => array('for' => 'Contact5Email'),
  1678. 'Email',
  1679. '/label',
  1680. array('input' => array(
  1681. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1682. 'id' => 'Contact5Email'
  1683. )),
  1684. '/div'
  1685. );
  1686. $this->assertTags($result, $expected);
  1687. $result = $this->Form->input('Contact.password');
  1688. $expected = array(
  1689. 'div' => array('class' => 'input password'),
  1690. 'label' => array('for' => 'ContactPassword'),
  1691. 'Password',
  1692. '/label',
  1693. array('input' => array(
  1694. 'type' => 'password', 'name' => 'data[Contact][password]',
  1695. 'id' => 'ContactPassword'
  1696. )),
  1697. '/div'
  1698. );
  1699. $this->assertTags($result, $expected);
  1700. $result = $this->Form->input('Contact.email', array(
  1701. 'type' => 'file', 'class' => 'textbox'
  1702. ));
  1703. $expected = array(
  1704. 'div' => array('class' => 'input file'),
  1705. 'label' => array('for' => 'ContactEmail'),
  1706. 'Email',
  1707. '/label',
  1708. array('input' => array(
  1709. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1710. 'id' => 'ContactEmail'
  1711. )),
  1712. '/div'
  1713. );
  1714. $this->assertTags($result, $expected);
  1715. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1716. $result = $this->Form->input('Contact.phone');
  1717. $expected = array(
  1718. 'div' => array('class' => 'input tel'),
  1719. 'label' => array('for' => 'ContactPhone'),
  1720. 'Phone',
  1721. '/label',
  1722. array('input' => array(
  1723. 'type' => 'tel', 'name' => 'data[Contact][phone]',
  1724. 'value' => 'Hello &amp; World &gt; weird chars',
  1725. 'id' => 'ContactPhone', 'maxlength' => 255
  1726. )),
  1727. '/div'
  1728. );
  1729. $this->assertTags($result, $expected);
  1730. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1731. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1732. $expected = array(
  1733. 'div' => array('class' => 'input text'),
  1734. 'label' => array('for' => 'myId'),
  1735. 'Field',
  1736. '/label',
  1737. 'input' => array(
  1738. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1739. 'value' => 'My value', 'id' => 'myId'
  1740. ),
  1741. '/div'
  1742. );
  1743. $this->assertTags($result, $expected);
  1744. unset($this->Form->request->data);
  1745. $Contact = ClassRegistry::getObject('Contact');
  1746. $Contact->validationErrors['field'] = array('Badness!');
  1747. $result = $this->Form->input('Contact.field');
  1748. $expected = array(
  1749. 'div' => array('class' => 'input text error'),
  1750. 'label' => array('for' => 'ContactField'),
  1751. 'Field',
  1752. '/label',
  1753. 'input' => array(
  1754. 'type' => 'text', 'name' => 'data[Contact][field]',
  1755. 'id' => 'ContactField', 'class' => 'form-error'
  1756. ),
  1757. array('div' => array('class' => 'error-message')),
  1758. 'Badness!',
  1759. '/div',
  1760. '/div'
  1761. );
  1762. $this->assertTags($result, $expected);
  1763. $result = $this->Form->input('Contact.field', array(
  1764. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1765. ));
  1766. $expected = array(
  1767. 'label' => array('for' => 'ContactField'),
  1768. 'Field',
  1769. '/label',
  1770. 'input' => array(
  1771. 'type' => 'text', 'name' => 'data[Contact][field]',
  1772. 'id' => 'ContactField', 'class' => 'form-error'
  1773. ),
  1774. array('span' => array('class' => 'error-message')),
  1775. 'Badness!',
  1776. '/span'
  1777. );
  1778. $this->assertTags($result, $expected);
  1779. $result = $this->Form->input('Contact.field', array(
  1780. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1781. ));
  1782. $expected = array(
  1783. 'div' => array('class' => 'input text error'),
  1784. 'label' => array('for' => 'ContactField'),
  1785. 'Field',
  1786. '/label',
  1787. 'input' => array(
  1788. 'type' => 'text', 'name' => 'data[Contact][field]',
  1789. 'id' => 'ContactField', 'class' => 'form-error'
  1790. ),
  1791. array('div' => array('class' => 'error')),
  1792. 'Badness!',
  1793. '/div'
  1794. );
  1795. $this->assertTags($result, $expected);
  1796. $result = $this->Form->input('Contact.field', array(
  1797. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1798. ));
  1799. $expected = array(
  1800. 'span' => array('class' => 'input text error'),
  1801. 'label' => array('for' => 'ContactField'),
  1802. 'Field',
  1803. '/label',
  1804. 'input' => array(
  1805. 'type' => 'text', 'name' => 'data[Contact][field]',
  1806. 'id' => 'ContactField', 'class' => 'form-error'
  1807. ),
  1808. 'Badness!',
  1809. '/span'
  1810. );
  1811. $this->assertTags($result, $expected);
  1812. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1813. $expected = array(
  1814. 'div' => array('class' => 'input text error'),
  1815. 'label' => array('for' => 'ContactField'),
  1816. 'Field',
  1817. '/label',
  1818. 'input' => array(
  1819. 'type' => 'text', 'name' => 'data[Contact][field]',
  1820. 'id' => 'ContactField', 'class' => 'form-error'
  1821. ),
  1822. 'A message to you, Rudy',
  1823. array('div' => array('class' => 'error-message')),
  1824. 'Badness!',
  1825. '/div',
  1826. '/div'
  1827. );
  1828. $this->assertTags($result, $expected);
  1829. $this->Form->setEntity(null);
  1830. $this->Form->setEntity('Contact.field');
  1831. $result = $this->Form->input('Contact.field', array(
  1832. 'after' => 'A message to you, Rudy', 'error' => false
  1833. ));
  1834. $expected = array(
  1835. 'div' => array('class' => 'input text'),
  1836. 'label' => array('for' => 'ContactField'),
  1837. 'Field',
  1838. '/label',
  1839. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1840. 'A message to you, Rudy',
  1841. '/div'
  1842. );
  1843. $this->assertTags($result, $expected);
  1844. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1845. $expected = array(
  1846. 'div' => array('class' => 'input text'),
  1847. 'label' => array('for' => 'ObjectField'),
  1848. 'Field',
  1849. '/label',
  1850. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1851. 'A message to you, Rudy',
  1852. '/div'
  1853. );
  1854. $this->assertTags($result, $expected);
  1855. $Contact->validationErrors['field'] = array('minLength');
  1856. $result = $this->Form->input('Contact.field', array(
  1857. 'error' => array(
  1858. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1859. 'maxLength' => 'login too large'
  1860. )
  1861. ));
  1862. $expected = array(
  1863. 'div' => array('class' => 'input text error'),
  1864. 'label' => array('for' => 'ContactField'),
  1865. 'Field',
  1866. '/label',
  1867. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1868. array('div' => array('class' => 'error-message')),
  1869. 'Le login doit contenir au moins 2 caractères',
  1870. '/div',
  1871. '/div'
  1872. );
  1873. $this->assertTags($result, $expected);
  1874. $Contact->validationErrors['field'] = array('maxLength');
  1875. $result = $this->Form->input('Contact.field', array(
  1876. 'error' => array(
  1877. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1878. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1879. 'maxLength' => 'login too large',
  1880. )
  1881. ));
  1882. $expected = array(
  1883. 'div' => array('class' => 'input text error'),
  1884. 'label' => array('for' => 'ContactField'),
  1885. 'Field',
  1886. '/label',
  1887. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1888. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1889. 'login too large',
  1890. '/span',
  1891. '/div'
  1892. );
  1893. $this->assertTags($result, $expected);
  1894. }
  1895. /**
  1896. * Test that inputs with 0 can be created.
  1897. *
  1898. * @return void
  1899. */
  1900. public function testInputZero() {
  1901. $this->Form->create('User');
  1902. $result = $this->Form->input('0');
  1903. $expected = array(
  1904. 'div' => array('class' => 'input text'),
  1905. 'label' => array('for' => 'User0'), '/label',
  1906. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  1907. '/div'
  1908. );
  1909. $this->assertTags($result, $expected);
  1910. }
  1911. /**
  1912. * test input() with checkbox creation
  1913. *
  1914. * @return void
  1915. */
  1916. public function testInputCheckbox() {
  1917. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1918. $expected = array(
  1919. 'div' => array('class' => 'input checkbox'),
  1920. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1921. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1922. '/div'
  1923. );
  1924. $this->assertTags($result, $expected);
  1925. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1926. $expected = array(
  1927. 'div' => array('class' => 'input checkbox'),
  1928. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1929. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1930. '/div'
  1931. );
  1932. $this->assertTags($result, $expected);
  1933. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1934. $expected = array(
  1935. 'div' => array('class' => 'input checkbox'),
  1936. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1937. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1938. '/div'
  1939. );
  1940. $this->assertTags($result, $expected);
  1941. }
  1942. /**
  1943. * test form->input() with time types.
  1944. *
  1945. */
  1946. public function testInputTime() {
  1947. extract($this->dateRegex);
  1948. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1949. $result = explode(':', $result);
  1950. $this->assertRegExp('/option value="23"/', $result[0]);
  1951. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1952. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1953. $result = explode(':', $result);
  1954. $this->assertRegExp('/option value="23"/', $result[0]);
  1955. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1956. $result = $this->Form->input('Model.field', array(
  1957. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  1958. ));
  1959. $result = explode(':', $result);
  1960. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1961. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1962. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1963. $result = $this->Form->input('Model.field', array(
  1964. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  1965. ));
  1966. $result = explode(':', $result);
  1967. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1968. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1969. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1970. $result = $this->Form->input('prueba', array(
  1971. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  1972. 'maxYear' => date('Y') + 1 ,'interval' => 15
  1973. ));
  1974. $result = explode(':', $result);
  1975. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1976. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1977. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1978. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  1979. $result = $this->Form->input('Random.start_time', array(
  1980. 'type' => 'time',
  1981. 'selected' => '18:15'
  1982. ));
  1983. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  1984. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  1985. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  1986. }
  1987. /**
  1988. * Test interval + selected near the hour roll over.
  1989. *
  1990. * @return void
  1991. */
  1992. public function testTimeSelectedWithInterval() {
  1993. $result = $this->Form->input('Model.start_time', array(
  1994. 'type' => 'time',
  1995. 'interval' => 15,
  1996. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  1997. ));
  1998. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  1999. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2000. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2001. $result = $this->Form->input('Model.start_time', array(
  2002. 'type' => 'time',
  2003. 'interval' => 15,
  2004. 'selected' => '2012-10-23 15:57:00'
  2005. ));
  2006. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2007. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2008. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2009. $result = $this->Form->input('Model.start_time', array(
  2010. 'timeFormat' => 24,
  2011. 'type' => 'time',
  2012. 'interval' => 15,
  2013. 'selected' => '15:57'
  2014. ));
  2015. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2016. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2017. $result = $this->Form->input('Model.start_time', array(
  2018. 'timeFormat' => 24,
  2019. 'type' => 'time',
  2020. 'interval' => 15,
  2021. 'selected' => '23:57'
  2022. ));
  2023. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2024. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2025. $result = $this->Form->input('Model.created', array(
  2026. 'timeFormat' => 24,
  2027. 'type' => 'datetime',
  2028. 'interval' => 15,
  2029. 'selected' => '2012-09-30 23:56'
  2030. ));
  2031. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2032. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2033. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2034. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2035. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2036. }
  2037. /**
  2038. * test form->input() with datetime, date and time types
  2039. *
  2040. * @return void
  2041. */
  2042. public function testInputDatetime() {
  2043. extract($this->dateRegex);
  2044. $result = $this->Form->input('prueba', array(
  2045. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2046. 'maxYear' => date('Y') + 1, 'interval' => 15
  2047. ));
  2048. $result = explode(':', $result);
  2049. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2050. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2051. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2052. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2053. //related to ticket #5013
  2054. $result = $this->Form->input('Contact.date', array(
  2055. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2056. ));
  2057. $this->assertRegExp('/class="customClass"/', $result);
  2058. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2059. $result = $this->Form->input('Contact.date', array(
  2060. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2061. ));
  2062. $this->assertRegExp('/id="customIdDay"/', $result);
  2063. $this->assertRegExp('/id="customIdMonth"/', $result);
  2064. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2065. $result = $this->Form->input('Model.field', array(
  2066. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2067. ));
  2068. $this->assertRegExp('/id="customIDDay"/', $result);
  2069. $this->assertRegExp('/id="customIDHour"/', $result);
  2070. $result = explode('</select><select', $result);
  2071. $result = explode(':', $result[1]);
  2072. $this->assertRegExp('/option value="23"/', $result[0]);
  2073. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2074. $result = $this->Form->input('Model.field', array(
  2075. 'type' => 'datetime', 'timeFormat' => 12
  2076. ));
  2077. $result = explode('</select><select', $result);
  2078. $result = explode(':', $result[1]);
  2079. $this->assertRegExp('/option value="12"/', $result[0]);
  2080. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2081. $this->Form->request->data = array('Contact' => array('created' => null));
  2082. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2083. $expected = array(
  2084. 'div' => array('class' => 'input date'),
  2085. 'label' => array('for' => 'ContactCreatedMonth'),
  2086. 'Created',
  2087. '/label',
  2088. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2089. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2090. $monthsRegex,
  2091. '/select', '-',
  2092. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2093. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2094. $daysRegex,
  2095. '/select', '-',
  2096. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2097. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2098. $yearsRegex,
  2099. '/select',
  2100. '/div'
  2101. );
  2102. $this->assertTags($result, $expected);
  2103. $this->Form->request->data = array('Contact' => array('created' => null));
  2104. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2105. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2106. $this->Form->request->data = array('Contact' => array('created' => null));
  2107. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2108. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2109. $result = $this->Form->input('Contact.created', array(
  2110. 'type' => 'date',
  2111. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2112. 'timeFormat' => 'NONE'
  2113. ));
  2114. $this->assertRegExp('/for\="created-month"/', $result);
  2115. }
  2116. /**
  2117. * Test generating checkboxes in a loop.
  2118. *
  2119. * @return void
  2120. */
  2121. public function testInputCheckboxesInLoop() {
  2122. for ($i = 1; $i < 5; $i++) {
  2123. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2124. $expected = array(
  2125. 'div' => array('class' => 'input checkbox'),
  2126. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2127. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2128. 'label' => array('for' => "Contact{$i}Email"),
  2129. 'Email',
  2130. '/label',
  2131. '/div'
  2132. );
  2133. $this->assertTags($result, $expected);
  2134. }
  2135. }
  2136. /**
  2137. * Test generating checkboxes with disabled elements.
  2138. *
  2139. * @return void
  2140. */
  2141. public function testInputCheckboxWithDisabledElements() {
  2142. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2143. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2144. $expected = array(
  2145. array('div' => array('class' => 'input select')),
  2146. array('label' => array('for' => "ContactMultiple")),
  2147. 'Multiple',
  2148. '/label',
  2149. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2150. array('div' => array('class' => 'checkbox')),
  2151. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2152. array('label' => array('for' => "ContactMultiple1")),
  2153. 'One',
  2154. '/label',
  2155. '/div',
  2156. array('div' => array('class' => 'checkbox')),
  2157. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2158. array('label' => array('for' => "ContactMultiple2")),
  2159. 'Two',
  2160. '/label',
  2161. '/div',
  2162. array('div' => array('class' => 'checkbox')),
  2163. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2164. array('label' => array('for' => "ContactMultiple3")),
  2165. 'Three',
  2166. '/label',
  2167. '/div',
  2168. '/div'
  2169. );
  2170. $this->assertTags($result, $expected);
  2171. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2172. $this->assertTags($result, $expected);
  2173. $disabled = array('2', 3);
  2174. $expected = array(
  2175. array('div' => array('class' => 'input select')),
  2176. array('label' => array('for' => "ContactMultiple")),
  2177. 'Multiple',
  2178. '/label',
  2179. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2180. array('div' => array('class' => 'checkbox')),
  2181. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2182. array('label' => array('for' => "ContactMultiple1")),
  2183. 'One',
  2184. '/label',
  2185. '/div',
  2186. array('div' => array('class' => 'checkbox')),
  2187. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2188. array('label' => array('for' => "ContactMultiple2")),
  2189. 'Two',
  2190. '/label',
  2191. '/div',
  2192. array('div' => array('class' => 'checkbox')),
  2193. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2194. array('label' => array('for' => "ContactMultiple3")),
  2195. 'Three',
  2196. '/label',
  2197. '/div',
  2198. '/div'
  2199. );
  2200. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2201. $this->assertTags($result, $expected);
  2202. }
  2203. /**
  2204. * test input name with leading integer, ensure attributes are generated correctly.
  2205. *
  2206. * @return void
  2207. */
  2208. public function testInputWithLeadingInteger() {
  2209. $result = $this->Form->text('0.Node.title');
  2210. $expected = array(
  2211. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2212. );
  2213. $this->assertTags($result, $expected);
  2214. }
  2215. /**
  2216. * test form->input() with select type inputs.
  2217. *
  2218. * @return void
  2219. */
  2220. public function testInputSelectType() {
  2221. $result = $this->Form->input('email', array(
  2222. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2223. );
  2224. $expected = array(
  2225. 'div' => array('class' => 'input select'),
  2226. 'label' => array('for' => 'email'),
  2227. 'Email',
  2228. '/label',
  2229. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2230. array('option' => array('value' => '')),
  2231. '/option',
  2232. array('option' => array('value' => 'è')),
  2233. 'Firést',
  2234. '/option',
  2235. array('option' => array('value' => 'é')),
  2236. 'Secoènd',
  2237. '/option',
  2238. '/select',
  2239. '/div'
  2240. );
  2241. $this->assertTags($result, $expected);
  2242. $result = $this->Form->input('email', array(
  2243. 'options' => array('First', 'Second'), 'empty' => true)
  2244. );
  2245. $expected = array(
  2246. 'div' => array('class' => 'input select'),
  2247. 'label' => array('for' => 'email'),
  2248. 'Email',
  2249. '/label',
  2250. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2251. array('option' => array('value' => '')),
  2252. '/option',
  2253. array('option' => array('value' => '0')),
  2254. 'First',
  2255. '/option',
  2256. array('option' => array('value' => '1')),
  2257. 'Second',
  2258. '/option',
  2259. '/select',
  2260. '/div'
  2261. );
  2262. $this->assertTags($result, $expected);
  2263. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2264. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2265. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2266. $expected = array(
  2267. 'div' => array('class' => 'input select'),
  2268. 'label' => array('for' => 'ModelUserId'),
  2269. 'User',
  2270. '/label',
  2271. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2272. array('option' => array('value' => '')),
  2273. '/option',
  2274. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2275. 'good',
  2276. '/option',
  2277. array('option' => array('value' => 'other')),
  2278. 'bad',
  2279. '/option',
  2280. '/select',
  2281. '/div'
  2282. );
  2283. $this->assertTags($result, $expected);
  2284. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2285. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2286. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2287. $expected = array(
  2288. 'div' => array('class' => 'input select'),
  2289. 'label' => array('for' => 'ThingUserId'),
  2290. 'User',
  2291. '/label',
  2292. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2293. array('option' => array('value' => '')),
  2294. 'Some Empty',
  2295. '/option',
  2296. array('option' => array('value' => 'value')),
  2297. 'good',
  2298. '/option',
  2299. array('option' => array('value' => 'other')),
  2300. 'bad',
  2301. '/option',
  2302. '/select',
  2303. '/div'
  2304. );
  2305. $this->assertTags($result, $expected);
  2306. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2307. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2308. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2309. $expected = array(
  2310. 'div' => array('class' => 'input select'),
  2311. 'label' => array('for' => 'ThingUserId'),
  2312. 'User',
  2313. '/label',
  2314. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2315. array('option' => array('value' => '')),
  2316. 'Some Empty',
  2317. '/option',
  2318. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2319. 'good',
  2320. '/option',
  2321. array('option' => array('value' => 'other')),
  2322. 'bad',
  2323. '/option',
  2324. '/select',
  2325. '/div'
  2326. );
  2327. $this->assertTags($result, $expected);
  2328. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2329. $this->Form->request->data = array('User' => array('User' => array('value')));
  2330. $result = $this->Form->input('User.User', array('empty' => true));
  2331. $expected = array(
  2332. 'div' => array('class' => 'input select'),
  2333. 'label' => array('for' => 'UserUser'),
  2334. 'User',
  2335. '/label',
  2336. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2337. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2338. array('option' => array('value' => '')),
  2339. '/option',
  2340. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2341. 'good',
  2342. '/option',
  2343. array('option' => array('value' => 'other')),
  2344. 'bad',
  2345. '/option',
  2346. '/select',
  2347. '/div'
  2348. );
  2349. $this->assertTags($result, $expected);
  2350. $this->Form->data = array();
  2351. $result = $this->Form->input('Publisher.id', array(
  2352. 'label' => 'Publisher',
  2353. 'type' => 'select',
  2354. 'multiple' => 'checkbox',
  2355. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2356. ));
  2357. $expected = array(
  2358. array('div' => array('class' => 'input select')),
  2359. array('label' => array('for' => 'PublisherId')),
  2360. 'Publisher',
  2361. '/label',
  2362. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2363. array('div' => array('class' => 'checkbox')),
  2364. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2365. array('label' => array('for' => 'PublisherIdValue1')),
  2366. 'Label 1',
  2367. '/label',
  2368. '/div',
  2369. array('div' => array('class' => 'checkbox')),
  2370. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2371. array('label' => array('for' => 'PublisherIdValue2')),
  2372. 'Label 2',
  2373. '/label',
  2374. '/div',
  2375. '/div'
  2376. );
  2377. $this->assertTags($result, $expected);
  2378. }
  2379. /**
  2380. * test that input() and a non standard primary key makes a hidden input by default.
  2381. *
  2382. * @return void
  2383. */
  2384. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2385. $this->Form->create('User');
  2386. $this->Form->fieldset = array(
  2387. 'User' => array(
  2388. 'fields' => array(
  2389. 'model_id' => array('type' => 'integer')
  2390. ),
  2391. 'validates' => array(),
  2392. 'key' => 'model_id'
  2393. )
  2394. );
  2395. $result = $this->Form->input('model_id');
  2396. $expected = array(
  2397. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2398. );
  2399. $this->assertTags($result, $expected);
  2400. }
  2401. /**
  2402. * test that overriding the magic select type widget is possible
  2403. *
  2404. * @return void
  2405. */
  2406. public function testInputOverridingMagicSelectType() {
  2407. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2408. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2409. $expected = array(
  2410. 'div' => array('class' => 'input text'),
  2411. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2412. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2413. '/div'
  2414. );
  2415. $this->assertTags($result, $expected);
  2416. //Check that magic types still work for plural/singular vars
  2417. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2418. $result = $this->Form->input('Model.type');
  2419. $expected = array(
  2420. 'div' => array('class' => 'input select'),
  2421. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2422. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2423. array('option' => array('value' => 'value')), 'good', '/option',
  2424. array('option' => array('value' => 'other')), 'bad', '/option',
  2425. '/select',
  2426. '/div'
  2427. );
  2428. $this->assertTags($result, $expected);
  2429. }
  2430. /**
  2431. * Test that magic input() selects are created for type=number
  2432. *
  2433. * @return void
  2434. */
  2435. public function testInputMagicSelectForTypeNumber() {
  2436. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2437. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2438. $result = $this->Form->input('ValidateUser.balance');
  2439. $expected = array(
  2440. 'div' => array('class' => 'input select'),
  2441. 'label' => array('for' => 'ValidateUserBalance'),
  2442. 'Balance',
  2443. '/label',
  2444. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2445. array('option' => array('value' => '0')),
  2446. 'nothing',
  2447. '/option',
  2448. array('option' => array('value' => '1', 'selected' => 'selected')),
  2449. 'some',
  2450. '/option',
  2451. array('option' => array('value' => '100')),
  2452. 'a lot',
  2453. '/option',
  2454. '/select',
  2455. '/div'
  2456. );
  2457. $this->assertTags($result, $expected);
  2458. }
  2459. /**
  2460. * Test that magic input() selects can easily be converted into radio types without error.
  2461. *
  2462. * @return void
  2463. */
  2464. public function testInputMagicSelectChangeToRadio() {
  2465. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2466. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2467. $this->assertRegExp('/input type="radio"/', $result);
  2468. }
  2469. /**
  2470. * fields with the same name as the model should work.
  2471. *
  2472. * @return void
  2473. */
  2474. public function testInputWithMatchingFieldAndModelName() {
  2475. $this->Form->create('User');
  2476. $this->Form->fieldset = array(
  2477. 'User' => array(
  2478. 'fields' => array(
  2479. 'User' => array('type' => 'text')
  2480. ),
  2481. 'validates' => array(),
  2482. 'key' => 'id'
  2483. )
  2484. );
  2485. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2486. $result = $this->Form->input('User', array('type' => 'text'));
  2487. $expected = array(
  2488. 'div' => array('class' => 'input text'),
  2489. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2490. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2491. '/div'
  2492. );
  2493. $this->assertTags($result, $expected);
  2494. }
  2495. /**
  2496. * testFormInputs method
  2497. *
  2498. * test correct results from form::inputs().
  2499. *
  2500. * @return void
  2501. */
  2502. public function testFormInputs() {
  2503. $this->Form->create('Contact');
  2504. $result = $this->Form->inputs('The Legend');
  2505. $expected = array(
  2506. '<fieldset',
  2507. '<legend',
  2508. 'The Legend',
  2509. '/legend',
  2510. '*/fieldset',
  2511. );
  2512. $this->assertTags($result, $expected);
  2513. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2514. $expected = array(
  2515. 'fieldset' => array('class' => 'classy-stuff'),
  2516. '<legend',
  2517. 'Field of Dreams',
  2518. '/legend',
  2519. '*/fieldset'
  2520. );
  2521. $this->assertTags($result, $expected);
  2522. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2523. $this->assertTags($result, $expected);
  2524. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2525. $this->assertTags($result, $expected);
  2526. $this->Form->create('Contact');
  2527. $this->Form->request['prefix'] = 'admin';
  2528. $this->Form->request['action'] = 'admin_edit';
  2529. $result = $this->Form->inputs();
  2530. $expected = array(
  2531. '<fieldset',
  2532. '<legend',
  2533. 'Edit Contact',
  2534. '/legend',
  2535. '*/fieldset',
  2536. );
  2537. $this->assertTags($result, $expected);
  2538. $this->Form->create('Contact');
  2539. $result = $this->Form->inputs(false);
  2540. $expected = array(
  2541. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2542. array('div' => array('class' => 'input text')),
  2543. '*/div',
  2544. array('div' => array('class' => 'input email')),
  2545. '*/div',
  2546. array('div' => array('class' => 'input tel')),
  2547. '*/div',
  2548. array('div' => array('class' => 'input password')),
  2549. '*/div',
  2550. array('div' => array('class' => 'input date')),
  2551. '*/div',
  2552. array('div' => array('class' => 'input date')),
  2553. '*/div',
  2554. array('div' => array('class' => 'input datetime')),
  2555. '*/div',
  2556. array('div' => array('class' => 'input number')),
  2557. '*/div',
  2558. array('div' => array('class' => 'input select')),
  2559. '*/div',
  2560. );
  2561. $this->assertTags($result, $expected);
  2562. $this->Form->create('Contact');
  2563. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2564. $expected = array(
  2565. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2566. array('div' => array('class' => 'input text')),
  2567. '*/div',
  2568. array('div' => array('class' => 'input email')),
  2569. '*/div',
  2570. array('div' => array('class' => 'input tel')),
  2571. '*/div',
  2572. array('div' => array('class' => 'input password')),
  2573. '*/div',
  2574. array('div' => array('class' => 'input date')),
  2575. '*/div',
  2576. array('div' => array('class' => 'input date')),
  2577. '*/div',
  2578. array('div' => array('class' => 'input datetime')),
  2579. '*/div',
  2580. array('div' => array('class' => 'input number')),
  2581. '*/div',
  2582. array('div' => array('class' => 'input select')),
  2583. '*/div',
  2584. );
  2585. $this->assertTags($result, $expected);
  2586. $this->Form->create('Contact');
  2587. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2588. $this->assertTags($result, $expected);
  2589. $this->Form->create('Contact');
  2590. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2591. $expected = array(
  2592. 'fieldset' => array(),
  2593. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2594. array('div' => array('class' => 'input text')),
  2595. '*/div',
  2596. array('div' => array('class' => 'input email')),
  2597. '*/div',
  2598. array('div' => array('class' => 'input tel')),
  2599. '*/div',
  2600. array('div' => array('class' => 'input password')),
  2601. '*/div',
  2602. array('div' => array('class' => 'input date')),
  2603. '*/div',
  2604. array('div' => array('class' => 'input date')),
  2605. '*/div',
  2606. array('div' => array('class' => 'input datetime')),
  2607. '*/div',
  2608. array('div' => array('class' => 'input number')),
  2609. '*/div',
  2610. array('div' => array('class' => 'input select')),
  2611. '*/div',
  2612. '/fieldset'
  2613. );
  2614. $this->assertTags($result, $expected);
  2615. $this->Form->create('Contact');
  2616. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2617. $expected = array(
  2618. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2619. array('div' => array('class' => 'input text')),
  2620. '*/div',
  2621. array('div' => array('class' => 'input email')),
  2622. '*/div',
  2623. array('div' => array('class' => 'input tel')),
  2624. '*/div',
  2625. array('div' => array('class' => 'input password')),
  2626. '*/div',
  2627. array('div' => array('class' => 'input date')),
  2628. '*/div',
  2629. array('div' => array('class' => 'input date')),
  2630. '*/div',
  2631. array('div' => array('class' => 'input datetime')),
  2632. '*/div',
  2633. array('div' => array('class' => 'input number')),
  2634. '*/div',
  2635. array('div' => array('class' => 'input select')),
  2636. '*/div',
  2637. );
  2638. $this->assertTags($result, $expected);
  2639. $this->Form->create('Contact');
  2640. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2641. $this->assertTags($result, $expected);
  2642. $this->Form->create('Contact');
  2643. $result = $this->Form->inputs('Hello');
  2644. $expected = array(
  2645. 'fieldset' => array(),
  2646. 'legend' => array(),
  2647. 'Hello',
  2648. '/legend',
  2649. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2650. array('div' => array('class' => 'input text')),
  2651. '*/div',
  2652. array('div' => array('class' => 'input email')),
  2653. '*/div',
  2654. array('div' => array('class' => 'input tel')),
  2655. '*/div',
  2656. array('div' => array('class' => 'input password')),
  2657. '*/div',
  2658. array('div' => array('class' => 'input date')),
  2659. '*/div',
  2660. array('div' => array('class' => 'input date')),
  2661. '*/div',
  2662. array('div' => array('class' => 'input datetime')),
  2663. '*/div',
  2664. array('div' => array('class' => 'input number')),
  2665. '*/div',
  2666. array('div' => array('class' => 'input select')),
  2667. '*/div',
  2668. '/fieldset'
  2669. );
  2670. $this->assertTags($result, $expected);
  2671. $this->Form->create('Contact');
  2672. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2673. $expected = array(
  2674. 'fieldset' => array(),
  2675. 'legend' => array(),
  2676. 'Hello',
  2677. '/legend',
  2678. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2679. array('div' => array('class' => 'input text')),
  2680. '*/div',
  2681. array('div' => array('class' => 'input email')),
  2682. '*/div',
  2683. array('div' => array('class' => 'input tel')),
  2684. '*/div',
  2685. array('div' => array('class' => 'input password')),
  2686. '*/div',
  2687. array('div' => array('class' => 'input date')),
  2688. '*/div',
  2689. array('div' => array('class' => 'input date')),
  2690. '*/div',
  2691. array('div' => array('class' => 'input datetime')),
  2692. '*/div',
  2693. array('div' => array('class' => 'input number')),
  2694. '*/div',
  2695. array('div' => array('class' => 'input select')),
  2696. '*/div',
  2697. '/fieldset'
  2698. );
  2699. $this->assertTags($result, $expected);
  2700. $this->Form->create('Contact');
  2701. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2702. $this->assertTags($result, $expected);
  2703. $this->Form->end();
  2704. $this->Form->create(false);
  2705. $expected = array(
  2706. 'fieldset' => array(),
  2707. array('div' => array('class' => 'input text')),
  2708. 'label' => array('for' => 'foo'),
  2709. 'Foo',
  2710. '/label',
  2711. 'input' => array('type' => 'text', 'name' => 'data[foo]', 'id' => 'foo'),
  2712. '*/div',
  2713. '/fieldset'
  2714. );
  2715. $result = $this->Form->inputs(
  2716. array('foo' => array('type' => 'text')),
  2717. array(),
  2718. array('legend' => false)
  2719. );
  2720. $this->assertTags($result, $expected);
  2721. }
  2722. /**
  2723. * testSelectAsCheckbox method
  2724. *
  2725. * test multi-select widget with checkbox formatting.
  2726. *
  2727. * @return void
  2728. */
  2729. public function testSelectAsCheckbox() {
  2730. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2731. $expected = array(
  2732. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2733. array('div' => array('class' => 'checkbox')),
  2734. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2735. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2736. 'first',
  2737. '/label',
  2738. '/div',
  2739. array('div' => array('class' => 'checkbox')),
  2740. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2741. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2742. 'second',
  2743. '/label',
  2744. '/div',
  2745. array('div' => array('class' => 'checkbox')),
  2746. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2747. array('label' => array('for' => 'ModelMultiField2')),
  2748. 'third',
  2749. '/label',
  2750. '/div',
  2751. );
  2752. $this->assertTags($result, $expected);
  2753. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2754. $expected = array(
  2755. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2756. array('div' => array('class' => 'checkbox')),
  2757. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2758. array('label' => array('for' => 'ModelMultiField12')),
  2759. 'half',
  2760. '/label',
  2761. '/div',
  2762. );
  2763. $this->assertTags($result, $expected);
  2764. }
  2765. /**
  2766. * testLabel method
  2767. *
  2768. * test label generation.
  2769. *
  2770. * @return void
  2771. */
  2772. public function testLabel() {
  2773. $this->Form->text('Person.name');
  2774. $result = $this->Form->label();
  2775. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2776. $this->Form->text('Person.name');
  2777. $result = $this->Form->label();
  2778. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2779. $result = $this->Form->label('Person.first_name');
  2780. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2781. $result = $this->Form->label('Person.first_name', 'Your first name');
  2782. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2783. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2784. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2785. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2786. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2787. $result = $this->Form->label('Person.first_name', '');
  2788. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2789. $result = $this->Form->label('Person.2.name', '');
  2790. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2791. }
  2792. /**
  2793. * testTextbox method
  2794. *
  2795. * test textbox element generation
  2796. *
  2797. * @return void
  2798. */
  2799. public function testTextbox() {
  2800. $result = $this->Form->text('Model.field');
  2801. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2802. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2803. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2804. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2805. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2806. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2807. $result = $this->Form->text('Model.text');
  2808. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2809. $Contact = ClassRegistry::getObject('Contact');
  2810. $Contact->validationErrors['text'] = array(true);
  2811. $this->Form->request->data['Contact']['text'] = 'test';
  2812. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2813. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2814. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2815. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2816. $expected = array(
  2817. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2818. );
  2819. $this->assertTags($result, $expected);
  2820. }
  2821. /**
  2822. * testDefaultValue method
  2823. *
  2824. * Test default value setting
  2825. *
  2826. * @return void
  2827. */
  2828. public function testDefaultValue() {
  2829. $this->Form->request->data['Model']['field'] = 'test';
  2830. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2831. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  2832. unset($this->Form->request->data['Model']['field']);
  2833. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2834. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  2835. }
  2836. /**
  2837. * testCheckboxDefaultValue method
  2838. *
  2839. * Test default value setting on checkbox() method
  2840. *
  2841. * @return void
  2842. */
  2843. public function testCheckboxDefaultValue() {
  2844. $this->Form->request->data['Model']['field'] = false;
  2845. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2846. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2847. unset($this->Form->request->data['Model']['field']);
  2848. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2849. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2850. $this->Form->request->data['Model']['field'] = true;
  2851. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2852. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2853. unset($this->Form->request->data['Model']['field']);
  2854. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2855. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2856. }
  2857. /**
  2858. * testError method
  2859. *
  2860. * Test field error generation
  2861. *
  2862. * @return void
  2863. */
  2864. public function testError() {
  2865. $Contact = ClassRegistry::getObject('Contact');
  2866. $Contact->validationErrors['field'] = array(1);
  2867. $result = $this->Form->error('Contact.field');
  2868. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  2869. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2870. $this->assertEquals('Error in field Field', $result);
  2871. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2872. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2873. $this->assertEquals('This field contains invalid input', $result);
  2874. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2875. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  2876. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  2877. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  2878. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  2879. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  2880. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2881. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  2882. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2883. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  2884. $this->assertEquals('<strong>Badness!</strong>', $result);
  2885. $Contact->validationErrors['field'] = array("email");
  2886. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  2887. $expected = array(
  2888. 'div' => array('class' => 'field-error'),
  2889. 'No good!',
  2890. '/div'
  2891. );
  2892. $this->assertTags($result, $expected);
  2893. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  2894. $result = $this->Form->error('Contact.field', array(
  2895. 'notEmpty' => 'Cannot be empty',
  2896. 'email' => 'No good!'
  2897. ));
  2898. $expected = array(
  2899. 'div' => array('class' => 'error-message'),
  2900. 'ul' => array(),
  2901. '<li', 'Cannot be empty', '/li',
  2902. '<li', 'No good!', '/li',
  2903. '<li', 'Something else', '/li',
  2904. '/ul',
  2905. '/div'
  2906. );
  2907. $this->assertTags($result, $expected);
  2908. // Testing error messages list options
  2909. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  2910. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  2911. $expected = array(
  2912. 'div' => array('class' => 'error-message'),
  2913. 'ol' => array(),
  2914. '<li', 'notEmpty', '/li',
  2915. '<li', 'email', '/li',
  2916. '/ol',
  2917. '/div'
  2918. );
  2919. $this->assertTags($result, $expected);
  2920. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  2921. $expected = array(
  2922. 'div' => array('class' => 'error-message'),
  2923. 'ol' => array(),
  2924. '<li', 'notEmpty', '/li',
  2925. '<li', 'email', '/li',
  2926. '/ol',
  2927. '/div'
  2928. );
  2929. $this->assertTags($result, $expected);
  2930. $result = $this->Form->error('Contact.field', null, array(
  2931. 'listOptions' => array(
  2932. 'class' => 'ul-class',
  2933. 'itemOptions' => array(
  2934. 'class' => 'li-class'
  2935. )
  2936. )
  2937. ));
  2938. $expected = array(
  2939. 'div' => array('class' => 'error-message'),
  2940. 'ul' => array('class' => 'ul-class'),
  2941. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  2942. array('li' => array('class' => 'li-class')), 'email', '/li',
  2943. '/ul',
  2944. '/div'
  2945. );
  2946. $this->assertTags($result, $expected);
  2947. }
  2948. /**
  2949. * test error options when using form->input();
  2950. *
  2951. * @return void
  2952. */
  2953. public function testInputErrorEscape() {
  2954. $this->Form->create('ValidateProfile');
  2955. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  2956. $ValidateProfile->validationErrors['city'] = array('required<br>');
  2957. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
  2958. $this->assertRegExp('/required&lt;br&gt;/', $result);
  2959. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => false))));
  2960. $this->assertRegExp('/required<br>/', $result);
  2961. }
  2962. /**
  2963. * testPassword method
  2964. *
  2965. * Test password element generation
  2966. *
  2967. * @return void
  2968. */
  2969. public function testPassword() {
  2970. $Contact = ClassRegistry::getObject('Contact');
  2971. $result = $this->Form->password('Contact.field');
  2972. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  2973. $Contact->validationErrors['passwd'] = 1;
  2974. $this->Form->request->data['Contact']['passwd'] = 'test';
  2975. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  2976. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2977. }
  2978. /**
  2979. * testRadio method
  2980. *
  2981. * Test radio element set generation
  2982. *
  2983. * @return void
  2984. */
  2985. public function testRadio() {
  2986. $result = $this->Form->radio('Model.field', array('option A'));
  2987. $expected = array(
  2988. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2989. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2990. 'label' => array('for' => 'ModelField0'),
  2991. 'option A',
  2992. '/label'
  2993. );
  2994. $this->assertTags($result, $expected);
  2995. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  2996. $expected = array(
  2997. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2998. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  2999. 'label' => array('for' => 'ModelField12'),
  3000. 'half',
  3001. '/label'
  3002. );
  3003. $this->assertTags($result, $expected);
  3004. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3005. $expected = array(
  3006. 'fieldset' => array(),
  3007. 'legend' => array(),
  3008. 'Field',
  3009. '/legend',
  3010. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3011. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3012. array('label' => array('for' => 'ModelField0')),
  3013. 'option A',
  3014. '/label',
  3015. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3016. array('label' => array('for' => 'ModelField1')),
  3017. 'option B',
  3018. '/label',
  3019. '/fieldset'
  3020. );
  3021. $this->assertTags($result, $expected);
  3022. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3023. $expected = array(
  3024. 'fieldset' => array(),
  3025. 'legend' => array(),
  3026. 'Field',
  3027. '/legend',
  3028. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3029. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3030. array('label' => array('for' => 'ModelField0')),
  3031. 'option A',
  3032. '/label',
  3033. 'br' => array(),
  3034. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3035. array('label' => array('for' => 'ModelField1')),
  3036. 'option B',
  3037. '/label',
  3038. '/fieldset'
  3039. );
  3040. $this->assertTags($result, $expected);
  3041. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3042. $expected = array(
  3043. 'fieldset' => array(),
  3044. 'legend' => array(),
  3045. 'Gender',
  3046. '/legend',
  3047. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3048. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3049. array('label' => array('for' => 'EmployeeGenderMale')),
  3050. 'Male',
  3051. '/label',
  3052. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3053. array('label' => array('for' => 'EmployeeGenderFemale')),
  3054. 'Female',
  3055. '/label',
  3056. '/fieldset',
  3057. );
  3058. $this->assertTags($result, $expected);
  3059. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3060. $expected = array(
  3061. 'fieldset' => array(),
  3062. 'legend' => array(),
  3063. 'Gender',
  3064. '/legend',
  3065. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3066. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3067. array('label' => array('for' => 'OfficerGenderMale')),
  3068. 'Male',
  3069. '/label',
  3070. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3071. array('label' => array('for' => 'OfficerGenderFemale')),
  3072. 'Female',
  3073. '/label',
  3074. '/fieldset',
  3075. );
  3076. $this->assertTags($result, $expected);
  3077. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3078. $expected = array(
  3079. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3080. array('input' => array(
  3081. 'type' => 'radio',
  3082. 'name' => 'data[Contact][1][imrequired]',
  3083. 'value' => '0',
  3084. 'id' => 'Contact1Imrequired0',
  3085. 'required' => 'required'
  3086. )),
  3087. 'label' => array('for' => 'Contact1Imrequired0'),
  3088. 'option A',
  3089. '/label'
  3090. );
  3091. $this->assertTags($result, $expected);
  3092. $result = $this->Form->radio('Model.1.field', array('option A'));
  3093. $expected = array(
  3094. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3095. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3096. 'label' => array('for' => 'Model1Field0'),
  3097. 'option A',
  3098. '/label'
  3099. );
  3100. $this->assertTags($result, $expected);
  3101. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3102. $expected = array(
  3103. 'fieldset' => array(),
  3104. 'legend' => array(),
  3105. 'Field',
  3106. '/legend',
  3107. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3108. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3109. array('label' => array('for' => 'ModelField0')),
  3110. 'option A',
  3111. '/label',
  3112. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3113. array('label' => array('for' => 'ModelField1')),
  3114. 'option B',
  3115. '/label',
  3116. '/fieldset'
  3117. );
  3118. $this->assertTags($result, $expected);
  3119. $result = $this->Form->radio(
  3120. 'Model.field',
  3121. array('option A', 'option B'),
  3122. array('between' => 'I am between')
  3123. );
  3124. $expected = array(
  3125. 'fieldset' => array(),
  3126. 'legend' => array(),
  3127. 'Field',
  3128. '/legend',
  3129. 'I am between',
  3130. 'input' => array(
  3131. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3132. 'value' => '', 'id' => 'ModelField_'
  3133. ),
  3134. array('input' => array(
  3135. 'type' => 'radio', 'name' => 'data[Model][field]',
  3136. 'value' => '0', 'id' => 'ModelField0'
  3137. )),
  3138. array('label' => array('for' => 'ModelField0')),
  3139. 'option A',
  3140. '/label',
  3141. array('input' => array(
  3142. 'type' => 'radio', 'name' => 'data[Model][field]',
  3143. 'value' => '1', 'id' => 'ModelField1'
  3144. )),
  3145. array('label' => array('for' => 'ModelField1')),
  3146. 'option B',
  3147. '/label',
  3148. '/fieldset'
  3149. );
  3150. $this->assertTags($result, $expected);
  3151. $result = $this->Form->radio(
  3152. 'Model.field',
  3153. array('option A', 'option B', 'option C'),
  3154. array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
  3155. );
  3156. $expected = array(
  3157. 'fieldset' => array(),
  3158. 'legend' => array(),
  3159. 'Field',
  3160. '/legend',
  3161. 'input' => array(
  3162. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3163. 'value' => '', 'id' => 'ModelField_'
  3164. ),
  3165. array('input' => array(
  3166. 'type' => 'radio', 'name' => 'data[Model][field]',
  3167. 'value' => '0', 'id' => 'ModelField0'
  3168. )),
  3169. array('label' => array('for' => 'ModelField0')),
  3170. 'option A',
  3171. '/label',
  3172. 'between A',
  3173. '--separator--',
  3174. array('input' => array(
  3175. 'type' => 'radio', 'name' => 'data[Model][field]',
  3176. 'value' => '1', 'id' => 'ModelField1'
  3177. )),
  3178. array('label' => array('for' => 'ModelField1')),
  3179. 'option B',
  3180. '/label',
  3181. 'between B',
  3182. '--separator--',
  3183. array('input' => array(
  3184. 'type' => 'radio', 'name' => 'data[Model][field]',
  3185. 'value' => '2', 'id' => 'ModelField2'
  3186. )),
  3187. array('label' => array('for' => 'ModelField2')),
  3188. 'option C',
  3189. '/label',
  3190. 'between C',
  3191. '/fieldset'
  3192. );
  3193. $this->assertTags($result, $expected);
  3194. }
  3195. /**
  3196. * Test that radios with a 0 value are selected under the correct conditions.
  3197. * Also ensure that values that are booleanish are handled correctly.
  3198. *
  3199. * @return void
  3200. */
  3201. public function testRadioOptionWithBooleanishValues() {
  3202. $expected = array(
  3203. 'fieldset' => array(),
  3204. 'legend' => array(),
  3205. 'Field',
  3206. '/legend',
  3207. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3208. array('label' => array('for' => 'ModelField1')),
  3209. 'Yes',
  3210. '/label',
  3211. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3212. array('label' => array('for' => 'ModelField0')),
  3213. 'No',
  3214. '/label',
  3215. '/fieldset'
  3216. );
  3217. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3218. $this->assertTags($result, $expected);
  3219. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3220. $this->assertTags($result, $expected);
  3221. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3222. $this->assertTags($result, $expected);
  3223. $expected = array(
  3224. 'fieldset' => array(),
  3225. 'legend' => array(),
  3226. 'Field',
  3227. '/legend',
  3228. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3229. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3230. array('label' => array('for' => 'ModelField1')),
  3231. 'Yes',
  3232. '/label',
  3233. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3234. array('label' => array('for' => 'ModelField0')),
  3235. 'No',
  3236. '/label',
  3237. '/fieldset'
  3238. );
  3239. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3240. $this->assertTags($result, $expected);
  3241. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3242. $this->assertTags($result, $expected);
  3243. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3244. $this->assertTags($result, $expected);
  3245. $expected = array(
  3246. 'fieldset' => array(),
  3247. 'legend' => array(),
  3248. 'Field',
  3249. '/legend',
  3250. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3251. array('label' => array('for' => 'ModelField1')),
  3252. 'Yes',
  3253. '/label',
  3254. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3255. array('label' => array('for' => 'ModelField0')),
  3256. 'No',
  3257. '/label',
  3258. '/fieldset'
  3259. );
  3260. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3261. $this->assertTags($result, $expected);
  3262. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3263. $this->assertTags($result, $expected);
  3264. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3265. $this->assertTags($result, $expected);
  3266. }
  3267. /**
  3268. * test disabled radio options
  3269. *
  3270. * @return void
  3271. */
  3272. public function testRadioDisabled() {
  3273. $result = $this->Form->radio(
  3274. 'Model.field',
  3275. array('option A', 'option B'),
  3276. array('disabled' => array('option A'), 'value' => '0')
  3277. );
  3278. $expected = array(
  3279. 'fieldset' => array(),
  3280. 'legend' => array(),
  3281. 'Field',
  3282. '/legend',
  3283. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3284. array('label' => array('for' => 'ModelField0')),
  3285. 'option A',
  3286. '/label',
  3287. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3288. array('label' => array('for' => 'ModelField1')),
  3289. 'option B',
  3290. '/label',
  3291. '/fieldset'
  3292. );
  3293. $this->assertTags($result, $expected);
  3294. $result = $this->Form->radio(
  3295. 'Model.field',
  3296. array('option A', 'option B'),
  3297. array('disabled' => true, 'value' => '0')
  3298. );
  3299. $expected = array(
  3300. 'fieldset' => array(),
  3301. 'legend' => array(),
  3302. 'Field',
  3303. '/legend',
  3304. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3305. array('label' => array('for' => 'ModelField0')),
  3306. 'option A',
  3307. '/label',
  3308. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3309. array('label' => array('for' => 'ModelField1')),
  3310. 'option B',
  3311. '/label',
  3312. '/fieldset'
  3313. );
  3314. $this->assertTags($result, $expected);
  3315. $result = $this->Form->radio(
  3316. 'Model.field',
  3317. array('option A', 'option B'),
  3318. array('disabled' => 'disabled', 'value' => '0')
  3319. );
  3320. $expected = array(
  3321. 'fieldset' => array(),
  3322. 'legend' => array(),
  3323. 'Field',
  3324. '/legend',
  3325. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3326. array('label' => array('for' => 'ModelField0')),
  3327. 'option A',
  3328. '/label',
  3329. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3330. array('label' => array('for' => 'ModelField1')),
  3331. 'option B',
  3332. '/label',
  3333. '/fieldset'
  3334. );
  3335. $this->assertTags($result, $expected);
  3336. $result = $this->Form->input('Model.field', array(
  3337. 'options' => array('1' => 'first', '2' => 'second'),
  3338. 'type' => 'radio',
  3339. 'before' => '--before--',
  3340. 'after' => '--after--',
  3341. 'separator' => '--separator--',
  3342. 'between' => array('--between first--', '--between second--')
  3343. ));
  3344. $expected = array(
  3345. 'div' => array('class' => 'input radio'),
  3346. '--before--',
  3347. 'fieldset' => array(),
  3348. 'legend' => array(),
  3349. 'Field',
  3350. '/legend',
  3351. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3352. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3353. array('label' => array('for' => 'ModelField1')),
  3354. 'first',
  3355. '/label',
  3356. '--between first--',
  3357. '--separator--',
  3358. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3359. array('label' => array('for' => 'ModelField2')),
  3360. 'second',
  3361. '/label',
  3362. '--between second--',
  3363. '/fieldset',
  3364. '--after--',
  3365. '/div'
  3366. );
  3367. $this->assertTags($result, $expected);
  3368. $result = $this->Form->input('Model.field', array(
  3369. 'options' => array('1' => 'first', '2' => 'second'),
  3370. 'type' => 'radio',
  3371. 'before' => '--before--',
  3372. 'after' => '--after--',
  3373. 'separator' => '--separator--',
  3374. 'between' => array('--between first--')
  3375. ));
  3376. $expected = array(
  3377. 'div' => array('class' => 'input radio'),
  3378. '--before--',
  3379. 'fieldset' => array(),
  3380. 'legend' => array(),
  3381. 'Field',
  3382. '/legend',
  3383. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3384. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3385. array('label' => array('for' => 'ModelField1')),
  3386. 'first',
  3387. '/label',
  3388. '--between first--',
  3389. '--separator--',
  3390. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3391. array('label' => array('for' => 'ModelField2')),
  3392. 'second',
  3393. '/label',
  3394. '/fieldset',
  3395. '--after--',
  3396. '/div'
  3397. );
  3398. }
  3399. /**
  3400. * test disabling the hidden input for radio buttons
  3401. *
  3402. * @return void
  3403. */
  3404. public function testRadioHiddenInputDisabling() {
  3405. $result = $this->Form->input('Model.1.field', array(
  3406. 'type' => 'radio',
  3407. 'options' => array('option A'),
  3408. 'hiddenField' => false
  3409. )
  3410. );
  3411. $expected = array(
  3412. 'div' => array('class' => 'input radio'),
  3413. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3414. 'label' => array('for' => 'Model1Field0'),
  3415. 'option A',
  3416. '/label',
  3417. '/div'
  3418. );
  3419. $this->assertTags($result, $expected);
  3420. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3421. $expected = array(
  3422. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3423. 'label' => array('for' => 'Model1Field0'),
  3424. 'option A',
  3425. '/label'
  3426. );
  3427. $this->assertTags($result, $expected);
  3428. }
  3429. /**
  3430. * test adding an empty option for radio buttons
  3431. *
  3432. * @return void
  3433. */
  3434. public function testRadioAddEmptyOption() {
  3435. $result = $this->Form->input('Model.1.field', array(
  3436. 'type' => 'radio',
  3437. 'options' => array('option A'),
  3438. 'empty' => true,
  3439. 'hiddenField' => false
  3440. ));
  3441. $expected = array(
  3442. 'div' => array('class' => 'input radio'),
  3443. 'fieldset' => array(),
  3444. 'legend' => array(),
  3445. 'Field',
  3446. '/legend',
  3447. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3448. array('label' => array('for' => 'Model1Field')),
  3449. __('empty'),
  3450. '/label',
  3451. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3452. array('label' => array('for' => 'Model1Field0')),
  3453. 'option A',
  3454. '/label',
  3455. '/fieldset',
  3456. '/div'
  3457. );
  3458. $this->assertTags($result, $expected);
  3459. $result = $this->Form->input('Model.1.field', array(
  3460. 'type' => 'radio',
  3461. 'options' => array('option A'),
  3462. 'empty' => 'CustomEmptyLabel',
  3463. 'hiddenField' => false
  3464. ));
  3465. $expected = array(
  3466. 'div' => array('class' => 'input radio'),
  3467. 'fieldset' => array(),
  3468. 'legend' => array(),
  3469. 'Field',
  3470. '/legend',
  3471. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3472. array('label' => array('for' => 'Model1Field')),
  3473. 'CustomEmptyLabel',
  3474. '/label',
  3475. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3476. array('label' => array('for' => 'Model1Field0')),
  3477. 'option A',
  3478. '/label',
  3479. '/fieldset',
  3480. '/div'
  3481. );
  3482. $this->assertTags($result, $expected);
  3483. $result = $this->Form->input('Model.1.field', array(
  3484. 'type' => 'radio',
  3485. 'options' => array('option A'),
  3486. 'empty' => false,
  3487. 'hiddenField' => false
  3488. ));
  3489. $this->assertTextNotContains('"Model1Field"', $result);
  3490. }
  3491. /**
  3492. * testSelect method
  3493. *
  3494. * Test select element generation.
  3495. *
  3496. * @return void
  3497. */
  3498. public function testSelect() {
  3499. $result = $this->Form->select('Model.field', array());
  3500. $expected = array(
  3501. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3502. array('option' => array('value' => '')),
  3503. '/option',
  3504. '/select'
  3505. );
  3506. $this->assertTags($result, $expected);
  3507. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3508. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3509. $expected = array(
  3510. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3511. array('option' => array('value' => '')),
  3512. '/option',
  3513. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3514. 'good',
  3515. '/option',
  3516. array('option' => array('value' => 'other')),
  3517. 'bad',
  3518. '/option',
  3519. '/select'
  3520. );
  3521. $this->assertTags($result, $expected);
  3522. $this->Form->request->data = array();
  3523. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3524. $expected = array(
  3525. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3526. array('option' => array('value' => '')),
  3527. '/option',
  3528. array('option' => array('value' => 'value')),
  3529. 'good',
  3530. '/option',
  3531. array('option' => array('value' => 'other')),
  3532. 'bad',
  3533. '/option',
  3534. '/select'
  3535. );
  3536. $this->assertTags($result, $expected);
  3537. $result = $this->Form->select(
  3538. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3539. array('empty' => false)
  3540. );
  3541. $expected = array(
  3542. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3543. array('option' => array('value' => 'first')),
  3544. 'first &quot;html&quot; &lt;chars&gt;',
  3545. '/option',
  3546. array('option' => array('value' => 'second')),
  3547. 'value',
  3548. '/option',
  3549. '/select'
  3550. );
  3551. $this->assertTags($result, $expected);
  3552. $result = $this->Form->select(
  3553. 'Model.field',
  3554. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3555. array('escape' => false, 'empty' => false)
  3556. );
  3557. $expected = array(
  3558. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3559. array('option' => array('value' => 'first')),
  3560. 'first "html" <chars>',
  3561. '/option',
  3562. array('option' => array('value' => 'second')),
  3563. 'value',
  3564. '/option',
  3565. '/select'
  3566. );
  3567. $this->assertTags($result, $expected);
  3568. $options = array(
  3569. array('value' => 'first', 'name' => 'First'),
  3570. array('value' => 'first', 'name' => 'Another First'),
  3571. );
  3572. $result = $this->Form->select(
  3573. 'Model.field',
  3574. $options,
  3575. array('escape' => false, 'empty' => false)
  3576. );
  3577. $expected = array(
  3578. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3579. array('option' => array('value' => 'first')),
  3580. 'First',
  3581. '/option',
  3582. array('option' => array('value' => 'first')),
  3583. 'Another First',
  3584. '/option',
  3585. '/select'
  3586. );
  3587. $this->assertTags($result, $expected);
  3588. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3589. $result = $this->Form->select(
  3590. 'Model.contact_id',
  3591. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3592. array('escape' => false, 'empty' => 'pick something')
  3593. );
  3594. $expected = array(
  3595. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3596. array('option' => array('value' => '')), 'pick something', '/option',
  3597. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3598. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3599. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3600. '/select'
  3601. );
  3602. $this->assertTags($result, $expected);
  3603. $this->Form->request->data['Model']['field'] = 0;
  3604. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3605. $expected = array(
  3606. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3607. array('option' => array('value' => '')), '/option',
  3608. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3609. array('option' => array('value' => '1')), 'Yes', '/option',
  3610. '/select'
  3611. );
  3612. $this->assertTags($result, $expected);
  3613. $this->Form->request->data['Model']['field'] = 50;
  3614. $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
  3615. $expected = array(
  3616. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3617. array('option' => array('value' => '')), '/option',
  3618. array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
  3619. array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
  3620. '/select'
  3621. );
  3622. $this->assertTags($result, $expected);
  3623. }
  3624. /**
  3625. * test that select() with optiongroups listens to the escape param.
  3626. *
  3627. * @return void
  3628. */
  3629. public function testSelectOptionGroupEscaping() {
  3630. $options = array(
  3631. '>< Key' => array(
  3632. 1 => 'One',
  3633. 2 => 'Two'
  3634. )
  3635. );
  3636. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3637. $expected = array(
  3638. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3639. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3640. array('option' => array('value' => '1')), 'One', '/option',
  3641. array('option' => array('value' => '2')), 'Two', '/option',
  3642. '/optgroup',
  3643. '/select'
  3644. );
  3645. $this->assertTags($result, $expected);
  3646. $options = array(
  3647. '>< Key' => array(
  3648. 1 => 'One',
  3649. 2 => 'Two'
  3650. )
  3651. );
  3652. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3653. $expected = array(
  3654. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3655. 'optgroup' => array('label' => '>< Key'),
  3656. array('option' => array('value' => '1')), 'One', '/option',
  3657. array('option' => array('value' => '2')), 'Two', '/option',
  3658. '/optgroup',
  3659. '/select'
  3660. );
  3661. $this->assertTags($result, $expected);
  3662. }
  3663. /**
  3664. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3665. *
  3666. * @return void
  3667. */
  3668. public function testSelectWithNullAttributes() {
  3669. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3670. $expected = array(
  3671. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3672. array('option' => array('value' => '0')),
  3673. 'first',
  3674. '/option',
  3675. array('option' => array('value' => '1')),
  3676. 'second',
  3677. '/option',
  3678. '/select'
  3679. );
  3680. $this->assertTags($result, $expected);
  3681. }
  3682. /**
  3683. * testNestedSelect method
  3684. *
  3685. * test select element generation with optgroups
  3686. *
  3687. * @return void
  3688. */
  3689. public function testNestedSelect() {
  3690. $result = $this->Form->select(
  3691. 'Model.field',
  3692. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3693. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3694. )), array('empty' => false)
  3695. );
  3696. $expected = array(
  3697. 'select' => array('name' => 'data[Model][field]',
  3698. 'id' => 'ModelField'),
  3699. array('option' => array('value' => 1)),
  3700. 'One',
  3701. '/option',
  3702. array('option' => array('value' => 2)),
  3703. 'Two',
  3704. '/option',
  3705. array('optgroup' => array('label' => 'Three')),
  3706. array('option' => array('value' => 4)),
  3707. 'Four',
  3708. '/option',
  3709. array('option' => array('value' => 5)),
  3710. 'Five',
  3711. '/option',
  3712. '/optgroup',
  3713. '/select'
  3714. );
  3715. $this->assertTags($result, $expected);
  3716. $result = $this->Form->select(
  3717. 'Model.field',
  3718. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3719. array('showParents' => true, 'empty' => false)
  3720. );
  3721. $expected = array(
  3722. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3723. array('option' => array('value' => 1)),
  3724. 'One',
  3725. '/option',
  3726. array('option' => array('value' => 2)),
  3727. 'Two',
  3728. '/option',
  3729. array('optgroup' => array('label' => 'Three')),
  3730. array('option' => array('value' => 3)),
  3731. 'Three',
  3732. '/option',
  3733. array('option' => array('value' => 4)),
  3734. 'Four',
  3735. '/option',
  3736. '/optgroup',
  3737. '/select'
  3738. );
  3739. $this->assertTags($result, $expected);
  3740. }
  3741. /**
  3742. * testSelectMultiple method
  3743. *
  3744. * test generation of multiple select elements
  3745. *
  3746. * @return void
  3747. */
  3748. public function testSelectMultiple() {
  3749. $options = array('first', 'second', 'third');
  3750. $result = $this->Form->select(
  3751. 'Model.multi_field', $options, array('multiple' => true)
  3752. );
  3753. $expected = array(
  3754. 'input' => array(
  3755. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3756. ),
  3757. 'select' => array(
  3758. 'name' => 'data[Model][multi_field][]',
  3759. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3760. ),
  3761. array('option' => array('value' => '0')),
  3762. 'first',
  3763. '/option',
  3764. array('option' => array('value' => '1')),
  3765. 'second',
  3766. '/option',
  3767. array('option' => array('value' => '2')),
  3768. 'third',
  3769. '/option',
  3770. '/select'
  3771. );
  3772. $this->assertTags($result, $expected);
  3773. $result = $this->Form->select(
  3774. 'Model.multi_field', $options, array('multiple' => 'multiple')
  3775. );
  3776. $expected = array(
  3777. 'input' => array(
  3778. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3779. ),
  3780. 'select' => array(
  3781. 'name' => 'data[Model][multi_field][]',
  3782. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3783. ),
  3784. array('option' => array('value' => '0')),
  3785. 'first',
  3786. '/option',
  3787. array('option' => array('value' => '1')),
  3788. 'second',
  3789. '/option',
  3790. array('option' => array('value' => '2')),
  3791. 'third',
  3792. '/option',
  3793. '/select'
  3794. );
  3795. $this->assertTags($result, $expected);
  3796. $result = $this->Form->select(
  3797. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  3798. );
  3799. $expected = array(
  3800. 'input' => array(
  3801. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3802. ),
  3803. 'select' => array(
  3804. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  3805. 'multiple' => 'multiple'
  3806. ),
  3807. array('option' => array('value' => '0', 'selected' => 'selected')),
  3808. 'first',
  3809. '/option',
  3810. array('option' => array('value' => '1', 'selected' => 'selected')),
  3811. 'second',
  3812. '/option',
  3813. array('option' => array('value' => '2')),
  3814. 'third',
  3815. '/option',
  3816. '/select'
  3817. );
  3818. $this->assertTags($result, $expected);
  3819. $result = $this->Form->select(
  3820. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  3821. );
  3822. $expected = array(
  3823. 'select' => array(
  3824. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  3825. ),
  3826. array('option' => array('value' => '0', 'selected' => 'selected')),
  3827. 'first',
  3828. '/option',
  3829. array('option' => array('value' => '1', 'selected' => 'selected')),
  3830. 'second',
  3831. '/option',
  3832. array('option' => array('value' => '2')),
  3833. 'third',
  3834. '/option',
  3835. '/select'
  3836. );
  3837. $this->assertTags($result, $expected);
  3838. }
  3839. /**
  3840. * test generation of habtm select boxes.
  3841. *
  3842. * @return void
  3843. */
  3844. public function testHabtmSelectBox() {
  3845. $this->View->viewVars['contactTags'] = array(
  3846. 1 => 'blue',
  3847. 2 => 'red',
  3848. 3 => 'green'
  3849. );
  3850. $this->Form->request->data = array(
  3851. 'Contact' => array(),
  3852. 'ContactTag' => array(
  3853. array(
  3854. 'id' => 1,
  3855. 'name' => 'blue'
  3856. ),
  3857. array(
  3858. 'id' => 3,
  3859. 'name' => 'green'
  3860. )
  3861. )
  3862. );
  3863. $this->Form->create('Contact');
  3864. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3865. $expected = array(
  3866. 'input' => array(
  3867. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3868. ),
  3869. 'select' => array(
  3870. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  3871. 'multiple' => 'multiple'
  3872. ),
  3873. array('option' => array('value' => '1', 'selected' => 'selected')),
  3874. 'blue',
  3875. '/option',
  3876. array('option' => array('value' => '2')),
  3877. 'red',
  3878. '/option',
  3879. array('option' => array('value' => '3', 'selected' => 'selected')),
  3880. 'green',
  3881. '/option',
  3882. '/select'
  3883. );
  3884. $this->assertTags($result, $expected);
  3885. }
  3886. /**
  3887. * test generation of multi select elements in checkbox format
  3888. *
  3889. * @return void
  3890. */
  3891. public function testSelectMultipleCheckboxes() {
  3892. $result = $this->Form->select(
  3893. 'Model.multi_field',
  3894. array('first', 'second', 'third'),
  3895. array('multiple' => 'checkbox')
  3896. );
  3897. $expected = array(
  3898. 'input' => array(
  3899. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3900. ),
  3901. array('div' => array('class' => 'checkbox')),
  3902. array('input' => array(
  3903. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3904. 'value' => '0', 'id' => 'ModelMultiField0'
  3905. )),
  3906. array('label' => array('for' => 'ModelMultiField0')),
  3907. 'first',
  3908. '/label',
  3909. '/div',
  3910. array('div' => array('class' => 'checkbox')),
  3911. array('input' => array(
  3912. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3913. 'value' => '1', 'id' => 'ModelMultiField1'
  3914. )),
  3915. array('label' => array('for' => 'ModelMultiField1')),
  3916. 'second',
  3917. '/label',
  3918. '/div',
  3919. array('div' => array('class' => 'checkbox')),
  3920. array('input' => array(
  3921. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3922. 'value' => '2', 'id' => 'ModelMultiField2'
  3923. )),
  3924. array('label' => array('for' => 'ModelMultiField2')),
  3925. 'third',
  3926. '/label',
  3927. '/div'
  3928. );
  3929. $this->assertTags($result, $expected);
  3930. $result = $this->Form->select(
  3931. 'Model.multi_field',
  3932. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3933. array('multiple' => 'checkbox')
  3934. );
  3935. $expected = array(
  3936. 'input' => array(
  3937. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3938. ),
  3939. array('div' => array('class' => 'checkbox')),
  3940. array('input' => array(
  3941. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3942. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  3943. )),
  3944. array('label' => array('for' => 'ModelMultiFieldA')),
  3945. 'first',
  3946. '/label',
  3947. '/div',
  3948. array('div' => array('class' => 'checkbox')),
  3949. array('input' => array(
  3950. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3951. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  3952. )),
  3953. array('label' => array('for' => 'ModelMultiFieldB')),
  3954. 'second',
  3955. '/label',
  3956. '/div',
  3957. array('div' => array('class' => 'checkbox')),
  3958. array('input' => array(
  3959. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3960. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  3961. )),
  3962. array('label' => array('for' => 'ModelMultiFieldC')),
  3963. 'third',
  3964. '/label',
  3965. '/div'
  3966. );
  3967. $this->assertTags($result, $expected);
  3968. $result = $this->Form->select(
  3969. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  3970. );
  3971. $expected = array(
  3972. 'input' => array(
  3973. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3974. ),
  3975. array('div' => array('class' => 'checkbox')),
  3976. array('input' => array(
  3977. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3978. 'value' => '1', 'id' => 'ModelMultiField1'
  3979. )),
  3980. array('label' => array('for' => 'ModelMultiField1')),
  3981. 'first',
  3982. '/label',
  3983. '/div'
  3984. );
  3985. $this->assertTags($result, $expected);
  3986. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3987. $result = $this->Form->select(
  3988. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3989. );
  3990. $expected = array(
  3991. 'input' => array(
  3992. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3993. ),
  3994. array('div' => array('class' => 'checkbox')),
  3995. array('input' => array(
  3996. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3997. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  3998. )),
  3999. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  4000. 'first',
  4001. '/label',
  4002. '/div',
  4003. array('div' => array('class' => 'checkbox')),
  4004. array('input' => array(
  4005. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4006. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4007. )),
  4008. array('label' => array('for' => 'ModelTagsArray')),
  4009. 'Array',
  4010. '/label',
  4011. '/div'
  4012. );
  4013. $this->assertTags($result, $expected);
  4014. }
  4015. /**
  4016. * test multiple checkboxes with div styles.
  4017. *
  4018. * @return void
  4019. */
  4020. public function testSelectMultipleCheckboxDiv() {
  4021. $result = $this->Form->select(
  4022. 'Model.tags',
  4023. array('first', 'second'),
  4024. array('multiple' => 'checkbox', 'class' => 'my-class')
  4025. );
  4026. $expected = array(
  4027. 'input' => array(
  4028. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4029. ),
  4030. array('div' => array('class' => 'my-class')),
  4031. array('input' => array(
  4032. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4033. 'value' => '0', 'id' => 'ModelTags0'
  4034. )),
  4035. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4036. '/div',
  4037. array('div' => array('class' => 'my-class')),
  4038. array('input' => array(
  4039. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4040. 'value' => '1', 'id' => 'ModelTags1'
  4041. )),
  4042. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4043. '/div'
  4044. );
  4045. $this->assertTags($result, $expected);
  4046. $result = $this->Form->input('Model.tags', array(
  4047. 'options' => array('first', 'second'),
  4048. 'multiple' => 'checkbox',
  4049. 'class' => 'my-class',
  4050. 'div' => false,
  4051. 'label' => false
  4052. ));
  4053. $this->assertTags($result, $expected);
  4054. $Contact = ClassRegistry::getObject('Contact');
  4055. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4056. $result = $this->Form->input('Contact.tags', array(
  4057. 'options' => array('one'),
  4058. 'multiple' => 'checkbox',
  4059. 'label' => false,
  4060. 'div' => false
  4061. ));
  4062. $expected = array(
  4063. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4064. array('div' => array('class' => 'checkbox form-error')),
  4065. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4066. array('label' => array('for' => 'ContactTags0')),
  4067. 'one',
  4068. '/label',
  4069. '/div'
  4070. );
  4071. $this->assertTags($result, $expected);
  4072. $result = $this->Form->input('Contact.tags', array(
  4073. 'options' => array('one'),
  4074. 'multiple' => 'checkbox',
  4075. 'class' => 'mycheckbox',
  4076. 'label' => false,
  4077. 'div' => false
  4078. ));
  4079. $expected = array(
  4080. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4081. array('div' => array('class' => 'mycheckbox form-error')),
  4082. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4083. array('label' => array('for' => 'ContactTags0')),
  4084. 'one',
  4085. '/label',
  4086. '/div'
  4087. );
  4088. $this->assertTags($result, $expected);
  4089. }
  4090. /**
  4091. * Checks the security hash array generated for multiple-input checkbox elements
  4092. *
  4093. * @return void
  4094. */
  4095. public function testSelectMultipleCheckboxSecurity() {
  4096. $this->Form->request['_Token'] = array('key' => 'testKey');
  4097. $this->assertEquals(array(), $this->Form->fields);
  4098. $result = $this->Form->select(
  4099. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4100. array('multiple' => 'checkbox')
  4101. );
  4102. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4103. $result = $this->Form->secure($this->Form->fields);
  4104. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4105. $this->assertRegExp('/"' . $key . '"/', $result);
  4106. }
  4107. /**
  4108. * Multiple select elements should always be secured as they always participate
  4109. * in the POST data.
  4110. *
  4111. * @return void
  4112. */
  4113. public function testSelectMultipleSecureWithNoOptions() {
  4114. $this->Form->request['_Token'] = array('key' => 'testkey');
  4115. $this->assertEquals(array(), $this->Form->fields);
  4116. $this->Form->select(
  4117. 'Model.select',
  4118. array(),
  4119. array('multiple' => true)
  4120. );
  4121. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4122. }
  4123. /**
  4124. * When a select box has no options it should not be added to the fields list
  4125. * as it always fail post validation.
  4126. *
  4127. * @return void
  4128. */
  4129. public function testSelectNoSecureWithNoOptions() {
  4130. $this->Form->request['_Token'] = array('key' => 'testkey');
  4131. $this->assertEquals(array(), $this->Form->fields);
  4132. $this->Form->select(
  4133. 'Model.select',
  4134. array()
  4135. );
  4136. $this->assertEquals(array(), $this->Form->fields);
  4137. $this->Form->select(
  4138. 'Model.select',
  4139. array(),
  4140. array('empty' => true)
  4141. );
  4142. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4143. }
  4144. /**
  4145. * testInputMultipleCheckboxes method
  4146. *
  4147. * test input() resulting in multi select elements being generated.
  4148. *
  4149. * @return void
  4150. */
  4151. public function testInputMultipleCheckboxes() {
  4152. $result = $this->Form->input('Model.multi_field', array(
  4153. 'options' => array('first', 'second', 'third'),
  4154. 'multiple' => 'checkbox'
  4155. ));
  4156. $expected = array(
  4157. array('div' => array('class' => 'input select')),
  4158. array('label' => array('for' => 'ModelMultiField')),
  4159. 'Multi Field',
  4160. '/label',
  4161. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4162. array('div' => array('class' => 'checkbox')),
  4163. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4164. array('label' => array('for' => 'ModelMultiField0')),
  4165. 'first',
  4166. '/label',
  4167. '/div',
  4168. array('div' => array('class' => 'checkbox')),
  4169. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4170. array('label' => array('for' => 'ModelMultiField1')),
  4171. 'second',
  4172. '/label',
  4173. '/div',
  4174. array('div' => array('class' => 'checkbox')),
  4175. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4176. array('label' => array('for' => 'ModelMultiField2')),
  4177. 'third',
  4178. '/label',
  4179. '/div',
  4180. '/div'
  4181. );
  4182. $this->assertTags($result, $expected);
  4183. $result = $this->Form->input('Model.multi_field', array(
  4184. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4185. 'multiple' => 'checkbox'
  4186. ));
  4187. $expected = array(
  4188. array('div' => array('class' => 'input select')),
  4189. array('label' => array('for' => 'ModelMultiField')),
  4190. 'Multi Field',
  4191. '/label',
  4192. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4193. array('div' => array('class' => 'checkbox')),
  4194. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4195. array('label' => array('for' => 'ModelMultiFieldA')),
  4196. 'first',
  4197. '/label',
  4198. '/div',
  4199. array('div' => array('class' => 'checkbox')),
  4200. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4201. array('label' => array('for' => 'ModelMultiFieldB')),
  4202. 'second',
  4203. '/label',
  4204. '/div',
  4205. array('div' => array('class' => 'checkbox')),
  4206. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4207. array('label' => array('for' => 'ModelMultiFieldC')),
  4208. 'third',
  4209. '/label',
  4210. '/div',
  4211. '/div'
  4212. );
  4213. $this->assertTags($result, $expected);
  4214. $result = $this->Form->input('Model.multi_field', array(
  4215. 'options' => array('1' => 'first'),
  4216. 'multiple' => 'checkbox',
  4217. 'label' => false,
  4218. 'div' => false
  4219. ));
  4220. $expected = array(
  4221. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4222. array('div' => array('class' => 'checkbox')),
  4223. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4224. array('label' => array('for' => 'ModelMultiField1')),
  4225. 'first',
  4226. '/label',
  4227. '/div'
  4228. );
  4229. $this->assertTags($result, $expected);
  4230. $result = $this->Form->input('Model.multi_field', array(
  4231. 'options' => array('2' => 'second'),
  4232. 'multiple' => 'checkbox',
  4233. 'label' => false,
  4234. 'div' => false
  4235. ));
  4236. $expected = array(
  4237. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4238. array('div' => array('class' => 'checkbox')),
  4239. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4240. array('label' => array('for' => 'ModelMultiField2')),
  4241. 'second',
  4242. '/label',
  4243. '/div'
  4244. );
  4245. $this->assertTags($result, $expected);
  4246. }
  4247. /**
  4248. * testSelectHiddenFieldOmission method
  4249. *
  4250. * test that select() with 'hiddenField' => false omits the hidden field
  4251. *
  4252. * @return void
  4253. */
  4254. public function testSelectHiddenFieldOmission() {
  4255. $result = $this->Form->select('Model.multi_field',
  4256. array('first', 'second'),
  4257. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4258. );
  4259. $expected = array(
  4260. array('div' => array('class' => 'checkbox')),
  4261. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4262. array('label' => array('for' => 'ModelMultiField0')),
  4263. 'first',
  4264. '/label',
  4265. '/div',
  4266. array('div' => array('class' => 'checkbox')),
  4267. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4268. array('label' => array('for' => 'ModelMultiField1')),
  4269. 'second',
  4270. '/label',
  4271. '/div'
  4272. );
  4273. $this->assertTags($result, $expected);
  4274. $result = $this->Form->input('Model.multi_field', array(
  4275. 'options' => array('first', 'second'),
  4276. 'multiple' => 'checkbox',
  4277. 'hiddenField' => false
  4278. ));
  4279. $expected = array(
  4280. array('div' => array('class' => 'input select')),
  4281. array('label' => array('for' => 'ModelMultiField')),
  4282. 'Multi Field',
  4283. '/label',
  4284. array('div' => array('class' => 'checkbox')),
  4285. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4286. array('label' => array('for' => 'ModelMultiField0')),
  4287. 'first',
  4288. '/label',
  4289. '/div',
  4290. array('div' => array('class' => 'checkbox')),
  4291. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4292. array('label' => array('for' => 'ModelMultiField1')),
  4293. 'second',
  4294. '/label',
  4295. '/div',
  4296. '/div'
  4297. );
  4298. $this->assertTags($result, $expected);
  4299. }
  4300. /**
  4301. * test that select() with multiple = checkbox works with overriding name attribute.
  4302. *
  4303. * @return void
  4304. */
  4305. public function testSelectCheckboxMultipleOverrideName() {
  4306. $result = $this->Form->input('category', array(
  4307. 'type' => 'select',
  4308. 'multiple' => 'checkbox',
  4309. 'name' => 'data[fish]',
  4310. 'options' => array('1', '2'),
  4311. 'div' => false,
  4312. 'label' => false,
  4313. ));
  4314. $expected = array(
  4315. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4316. array('div' => array('class' => 'checkbox')),
  4317. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4318. array('label' => array('for' => 'Category0')), '1', '/label',
  4319. '/div',
  4320. array('div' => array('class' => 'checkbox')),
  4321. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4322. array('label' => array('for' => 'Category1')), '2', '/label',
  4323. '/div'
  4324. );
  4325. $this->assertTags($result, $expected);
  4326. }
  4327. /**
  4328. * Test that 'id' overrides all the checkbox id's as well.
  4329. *
  4330. * @return void
  4331. */
  4332. public function testSelectCheckboxMultipleId() {
  4333. $result = $this->Form->select(
  4334. 'Model.multi_field',
  4335. array('first', 'second', 'third'),
  4336. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4337. );
  4338. $expected = array(
  4339. 'input' => array(
  4340. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4341. ),
  4342. array('div' => array('class' => 'checkbox')),
  4343. array('input' => array(
  4344. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4345. 'value' => '0', 'id' => 'CustomId0'
  4346. )),
  4347. array('label' => array('for' => 'CustomId0')),
  4348. 'first',
  4349. '/label',
  4350. '/div',
  4351. array('div' => array('class' => 'checkbox')),
  4352. array('input' => array(
  4353. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4354. 'value' => '1', 'id' => 'CustomId1'
  4355. )),
  4356. array('label' => array('for' => 'CustomId1')),
  4357. 'second',
  4358. '/label',
  4359. '/div',
  4360. array('div' => array('class' => 'checkbox')),
  4361. array('input' => array(
  4362. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4363. 'value' => '2', 'id' => 'CustomId2'
  4364. )),
  4365. array('label' => array('for' => 'CustomId2')),
  4366. 'third',
  4367. '/label',
  4368. '/div'
  4369. );
  4370. $this->assertTags($result, $expected);
  4371. }
  4372. /**
  4373. * testCheckbox method
  4374. *
  4375. * Test generation of checkboxes
  4376. *
  4377. * @return void
  4378. */
  4379. public function testCheckbox() {
  4380. $result = $this->Form->checkbox('Model.field');
  4381. $expected = array(
  4382. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4383. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4384. );
  4385. $this->assertTags($result, $expected);
  4386. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4387. $expected = array(
  4388. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4389. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4390. );
  4391. $this->assertTags($result, $expected);
  4392. $Contact = ClassRegistry::getObject('Contact');
  4393. $Contact->validationErrors['field'] = 1;
  4394. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4395. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4396. $expected = array(
  4397. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4398. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4399. );
  4400. $this->assertTags($result, $expected);
  4401. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4402. $expected = array(
  4403. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4404. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4405. );
  4406. $this->assertTags($result, $expected);
  4407. $this->Form->request->data['Contact']['field'] = '';
  4408. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4409. $expected = array(
  4410. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4411. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4412. );
  4413. $this->assertTags($result, $expected);
  4414. $Contact->validationErrors = array();
  4415. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4416. $expected = array(
  4417. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4418. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4419. );
  4420. $this->assertTags($result, $expected);
  4421. $this->Form->request->data['Contact']['published'] = 1;
  4422. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4423. $expected = array(
  4424. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4425. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4426. );
  4427. $this->assertTags($result, $expected);
  4428. $this->Form->request->data['Contact']['published'] = 0;
  4429. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4430. $expected = array(
  4431. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4432. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4433. );
  4434. $this->assertTags($result, $expected);
  4435. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4436. $expected = array(
  4437. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4438. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4439. );
  4440. $this->assertTags($result, $expected);
  4441. $result = $this->Form->checkbox('CustomField.1.value');
  4442. $expected = array(
  4443. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4444. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4445. );
  4446. $this->assertTags($result, $expected);
  4447. }
  4448. /**
  4449. * test checkbox() with a custom name attribute
  4450. *
  4451. * @return void
  4452. */
  4453. public function testCheckboxCustomNameAttribute() {
  4454. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4455. $expected = array(
  4456. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4457. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4458. );
  4459. $this->assertTags($result, $expected);
  4460. }
  4461. /**
  4462. * test the checked option for checkboxes.
  4463. *
  4464. * @return void
  4465. */
  4466. public function testCheckboxCheckedOption() {
  4467. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4468. $expected = array(
  4469. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4470. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4471. );
  4472. $this->assertTags($result, $expected);
  4473. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4474. $expected = array(
  4475. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4476. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4477. );
  4478. $this->assertTags($result, $expected);
  4479. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4480. $expected = array(
  4481. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4482. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4483. );
  4484. $this->assertTags($result, $expected);
  4485. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4486. $expected = array(
  4487. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4488. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4489. );
  4490. $this->assertTags($result, $expected);
  4491. $this->Form->request->data['Model']['field'] = 1;
  4492. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4493. $expected = array(
  4494. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4495. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4496. );
  4497. $this->assertTags($result, $expected);
  4498. }
  4499. /**
  4500. * Test that disabled attribute works on both the checkbox and hidden input.
  4501. *
  4502. * @return void
  4503. */
  4504. public function testCheckboxDisabling() {
  4505. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4506. $expected = array(
  4507. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4508. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4509. );
  4510. $this->assertTags($result, $expected);
  4511. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4512. $expected = array(
  4513. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4514. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4515. );
  4516. $this->assertTags($result, $expected);
  4517. }
  4518. /**
  4519. * Test that the hidden input for checkboxes can be omitted or set to a
  4520. * specific value.
  4521. *
  4522. * @return void
  4523. */
  4524. public function testCheckboxHiddenField() {
  4525. $result = $this->Form->input('UserForm.something', array(
  4526. 'type' => 'checkbox',
  4527. 'hiddenField' => false
  4528. ));
  4529. $expected = array(
  4530. 'div' => array('class' => 'input checkbox'),
  4531. array('input' => array(
  4532. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4533. 'value' => '1', 'id' => 'UserFormSomething'
  4534. )),
  4535. 'label' => array('for' => 'UserFormSomething'),
  4536. 'Something',
  4537. '/label',
  4538. '/div'
  4539. );
  4540. $this->assertTags($result, $expected);
  4541. $result = $this->Form->input('UserForm.something', array(
  4542. 'type' => 'checkbox',
  4543. 'value' => 'Y',
  4544. 'hiddenField' => 'N',
  4545. ));
  4546. $expected = array(
  4547. 'div' => array('class' => 'input checkbox'),
  4548. array('input' => array(
  4549. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  4550. 'value' => 'N', 'id' => 'UserFormSomething_'
  4551. )),
  4552. array('input' => array(
  4553. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4554. 'value' => 'Y', 'id' => 'UserFormSomething'
  4555. )),
  4556. 'label' => array('for' => 'UserFormSomething'),
  4557. 'Something',
  4558. '/label',
  4559. '/div'
  4560. );
  4561. $this->assertTags($result, $expected);
  4562. }
  4563. /**
  4564. * testDateTime method
  4565. *
  4566. * Test generation of date/time select elements
  4567. *
  4568. * @return void
  4569. */
  4570. public function testDateTime() {
  4571. extract($this->dateRegex);
  4572. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4573. $now = strtotime('now');
  4574. $expected = array(
  4575. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4576. $daysRegex,
  4577. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4578. date('j', $now),
  4579. '/option',
  4580. '*/select',
  4581. '-',
  4582. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4583. $monthsRegex,
  4584. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4585. date('F', $now),
  4586. '/option',
  4587. '*/select',
  4588. '-',
  4589. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4590. $yearsRegex,
  4591. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4592. date('Y', $now),
  4593. '/option',
  4594. '*/select',
  4595. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4596. $hoursRegex,
  4597. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4598. date('g', $now),
  4599. '/option',
  4600. '*/select',
  4601. ':',
  4602. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4603. $minutesRegex,
  4604. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4605. date('i', $now),
  4606. '/option',
  4607. '*/select',
  4608. ' ',
  4609. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4610. $meridianRegex,
  4611. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4612. date('a', $now),
  4613. '/option',
  4614. '*/select'
  4615. );
  4616. $this->assertTags($result, $expected);
  4617. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4618. $expected = array(
  4619. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4620. $daysRegex,
  4621. array('option' => array('value' => '')),
  4622. '/option',
  4623. '*/select',
  4624. '-',
  4625. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4626. $monthsRegex,
  4627. array('option' => array('value' => '')),
  4628. '/option',
  4629. '*/select',
  4630. '-',
  4631. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4632. $yearsRegex,
  4633. array('option' => array('value' => '')),
  4634. '/option',
  4635. '*/select',
  4636. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4637. $hoursRegex,
  4638. array('option' => array('value' => '')),
  4639. '/option',
  4640. '*/select',
  4641. ':',
  4642. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4643. $minutesRegex,
  4644. array('option' => array('value' => '')),
  4645. '/option',
  4646. '*/select',
  4647. ' ',
  4648. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4649. $meridianRegex,
  4650. array('option' => array('value' => '')),
  4651. '/option',
  4652. '*/select'
  4653. );
  4654. $this->assertTags($result, $expected);
  4655. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4656. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  4657. $this->assertTags($result, $expected);
  4658. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4659. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  4660. $this->assertTags($result, $expected);
  4661. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4662. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  4663. $expected = array(
  4664. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4665. $daysRegex,
  4666. array('option' => array('value' => '')),
  4667. '/option',
  4668. '*/select',
  4669. '-',
  4670. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4671. $monthsRegex,
  4672. array('option' => array('value' => '')),
  4673. '/option',
  4674. '*/select',
  4675. '-',
  4676. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4677. $yearsRegex,
  4678. array('option' => array('value' => '')),
  4679. '/option',
  4680. '*/select',
  4681. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4682. $hoursRegex,
  4683. array('option' => array('value' => '')),
  4684. '/option',
  4685. '*/select',
  4686. ':',
  4687. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4688. $minutesRegex,
  4689. array('option' => array('value' => '')),
  4690. '/option',
  4691. array('option' => array('value' => '00')),
  4692. '00',
  4693. '/option',
  4694. array('option' => array('value' => '05')),
  4695. '05',
  4696. '/option',
  4697. array('option' => array('value' => '10')),
  4698. '10',
  4699. '/option',
  4700. '*/select',
  4701. ' ',
  4702. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4703. $meridianRegex,
  4704. array('option' => array('value' => '')),
  4705. '/option',
  4706. '*/select'
  4707. );
  4708. $this->assertTags($result, $expected);
  4709. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4710. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4711. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4712. $this->Form->request->data['Contact']['data'] = null;
  4713. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4714. $expected = array(
  4715. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4716. $daysRegex,
  4717. array('option' => array('value' => '')),
  4718. '/option',
  4719. '*/select',
  4720. '-',
  4721. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4722. $monthsRegex,
  4723. array('option' => array('value' => '')),
  4724. '/option',
  4725. '*/select',
  4726. '-',
  4727. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4728. $yearsRegex,
  4729. array('option' => array('value' => '')),
  4730. '/option',
  4731. '*/select',
  4732. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4733. $hoursRegex,
  4734. array('option' => array('value' => '')),
  4735. '/option',
  4736. '*/select',
  4737. ':',
  4738. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4739. $minutesRegex,
  4740. array('option' => array('value' => '')),
  4741. '/option',
  4742. '*/select',
  4743. ' ',
  4744. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4745. $meridianRegex,
  4746. array('option' => array('value' => '')),
  4747. '/option',
  4748. '*/select'
  4749. );
  4750. $this->assertTags($result, $expected);
  4751. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4752. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  4753. $now = strtotime($this->Form->data['Model']['field']);
  4754. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  4755. $expected = array(
  4756. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4757. $daysRegex,
  4758. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4759. date('j', $now),
  4760. '/option',
  4761. '*/select',
  4762. '-',
  4763. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4764. $monthsRegex,
  4765. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4766. date('F', $now),
  4767. '/option',
  4768. '*/select',
  4769. '-',
  4770. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  4771. $yearsRegex,
  4772. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4773. date('Y', $now),
  4774. '/option',
  4775. '*/select',
  4776. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4777. $hoursRegex,
  4778. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4779. date('g', $now),
  4780. '/option',
  4781. '*/select',
  4782. ':',
  4783. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4784. $minutesRegex,
  4785. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4786. date('i', $now),
  4787. '/option',
  4788. '*/select',
  4789. ' ',
  4790. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  4791. $meridianRegex,
  4792. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4793. date('a', $now),
  4794. '/option',
  4795. '*/select'
  4796. );
  4797. $this->assertTags($result, $expected);
  4798. $selected = strtotime('2008-10-26 12:33:00');
  4799. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  4800. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  4801. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  4802. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  4803. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  4804. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  4805. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  4806. $this->Form->create('Contact');
  4807. $result = $this->Form->input('published');
  4808. $now = strtotime('now');
  4809. $expected = array(
  4810. 'div' => array('class' => 'input date'),
  4811. 'label' => array('for' => 'ContactPublishedMonth'),
  4812. 'Published',
  4813. '/label',
  4814. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4815. $monthsRegex,
  4816. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4817. date('F', $now),
  4818. '/option',
  4819. '*/select',
  4820. '-',
  4821. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4822. $daysRegex,
  4823. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4824. date('j', $now),
  4825. '/option',
  4826. '*/select',
  4827. '-',
  4828. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4829. $yearsRegex,
  4830. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4831. date('Y', $now),
  4832. '/option',
  4833. '*/select',
  4834. '/div'
  4835. );
  4836. $this->assertTags($result, $expected);
  4837. $result = $this->Form->input('published2', array('type' => 'date'));
  4838. $now = strtotime('now');
  4839. $expected = array(
  4840. 'div' => array('class' => 'input date'),
  4841. 'label' => array('for' => 'ContactPublished2Month'),
  4842. 'Published2',
  4843. '/label',
  4844. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  4845. $monthsRegex,
  4846. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4847. date('F', $now),
  4848. '/option',
  4849. '*/select',
  4850. '-',
  4851. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  4852. $daysRegex,
  4853. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4854. date('j', $now),
  4855. '/option',
  4856. '*/select',
  4857. '-',
  4858. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  4859. $yearsRegex,
  4860. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4861. date('Y', $now),
  4862. '/option',
  4863. '*/select',
  4864. '/div'
  4865. );
  4866. $this->assertTags($result, $expected);
  4867. $this->Form->create('Contact');
  4868. $result = $this->Form->input('published', array('monthNames' => false));
  4869. $now = strtotime('now');
  4870. $expected = array(
  4871. 'div' => array('class' => 'input date'),
  4872. 'label' => array('for' => 'ContactPublishedMonth'),
  4873. 'Published',
  4874. '/label',
  4875. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4876. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4877. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4878. date('m', $now),
  4879. '/option',
  4880. '*/select',
  4881. '-',
  4882. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4883. $daysRegex,
  4884. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4885. date('j', $now),
  4886. '/option',
  4887. '*/select',
  4888. '-',
  4889. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4890. $yearsRegex,
  4891. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4892. date('Y', $now),
  4893. '/option',
  4894. '*/select',
  4895. '/div'
  4896. );
  4897. $this->assertTags($result, $expected);
  4898. $result = $this->Form->input('published', array('type' => 'time'));
  4899. $now = strtotime('now');
  4900. $expected = array(
  4901. 'div' => array('class' => 'input time'),
  4902. 'label' => array('for' => 'ContactPublishedHour'),
  4903. 'Published',
  4904. '/label',
  4905. array('select' => array('name' => 'data[Contact][published][hour]', 'id' => 'ContactPublishedHour')),
  4906. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4907. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4908. date('g', $now),
  4909. '/option',
  4910. '*/select',
  4911. ':',
  4912. );
  4913. $this->assertTags($result, $expected);
  4914. $result = $this->Form->input('published', array(
  4915. 'timeFormat' => 24,
  4916. 'interval' => 5,
  4917. 'selected' => strtotime('2009-09-03 13:37:00'),
  4918. 'type' => 'datetime'
  4919. ));
  4920. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4921. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  4922. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  4923. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  4924. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  4925. $this->assertNoErrors();
  4926. $this->Form->request->data['Contact'] = array(
  4927. 'date' => array(
  4928. 'day' => '',
  4929. 'month' => '',
  4930. 'year' => '',
  4931. 'hour' => '',
  4932. 'min' => '',
  4933. 'meridian' => ''
  4934. )
  4935. );
  4936. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4937. }
  4938. /**
  4939. * test that datetime() and default values work.
  4940. *
  4941. * @return void
  4942. */
  4943. public function testDatetimeWithDefault() {
  4944. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  4945. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4946. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4947. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4948. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  4949. 'default' => '2009-06-01 11:15:30'
  4950. ));
  4951. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4952. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4953. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4954. }
  4955. /**
  4956. * test that bogus non-date time data doesn't cause errors.
  4957. *
  4958. * @return void
  4959. */
  4960. public function testDateTimeWithBogusData() {
  4961. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  4962. $this->assertNotRegExp('/selected="selected">\d/', $result);
  4963. }
  4964. /**
  4965. * testDateTimeEmptyAsArray
  4966. *
  4967. * @return void
  4968. */
  4969. public function testDateTimeEmptyAsArray() {
  4970. $result = $this->Form->dateTime('Contact.date',
  4971. 'DMY',
  4972. '12',
  4973. array(
  4974. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  4975. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  4976. )
  4977. )
  4978. );
  4979. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4980. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4981. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4982. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4983. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4984. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4985. $result = $this->Form->dateTime('Contact.date',
  4986. 'DMY',
  4987. '12',
  4988. array(
  4989. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4990. )
  4991. );
  4992. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4993. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4994. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4995. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  4996. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  4997. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  4998. }
  4999. /**
  5000. * testFormDateTimeMulti method
  5001. *
  5002. * test multiple datetime element generation
  5003. *
  5004. * @return void
  5005. */
  5006. public function testFormDateTimeMulti() {
  5007. extract($this->dateRegex);
  5008. $result = $this->Form->dateTime('Contact.1.updated');
  5009. $expected = array(
  5010. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  5011. $daysRegex,
  5012. array('option' => array('value' => '')),
  5013. '/option',
  5014. '*/select',
  5015. '-',
  5016. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  5017. $monthsRegex,
  5018. array('option' => array('value' => '')),
  5019. '/option',
  5020. '*/select',
  5021. '-',
  5022. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  5023. $yearsRegex,
  5024. array('option' => array('value' => '')),
  5025. '/option',
  5026. '*/select',
  5027. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  5028. $hoursRegex,
  5029. array('option' => array('value' => '')),
  5030. '/option',
  5031. '*/select',
  5032. ':',
  5033. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5034. $minutesRegex,
  5035. array('option' => array('value' => '')),
  5036. '/option',
  5037. '*/select',
  5038. ' ',
  5039. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5040. $meridianRegex,
  5041. array('option' => array('value' => '')),
  5042. '/option',
  5043. '*/select'
  5044. );
  5045. $this->assertTags($result, $expected);
  5046. $result = $this->Form->dateTime('Contact.2.updated');
  5047. $expected = array(
  5048. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5049. $daysRegex,
  5050. array('option' => array('value' => '')),
  5051. '/option',
  5052. '*/select',
  5053. '-',
  5054. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5055. $monthsRegex,
  5056. array('option' => array('value' => '')),
  5057. '/option',
  5058. '*/select',
  5059. '-',
  5060. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5061. $yearsRegex,
  5062. array('option' => array('value' => '')),
  5063. '/option',
  5064. '*/select',
  5065. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5066. $hoursRegex,
  5067. array('option' => array('value' => '')),
  5068. '/option',
  5069. '*/select',
  5070. ':',
  5071. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5072. $minutesRegex,
  5073. array('option' => array('value' => '')),
  5074. '/option',
  5075. '*/select',
  5076. ' ',
  5077. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5078. $meridianRegex,
  5079. array('option' => array('value' => '')),
  5080. '/option',
  5081. '*/select'
  5082. );
  5083. $this->assertTags($result, $expected);
  5084. }
  5085. /**
  5086. * When changing the date format, the label should always focus the first select box when
  5087. * clicked.
  5088. *
  5089. * @return void
  5090. */
  5091. public function testDateTimeLabelIdMatchesFirstInput() {
  5092. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5093. $this->assertContains('label for="ModelDateMonth"', $result);
  5094. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5095. $this->assertContains('label for="ModelDateDay"', $result);
  5096. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5097. $this->assertContains('label for="ModelDateYear"', $result);
  5098. }
  5099. /**
  5100. * testMonth method
  5101. *
  5102. * @return void
  5103. */
  5104. public function testMonth() {
  5105. $result = $this->Form->month('Model.field');
  5106. $expected = array(
  5107. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5108. array('option' => array('value' => '')),
  5109. '/option',
  5110. array('option' => array('value' => '01')),
  5111. date('F', strtotime('2008-01-01 00:00:00')),
  5112. '/option',
  5113. array('option' => array('value' => '02')),
  5114. date('F', strtotime('2008-02-01 00:00:00')),
  5115. '/option',
  5116. '*/select',
  5117. );
  5118. $this->assertTags($result, $expected);
  5119. $result = $this->Form->month('Model.field', array('empty' => true));
  5120. $expected = array(
  5121. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5122. array('option' => array('value' => '')),
  5123. '/option',
  5124. array('option' => array('value' => '01')),
  5125. date('F', strtotime('2008-01-01 00:00:00')),
  5126. '/option',
  5127. array('option' => array('value' => '02')),
  5128. date('F', strtotime('2008-02-01 00:00:00')),
  5129. '/option',
  5130. '*/select',
  5131. );
  5132. $this->assertTags($result, $expected);
  5133. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5134. $expected = array(
  5135. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5136. array('option' => array('value' => '')),
  5137. '/option',
  5138. array('option' => array('value' => '01')),
  5139. '01',
  5140. '/option',
  5141. array('option' => array('value' => '02')),
  5142. '02',
  5143. '/option',
  5144. '*/select',
  5145. );
  5146. $this->assertTags($result, $expected);
  5147. $monthNames = array(
  5148. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5149. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5150. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5151. $expected = array(
  5152. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5153. array('option' => array('value' => '')),
  5154. '/option',
  5155. array('option' => array('value' => '01')),
  5156. 'Jan',
  5157. '/option',
  5158. array('option' => array('value' => '02')),
  5159. 'Feb',
  5160. '/option',
  5161. '*/select',
  5162. );
  5163. $this->assertTags($result, $expected);
  5164. }
  5165. /**
  5166. * testDay method
  5167. *
  5168. * @return void
  5169. */
  5170. public function testDay() {
  5171. extract($this->dateRegex);
  5172. $result = $this->Form->day('Model.field', array('value' => false));
  5173. $expected = array(
  5174. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5175. array('option' => array('value' => '')),
  5176. '/option',
  5177. array('option' => array('value' => '01')),
  5178. '1',
  5179. '/option',
  5180. array('option' => array('value' => '02')),
  5181. '2',
  5182. '/option',
  5183. $daysRegex,
  5184. '/select',
  5185. );
  5186. $this->assertTags($result, $expected);
  5187. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5188. $result = $this->Form->day('Model.field');
  5189. $expected = array(
  5190. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5191. array('option' => array('value' => '')),
  5192. '/option',
  5193. array('option' => array('value' => '01')),
  5194. '1',
  5195. '/option',
  5196. array('option' => array('value' => '02')),
  5197. '2',
  5198. '/option',
  5199. $daysRegex,
  5200. array('option' => array('value' => '10', 'selected' => 'selected')),
  5201. '10',
  5202. '/option',
  5203. $daysRegex,
  5204. '/select',
  5205. );
  5206. $this->assertTags($result, $expected);
  5207. $this->Form->request->data['Model']['field'] = '';
  5208. $result = $this->Form->day('Model.field', array('value' => '10'));
  5209. $expected = array(
  5210. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5211. array('option' => array('value' => '')),
  5212. '/option',
  5213. array('option' => array('value' => '01')),
  5214. '1',
  5215. '/option',
  5216. array('option' => array('value' => '02')),
  5217. '2',
  5218. '/option',
  5219. $daysRegex,
  5220. array('option' => array('value' => '10', 'selected' => 'selected')),
  5221. '10',
  5222. '/option',
  5223. $daysRegex,
  5224. '/select',
  5225. );
  5226. $this->assertTags($result, $expected);
  5227. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5228. $result = $this->Form->day('Model.field', array('value' => true));
  5229. $expected = array(
  5230. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5231. array('option' => array('value' => '')),
  5232. '/option',
  5233. array('option' => array('value' => '01')),
  5234. '1',
  5235. '/option',
  5236. array('option' => array('value' => '02')),
  5237. '2',
  5238. '/option',
  5239. $daysRegex,
  5240. array('option' => array('value' => '10', 'selected' => 'selected')),
  5241. '10',
  5242. '/option',
  5243. $daysRegex,
  5244. '/select',
  5245. );
  5246. $this->assertTags($result, $expected);
  5247. }
  5248. /**
  5249. * testMinute method
  5250. *
  5251. * @return void
  5252. */
  5253. public function testMinute() {
  5254. extract($this->dateRegex);
  5255. $result = $this->Form->minute('Model.field');
  5256. $expected = array(
  5257. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5258. array('option' => array('value' => '')),
  5259. '/option',
  5260. array('option' => array('value' => '00')),
  5261. '00',
  5262. '/option',
  5263. array('option' => array('value' => '01')),
  5264. '01',
  5265. '/option',
  5266. array('option' => array('value' => '02')),
  5267. '02',
  5268. '/option',
  5269. $minutesRegex,
  5270. '/select',
  5271. );
  5272. $this->assertTags($result, $expected);
  5273. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5274. $result = $this->Form->minute('Model.field');
  5275. $expected = array(
  5276. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5277. array('option' => array('value' => '')),
  5278. '/option',
  5279. array('option' => array('value' => '00')),
  5280. '00',
  5281. '/option',
  5282. array('option' => array('value' => '01')),
  5283. '01',
  5284. '/option',
  5285. array('option' => array('value' => '02')),
  5286. '02',
  5287. '/option',
  5288. $minutesRegex,
  5289. array('option' => array('value' => '12', 'selected' => 'selected')),
  5290. '12',
  5291. '/option',
  5292. $minutesRegex,
  5293. '/select',
  5294. );
  5295. $this->assertTags($result, $expected);
  5296. $this->Form->request->data['Model']['field'] = '';
  5297. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5298. $expected = array(
  5299. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5300. array('option' => array('value' => '')),
  5301. '/option',
  5302. array('option' => array('value' => '00')),
  5303. '00',
  5304. '/option',
  5305. array('option' => array('value' => '05')),
  5306. '05',
  5307. '/option',
  5308. array('option' => array('value' => '10')),
  5309. '10',
  5310. '/option',
  5311. $minutesRegex,
  5312. '/select',
  5313. );
  5314. $this->assertTags($result, $expected);
  5315. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5316. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5317. $expected = array(
  5318. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5319. array('option' => array('value' => '')),
  5320. '/option',
  5321. array('option' => array('value' => '00')),
  5322. '00',
  5323. '/option',
  5324. array('option' => array('value' => '05')),
  5325. '05',
  5326. '/option',
  5327. array('option' => array('value' => '10', 'selected' => 'selected')),
  5328. '10',
  5329. '/option',
  5330. $minutesRegex,
  5331. '/select',
  5332. );
  5333. $this->assertTags($result, $expected);
  5334. }
  5335. /**
  5336. * testHour method
  5337. *
  5338. * @return void
  5339. */
  5340. public function testHour() {
  5341. extract($this->dateRegex);
  5342. $result = $this->Form->hour('Model.field', false);
  5343. $expected = array(
  5344. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5345. array('option' => array('value' => '')),
  5346. '/option',
  5347. array('option' => array('value' => '01')),
  5348. '1',
  5349. '/option',
  5350. array('option' => array('value' => '02')),
  5351. '2',
  5352. '/option',
  5353. $hoursRegex,
  5354. '/select',
  5355. );
  5356. $this->assertTags($result, $expected);
  5357. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5358. $result = $this->Form->hour('Model.field', false);
  5359. $expected = array(
  5360. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5361. array('option' => array('value' => '')),
  5362. '/option',
  5363. array('option' => array('value' => '01')),
  5364. '1',
  5365. '/option',
  5366. array('option' => array('value' => '02')),
  5367. '2',
  5368. '/option',
  5369. $hoursRegex,
  5370. array('option' => array('value' => '12', 'selected' => 'selected')),
  5371. '12',
  5372. '/option',
  5373. '/select',
  5374. );
  5375. $this->assertTags($result, $expected);
  5376. $this->Form->request->data['Model']['field'] = '';
  5377. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5378. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5379. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  5380. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5381. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5382. $result = $this->Form->hour('Model.field', true);
  5383. $expected = array(
  5384. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5385. array('option' => array('value' => '')),
  5386. '/option',
  5387. array('option' => array('value' => '00', 'selected' => 'selected')),
  5388. '0',
  5389. '/option',
  5390. array('option' => array('value' => '01')),
  5391. '1',
  5392. '/option',
  5393. array('option' => array('value' => '02')),
  5394. '2',
  5395. '/option',
  5396. $hoursRegex,
  5397. '/select',
  5398. );
  5399. $this->assertTags($result, $expected);
  5400. unset($this->Form->request->data['Model']['field']);
  5401. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5402. $thisHour = date('H');
  5403. $optValue = date('G');
  5404. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5405. }
  5406. /**
  5407. * testYear method
  5408. *
  5409. * @return void
  5410. */
  5411. public function testYear() {
  5412. $result = $this->Form->year('Model.field', 2006, 2007);
  5413. $expected = array(
  5414. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5415. array('option' => array('value' => '')),
  5416. '/option',
  5417. array('option' => array('value' => '2007')),
  5418. '2007',
  5419. '/option',
  5420. array('option' => array('value' => '2006')),
  5421. '2006',
  5422. '/option',
  5423. '/select',
  5424. );
  5425. $this->assertTags($result, $expected);
  5426. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5427. $expected = array(
  5428. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5429. array('option' => array('value' => '')),
  5430. '/option',
  5431. array('option' => array('value' => '2006')),
  5432. '2006',
  5433. '/option',
  5434. array('option' => array('value' => '2007')),
  5435. '2007',
  5436. '/option',
  5437. '/select',
  5438. );
  5439. $this->assertTags($result, $expected);
  5440. $this->request->data['Contact']['published'] = '';
  5441. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5442. $expected = array(
  5443. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5444. array('option' => array('value' => '')),
  5445. '/option',
  5446. array('option' => array('value' => '2007')),
  5447. '2007',
  5448. '/option',
  5449. array('option' => array('value' => '2006')),
  5450. '2006',
  5451. '/option',
  5452. '/select',
  5453. );
  5454. $this->assertTags($result, $expected);
  5455. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5456. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5457. $expected = array(
  5458. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5459. array('option' => array('value' => '2007')),
  5460. '2007',
  5461. '/option',
  5462. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5463. '2006',
  5464. '/option',
  5465. '/select',
  5466. );
  5467. $this->assertTags($result, $expected);
  5468. $this->Form->request->data['Contact']['published'] = '';
  5469. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5470. $expected = array(
  5471. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5472. array('option' => array('value' => '')),
  5473. '/option',
  5474. array('option' => array('value' => '2007')),
  5475. '2007',
  5476. '/option',
  5477. array('option' => array('value' => '2006')),
  5478. '2006',
  5479. '/option',
  5480. '/select',
  5481. );
  5482. $this->assertTags($result, $expected);
  5483. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5484. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5485. $expected = array(
  5486. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5487. array('option' => array('value' => '2007')),
  5488. '2007',
  5489. '/option',
  5490. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5491. '2006',
  5492. '/option',
  5493. '/select',
  5494. );
  5495. $this->assertTags($result, $expected);
  5496. $this->Form->request->data['Contact']['published'] = '';
  5497. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5498. $expected = array(
  5499. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5500. array('option' => array('value' => '')),
  5501. '/option',
  5502. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5503. '2007',
  5504. '/option',
  5505. array('option' => array('value' => '2006')),
  5506. '2006',
  5507. '/option',
  5508. '/select',
  5509. );
  5510. $this->assertTags($result, $expected);
  5511. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5512. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5513. $expected = array(
  5514. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5515. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5516. '2007',
  5517. '/option',
  5518. array('option' => array('value' => '2006')),
  5519. '2006',
  5520. '/option',
  5521. '/select',
  5522. );
  5523. $this->assertTags($result, $expected);
  5524. $this->Form->request->data['Contact']['published'] = '';
  5525. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5526. $expected = array(
  5527. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5528. array('option' => array('value' => '2008')),
  5529. '2008',
  5530. '/option',
  5531. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5532. '2007',
  5533. '/option',
  5534. array('option' => array('value' => '2006')),
  5535. '2006',
  5536. '/option',
  5537. '/select',
  5538. );
  5539. $this->assertTags($result, $expected);
  5540. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5541. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5542. $expected = array(
  5543. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5544. array('option' => array('value' => '2008')),
  5545. '2008',
  5546. '/option',
  5547. array('option' => array('value' => '2007')),
  5548. '2007',
  5549. '/option',
  5550. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5551. '2006',
  5552. '/option',
  5553. '/select',
  5554. );
  5555. $this->assertTags($result, $expected);
  5556. $this->Form->request->data = array();
  5557. $this->Form->create('Contact');
  5558. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5559. $expected = array(
  5560. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5561. array('option' => array('value' => '2008')),
  5562. '2008',
  5563. '/option',
  5564. array('option' => array('value' => '2007')),
  5565. '2007',
  5566. '/option',
  5567. array('option' => array('value' => '2006')),
  5568. '2006',
  5569. '/option',
  5570. '/select',
  5571. );
  5572. $this->assertTags($result, $expected);
  5573. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  5574. $this->assertContains('data[Contact][published][year]', $result);
  5575. }
  5576. /**
  5577. * testTextArea method
  5578. *
  5579. * @return void
  5580. */
  5581. public function testTextArea() {
  5582. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  5583. $result = $this->Form->textarea('Model.field');
  5584. $expected = array(
  5585. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5586. 'some test data',
  5587. '/textarea',
  5588. );
  5589. $this->assertTags($result, $expected);
  5590. $result = $this->Form->textarea('Model.tmp');
  5591. $expected = array(
  5592. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  5593. '/textarea',
  5594. );
  5595. $this->assertTags($result, $expected);
  5596. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5597. $result = $this->Form->textarea('Model.field');
  5598. $expected = array(
  5599. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5600. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5601. '/textarea',
  5602. );
  5603. $this->assertTags($result, $expected);
  5604. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5605. $result = $this->Form->textarea('Model.field', array('escape' => false));
  5606. $expected = array(
  5607. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5608. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5609. '/textarea',
  5610. );
  5611. $this->assertTags($result, $expected);
  5612. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  5613. $result = $this->Form->textarea('Model.0.OtherModel.field');
  5614. $expected = array(
  5615. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  5616. '/textarea'
  5617. );
  5618. $this->assertTags($result, $expected);
  5619. }
  5620. /**
  5621. * testTextAreaWithStupidCharacters method
  5622. *
  5623. * test text area with non-ascii characters
  5624. *
  5625. * @return void
  5626. */
  5627. public function testTextAreaWithStupidCharacters() {
  5628. $this->loadFixtures('Post');
  5629. $result = $this->Form->input('Post.content', array(
  5630. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  5631. ));
  5632. $expected = array(
  5633. 'div' => array('class' => 'input textarea'),
  5634. 'label' => array('for' => 'PostContent'),
  5635. 'Current Text',
  5636. '/label',
  5637. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  5638. 'GREAT®',
  5639. '/textarea',
  5640. '/div'
  5641. );
  5642. $this->assertTags($result, $expected);
  5643. }
  5644. /**
  5645. * testHiddenField method
  5646. *
  5647. * @return void
  5648. */
  5649. public function testHiddenField() {
  5650. $Contact = ClassRegistry::getObject('Contact');
  5651. $Contact->validationErrors['field'] = 1;
  5652. $this->Form->request->data['Contact']['field'] = 'test';
  5653. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  5654. $this->assertTags($result, array(
  5655. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  5656. );
  5657. }
  5658. /**
  5659. * testFileUploadField method
  5660. *
  5661. * @return void
  5662. */
  5663. public function testFileUploadField() {
  5664. $result = $this->Form->file('Model.upload');
  5665. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5666. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  5667. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  5668. $expected = array(
  5669. 'div' => array('class' => 'input file'),
  5670. 'label' => array('for' => 'ModelUpload'),
  5671. 'Upload',
  5672. '/label',
  5673. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  5674. '/div'
  5675. );
  5676. $this->assertTags($result, $expected);
  5677. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  5678. $result = $this->Form->file('Model.upload');
  5679. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5680. }
  5681. /**
  5682. * test File upload input on a model not used in create();
  5683. *
  5684. * @return void
  5685. */
  5686. public function testFileUploadOnOtherModel() {
  5687. $this->Form->create('ValidateUser', array('type' => 'file'));
  5688. $result = $this->Form->file('ValidateProfile.city');
  5689. $expected = array(
  5690. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  5691. );
  5692. $this->assertTags($result, $expected);
  5693. }
  5694. /**
  5695. * testButton method
  5696. *
  5697. * @return void
  5698. */
  5699. public function testButton() {
  5700. $result = $this->Form->button('Hi');
  5701. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5702. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5703. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5704. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5705. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5706. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5707. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5708. $result = $this->Form->button('No type', array('type' => false));
  5709. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5710. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  5711. $this->assertNotRegExp('/\&039/', $result);
  5712. }
  5713. /**
  5714. * Test that button() makes unlocked fields by default.
  5715. *
  5716. * @return void
  5717. */
  5718. public function testButtonUnlockedByDefault() {
  5719. $this->Form->request->params['_Token']['key'] = 'secured';
  5720. $this->Form->button('Save', array('name' => 'save'));
  5721. $this->Form->button('Clear');
  5722. $result = $this->Form->unlockField();
  5723. $this->assertEquals(array('save'), $result);
  5724. }
  5725. /**
  5726. * testPostButton method
  5727. *
  5728. * @return void
  5729. */
  5730. public function testPostButton() {
  5731. $result = $this->Form->postButton('Hi', '/controller/action');
  5732. $this->assertTags($result, array(
  5733. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5734. 'div' => array('style' => 'display:none;'),
  5735. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5736. '/div',
  5737. 'button' => array('type' => 'submit'),
  5738. 'Hi',
  5739. '/button',
  5740. '/form'
  5741. ));
  5742. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5743. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  5744. }
  5745. /**
  5746. * Test that postButton adds _Token fields.
  5747. *
  5748. * @return void
  5749. */
  5750. public function testSecurePostButton() {
  5751. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5752. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5753. $expected = array(
  5754. 'form' => array(
  5755. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5756. ),
  5757. array('div' => array('style' => 'display:none;')),
  5758. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5759. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5760. '/div',
  5761. 'button' => array('type' => 'submit'),
  5762. 'Delete',
  5763. '/button',
  5764. array('div' => array('style' => 'display:none;')),
  5765. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5766. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5767. '/div',
  5768. '/form',
  5769. );
  5770. $this->assertTags($result, $expected);
  5771. }
  5772. /**
  5773. * testPostLink method
  5774. *
  5775. * @return void
  5776. */
  5777. public function testPostLink() {
  5778. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5779. $this->assertTags($result, array(
  5780. 'form' => array(
  5781. 'method' => 'post', 'action' => '/posts/delete/1',
  5782. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5783. ),
  5784. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5785. '/form',
  5786. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5787. 'Delete',
  5788. '/a'
  5789. ));
  5790. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  5791. $this->assertTags($result, array(
  5792. 'form' => array(
  5793. 'method' => 'post', 'action' => '/posts/delete/1',
  5794. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5795. ),
  5796. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  5797. '/form',
  5798. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5799. 'Delete',
  5800. '/a'
  5801. ));
  5802. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5803. $this->assertTags($result, array(
  5804. 'form' => array(
  5805. 'method' => 'post', 'action' => '/posts/delete/1',
  5806. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5807. ),
  5808. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5809. '/form',
  5810. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5811. 'Delete',
  5812. '/a'
  5813. ));
  5814. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5815. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  5816. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  5817. $this->assertTags($result, array(
  5818. 'form' => array(
  5819. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  5820. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5821. ),
  5822. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5823. '/form',
  5824. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5825. 'Delete',
  5826. '/a'
  5827. ));
  5828. }
  5829. /**
  5830. * Test that postLink adds _Token fields.
  5831. *
  5832. * @return void
  5833. */
  5834. public function testSecurePostLink() {
  5835. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5836. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5837. $expected = array(
  5838. 'form' => array(
  5839. 'method' => 'post', 'action' => '/posts/delete/1',
  5840. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5841. ),
  5842. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5843. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5844. 'div' => array('style' => 'display:none;'),
  5845. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5846. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5847. '/div',
  5848. '/form',
  5849. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5850. 'Delete',
  5851. '/a'
  5852. );
  5853. $this->assertTags($result, $expected);
  5854. }
  5855. /**
  5856. * testSubmitButton method
  5857. *
  5858. * @return void
  5859. */
  5860. public function testSubmitButton() {
  5861. $result = $this->Form->submit('');
  5862. $expected = array(
  5863. 'div' => array('class' => 'submit'),
  5864. 'input' => array('type' => 'submit', 'value' => ''),
  5865. '/div'
  5866. );
  5867. $this->assertTags($result, $expected);
  5868. $result = $this->Form->submit('Test Submit');
  5869. $expected = array(
  5870. 'div' => array('class' => 'submit'),
  5871. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5872. '/div'
  5873. );
  5874. $this->assertTags($result, $expected);
  5875. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5876. $expected = array(
  5877. 'span' => array('class' => 'submit'),
  5878. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5879. '/span'
  5880. );
  5881. $this->assertTags($result, $expected);
  5882. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5883. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5884. $this->assertTags($result, $expected);
  5885. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5886. $expected = array(
  5887. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5888. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5889. '/div'
  5890. );
  5891. $this->assertTags($result, $expected);
  5892. $result = $this->Form->submit('Next >');
  5893. $expected = array(
  5894. 'div' => array('class' => 'submit'),
  5895. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5896. '/div'
  5897. );
  5898. $this->assertTags($result, $expected);
  5899. $result = $this->Form->submit('Next >', array('escape' => false));
  5900. $expected = array(
  5901. 'div' => array('class' => 'submit'),
  5902. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5903. '/div'
  5904. );
  5905. $this->assertTags($result, $expected);
  5906. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5907. $expected = array(
  5908. 'div' => array('class' => 'submit'),
  5909. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5910. '/div'
  5911. );
  5912. $this->assertTags($result, $expected);
  5913. $before = '--before--';
  5914. $after = '--after--';
  5915. $result = $this->Form->submit('Test', array('before' => $before));
  5916. $expected = array(
  5917. 'div' => array('class' => 'submit'),
  5918. '--before--',
  5919. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5920. '/div'
  5921. );
  5922. $this->assertTags($result, $expected);
  5923. $result = $this->Form->submit('Test', array('after' => $after));
  5924. $expected = array(
  5925. 'div' => array('class' => 'submit'),
  5926. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5927. '--after--',
  5928. '/div'
  5929. );
  5930. $this->assertTags($result, $expected);
  5931. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5932. $expected = array(
  5933. 'div' => array('class' => 'submit'),
  5934. '--before--',
  5935. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5936. '--after--',
  5937. '/div'
  5938. );
  5939. $this->assertTags($result, $expected);
  5940. }
  5941. /**
  5942. * test image submit types.
  5943. *
  5944. * @return void
  5945. */
  5946. public function testSubmitImage() {
  5947. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5948. $expected = array(
  5949. 'div' => array('class' => 'submit'),
  5950. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5951. '/div'
  5952. );
  5953. $this->assertTags($result, $expected);
  5954. $result = $this->Form->submit('/relative/cake.power.gif');
  5955. $expected = array(
  5956. 'div' => array('class' => 'submit'),
  5957. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5958. '/div'
  5959. );
  5960. $this->assertTags($result, $expected);
  5961. $result = $this->Form->submit('cake.power.gif');
  5962. $expected = array(
  5963. 'div' => array('class' => 'submit'),
  5964. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5965. '/div'
  5966. );
  5967. $this->assertTags($result, $expected);
  5968. $result = $this->Form->submit('Not.an.image');
  5969. $expected = array(
  5970. 'div' => array('class' => 'submit'),
  5971. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5972. '/div'
  5973. );
  5974. $this->assertTags($result, $expected);
  5975. $after = '--after--';
  5976. $before = '--before--';
  5977. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5978. $expected = array(
  5979. 'div' => array('class' => 'submit'),
  5980. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5981. '--after--',
  5982. '/div'
  5983. );
  5984. $this->assertTags($result, $expected);
  5985. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5986. $expected = array(
  5987. 'div' => array('class' => 'submit'),
  5988. '--before--',
  5989. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5990. '/div'
  5991. );
  5992. $this->assertTags($result, $expected);
  5993. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5994. $expected = array(
  5995. 'div' => array('class' => 'submit'),
  5996. '--before--',
  5997. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5998. '--after--',
  5999. '/div'
  6000. );
  6001. $this->assertTags($result, $expected);
  6002. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  6003. $expected = array(
  6004. 'div' => array('class' => 'submit'),
  6005. '--before--',
  6006. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6007. '--after--',
  6008. '/div'
  6009. );
  6010. $this->assertTags($result, $expected);
  6011. }
  6012. /**
  6013. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6014. * be submitted at a time.
  6015. *
  6016. * @return void
  6017. */
  6018. public function testSubmitUnlockedByDefault() {
  6019. $this->Form->request->params['_Token']['key'] = 'secured';
  6020. $this->Form->submit('Go go');
  6021. $this->Form->submit('Save', array('name' => 'save'));
  6022. $result = $this->Form->unlockField();
  6023. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  6024. }
  6025. /**
  6026. * Test submit image with timestamps.
  6027. *
  6028. * @return void
  6029. */
  6030. public function testSubmitImageTimestamp() {
  6031. Configure::write('Asset.timestamp', 'force');
  6032. $result = $this->Form->submit('cake.power.gif');
  6033. $expected = array(
  6034. 'div' => array('class' => 'submit'),
  6035. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  6036. '/div'
  6037. );
  6038. $this->assertTags($result, $expected);
  6039. }
  6040. /**
  6041. * test the create() method
  6042. *
  6043. * @return void
  6044. */
  6045. public function testCreate() {
  6046. $result = $this->Form->create('Contact');
  6047. $encoding = strtolower(Configure::read('App.encoding'));
  6048. $expected = array(
  6049. 'form' => array(
  6050. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6051. 'accept-charset' => $encoding
  6052. ),
  6053. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  6054. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6055. '/div'
  6056. );
  6057. $this->assertTags($result, $expected);
  6058. $result = $this->Form->create('Contact', array('type' => 'GET'));
  6059. $expected = array('form' => array(
  6060. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6061. 'accept-charset' => $encoding
  6062. ));
  6063. $this->assertTags($result, $expected);
  6064. $result = $this->Form->create('Contact', array('type' => 'get'));
  6065. $expected = array('form' => array(
  6066. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6067. 'accept-charset' => $encoding
  6068. ));
  6069. $this->assertTags($result, $expected);
  6070. $result = $this->Form->create('Contact', array('type' => 'put'));
  6071. $expected = array(
  6072. 'form' => array(
  6073. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6074. 'accept-charset' => $encoding
  6075. ),
  6076. 'div' => array('style' => 'display:none;'),
  6077. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6078. '/div'
  6079. );
  6080. $this->assertTags($result, $expected);
  6081. $result = $this->Form->create('Contact', array('type' => 'file'));
  6082. $expected = array(
  6083. 'form' => array(
  6084. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6085. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6086. ),
  6087. 'div' => array('style' => 'display:none;'),
  6088. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6089. '/div'
  6090. );
  6091. $this->assertTags($result, $expected);
  6092. $this->Form->request->data['Contact']['id'] = 1;
  6093. $this->Form->request->here = '/contacts/edit/1';
  6094. $this->Form->request['action'] = 'edit';
  6095. $result = $this->Form->create('Contact');
  6096. $expected = array(
  6097. 'form' => array(
  6098. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6099. 'accept-charset' => $encoding
  6100. ),
  6101. 'div' => array('style' => 'display:none;'),
  6102. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6103. '/div'
  6104. );
  6105. $this->assertTags($result, $expected);
  6106. $this->Form->request->data['Contact']['id'] = 1;
  6107. $this->Form->request->here = '/contacts/edit/1';
  6108. $this->Form->request['action'] = 'edit';
  6109. $result = $this->Form->create('Contact', array('type' => 'file'));
  6110. $expected = array(
  6111. 'form' => array(
  6112. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6113. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6114. ),
  6115. 'div' => array('style' => 'display:none;'),
  6116. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6117. '/div'
  6118. );
  6119. $this->assertTags($result, $expected);
  6120. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  6121. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  6122. $expected = array(
  6123. 'form' => array(
  6124. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  6125. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  6126. ),
  6127. 'div' => array('style' => 'display:none;'),
  6128. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6129. '/div'
  6130. );
  6131. $this->assertTags($result, $expected);
  6132. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  6133. $expected = array(
  6134. 'form' => array(
  6135. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  6136. 'accept-charset' => $encoding
  6137. ),
  6138. 'div' => array('style' => 'display:none;'),
  6139. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6140. '/div'
  6141. );
  6142. $this->assertTags($result, $expected);
  6143. $this->Form->request['action'] = 'add';
  6144. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  6145. $expected = array(
  6146. 'form' => array(
  6147. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  6148. 'accept-charset' => $encoding
  6149. ),
  6150. 'div' => array('style' => 'display:none;'),
  6151. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6152. '/div'
  6153. );
  6154. $this->assertTags($result, $expected);
  6155. $result = $this->Form->create('User', array('action' => 'login'));
  6156. $expected = array(
  6157. 'form' => array(
  6158. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  6159. 'accept-charset' => $encoding
  6160. ),
  6161. 'div' => array('style' => 'display:none;'),
  6162. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6163. '/div'
  6164. );
  6165. $this->assertTags($result, $expected);
  6166. $result = $this->Form->create('User', array('url' => '/users/login'));
  6167. $expected = array(
  6168. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  6169. 'div' => array('style' => 'display:none;'),
  6170. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6171. '/div'
  6172. );
  6173. $this->assertTags($result, $expected);
  6174. $this->Form->request['controller'] = 'pages';
  6175. $result = $this->Form->create('User', array('action' => 'signup'));
  6176. $expected = array(
  6177. 'form' => array(
  6178. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  6179. 'accept-charset' => $encoding
  6180. ),
  6181. 'div' => array('style' => 'display:none;'),
  6182. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6183. '/div'
  6184. );
  6185. $this->assertTags($result, $expected);
  6186. $this->Form->request->data = array();
  6187. $this->Form->request['controller'] = 'contacts';
  6188. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6189. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  6190. $expected = array(
  6191. 'form' => array(
  6192. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  6193. 'accept-charset' => 'utf-8'
  6194. ),
  6195. 'div' => array('style' => 'display:none;'),
  6196. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6197. '/div'
  6198. );
  6199. $this->assertTags($result, $expected);
  6200. }
  6201. /**
  6202. * Test the onsubmit option for create()
  6203. *
  6204. * @return void
  6205. */
  6206. public function testCreateOnSubmit() {
  6207. $this->Form->request->data = array();
  6208. $this->Form->request['controller'] = 'contacts';
  6209. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6210. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  6211. $expected = array(
  6212. 'form' => array(
  6213. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  6214. 'accept-charset' => 'utf-8'
  6215. ),
  6216. 'div' => array('style' => 'display:none;'),
  6217. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6218. '/div'
  6219. );
  6220. $this->assertTags($result, $expected);
  6221. $this->Form->request->data = array();
  6222. $this->Form->request['controller'] = 'contacts';
  6223. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6224. $result = $this->Form->create(array(
  6225. 'url' => array('action' => 'index', 'param'),
  6226. 'default' => false,
  6227. 'onsubmit' => 'someFunction();'
  6228. ));
  6229. $expected = array(
  6230. 'form' => array(
  6231. 'id' => 'ContactAddForm', 'method' => 'post',
  6232. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6233. 'action' => '/contacts/index/param',
  6234. 'accept-charset' => 'utf-8'
  6235. ),
  6236. 'div' => array('style' => 'display:none;'),
  6237. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6238. '/div'
  6239. );
  6240. $this->assertTags($result, $expected);
  6241. }
  6242. /**
  6243. * test create() with automatic url generation
  6244. *
  6245. * @return void
  6246. */
  6247. public function testCreateAutoUrl() {
  6248. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6249. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6250. $this->Form->request->base = '/base_url';
  6251. $result = $this->Form->create('Contact');
  6252. $expected = array(
  6253. 'form' => array(
  6254. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6255. 'accept-charset' => 'utf-8'
  6256. ),
  6257. 'div' => array('style' => 'display:none;'),
  6258. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6259. '/div'
  6260. );
  6261. $this->assertTags($result, $expected);
  6262. $this->Form->request['action'] = 'delete';
  6263. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6264. $this->Form->request->base = '/base_url';
  6265. $result = $this->Form->create('Contact');
  6266. $expected = array(
  6267. 'form' => array(
  6268. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6269. 'accept-charset' => 'utf-8'
  6270. ),
  6271. 'div' => array('style' => 'display:none;'),
  6272. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6273. '/div'
  6274. );
  6275. $this->assertTags($result, $expected);
  6276. }
  6277. /**
  6278. * test create() with a custom route
  6279. *
  6280. * @return void
  6281. */
  6282. public function testCreateCustomRoute() {
  6283. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6284. $encoding = strtolower(Configure::read('App.encoding'));
  6285. $result = $this->Form->create('User', array('action' => 'login'));
  6286. $expected = array(
  6287. 'form' => array(
  6288. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6289. 'accept-charset' => $encoding
  6290. ),
  6291. 'div' => array('style' => 'display:none;'),
  6292. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6293. '/div'
  6294. );
  6295. $this->assertTags($result, $expected);
  6296. }
  6297. /**
  6298. * test that inputDefaults are stored and used.
  6299. *
  6300. * @return void
  6301. */
  6302. public function testCreateWithInputDefaults() {
  6303. $this->Form->create('User', array(
  6304. 'inputDefaults' => array(
  6305. 'div' => false,
  6306. 'label' => false,
  6307. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6308. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6309. )
  6310. ));
  6311. $result = $this->Form->input('username');
  6312. $expected = array(
  6313. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6314. );
  6315. $this->assertTags($result, $expected);
  6316. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6317. $expected = array(
  6318. 'div' => array('class' => 'input text'),
  6319. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6320. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6321. '/div'
  6322. );
  6323. $this->assertTags($result, $expected);
  6324. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  6325. $expected = array(
  6326. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6327. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  6328. );
  6329. $this->assertTags($result, $expected);
  6330. $this->Form->create('User', array(
  6331. 'inputDefaults' => array(
  6332. 'div' => false,
  6333. 'label' => array('class' => 'nice', 'for' => 'changed'),
  6334. )
  6335. ));
  6336. $result = $this->Form->input('username', array('div' => true));
  6337. $expected = array(
  6338. 'div' => array('class' => 'input text'),
  6339. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  6340. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6341. '/div'
  6342. );
  6343. $this->assertTags($result, $expected);
  6344. }
  6345. /**
  6346. * test automatic accept-charset overriding
  6347. *
  6348. * @return void
  6349. */
  6350. public function testCreateWithAcceptCharset() {
  6351. $result = $this->Form->create('UserForm', array(
  6352. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  6353. )
  6354. );
  6355. $expected = array(
  6356. 'form' => array(
  6357. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6358. 'accept-charset' => 'iso-8859-1'
  6359. ),
  6360. 'div' => array('style' => 'display:none;'),
  6361. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6362. '/div'
  6363. );
  6364. $this->assertTags($result, $expected);
  6365. }
  6366. /**
  6367. * Test base form url when url param is passed with multiple parameters (&)
  6368. *
  6369. */
  6370. public function testCreateQuerystringrequest() {
  6371. $encoding = strtolower(Configure::read('App.encoding'));
  6372. $result = $this->Form->create('Contact', array(
  6373. 'type' => 'post',
  6374. 'escape' => false,
  6375. 'url' => array(
  6376. 'controller' => 'controller',
  6377. 'action' => 'action',
  6378. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6379. )
  6380. ));
  6381. $expected = array(
  6382. 'form' => array(
  6383. 'id' => 'ContactAddForm',
  6384. 'method' => 'post',
  6385. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6386. 'accept-charset' => $encoding
  6387. ),
  6388. 'div' => array('style' => 'display:none;'),
  6389. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6390. '/div'
  6391. );
  6392. $this->assertTags($result, $expected);
  6393. $result = $this->Form->create('Contact', array(
  6394. 'type' => 'post',
  6395. 'url' => array(
  6396. 'controller' => 'controller',
  6397. 'action' => 'action',
  6398. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6399. )
  6400. ));
  6401. $expected = array(
  6402. 'form' => array(
  6403. 'id' => 'ContactAddForm',
  6404. 'method' => 'post',
  6405. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6406. 'accept-charset' => $encoding
  6407. ),
  6408. 'div' => array('style' => 'display:none;'),
  6409. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6410. '/div'
  6411. );
  6412. $this->assertTags($result, $expected);
  6413. }
  6414. /**
  6415. * test that create() doesn't cause errors by multiple id's being in the primary key
  6416. * as could happen with multiple select or checkboxes.
  6417. *
  6418. * @return void
  6419. */
  6420. public function testCreateWithMultipleIdInData() {
  6421. $encoding = strtolower(Configure::read('App.encoding'));
  6422. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6423. $result = $this->Form->create('Contact');
  6424. $expected = array(
  6425. 'form' => array(
  6426. 'id' => 'ContactAddForm',
  6427. 'method' => 'post',
  6428. 'action' => '/contacts/add',
  6429. 'accept-charset' => $encoding
  6430. ),
  6431. 'div' => array('style' => 'display:none;'),
  6432. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6433. '/div'
  6434. );
  6435. $this->assertTags($result, $expected);
  6436. }
  6437. /**
  6438. * test that create() doesn't add in extra passed params.
  6439. *
  6440. * @return void
  6441. */
  6442. public function testCreatePassedArgs() {
  6443. $encoding = strtolower(Configure::read('App.encoding'));
  6444. $this->Form->request->data['Contact']['id'] = 1;
  6445. $result = $this->Form->create('Contact', array(
  6446. 'type' => 'post',
  6447. 'escape' => false,
  6448. 'url' => array(
  6449. 'action' => 'edit',
  6450. 'myparam'
  6451. )
  6452. ));
  6453. $expected = array(
  6454. 'form' => array(
  6455. 'id' => 'ContactAddForm',
  6456. 'method' => 'post',
  6457. 'action' => '/contacts/edit/myparam',
  6458. 'accept-charset' => $encoding
  6459. ),
  6460. 'div' => array('style' => 'display:none;'),
  6461. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6462. '/div'
  6463. );
  6464. $this->assertTags($result, $expected);
  6465. }
  6466. /**
  6467. * test creating a get form, and get form inputs.
  6468. *
  6469. * @return void
  6470. */
  6471. public function testGetFormCreate() {
  6472. $encoding = strtolower(Configure::read('App.encoding'));
  6473. $result = $this->Form->create('Contact', array('type' => 'get'));
  6474. $this->assertTags($result, array('form' => array(
  6475. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6476. 'accept-charset' => $encoding
  6477. )));
  6478. $result = $this->Form->text('Contact.name');
  6479. $this->assertTags($result, array('input' => array(
  6480. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6481. )));
  6482. $result = $this->Form->password('password');
  6483. $this->assertTags($result, array('input' => array(
  6484. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6485. )));
  6486. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6487. $result = $this->Form->text('user_form');
  6488. $this->assertTags($result, array('input' => array(
  6489. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6490. )));
  6491. }
  6492. /**
  6493. * test get form, and inputs when the model param is false
  6494. *
  6495. * @return void
  6496. */
  6497. public function testGetFormWithFalseModel() {
  6498. $encoding = strtolower(Configure::read('App.encoding'));
  6499. $this->Form->request['controller'] = 'contact_test';
  6500. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6501. $expected = array('form' => array(
  6502. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6503. 'accept-charset' => $encoding
  6504. ));
  6505. $this->assertTags($result, $expected);
  6506. $result = $this->Form->text('reason');
  6507. $expected = array(
  6508. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6509. );
  6510. $this->assertTags($result, $expected);
  6511. }
  6512. /**
  6513. * test that datetime() works with GET style forms.
  6514. *
  6515. * @return void
  6516. */
  6517. public function testDateTimeWithGetForms() {
  6518. extract($this->dateRegex);
  6519. $this->Form->create('Contact', array('type' => 'get'));
  6520. $result = $this->Form->datetime('created');
  6521. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6522. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6523. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6524. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6525. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6526. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6527. }
  6528. /**
  6529. * testEditFormWithData method
  6530. *
  6531. * test auto populating form elements from submitted data.
  6532. *
  6533. * @return void
  6534. */
  6535. public function testEditFormWithData() {
  6536. $this->Form->request->data = array('Person' => array(
  6537. 'id' => 1,
  6538. 'first_name' => 'Nate',
  6539. 'last_name' => 'Abele',
  6540. 'email' => '[email protected]'
  6541. ));
  6542. $this->Form->request->addParams(array(
  6543. 'models' => array('Person'),
  6544. 'controller' => 'people',
  6545. 'action' => 'add'
  6546. ));
  6547. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6548. $this->Form->create();
  6549. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6550. $expected = array(
  6551. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6552. 'select' => array(
  6553. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6554. ),
  6555. array('option' => array('value' => 1)), 'Nate', '/option',
  6556. array('option' => array('value' => 2)), 'Garrett', '/option',
  6557. array('option' => array('value' => 3)), 'Larry', '/option',
  6558. '/select'
  6559. );
  6560. $this->assertTags($result, $expected);
  6561. }
  6562. /**
  6563. * Test that required fields are created for various types of validation.
  6564. *
  6565. * @return void
  6566. */
  6567. public function testFormInputRequiredDetection() {
  6568. $this->Form->create('Contact');
  6569. $result = $this->Form->input('Contact.non_existing');
  6570. $expected = array(
  6571. 'div' => array('class' => 'input text'),
  6572. 'label' => array('for' => 'ContactNonExisting'),
  6573. 'Non Existing',
  6574. '/label',
  6575. 'input' => array(
  6576. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  6577. 'id' => 'ContactNonExisting'
  6578. ),
  6579. '/div'
  6580. );
  6581. $this->assertTags($result, $expected);
  6582. $result = $this->Form->input('Contact.imrequired');
  6583. $expected = array(
  6584. 'div' => array('class' => 'input text required'),
  6585. 'label' => array('for' => 'ContactImrequired'),
  6586. 'Imrequired',
  6587. '/label',
  6588. 'input' => array(
  6589. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  6590. 'id' => 'ContactImrequired',
  6591. 'required' => 'required'
  6592. ),
  6593. '/div'
  6594. );
  6595. $this->assertTags($result, $expected);
  6596. $result = $this->Form->input('Contact.imalsorequired');
  6597. $expected = array(
  6598. 'div' => array('class' => 'input text required'),
  6599. 'label' => array('for' => 'ContactImalsorequired'),
  6600. 'Imalsorequired',
  6601. '/label',
  6602. 'input' => array(
  6603. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  6604. 'id' => 'ContactImalsorequired',
  6605. 'required' => 'required'
  6606. ),
  6607. '/div'
  6608. );
  6609. $this->assertTags($result, $expected);
  6610. $result = $this->Form->input('Contact.imrequiredtoo');
  6611. $expected = array(
  6612. 'div' => array('class' => 'input text required'),
  6613. 'label' => array('for' => 'ContactImrequiredtoo'),
  6614. 'Imrequiredtoo',
  6615. '/label',
  6616. 'input' => array(
  6617. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  6618. 'id' => 'ContactImrequiredtoo',
  6619. 'required' => 'required'
  6620. ),
  6621. '/div'
  6622. );
  6623. $this->assertTags($result, $expected);
  6624. $result = $this->Form->input('Contact.required_one');
  6625. $expected = array(
  6626. 'div' => array('class' => 'input text required'),
  6627. 'label' => array('for' => 'ContactRequiredOne'),
  6628. 'Required One',
  6629. '/label',
  6630. 'input' => array(
  6631. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  6632. 'id' => 'ContactRequiredOne',
  6633. 'required' => 'required'
  6634. ),
  6635. '/div'
  6636. );
  6637. $this->assertTags($result, $expected);
  6638. $result = $this->Form->input('Contact.string_required');
  6639. $expected = array(
  6640. 'div' => array('class' => 'input text required'),
  6641. 'label' => array('for' => 'ContactStringRequired'),
  6642. 'String Required',
  6643. '/label',
  6644. 'input' => array(
  6645. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  6646. 'id' => 'ContactStringRequired',
  6647. 'required' => 'required'
  6648. ),
  6649. '/div'
  6650. );
  6651. $this->assertTags($result, $expected);
  6652. $result = $this->Form->input('Contact.imnotrequired');
  6653. $expected = array(
  6654. 'div' => array('class' => 'input text'),
  6655. 'label' => array('for' => 'ContactImnotrequired'),
  6656. 'Imnotrequired',
  6657. '/label',
  6658. 'input' => array(
  6659. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  6660. 'id' => 'ContactImnotrequired'
  6661. ),
  6662. '/div'
  6663. );
  6664. $this->assertTags($result, $expected);
  6665. $result = $this->Form->input('Contact.imalsonotrequired');
  6666. $expected = array(
  6667. 'div' => array('class' => 'input text'),
  6668. 'label' => array('for' => 'ContactImalsonotrequired'),
  6669. 'Imalsonotrequired',
  6670. '/label',
  6671. 'input' => array(
  6672. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  6673. 'id' => 'ContactImalsonotrequired'
  6674. ),
  6675. '/div'
  6676. );
  6677. $this->assertTags($result, $expected);
  6678. $result = $this->Form->input('Contact.imalsonotrequired2');
  6679. $expected = array(
  6680. 'div' => array('class' => 'input text'),
  6681. 'label' => array('for' => 'ContactImalsonotrequired2'),
  6682. 'Imalsonotrequired2',
  6683. '/label',
  6684. 'input' => array(
  6685. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  6686. 'id' => 'ContactImalsonotrequired2'
  6687. ),
  6688. '/div'
  6689. );
  6690. $this->assertTags($result, $expected);
  6691. $result = $this->Form->input('Contact.imnotrequiredeither');
  6692. $expected = array(
  6693. 'div' => array('class' => 'input text'),
  6694. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6695. 'Imnotrequiredeither',
  6696. '/label',
  6697. 'input' => array(
  6698. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  6699. 'id' => 'ContactImnotrequiredeither'
  6700. ),
  6701. '/div'
  6702. );
  6703. $this->assertTags($result, $expected);
  6704. $result = $this->Form->input('Contact.iamrequiredalways');
  6705. $expected = array(
  6706. 'div' => array('class' => 'input text required'),
  6707. 'label' => array('for' => 'ContactIamrequiredalways'),
  6708. 'Iamrequiredalways',
  6709. '/label',
  6710. 'input' => array(
  6711. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  6712. 'id' => 'ContactIamrequiredalways',
  6713. 'required' => 'required'
  6714. ),
  6715. '/div'
  6716. );
  6717. $this->assertTags($result, $expected);
  6718. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  6719. $expected = array(
  6720. 'div' => array('class' => 'input checkbox required'),
  6721. array('input' => array(
  6722. 'type' => 'hidden',
  6723. 'name' => 'data[Contact][boolean_field]',
  6724. 'id' => 'ContactBooleanField_',
  6725. 'value' => '0'
  6726. )),
  6727. array('input' => array(
  6728. 'type' => 'checkbox',
  6729. 'name' => 'data[Contact][boolean_field]',
  6730. 'value' => '1',
  6731. 'id' => 'ContactBooleanField'
  6732. )),
  6733. 'label' => array('for' => 'ContactBooleanField'),
  6734. 'Boolean Field',
  6735. '/label',
  6736. '/div'
  6737. );
  6738. $this->assertTags($result, $expected);
  6739. }
  6740. /**
  6741. * testFormMagicInput method
  6742. *
  6743. * @return void
  6744. */
  6745. public function testFormMagicInput() {
  6746. $encoding = strtolower(Configure::read('App.encoding'));
  6747. $result = $this->Form->create('Contact');
  6748. $expected = array(
  6749. 'form' => array(
  6750. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6751. 'accept-charset' => $encoding
  6752. ),
  6753. 'div' => array('style' => 'display:none;'),
  6754. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6755. '/div'
  6756. );
  6757. $this->assertTags($result, $expected);
  6758. $result = $this->Form->input('name');
  6759. $expected = array(
  6760. 'div' => array('class' => 'input text'),
  6761. 'label' => array('for' => 'ContactName'),
  6762. 'Name',
  6763. '/label',
  6764. 'input' => array(
  6765. 'type' => 'text', 'name' => 'data[Contact][name]',
  6766. 'id' => 'ContactName', 'maxlength' => '255'
  6767. ),
  6768. '/div'
  6769. );
  6770. $this->assertTags($result, $expected);
  6771. $result = $this->Form->input('non_existing_field_in_contact_model');
  6772. $expected = array(
  6773. 'div' => array('class' => 'input text'),
  6774. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  6775. 'Non Existing Field In Contact Model',
  6776. '/label',
  6777. 'input' => array(
  6778. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  6779. 'id' => 'ContactNonExistingFieldInContactModel'
  6780. ),
  6781. '/div'
  6782. );
  6783. $this->assertTags($result, $expected);
  6784. $result = $this->Form->input('Address.street');
  6785. $expected = array(
  6786. 'div' => array('class' => 'input text'),
  6787. 'label' => array('for' => 'AddressStreet'),
  6788. 'Street',
  6789. '/label',
  6790. 'input' => array(
  6791. 'type' => 'text', 'name' => 'data[Address][street]',
  6792. 'id' => 'AddressStreet'
  6793. ),
  6794. '/div'
  6795. );
  6796. $this->assertTags($result, $expected);
  6797. $result = $this->Form->input('Address.non_existing_field_in_model');
  6798. $expected = array(
  6799. 'div' => array('class' => 'input text'),
  6800. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  6801. 'Non Existing Field In Model',
  6802. '/label',
  6803. 'input' => array(
  6804. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  6805. 'id' => 'AddressNonExistingFieldInModel'
  6806. ),
  6807. '/div'
  6808. );
  6809. $this->assertTags($result, $expected);
  6810. $result = $this->Form->input('name', array('div' => false));
  6811. $expected = array(
  6812. 'label' => array('for' => 'ContactName'),
  6813. 'Name',
  6814. '/label',
  6815. 'input' => array(
  6816. 'type' => 'text', 'name' => 'data[Contact][name]',
  6817. 'id' => 'ContactName', 'maxlength' => '255'
  6818. )
  6819. );
  6820. $this->assertTags($result, $expected);
  6821. extract($this->dateRegex);
  6822. $now = strtotime('now');
  6823. $result = $this->Form->input('Contact.published', array('div' => false));
  6824. $expected = array(
  6825. 'label' => array('for' => 'ContactPublishedMonth'),
  6826. 'Published',
  6827. '/label',
  6828. array('select' => array(
  6829. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  6830. )),
  6831. $monthsRegex,
  6832. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6833. date('F', $now),
  6834. '/option',
  6835. '*/select',
  6836. '-',
  6837. array('select' => array(
  6838. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  6839. )),
  6840. $daysRegex,
  6841. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6842. date('j', $now),
  6843. '/option',
  6844. '*/select',
  6845. '-',
  6846. array('select' => array(
  6847. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  6848. )),
  6849. $yearsRegex,
  6850. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6851. date('Y', $now),
  6852. '*/select'
  6853. );
  6854. $this->assertTags($result, $expected);
  6855. $result = $this->Form->input('Contact.updated', array('div' => false));
  6856. $expected = array(
  6857. 'label' => array('for' => 'ContactUpdatedMonth'),
  6858. 'Updated',
  6859. '/label',
  6860. array('select' => array(
  6861. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  6862. )),
  6863. $monthsRegex,
  6864. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6865. date('F', $now),
  6866. '/option',
  6867. '*/select',
  6868. '-',
  6869. array('select' => array(
  6870. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  6871. )),
  6872. $daysRegex,
  6873. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6874. date('j', $now),
  6875. '/option',
  6876. '*/select',
  6877. '-',
  6878. array('select' => array(
  6879. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  6880. )),
  6881. $yearsRegex,
  6882. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6883. date('Y', $now),
  6884. '*/select'
  6885. );
  6886. $this->assertTags($result, $expected);
  6887. $result = $this->Form->input('UserForm.stuff');
  6888. $expected = array(
  6889. 'div' => array('class' => 'input text'),
  6890. 'label' => array('for' => 'UserFormStuff'),
  6891. 'Stuff',
  6892. '/label',
  6893. 'input' => array(
  6894. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  6895. 'id' => 'UserFormStuff', 'maxlength' => 10
  6896. ),
  6897. '/div'
  6898. );
  6899. $this->assertTags($result, $expected);
  6900. }
  6901. /**
  6902. * testForMagicInputNonExistingNorValidated method
  6903. *
  6904. * @return void
  6905. */
  6906. public function testForMagicInputNonExistingNorValidated() {
  6907. $encoding = strtolower(Configure::read('App.encoding'));
  6908. $result = $this->Form->create('Contact');
  6909. $expected = array(
  6910. 'form' => array(
  6911. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6912. 'accept-charset' => $encoding
  6913. ),
  6914. 'div' => array('style' => 'display:none;'),
  6915. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6916. '/div'
  6917. );
  6918. $this->assertTags($result, $expected);
  6919. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6920. $expected = array(
  6921. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6922. 'Non Existing Nor Validated',
  6923. '/label',
  6924. 'input' => array(
  6925. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6926. 'id' => 'ContactNonExistingNorValidated'
  6927. )
  6928. );
  6929. $this->assertTags($result, $expected);
  6930. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6931. 'div' => false, 'value' => 'my value'
  6932. ));
  6933. $expected = array(
  6934. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6935. 'Non Existing Nor Validated',
  6936. '/label',
  6937. 'input' => array(
  6938. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6939. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6940. )
  6941. );
  6942. $this->assertTags($result, $expected);
  6943. $this->Form->request->data = array(
  6944. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6945. ));
  6946. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6947. $expected = array(
  6948. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6949. 'Non Existing Nor Validated',
  6950. '/label',
  6951. 'input' => array(
  6952. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6953. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6954. )
  6955. );
  6956. $this->assertTags($result, $expected);
  6957. }
  6958. /**
  6959. * testFormMagicInputLabel method
  6960. *
  6961. * @return void
  6962. */
  6963. public function testFormMagicInputLabel() {
  6964. $encoding = strtolower(Configure::read('App.encoding'));
  6965. $result = $this->Form->create('Contact');
  6966. $expected = array(
  6967. 'form' => array(
  6968. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6969. 'accept-charset' => $encoding
  6970. ),
  6971. 'div' => array('style' => 'display:none;'),
  6972. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6973. '/div'
  6974. );
  6975. $this->assertTags($result, $expected);
  6976. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  6977. $this->assertTags($result, array('input' => array(
  6978. 'name' => 'data[Contact][name]', 'type' => 'text',
  6979. 'id' => 'ContactName', 'maxlength' => '255')
  6980. ));
  6981. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  6982. $expected = array(
  6983. 'label' => array('for' => 'ContactName'),
  6984. 'My label',
  6985. '/label',
  6986. 'input' => array(
  6987. 'type' => 'text', 'name' => 'data[Contact][name]',
  6988. 'id' => 'ContactName', 'maxlength' => '255'
  6989. )
  6990. );
  6991. $this->assertTags($result, $expected);
  6992. $result = $this->Form->input('Contact.name', array(
  6993. 'div' => false, 'label' => array('class' => 'mandatory')
  6994. ));
  6995. $expected = array(
  6996. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6997. 'Name',
  6998. '/label',
  6999. 'input' => array(
  7000. 'type' => 'text', 'name' => 'data[Contact][name]',
  7001. 'id' => 'ContactName', 'maxlength' => '255'
  7002. )
  7003. );
  7004. $this->assertTags($result, $expected);
  7005. $result = $this->Form->input('Contact.name', array(
  7006. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  7007. ));
  7008. $expected = array(
  7009. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7010. 'My label',
  7011. '/label',
  7012. 'input' => array(
  7013. 'type' => 'text', 'name' => 'data[Contact][name]',
  7014. 'id' => 'ContactName', 'maxlength' => '255'
  7015. )
  7016. );
  7017. $this->assertTags($result, $expected);
  7018. $result = $this->Form->input('Contact.name', array(
  7019. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  7020. ));
  7021. $expected = array(
  7022. 'label' => array('for' => 'my_id'),
  7023. 'Name',
  7024. '/label',
  7025. 'input' => array(
  7026. 'type' => 'text', 'name' => 'data[Contact][name]',
  7027. 'id' => 'my_id', 'maxlength' => '255'
  7028. )
  7029. );
  7030. $this->assertTags($result, $expected);
  7031. $result = $this->Form->input('1.id');
  7032. $this->assertTags($result, array('input' => array(
  7033. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7034. 'id' => 'Contact1Id'
  7035. )));
  7036. $result = $this->Form->input("1.name");
  7037. $expected = array(
  7038. 'div' => array('class' => 'input text'),
  7039. 'label' => array('for' => 'Contact1Name'),
  7040. 'Name',
  7041. '/label',
  7042. 'input' => array(
  7043. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  7044. 'id' => 'Contact1Name', 'maxlength' => '255'
  7045. ),
  7046. '/div'
  7047. );
  7048. $this->assertTags($result, $expected);
  7049. $result = $this->Form->input('Contact.1.id');
  7050. $this->assertTags($result, array(
  7051. 'input' => array(
  7052. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7053. 'id' => 'Contact1Id'
  7054. )
  7055. ));
  7056. $result = $this->Form->input("Model.1.name");
  7057. $expected = array(
  7058. 'div' => array('class' => 'input text'),
  7059. 'label' => array('for' => 'Model1Name'),
  7060. 'Name',
  7061. '/label',
  7062. 'input' => array(
  7063. 'type' => 'text', 'name' => 'data[Model][1][name]',
  7064. 'id' => 'Model1Name'
  7065. ),
  7066. '/div'
  7067. );
  7068. $this->assertTags($result, $expected);
  7069. }
  7070. /**
  7071. * testFormEnd method
  7072. *
  7073. * @return void
  7074. */
  7075. public function testFormEnd() {
  7076. $this->assertEquals('</form>', $this->Form->end());
  7077. $result = $this->Form->end('');
  7078. $expected = array(
  7079. 'div' => array('class' => 'submit'),
  7080. 'input' => array('type' => 'submit', 'value' => ''),
  7081. '/div',
  7082. '/form'
  7083. );
  7084. $this->assertTags($result, $expected);
  7085. $result = $this->Form->end(array('label' => ''));
  7086. $expected = array(
  7087. 'div' => array('class' => 'submit'),
  7088. 'input' => array('type' => 'submit', 'value' => ''),
  7089. '/div',
  7090. '/form'
  7091. );
  7092. $this->assertTags($result, $expected);
  7093. $result = $this->Form->end('save');
  7094. $expected = array(
  7095. 'div' => array('class' => 'submit'),
  7096. 'input' => array('type' => 'submit', 'value' => 'save'),
  7097. '/div',
  7098. '/form'
  7099. );
  7100. $this->assertTags($result, $expected);
  7101. $result = $this->Form->end(array('label' => 'save'));
  7102. $expected = array(
  7103. 'div' => array('class' => 'submit'),
  7104. 'input' => array('type' => 'submit', 'value' => 'save'),
  7105. '/div',
  7106. '/form'
  7107. );
  7108. $this->assertTags($result, $expected);
  7109. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  7110. $expected = array(
  7111. 'div' => array('class' => 'submit'),
  7112. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7113. '/div',
  7114. '/form'
  7115. );
  7116. $this->assertTags($result, $expected);
  7117. $result = $this->Form->end(array('name' => 'Whatever'));
  7118. $expected = array(
  7119. 'div' => array('class' => 'submit'),
  7120. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  7121. '/div',
  7122. '/form'
  7123. );
  7124. $this->assertTags($result, $expected);
  7125. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  7126. $expected = array(
  7127. 'div' => array('class' => 'good'),
  7128. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7129. '/div',
  7130. '/form'
  7131. );
  7132. $this->assertTags($result, $expected);
  7133. $result = $this->Form->end(array(
  7134. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  7135. ));
  7136. $expected = array(
  7137. 'div' => array('class' => 'good'),
  7138. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7139. '/div',
  7140. '/form'
  7141. );
  7142. $this->assertTags($result, $expected);
  7143. }
  7144. /**
  7145. * testMultipleFormWithIdFields method
  7146. *
  7147. * @return void
  7148. */
  7149. public function testMultipleFormWithIdFields() {
  7150. $this->Form->create('UserForm');
  7151. $result = $this->Form->input('id');
  7152. $this->assertTags($result, array('input' => array(
  7153. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  7154. )));
  7155. $result = $this->Form->input('ValidateItem.id');
  7156. $this->assertTags($result, array('input' => array(
  7157. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  7158. 'id' => 'ValidateItemId'
  7159. )));
  7160. $result = $this->Form->input('ValidateUser.id');
  7161. $this->assertTags($result, array('input' => array(
  7162. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  7163. 'id' => 'ValidateUserId'
  7164. )));
  7165. }
  7166. /**
  7167. * testDbLessModel method
  7168. *
  7169. * @return void
  7170. */
  7171. public function testDbLessModel() {
  7172. $this->Form->create('TestMail');
  7173. $result = $this->Form->input('name');
  7174. $expected = array(
  7175. 'div' => array('class' => 'input text'),
  7176. 'label' => array('for' => 'TestMailName'),
  7177. 'Name',
  7178. '/label',
  7179. 'input' => array(
  7180. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7181. 'id' => 'TestMailName'
  7182. ),
  7183. '/div'
  7184. );
  7185. $this->assertTags($result, $expected);
  7186. ClassRegistry::init('TestMail');
  7187. $this->Form->create('TestMail');
  7188. $result = $this->Form->input('name');
  7189. $expected = array(
  7190. 'div' => array('class' => 'input text'),
  7191. 'label' => array('for' => 'TestMailName'),
  7192. 'Name',
  7193. '/label',
  7194. 'input' => array(
  7195. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7196. 'id' => 'TestMailName'
  7197. ),
  7198. '/div'
  7199. );
  7200. $this->assertTags($result, $expected);
  7201. }
  7202. /**
  7203. * testBrokenness method
  7204. *
  7205. * @return void
  7206. */
  7207. public function testBrokenness() {
  7208. /*
  7209. * #4 This test has two parents and four children. By default (as of r7117) both
  7210. * parents are show but the first parent is missing a child. This is the inconsistency
  7211. * in the default behaviour - one parent has all children, the other does not - dependent
  7212. * on the data values.
  7213. */
  7214. $result = $this->Form->select('Model.field', array(
  7215. 'Fred' => array(
  7216. 'freds_son_1' => 'Fred',
  7217. 'freds_son_2' => 'Freddie'
  7218. ),
  7219. 'Bert' => array(
  7220. 'berts_son_1' => 'Albert',
  7221. 'berts_son_2' => 'Bertie')
  7222. ),
  7223. array('showParents' => true, 'empty' => false)
  7224. );
  7225. $expected = array(
  7226. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7227. array('optgroup' => array('label' => 'Fred')),
  7228. array('option' => array('value' => 'freds_son_1')),
  7229. 'Fred',
  7230. '/option',
  7231. array('option' => array('value' => 'freds_son_2')),
  7232. 'Freddie',
  7233. '/option',
  7234. '/optgroup',
  7235. array('optgroup' => array('label' => 'Bert')),
  7236. array('option' => array('value' => 'berts_son_1')),
  7237. 'Albert',
  7238. '/option',
  7239. array('option' => array('value' => 'berts_son_2')),
  7240. 'Bertie',
  7241. '/option',
  7242. '/optgroup',
  7243. '/select'
  7244. );
  7245. $this->assertTags($result, $expected);
  7246. /*
  7247. * #2 This is structurally identical to the test above (#1) - only the parent name has
  7248. * changed, so we should expect the same select list data, just with a different name
  7249. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  7250. * This is where data corruption can occur, because when a select value is missing from
  7251. * a list a form will substitute the first value in the list - without the user knowing.
  7252. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  7253. * affect the availability of 3 => 'Three' as a valid option.
  7254. */
  7255. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  7256. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  7257. ));
  7258. $result = $this->Form->select(
  7259. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  7260. );
  7261. $expected = array(
  7262. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7263. array('option' => array('value' => 1)),
  7264. 'One',
  7265. '/option',
  7266. array('option' => array('value' => 2)),
  7267. 'Two',
  7268. '/option',
  7269. array('optgroup' => array('label' => 'Three')),
  7270. array('option' => array('value' => 3)),
  7271. 'Three',
  7272. '/option',
  7273. array('option' => array('value' => 4)),
  7274. 'Four',
  7275. '/option',
  7276. array('option' => array('value' => 5)),
  7277. 'Five',
  7278. '/option',
  7279. '/optgroup',
  7280. '/select'
  7281. );
  7282. $this->assertTags($result, $expected);
  7283. }
  7284. /**
  7285. * Test the generation of fields for a multi record form.
  7286. *
  7287. * @return void
  7288. */
  7289. public function testMultiRecordForm() {
  7290. $this->Form->create('ValidateProfile');
  7291. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  7292. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  7293. $expected = array(
  7294. 'div' => array('class' => 'input textarea'),
  7295. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  7296. 'Name',
  7297. '/label',
  7298. 'textarea' => array(
  7299. 'id' => 'ValidateProfile1ValidateItem2Name',
  7300. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  7301. 'cols' => 30,
  7302. 'rows' => 6
  7303. ),
  7304. 'Value',
  7305. '/textarea',
  7306. '/div'
  7307. );
  7308. $this->assertTags($result, $expected);
  7309. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
  7310. $expected = array(
  7311. 'div' => array('class' => 'input date'),
  7312. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7313. 'Created',
  7314. '/label',
  7315. array('select' => array(
  7316. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  7317. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7318. )
  7319. ),
  7320. array('option' => array('value' => '')), '/option',
  7321. $this->dateRegex['monthsRegex'],
  7322. '/select', '-',
  7323. array('select' => array(
  7324. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  7325. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7326. )
  7327. ),
  7328. array('option' => array('value' => '')), '/option',
  7329. $this->dateRegex['daysRegex'],
  7330. '/select', '-',
  7331. array('select' => array(
  7332. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  7333. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7334. )
  7335. ),
  7336. array('option' => array('value' => '')), '/option',
  7337. $this->dateRegex['yearsRegex'],
  7338. '/select',
  7339. '/div'
  7340. );
  7341. $this->assertTags($result, $expected);
  7342. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7343. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7344. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7345. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7346. $expected = array(
  7347. 'div' => array('class' => 'input select error'),
  7348. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7349. 'Profile',
  7350. '/label',
  7351. 'select' => array(
  7352. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7353. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7354. 'class' => 'form-error'
  7355. ),
  7356. '/select',
  7357. array('div' => array('class' => 'error-message')),
  7358. 'Error',
  7359. '/div',
  7360. '/div'
  7361. );
  7362. $this->assertTags($result, $expected);
  7363. }
  7364. /**
  7365. * test the correct display of multi-record form validation errors.
  7366. *
  7367. * @return void
  7368. */
  7369. public function testMultiRecordFormValidationErrors() {
  7370. $this->Form->create('ValidateProfile');
  7371. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7372. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7373. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7374. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7375. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7376. $result = $this->Form->error('ValidateProfile.2.city');
  7377. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7378. $result = $this->Form->error('2.city');
  7379. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7380. }
  7381. /**
  7382. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7383. *
  7384. * @return void
  7385. */
  7386. public function testInputTemplate() {
  7387. $result = $this->Form->input('Contact.email', array(
  7388. 'type' => 'text', 'format' => array('input')
  7389. ));
  7390. $expected = array(
  7391. 'div' => array('class' => 'input text'),
  7392. 'input' => array(
  7393. 'type' => 'text', 'name' => 'data[Contact][email]',
  7394. 'id' => 'ContactEmail'
  7395. ),
  7396. '/div'
  7397. );
  7398. $this->assertTags($result, $expected);
  7399. $result = $this->Form->input('Contact.email', array(
  7400. 'type' => 'text', 'format' => array('input', 'label'),
  7401. 'label' => '<em>Email (required)</em>'
  7402. ));
  7403. $expected = array(
  7404. 'div' => array('class' => 'input text'),
  7405. array('input' => array(
  7406. 'type' => 'text', 'name' => 'data[Contact][email]',
  7407. 'id' => 'ContactEmail'
  7408. )),
  7409. 'label' => array('for' => 'ContactEmail'),
  7410. 'em' => array(),
  7411. 'Email (required)',
  7412. '/em',
  7413. '/label',
  7414. '/div'
  7415. );
  7416. $this->assertTags($result, $expected);
  7417. $result = $this->Form->input('Contact.email', array(
  7418. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7419. 'between' => '<div>Something in the middle</div>',
  7420. 'after' => '<span>Some text at the end</span>'
  7421. ));
  7422. $expected = array(
  7423. 'div' => array('class' => 'input text'),
  7424. array('input' => array(
  7425. 'type' => 'text', 'name' => 'data[Contact][email]',
  7426. 'id' => 'ContactEmail'
  7427. )),
  7428. array('div' => array()),
  7429. 'Something in the middle',
  7430. '/div',
  7431. 'label' => array('for' => 'ContactEmail'),
  7432. 'Email',
  7433. '/label',
  7434. 'span' => array(),
  7435. 'Some text at the end',
  7436. '/span',
  7437. '/div'
  7438. );
  7439. $this->assertTags($result, $expected);
  7440. $result = $this->Form->input('Contact.method', array(
  7441. 'type' => 'radio',
  7442. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7443. 'between' => 'I am between',
  7444. ));
  7445. $expected = array(
  7446. 'div' => array('class' => 'input radio'),
  7447. 'fieldset' => array(),
  7448. 'legend' => array(),
  7449. 'Method',
  7450. '/legend',
  7451. 'I am between',
  7452. 'input' => array(
  7453. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  7454. 'value' => '', 'id' => 'ContactMethod_'
  7455. ),
  7456. array('input' => array(
  7457. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7458. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7459. )),
  7460. array('label' => array('for' => 'ContactMethodEmail')),
  7461. 'Email',
  7462. '/label',
  7463. array('input' => array(
  7464. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7465. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7466. )),
  7467. array('label' => array('for' => 'ContactMethodPigeon')),
  7468. 'Pigeon',
  7469. '/label',
  7470. '/fieldset',
  7471. '/div',
  7472. );
  7473. $this->assertTags($result, $expected);
  7474. }
  7475. /**
  7476. * test that some html5 inputs + FormHelper::__call() work
  7477. *
  7478. * @return void
  7479. */
  7480. public function testHtml5Inputs() {
  7481. $result = $this->Form->email('User.email');
  7482. $expected = array(
  7483. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  7484. );
  7485. $this->assertTags($result, $expected);
  7486. $result = $this->Form->search('User.query');
  7487. $expected = array(
  7488. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  7489. );
  7490. $this->assertTags($result, $expected);
  7491. $result = $this->Form->search('User.query', array('value' => 'test'));
  7492. $expected = array(
  7493. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7494. );
  7495. $this->assertTags($result, $expected);
  7496. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7497. $expected = array(
  7498. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7499. );
  7500. $this->assertTags($result, $expected);
  7501. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  7502. $expected = array(
  7503. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7504. );
  7505. $this->assertTags($result, $expected);
  7506. }
  7507. /**
  7508. *
  7509. * @expectedException CakeException
  7510. * @return void
  7511. */
  7512. public function testHtml5InputException() {
  7513. $this->Form->email();
  7514. }
  7515. /**
  7516. * Tests that a model can be loaded from the model names passed in the request object
  7517. *
  7518. * @return void
  7519. */
  7520. public function testIntrospectModelFromRequest() {
  7521. $this->loadFixtures('Post');
  7522. App::build(array(
  7523. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  7524. ));
  7525. CakePlugin::load('TestPlugin');
  7526. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  7527. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  7528. $this->Form->create('TestPluginPost');
  7529. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  7530. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  7531. CakePlugin::unload();
  7532. App::build();
  7533. }
  7534. /**
  7535. * Tests that it is possible to set the validation errors directly in the helper for a field
  7536. *
  7537. * @return void
  7538. */
  7539. public function testCustomValidationErrors() {
  7540. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7541. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7542. $this->assertEquals('Badness!', $result);
  7543. }
  7544. /**
  7545. * Tests that the 'on' key validates as expected on create
  7546. *
  7547. * @return void
  7548. */
  7549. public function testRequiredOnCreate() {
  7550. $this->Form->create('Contact');
  7551. $result = $this->Form->input('Contact.imrequiredonupdate');
  7552. $expected = array(
  7553. 'div' => array('class' => 'input text'),
  7554. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7555. 'Imrequiredonupdate',
  7556. '/label',
  7557. 'input' => array(
  7558. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7559. 'id' => 'ContactImrequiredonupdate'
  7560. ),
  7561. '/div'
  7562. );
  7563. $this->assertTags($result, $expected);
  7564. $result = $this->Form->input('Contact.imrequiredoncreate');
  7565. $expected = array(
  7566. 'div' => array('class' => 'input text required'),
  7567. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7568. 'Imrequiredoncreate',
  7569. '/label',
  7570. 'input' => array(
  7571. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7572. 'id' => 'ContactImrequiredoncreate',
  7573. 'required' => 'required'
  7574. ),
  7575. '/div'
  7576. );
  7577. $this->assertTags($result, $expected);
  7578. $result = $this->Form->input('Contact.imrequiredonboth');
  7579. $expected = array(
  7580. 'div' => array('class' => 'input text required'),
  7581. 'label' => array('for' => 'ContactImrequiredonboth'),
  7582. 'Imrequiredonboth',
  7583. '/label',
  7584. 'input' => array(
  7585. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7586. 'id' => 'ContactImrequiredonboth',
  7587. 'required' => 'required'
  7588. ),
  7589. '/div'
  7590. );
  7591. $this->assertTags($result, $expected);
  7592. $this->Form->inputDefaults(array('required' => false));
  7593. $result = $this->Form->input('Contact.imrequired');
  7594. $expected = array(
  7595. 'div' => array('class' => 'input text required'),
  7596. 'label' => array('for' => 'ContactImrequired'),
  7597. 'Imrequired',
  7598. '/label',
  7599. 'input' => array(
  7600. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7601. 'id' => 'ContactImrequired'
  7602. ),
  7603. '/div'
  7604. );
  7605. $this->assertTags($result, $expected);
  7606. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  7607. $this->assertTags($result, $expected);
  7608. }
  7609. /**
  7610. * Tests that the 'on' key validates as expected on update
  7611. *
  7612. * @return void
  7613. */
  7614. public function testRequiredOnUpdate() {
  7615. $this->Form->request->data['Contact']['id'] = 1;
  7616. $this->Form->create('Contact');
  7617. $result = $this->Form->input('Contact.imrequiredonupdate');
  7618. $expected = array(
  7619. 'div' => array('class' => 'input text required'),
  7620. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7621. 'Imrequiredonupdate',
  7622. '/label',
  7623. 'input' => array(
  7624. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7625. 'id' => 'ContactImrequiredonupdate',
  7626. 'required' => 'required'
  7627. ),
  7628. '/div'
  7629. );
  7630. $this->assertTags($result, $expected);
  7631. $result = $this->Form->input('Contact.imrequiredoncreate');
  7632. $expected = array(
  7633. 'div' => array('class' => 'input text'),
  7634. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7635. 'Imrequiredoncreate',
  7636. '/label',
  7637. 'input' => array(
  7638. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7639. 'id' => 'ContactImrequiredoncreate'
  7640. ),
  7641. '/div'
  7642. );
  7643. $this->assertTags($result, $expected);
  7644. $result = $this->Form->input('Contact.imrequiredonboth');
  7645. $expected = array(
  7646. 'div' => array('class' => 'input text required'),
  7647. 'label' => array('for' => 'ContactImrequiredonboth'),
  7648. 'Imrequiredonboth',
  7649. '/label',
  7650. 'input' => array(
  7651. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7652. 'id' => 'ContactImrequiredonboth',
  7653. 'required' => 'required'
  7654. ),
  7655. '/div'
  7656. );
  7657. $this->assertTags($result, $expected);
  7658. $result = $this->Form->input('Contact.imrequired');
  7659. $expected = array(
  7660. 'div' => array('class' => 'input text required'),
  7661. 'label' => array('for' => 'ContactImrequired'),
  7662. 'Imrequired',
  7663. '/label',
  7664. 'input' => array(
  7665. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7666. 'id' => 'ContactImrequired',
  7667. 'required' => 'required'
  7668. ),
  7669. '/div'
  7670. );
  7671. $this->assertTags($result, $expected);
  7672. }
  7673. /**
  7674. * Test inputDefaults setter and getter
  7675. *
  7676. * @return void
  7677. */
  7678. public function testInputDefaults() {
  7679. $this->Form->create('Contact');
  7680. $this->Form->inputDefaults(array(
  7681. 'label' => false,
  7682. 'div' => array(
  7683. 'style' => 'color: #000;'
  7684. )
  7685. ));
  7686. $result = $this->Form->input('Contact.field1');
  7687. $expected = array(
  7688. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  7689. 'input' => array(
  7690. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7691. 'id' => 'ContactField1'
  7692. ),
  7693. '/div'
  7694. );
  7695. $this->assertTags($result, $expected);
  7696. $this->Form->inputDefaults(array(
  7697. 'div' => false,
  7698. 'label' => 'Label',
  7699. ));
  7700. $result = $this->Form->input('Contact.field1');
  7701. $expected = array(
  7702. 'label' => array('for' => 'ContactField1'),
  7703. 'Label',
  7704. '/label',
  7705. 'input' => array(
  7706. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7707. 'id' => 'ContactField1'
  7708. ),
  7709. );
  7710. $this->assertTags($result, $expected);
  7711. $this->Form->inputDefaults(array(
  7712. 'label' => false,
  7713. ), true);
  7714. $result = $this->Form->input('Contact.field1');
  7715. $expected = array(
  7716. 'input' => array(
  7717. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7718. 'id' => 'ContactField1'
  7719. ),
  7720. );
  7721. $this->assertTags($result, $expected);
  7722. $result = $this->Form->inputDefaults();
  7723. $expected = array(
  7724. 'div' => false,
  7725. 'label' => false,
  7726. );
  7727. $this->assertEqual($result, $expected);
  7728. }
  7729. }