FormHelperTest.php 262 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc.
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc.
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.View.Helper
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('ClassRegistry', 'Utility');
  20. App::uses('Controller', 'Controller');
  21. App::uses('View', 'View');
  22. App::uses('Model', 'Model');
  23. App::uses('Security', 'Utility');
  24. App::uses('CakeRequest', 'Network');
  25. App::uses('HtmlHelper', 'View/Helper');
  26. App::uses('FormHelper', 'View/Helper');
  27. App::uses('Router', 'Routing');
  28. /**
  29. * ContactTestController class
  30. *
  31. * @package cake
  32. * @package Cake.Test.Case.View.Helper
  33. */
  34. class ContactTestController extends Controller {
  35. /**
  36. * name property
  37. *
  38. * @var string 'ContactTest'
  39. */
  40. public $name = 'ContactTest';
  41. /**
  42. * uses property
  43. *
  44. * @var mixed null
  45. */
  46. public $uses = null;
  47. }
  48. /**
  49. * Contact class
  50. *
  51. * @package cake
  52. * @package Cake.Test.Case.View.Helper
  53. */
  54. class Contact extends CakeTestModel {
  55. /**
  56. * primaryKey property
  57. *
  58. * @var string 'id'
  59. */
  60. public $primaryKey = 'id';
  61. /**
  62. * useTable property
  63. *
  64. * @var bool false
  65. */
  66. public $useTable = false;
  67. /**
  68. * name property
  69. *
  70. * @var string 'Contact'
  71. */
  72. public $name = 'Contact';
  73. /**
  74. * Default schema
  75. *
  76. * @var array
  77. */
  78. protected $_schema = array(
  79. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  80. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  81. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  82. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  83. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  84. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  85. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  86. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  87. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  88. );
  89. /**
  90. * validate property
  91. *
  92. * @var array
  93. */
  94. public $validate = array(
  95. 'non_existing' => array(),
  96. 'idontexist' => array(),
  97. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  98. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  99. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  100. 'imrequiredonboth' => array(
  101. 'required' => array('rule' => 'alphaNumeric'),
  102. ),
  103. 'string_required' => 'notEmpty',
  104. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  105. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  106. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  107. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  108. 'imalsonotrequired' => array(
  109. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  110. 'between' => array('rule' => array('between', 5, 30)),
  111. ),
  112. 'imalsonotrequired2' => array(
  113. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  114. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  115. ),
  116. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  117. 'iamrequiredalways' => array(
  118. 'email' => array('rule' => 'email'),
  119. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  120. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  121. ),
  122. );
  123. /**
  124. * schema method
  125. *
  126. * @return void
  127. */
  128. public function setSchema($schema) {
  129. $this->_schema = $schema;
  130. }
  131. /**
  132. * hasAndBelongsToMany property
  133. *
  134. * @var array
  135. */
  136. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  137. /**
  138. * hasAndBelongsToMany property
  139. *
  140. * @var array
  141. */
  142. public $belongsTo = array('User' => array('className' => 'UserForm'));
  143. }
  144. /**
  145. * ContactTagsContact class
  146. *
  147. * @package cake
  148. * @package Cake.Test.Case.View.Helper
  149. */
  150. class ContactTagsContact extends CakeTestModel {
  151. /**
  152. * useTable property
  153. *
  154. * @var bool false
  155. */
  156. public $useTable = false;
  157. /**
  158. * name property
  159. *
  160. * @var string 'Contact'
  161. */
  162. public $name = 'ContactTagsContact';
  163. /**
  164. * Default schema
  165. *
  166. * @var array
  167. */
  168. protected $_schema = array(
  169. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  170. 'contact_tag_id' => array(
  171. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  172. )
  173. );
  174. /**
  175. * schema method
  176. *
  177. * @return void
  178. */
  179. public function setSchema($schema) {
  180. $this->_schema = $schema;
  181. }
  182. }
  183. /**
  184. * ContactNonStandardPk class
  185. *
  186. * @package cake
  187. * @package Cake.Test.Case.View.Helper
  188. */
  189. class ContactNonStandardPk extends Contact {
  190. /**
  191. * primaryKey property
  192. *
  193. * @var string 'pk'
  194. */
  195. public $primaryKey = 'pk';
  196. /**
  197. * name property
  198. *
  199. * @var string 'ContactNonStandardPk'
  200. */
  201. public $name = 'ContactNonStandardPk';
  202. /**
  203. * schema method
  204. *
  205. * @return void
  206. */
  207. public function schema($field = false) {
  208. $this->_schema = parent::schema();
  209. $this->_schema['pk'] = $this->_schema['id'];
  210. unset($this->_schema['id']);
  211. return $this->_schema;
  212. }
  213. }
  214. /**
  215. * ContactTag class
  216. *
  217. * @package cake
  218. * @package Cake.Test.Case.View.Helper
  219. */
  220. class ContactTag extends Model {
  221. /**
  222. * useTable property
  223. *
  224. * @var bool false
  225. */
  226. public $useTable = false;
  227. /**
  228. * schema definition
  229. *
  230. * @var array
  231. */
  232. protected $_schema = array(
  233. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  234. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  235. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  236. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  237. );
  238. }
  239. /**
  240. * UserForm class
  241. *
  242. * @package cake
  243. * @package Cake.Test.Case.View.Helper
  244. */
  245. class UserForm extends CakeTestModel {
  246. /**
  247. * useTable property
  248. *
  249. * @var bool false
  250. */
  251. public $useTable = false;
  252. /**
  253. * primaryKey property
  254. *
  255. * @var string 'id'
  256. */
  257. public $primaryKey = 'id';
  258. /**
  259. * name property
  260. *
  261. * @var string 'UserForm'
  262. */
  263. public $name = 'UserForm';
  264. /**
  265. * hasMany property
  266. *
  267. * @var array
  268. */
  269. public $hasMany = array(
  270. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  271. ));
  272. /**
  273. * schema definition
  274. *
  275. * @var array
  276. */
  277. protected $_schema = array(
  278. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  279. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  280. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  281. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  282. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  283. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  284. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  285. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  286. );
  287. }
  288. /**
  289. * OpenidUrl class
  290. *
  291. * @package cake
  292. * @package Cake.Test.Case.View.Helper
  293. */
  294. class OpenidUrl extends CakeTestModel {
  295. /**
  296. * useTable property
  297. *
  298. * @var bool false
  299. */
  300. public $useTable = false;
  301. /**
  302. * primaryKey property
  303. *
  304. * @var string 'id'
  305. */
  306. public $primaryKey = 'id';
  307. /**
  308. * name property
  309. *
  310. * @var string 'OpenidUrl'
  311. */
  312. public $name = 'OpenidUrl';
  313. /**
  314. * belongsTo property
  315. *
  316. * @var array
  317. */
  318. public $belongsTo = array('UserForm' => array(
  319. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  320. ));
  321. /**
  322. * validate property
  323. *
  324. * @var array
  325. */
  326. public $validate = array('openid_not_registered' => array());
  327. /**
  328. * schema method
  329. *
  330. * @var array
  331. */
  332. protected $_schema = array(
  333. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  334. 'user_form_id' => array(
  335. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  336. ),
  337. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  338. );
  339. /**
  340. * beforeValidate method
  341. *
  342. * @return void
  343. */
  344. public function beforeValidate($options = array()) {
  345. $this->invalidate('openid_not_registered');
  346. return true;
  347. }
  348. }
  349. /**
  350. * ValidateUser class
  351. *
  352. * @package cake
  353. * @package Cake.Test.Case.View.Helper
  354. */
  355. class ValidateUser extends CakeTestModel {
  356. /**
  357. * primaryKey property
  358. *
  359. * @var string 'id'
  360. */
  361. public $primaryKey = 'id';
  362. /**
  363. * useTable property
  364. *
  365. * @var bool false
  366. */
  367. public $useTable = false;
  368. /**
  369. * name property
  370. *
  371. * @var string 'ValidateUser'
  372. */
  373. public $name = 'ValidateUser';
  374. /**
  375. * hasOne property
  376. *
  377. * @var array
  378. */
  379. public $hasOne = array('ValidateProfile' => array(
  380. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  381. ));
  382. /**
  383. * schema method
  384. *
  385. * @var array
  386. */
  387. protected $_schema = array(
  388. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  389. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  390. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  391. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  392. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  393. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  394. );
  395. /**
  396. * beforeValidate method
  397. *
  398. * @return void
  399. */
  400. public function beforeValidate($options = array()) {
  401. $this->invalidate('email');
  402. return false;
  403. }
  404. }
  405. /**
  406. * ValidateProfile class
  407. *
  408. * @package cake
  409. * @package Cake.Test.Case.View.Helper
  410. */
  411. class ValidateProfile extends CakeTestModel {
  412. /**
  413. * primaryKey property
  414. *
  415. * @var string 'id'
  416. */
  417. public $primaryKey = 'id';
  418. /**
  419. * useTable property
  420. *
  421. * @var bool false
  422. */
  423. public $useTable = false;
  424. /**
  425. * schema property
  426. *
  427. * @var array
  428. */
  429. protected $_schema = array(
  430. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  431. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  432. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  433. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  434. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  435. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  436. );
  437. /**
  438. * name property
  439. *
  440. * @var string 'ValidateProfile'
  441. */
  442. public $name = 'ValidateProfile';
  443. /**
  444. * hasOne property
  445. *
  446. * @var array
  447. */
  448. public $hasOne = array('ValidateItem' => array(
  449. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  450. ));
  451. /**
  452. * belongsTo property
  453. *
  454. * @var array
  455. */
  456. public $belongsTo = array('ValidateUser' => array(
  457. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  458. ));
  459. /**
  460. * beforeValidate method
  461. *
  462. * @return void
  463. */
  464. public function beforeValidate($options = array()) {
  465. $this->invalidate('full_name');
  466. $this->invalidate('city');
  467. return false;
  468. }
  469. }
  470. /**
  471. * ValidateItem class
  472. *
  473. * @package cake
  474. * @package Cake.Test.Case.View.Helper
  475. */
  476. class ValidateItem extends CakeTestModel {
  477. /**
  478. * primaryKey property
  479. *
  480. * @var string 'id'
  481. */
  482. public $primaryKey = 'id';
  483. /**
  484. * useTable property
  485. *
  486. * @var bool false
  487. */
  488. public $useTable = false;
  489. /**
  490. * name property
  491. *
  492. * @var string 'ValidateItem'
  493. */
  494. public $name = 'ValidateItem';
  495. /**
  496. * schema property
  497. *
  498. * @var array
  499. */
  500. protected $_schema = array(
  501. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  502. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  503. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  504. 'description' => array(
  505. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  506. ),
  507. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  508. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  509. );
  510. /**
  511. * belongsTo property
  512. *
  513. * @var array
  514. */
  515. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  516. /**
  517. * beforeValidate method
  518. *
  519. * @return void
  520. */
  521. public function beforeValidate($options = array()) {
  522. $this->invalidate('description');
  523. return false;
  524. }
  525. }
  526. /**
  527. * TestMail class
  528. *
  529. * @package cake
  530. * @package Cake.Test.Case.View.Helper
  531. */
  532. class TestMail extends CakeTestModel {
  533. /**
  534. * primaryKey property
  535. *
  536. * @var string 'id'
  537. */
  538. public $primaryKey = 'id';
  539. /**
  540. * useTable property
  541. *
  542. * @var bool false
  543. */
  544. public $useTable = false;
  545. /**
  546. * name property
  547. *
  548. * @var string 'TestMail'
  549. */
  550. public $name = 'TestMail';
  551. }
  552. /**
  553. * FormHelperTest class
  554. *
  555. * @package cake
  556. * @subpackage Cake.Test.Case.View.Helper
  557. * @property FormHelper $Form
  558. */
  559. class FormHelperTest extends CakeTestCase {
  560. /**
  561. * Fixtures to be used
  562. *
  563. * @var array
  564. */
  565. public $fixtures = array('core.post');
  566. /**
  567. * Do not load the fixtures by default
  568. *
  569. * @var boolean
  570. */
  571. public $autoFixtures = false;
  572. /**
  573. * setUp method
  574. *
  575. * @return void
  576. */
  577. public function setUp() {
  578. parent::setUp();
  579. Configure::write('App.base', '');
  580. $this->Controller = new ContactTestController();
  581. $this->View = new View($this->Controller);
  582. $this->Form = new FormHelper($this->View);
  583. $this->Form->Html = new HtmlHelper($this->View);
  584. $this->Form->request = new CakeRequest('contacts/add', false);
  585. $this->Form->request->here = '/contacts/add';
  586. $this->Form->request['action'] = 'add';
  587. $this->Form->request->webroot = '';
  588. $this->Form->request->base = '';
  589. ClassRegistry::addObject('Contact', new Contact());
  590. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  591. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  592. ClassRegistry::addObject('User', new UserForm());
  593. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  594. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  595. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  596. $this->oldSalt = Configure::read('Security.salt');
  597. $this->dateRegex = array(
  598. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  599. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  600. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  601. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  602. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  603. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  604. );
  605. Configure::write('Security.salt', 'foo!');
  606. }
  607. /**
  608. * tearDown method
  609. *
  610. * @return void
  611. */
  612. public function tearDown() {
  613. parent::tearDown();
  614. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  615. Configure::write('Security.salt', $this->oldSalt);
  616. }
  617. /**
  618. * testFormCreateWithSecurity method
  619. *
  620. * Test form->create() with security key.
  621. *
  622. * @return void
  623. */
  624. public function testCreateWithSecurity() {
  625. $this->Form->request['_Token'] = array('key' => 'testKey');
  626. $encoding = strtolower(Configure::read('App.encoding'));
  627. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  628. $expected = array(
  629. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  630. 'div' => array('style' => 'display:none;'),
  631. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  632. array('input' => array(
  633. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  634. )),
  635. '/div'
  636. );
  637. $this->assertTags($result, $expected);
  638. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  639. $expected['form']['id'] = 'MyForm';
  640. $this->assertTags($result, $expected);
  641. }
  642. /**
  643. * test that create() clears the fields property so it starts fresh
  644. *
  645. * @return void
  646. */
  647. public function testCreateClearingFields() {
  648. $this->Form->fields = array('model_id');
  649. $this->Form->create('Contact');
  650. $this->assertEquals(array(), $this->Form->fields);
  651. }
  652. /**
  653. * Tests form hash generation with model-less data
  654. *
  655. * @return void
  656. */
  657. public function testValidateHashNoModel() {
  658. $this->Form->request['_Token'] = array('key' => 'foo');
  659. $result = $this->Form->secure(array('anything'));
  660. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  661. }
  662. /**
  663. * Tests that models with identical field names get resolved properly
  664. *
  665. * @return void
  666. */
  667. public function testDuplicateFieldNameResolution() {
  668. $result = $this->Form->create('ValidateUser');
  669. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  670. $result = $this->Form->input('ValidateItem.name');
  671. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  672. $result = $this->Form->input('ValidateUser.name');
  673. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  674. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  675. $this->assertRegExp('/type="text"/', $result);
  676. $result = $this->Form->input('ValidateItem.name');
  677. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  678. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  679. $this->assertRegExp('/<textarea/', $result);
  680. $result = $this->Form->input('name');
  681. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  682. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  683. $this->assertRegExp('/type="text"/', $result);
  684. }
  685. /**
  686. * Tests that hidden fields generated for checkboxes don't get locked
  687. *
  688. * @return void
  689. */
  690. public function testNoCheckboxLocking() {
  691. $this->Form->request['_Token'] = array('key' => 'foo');
  692. $this->assertSame(array(), $this->Form->fields);
  693. $this->Form->checkbox('check', array('value' => '1'));
  694. $this->assertSame($this->Form->fields, array('check'));
  695. }
  696. /**
  697. * testFormSecurityFields method
  698. *
  699. * Test generation of secure form hash generation.
  700. *
  701. * @return void
  702. */
  703. public function testFormSecurityFields() {
  704. $key = 'testKey';
  705. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  706. $this->Form->request['_Token'] = array('key' => $key);
  707. $result = $this->Form->secure($fields);
  708. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  709. $hash .= ':' . 'Model.valid';
  710. $hash = urlencode($hash);
  711. $expected = array(
  712. 'div' => array('style' => 'display:none;'),
  713. array('input' => array(
  714. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  715. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  716. )),
  717. array('input' => array(
  718. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  719. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  720. )),
  721. '/div'
  722. );
  723. $this->assertTags($result, $expected);
  724. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  725. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  726. $result = $this->Form->secure($fields);
  727. $expected = array(
  728. 'div' => array('class' => 'hidden'),
  729. array('input' => array(
  730. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  731. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  732. )),
  733. array('input' => array(
  734. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  735. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  736. )),
  737. '/div'
  738. );
  739. $this->assertTags($result, $expected);
  740. }
  741. /**
  742. * Tests correct generation of number fields for double and float fields
  743. *
  744. * @return void
  745. */
  746. public function testTextFieldGenerationForFloats() {
  747. $model = ClassRegistry::getObject('Contact');
  748. $model->setSchema(array('foo' => array(
  749. 'type' => 'float',
  750. 'null' => false,
  751. 'default' => null,
  752. 'length' => null
  753. )));
  754. $this->Form->create('Contact');
  755. $result = $this->Form->input('foo');
  756. $expected = array(
  757. 'div' => array('class' => 'input number'),
  758. 'label' => array('for' => 'ContactFoo'),
  759. 'Foo',
  760. '/label',
  761. array('input' => array(
  762. 'type' => 'number',
  763. 'name' => 'data[Contact][foo]',
  764. 'id' => 'ContactFoo',
  765. 'step' => 'any'
  766. )),
  767. '/div'
  768. );
  769. $this->assertTags($result, $expected);
  770. $result = $this->Form->input('foo', array('step' => 0.5));
  771. $expected = array(
  772. 'div' => array('class' => 'input number'),
  773. 'label' => array('for' => 'ContactFoo'),
  774. 'Foo',
  775. '/label',
  776. array('input' => array(
  777. 'type' => 'number',
  778. 'name' => 'data[Contact][foo]',
  779. 'id' => 'ContactFoo',
  780. 'step' => '0.5'
  781. )),
  782. '/div'
  783. );
  784. $this->assertTags($result, $expected);
  785. }
  786. /**
  787. * Tests correct generation of number fields for integer fields
  788. *
  789. * @access public
  790. * @return void
  791. */
  792. public function testTextFieldTypeNumberGenerationForIntegers() {
  793. $model = ClassRegistry::getObject('Contact');
  794. $model->setSchema(array('foo' => array(
  795. 'type' => 'integer',
  796. 'null' => false,
  797. 'default' => null,
  798. 'length' => null
  799. )));
  800. $this->Form->create('Contact');
  801. $result = $this->Form->input('foo');
  802. $expected = array(
  803. 'div' => array('class' => 'input number'),
  804. 'label' => array('for' => 'ContactFoo'),
  805. 'Foo',
  806. '/label',
  807. array('input' => array(
  808. 'type' => 'number', 'name' => 'data[Contact][foo]',
  809. 'id' => 'ContactFoo'
  810. )),
  811. '/div'
  812. );
  813. $this->assertTags($result, $expected);
  814. }
  815. /**
  816. * testFormSecurityMultipleFields method
  817. *
  818. * Test secure() with multiple row form. Ensure hash is correct.
  819. *
  820. * @return void
  821. */
  822. public function testFormSecurityMultipleFields() {
  823. $key = 'testKey';
  824. $fields = array(
  825. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  826. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  827. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  828. );
  829. $this->Form->request['_Token'] = array('key' => $key);
  830. $result = $this->Form->secure($fields);
  831. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  832. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  833. $expected = array(
  834. 'div' => array('style' => 'display:none;'),
  835. array('input' => array(
  836. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  837. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  838. )),
  839. array('input' => array(
  840. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  841. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  842. )),
  843. '/div'
  844. );
  845. $this->assertTags($result, $expected);
  846. }
  847. /**
  848. * testFormSecurityMultipleSubmitButtons
  849. *
  850. * test form submit generation and ensure that _Token is only created on end()
  851. *
  852. * @return void
  853. */
  854. public function testFormSecurityMultipleSubmitButtons() {
  855. $key = 'testKey';
  856. $this->Form->request['_Token'] = array('key' => $key);
  857. $this->Form->create('Addresses');
  858. $this->Form->input('Address.title');
  859. $this->Form->input('Address.first_name');
  860. $result = $this->Form->submit('Save', array('name' => 'save'));
  861. $expected = array(
  862. 'div' => array('class' => 'submit'),
  863. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  864. '/div',
  865. );
  866. $this->assertTags($result, $expected);
  867. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  868. $expected = array(
  869. 'div' => array('class' => 'submit'),
  870. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  871. '/div',
  872. );
  873. $this->assertTags($result, $expected);
  874. $result = $this->Form->end(null);
  875. $expected = array(
  876. 'div' => array('style' => 'display:none;'),
  877. array('input' => array(
  878. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  879. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  880. )),
  881. array('input' => array(
  882. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  883. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  884. )),
  885. '/div'
  886. );
  887. $this->assertTags($result, $expected);
  888. }
  889. /**
  890. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  891. *
  892. * @return void
  893. */
  894. public function testSecurityButtonNestedNamed() {
  895. $key = 'testKey';
  896. $this->Form->request['_Token'] = array('key' => $key);
  897. $this->Form->create('Addresses');
  898. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  899. $result = $this->Form->unlockField();
  900. $this->assertEquals(array('Address.button'), $result);
  901. }
  902. /**
  903. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  904. *
  905. * @return void
  906. */
  907. public function testSecuritySubmitNestedNamed() {
  908. $key = 'testKey';
  909. $this->Form->request['_Token'] = array('key' => $key);
  910. $this->Form->create('Addresses');
  911. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  912. $result = $this->Form->unlockField();
  913. $this->assertEquals(array('Address.button'), $result);
  914. }
  915. /**
  916. * Test that the correct fields are unlocked for image submits with no names.
  917. *
  918. * @return void
  919. */
  920. public function testSecuritySubmitImageNoName() {
  921. $key = 'testKey';
  922. $this->Form->request['_Token'] = array('key' => $key);
  923. $this->Form->create('User');
  924. $result = $this->Form->submit('save.png');
  925. $expected = array(
  926. 'div' => array('class' => 'submit'),
  927. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  928. '/div'
  929. );
  930. $this->assertTags($result, $expected);
  931. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  932. }
  933. /**
  934. * Test that the correct fields are unlocked for image submits with names.
  935. *
  936. * @return void
  937. */
  938. public function testSecuritySubmitImageName() {
  939. $key = 'testKey';
  940. $this->Form->request['_Token'] = array('key' => $key);
  941. $this->Form->create('User');
  942. $result = $this->Form->submit('save.png', array('name' => 'test'));
  943. $expected = array(
  944. 'div' => array('class' => 'submit'),
  945. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  946. '/div'
  947. );
  948. $this->assertTags($result, $expected);
  949. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  950. }
  951. /**
  952. * testFormSecurityMultipleInputFields method
  953. *
  954. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  955. *
  956. * @return void
  957. */
  958. public function testFormSecurityMultipleInputFields() {
  959. $key = 'testKey';
  960. $this->Form->request['_Token'] = array('key' => $key);
  961. $this->Form->create('Addresses');
  962. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  963. $this->Form->input('Addresses.0.title');
  964. $this->Form->input('Addresses.0.first_name');
  965. $this->Form->input('Addresses.0.last_name');
  966. $this->Form->input('Addresses.0.address');
  967. $this->Form->input('Addresses.0.city');
  968. $this->Form->input('Addresses.0.phone');
  969. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  970. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  971. $this->Form->input('Addresses.1.title');
  972. $this->Form->input('Addresses.1.first_name');
  973. $this->Form->input('Addresses.1.last_name');
  974. $this->Form->input('Addresses.1.address');
  975. $this->Form->input('Addresses.1.city');
  976. $this->Form->input('Addresses.1.phone');
  977. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  978. $result = $this->Form->secure($this->Form->fields);
  979. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  980. $expected = array(
  981. 'div' => array('style' => 'display:none;'),
  982. array('input' => array(
  983. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  984. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  985. )),
  986. array('input' => array(
  987. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  988. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  989. )),
  990. '/div'
  991. );
  992. $this->assertTags($result, $expected);
  993. }
  994. /**
  995. * Test form security with Model.field.0 style inputs
  996. *
  997. * @return void
  998. */
  999. public function testFormSecurityArrayFields() {
  1000. $key = 'testKey';
  1001. $this->Form->request->params['_Token']['key'] = $key;
  1002. $this->Form->create('Address');
  1003. $this->Form->input('Address.primary.1');
  1004. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1005. $this->Form->input('Address.secondary.1.0');
  1006. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1007. }
  1008. /**
  1009. * testFormSecurityMultipleInputDisabledFields method
  1010. *
  1011. * test secure form generation with multiple records and disabled fields.
  1012. *
  1013. * @return void
  1014. */
  1015. public function testFormSecurityMultipleInputDisabledFields() {
  1016. $key = 'testKey';
  1017. $this->Form->request->params['_Token'] = array(
  1018. 'key' => $key,
  1019. 'unlockedFields' => array('first_name', 'address')
  1020. );
  1021. $this->Form->create();
  1022. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1023. $this->Form->input('Addresses.0.title');
  1024. $this->Form->input('Addresses.0.first_name');
  1025. $this->Form->input('Addresses.0.last_name');
  1026. $this->Form->input('Addresses.0.address');
  1027. $this->Form->input('Addresses.0.city');
  1028. $this->Form->input('Addresses.0.phone');
  1029. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1030. $this->Form->input('Addresses.1.title');
  1031. $this->Form->input('Addresses.1.first_name');
  1032. $this->Form->input('Addresses.1.last_name');
  1033. $this->Form->input('Addresses.1.address');
  1034. $this->Form->input('Addresses.1.city');
  1035. $this->Form->input('Addresses.1.phone');
  1036. $result = $this->Form->secure($this->Form->fields);
  1037. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  1038. $expected = array(
  1039. 'div' => array('style' => 'display:none;'),
  1040. array('input' => array(
  1041. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1042. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1043. )),
  1044. array('input' => array(
  1045. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1046. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1047. )),
  1048. '/div'
  1049. );
  1050. $this->assertTags($result, $expected);
  1051. }
  1052. /**
  1053. * testFormSecurityInputDisabledFields method
  1054. *
  1055. * Test single record form with disabled fields.
  1056. *
  1057. * @return void
  1058. */
  1059. public function testFormSecurityInputUnlockedFields() {
  1060. $key = 'testKey';
  1061. $this->Form->request['_Token'] = array(
  1062. 'key' => $key,
  1063. 'unlockedFields' => array('first_name', 'address')
  1064. );
  1065. $this->Form->create();
  1066. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1067. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1068. $this->Form->input('Addresses.title');
  1069. $this->Form->input('Addresses.first_name');
  1070. $this->Form->input('Addresses.last_name');
  1071. $this->Form->input('Addresses.address');
  1072. $this->Form->input('Addresses.city');
  1073. $this->Form->input('Addresses.phone');
  1074. $result = $this->Form->fields;
  1075. $expected = array(
  1076. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1077. 'Addresses.city', 'Addresses.phone'
  1078. );
  1079. $this->assertEquals($expected, $result);
  1080. $result = $this->Form->secure($expected);
  1081. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1082. $expected = array(
  1083. 'div' => array('style' => 'display:none;'),
  1084. array('input' => array(
  1085. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1086. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1087. )),
  1088. array('input' => array(
  1089. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1090. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1091. )),
  1092. '/div'
  1093. );
  1094. $this->assertTags($result, $expected);
  1095. }
  1096. /**
  1097. * test securing inputs with custom name attributes.
  1098. *
  1099. * @return void
  1100. */
  1101. public function testFormSecureWithCustomNameAttribute() {
  1102. $this->Form->request->params['_Token']['key'] = 'testKey';
  1103. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1104. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1105. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1106. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1107. }
  1108. /**
  1109. * testFormSecuredInput method
  1110. *
  1111. * Test generation of entire secure form, assertions made on input() output.
  1112. *
  1113. * @return void
  1114. */
  1115. public function testFormSecuredInput() {
  1116. $this->Form->request['_Token'] = array('key' => 'testKey');
  1117. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1118. $encoding = strtolower(Configure::read('App.encoding'));
  1119. $expected = array(
  1120. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1121. 'div' => array('style' => 'display:none;'),
  1122. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1123. array('input' => array(
  1124. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1125. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1126. )),
  1127. '/div'
  1128. );
  1129. $this->assertTags($result, $expected);
  1130. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1131. $expected = array(
  1132. 'div' => array('class' => 'input text'),
  1133. 'label' => array('for' => 'UserFormPublished'),
  1134. 'Published',
  1135. '/label',
  1136. array('input' => array(
  1137. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1138. 'id' => 'UserFormPublished'
  1139. )),
  1140. '/div'
  1141. );
  1142. $this->assertTags($result, $expected);
  1143. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1144. $expected = array(
  1145. 'div' => array('class' => 'input text'),
  1146. 'label' => array('for' => 'UserFormOther'),
  1147. 'Other',
  1148. '/label',
  1149. array('input' => array(
  1150. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1151. 'id' => 'UserFormOther'
  1152. )),
  1153. '/div'
  1154. );
  1155. $this->assertTags($result, $expected);
  1156. $result = $this->Form->hidden('UserForm.stuff');
  1157. $expected = array('input' => array(
  1158. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1159. 'id' => 'UserFormStuff'
  1160. ));
  1161. $this->assertTags($result, $expected);
  1162. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1163. $expected = array('input' => array(
  1164. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1165. 'value' => '0', 'id' => 'UserFormHidden'
  1166. ));
  1167. $this->assertTags($result, $expected);
  1168. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1169. $expected = array(
  1170. 'div' => array('class' => 'input checkbox'),
  1171. array('input' => array(
  1172. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1173. 'value' => '0', 'id' => 'UserFormSomething_'
  1174. )),
  1175. array('input' => array(
  1176. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1177. 'value' => '1', 'id' => 'UserFormSomething'
  1178. )),
  1179. 'label' => array('for' => 'UserFormSomething'),
  1180. 'Something',
  1181. '/label',
  1182. '/div'
  1183. );
  1184. $this->assertTags($result, $expected);
  1185. $result = $this->Form->fields;
  1186. $expected = array(
  1187. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1188. 'UserForm.hidden' => '0', 'UserForm.something'
  1189. );
  1190. $this->assertEquals($expected, $result);
  1191. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1192. $result = $this->Form->secure($this->Form->fields);
  1193. $expected = array(
  1194. 'div' => array('style' => 'display:none;'),
  1195. array('input' => array(
  1196. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1197. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1198. )),
  1199. array('input' => array(
  1200. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1201. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1202. )),
  1203. '/div'
  1204. );
  1205. $this->assertTags($result, $expected);
  1206. }
  1207. /**
  1208. * Tests that the correct keys are added to the field hash index
  1209. *
  1210. * @return void
  1211. */
  1212. public function testFormSecuredFileInput() {
  1213. $this->Form->request['_Token'] = array('key' => 'testKey');
  1214. $this->assertEquals(array(), $this->Form->fields);
  1215. $result = $this->Form->file('Attachment.file');
  1216. $expected = array(
  1217. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1218. 'Attachment.file.error', 'Attachment.file.size'
  1219. );
  1220. $this->assertEquals($expected, $this->Form->fields);
  1221. }
  1222. /**
  1223. * test that multiple selects keys are added to field hash
  1224. *
  1225. * @return void
  1226. */
  1227. public function testFormSecuredMultipleSelect() {
  1228. $this->Form->request['_Token'] = array('key' => 'testKey');
  1229. $this->assertEquals(array(), $this->Form->fields);
  1230. $options = array('1' => 'one', '2' => 'two');
  1231. $this->Form->select('Model.select', $options);
  1232. $expected = array('Model.select');
  1233. $this->assertEquals($expected, $this->Form->fields);
  1234. $this->Form->fields = array();
  1235. $this->Form->select('Model.select', $options, array('multiple' => true));
  1236. $this->assertEquals($expected, $this->Form->fields);
  1237. }
  1238. /**
  1239. * testFormSecuredRadio method
  1240. *
  1241. * @return void
  1242. */
  1243. public function testFormSecuredRadio() {
  1244. $this->Form->request['_Token'] = array('key' => 'testKey');
  1245. $this->assertEquals(array(), $this->Form->fields);
  1246. $options = array('1' => 'option1', '2' => 'option2');
  1247. $this->Form->radio('Test.test', $options);
  1248. $expected = array('Test.test');
  1249. $this->assertEquals($expected, $this->Form->fields);
  1250. }
  1251. /**
  1252. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1253. * hashing.
  1254. *
  1255. * @return void
  1256. */
  1257. public function testFormSecuredAndDisabled() {
  1258. $this->Form->request['_Token'] = array('key' => 'testKey');
  1259. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1260. $this->Form->text('Model.text', array('disabled' => true));
  1261. $this->Form->password('Model.text', array('disabled' => true));
  1262. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1263. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1264. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1265. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1266. $this->Form->month('Model.month', array('disabled' => true));
  1267. $this->Form->day('Model.day', array('disabled' => true));
  1268. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1269. $this->Form->minute('Model.minute', array('disabled' => true));
  1270. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1271. $expected = array(
  1272. 'Model.radio' => ''
  1273. );
  1274. $this->assertEquals($expected, $this->Form->fields);
  1275. }
  1276. /**
  1277. * testDisableSecurityUsingForm method
  1278. *
  1279. * @return void
  1280. */
  1281. public function testDisableSecurityUsingForm() {
  1282. $this->Form->request['_Token'] = array(
  1283. 'key' => 'testKey',
  1284. 'disabledFields' => array()
  1285. );
  1286. $this->Form->create();
  1287. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1288. $this->Form->input('Addresses.title');
  1289. $this->Form->input('Addresses.first_name', array('secure' => false));
  1290. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1291. $this->Form->input('Addresses.zip', array(
  1292. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1293. ));
  1294. $result = $this->Form->fields;
  1295. $expected = array(
  1296. 'Addresses.id' => '123456', 'Addresses.title',
  1297. );
  1298. $this->assertEquals($expected, $result);
  1299. }
  1300. /**
  1301. * test disableField
  1302. *
  1303. * @return void
  1304. */
  1305. public function testUnlockFieldAddsToList() {
  1306. $this->Form->request['_Token'] = array(
  1307. 'key' => 'testKey',
  1308. 'unlockedFields' => array()
  1309. );
  1310. $this->Form->create('Contact');
  1311. $this->Form->unlockField('Contact.name');
  1312. $this->Form->text('Contact.name');
  1313. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1314. $this->assertEquals(array(), $this->Form->fields);
  1315. }
  1316. /**
  1317. * test unlockField removing from fields array.
  1318. *
  1319. * @return void
  1320. */
  1321. public function testUnlockFieldRemovingFromFields() {
  1322. $this->Form->request['_Token'] = array(
  1323. 'key' => 'testKey',
  1324. 'unlockedFields' => array()
  1325. );
  1326. $this->Form->create('Contact');
  1327. $this->Form->hidden('Contact.id', array('value' => 1));
  1328. $this->Form->text('Contact.name');
  1329. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1330. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1331. $this->Form->unlockField('Contact.name');
  1332. $this->Form->unlockField('Contact.id');
  1333. $this->assertEquals(array(), $this->Form->fields);
  1334. }
  1335. /**
  1336. * testTagIsInvalid method
  1337. *
  1338. * @return void
  1339. */
  1340. public function testTagIsInvalid() {
  1341. $Contact = ClassRegistry::getObject('Contact');
  1342. $Contact->validationErrors[0]['email'] = array('Please provide an email');
  1343. $this->Form->setEntity('Contact.0.email');
  1344. $result = $this->Form->tagIsInvalid();
  1345. $expected = array('Please provide an email');
  1346. $this->assertEquals($expected, $result);
  1347. $this->Form->setEntity('Contact.1.email');
  1348. $result = $this->Form->tagIsInvalid();
  1349. $expected = false;
  1350. $this->assertSame($expected, $result);
  1351. $this->Form->setEntity('Contact.0.name');
  1352. $result = $this->Form->tagIsInvalid();
  1353. $expected = false;
  1354. $this->assertSame($expected, $result);
  1355. }
  1356. /**
  1357. * testPasswordValidation method
  1358. *
  1359. * test validation errors on password input.
  1360. *
  1361. * @return void
  1362. */
  1363. public function testPasswordValidation() {
  1364. $Contact = ClassRegistry::getObject('Contact');
  1365. $Contact->validationErrors['password'] = array('Please provide a password');
  1366. $result = $this->Form->input('Contact.password');
  1367. $expected = array(
  1368. 'div' => array('class' => 'input password error'),
  1369. 'label' => array('for' => 'ContactPassword'),
  1370. 'Password',
  1371. '/label',
  1372. 'input' => array(
  1373. 'type' => 'password', 'name' => 'data[Contact][password]',
  1374. 'id' => 'ContactPassword', 'class' => 'form-error'
  1375. ),
  1376. array('div' => array('class' => 'error-message')),
  1377. 'Please provide a password',
  1378. '/div',
  1379. '/div'
  1380. );
  1381. $this->assertTags($result, $expected);
  1382. }
  1383. /**
  1384. * testEmptyErrorValidation method
  1385. *
  1386. * test validation error div when validation message is an empty string
  1387. *
  1388. * @access public
  1389. * @return void
  1390. */
  1391. public function testEmptyErrorValidation() {
  1392. $this->Form->validationErrors['Contact']['password'] = '';
  1393. $result = $this->Form->input('Contact.password');
  1394. $expected = array(
  1395. 'div' => array('class' => 'input password error'),
  1396. 'label' => array('for' => 'ContactPassword'),
  1397. 'Password',
  1398. '/label',
  1399. 'input' => array(
  1400. 'type' => 'password', 'name' => 'data[Contact][password]',
  1401. 'id' => 'ContactPassword', 'class' => 'form-error'
  1402. ),
  1403. array('div' => array('class' => 'error-message')),
  1404. array(),
  1405. '/div',
  1406. '/div'
  1407. );
  1408. $this->assertTags($result, $expected);
  1409. }
  1410. /**
  1411. * testEmptyInputErrorValidation method
  1412. *
  1413. * test validation error div when validation message is overridden by an empty string when calling input()
  1414. *
  1415. * @access public
  1416. * @return void
  1417. */
  1418. public function testEmptyInputErrorValidation() {
  1419. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1420. $result = $this->Form->input('Contact.password', array('error' => ''));
  1421. $expected = array(
  1422. 'div' => array('class' => 'input password error'),
  1423. 'label' => array('for' => 'ContactPassword'),
  1424. 'Password',
  1425. '/label',
  1426. 'input' => array(
  1427. 'type' => 'password', 'name' => 'data[Contact][password]',
  1428. 'id' => 'ContactPassword', 'class' => 'form-error'
  1429. ),
  1430. array('div' => array('class' => 'error-message')),
  1431. array(),
  1432. '/div',
  1433. '/div'
  1434. );
  1435. $this->assertTags($result, $expected);
  1436. }
  1437. /**
  1438. * testFormValidationAssociated method
  1439. *
  1440. * test display of form errors in conjunction with model::validates.
  1441. *
  1442. * @return void
  1443. */
  1444. public function testFormValidationAssociated() {
  1445. $this->UserForm = ClassRegistry::getObject('UserForm');
  1446. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1447. $data = array(
  1448. 'UserForm' => array('name' => 'user'),
  1449. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1450. );
  1451. $result = $this->UserForm->OpenidUrl->create($data);
  1452. $this->assertFalse(empty($result));
  1453. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1454. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1455. $encoding = strtolower(Configure::read('App.encoding'));
  1456. $expected = array(
  1457. 'form' => array(
  1458. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1459. 'accept-charset' => $encoding
  1460. ),
  1461. 'div' => array('style' => 'display:none;'),
  1462. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1463. '/div'
  1464. );
  1465. $this->assertTags($result, $expected);
  1466. $result = $this->Form->error(
  1467. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1468. );
  1469. $this->assertEquals('Error, not registered', $result);
  1470. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1471. }
  1472. /**
  1473. * testFormValidationAssociatedFirstLevel method
  1474. *
  1475. * test form error display with associated model.
  1476. *
  1477. * @return void
  1478. */
  1479. public function testFormValidationAssociatedFirstLevel() {
  1480. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1481. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1482. $data = array(
  1483. 'ValidateUser' => array('name' => 'mariano'),
  1484. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1485. );
  1486. $result = $this->ValidateUser->create($data);
  1487. $this->assertFalse(empty($result));
  1488. $this->assertFalse($this->ValidateUser->validates());
  1489. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1490. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1491. $encoding = strtolower(Configure::read('App.encoding'));
  1492. $expected = array(
  1493. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1494. 'div' => array('style' => 'display:none;'),
  1495. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1496. '/div'
  1497. );
  1498. $this->assertTags($result, $expected);
  1499. $result = $this->Form->error(
  1500. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1501. );
  1502. $this->assertEquals('Invalid email', $result);
  1503. $result = $this->Form->error(
  1504. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1505. );
  1506. $this->assertEquals('Invalid name', $result);
  1507. $result = $this->Form->error(
  1508. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1509. );
  1510. $this->assertEquals('Invalid city', $result);
  1511. unset($this->ValidateUser->ValidateProfile);
  1512. unset($this->ValidateUser);
  1513. }
  1514. /**
  1515. * testFormValidationAssociatedSecondLevel method
  1516. *
  1517. * test form error display with associated model.
  1518. *
  1519. * @return void
  1520. */
  1521. public function testFormValidationAssociatedSecondLevel() {
  1522. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1523. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1524. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1525. $data = array(
  1526. 'ValidateUser' => array('name' => 'mariano'),
  1527. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1528. 'ValidateItem' => array('name' => 'Item')
  1529. );
  1530. $result = $this->ValidateUser->create($data);
  1531. $this->assertFalse(empty($result));
  1532. $this->assertFalse($this->ValidateUser->validates());
  1533. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1534. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1535. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1536. $encoding = strtolower(Configure::read('App.encoding'));
  1537. $expected = array(
  1538. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1539. 'div' => array('style' => 'display:none;'),
  1540. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1541. '/div'
  1542. );
  1543. $this->assertTags($result, $expected);
  1544. $result = $this->Form->error(
  1545. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1546. );
  1547. $this->assertEquals('Invalid email', $result);
  1548. $result = $this->Form->error(
  1549. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1550. );
  1551. $this->assertEquals('Invalid name', $result);
  1552. $result = $this->Form->error(
  1553. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1554. );
  1555. $result = $this->Form->error(
  1556. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1557. );
  1558. $this->assertEquals('Invalid description', $result);
  1559. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1560. unset($this->ValidateUser->ValidateProfile);
  1561. unset($this->ValidateUser);
  1562. }
  1563. /**
  1564. * testFormValidationMultiRecord method
  1565. *
  1566. * test form error display with multiple records.
  1567. *
  1568. * @return void
  1569. */
  1570. public function testFormValidationMultiRecord() {
  1571. $Contact = ClassRegistry::getObject('Contact');
  1572. $Contact->validationErrors[2] = array(
  1573. 'name' => array('This field cannot be left blank')
  1574. );
  1575. $result = $this->Form->input('Contact.2.name');
  1576. $expected = array(
  1577. 'div' => array('class' => 'input text error'),
  1578. 'label' => array('for' => 'Contact2Name'),
  1579. 'Name',
  1580. '/label',
  1581. 'input' => array(
  1582. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1583. 'class' => 'form-error', 'maxlength' => 255
  1584. ),
  1585. array('div' => array('class' => 'error-message')),
  1586. 'This field cannot be left blank',
  1587. '/div',
  1588. '/div'
  1589. );
  1590. $this->assertTags($result, $expected);
  1591. }
  1592. /**
  1593. * testMultipleInputValidation method
  1594. *
  1595. * test multiple record form validation error display.
  1596. *
  1597. * @return void
  1598. */
  1599. public function testMultipleInputValidation() {
  1600. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1601. $Address->validationErrors[0] = array(
  1602. 'title' => array('This field cannot be empty'),
  1603. 'first_name' => array('This field cannot be empty')
  1604. );
  1605. $Address->validationErrors[1] = array(
  1606. 'last_name' => array('You must have a last name')
  1607. );
  1608. $this->Form->create();
  1609. $result = $this->Form->input('Address.0.title');
  1610. $expected = array(
  1611. 'div' => array('class'),
  1612. 'label' => array('for'),
  1613. 'preg:/[^<]+/',
  1614. '/label',
  1615. 'input' => array(
  1616. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1617. ),
  1618. array('div' => array('class' => 'error-message')),
  1619. 'This field cannot be empty',
  1620. '/div',
  1621. '/div'
  1622. );
  1623. $this->assertTags($result, $expected);
  1624. $result = $this->Form->input('Address.0.first_name');
  1625. $expected = array(
  1626. 'div' => array('class'),
  1627. 'label' => array('for'),
  1628. 'preg:/[^<]+/',
  1629. '/label',
  1630. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1631. array('div' => array('class' => 'error-message')),
  1632. 'This field cannot be empty',
  1633. '/div',
  1634. '/div'
  1635. );
  1636. $this->assertTags($result, $expected);
  1637. $result = $this->Form->input('Address.0.last_name');
  1638. $expected = array(
  1639. 'div' => array('class'),
  1640. 'label' => array('for'),
  1641. 'preg:/[^<]+/',
  1642. '/label',
  1643. 'input' => array('type' => 'text', 'name', 'id'),
  1644. '/div'
  1645. );
  1646. $this->assertTags($result, $expected);
  1647. $result = $this->Form->input('Address.1.last_name');
  1648. $expected = array(
  1649. 'div' => array('class'),
  1650. 'label' => array('for'),
  1651. 'preg:/[^<]+/',
  1652. '/label',
  1653. 'input' => array(
  1654. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1655. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1656. ),
  1657. array('div' => array('class' => 'error-message')),
  1658. 'You must have a last name',
  1659. '/div',
  1660. '/div'
  1661. );
  1662. $this->assertTags($result, $expected);
  1663. }
  1664. /**
  1665. * testInput method
  1666. *
  1667. * Test various incarnations of input().
  1668. *
  1669. * @return void
  1670. */
  1671. public function testInput() {
  1672. $result = $this->Form->input('ValidateUser.balance');
  1673. $expected = array(
  1674. 'div' => array('class'),
  1675. 'label' => array('for'),
  1676. 'Balance',
  1677. '/label',
  1678. 'input' => array('name', 'type' => 'number', 'maxlength' => 8, 'id'),
  1679. '/div',
  1680. );
  1681. $this->assertTags($result, $expected);
  1682. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1683. $expected = array(
  1684. 'div' => array('class' => 'input text'),
  1685. 'label' => array('for' => 'custom'),
  1686. 'Email',
  1687. '/label',
  1688. array('input' => array(
  1689. 'type' => 'text', 'name' => 'data[Contact][email]',
  1690. 'id' => 'custom', 'maxlength' => 255
  1691. )),
  1692. '/div'
  1693. );
  1694. $this->assertTags($result, $expected);
  1695. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1696. $expected = array(
  1697. '<div',
  1698. 'label' => array('for' => 'ContactEmail'),
  1699. 'Email',
  1700. '/label',
  1701. array('input' => array(
  1702. 'type' => 'text', 'name' => 'data[Contact][email]',
  1703. 'id' => 'ContactEmail', 'maxlength' => 255
  1704. )),
  1705. '/div'
  1706. );
  1707. $this->assertTags($result, $expected);
  1708. $result = $this->Form->hidden('Contact.idontexist');
  1709. $expected = array('input' => array(
  1710. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1711. 'id' => 'ContactIdontexist'
  1712. ));
  1713. $this->assertTags($result, $expected);
  1714. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1715. $expected = array(
  1716. 'div' => array('class' => 'input text'),
  1717. 'label' => array('for' => 'ContactEmail'),
  1718. 'Email',
  1719. '/label',
  1720. array('input' => array(
  1721. 'type' => 'text', 'name' => 'data[Contact][email]',
  1722. 'id' => 'ContactEmail'
  1723. )),
  1724. '/div'
  1725. );
  1726. $this->assertTags($result, $expected);
  1727. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1728. $expected = array(
  1729. 'div' => array('class' => 'input text'),
  1730. 'label' => array('for' => 'Contact5Email'),
  1731. 'Email',
  1732. '/label',
  1733. array('input' => array(
  1734. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1735. 'id' => 'Contact5Email'
  1736. )),
  1737. '/div'
  1738. );
  1739. $this->assertTags($result, $expected);
  1740. $result = $this->Form->input('Contact.password');
  1741. $expected = array(
  1742. 'div' => array('class' => 'input password'),
  1743. 'label' => array('for' => 'ContactPassword'),
  1744. 'Password',
  1745. '/label',
  1746. array('input' => array(
  1747. 'type' => 'password', 'name' => 'data[Contact][password]',
  1748. 'id' => 'ContactPassword'
  1749. )),
  1750. '/div'
  1751. );
  1752. $this->assertTags($result, $expected);
  1753. $result = $this->Form->input('Contact.email', array(
  1754. 'type' => 'file', 'class' => 'textbox'
  1755. ));
  1756. $expected = array(
  1757. 'div' => array('class' => 'input file'),
  1758. 'label' => array('for' => 'ContactEmail'),
  1759. 'Email',
  1760. '/label',
  1761. array('input' => array(
  1762. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1763. 'id' => 'ContactEmail'
  1764. )),
  1765. '/div'
  1766. );
  1767. $this->assertTags($result, $expected);
  1768. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1769. $result = $this->Form->input('Contact.phone');
  1770. $expected = array(
  1771. 'div' => array('class' => 'input text'),
  1772. 'label' => array('for' => 'ContactPhone'),
  1773. 'Phone',
  1774. '/label',
  1775. array('input' => array(
  1776. 'type' => 'text', 'name' => 'data[Contact][phone]',
  1777. 'value' => 'Hello &amp; World &gt; weird chars',
  1778. 'id' => 'ContactPhone', 'maxlength' => 255
  1779. )),
  1780. '/div'
  1781. );
  1782. $this->assertTags($result, $expected);
  1783. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1784. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1785. $expected = array(
  1786. 'div' => array('class' => 'input text'),
  1787. 'label' => array('for' => 'myId'),
  1788. 'Field',
  1789. '/label',
  1790. 'input' => array(
  1791. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1792. 'value' => 'My value', 'id' => 'myId'
  1793. ),
  1794. '/div'
  1795. );
  1796. $this->assertTags($result, $expected);
  1797. unset($this->Form->request->data);
  1798. $Contact = ClassRegistry::getObject('Contact');
  1799. $Contact->validationErrors['field'] = array('Badness!');
  1800. $result = $this->Form->input('Contact.field');
  1801. $expected = array(
  1802. 'div' => array('class' => 'input text error'),
  1803. 'label' => array('for' => 'ContactField'),
  1804. 'Field',
  1805. '/label',
  1806. 'input' => array(
  1807. 'type' => 'text', 'name' => 'data[Contact][field]',
  1808. 'id' => 'ContactField', 'class' => 'form-error'
  1809. ),
  1810. array('div' => array('class' => 'error-message')),
  1811. 'Badness!',
  1812. '/div',
  1813. '/div'
  1814. );
  1815. $this->assertTags($result, $expected);
  1816. $result = $this->Form->input('Contact.field', array(
  1817. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1818. ));
  1819. $expected = array(
  1820. 'label' => array('for' => 'ContactField'),
  1821. 'Field',
  1822. '/label',
  1823. 'input' => array(
  1824. 'type' => 'text', 'name' => 'data[Contact][field]',
  1825. 'id' => 'ContactField', 'class' => 'form-error'
  1826. ),
  1827. array('span' => array('class' => 'error-message')),
  1828. 'Badness!',
  1829. '/span'
  1830. );
  1831. $this->assertTags($result, $expected);
  1832. $result = $this->Form->input('Contact.field', array(
  1833. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1834. ));
  1835. $expected = array(
  1836. 'div' => array('class' => 'input text error'),
  1837. 'label' => array('for' => 'ContactField'),
  1838. 'Field',
  1839. '/label',
  1840. 'input' => array(
  1841. 'type' => 'text', 'name' => 'data[Contact][field]',
  1842. 'id' => 'ContactField', 'class' => 'form-error'
  1843. ),
  1844. array('div' => array('class' => 'error')),
  1845. 'Badness!',
  1846. '/div'
  1847. );
  1848. $this->assertTags($result, $expected);
  1849. $result = $this->Form->input('Contact.field', array(
  1850. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1851. ));
  1852. $expected = array(
  1853. 'span' => array('class' => 'input text error'),
  1854. 'label' => array('for' => 'ContactField'),
  1855. 'Field',
  1856. '/label',
  1857. 'input' => array(
  1858. 'type' => 'text', 'name' => 'data[Contact][field]',
  1859. 'id' => 'ContactField', 'class' => 'form-error'
  1860. ),
  1861. 'Badness!',
  1862. '/span'
  1863. );
  1864. $this->assertTags($result, $expected);
  1865. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1866. $expected = array(
  1867. 'div' => array('class' => 'input text error'),
  1868. 'label' => array('for' => 'ContactField'),
  1869. 'Field',
  1870. '/label',
  1871. 'input' => array(
  1872. 'type' => 'text', 'name' => 'data[Contact][field]',
  1873. 'id' => 'ContactField', 'class' => 'form-error'
  1874. ),
  1875. 'A message to you, Rudy',
  1876. array('div' => array('class' => 'error-message')),
  1877. 'Badness!',
  1878. '/div',
  1879. '/div'
  1880. );
  1881. $this->assertTags($result, $expected);
  1882. $this->Form->setEntity(null);
  1883. $this->Form->setEntity('Contact.field');
  1884. $result = $this->Form->input('Contact.field', array(
  1885. 'after' => 'A message to you, Rudy', 'error' => false
  1886. ));
  1887. $expected = array(
  1888. 'div' => array('class' => 'input text'),
  1889. 'label' => array('for' => 'ContactField'),
  1890. 'Field',
  1891. '/label',
  1892. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1893. 'A message to you, Rudy',
  1894. '/div'
  1895. );
  1896. $this->assertTags($result, $expected);
  1897. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1898. $expected = array(
  1899. 'div' => array('class' => 'input text'),
  1900. 'label' => array('for' => 'ObjectField'),
  1901. 'Field',
  1902. '/label',
  1903. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1904. 'A message to you, Rudy',
  1905. '/div'
  1906. );
  1907. $this->assertTags($result, $expected);
  1908. $Contact->validationErrors['field'] = array('minLength');
  1909. $result = $this->Form->input('Contact.field', array(
  1910. 'error' => array(
  1911. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1912. 'maxLength' => 'login too large'
  1913. )
  1914. ));
  1915. $expected = array(
  1916. 'div' => array('class' => 'input text error'),
  1917. 'label' => array('for' => 'ContactField'),
  1918. 'Field',
  1919. '/label',
  1920. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1921. array('div' => array('class' => 'error-message')),
  1922. 'Le login doit contenir au moins 2 caractères',
  1923. '/div',
  1924. '/div'
  1925. );
  1926. $this->assertTags($result, $expected);
  1927. $Contact->validationErrors['field'] = array('maxLength');
  1928. $result = $this->Form->input('Contact.field', array(
  1929. 'error' => array(
  1930. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1931. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1932. 'maxLength' => 'login too large',
  1933. )
  1934. ));
  1935. $expected = array(
  1936. 'div' => array('class' => 'input text error'),
  1937. 'label' => array('for' => 'ContactField'),
  1938. 'Field',
  1939. '/label',
  1940. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1941. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1942. 'login too large',
  1943. '/span',
  1944. '/div'
  1945. );
  1946. $this->assertTags($result, $expected);
  1947. }
  1948. /**
  1949. * Test that inputs with 0 can be created.
  1950. *
  1951. * @return void
  1952. */
  1953. public function testInputZero() {
  1954. $this->Form->create('User');
  1955. $result = $this->Form->input('0');
  1956. $expected = array(
  1957. 'div' => array('class' => 'input text'),
  1958. 'label' => array('for' => 'User0'), '/label',
  1959. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  1960. '/div'
  1961. );
  1962. $this->assertTags($result, $expected);
  1963. }
  1964. /**
  1965. * test input() with checkbox creation
  1966. *
  1967. * @return void
  1968. */
  1969. public function testInputCheckbox() {
  1970. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1971. $expected = array(
  1972. 'div' => array('class' => 'input checkbox'),
  1973. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1974. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1975. '/div'
  1976. );
  1977. $this->assertTags($result, $expected);
  1978. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1979. $expected = array(
  1980. 'div' => array('class' => 'input checkbox'),
  1981. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1982. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1983. '/div'
  1984. );
  1985. $this->assertTags($result, $expected);
  1986. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1987. $expected = array(
  1988. 'div' => array('class' => 'input checkbox'),
  1989. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1990. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1991. '/div'
  1992. );
  1993. $this->assertTags($result, $expected);
  1994. }
  1995. /**
  1996. * test form->input() with time types.
  1997. *
  1998. */
  1999. public function testInputTime() {
  2000. extract($this->dateRegex);
  2001. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2002. $result = explode(':', $result);
  2003. $this->assertRegExp('/option value="23"/', $result[0]);
  2004. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2005. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2006. $result = explode(':', $result);
  2007. $this->assertRegExp('/option value="23"/', $result[0]);
  2008. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2009. $result = $this->Form->input('Model.field', array(
  2010. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2011. ));
  2012. $result = explode(':', $result);
  2013. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2014. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2015. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2016. $result = $this->Form->input('Model.field', array(
  2017. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2018. ));
  2019. $result = explode(':', $result);
  2020. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2021. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2022. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2023. $result = $this->Form->input('prueba', array(
  2024. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  2025. 'maxYear' => date('Y') + 1 ,'interval' => 15
  2026. ));
  2027. $result = explode(':', $result);
  2028. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2029. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2030. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2031. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2032. $result = $this->Form->input('Random.start_time', array(
  2033. 'type' => 'time',
  2034. 'selected' => '18:15'
  2035. ));
  2036. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2037. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2038. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2039. }
  2040. /**
  2041. * Test interval + selected near the hour roll over.
  2042. *
  2043. * @return void
  2044. */
  2045. public function testTimeSelectedWithInterval() {
  2046. $result = $this->Form->input('Model.start_time', array(
  2047. 'type' => 'time',
  2048. 'interval' => 15,
  2049. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2050. ));
  2051. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2052. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2053. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2054. $result = $this->Form->input('Model.start_time', array(
  2055. 'type' => 'time',
  2056. 'interval' => 15,
  2057. 'selected' => '2012-10-23 15:57:00'
  2058. ));
  2059. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2060. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2061. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2062. $result = $this->Form->input('Model.start_time', array(
  2063. 'timeFormat' => 24,
  2064. 'type' => 'time',
  2065. 'interval' => 15,
  2066. 'selected' => '15:57'
  2067. ));
  2068. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2069. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2070. $result = $this->Form->input('Model.start_time', array(
  2071. 'timeFormat' => 24,
  2072. 'type' => 'time',
  2073. 'interval' => 15,
  2074. 'selected' => '23:57'
  2075. ));
  2076. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2077. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2078. $result = $this->Form->input('Model.created', array(
  2079. 'timeFormat' => 24,
  2080. 'type' => 'datetime',
  2081. 'interval' => 15,
  2082. 'selected' => '2012-09-30 23:56'
  2083. ));
  2084. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2085. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2086. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2087. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2088. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2089. }
  2090. /**
  2091. * test form->input() with datetime, date and time types
  2092. *
  2093. * @return void
  2094. */
  2095. public function testInputDatetime() {
  2096. extract($this->dateRegex);
  2097. $result = $this->Form->input('prueba', array(
  2098. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2099. 'maxYear' => date('Y') + 1, 'interval' => 15
  2100. ));
  2101. $result = explode(':', $result);
  2102. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2103. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2104. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2105. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2106. //related to ticket #5013
  2107. $result = $this->Form->input('Contact.date', array(
  2108. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2109. ));
  2110. $this->assertRegExp('/class="customClass"/', $result);
  2111. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2112. $result = $this->Form->input('Contact.date', array(
  2113. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2114. ));
  2115. $this->assertRegExp('/id="customIdDay"/', $result);
  2116. $this->assertRegExp('/id="customIdMonth"/', $result);
  2117. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2118. $result = $this->Form->input('Model.field', array(
  2119. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2120. ));
  2121. $this->assertRegExp('/id="customIDDay"/', $result);
  2122. $this->assertRegExp('/id="customIDHour"/', $result);
  2123. $result = explode('</select><select', $result);
  2124. $result = explode(':', $result[1]);
  2125. $this->assertRegExp('/option value="23"/', $result[0]);
  2126. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2127. $result = $this->Form->input('Model.field', array(
  2128. 'type' => 'datetime', 'timeFormat' => 12
  2129. ));
  2130. $result = explode('</select><select', $result);
  2131. $result = explode(':', $result[1]);
  2132. $this->assertRegExp('/option value="12"/', $result[0]);
  2133. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2134. $this->Form->request->data = array('Contact' => array('created' => null));
  2135. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2136. $expected = array(
  2137. 'div' => array('class' => 'input date'),
  2138. 'label' => array('for' => 'ContactCreatedMonth'),
  2139. 'Created',
  2140. '/label',
  2141. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2142. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2143. $monthsRegex,
  2144. '/select', '-',
  2145. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2146. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2147. $daysRegex,
  2148. '/select', '-',
  2149. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2150. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2151. $yearsRegex,
  2152. '/select',
  2153. '/div'
  2154. );
  2155. $this->assertTags($result, $expected);
  2156. $this->Form->request->data = array('Contact' => array('created' => null));
  2157. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2158. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2159. $this->Form->request->data = array('Contact' => array('created' => null));
  2160. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2161. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2162. $result = $this->Form->input('Contact.created', array(
  2163. 'type' => 'date',
  2164. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2165. 'timeFormat' => 'NONE'
  2166. ));
  2167. $this->assertRegExp('/for\="created-month"/', $result);
  2168. }
  2169. /**
  2170. * Test generating checkboxes in a loop.
  2171. *
  2172. * @return void
  2173. */
  2174. public function testInputCheckboxesInLoop() {
  2175. for ($i = 1; $i < 5; $i++) {
  2176. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2177. $expected = array(
  2178. 'div' => array('class' => 'input checkbox'),
  2179. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2180. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2181. 'label' => array('for' => "Contact{$i}Email"),
  2182. 'Email',
  2183. '/label',
  2184. '/div'
  2185. );
  2186. $this->assertTags($result, $expected);
  2187. }
  2188. }
  2189. /**
  2190. * Test generating checkboxes with disabled elements.
  2191. *
  2192. * @return void
  2193. */
  2194. public function testInputCheckboxWithDisabledElements() {
  2195. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2196. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2197. $expected = array(
  2198. array('div' => array('class' => 'input select')),
  2199. array('label' => array('for' => "ContactMultiple")),
  2200. 'Multiple',
  2201. '/label',
  2202. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2203. array('div' => array('class' => 'checkbox')),
  2204. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2205. array('label' => array('for' => "ContactMultiple1")),
  2206. 'One',
  2207. '/label',
  2208. '/div',
  2209. array('div' => array('class' => 'checkbox')),
  2210. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2211. array('label' => array('for' => "ContactMultiple2")),
  2212. 'Two',
  2213. '/label',
  2214. '/div',
  2215. array('div' => array('class' => 'checkbox')),
  2216. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2217. array('label' => array('for' => "ContactMultiple3")),
  2218. 'Three',
  2219. '/label',
  2220. '/div',
  2221. '/div'
  2222. );
  2223. $this->assertTags($result, $expected);
  2224. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2225. $this->assertTags($result, $expected);
  2226. $disabled = array('2', 3);
  2227. $expected = array(
  2228. array('div' => array('class' => 'input select')),
  2229. array('label' => array('for' => "ContactMultiple")),
  2230. 'Multiple',
  2231. '/label',
  2232. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2233. array('div' => array('class' => 'checkbox')),
  2234. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2235. array('label' => array('for' => "ContactMultiple1")),
  2236. 'One',
  2237. '/label',
  2238. '/div',
  2239. array('div' => array('class' => 'checkbox')),
  2240. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2241. array('label' => array('for' => "ContactMultiple2")),
  2242. 'Two',
  2243. '/label',
  2244. '/div',
  2245. array('div' => array('class' => 'checkbox')),
  2246. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2247. array('label' => array('for' => "ContactMultiple3")),
  2248. 'Three',
  2249. '/label',
  2250. '/div',
  2251. '/div'
  2252. );
  2253. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2254. $this->assertTags($result, $expected);
  2255. }
  2256. /**
  2257. * test input name with leading integer, ensure attributes are generated correctly.
  2258. *
  2259. * @return void
  2260. */
  2261. public function testInputWithLeadingInteger() {
  2262. $result = $this->Form->text('0.Node.title');
  2263. $expected = array(
  2264. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2265. );
  2266. $this->assertTags($result, $expected);
  2267. }
  2268. /**
  2269. * test form->input() with select type inputs.
  2270. *
  2271. * @return void
  2272. */
  2273. public function testInputSelectType() {
  2274. $result = $this->Form->input('email', array(
  2275. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2276. );
  2277. $expected = array(
  2278. 'div' => array('class' => 'input select'),
  2279. 'label' => array('for' => 'email'),
  2280. 'Email',
  2281. '/label',
  2282. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2283. array('option' => array('value' => '')),
  2284. '/option',
  2285. array('option' => array('value' => 'è')),
  2286. 'Firést',
  2287. '/option',
  2288. array('option' => array('value' => 'é')),
  2289. 'Secoènd',
  2290. '/option',
  2291. '/select',
  2292. '/div'
  2293. );
  2294. $this->assertTags($result, $expected);
  2295. $result = $this->Form->input('email', array(
  2296. 'options' => array('First', 'Second'), 'empty' => true)
  2297. );
  2298. $expected = array(
  2299. 'div' => array('class' => 'input select'),
  2300. 'label' => array('for' => 'email'),
  2301. 'Email',
  2302. '/label',
  2303. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2304. array('option' => array('value' => '')),
  2305. '/option',
  2306. array('option' => array('value' => '0')),
  2307. 'First',
  2308. '/option',
  2309. array('option' => array('value' => '1')),
  2310. 'Second',
  2311. '/option',
  2312. '/select',
  2313. '/div'
  2314. );
  2315. $this->assertTags($result, $expected);
  2316. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2317. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2318. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2319. $expected = array(
  2320. 'div' => array('class' => 'input select'),
  2321. 'label' => array('for' => 'ModelUserId'),
  2322. 'User',
  2323. '/label',
  2324. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2325. array('option' => array('value' => '')),
  2326. '/option',
  2327. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2328. 'good',
  2329. '/option',
  2330. array('option' => array('value' => 'other')),
  2331. 'bad',
  2332. '/option',
  2333. '/select',
  2334. '/div'
  2335. );
  2336. $this->assertTags($result, $expected);
  2337. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2338. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2339. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2340. $expected = array(
  2341. 'div' => array('class' => 'input select'),
  2342. 'label' => array('for' => 'ThingUserId'),
  2343. 'User',
  2344. '/label',
  2345. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2346. array('option' => array('value' => '')),
  2347. 'Some Empty',
  2348. '/option',
  2349. array('option' => array('value' => 'value')),
  2350. 'good',
  2351. '/option',
  2352. array('option' => array('value' => 'other')),
  2353. 'bad',
  2354. '/option',
  2355. '/select',
  2356. '/div'
  2357. );
  2358. $this->assertTags($result, $expected);
  2359. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2360. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2361. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2362. $expected = array(
  2363. 'div' => array('class' => 'input select'),
  2364. 'label' => array('for' => 'ThingUserId'),
  2365. 'User',
  2366. '/label',
  2367. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2368. array('option' => array('value' => '')),
  2369. 'Some Empty',
  2370. '/option',
  2371. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2372. 'good',
  2373. '/option',
  2374. array('option' => array('value' => 'other')),
  2375. 'bad',
  2376. '/option',
  2377. '/select',
  2378. '/div'
  2379. );
  2380. $this->assertTags($result, $expected);
  2381. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2382. $this->Form->request->data = array('User' => array('User' => array('value')));
  2383. $result = $this->Form->input('User.User', array('empty' => true));
  2384. $expected = array(
  2385. 'div' => array('class' => 'input select'),
  2386. 'label' => array('for' => 'UserUser'),
  2387. 'User',
  2388. '/label',
  2389. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2390. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2391. array('option' => array('value' => '')),
  2392. '/option',
  2393. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2394. 'good',
  2395. '/option',
  2396. array('option' => array('value' => 'other')),
  2397. 'bad',
  2398. '/option',
  2399. '/select',
  2400. '/div'
  2401. );
  2402. $this->assertTags($result, $expected);
  2403. $this->Form->data = array();
  2404. $result = $this->Form->input('Publisher.id', array(
  2405. 'label' => 'Publisher',
  2406. 'type' => 'select',
  2407. 'multiple' => 'checkbox',
  2408. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2409. ));
  2410. $expected = array(
  2411. array('div' => array('class' => 'input select')),
  2412. array('label' => array('for' => 'PublisherId')),
  2413. 'Publisher',
  2414. '/label',
  2415. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2416. array('div' => array('class' => 'checkbox')),
  2417. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2418. array('label' => array('for' => 'PublisherIdValue1')),
  2419. 'Label 1',
  2420. '/label',
  2421. '/div',
  2422. array('div' => array('class' => 'checkbox')),
  2423. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2424. array('label' => array('for' => 'PublisherIdValue2')),
  2425. 'Label 2',
  2426. '/label',
  2427. '/div',
  2428. '/div'
  2429. );
  2430. $this->assertTags($result, $expected);
  2431. }
  2432. /**
  2433. * test that input() and a non standard primary key makes a hidden input by default.
  2434. *
  2435. * @return void
  2436. */
  2437. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2438. $this->Form->create('User');
  2439. $this->Form->fieldset = array(
  2440. 'User' => array(
  2441. 'fields' => array(
  2442. 'model_id' => array('type' => 'integer')
  2443. ),
  2444. 'validates' => array(),
  2445. 'key' => 'model_id'
  2446. )
  2447. );
  2448. $result = $this->Form->input('model_id');
  2449. $expected = array(
  2450. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2451. );
  2452. $this->assertTags($result, $expected);
  2453. }
  2454. /**
  2455. * test that overriding the magic select type widget is possible
  2456. *
  2457. * @return void
  2458. */
  2459. public function testInputOverridingMagicSelectType() {
  2460. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2461. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2462. $expected = array(
  2463. 'div' => array('class' => 'input text'),
  2464. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2465. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2466. '/div'
  2467. );
  2468. $this->assertTags($result, $expected);
  2469. //Check that magic types still work for plural/singular vars
  2470. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2471. $result = $this->Form->input('Model.type');
  2472. $expected = array(
  2473. 'div' => array('class' => 'input select'),
  2474. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2475. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2476. array('option' => array('value' => 'value')), 'good', '/option',
  2477. array('option' => array('value' => 'other')), 'bad', '/option',
  2478. '/select',
  2479. '/div'
  2480. );
  2481. $this->assertTags($result, $expected);
  2482. }
  2483. /**
  2484. * Test that magic input() selects are created for type=number
  2485. *
  2486. * @return void
  2487. */
  2488. public function testInputMagicSelectForTypeNumber() {
  2489. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2490. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2491. $result = $this->Form->input('ValidateUser.balance');
  2492. $expected = array(
  2493. 'div' => array('class' => 'input select'),
  2494. 'label' => array('for' => 'ValidateUserBalance'),
  2495. 'Balance',
  2496. '/label',
  2497. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2498. array('option' => array('value' => '0')),
  2499. 'nothing',
  2500. '/option',
  2501. array('option' => array('value' => '1', 'selected' => 'selected')),
  2502. 'some',
  2503. '/option',
  2504. array('option' => array('value' => '100')),
  2505. 'a lot',
  2506. '/option',
  2507. '/select',
  2508. '/div'
  2509. );
  2510. $this->assertTags($result, $expected);
  2511. }
  2512. /**
  2513. * Test that magic input() selects can easily be converted into radio types without error.
  2514. *
  2515. * @return void
  2516. */
  2517. public function testInputMagicSelectChangeToRadio() {
  2518. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2519. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2520. $this->assertRegExp('/input type="radio"/', $result);
  2521. }
  2522. /**
  2523. * fields with the same name as the model should work.
  2524. *
  2525. * @return void
  2526. */
  2527. public function testInputWithMatchingFieldAndModelName() {
  2528. $this->Form->create('User');
  2529. $this->Form->fieldset = array(
  2530. 'User' => array(
  2531. 'fields' => array(
  2532. 'User' => array('type' => 'text')
  2533. ),
  2534. 'validates' => array(),
  2535. 'key' => 'id'
  2536. )
  2537. );
  2538. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2539. $result = $this->Form->input('User', array('type' => 'text'));
  2540. $expected = array(
  2541. 'div' => array('class' => 'input text'),
  2542. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2543. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2544. '/div'
  2545. );
  2546. $this->assertTags($result, $expected);
  2547. }
  2548. /**
  2549. * testFormInputs method
  2550. *
  2551. * test correct results from form::inputs().
  2552. *
  2553. * @return void
  2554. */
  2555. public function testFormInputs() {
  2556. $this->Form->create('Contact');
  2557. $result = $this->Form->inputs('The Legend');
  2558. $expected = array(
  2559. '<fieldset',
  2560. '<legend',
  2561. 'The Legend',
  2562. '/legend',
  2563. '*/fieldset',
  2564. );
  2565. $this->assertTags($result, $expected);
  2566. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2567. $expected = array(
  2568. 'fieldset' => array('class' => 'classy-stuff'),
  2569. '<legend',
  2570. 'Field of Dreams',
  2571. '/legend',
  2572. '*/fieldset'
  2573. );
  2574. $this->assertTags($result, $expected);
  2575. $this->Form->create('Contact');
  2576. $this->Form->request['prefix'] = 'admin';
  2577. $this->Form->request['action'] = 'admin_edit';
  2578. $result = $this->Form->inputs();
  2579. $expected = array(
  2580. '<fieldset',
  2581. '<legend',
  2582. 'Edit Contact',
  2583. '/legend',
  2584. '*/fieldset',
  2585. );
  2586. $this->assertTags($result, $expected);
  2587. $this->Form->create('Contact');
  2588. $result = $this->Form->inputs(false);
  2589. $expected = array(
  2590. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2591. array('div' => array('class' => 'input text')),
  2592. '*/div',
  2593. array('div' => array('class' => 'input text')),
  2594. '*/div',
  2595. array('div' => array('class' => 'input text')),
  2596. '*/div',
  2597. array('div' => array('class' => 'input password')),
  2598. '*/div',
  2599. array('div' => array('class' => 'input date')),
  2600. '*/div',
  2601. array('div' => array('class' => 'input date')),
  2602. '*/div',
  2603. array('div' => array('class' => 'input datetime')),
  2604. '*/div',
  2605. array('div' => array('class' => 'input number')),
  2606. '*/div',
  2607. array('div' => array('class' => 'input select')),
  2608. '*/div',
  2609. );
  2610. $this->assertTags($result, $expected);
  2611. $this->Form->create('Contact');
  2612. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2613. $expected = array(
  2614. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2615. array('div' => array('class' => 'input text')),
  2616. '*/div',
  2617. array('div' => array('class' => 'input text')),
  2618. '*/div',
  2619. array('div' => array('class' => 'input text')),
  2620. '*/div',
  2621. array('div' => array('class' => 'input password')),
  2622. '*/div',
  2623. array('div' => array('class' => 'input date')),
  2624. '*/div',
  2625. array('div' => array('class' => 'input date')),
  2626. '*/div',
  2627. array('div' => array('class' => 'input datetime')),
  2628. '*/div',
  2629. array('div' => array('class' => 'input number')),
  2630. '*/div',
  2631. array('div' => array('class' => 'input select')),
  2632. '*/div',
  2633. );
  2634. $this->assertTags($result, $expected);
  2635. $this->Form->create('Contact');
  2636. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2637. $expected = array(
  2638. 'fieldset' => array(),
  2639. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2640. array('div' => array('class' => 'input text')),
  2641. '*/div',
  2642. array('div' => array('class' => 'input text')),
  2643. '*/div',
  2644. array('div' => array('class' => 'input text')),
  2645. '*/div',
  2646. array('div' => array('class' => 'input password')),
  2647. '*/div',
  2648. array('div' => array('class' => 'input date')),
  2649. '*/div',
  2650. array('div' => array('class' => 'input date')),
  2651. '*/div',
  2652. array('div' => array('class' => 'input datetime')),
  2653. '*/div',
  2654. array('div' => array('class' => 'input number')),
  2655. '*/div',
  2656. array('div' => array('class' => 'input select')),
  2657. '*/div',
  2658. '/fieldset'
  2659. );
  2660. $this->assertTags($result, $expected);
  2661. $this->Form->create('Contact');
  2662. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2663. $expected = array(
  2664. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2665. array('div' => array('class' => 'input text')),
  2666. '*/div',
  2667. array('div' => array('class' => 'input text')),
  2668. '*/div',
  2669. array('div' => array('class' => 'input text')),
  2670. '*/div',
  2671. array('div' => array('class' => 'input password')),
  2672. '*/div',
  2673. array('div' => array('class' => 'input date')),
  2674. '*/div',
  2675. array('div' => array('class' => 'input date')),
  2676. '*/div',
  2677. array('div' => array('class' => 'input datetime')),
  2678. '*/div',
  2679. array('div' => array('class' => 'input number')),
  2680. '*/div',
  2681. array('div' => array('class' => 'input select')),
  2682. '*/div',
  2683. );
  2684. $this->assertTags($result, $expected);
  2685. $this->Form->create('Contact');
  2686. $result = $this->Form->inputs('Hello');
  2687. $expected = array(
  2688. 'fieldset' => array(),
  2689. 'legend' => array(),
  2690. 'Hello',
  2691. '/legend',
  2692. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2693. array('div' => array('class' => 'input text')),
  2694. '*/div',
  2695. array('div' => array('class' => 'input text')),
  2696. '*/div',
  2697. array('div' => array('class' => 'input text')),
  2698. '*/div',
  2699. array('div' => array('class' => 'input password')),
  2700. '*/div',
  2701. array('div' => array('class' => 'input date')),
  2702. '*/div',
  2703. array('div' => array('class' => 'input date')),
  2704. '*/div',
  2705. array('div' => array('class' => 'input datetime')),
  2706. '*/div',
  2707. array('div' => array('class' => 'input number')),
  2708. '*/div',
  2709. array('div' => array('class' => 'input select')),
  2710. '*/div',
  2711. '/fieldset'
  2712. );
  2713. $this->assertTags($result, $expected);
  2714. $this->Form->create('Contact');
  2715. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2716. $expected = array(
  2717. 'fieldset' => array(),
  2718. 'legend' => array(),
  2719. 'Hello',
  2720. '/legend',
  2721. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2722. array('div' => array('class' => 'input text')),
  2723. '*/div',
  2724. array('div' => array('class' => 'input text')),
  2725. '*/div',
  2726. array('div' => array('class' => 'input text')),
  2727. '*/div',
  2728. array('div' => array('class' => 'input password')),
  2729. '*/div',
  2730. array('div' => array('class' => 'input date')),
  2731. '*/div',
  2732. array('div' => array('class' => 'input date')),
  2733. '*/div',
  2734. array('div' => array('class' => 'input datetime')),
  2735. '*/div',
  2736. array('div' => array('class' => 'input number')),
  2737. '*/div',
  2738. array('div' => array('class' => 'input select')),
  2739. '*/div',
  2740. '/fieldset'
  2741. );
  2742. $this->assertTags($result, $expected);
  2743. }
  2744. /**
  2745. * testSelectAsCheckbox method
  2746. *
  2747. * test multi-select widget with checkbox formatting.
  2748. *
  2749. * @return void
  2750. */
  2751. public function testSelectAsCheckbox() {
  2752. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2753. $expected = array(
  2754. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2755. array('div' => array('class' => 'checkbox')),
  2756. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2757. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2758. 'first',
  2759. '/label',
  2760. '/div',
  2761. array('div' => array('class' => 'checkbox')),
  2762. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2763. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2764. 'second',
  2765. '/label',
  2766. '/div',
  2767. array('div' => array('class' => 'checkbox')),
  2768. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2769. array('label' => array('for' => 'ModelMultiField2')),
  2770. 'third',
  2771. '/label',
  2772. '/div',
  2773. );
  2774. $this->assertTags($result, $expected);
  2775. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2776. $expected = array(
  2777. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2778. array('div' => array('class' => 'checkbox')),
  2779. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2780. array('label' => array('for' => 'ModelMultiField12')),
  2781. 'half',
  2782. '/label',
  2783. '/div',
  2784. );
  2785. $this->assertTags($result, $expected);
  2786. }
  2787. /**
  2788. * testLabel method
  2789. *
  2790. * test label generation.
  2791. *
  2792. * @return void
  2793. */
  2794. public function testLabel() {
  2795. $this->Form->text('Person.name');
  2796. $result = $this->Form->label();
  2797. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2798. $this->Form->text('Person.name');
  2799. $result = $this->Form->label();
  2800. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2801. $result = $this->Form->label('Person.first_name');
  2802. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2803. $result = $this->Form->label('Person.first_name', 'Your first name');
  2804. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2805. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2806. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2807. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2808. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2809. $result = $this->Form->label('Person.first_name', '');
  2810. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2811. $result = $this->Form->label('Person.2.name', '');
  2812. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2813. }
  2814. /**
  2815. * testTextbox method
  2816. *
  2817. * test textbox element generation
  2818. *
  2819. * @return void
  2820. */
  2821. public function testTextbox() {
  2822. $result = $this->Form->text('Model.field');
  2823. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2824. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2825. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2826. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2827. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2828. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2829. $result = $this->Form->text('Model.text');
  2830. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2831. $Contact = ClassRegistry::getObject('Contact');
  2832. $Contact->validationErrors['text'] = array(true);
  2833. $this->Form->request->data['Contact']['text'] = 'test';
  2834. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2835. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2836. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2837. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2838. $expected = array(
  2839. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2840. );
  2841. $this->assertTags($result, $expected);
  2842. }
  2843. /**
  2844. * testDefaultValue method
  2845. *
  2846. * Test default value setting
  2847. *
  2848. * @return void
  2849. */
  2850. public function testDefaultValue() {
  2851. $this->Form->request->data['Model']['field'] = 'test';
  2852. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2853. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  2854. unset($this->Form->request->data['Model']['field']);
  2855. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2856. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  2857. }
  2858. /**
  2859. * testCheckboxDefaultValue method
  2860. *
  2861. * Test default value setting on checkbox() method
  2862. *
  2863. * @return void
  2864. */
  2865. public function testCheckboxDefaultValue() {
  2866. $this->Form->request->data['Model']['field'] = false;
  2867. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2868. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2869. unset($this->Form->request->data['Model']['field']);
  2870. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2871. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2872. $this->Form->request->data['Model']['field'] = true;
  2873. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2874. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2875. unset($this->Form->request->data['Model']['field']);
  2876. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2877. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2878. }
  2879. /**
  2880. * testError method
  2881. *
  2882. * Test field error generation
  2883. *
  2884. * @return void
  2885. */
  2886. public function testError() {
  2887. $Contact = ClassRegistry::getObject('Contact');
  2888. $Contact->validationErrors['field'] = array(1);
  2889. $result = $this->Form->error('Contact.field');
  2890. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  2891. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2892. $this->assertEquals('Error in field Field', $result);
  2893. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2894. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2895. $this->assertEquals('This field contains invalid input', $result);
  2896. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2897. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  2898. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  2899. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  2900. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  2901. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  2902. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2903. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  2904. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2905. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  2906. $this->assertEquals('<strong>Badness!</strong>', $result);
  2907. $Contact->validationErrors['field'] = array("email");
  2908. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  2909. $expected = array(
  2910. 'div' => array('class' => 'field-error'),
  2911. 'No good!',
  2912. '/div'
  2913. );
  2914. $this->assertTags($result, $expected);
  2915. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  2916. $result = $this->Form->error('Contact.field', array(
  2917. 'notEmpty' => 'Cannot be empty',
  2918. 'email' => 'No good!'
  2919. ));
  2920. $expected = array(
  2921. 'div' => array('class' => 'error-message'),
  2922. 'ul' => array(),
  2923. '<li', 'Cannot be empty', '/li',
  2924. '<li', 'No good!', '/li',
  2925. '<li', 'Something else', '/li',
  2926. '/ul',
  2927. '/div'
  2928. );
  2929. $this->assertTags($result, $expected);
  2930. /** Testing error messages list options **/
  2931. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  2932. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  2933. $expected = array(
  2934. 'div' => array('class' => 'error-message'),
  2935. 'ol' => array(),
  2936. '<li', 'notEmpty', '/li',
  2937. '<li', 'email', '/li',
  2938. '/ol',
  2939. '/div'
  2940. );
  2941. $this->assertTags($result, $expected);
  2942. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  2943. $expected = array(
  2944. 'div' => array('class' => 'error-message'),
  2945. 'ol' => array(),
  2946. '<li', 'notEmpty', '/li',
  2947. '<li', 'email', '/li',
  2948. '/ol',
  2949. '/div'
  2950. );
  2951. $this->assertTags($result, $expected);
  2952. $result = $this->Form->error('Contact.field', null, array(
  2953. 'listOptions' => array(
  2954. 'class' => 'ul-class',
  2955. 'itemOptions' => array(
  2956. 'class' => 'li-class'
  2957. )
  2958. )
  2959. ));
  2960. $expected = array(
  2961. 'div' => array('class' => 'error-message'),
  2962. 'ul' => array('class' => 'ul-class'),
  2963. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  2964. array('li' => array('class' => 'li-class')), 'email', '/li',
  2965. '/ul',
  2966. '/div'
  2967. );
  2968. $this->assertTags($result, $expected);
  2969. }
  2970. /**
  2971. * test error options when using form->input();
  2972. *
  2973. * @return void
  2974. */
  2975. public function testInputErrorEscape() {
  2976. $this->Form->create('ValidateProfile');
  2977. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  2978. $ValidateProfile->validationErrors['city'] = array('required<br>');
  2979. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
  2980. $this->assertRegExp('/required&lt;br&gt;/', $result);
  2981. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => false))));
  2982. $this->assertRegExp('/required<br>/', $result);
  2983. }
  2984. /**
  2985. * testPassword method
  2986. *
  2987. * Test password element generation
  2988. *
  2989. * @return void
  2990. */
  2991. public function testPassword() {
  2992. $Contact = ClassRegistry::getObject('Contact');
  2993. $result = $this->Form->password('Contact.field');
  2994. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  2995. $Contact->validationErrors['passwd'] = 1;
  2996. $this->Form->request->data['Contact']['passwd'] = 'test';
  2997. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  2998. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2999. }
  3000. /**
  3001. * testRadio method
  3002. *
  3003. * Test radio element set generation
  3004. *
  3005. * @return void
  3006. */
  3007. public function testRadio() {
  3008. $result = $this->Form->radio('Model.field', array('option A'));
  3009. $expected = array(
  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. 'label' => array('for' => 'ModelField0'),
  3013. 'option A',
  3014. '/label'
  3015. );
  3016. $this->assertTags($result, $expected);
  3017. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3018. $expected = array(
  3019. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3020. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  3021. 'label' => array('for' => 'ModelField12'),
  3022. 'half',
  3023. '/label'
  3024. );
  3025. $this->assertTags($result, $expected);
  3026. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3027. $expected = array(
  3028. 'fieldset' => array(),
  3029. 'legend' => array(),
  3030. 'Field',
  3031. '/legend',
  3032. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3033. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3034. array('label' => array('for' => 'ModelField0')),
  3035. 'option A',
  3036. '/label',
  3037. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3038. array('label' => array('for' => 'ModelField1')),
  3039. 'option B',
  3040. '/label',
  3041. '/fieldset'
  3042. );
  3043. $this->assertTags($result, $expected);
  3044. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3045. $expected = array(
  3046. 'fieldset' => array(),
  3047. 'legend' => array(),
  3048. 'Field',
  3049. '/legend',
  3050. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3051. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3052. array('label' => array('for' => 'ModelField0')),
  3053. 'option A',
  3054. '/label',
  3055. 'br' => array(),
  3056. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3057. array('label' => array('for' => 'ModelField1')),
  3058. 'option B',
  3059. '/label',
  3060. '/fieldset'
  3061. );
  3062. $this->assertTags($result, $expected);
  3063. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3064. $expected = array(
  3065. 'div' => array('class' => 'input radio'),
  3066. 'fieldset' => array(),
  3067. 'legend' => array(),
  3068. 'Legend title',
  3069. '/legend',
  3070. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3071. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3072. array('label' => array('for' => 'NewsletterSubscribe0')),
  3073. 'Unsubscribe',
  3074. '/label',
  3075. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3076. array('label' => array('for' => 'NewsletterSubscribe1')),
  3077. 'Subscribe',
  3078. '/label',
  3079. '/fieldset',
  3080. '/div'
  3081. );
  3082. $this->assertTags($result, $expected);
  3083. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3084. $expected = array(
  3085. 'div' => array('class' => 'input radio'),
  3086. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3087. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3088. array('label' => array('for' => 'NewsletterSubscribe0')),
  3089. 'Unsubscribe',
  3090. '/label',
  3091. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3092. array('label' => array('for' => 'NewsletterSubscribe1')),
  3093. 'Subscribe',
  3094. '/label',
  3095. '/div'
  3096. );
  3097. $this->assertTags($result, $expected);
  3098. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3099. $expected = array(
  3100. 'div' => array('class' => 'input radio'),
  3101. 'fieldset' => array(),
  3102. 'legend' => array(),
  3103. 'Legend title',
  3104. '/legend',
  3105. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3106. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3107. 'Unsubscribe',
  3108. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3109. 'Subscribe',
  3110. '/fieldset',
  3111. '/div'
  3112. );
  3113. $this->assertTags($result, $expected);
  3114. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3115. $expected = array(
  3116. 'div' => array('class' => 'input radio'),
  3117. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3118. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3119. 'Unsubscribe',
  3120. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3121. 'Subscribe',
  3122. '/div'
  3123. );
  3124. $this->assertTags($result, $expected);
  3125. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'value' => '1', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3126. $expected = array(
  3127. 'div' => array('class' => 'input radio'),
  3128. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3129. 'Unsubscribe',
  3130. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1', 'checked' => 'checked')),
  3131. 'Subscribe',
  3132. '/div'
  3133. );
  3134. $this->assertTags($result, $expected);
  3135. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3136. $expected = array(
  3137. 'fieldset' => array(),
  3138. 'legend' => array(),
  3139. 'Gender',
  3140. '/legend',
  3141. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3142. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3143. array('label' => array('for' => 'EmployeeGenderMale')),
  3144. 'Male',
  3145. '/label',
  3146. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3147. array('label' => array('for' => 'EmployeeGenderFemale')),
  3148. 'Female',
  3149. '/label',
  3150. '/fieldset',
  3151. );
  3152. $this->assertTags($result, $expected);
  3153. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3154. $expected = array(
  3155. 'fieldset' => array(),
  3156. 'legend' => array(),
  3157. 'Gender',
  3158. '/legend',
  3159. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3160. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3161. array('label' => array('for' => 'OfficerGenderMale')),
  3162. 'Male',
  3163. '/label',
  3164. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3165. array('label' => array('for' => 'OfficerGenderFemale')),
  3166. 'Female',
  3167. '/label',
  3168. '/fieldset',
  3169. );
  3170. $this->assertTags($result, $expected);
  3171. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3172. $expected = array(
  3173. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3174. array('input' => array('type' => 'radio', 'name' => 'data[Contact][1][imrequired]', 'value' => '0', 'id' => 'Contact1Imrequired0')),
  3175. 'label' => array('for' => 'Contact1Imrequired0'),
  3176. 'option A',
  3177. '/label'
  3178. );
  3179. $this->assertTags($result, $expected);
  3180. $result = $this->Form->radio('Model.1.field', array('option A'));
  3181. $expected = array(
  3182. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3183. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3184. 'label' => array('for' => 'Model1Field0'),
  3185. 'option A',
  3186. '/label'
  3187. );
  3188. $this->assertTags($result, $expected);
  3189. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3190. $expected = array(
  3191. 'fieldset' => array(),
  3192. 'legend' => array(),
  3193. 'Field',
  3194. '/legend',
  3195. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3196. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3197. array('label' => array('for' => 'ModelField0')),
  3198. 'option A',
  3199. '/label',
  3200. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3201. array('label' => array('for' => 'ModelField1')),
  3202. 'option B',
  3203. '/label',
  3204. '/fieldset'
  3205. );
  3206. $this->assertTags($result, $expected);
  3207. $result = $this->Form->radio(
  3208. 'Model.field',
  3209. array('option A', 'option B'),
  3210. array('between' => 'I am between')
  3211. );
  3212. $expected = array(
  3213. 'fieldset' => array(),
  3214. 'legend' => array(),
  3215. 'Field',
  3216. '/legend',
  3217. 'I am between',
  3218. 'input' => array(
  3219. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3220. 'value' => '', 'id' => 'ModelField_'
  3221. ),
  3222. array('input' => array(
  3223. 'type' => 'radio', 'name' => 'data[Model][field]',
  3224. 'value' => '0', 'id' => 'ModelField0'
  3225. )),
  3226. array('label' => array('for' => 'ModelField0')),
  3227. 'option A',
  3228. '/label',
  3229. array('input' => array(
  3230. 'type' => 'radio', 'name' => 'data[Model][field]',
  3231. 'value' => '1', 'id' => 'ModelField1'
  3232. )),
  3233. array('label' => array('for' => 'ModelField1')),
  3234. 'option B',
  3235. '/label',
  3236. '/fieldset'
  3237. );
  3238. $this->assertTags($result, $expected);
  3239. }
  3240. /**
  3241. * Test that radios with a 0 value are selected under the correct conditions.
  3242. *
  3243. * @return void
  3244. */
  3245. public function testRadioOptionWithZeroValue() {
  3246. $expected = array(
  3247. 'fieldset' => array(),
  3248. 'legend' => array(),
  3249. 'Field',
  3250. '/legend',
  3251. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3252. array('label' => array('for' => 'ModelField1')),
  3253. 'Yes',
  3254. '/label',
  3255. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3256. array('label' => array('for' => 'ModelField0')),
  3257. 'No',
  3258. '/label',
  3259. '/fieldset'
  3260. );
  3261. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3262. $this->assertTags($result, $expected);
  3263. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3264. $this->assertTags($result, $expected);
  3265. $expected = array(
  3266. 'fieldset' => array(),
  3267. 'legend' => array(),
  3268. 'Field',
  3269. '/legend',
  3270. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3271. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3272. array('label' => array('for' => 'ModelField1')),
  3273. 'Yes',
  3274. '/label',
  3275. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3276. array('label' => array('for' => 'ModelField0')),
  3277. 'No',
  3278. '/label',
  3279. '/fieldset'
  3280. );
  3281. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3282. $this->assertTags($result, $expected);
  3283. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3284. $this->assertTags($result, $expected);
  3285. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3286. $this->assertTags($result, $expected);
  3287. }
  3288. /**
  3289. * test disabled radio options
  3290. *
  3291. * @return void
  3292. */
  3293. public function testRadioDisabled() {
  3294. $result = $this->Form->radio(
  3295. 'Model.field',
  3296. array('option A', 'option B'),
  3297. array('disabled' => array('option A'), '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')),
  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' => true, '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->radio(
  3337. 'Model.field',
  3338. array('option A', 'option B'),
  3339. array('disabled' => 'disabled', 'value' => '0')
  3340. );
  3341. $expected = array(
  3342. 'fieldset' => array(),
  3343. 'legend' => array(),
  3344. 'Field',
  3345. '/legend',
  3346. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3347. array('label' => array('for' => 'ModelField0')),
  3348. 'option A',
  3349. '/label',
  3350. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3351. array('label' => array('for' => 'ModelField1')),
  3352. 'option B',
  3353. '/label',
  3354. '/fieldset'
  3355. );
  3356. $this->assertTags($result, $expected);
  3357. }
  3358. /**
  3359. * test disabling the hidden input for radio buttons
  3360. *
  3361. * @return void
  3362. */
  3363. public function testRadioHiddenInputDisabling() {
  3364. $result = $this->Form->input('Model.1.field', array(
  3365. 'type' => 'radio',
  3366. 'options' => array('option A'),
  3367. 'hiddenField' => false
  3368. )
  3369. );
  3370. $expected = array(
  3371. 'div' => array('class' => 'input radio'),
  3372. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3373. 'label' => array('for' => 'Model1Field0'),
  3374. 'option A',
  3375. '/label',
  3376. '/div'
  3377. );
  3378. $this->assertTags($result, $expected);
  3379. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3380. $expected = array(
  3381. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3382. 'label' => array('for' => 'Model1Field0'),
  3383. 'option A',
  3384. '/label'
  3385. );
  3386. $this->assertTags($result, $expected);
  3387. }
  3388. /**
  3389. * test adding an empty option for radio buttons
  3390. *
  3391. * @return void
  3392. */
  3393. public function testRadioAddEmptyOption() {
  3394. $result = $this->Form->input('Model.1.field', array(
  3395. 'type' => 'radio',
  3396. 'options' => array('option A'),
  3397. 'empty' => true,
  3398. 'hiddenField' => false
  3399. ));
  3400. $expected = array(
  3401. 'div' => array('class' => 'input radio'),
  3402. 'fieldset' => array(),
  3403. 'legend' => array(),
  3404. 'Field',
  3405. '/legend',
  3406. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3407. array('label' => array('for' => 'Model1Field')),
  3408. __('empty'),
  3409. '/label',
  3410. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3411. array('label' => array('for' => 'Model1Field0')),
  3412. 'option A',
  3413. '/label',
  3414. '/fieldset',
  3415. '/div'
  3416. );
  3417. $this->assertTags($result, $expected);
  3418. $result = $this->Form->input('Model.1.field', array(
  3419. 'type' => 'radio',
  3420. 'options' => array('option A'),
  3421. 'empty' => 'CustomEmptyLabel',
  3422. 'hiddenField' => false
  3423. ));
  3424. $expected = array(
  3425. 'div' => array('class' => 'input radio'),
  3426. 'fieldset' => array(),
  3427. 'legend' => array(),
  3428. 'Field',
  3429. '/legend',
  3430. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3431. array('label' => array('for' => 'Model1Field')),
  3432. 'CustomEmptyLabel',
  3433. '/label',
  3434. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3435. array('label' => array('for' => 'Model1Field0')),
  3436. 'option A',
  3437. '/label',
  3438. '/fieldset',
  3439. '/div'
  3440. );
  3441. $this->assertTags($result, $expected);
  3442. $result = $this->Form->input('Model.1.field', array(
  3443. 'type' => 'radio',
  3444. 'options' => array('option A'),
  3445. 'empty' => false,
  3446. 'hiddenField' => false
  3447. ));
  3448. $this->assertTextNotContains('"Model1Field"', $result);
  3449. }
  3450. /**
  3451. * testSelect method
  3452. *
  3453. * Test select element generation.
  3454. *
  3455. * @return void
  3456. */
  3457. public function testSelect() {
  3458. $result = $this->Form->select('Model.field', array());
  3459. $expected = array(
  3460. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3461. array('option' => array('value' => '')),
  3462. '/option',
  3463. '/select'
  3464. );
  3465. $this->assertTags($result, $expected);
  3466. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3467. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3468. $expected = array(
  3469. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3470. array('option' => array('value' => '')),
  3471. '/option',
  3472. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3473. 'good',
  3474. '/option',
  3475. array('option' => array('value' => 'other')),
  3476. 'bad',
  3477. '/option',
  3478. '/select'
  3479. );
  3480. $this->assertTags($result, $expected);
  3481. $this->Form->request->data = array();
  3482. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3483. $expected = array(
  3484. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3485. array('option' => array('value' => '')),
  3486. '/option',
  3487. array('option' => array('value' => 'value')),
  3488. 'good',
  3489. '/option',
  3490. array('option' => array('value' => 'other')),
  3491. 'bad',
  3492. '/option',
  3493. '/select'
  3494. );
  3495. $this->assertTags($result, $expected);
  3496. $result = $this->Form->select(
  3497. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3498. array('empty' => false)
  3499. );
  3500. $expected = array(
  3501. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3502. array('option' => array('value' => 'first')),
  3503. 'first &quot;html&quot; &lt;chars&gt;',
  3504. '/option',
  3505. array('option' => array('value' => 'second')),
  3506. 'value',
  3507. '/option',
  3508. '/select'
  3509. );
  3510. $this->assertTags($result, $expected);
  3511. $result = $this->Form->select(
  3512. 'Model.field',
  3513. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3514. array('escape' => false, 'empty' => false)
  3515. );
  3516. $expected = array(
  3517. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3518. array('option' => array('value' => 'first')),
  3519. 'first "html" <chars>',
  3520. '/option',
  3521. array('option' => array('value' => 'second')),
  3522. 'value',
  3523. '/option',
  3524. '/select'
  3525. );
  3526. $this->assertTags($result, $expected);
  3527. $options = array(
  3528. array('value' => 'first', 'name' => 'First'),
  3529. array('value' => 'first', 'name' => 'Another First'),
  3530. );
  3531. $result = $this->Form->select(
  3532. 'Model.field',
  3533. $options,
  3534. array('escape' => false, 'empty' => false)
  3535. );
  3536. $expected = array(
  3537. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3538. array('option' => array('value' => 'first')),
  3539. 'First',
  3540. '/option',
  3541. array('option' => array('value' => 'first')),
  3542. 'Another First',
  3543. '/option',
  3544. '/select'
  3545. );
  3546. $this->assertTags($result, $expected);
  3547. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3548. $result = $this->Form->select(
  3549. 'Model.contact_id',
  3550. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3551. array('escape' => false, 'empty' => 'pick something')
  3552. );
  3553. $expected = array(
  3554. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3555. array('option' => array('value' => '')), 'pick something', '/option',
  3556. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3557. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3558. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3559. '/select'
  3560. );
  3561. $this->assertTags($result, $expected);
  3562. $this->Form->request->data['Model']['field'] = 0;
  3563. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3564. $expected = array(
  3565. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3566. array('option' => array('value' => '')), '/option',
  3567. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3568. array('option' => array('value' => '1')), 'Yes', '/option',
  3569. '/select'
  3570. );
  3571. $this->assertTags($result, $expected);
  3572. }
  3573. /**
  3574. * test that select() with optiongroups listens to the escape param.
  3575. *
  3576. * @return void
  3577. */
  3578. public function testSelectOptionGroupEscaping() {
  3579. $options = array(
  3580. '>< Key' => array(
  3581. 1 => 'One',
  3582. 2 => 'Two'
  3583. )
  3584. );
  3585. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3586. $expected = array(
  3587. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3588. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3589. array('option' => array('value' => '1')), 'One', '/option',
  3590. array('option' => array('value' => '2')), 'Two', '/option',
  3591. '/optgroup',
  3592. '/select'
  3593. );
  3594. $this->assertTags($result, $expected);
  3595. $options = array(
  3596. '>< Key' => array(
  3597. 1 => 'One',
  3598. 2 => 'Two'
  3599. )
  3600. );
  3601. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3602. $expected = array(
  3603. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3604. 'optgroup' => array('label' => '>< Key'),
  3605. array('option' => array('value' => '1')), 'One', '/option',
  3606. array('option' => array('value' => '2')), 'Two', '/option',
  3607. '/optgroup',
  3608. '/select'
  3609. );
  3610. $this->assertTags($result, $expected);
  3611. }
  3612. /**
  3613. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3614. *
  3615. * @return void
  3616. */
  3617. public function testSelectWithNullAttributes() {
  3618. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3619. $expected = array(
  3620. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3621. array('option' => array('value' => '0')),
  3622. 'first',
  3623. '/option',
  3624. array('option' => array('value' => '1')),
  3625. 'second',
  3626. '/option',
  3627. '/select'
  3628. );
  3629. $this->assertTags($result, $expected);
  3630. }
  3631. /**
  3632. * testNestedSelect method
  3633. *
  3634. * test select element generation with optgroups
  3635. *
  3636. * @return void
  3637. */
  3638. public function testNestedSelect() {
  3639. $result = $this->Form->select(
  3640. 'Model.field',
  3641. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3642. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3643. )), array('empty' => false)
  3644. );
  3645. $expected = array(
  3646. 'select' => array('name' => 'data[Model][field]',
  3647. 'id' => 'ModelField'),
  3648. array('option' => array('value' => 1)),
  3649. 'One',
  3650. '/option',
  3651. array('option' => array('value' => 2)),
  3652. 'Two',
  3653. '/option',
  3654. array('optgroup' => array('label' => 'Three')),
  3655. array('option' => array('value' => 4)),
  3656. 'Four',
  3657. '/option',
  3658. array('option' => array('value' => 5)),
  3659. 'Five',
  3660. '/option',
  3661. '/optgroup',
  3662. '/select'
  3663. );
  3664. $this->assertTags($result, $expected);
  3665. $result = $this->Form->select(
  3666. 'Model.field',
  3667. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3668. array('showParents' => true, 'empty' => false)
  3669. );
  3670. $expected = array(
  3671. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3672. array('option' => array('value' => 1)),
  3673. 'One',
  3674. '/option',
  3675. array('option' => array('value' => 2)),
  3676. 'Two',
  3677. '/option',
  3678. array('optgroup' => array('label' => 'Three')),
  3679. array('option' => array('value' => 3)),
  3680. 'Three',
  3681. '/option',
  3682. array('option' => array('value' => 4)),
  3683. 'Four',
  3684. '/option',
  3685. '/optgroup',
  3686. '/select'
  3687. );
  3688. $this->assertTags($result, $expected);
  3689. }
  3690. /**
  3691. * testSelectMultiple method
  3692. *
  3693. * test generation of multiple select elements
  3694. *
  3695. * @return void
  3696. */
  3697. public function testSelectMultiple() {
  3698. $options = array('first', 'second', 'third');
  3699. $result = $this->Form->select(
  3700. 'Model.multi_field', $options, array('multiple' => true)
  3701. );
  3702. $expected = array(
  3703. 'input' => array(
  3704. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3705. ),
  3706. 'select' => array(
  3707. 'name' => 'data[Model][multi_field][]',
  3708. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3709. ),
  3710. array('option' => array('value' => '0')),
  3711. 'first',
  3712. '/option',
  3713. array('option' => array('value' => '1')),
  3714. 'second',
  3715. '/option',
  3716. array('option' => array('value' => '2')),
  3717. 'third',
  3718. '/option',
  3719. '/select'
  3720. );
  3721. $this->assertTags($result, $expected);
  3722. $result = $this->Form->select(
  3723. 'Model.multi_field', $options, array('multiple' => 'multiple')
  3724. );
  3725. $expected = array(
  3726. 'input' => array(
  3727. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3728. ),
  3729. 'select' => array(
  3730. 'name' => 'data[Model][multi_field][]',
  3731. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3732. ),
  3733. array('option' => array('value' => '0')),
  3734. 'first',
  3735. '/option',
  3736. array('option' => array('value' => '1')),
  3737. 'second',
  3738. '/option',
  3739. array('option' => array('value' => '2')),
  3740. 'third',
  3741. '/option',
  3742. '/select'
  3743. );
  3744. $this->assertTags($result, $expected);
  3745. $result = $this->Form->select(
  3746. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  3747. );
  3748. $expected = array(
  3749. 'input' => array(
  3750. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3751. ),
  3752. 'select' => array(
  3753. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  3754. 'multiple' => 'multiple'
  3755. ),
  3756. array('option' => array('value' => '0', 'selected' => 'selected')),
  3757. 'first',
  3758. '/option',
  3759. array('option' => array('value' => '1', 'selected' => 'selected')),
  3760. 'second',
  3761. '/option',
  3762. array('option' => array('value' => '2')),
  3763. 'third',
  3764. '/option',
  3765. '/select'
  3766. );
  3767. $this->assertTags($result, $expected);
  3768. $result = $this->Form->select(
  3769. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  3770. );
  3771. $expected = array(
  3772. 'select' => array(
  3773. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  3774. ),
  3775. array('option' => array('value' => '0', 'selected' => 'selected')),
  3776. 'first',
  3777. '/option',
  3778. array('option' => array('value' => '1', 'selected' => 'selected')),
  3779. 'second',
  3780. '/option',
  3781. array('option' => array('value' => '2')),
  3782. 'third',
  3783. '/option',
  3784. '/select'
  3785. );
  3786. $this->assertTags($result, $expected);
  3787. }
  3788. /**
  3789. * test generation of habtm select boxes.
  3790. *
  3791. * @return void
  3792. */
  3793. public function testHabtmSelectBox() {
  3794. $this->View->viewVars['contactTags'] = array(
  3795. 1 => 'blue',
  3796. 2 => 'red',
  3797. 3 => 'green'
  3798. );
  3799. $this->Form->request->data = array(
  3800. 'Contact' => array(),
  3801. 'ContactTag' => array(
  3802. array(
  3803. 'id' => 1,
  3804. 'name' => 'blue'
  3805. ),
  3806. array(
  3807. 'id' => 3,
  3808. 'name' => 'green'
  3809. )
  3810. )
  3811. );
  3812. $this->Form->create('Contact');
  3813. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3814. $expected = array(
  3815. 'input' => array(
  3816. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3817. ),
  3818. 'select' => array(
  3819. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  3820. 'multiple' => 'multiple'
  3821. ),
  3822. array('option' => array('value' => '1', 'selected' => 'selected')),
  3823. 'blue',
  3824. '/option',
  3825. array('option' => array('value' => '2')),
  3826. 'red',
  3827. '/option',
  3828. array('option' => array('value' => '3', 'selected' => 'selected')),
  3829. 'green',
  3830. '/option',
  3831. '/select'
  3832. );
  3833. $this->assertTags($result, $expected);
  3834. }
  3835. /**
  3836. * test generation of multi select elements in checkbox format
  3837. *
  3838. * @return void
  3839. */
  3840. public function testSelectMultipleCheckboxes() {
  3841. $result = $this->Form->select(
  3842. 'Model.multi_field',
  3843. array('first', 'second', 'third'),
  3844. array('multiple' => 'checkbox')
  3845. );
  3846. $expected = array(
  3847. 'input' => array(
  3848. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3849. ),
  3850. array('div' => array('class' => 'checkbox')),
  3851. array('input' => array(
  3852. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3853. 'value' => '0', 'id' => 'ModelMultiField0'
  3854. )),
  3855. array('label' => array('for' => 'ModelMultiField0')),
  3856. 'first',
  3857. '/label',
  3858. '/div',
  3859. array('div' => array('class' => 'checkbox')),
  3860. array('input' => array(
  3861. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3862. 'value' => '1', 'id' => 'ModelMultiField1'
  3863. )),
  3864. array('label' => array('for' => 'ModelMultiField1')),
  3865. 'second',
  3866. '/label',
  3867. '/div',
  3868. array('div' => array('class' => 'checkbox')),
  3869. array('input' => array(
  3870. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3871. 'value' => '2', 'id' => 'ModelMultiField2'
  3872. )),
  3873. array('label' => array('for' => 'ModelMultiField2')),
  3874. 'third',
  3875. '/label',
  3876. '/div'
  3877. );
  3878. $this->assertTags($result, $expected);
  3879. $result = $this->Form->select(
  3880. 'Model.multi_field',
  3881. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3882. array('multiple' => 'checkbox')
  3883. );
  3884. $expected = array(
  3885. 'input' => array(
  3886. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3887. ),
  3888. array('div' => array('class' => 'checkbox')),
  3889. array('input' => array(
  3890. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3891. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  3892. )),
  3893. array('label' => array('for' => 'ModelMultiFieldA')),
  3894. 'first',
  3895. '/label',
  3896. '/div',
  3897. array('div' => array('class' => 'checkbox')),
  3898. array('input' => array(
  3899. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3900. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  3901. )),
  3902. array('label' => array('for' => 'ModelMultiFieldB')),
  3903. 'second',
  3904. '/label',
  3905. '/div',
  3906. array('div' => array('class' => 'checkbox')),
  3907. array('input' => array(
  3908. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3909. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  3910. )),
  3911. array('label' => array('for' => 'ModelMultiFieldC')),
  3912. 'third',
  3913. '/label',
  3914. '/div'
  3915. );
  3916. $this->assertTags($result, $expected);
  3917. $result = $this->Form->select(
  3918. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  3919. );
  3920. $expected = array(
  3921. 'input' => array(
  3922. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3923. ),
  3924. array('div' => array('class' => 'checkbox')),
  3925. array('input' => array(
  3926. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3927. 'value' => '1', 'id' => 'ModelMultiField1'
  3928. )),
  3929. array('label' => array('for' => 'ModelMultiField1')),
  3930. 'first',
  3931. '/label',
  3932. '/div'
  3933. );
  3934. $this->assertTags($result, $expected);
  3935. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3936. $result = $this->Form->select(
  3937. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3938. );
  3939. $expected = array(
  3940. 'input' => array(
  3941. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3942. ),
  3943. array('div' => array('class' => 'checkbox')),
  3944. array('input' => array(
  3945. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3946. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  3947. )),
  3948. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  3949. 'first',
  3950. '/label',
  3951. '/div',
  3952. array('div' => array('class' => 'checkbox')),
  3953. array('input' => array(
  3954. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3955. 'value' => 'Array', 'id' => 'ModelTagsArray'
  3956. )),
  3957. array('label' => array('for' => 'ModelTagsArray')),
  3958. 'Array',
  3959. '/label',
  3960. '/div'
  3961. );
  3962. $this->assertTags($result, $expected);
  3963. }
  3964. /**
  3965. * test multiple checkboxes with div styles.
  3966. *
  3967. * @return void
  3968. */
  3969. public function testSelectMultipleCheckboxDiv() {
  3970. $result = $this->Form->select(
  3971. 'Model.tags',
  3972. array('first', 'second'),
  3973. array('multiple' => 'checkbox', 'class' => 'my-class')
  3974. );
  3975. $expected = array(
  3976. 'input' => array(
  3977. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3978. ),
  3979. array('div' => array('class' => 'my-class')),
  3980. array('input' => array(
  3981. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3982. 'value' => '0', 'id' => 'ModelTags0'
  3983. )),
  3984. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  3985. '/div',
  3986. array('div' => array('class' => 'my-class')),
  3987. array('input' => array(
  3988. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3989. 'value' => '1', 'id' => 'ModelTags1'
  3990. )),
  3991. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  3992. '/div'
  3993. );
  3994. $this->assertTags($result, $expected);
  3995. $result = $this->Form->input('Model.tags', array(
  3996. 'options' => array('first', 'second'),
  3997. 'multiple' => 'checkbox',
  3998. 'class' => 'my-class',
  3999. 'div' => false,
  4000. 'label' => false
  4001. ));
  4002. $this->assertTags($result, $expected);
  4003. $Contact = ClassRegistry::getObject('Contact');
  4004. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4005. $result = $this->Form->input('Contact.tags', array(
  4006. 'options' => array('one'),
  4007. 'multiple' => 'checkbox',
  4008. 'label' => false,
  4009. 'div' => false
  4010. ));
  4011. $expected = array(
  4012. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4013. array('div' => array('class' => 'checkbox form-error')),
  4014. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4015. array('label' => array('for' => 'ContactTags0')),
  4016. 'one',
  4017. '/label',
  4018. '/div'
  4019. );
  4020. $this->assertTags($result, $expected);
  4021. $result = $this->Form->input('Contact.tags', array(
  4022. 'options' => array('one'),
  4023. 'multiple' => 'checkbox',
  4024. 'class' => 'mycheckbox',
  4025. 'label' => false,
  4026. 'div' => false
  4027. ));
  4028. $expected = array(
  4029. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4030. array('div' => array('class' => 'mycheckbox form-error')),
  4031. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4032. array('label' => array('for' => 'ContactTags0')),
  4033. 'one',
  4034. '/label',
  4035. '/div'
  4036. );
  4037. $this->assertTags($result, $expected);
  4038. }
  4039. /**
  4040. * Checks the security hash array generated for multiple-input checkbox elements
  4041. *
  4042. * @return void
  4043. */
  4044. public function testSelectMultipleCheckboxSecurity() {
  4045. $this->Form->request['_Token'] = array('key' => 'testKey');
  4046. $this->assertEquals(array(), $this->Form->fields);
  4047. $result = $this->Form->select(
  4048. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4049. array('multiple' => 'checkbox')
  4050. );
  4051. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4052. $result = $this->Form->secure($this->Form->fields);
  4053. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4054. $this->assertRegExp('/"' . $key . '"/', $result);
  4055. }
  4056. /**
  4057. * Multiple select elements should always be secured as they always participate
  4058. * in the POST data.
  4059. *
  4060. * @return void
  4061. */
  4062. public function testSelectMultipleSecureWithNoOptions() {
  4063. $this->Form->request['_Token'] = array('key' => 'testkey');
  4064. $this->assertEquals(array(), $this->Form->fields);
  4065. $result = $this->Form->select(
  4066. 'Model.select',
  4067. array(),
  4068. array('multiple' => true)
  4069. );
  4070. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4071. }
  4072. /**
  4073. * When a select box has no options it should not be added to the fields list
  4074. * as it always fail post validation.
  4075. *
  4076. * @return void
  4077. */
  4078. public function testSelectNoSecureWithNoOptions() {
  4079. $this->Form->request['_Token'] = array('key' => 'testkey');
  4080. $this->assertEquals(array(), $this->Form->fields);
  4081. $this->Form->select(
  4082. 'Model.select',
  4083. array()
  4084. );
  4085. $this->assertEquals(array(), $this->Form->fields);
  4086. $this->Form->select(
  4087. 'Model.select',
  4088. array(),
  4089. array('empty' => true)
  4090. );
  4091. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4092. }
  4093. /**
  4094. * testInputMultipleCheckboxes method
  4095. *
  4096. * test input() resulting in multi select elements being generated.
  4097. *
  4098. * @return void
  4099. */
  4100. public function testInputMultipleCheckboxes() {
  4101. $result = $this->Form->input('Model.multi_field', array(
  4102. 'options' => array('first', 'second', 'third'),
  4103. 'multiple' => 'checkbox'
  4104. ));
  4105. $expected = array(
  4106. array('div' => array('class' => 'input select')),
  4107. array('label' => array('for' => 'ModelMultiField')),
  4108. 'Multi Field',
  4109. '/label',
  4110. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4111. array('div' => array('class' => 'checkbox')),
  4112. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4113. array('label' => array('for' => 'ModelMultiField0')),
  4114. 'first',
  4115. '/label',
  4116. '/div',
  4117. array('div' => array('class' => 'checkbox')),
  4118. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4119. array('label' => array('for' => 'ModelMultiField1')),
  4120. 'second',
  4121. '/label',
  4122. '/div',
  4123. array('div' => array('class' => 'checkbox')),
  4124. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4125. array('label' => array('for' => 'ModelMultiField2')),
  4126. 'third',
  4127. '/label',
  4128. '/div',
  4129. '/div'
  4130. );
  4131. $this->assertTags($result, $expected);
  4132. $result = $this->Form->input('Model.multi_field', array(
  4133. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4134. 'multiple' => 'checkbox'
  4135. ));
  4136. $expected = array(
  4137. array('div' => array('class' => 'input select')),
  4138. array('label' => array('for' => 'ModelMultiField')),
  4139. 'Multi Field',
  4140. '/label',
  4141. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4142. array('div' => array('class' => 'checkbox')),
  4143. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4144. array('label' => array('for' => 'ModelMultiFieldA')),
  4145. 'first',
  4146. '/label',
  4147. '/div',
  4148. array('div' => array('class' => 'checkbox')),
  4149. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4150. array('label' => array('for' => 'ModelMultiFieldB')),
  4151. 'second',
  4152. '/label',
  4153. '/div',
  4154. array('div' => array('class' => 'checkbox')),
  4155. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4156. array('label' => array('for' => 'ModelMultiFieldC')),
  4157. 'third',
  4158. '/label',
  4159. '/div',
  4160. '/div'
  4161. );
  4162. $this->assertTags($result, $expected);
  4163. $result = $this->Form->input('Model.multi_field', array(
  4164. 'options' => array('1' => 'first'),
  4165. 'multiple' => 'checkbox',
  4166. 'label' => false,
  4167. 'div' => false
  4168. ));
  4169. $expected = array(
  4170. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4171. array('div' => array('class' => 'checkbox')),
  4172. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4173. array('label' => array('for' => 'ModelMultiField1')),
  4174. 'first',
  4175. '/label',
  4176. '/div'
  4177. );
  4178. $this->assertTags($result, $expected);
  4179. $result = $this->Form->input('Model.multi_field', array(
  4180. 'options' => array('2' => 'second'),
  4181. 'multiple' => 'checkbox',
  4182. 'label' => false,
  4183. 'div' => false
  4184. ));
  4185. $expected = array(
  4186. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4187. array('div' => array('class' => 'checkbox')),
  4188. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4189. array('label' => array('for' => 'ModelMultiField2')),
  4190. 'second',
  4191. '/label',
  4192. '/div'
  4193. );
  4194. $this->assertTags($result, $expected);
  4195. }
  4196. /**
  4197. * testSelectHiddenFieldOmission method
  4198. *
  4199. * test that select() with 'hiddenField' => false omits the hidden field
  4200. *
  4201. * @return void
  4202. */
  4203. public function testSelectHiddenFieldOmission() {
  4204. $result = $this->Form->select('Model.multi_field',
  4205. array('first', 'second'),
  4206. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4207. );
  4208. $expected = array(
  4209. array('div' => array('class' => 'checkbox')),
  4210. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4211. array('label' => array('for' => 'ModelMultiField0')),
  4212. 'first',
  4213. '/label',
  4214. '/div',
  4215. array('div' => array('class' => 'checkbox')),
  4216. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4217. array('label' => array('for' => 'ModelMultiField1')),
  4218. 'second',
  4219. '/label',
  4220. '/div'
  4221. );
  4222. $this->assertTags($result, $expected);
  4223. $result = $this->Form->input('Model.multi_field', array(
  4224. 'options' => array('first', 'second'),
  4225. 'multiple' => 'checkbox',
  4226. 'hiddenField' => false
  4227. ));
  4228. $expected = array(
  4229. array('div' => array('class' => 'input select')),
  4230. array('label' => array('for' => 'ModelMultiField')),
  4231. 'Multi Field',
  4232. '/label',
  4233. array('div' => array('class' => 'checkbox')),
  4234. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4235. array('label' => array('for' => 'ModelMultiField0')),
  4236. 'first',
  4237. '/label',
  4238. '/div',
  4239. array('div' => array('class' => 'checkbox')),
  4240. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4241. array('label' => array('for' => 'ModelMultiField1')),
  4242. 'second',
  4243. '/label',
  4244. '/div',
  4245. '/div'
  4246. );
  4247. $this->assertTags($result, $expected);
  4248. }
  4249. /**
  4250. * test that select() with multiple = checkbox works with overriding name attribute.
  4251. *
  4252. * @return void
  4253. */
  4254. public function testSelectCheckboxMultipleOverrideName() {
  4255. $result = $this->Form->input('category', array(
  4256. 'type' => 'select',
  4257. 'multiple' => 'checkbox',
  4258. 'name' => 'data[fish]',
  4259. 'options' => array('1', '2'),
  4260. 'div' => false,
  4261. 'label' => false,
  4262. ));
  4263. $expected = array(
  4264. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4265. array('div' => array('class' => 'checkbox')),
  4266. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4267. array('label' => array('for' => 'Category0')), '1', '/label',
  4268. '/div',
  4269. array('div' => array('class' => 'checkbox')),
  4270. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4271. array('label' => array('for' => 'Category1')), '2', '/label',
  4272. '/div'
  4273. );
  4274. $this->assertTags($result, $expected);
  4275. }
  4276. /**
  4277. * Test that 'id' overrides all the checkbox id's as well.
  4278. *
  4279. * @return void
  4280. */
  4281. public function testSelectCheckboxMultipleId() {
  4282. $result = $this->Form->select(
  4283. 'Model.multi_field',
  4284. array('first', 'second', 'third'),
  4285. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4286. );
  4287. $expected = array(
  4288. 'input' => array(
  4289. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4290. ),
  4291. array('div' => array('class' => 'checkbox')),
  4292. array('input' => array(
  4293. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4294. 'value' => '0', 'id' => 'CustomId0'
  4295. )),
  4296. array('label' => array('for' => 'CustomId0')),
  4297. 'first',
  4298. '/label',
  4299. '/div',
  4300. array('div' => array('class' => 'checkbox')),
  4301. array('input' => array(
  4302. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4303. 'value' => '1', 'id' => 'CustomId1'
  4304. )),
  4305. array('label' => array('for' => 'CustomId1')),
  4306. 'second',
  4307. '/label',
  4308. '/div',
  4309. array('div' => array('class' => 'checkbox')),
  4310. array('input' => array(
  4311. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4312. 'value' => '2', 'id' => 'CustomId2'
  4313. )),
  4314. array('label' => array('for' => 'CustomId2')),
  4315. 'third',
  4316. '/label',
  4317. '/div'
  4318. );
  4319. $this->assertTags($result, $expected);
  4320. }
  4321. /**
  4322. * testCheckbox method
  4323. *
  4324. * Test generation of checkboxes
  4325. *
  4326. * @return void
  4327. */
  4328. public function testCheckbox() {
  4329. $result = $this->Form->checkbox('Model.field');
  4330. $expected = array(
  4331. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4332. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4333. );
  4334. $this->assertTags($result, $expected);
  4335. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4336. $expected = array(
  4337. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4338. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4339. );
  4340. $this->assertTags($result, $expected);
  4341. $Contact = ClassRegistry::getObject('Contact');
  4342. $Contact->validationErrors['field'] = 1;
  4343. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4344. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4345. $expected = array(
  4346. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4347. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4348. );
  4349. $this->assertTags($result, $expected);
  4350. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4351. $expected = array(
  4352. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4353. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4354. );
  4355. $this->assertTags($result, $expected);
  4356. $this->Form->request->data['Contact']['field'] = '';
  4357. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4358. $expected = array(
  4359. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4360. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4361. );
  4362. $this->assertTags($result, $expected);
  4363. $Contact->validationErrors = array();
  4364. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4365. $expected = array(
  4366. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4367. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4368. );
  4369. $this->assertTags($result, $expected);
  4370. $this->Form->request->data['Contact']['published'] = 1;
  4371. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4372. $expected = array(
  4373. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4374. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4375. );
  4376. $this->assertTags($result, $expected);
  4377. $this->Form->request->data['Contact']['published'] = 0;
  4378. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4379. $expected = array(
  4380. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4381. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4382. );
  4383. $this->assertTags($result, $expected);
  4384. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4385. $expected = array(
  4386. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4387. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4388. );
  4389. $this->assertTags($result, $expected);
  4390. $result = $this->Form->checkbox('CustomField.1.value');
  4391. $expected = array(
  4392. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4393. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4394. );
  4395. $this->assertTags($result, $expected);
  4396. }
  4397. /**
  4398. * test checkbox() with a custom name attribute
  4399. *
  4400. * @return void
  4401. */
  4402. public function testCheckboxCustomNameAttribute() {
  4403. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4404. $expected = array(
  4405. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4406. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4407. );
  4408. $this->assertTags($result, $expected);
  4409. }
  4410. /**
  4411. * test the checked option for checkboxes.
  4412. *
  4413. * @return void
  4414. */
  4415. public function testCheckboxCheckedOption() {
  4416. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4417. $expected = array(
  4418. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4419. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4420. );
  4421. $this->assertTags($result, $expected);
  4422. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4423. $expected = array(
  4424. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4425. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4426. );
  4427. $this->assertTags($result, $expected);
  4428. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4429. $expected = array(
  4430. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4431. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4432. );
  4433. $this->assertTags($result, $expected);
  4434. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4435. $expected = array(
  4436. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4437. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4438. );
  4439. $this->assertTags($result, $expected);
  4440. $this->Form->request->data['Model']['field'] = 1;
  4441. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4442. $expected = array(
  4443. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4444. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4445. );
  4446. $this->assertTags($result, $expected);
  4447. }
  4448. /**
  4449. * Test that disabled attribute works on both the checkbox and hidden input.
  4450. *
  4451. * @return void
  4452. */
  4453. public function testCheckboxDisabling() {
  4454. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4455. $expected = array(
  4456. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4457. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4458. );
  4459. $this->assertTags($result, $expected);
  4460. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4461. $expected = array(
  4462. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4463. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4464. );
  4465. $this->assertTags($result, $expected);
  4466. }
  4467. /**
  4468. * Test that the hidden input for checkboxes can be omitted or set to a
  4469. * specific value.
  4470. *
  4471. * @return void
  4472. */
  4473. public function testCheckboxHiddenField() {
  4474. $result = $this->Form->input('UserForm.something', array(
  4475. 'type' => 'checkbox',
  4476. 'hiddenField' => false
  4477. ));
  4478. $expected = array(
  4479. 'div' => array('class' => 'input checkbox'),
  4480. array('input' => array(
  4481. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4482. 'value' => '1', 'id' => 'UserFormSomething'
  4483. )),
  4484. 'label' => array('for' => 'UserFormSomething'),
  4485. 'Something',
  4486. '/label',
  4487. '/div'
  4488. );
  4489. $this->assertTags($result, $expected);
  4490. $result = $this->Form->input('UserForm.something', array(
  4491. 'type' => 'checkbox',
  4492. 'value' => 'Y',
  4493. 'hiddenField' => 'N',
  4494. ));
  4495. $expected = array(
  4496. 'div' => array('class' => 'input checkbox'),
  4497. array('input' => array(
  4498. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  4499. 'value' => 'N', 'id' => 'UserFormSomething_'
  4500. )),
  4501. array('input' => array(
  4502. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4503. 'value' => 'Y', 'id' => 'UserFormSomething'
  4504. )),
  4505. 'label' => array('for' => 'UserFormSomething'),
  4506. 'Something',
  4507. '/label',
  4508. '/div'
  4509. );
  4510. $this->assertTags($result, $expected);
  4511. }
  4512. /**
  4513. * testDateTime method
  4514. *
  4515. * Test generation of date/time select elements
  4516. *
  4517. * @return void
  4518. */
  4519. public function testDateTime() {
  4520. extract($this->dateRegex);
  4521. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4522. $now = strtotime('now');
  4523. $expected = array(
  4524. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4525. $daysRegex,
  4526. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4527. date('j', $now),
  4528. '/option',
  4529. '*/select',
  4530. '-',
  4531. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4532. $monthsRegex,
  4533. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4534. date('F', $now),
  4535. '/option',
  4536. '*/select',
  4537. '-',
  4538. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4539. $yearsRegex,
  4540. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4541. date('Y', $now),
  4542. '/option',
  4543. '*/select',
  4544. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4545. $hoursRegex,
  4546. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4547. date('g', $now),
  4548. '/option',
  4549. '*/select',
  4550. ':',
  4551. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4552. $minutesRegex,
  4553. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4554. date('i', $now),
  4555. '/option',
  4556. '*/select',
  4557. ' ',
  4558. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4559. $meridianRegex,
  4560. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4561. date('a', $now),
  4562. '/option',
  4563. '*/select'
  4564. );
  4565. $this->assertTags($result, $expected);
  4566. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4567. $expected = array(
  4568. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4569. $daysRegex,
  4570. array('option' => array('value' => '')),
  4571. '/option',
  4572. '*/select',
  4573. '-',
  4574. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4575. $monthsRegex,
  4576. array('option' => array('value' => '')),
  4577. '/option',
  4578. '*/select',
  4579. '-',
  4580. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4581. $yearsRegex,
  4582. array('option' => array('value' => '')),
  4583. '/option',
  4584. '*/select',
  4585. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4586. $hoursRegex,
  4587. array('option' => array('value' => '')),
  4588. '/option',
  4589. '*/select',
  4590. ':',
  4591. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4592. $minutesRegex,
  4593. array('option' => array('value' => '')),
  4594. '/option',
  4595. '*/select',
  4596. ' ',
  4597. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4598. $meridianRegex,
  4599. array('option' => array('value' => '')),
  4600. '/option',
  4601. '*/select'
  4602. );
  4603. $this->assertTags($result, $expected);
  4604. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4605. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  4606. $this->assertTags($result, $expected);
  4607. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4608. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  4609. $this->assertTags($result, $expected);
  4610. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4611. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  4612. $expected = array(
  4613. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4614. $daysRegex,
  4615. array('option' => array('value' => '')),
  4616. '/option',
  4617. '*/select',
  4618. '-',
  4619. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4620. $monthsRegex,
  4621. array('option' => array('value' => '')),
  4622. '/option',
  4623. '*/select',
  4624. '-',
  4625. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4626. $yearsRegex,
  4627. array('option' => array('value' => '')),
  4628. '/option',
  4629. '*/select',
  4630. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4631. $hoursRegex,
  4632. array('option' => array('value' => '')),
  4633. '/option',
  4634. '*/select',
  4635. ':',
  4636. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4637. $minutesRegex,
  4638. array('option' => array('value' => '')),
  4639. '/option',
  4640. array('option' => array('value' => '00')),
  4641. '00',
  4642. '/option',
  4643. array('option' => array('value' => '05')),
  4644. '05',
  4645. '/option',
  4646. array('option' => array('value' => '10')),
  4647. '10',
  4648. '/option',
  4649. '*/select',
  4650. ' ',
  4651. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4652. $meridianRegex,
  4653. array('option' => array('value' => '')),
  4654. '/option',
  4655. '*/select'
  4656. );
  4657. $this->assertTags($result, $expected);
  4658. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4659. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4660. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4661. $this->Form->request->data['Contact']['data'] = null;
  4662. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  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. '*/select',
  4692. ' ',
  4693. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4694. $meridianRegex,
  4695. array('option' => array('value' => '')),
  4696. '/option',
  4697. '*/select'
  4698. );
  4699. $this->assertTags($result, $expected);
  4700. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4701. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  4702. $now = strtotime($this->Form->data['Model']['field']);
  4703. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  4704. $expected = array(
  4705. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4706. $daysRegex,
  4707. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4708. date('j', $now),
  4709. '/option',
  4710. '*/select',
  4711. '-',
  4712. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4713. $monthsRegex,
  4714. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4715. date('F', $now),
  4716. '/option',
  4717. '*/select',
  4718. '-',
  4719. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  4720. $yearsRegex,
  4721. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4722. date('Y', $now),
  4723. '/option',
  4724. '*/select',
  4725. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4726. $hoursRegex,
  4727. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4728. date('g', $now),
  4729. '/option',
  4730. '*/select',
  4731. ':',
  4732. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4733. $minutesRegex,
  4734. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4735. date('i', $now),
  4736. '/option',
  4737. '*/select',
  4738. ' ',
  4739. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  4740. $meridianRegex,
  4741. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4742. date('a', $now),
  4743. '/option',
  4744. '*/select'
  4745. );
  4746. $this->assertTags($result, $expected);
  4747. $selected = strtotime('2008-10-26 12:33:00');
  4748. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  4749. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  4750. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  4751. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  4752. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  4753. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  4754. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  4755. $this->Form->create('Contact');
  4756. $result = $this->Form->input('published');
  4757. $now = strtotime('now');
  4758. $expected = array(
  4759. 'div' => array('class' => 'input date'),
  4760. 'label' => array('for' => 'ContactPublishedMonth'),
  4761. 'Published',
  4762. '/label',
  4763. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  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[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4771. $daysRegex,
  4772. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4773. date('j', $now),
  4774. '/option',
  4775. '*/select',
  4776. '-',
  4777. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4778. $yearsRegex,
  4779. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4780. date('Y', $now),
  4781. '/option',
  4782. '*/select',
  4783. '/div'
  4784. );
  4785. $this->assertTags($result, $expected);
  4786. $result = $this->Form->input('published2', array('type' => 'date'));
  4787. $now = strtotime('now');
  4788. $expected = array(
  4789. 'div' => array('class' => 'input date'),
  4790. 'label' => array('for' => 'ContactPublished2Month'),
  4791. 'Published2',
  4792. '/label',
  4793. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  4794. $monthsRegex,
  4795. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4796. date('F', $now),
  4797. '/option',
  4798. '*/select',
  4799. '-',
  4800. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  4801. $daysRegex,
  4802. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4803. date('j', $now),
  4804. '/option',
  4805. '*/select',
  4806. '-',
  4807. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  4808. $yearsRegex,
  4809. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4810. date('Y', $now),
  4811. '/option',
  4812. '*/select',
  4813. '/div'
  4814. );
  4815. $this->assertTags($result, $expected);
  4816. $this->Form->create('Contact');
  4817. $result = $this->Form->input('published', array('monthNames' => false));
  4818. $now = strtotime('now');
  4819. $expected = array(
  4820. 'div' => array('class' => 'input date'),
  4821. 'label' => array('for' => 'ContactPublishedMonth'),
  4822. 'Published',
  4823. '/label',
  4824. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4825. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4826. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4827. date('m', $now),
  4828. '/option',
  4829. '*/select',
  4830. '-',
  4831. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4832. $daysRegex,
  4833. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4834. date('j', $now),
  4835. '/option',
  4836. '*/select',
  4837. '-',
  4838. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4839. $yearsRegex,
  4840. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4841. date('Y', $now),
  4842. '/option',
  4843. '*/select',
  4844. '/div'
  4845. );
  4846. $this->assertTags($result, $expected);
  4847. $result = $this->Form->input('published', array('type' => 'time'));
  4848. $now = strtotime('now');
  4849. $expected = array(
  4850. 'div' => array('class' => 'input time'),
  4851. 'label' => array('for' => 'ContactPublishedHour'),
  4852. 'Published',
  4853. '/label',
  4854. array('select' => array('name' => 'data[Contact][published][hour]', 'id' => 'ContactPublishedHour')),
  4855. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4856. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4857. date('g', $now),
  4858. '/option',
  4859. '*/select',
  4860. ':',
  4861. );
  4862. $this->assertTags($result, $expected);
  4863. $result = $this->Form->input('published', array(
  4864. 'timeFormat' => 24,
  4865. 'interval' => 5,
  4866. 'selected' => strtotime('2009-09-03 13:37:00'),
  4867. 'type' => 'datetime'
  4868. ));
  4869. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4870. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  4871. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  4872. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  4873. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  4874. $this->assertNoErrors();
  4875. $this->Form->request->data['Contact'] = array(
  4876. 'date' => array(
  4877. 'day' => '',
  4878. 'month' => '',
  4879. 'year' => '',
  4880. 'hour' => '',
  4881. 'min' => '',
  4882. 'meridian' => ''
  4883. )
  4884. );
  4885. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4886. }
  4887. /**
  4888. * test that datetime() and default values work.
  4889. *
  4890. * @return void
  4891. */
  4892. public function testDatetimeWithDefault() {
  4893. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  4894. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4895. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4896. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4897. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  4898. 'default' => '2009-06-01 11:15:30'
  4899. ));
  4900. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4901. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4902. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4903. }
  4904. /**
  4905. * test that bogus non-date time data doesn't cause errors.
  4906. *
  4907. * @return void
  4908. */
  4909. public function testDateTimeWithBogusData() {
  4910. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  4911. $this->assertNotRegExp('/selected="selected">\d/', $result);
  4912. }
  4913. /**
  4914. * testDateTimeEmptyAsArray
  4915. *
  4916. * @return void
  4917. */
  4918. public function testDateTimeEmptyAsArray() {
  4919. $result = $this->Form->dateTime('Contact.date',
  4920. 'DMY',
  4921. '12',
  4922. array(
  4923. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  4924. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  4925. )
  4926. )
  4927. );
  4928. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4929. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4930. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4931. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4932. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4933. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4934. $result = $this->Form->dateTime('Contact.date',
  4935. 'DMY',
  4936. '12',
  4937. array(
  4938. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4939. )
  4940. );
  4941. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4942. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4943. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4944. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  4945. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  4946. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  4947. }
  4948. /**
  4949. * testFormDateTimeMulti method
  4950. *
  4951. * test multiple datetime element generation
  4952. *
  4953. * @return void
  4954. */
  4955. public function testFormDateTimeMulti() {
  4956. extract($this->dateRegex);
  4957. $result = $this->Form->dateTime('Contact.1.updated');
  4958. $expected = array(
  4959. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  4960. $daysRegex,
  4961. array('option' => array('value' => '')),
  4962. '/option',
  4963. '*/select',
  4964. '-',
  4965. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  4966. $monthsRegex,
  4967. array('option' => array('value' => '')),
  4968. '/option',
  4969. '*/select',
  4970. '-',
  4971. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  4972. $yearsRegex,
  4973. array('option' => array('value' => '')),
  4974. '/option',
  4975. '*/select',
  4976. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  4977. $hoursRegex,
  4978. array('option' => array('value' => '')),
  4979. '/option',
  4980. '*/select',
  4981. ':',
  4982. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  4983. $minutesRegex,
  4984. array('option' => array('value' => '')),
  4985. '/option',
  4986. '*/select',
  4987. ' ',
  4988. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  4989. $meridianRegex,
  4990. array('option' => array('value' => '')),
  4991. '/option',
  4992. '*/select'
  4993. );
  4994. $this->assertTags($result, $expected);
  4995. $result = $this->Form->dateTime('Contact.2.updated');
  4996. $expected = array(
  4997. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  4998. $daysRegex,
  4999. array('option' => array('value' => '')),
  5000. '/option',
  5001. '*/select',
  5002. '-',
  5003. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5004. $monthsRegex,
  5005. array('option' => array('value' => '')),
  5006. '/option',
  5007. '*/select',
  5008. '-',
  5009. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5010. $yearsRegex,
  5011. array('option' => array('value' => '')),
  5012. '/option',
  5013. '*/select',
  5014. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5015. $hoursRegex,
  5016. array('option' => array('value' => '')),
  5017. '/option',
  5018. '*/select',
  5019. ':',
  5020. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5021. $minutesRegex,
  5022. array('option' => array('value' => '')),
  5023. '/option',
  5024. '*/select',
  5025. ' ',
  5026. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5027. $meridianRegex,
  5028. array('option' => array('value' => '')),
  5029. '/option',
  5030. '*/select'
  5031. );
  5032. $this->assertTags($result, $expected);
  5033. }
  5034. /**
  5035. * When changing the date format, the label should always focus the first select box when
  5036. * clicked.
  5037. *
  5038. * @return void
  5039. */
  5040. public function testDateTimeLabelIdMatchesFirstInput() {
  5041. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5042. $this->assertContains('label for="ModelDateMonth"', $result);
  5043. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5044. $this->assertContains('label for="ModelDateDay"', $result);
  5045. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5046. $this->assertContains('label for="ModelDateYear"', $result);
  5047. }
  5048. /**
  5049. * testMonth method
  5050. *
  5051. * @return void
  5052. */
  5053. public function testMonth() {
  5054. $result = $this->Form->month('Model.field');
  5055. $expected = array(
  5056. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5057. array('option' => array('value' => '')),
  5058. '/option',
  5059. array('option' => array('value' => '01')),
  5060. date('F', strtotime('2008-01-01 00:00:00')),
  5061. '/option',
  5062. array('option' => array('value' => '02')),
  5063. date('F', strtotime('2008-02-01 00:00:00')),
  5064. '/option',
  5065. '*/select',
  5066. );
  5067. $this->assertTags($result, $expected);
  5068. $result = $this->Form->month('Model.field', array('empty' => true));
  5069. $expected = array(
  5070. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5071. array('option' => array('value' => '')),
  5072. '/option',
  5073. array('option' => array('value' => '01')),
  5074. date('F', strtotime('2008-01-01 00:00:00')),
  5075. '/option',
  5076. array('option' => array('value' => '02')),
  5077. date('F', strtotime('2008-02-01 00:00:00')),
  5078. '/option',
  5079. '*/select',
  5080. );
  5081. $this->assertTags($result, $expected);
  5082. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5083. $expected = array(
  5084. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5085. array('option' => array('value' => '')),
  5086. '/option',
  5087. array('option' => array('value' => '01')),
  5088. '01',
  5089. '/option',
  5090. array('option' => array('value' => '02')),
  5091. '02',
  5092. '/option',
  5093. '*/select',
  5094. );
  5095. $this->assertTags($result, $expected);
  5096. $monthNames = array(
  5097. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5098. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5099. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5100. $expected = array(
  5101. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5102. array('option' => array('value' => '')),
  5103. '/option',
  5104. array('option' => array('value' => '01')),
  5105. 'Jan',
  5106. '/option',
  5107. array('option' => array('value' => '02')),
  5108. 'Feb',
  5109. '/option',
  5110. '*/select',
  5111. );
  5112. $this->assertTags($result, $expected);
  5113. }
  5114. /**
  5115. * testDay method
  5116. *
  5117. * @return void
  5118. */
  5119. public function testDay() {
  5120. extract($this->dateRegex);
  5121. $result = $this->Form->day('Model.field', array('value' => false));
  5122. $expected = array(
  5123. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5124. array('option' => array('value' => '')),
  5125. '/option',
  5126. array('option' => array('value' => '01')),
  5127. '1',
  5128. '/option',
  5129. array('option' => array('value' => '02')),
  5130. '2',
  5131. '/option',
  5132. $daysRegex,
  5133. '/select',
  5134. );
  5135. $this->assertTags($result, $expected);
  5136. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5137. $result = $this->Form->day('Model.field');
  5138. $expected = array(
  5139. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5140. array('option' => array('value' => '')),
  5141. '/option',
  5142. array('option' => array('value' => '01')),
  5143. '1',
  5144. '/option',
  5145. array('option' => array('value' => '02')),
  5146. '2',
  5147. '/option',
  5148. $daysRegex,
  5149. array('option' => array('value' => '10', 'selected' => 'selected')),
  5150. '10',
  5151. '/option',
  5152. $daysRegex,
  5153. '/select',
  5154. );
  5155. $this->assertTags($result, $expected);
  5156. $this->Form->request->data['Model']['field'] = '';
  5157. $result = $this->Form->day('Model.field', array('value' => '10'));
  5158. $expected = array(
  5159. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5160. array('option' => array('value' => '')),
  5161. '/option',
  5162. array('option' => array('value' => '01')),
  5163. '1',
  5164. '/option',
  5165. array('option' => array('value' => '02')),
  5166. '2',
  5167. '/option',
  5168. $daysRegex,
  5169. array('option' => array('value' => '10', 'selected' => 'selected')),
  5170. '10',
  5171. '/option',
  5172. $daysRegex,
  5173. '/select',
  5174. );
  5175. $this->assertTags($result, $expected);
  5176. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5177. $result = $this->Form->day('Model.field', array('value' => true));
  5178. $expected = array(
  5179. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5180. array('option' => array('value' => '')),
  5181. '/option',
  5182. array('option' => array('value' => '01')),
  5183. '1',
  5184. '/option',
  5185. array('option' => array('value' => '02')),
  5186. '2',
  5187. '/option',
  5188. $daysRegex,
  5189. array('option' => array('value' => '10', 'selected' => 'selected')),
  5190. '10',
  5191. '/option',
  5192. $daysRegex,
  5193. '/select',
  5194. );
  5195. $this->assertTags($result, $expected);
  5196. }
  5197. /**
  5198. * testMinute method
  5199. *
  5200. * @return void
  5201. */
  5202. public function testMinute() {
  5203. extract($this->dateRegex);
  5204. $result = $this->Form->minute('Model.field');
  5205. $expected = array(
  5206. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5207. array('option' => array('value' => '')),
  5208. '/option',
  5209. array('option' => array('value' => '00')),
  5210. '00',
  5211. '/option',
  5212. array('option' => array('value' => '01')),
  5213. '01',
  5214. '/option',
  5215. array('option' => array('value' => '02')),
  5216. '02',
  5217. '/option',
  5218. $minutesRegex,
  5219. '/select',
  5220. );
  5221. $this->assertTags($result, $expected);
  5222. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5223. $result = $this->Form->minute('Model.field');
  5224. $expected = array(
  5225. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5226. array('option' => array('value' => '')),
  5227. '/option',
  5228. array('option' => array('value' => '00')),
  5229. '00',
  5230. '/option',
  5231. array('option' => array('value' => '01')),
  5232. '01',
  5233. '/option',
  5234. array('option' => array('value' => '02')),
  5235. '02',
  5236. '/option',
  5237. $minutesRegex,
  5238. array('option' => array('value' => '12', 'selected' => 'selected')),
  5239. '12',
  5240. '/option',
  5241. $minutesRegex,
  5242. '/select',
  5243. );
  5244. $this->assertTags($result, $expected);
  5245. $this->Form->request->data['Model']['field'] = '';
  5246. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5247. $expected = array(
  5248. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5249. array('option' => array('value' => '')),
  5250. '/option',
  5251. array('option' => array('value' => '00')),
  5252. '00',
  5253. '/option',
  5254. array('option' => array('value' => '05')),
  5255. '05',
  5256. '/option',
  5257. array('option' => array('value' => '10')),
  5258. '10',
  5259. '/option',
  5260. $minutesRegex,
  5261. '/select',
  5262. );
  5263. $this->assertTags($result, $expected);
  5264. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5265. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5266. $expected = array(
  5267. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5268. array('option' => array('value' => '')),
  5269. '/option',
  5270. array('option' => array('value' => '00')),
  5271. '00',
  5272. '/option',
  5273. array('option' => array('value' => '05')),
  5274. '05',
  5275. '/option',
  5276. array('option' => array('value' => '10', 'selected' => 'selected')),
  5277. '10',
  5278. '/option',
  5279. $minutesRegex,
  5280. '/select',
  5281. );
  5282. $this->assertTags($result, $expected);
  5283. }
  5284. /**
  5285. * testHour method
  5286. *
  5287. * @return void
  5288. */
  5289. public function testHour() {
  5290. extract($this->dateRegex);
  5291. $result = $this->Form->hour('Model.field', false);
  5292. $expected = array(
  5293. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5294. array('option' => array('value' => '')),
  5295. '/option',
  5296. array('option' => array('value' => '01')),
  5297. '1',
  5298. '/option',
  5299. array('option' => array('value' => '02')),
  5300. '2',
  5301. '/option',
  5302. $hoursRegex,
  5303. '/select',
  5304. );
  5305. $this->assertTags($result, $expected);
  5306. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5307. $result = $this->Form->hour('Model.field', false);
  5308. $expected = array(
  5309. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5310. array('option' => array('value' => '')),
  5311. '/option',
  5312. array('option' => array('value' => '01')),
  5313. '1',
  5314. '/option',
  5315. array('option' => array('value' => '02')),
  5316. '2',
  5317. '/option',
  5318. $hoursRegex,
  5319. array('option' => array('value' => '12', 'selected' => 'selected')),
  5320. '12',
  5321. '/option',
  5322. '/select',
  5323. );
  5324. $this->assertTags($result, $expected);
  5325. $this->Form->request->data['Model']['field'] = '';
  5326. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5327. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5328. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  5329. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5330. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5331. $result = $this->Form->hour('Model.field', true);
  5332. $expected = array(
  5333. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5334. array('option' => array('value' => '')),
  5335. '/option',
  5336. array('option' => array('value' => '00', 'selected' => 'selected')),
  5337. '0',
  5338. '/option',
  5339. array('option' => array('value' => '01')),
  5340. '1',
  5341. '/option',
  5342. array('option' => array('value' => '02')),
  5343. '2',
  5344. '/option',
  5345. $hoursRegex,
  5346. '/select',
  5347. );
  5348. $this->assertTags($result, $expected);
  5349. unset($this->Form->request->data['Model']['field']);
  5350. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5351. $thisHour = date('H');
  5352. $optValue = date('G');
  5353. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5354. }
  5355. /**
  5356. * testYear method
  5357. *
  5358. * @return void
  5359. */
  5360. public function testYear() {
  5361. $result = $this->Form->year('Model.field', 2006, 2007);
  5362. $expected = array(
  5363. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5364. array('option' => array('value' => '')),
  5365. '/option',
  5366. array('option' => array('value' => '2007')),
  5367. '2007',
  5368. '/option',
  5369. array('option' => array('value' => '2006')),
  5370. '2006',
  5371. '/option',
  5372. '/select',
  5373. );
  5374. $this->assertTags($result, $expected);
  5375. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5376. $expected = array(
  5377. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5378. array('option' => array('value' => '')),
  5379. '/option',
  5380. array('option' => array('value' => '2006')),
  5381. '2006',
  5382. '/option',
  5383. array('option' => array('value' => '2007')),
  5384. '2007',
  5385. '/option',
  5386. '/select',
  5387. );
  5388. $this->assertTags($result, $expected);
  5389. $this->request->data['Contact']['published'] = '';
  5390. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5391. $expected = array(
  5392. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5393. array('option' => array('value' => '')),
  5394. '/option',
  5395. array('option' => array('value' => '2007')),
  5396. '2007',
  5397. '/option',
  5398. array('option' => array('value' => '2006')),
  5399. '2006',
  5400. '/option',
  5401. '/select',
  5402. );
  5403. $this->assertTags($result, $expected);
  5404. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5405. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5406. $expected = array(
  5407. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5408. array('option' => array('value' => '2007')),
  5409. '2007',
  5410. '/option',
  5411. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5412. '2006',
  5413. '/option',
  5414. '/select',
  5415. );
  5416. $this->assertTags($result, $expected);
  5417. $this->Form->request->data['Contact']['published'] = '';
  5418. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5419. $expected = array(
  5420. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5421. array('option' => array('value' => '')),
  5422. '/option',
  5423. array('option' => array('value' => '2007')),
  5424. '2007',
  5425. '/option',
  5426. array('option' => array('value' => '2006')),
  5427. '2006',
  5428. '/option',
  5429. '/select',
  5430. );
  5431. $this->assertTags($result, $expected);
  5432. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5433. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5434. $expected = array(
  5435. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5436. array('option' => array('value' => '2007')),
  5437. '2007',
  5438. '/option',
  5439. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5440. '2006',
  5441. '/option',
  5442. '/select',
  5443. );
  5444. $this->assertTags($result, $expected);
  5445. $this->Form->request->data['Contact']['published'] = '';
  5446. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5447. $expected = array(
  5448. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5449. array('option' => array('value' => '')),
  5450. '/option',
  5451. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5452. '2007',
  5453. '/option',
  5454. array('option' => array('value' => '2006')),
  5455. '2006',
  5456. '/option',
  5457. '/select',
  5458. );
  5459. $this->assertTags($result, $expected);
  5460. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5461. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5462. $expected = array(
  5463. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5464. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5465. '2007',
  5466. '/option',
  5467. array('option' => array('value' => '2006')),
  5468. '2006',
  5469. '/option',
  5470. '/select',
  5471. );
  5472. $this->assertTags($result, $expected);
  5473. $this->Form->request->data['Contact']['published'] = '';
  5474. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5475. $expected = array(
  5476. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5477. array('option' => array('value' => '2008')),
  5478. '2008',
  5479. '/option',
  5480. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5481. '2007',
  5482. '/option',
  5483. array('option' => array('value' => '2006')),
  5484. '2006',
  5485. '/option',
  5486. '/select',
  5487. );
  5488. $this->assertTags($result, $expected);
  5489. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5490. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5491. $expected = array(
  5492. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5493. array('option' => array('value' => '2008')),
  5494. '2008',
  5495. '/option',
  5496. array('option' => array('value' => '2007')),
  5497. '2007',
  5498. '/option',
  5499. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5500. '2006',
  5501. '/option',
  5502. '/select',
  5503. );
  5504. $this->assertTags($result, $expected);
  5505. $this->Form->request->data = array();
  5506. $this->Form->create('Contact');
  5507. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5508. $expected = array(
  5509. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5510. array('option' => array('value' => '2008')),
  5511. '2008',
  5512. '/option',
  5513. array('option' => array('value' => '2007')),
  5514. '2007',
  5515. '/option',
  5516. array('option' => array('value' => '2006')),
  5517. '2006',
  5518. '/option',
  5519. '/select',
  5520. );
  5521. $this->assertTags($result, $expected);
  5522. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  5523. $this->assertContains('data[Contact][published][year]', $result);
  5524. }
  5525. /**
  5526. * testTextArea method
  5527. *
  5528. * @return void
  5529. */
  5530. public function testTextArea() {
  5531. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  5532. $result = $this->Form->textarea('Model.field');
  5533. $expected = array(
  5534. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5535. 'some test data',
  5536. '/textarea',
  5537. );
  5538. $this->assertTags($result, $expected);
  5539. $result = $this->Form->textarea('Model.tmp');
  5540. $expected = array(
  5541. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  5542. '/textarea',
  5543. );
  5544. $this->assertTags($result, $expected);
  5545. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5546. $result = $this->Form->textarea('Model.field');
  5547. $expected = array(
  5548. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5549. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5550. '/textarea',
  5551. );
  5552. $this->assertTags($result, $expected);
  5553. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5554. $result = $this->Form->textarea('Model.field', array('escape' => false));
  5555. $expected = array(
  5556. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5557. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5558. '/textarea',
  5559. );
  5560. $this->assertTags($result, $expected);
  5561. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  5562. $result = $this->Form->textarea('Model.0.OtherModel.field');
  5563. $expected = array(
  5564. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  5565. '/textarea'
  5566. );
  5567. $this->assertTags($result, $expected);
  5568. }
  5569. /**
  5570. * testTextAreaWithStupidCharacters method
  5571. *
  5572. * test text area with non-ascii characters
  5573. *
  5574. * @return void
  5575. */
  5576. public function testTextAreaWithStupidCharacters() {
  5577. $this->loadFixtures('Post');
  5578. $result = $this->Form->input('Post.content', array(
  5579. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  5580. ));
  5581. $expected = array(
  5582. 'div' => array('class' => 'input text'),
  5583. 'label' => array('for' => 'PostContent'),
  5584. 'Current Text',
  5585. '/label',
  5586. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  5587. 'GREAT®',
  5588. '/textarea',
  5589. '/div'
  5590. );
  5591. $this->assertTags($result, $expected);
  5592. }
  5593. /**
  5594. * testHiddenField method
  5595. *
  5596. * @return void
  5597. */
  5598. public function testHiddenField() {
  5599. $Contact = ClassRegistry::getObject('Contact');
  5600. $Contact->validationErrors['field'] = 1;
  5601. $this->Form->request->data['Contact']['field'] = 'test';
  5602. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  5603. $this->assertTags($result, array(
  5604. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  5605. );
  5606. }
  5607. /**
  5608. * testFileUploadField method
  5609. *
  5610. * @return void
  5611. */
  5612. public function testFileUploadField() {
  5613. $result = $this->Form->file('Model.upload');
  5614. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5615. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  5616. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  5617. $expected = array(
  5618. 'div' => array('class' => 'input file'),
  5619. 'label' => array('for' => 'ModelUpload'),
  5620. 'Upload',
  5621. '/label',
  5622. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  5623. '/div'
  5624. );
  5625. $this->assertTags($result, $expected);
  5626. }
  5627. /**
  5628. * test File upload input on a model not used in create();
  5629. *
  5630. * @return void
  5631. */
  5632. public function testFileUploadOnOtherModel() {
  5633. $this->Form->create('ValidateUser', array('type' => 'file'));
  5634. $result = $this->Form->file('ValidateProfile.city');
  5635. $expected = array(
  5636. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  5637. );
  5638. $this->assertTags($result, $expected);
  5639. }
  5640. /**
  5641. * testButton method
  5642. *
  5643. * @return void
  5644. */
  5645. public function testButton() {
  5646. $result = $this->Form->button('Hi');
  5647. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5648. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5649. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5650. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5651. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5652. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5653. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5654. $result = $this->Form->button('No type', array('type' => false));
  5655. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5656. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  5657. $this->assertNotRegExp('/\&039/', $result);
  5658. }
  5659. /**
  5660. * Test that button() makes unlocked fields by default.
  5661. *
  5662. * @return void
  5663. */
  5664. public function testButtonUnlockedByDefault() {
  5665. $this->Form->request->params['_Token']['key'] = 'secured';
  5666. $this->Form->button('Save', array('name' => 'save'));
  5667. $this->Form->button('Clear');
  5668. $result = $this->Form->unlockField();
  5669. $this->assertEquals(array('save'), $result);
  5670. }
  5671. /**
  5672. * testPostButton method
  5673. *
  5674. * @return void
  5675. */
  5676. public function testPostButton() {
  5677. $result = $this->Form->postButton('Hi', '/controller/action');
  5678. $this->assertTags($result, array(
  5679. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5680. 'div' => array('style' => 'display:none;'),
  5681. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5682. '/div',
  5683. 'button' => array('type' => 'submit'),
  5684. 'Hi',
  5685. '/button',
  5686. '/form'
  5687. ));
  5688. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5689. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  5690. }
  5691. /**
  5692. * Test that postButton adds _Token fields.
  5693. *
  5694. * @return void
  5695. */
  5696. public function testSecurePostButton() {
  5697. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5698. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5699. $expected = array(
  5700. 'form' => array(
  5701. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5702. ),
  5703. array('div' => array('style' => 'display:none;')),
  5704. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5705. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5706. '/div',
  5707. 'button' => array('type' => 'submit'),
  5708. 'Delete',
  5709. '/button',
  5710. array('div' => array('style' => 'display:none;')),
  5711. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5712. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5713. '/div',
  5714. '/form',
  5715. );
  5716. $this->assertTags($result, $expected);
  5717. }
  5718. /**
  5719. * testPostLink method
  5720. *
  5721. * @return void
  5722. */
  5723. public function testPostLink() {
  5724. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5725. $this->assertTags($result, array(
  5726. 'form' => array(
  5727. 'method' => 'post', 'action' => '/posts/delete/1',
  5728. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5729. ),
  5730. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5731. '/form',
  5732. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5733. 'Delete',
  5734. '/a'
  5735. ));
  5736. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  5737. $this->assertTags($result, array(
  5738. 'form' => array(
  5739. 'method' => 'post', 'action' => '/posts/delete/1',
  5740. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5741. ),
  5742. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  5743. '/form',
  5744. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5745. 'Delete',
  5746. '/a'
  5747. ));
  5748. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5749. $this->assertTags($result, array(
  5750. 'form' => array(
  5751. 'method' => 'post', 'action' => '/posts/delete/1',
  5752. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5753. ),
  5754. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5755. '/form',
  5756. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5757. 'Delete',
  5758. '/a'
  5759. ));
  5760. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5761. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  5762. }
  5763. /**
  5764. * Test that postLink adds _Token fields.
  5765. *
  5766. * @return void
  5767. */
  5768. public function testSecurePostLink() {
  5769. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5770. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5771. $expected = array(
  5772. 'form' => array(
  5773. 'method' => 'post', 'action' => '/posts/delete/1',
  5774. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5775. ),
  5776. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5777. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5778. 'div' => array('style' => 'display:none;'),
  5779. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5780. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5781. '/div',
  5782. '/form',
  5783. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5784. 'Delete',
  5785. '/a'
  5786. );
  5787. $this->assertTags($result, $expected);
  5788. }
  5789. /**
  5790. * testSubmitButton method
  5791. *
  5792. * @return void
  5793. */
  5794. public function testSubmitButton() {
  5795. $result = $this->Form->submit('');
  5796. $expected = array(
  5797. 'div' => array('class' => 'submit'),
  5798. 'input' => array('type' => 'submit', 'value' => ''),
  5799. '/div'
  5800. );
  5801. $this->assertTags($result, $expected);
  5802. $result = $this->Form->submit('Test Submit');
  5803. $expected = array(
  5804. 'div' => array('class' => 'submit'),
  5805. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5806. '/div'
  5807. );
  5808. $this->assertTags($result, $expected);
  5809. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5810. $expected = array(
  5811. 'span' => array('class' => 'submit'),
  5812. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5813. '/span'
  5814. );
  5815. $this->assertTags($result, $expected);
  5816. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5817. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5818. $this->assertTags($result, $expected);
  5819. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5820. $expected = array(
  5821. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5822. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5823. '/div'
  5824. );
  5825. $this->assertTags($result, $expected);
  5826. $result = $this->Form->submit('Next >');
  5827. $expected = array(
  5828. 'div' => array('class' => 'submit'),
  5829. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5830. '/div'
  5831. );
  5832. $this->assertTags($result, $expected);
  5833. $result = $this->Form->submit('Next >', array('escape' => false));
  5834. $expected = array(
  5835. 'div' => array('class' => 'submit'),
  5836. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5837. '/div'
  5838. );
  5839. $this->assertTags($result, $expected);
  5840. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5841. $expected = array(
  5842. 'div' => array('class' => 'submit'),
  5843. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5844. '/div'
  5845. );
  5846. $this->assertTags($result, $expected);
  5847. $before = '--before--';
  5848. $after = '--after--';
  5849. $result = $this->Form->submit('Test', array('before' => $before));
  5850. $expected = array(
  5851. 'div' => array('class' => 'submit'),
  5852. '--before--',
  5853. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5854. '/div'
  5855. );
  5856. $this->assertTags($result, $expected);
  5857. $result = $this->Form->submit('Test', array('after' => $after));
  5858. $expected = array(
  5859. 'div' => array('class' => 'submit'),
  5860. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5861. '--after--',
  5862. '/div'
  5863. );
  5864. $this->assertTags($result, $expected);
  5865. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5866. $expected = array(
  5867. 'div' => array('class' => 'submit'),
  5868. '--before--',
  5869. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5870. '--after--',
  5871. '/div'
  5872. );
  5873. $this->assertTags($result, $expected);
  5874. }
  5875. /**
  5876. * test image submit types.
  5877. *
  5878. * @return void
  5879. */
  5880. public function testSubmitImage() {
  5881. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5882. $expected = array(
  5883. 'div' => array('class' => 'submit'),
  5884. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5885. '/div'
  5886. );
  5887. $this->assertTags($result, $expected);
  5888. $result = $this->Form->submit('/relative/cake.power.gif');
  5889. $expected = array(
  5890. 'div' => array('class' => 'submit'),
  5891. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5892. '/div'
  5893. );
  5894. $this->assertTags($result, $expected);
  5895. $result = $this->Form->submit('cake.power.gif');
  5896. $expected = array(
  5897. 'div' => array('class' => 'submit'),
  5898. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5899. '/div'
  5900. );
  5901. $this->assertTags($result, $expected);
  5902. $result = $this->Form->submit('Not.an.image');
  5903. $expected = array(
  5904. 'div' => array('class' => 'submit'),
  5905. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5906. '/div'
  5907. );
  5908. $this->assertTags($result, $expected);
  5909. $after = '--after--';
  5910. $before = '--before--';
  5911. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5912. $expected = array(
  5913. 'div' => array('class' => 'submit'),
  5914. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5915. '--after--',
  5916. '/div'
  5917. );
  5918. $this->assertTags($result, $expected);
  5919. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5920. $expected = array(
  5921. 'div' => array('class' => 'submit'),
  5922. '--before--',
  5923. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5924. '/div'
  5925. );
  5926. $this->assertTags($result, $expected);
  5927. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5928. $expected = array(
  5929. 'div' => array('class' => 'submit'),
  5930. '--before--',
  5931. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5932. '--after--',
  5933. '/div'
  5934. );
  5935. $this->assertTags($result, $expected);
  5936. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  5937. $expected = array(
  5938. 'div' => array('class' => 'submit'),
  5939. '--before--',
  5940. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5941. '--after--',
  5942. '/div'
  5943. );
  5944. $this->assertTags($result, $expected);
  5945. }
  5946. /**
  5947. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5948. * be submitted at a time.
  5949. *
  5950. * @return void
  5951. */
  5952. public function testSubmitUnlockedByDefault() {
  5953. $this->Form->request->params['_Token']['key'] = 'secured';
  5954. $this->Form->submit('Go go');
  5955. $this->Form->submit('Save', array('name' => 'save'));
  5956. $result = $this->Form->unlockField();
  5957. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5958. }
  5959. /**
  5960. * Test submit image with timestamps.
  5961. *
  5962. * @return void
  5963. */
  5964. public function testSubmitImageTimestamp() {
  5965. Configure::write('Asset.timestamp', 'force');
  5966. $result = $this->Form->submit('cake.power.gif');
  5967. $expected = array(
  5968. 'div' => array('class' => 'submit'),
  5969. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  5970. '/div'
  5971. );
  5972. $this->assertTags($result, $expected);
  5973. }
  5974. /**
  5975. * test the create() method
  5976. *
  5977. * @return void
  5978. */
  5979. public function testCreate() {
  5980. $result = $this->Form->create('Contact');
  5981. $encoding = strtolower(Configure::read('App.encoding'));
  5982. $expected = array(
  5983. 'form' => array(
  5984. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5985. 'accept-charset' => $encoding
  5986. ),
  5987. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  5988. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5989. '/div'
  5990. );
  5991. $this->assertTags($result, $expected);
  5992. $result = $this->Form->create('Contact', array('type' => 'GET'));
  5993. $expected = array('form' => array(
  5994. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5995. 'accept-charset' => $encoding
  5996. ));
  5997. $this->assertTags($result, $expected);
  5998. $result = $this->Form->create('Contact', array('type' => 'get'));
  5999. $expected = array('form' => array(
  6000. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6001. 'accept-charset' => $encoding
  6002. ));
  6003. $this->assertTags($result, $expected);
  6004. $result = $this->Form->create('Contact', array('type' => 'put'));
  6005. $expected = array(
  6006. 'form' => array(
  6007. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6008. 'accept-charset' => $encoding
  6009. ),
  6010. 'div' => array('style' => 'display:none;'),
  6011. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6012. '/div'
  6013. );
  6014. $this->assertTags($result, $expected);
  6015. $result = $this->Form->create('Contact', array('type' => 'file'));
  6016. $expected = array(
  6017. 'form' => array(
  6018. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6019. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6020. ),
  6021. 'div' => array('style' => 'display:none;'),
  6022. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6023. '/div'
  6024. );
  6025. $this->assertTags($result, $expected);
  6026. $this->Form->request->data['Contact']['id'] = 1;
  6027. $this->Form->request->here = '/contacts/edit/1';
  6028. $this->Form->request['action'] = 'edit';
  6029. $result = $this->Form->create('Contact');
  6030. $expected = array(
  6031. 'form' => array(
  6032. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6033. 'accept-charset' => $encoding
  6034. ),
  6035. 'div' => array('style' => 'display:none;'),
  6036. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6037. '/div'
  6038. );
  6039. $this->assertTags($result, $expected);
  6040. $this->Form->request->data['Contact']['id'] = 1;
  6041. $this->Form->request->here = '/contacts/edit/1';
  6042. $this->Form->request['action'] = 'edit';
  6043. $result = $this->Form->create('Contact', array('type' => 'file'));
  6044. $expected = array(
  6045. 'form' => array(
  6046. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6047. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6048. ),
  6049. 'div' => array('style' => 'display:none;'),
  6050. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6051. '/div'
  6052. );
  6053. $this->assertTags($result, $expected);
  6054. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  6055. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  6056. $expected = array(
  6057. 'form' => array(
  6058. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  6059. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  6060. ),
  6061. 'div' => array('style' => 'display:none;'),
  6062. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6063. '/div'
  6064. );
  6065. $this->assertTags($result, $expected);
  6066. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  6067. $expected = array(
  6068. 'form' => array(
  6069. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  6070. 'accept-charset' => $encoding
  6071. ),
  6072. 'div' => array('style' => 'display:none;'),
  6073. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6074. '/div'
  6075. );
  6076. $this->assertTags($result, $expected);
  6077. $this->Form->request['action'] = 'add';
  6078. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  6079. $expected = array(
  6080. 'form' => array(
  6081. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  6082. 'accept-charset' => $encoding
  6083. ),
  6084. 'div' => array('style' => 'display:none;'),
  6085. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6086. '/div'
  6087. );
  6088. $this->assertTags($result, $expected);
  6089. $result = $this->Form->create('User', array('action' => 'login'));
  6090. $expected = array(
  6091. 'form' => array(
  6092. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  6093. 'accept-charset' => $encoding
  6094. ),
  6095. 'div' => array('style' => 'display:none;'),
  6096. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6097. '/div'
  6098. );
  6099. $this->assertTags($result, $expected);
  6100. $result = $this->Form->create('User', array('url' => '/users/login'));
  6101. $expected = array(
  6102. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  6103. 'div' => array('style' => 'display:none;'),
  6104. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6105. '/div'
  6106. );
  6107. $this->assertTags($result, $expected);
  6108. $this->Form->request['controller'] = 'pages';
  6109. $result = $this->Form->create('User', array('action' => 'signup'));
  6110. $expected = array(
  6111. 'form' => array(
  6112. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  6113. 'accept-charset' => $encoding
  6114. ),
  6115. 'div' => array('style' => 'display:none;'),
  6116. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6117. '/div'
  6118. );
  6119. $this->assertTags($result, $expected);
  6120. $this->Form->request->data = array();
  6121. $this->Form->request['controller'] = 'contacts';
  6122. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6123. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  6124. $expected = array(
  6125. 'form' => array(
  6126. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  6127. 'accept-charset' => 'utf-8'
  6128. ),
  6129. 'div' => array('style' => 'display:none;'),
  6130. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6131. '/div'
  6132. );
  6133. $this->assertTags($result, $expected);
  6134. }
  6135. /**
  6136. * Test the onsubmit option for create()
  6137. *
  6138. * @return void
  6139. */
  6140. public function testCreateOnSubmit() {
  6141. $this->Form->request->data = array();
  6142. $this->Form->request['controller'] = 'contacts';
  6143. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6144. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  6145. $expected = array(
  6146. 'form' => array(
  6147. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  6148. 'accept-charset' => 'utf-8'
  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. $this->Form->request->data = array();
  6156. $this->Form->request['controller'] = 'contacts';
  6157. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6158. $result = $this->Form->create(array(
  6159. 'url' => array('action' => 'index', 'param'),
  6160. 'default' => false,
  6161. 'onsubmit' => 'someFunction();'
  6162. ));
  6163. $expected = array(
  6164. 'form' => array(
  6165. 'id' => 'ContactAddForm', 'method' => 'post',
  6166. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6167. 'action' => '/contacts/index/param',
  6168. 'accept-charset' => 'utf-8'
  6169. ),
  6170. 'div' => array('style' => 'display:none;'),
  6171. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6172. '/div'
  6173. );
  6174. $this->assertTags($result, $expected);
  6175. }
  6176. /**
  6177. * test create() with automatic url generation
  6178. *
  6179. * @return void
  6180. */
  6181. public function testCreateAutoUrl() {
  6182. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6183. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6184. $this->Form->request->base = '/base_url';
  6185. $result = $this->Form->create('Contact');
  6186. $expected = array(
  6187. 'form' => array(
  6188. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6189. 'accept-charset' => 'utf-8'
  6190. ),
  6191. 'div' => array('style' => 'display:none;'),
  6192. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6193. '/div'
  6194. );
  6195. $this->assertTags($result, $expected);
  6196. $this->Form->request['action'] = 'delete';
  6197. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6198. $this->Form->request->base = '/base_url';
  6199. $result = $this->Form->create('Contact');
  6200. $expected = array(
  6201. 'form' => array(
  6202. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6203. 'accept-charset' => 'utf-8'
  6204. ),
  6205. 'div' => array('style' => 'display:none;'),
  6206. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6207. '/div'
  6208. );
  6209. $this->assertTags($result, $expected);
  6210. }
  6211. /**
  6212. * test create() with a custom route
  6213. *
  6214. * @return void
  6215. */
  6216. public function testCreateCustomRoute() {
  6217. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6218. $encoding = strtolower(Configure::read('App.encoding'));
  6219. $result = $this->Form->create('User', array('action' => 'login'));
  6220. $expected = array(
  6221. 'form' => array(
  6222. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6223. 'accept-charset' => $encoding
  6224. ),
  6225. 'div' => array('style' => 'display:none;'),
  6226. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6227. '/div'
  6228. );
  6229. $this->assertTags($result, $expected);
  6230. }
  6231. /**
  6232. * test that inputDefaults are stored and used.
  6233. *
  6234. * @return void
  6235. */
  6236. public function testCreateWithInputDefaults() {
  6237. $this->Form->create('User', array(
  6238. 'inputDefaults' => array(
  6239. 'div' => false,
  6240. 'label' => false,
  6241. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6242. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6243. )
  6244. ));
  6245. $result = $this->Form->input('username');
  6246. $expected = array(
  6247. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6248. );
  6249. $this->assertTags($result, $expected);
  6250. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6251. $expected = array(
  6252. 'div' => array('class' => 'input text'),
  6253. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6254. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6255. '/div'
  6256. );
  6257. $this->assertTags($result, $expected);
  6258. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  6259. $expected = array(
  6260. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6261. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  6262. );
  6263. $this->assertTags($result, $expected);
  6264. $this->Form->create('User', array(
  6265. 'inputDefaults' => array(
  6266. 'div' => false,
  6267. 'label' => array('class' => 'nice', 'for' => 'changed'),
  6268. )
  6269. ));
  6270. $result = $this->Form->input('username', array('div' => true));
  6271. $expected = array(
  6272. 'div' => array('class' => 'input text'),
  6273. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  6274. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6275. '/div'
  6276. );
  6277. $this->assertTags($result, $expected);
  6278. }
  6279. /**
  6280. * test automatic accept-charset overriding
  6281. *
  6282. * @return void
  6283. */
  6284. public function testCreateWithAcceptCharset() {
  6285. $result = $this->Form->create('UserForm', array(
  6286. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  6287. )
  6288. );
  6289. $expected = array(
  6290. 'form' => array(
  6291. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6292. 'accept-charset' => 'iso-8859-1'
  6293. ),
  6294. 'div' => array('style' => 'display:none;'),
  6295. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6296. '/div'
  6297. );
  6298. $this->assertTags($result, $expected);
  6299. }
  6300. /**
  6301. * Test base form url when url param is passed with multiple parameters (&)
  6302. *
  6303. */
  6304. public function testCreateQuerystringrequest() {
  6305. $encoding = strtolower(Configure::read('App.encoding'));
  6306. $result = $this->Form->create('Contact', array(
  6307. 'type' => 'post',
  6308. 'escape' => false,
  6309. 'url' => array(
  6310. 'controller' => 'controller',
  6311. 'action' => 'action',
  6312. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6313. )
  6314. ));
  6315. $expected = array(
  6316. 'form' => array(
  6317. 'id' => 'ContactAddForm',
  6318. 'method' => 'post',
  6319. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6320. 'accept-charset' => $encoding
  6321. ),
  6322. 'div' => array('style' => 'display:none;'),
  6323. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6324. '/div'
  6325. );
  6326. $this->assertTags($result, $expected);
  6327. $result = $this->Form->create('Contact', array(
  6328. 'type' => 'post',
  6329. 'url' => array(
  6330. 'controller' => 'controller',
  6331. 'action' => 'action',
  6332. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6333. )
  6334. ));
  6335. $expected = array(
  6336. 'form' => array(
  6337. 'id' => 'ContactAddForm',
  6338. 'method' => 'post',
  6339. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6340. 'accept-charset' => $encoding
  6341. ),
  6342. 'div' => array('style' => 'display:none;'),
  6343. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6344. '/div'
  6345. );
  6346. $this->assertTags($result, $expected);
  6347. }
  6348. /**
  6349. * test that create() doesn't cause errors by multiple id's being in the primary key
  6350. * as could happen with multiple select or checkboxes.
  6351. *
  6352. * @return void
  6353. */
  6354. public function testCreateWithMultipleIdInData() {
  6355. $encoding = strtolower(Configure::read('App.encoding'));
  6356. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6357. $result = $this->Form->create('Contact');
  6358. $expected = array(
  6359. 'form' => array(
  6360. 'id' => 'ContactAddForm',
  6361. 'method' => 'post',
  6362. 'action' => '/contacts/add',
  6363. 'accept-charset' => $encoding
  6364. ),
  6365. 'div' => array('style' => 'display:none;'),
  6366. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6367. '/div'
  6368. );
  6369. $this->assertTags($result, $expected);
  6370. }
  6371. /**
  6372. * test that create() doesn't add in extra passed params.
  6373. *
  6374. * @return void
  6375. */
  6376. public function testCreatePassedArgs() {
  6377. $encoding = strtolower(Configure::read('App.encoding'));
  6378. $this->Form->request->data['Contact']['id'] = 1;
  6379. $result = $this->Form->create('Contact', array(
  6380. 'type' => 'post',
  6381. 'escape' => false,
  6382. 'url' => array(
  6383. 'action' => 'edit',
  6384. 'myparam'
  6385. )
  6386. ));
  6387. $expected = array(
  6388. 'form' => array(
  6389. 'id' => 'ContactAddForm',
  6390. 'method' => 'post',
  6391. 'action' => '/contacts/edit/myparam',
  6392. 'accept-charset' => $encoding
  6393. ),
  6394. 'div' => array('style' => 'display:none;'),
  6395. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6396. '/div'
  6397. );
  6398. $this->assertTags($result, $expected);
  6399. }
  6400. /**
  6401. * test creating a get form, and get form inputs.
  6402. *
  6403. * @return void
  6404. */
  6405. public function testGetFormCreate() {
  6406. $encoding = strtolower(Configure::read('App.encoding'));
  6407. $result = $this->Form->create('Contact', array('type' => 'get'));
  6408. $this->assertTags($result, array('form' => array(
  6409. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6410. 'accept-charset' => $encoding
  6411. )));
  6412. $result = $this->Form->text('Contact.name');
  6413. $this->assertTags($result, array('input' => array(
  6414. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6415. )));
  6416. $result = $this->Form->password('password');
  6417. $this->assertTags($result, array('input' => array(
  6418. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6419. )));
  6420. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6421. $result = $this->Form->text('user_form');
  6422. $this->assertTags($result, array('input' => array(
  6423. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6424. )));
  6425. }
  6426. /**
  6427. * test get form, and inputs when the model param is false
  6428. *
  6429. * @return void
  6430. */
  6431. public function testGetFormWithFalseModel() {
  6432. $encoding = strtolower(Configure::read('App.encoding'));
  6433. $this->Form->request['controller'] = 'contact_test';
  6434. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6435. $expected = array('form' => array(
  6436. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6437. 'accept-charset' => $encoding
  6438. ));
  6439. $this->assertTags($result, $expected);
  6440. $result = $this->Form->text('reason');
  6441. $expected = array(
  6442. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6443. );
  6444. $this->assertTags($result, $expected);
  6445. }
  6446. /**
  6447. * test that datetime() works with GET style forms.
  6448. *
  6449. * @return void
  6450. */
  6451. public function testDateTimeWithGetForms() {
  6452. extract($this->dateRegex);
  6453. $this->Form->create('Contact', array('type' => 'get'));
  6454. $result = $this->Form->datetime('created');
  6455. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6456. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6457. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6458. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6459. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6460. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6461. }
  6462. /**
  6463. * testEditFormWithData method
  6464. *
  6465. * test auto populating form elements from submitted data.
  6466. *
  6467. * @return void
  6468. */
  6469. public function testEditFormWithData() {
  6470. $this->Form->request->data = array('Person' => array(
  6471. 'id' => 1,
  6472. 'first_name' => 'Nate',
  6473. 'last_name' => 'Abele',
  6474. 'email' => 'nate@example.com'
  6475. ));
  6476. $this->Form->request->addParams(array(
  6477. 'models' => array('Person'),
  6478. 'controller' => 'people',
  6479. 'action' => 'add'
  6480. ));
  6481. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6482. $this->Form->create();
  6483. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6484. $expected = array(
  6485. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6486. 'select' => array(
  6487. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6488. ),
  6489. array('option' => array('value' => 1)), 'Nate', '/option',
  6490. array('option' => array('value' => 2)), 'Garrett', '/option',
  6491. array('option' => array('value' => 3)), 'Larry', '/option',
  6492. '/select'
  6493. );
  6494. $this->assertTags($result, $expected);
  6495. }
  6496. /**
  6497. * Test that required fields are created for various types of validation.
  6498. *
  6499. * @return void
  6500. */
  6501. public function testFormInputRequiredDetection() {
  6502. $this->Form->create('Contact');
  6503. $result = $this->Form->input('Contact.non_existing');
  6504. $expected = array(
  6505. 'div' => array('class' => 'input text'),
  6506. 'label' => array('for' => 'ContactNonExisting'),
  6507. 'Non Existing',
  6508. '/label',
  6509. 'input' => array(
  6510. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  6511. 'id' => 'ContactNonExisting'
  6512. ),
  6513. '/div'
  6514. );
  6515. $this->assertTags($result, $expected);
  6516. $result = $this->Form->input('Contact.imrequired');
  6517. $expected = array(
  6518. 'div' => array('class' => 'input text required'),
  6519. 'label' => array('for' => 'ContactImrequired'),
  6520. 'Imrequired',
  6521. '/label',
  6522. 'input' => array(
  6523. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  6524. 'id' => 'ContactImrequired'
  6525. ),
  6526. '/div'
  6527. );
  6528. $this->assertTags($result, $expected);
  6529. $result = $this->Form->input('Contact.imalsorequired');
  6530. $expected = array(
  6531. 'div' => array('class' => 'input text required'),
  6532. 'label' => array('for' => 'ContactImalsorequired'),
  6533. 'Imalsorequired',
  6534. '/label',
  6535. 'input' => array(
  6536. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  6537. 'id' => 'ContactImalsorequired'
  6538. ),
  6539. '/div'
  6540. );
  6541. $this->assertTags($result, $expected);
  6542. $result = $this->Form->input('Contact.imrequiredtoo');
  6543. $expected = array(
  6544. 'div' => array('class' => 'input text required'),
  6545. 'label' => array('for' => 'ContactImrequiredtoo'),
  6546. 'Imrequiredtoo',
  6547. '/label',
  6548. 'input' => array(
  6549. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  6550. 'id' => 'ContactImrequiredtoo'
  6551. ),
  6552. '/div'
  6553. );
  6554. $this->assertTags($result, $expected);
  6555. $result = $this->Form->input('Contact.required_one');
  6556. $expected = array(
  6557. 'div' => array('class' => 'input text required'),
  6558. 'label' => array('for' => 'ContactRequiredOne'),
  6559. 'Required One',
  6560. '/label',
  6561. 'input' => array(
  6562. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  6563. 'id' => 'ContactRequiredOne'
  6564. ),
  6565. '/div'
  6566. );
  6567. $this->assertTags($result, $expected);
  6568. $result = $this->Form->input('Contact.string_required');
  6569. $expected = array(
  6570. 'div' => array('class' => 'input text required'),
  6571. 'label' => array('for' => 'ContactStringRequired'),
  6572. 'String Required',
  6573. '/label',
  6574. 'input' => array(
  6575. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  6576. 'id' => 'ContactStringRequired'
  6577. ),
  6578. '/div'
  6579. );
  6580. $this->assertTags($result, $expected);
  6581. $result = $this->Form->input('Contact.imnotrequired');
  6582. $expected = array(
  6583. 'div' => array('class' => 'input text'),
  6584. 'label' => array('for' => 'ContactImnotrequired'),
  6585. 'Imnotrequired',
  6586. '/label',
  6587. 'input' => array(
  6588. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  6589. 'id' => 'ContactImnotrequired'
  6590. ),
  6591. '/div'
  6592. );
  6593. $this->assertTags($result, $expected);
  6594. $result = $this->Form->input('Contact.imalsonotrequired');
  6595. $expected = array(
  6596. 'div' => array('class' => 'input text'),
  6597. 'label' => array('for' => 'ContactImalsonotrequired'),
  6598. 'Imalsonotrequired',
  6599. '/label',
  6600. 'input' => array(
  6601. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  6602. 'id' => 'ContactImalsonotrequired'
  6603. ),
  6604. '/div'
  6605. );
  6606. $this->assertTags($result, $expected);
  6607. $result = $this->Form->input('Contact.imalsonotrequired2');
  6608. $expected = array(
  6609. 'div' => array('class' => 'input text'),
  6610. 'label' => array('for' => 'ContactImalsonotrequired2'),
  6611. 'Imalsonotrequired2',
  6612. '/label',
  6613. 'input' => array(
  6614. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  6615. 'id' => 'ContactImalsonotrequired2'
  6616. ),
  6617. '/div'
  6618. );
  6619. $this->assertTags($result, $expected);
  6620. $result = $this->Form->input('Contact.imnotrequiredeither');
  6621. $expected = array(
  6622. 'div' => array('class' => 'input text'),
  6623. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6624. 'Imnotrequiredeither',
  6625. '/label',
  6626. 'input' => array(
  6627. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  6628. 'id' => 'ContactImnotrequiredeither'
  6629. ),
  6630. '/div'
  6631. );
  6632. $this->assertTags($result, $expected);
  6633. $result = $this->Form->input('Contact.iamrequiredalways');
  6634. $expected = array(
  6635. 'div' => array('class' => 'input text required'),
  6636. 'label' => array('for' => 'ContactIamrequiredalways'),
  6637. 'Iamrequiredalways',
  6638. '/label',
  6639. 'input' => array(
  6640. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  6641. 'id' => 'ContactIamrequiredalways'
  6642. ),
  6643. '/div'
  6644. );
  6645. $this->assertTags($result, $expected);
  6646. }
  6647. /**
  6648. * testFormMagicInput method
  6649. *
  6650. * @return void
  6651. */
  6652. public function testFormMagicInput() {
  6653. $encoding = strtolower(Configure::read('App.encoding'));
  6654. $result = $this->Form->create('Contact');
  6655. $expected = array(
  6656. 'form' => array(
  6657. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6658. 'accept-charset' => $encoding
  6659. ),
  6660. 'div' => array('style' => 'display:none;'),
  6661. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6662. '/div'
  6663. );
  6664. $this->assertTags($result, $expected);
  6665. $result = $this->Form->input('name');
  6666. $expected = array(
  6667. 'div' => array('class' => 'input text'),
  6668. 'label' => array('for' => 'ContactName'),
  6669. 'Name',
  6670. '/label',
  6671. 'input' => array(
  6672. 'type' => 'text', 'name' => 'data[Contact][name]',
  6673. 'id' => 'ContactName', 'maxlength' => '255'
  6674. ),
  6675. '/div'
  6676. );
  6677. $this->assertTags($result, $expected);
  6678. $result = $this->Form->input('non_existing_field_in_contact_model');
  6679. $expected = array(
  6680. 'div' => array('class' => 'input text'),
  6681. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  6682. 'Non Existing Field In Contact Model',
  6683. '/label',
  6684. 'input' => array(
  6685. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  6686. 'id' => 'ContactNonExistingFieldInContactModel'
  6687. ),
  6688. '/div'
  6689. );
  6690. $this->assertTags($result, $expected);
  6691. $result = $this->Form->input('Address.street');
  6692. $expected = array(
  6693. 'div' => array('class' => 'input text'),
  6694. 'label' => array('for' => 'AddressStreet'),
  6695. 'Street',
  6696. '/label',
  6697. 'input' => array(
  6698. 'type' => 'text', 'name' => 'data[Address][street]',
  6699. 'id' => 'AddressStreet'
  6700. ),
  6701. '/div'
  6702. );
  6703. $this->assertTags($result, $expected);
  6704. $result = $this->Form->input('Address.non_existing_field_in_model');
  6705. $expected = array(
  6706. 'div' => array('class' => 'input text'),
  6707. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  6708. 'Non Existing Field In Model',
  6709. '/label',
  6710. 'input' => array(
  6711. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  6712. 'id' => 'AddressNonExistingFieldInModel'
  6713. ),
  6714. '/div'
  6715. );
  6716. $this->assertTags($result, $expected);
  6717. $result = $this->Form->input('name', array('div' => false));
  6718. $expected = array(
  6719. 'label' => array('for' => 'ContactName'),
  6720. 'Name',
  6721. '/label',
  6722. 'input' => array(
  6723. 'type' => 'text', 'name' => 'data[Contact][name]',
  6724. 'id' => 'ContactName', 'maxlength' => '255'
  6725. )
  6726. );
  6727. $this->assertTags($result, $expected);
  6728. extract($this->dateRegex);
  6729. $now = strtotime('now');
  6730. $result = $this->Form->input('Contact.published', array('div' => false));
  6731. $expected = array(
  6732. 'label' => array('for' => 'ContactPublishedMonth'),
  6733. 'Published',
  6734. '/label',
  6735. array('select' => array(
  6736. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  6737. )),
  6738. $monthsRegex,
  6739. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6740. date('F', $now),
  6741. '/option',
  6742. '*/select',
  6743. '-',
  6744. array('select' => array(
  6745. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  6746. )),
  6747. $daysRegex,
  6748. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6749. date('j', $now),
  6750. '/option',
  6751. '*/select',
  6752. '-',
  6753. array('select' => array(
  6754. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  6755. )),
  6756. $yearsRegex,
  6757. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6758. date('Y', $now),
  6759. '*/select'
  6760. );
  6761. $this->assertTags($result, $expected);
  6762. $result = $this->Form->input('Contact.updated', array('div' => false));
  6763. $expected = array(
  6764. 'label' => array('for' => 'ContactUpdatedMonth'),
  6765. 'Updated',
  6766. '/label',
  6767. array('select' => array(
  6768. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  6769. )),
  6770. $monthsRegex,
  6771. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6772. date('F', $now),
  6773. '/option',
  6774. '*/select',
  6775. '-',
  6776. array('select' => array(
  6777. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  6778. )),
  6779. $daysRegex,
  6780. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6781. date('j', $now),
  6782. '/option',
  6783. '*/select',
  6784. '-',
  6785. array('select' => array(
  6786. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  6787. )),
  6788. $yearsRegex,
  6789. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6790. date('Y', $now),
  6791. '*/select'
  6792. );
  6793. $this->assertTags($result, $expected);
  6794. $result = $this->Form->input('UserForm.stuff');
  6795. $expected = array(
  6796. 'div' => array('class' => 'input text'),
  6797. 'label' => array('for' => 'UserFormStuff'),
  6798. 'Stuff',
  6799. '/label',
  6800. 'input' => array(
  6801. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  6802. 'id' => 'UserFormStuff', 'maxlength' => 10
  6803. ),
  6804. '/div'
  6805. );
  6806. $this->assertTags($result, $expected);
  6807. }
  6808. /**
  6809. * testForMagicInputNonExistingNorValidated method
  6810. *
  6811. * @return void
  6812. */
  6813. public function testForMagicInputNonExistingNorValidated() {
  6814. $encoding = strtolower(Configure::read('App.encoding'));
  6815. $result = $this->Form->create('Contact');
  6816. $expected = array(
  6817. 'form' => array(
  6818. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6819. 'accept-charset' => $encoding
  6820. ),
  6821. 'div' => array('style' => 'display:none;'),
  6822. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6823. '/div'
  6824. );
  6825. $this->assertTags($result, $expected);
  6826. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6827. $expected = array(
  6828. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6829. 'Non Existing Nor Validated',
  6830. '/label',
  6831. 'input' => array(
  6832. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6833. 'id' => 'ContactNonExistingNorValidated'
  6834. )
  6835. );
  6836. $this->assertTags($result, $expected);
  6837. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6838. 'div' => false, 'value' => 'my value'
  6839. ));
  6840. $expected = array(
  6841. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6842. 'Non Existing Nor Validated',
  6843. '/label',
  6844. 'input' => array(
  6845. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6846. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6847. )
  6848. );
  6849. $this->assertTags($result, $expected);
  6850. $this->Form->request->data = array(
  6851. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6852. ));
  6853. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6854. $expected = array(
  6855. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6856. 'Non Existing Nor Validated',
  6857. '/label',
  6858. 'input' => array(
  6859. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6860. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6861. )
  6862. );
  6863. $this->assertTags($result, $expected);
  6864. }
  6865. /**
  6866. * testFormMagicInputLabel method
  6867. *
  6868. * @return void
  6869. */
  6870. public function testFormMagicInputLabel() {
  6871. $encoding = strtolower(Configure::read('App.encoding'));
  6872. $result = $this->Form->create('Contact');
  6873. $expected = array(
  6874. 'form' => array(
  6875. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6876. 'accept-charset' => $encoding
  6877. ),
  6878. 'div' => array('style' => 'display:none;'),
  6879. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6880. '/div'
  6881. );
  6882. $this->assertTags($result, $expected);
  6883. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  6884. $this->assertTags($result, array('input' => array(
  6885. 'name' => 'data[Contact][name]', 'type' => 'text',
  6886. 'id' => 'ContactName', 'maxlength' => '255')
  6887. ));
  6888. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  6889. $expected = array(
  6890. 'label' => array('for' => 'ContactName'),
  6891. 'My label',
  6892. '/label',
  6893. 'input' => array(
  6894. 'type' => 'text', 'name' => 'data[Contact][name]',
  6895. 'id' => 'ContactName', 'maxlength' => '255'
  6896. )
  6897. );
  6898. $this->assertTags($result, $expected);
  6899. $result = $this->Form->input('Contact.name', array(
  6900. 'div' => false, 'label' => array('class' => 'mandatory')
  6901. ));
  6902. $expected = array(
  6903. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6904. 'Name',
  6905. '/label',
  6906. 'input' => array(
  6907. 'type' => 'text', 'name' => 'data[Contact][name]',
  6908. 'id' => 'ContactName', 'maxlength' => '255'
  6909. )
  6910. );
  6911. $this->assertTags($result, $expected);
  6912. $result = $this->Form->input('Contact.name', array(
  6913. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  6914. ));
  6915. $expected = array(
  6916. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6917. 'My label',
  6918. '/label',
  6919. 'input' => array(
  6920. 'type' => 'text', 'name' => 'data[Contact][name]',
  6921. 'id' => 'ContactName', 'maxlength' => '255'
  6922. )
  6923. );
  6924. $this->assertTags($result, $expected);
  6925. $result = $this->Form->input('Contact.name', array(
  6926. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  6927. ));
  6928. $expected = array(
  6929. 'label' => array('for' => 'my_id'),
  6930. 'Name',
  6931. '/label',
  6932. 'input' => array(
  6933. 'type' => 'text', 'name' => 'data[Contact][name]',
  6934. 'id' => 'my_id', 'maxlength' => '255'
  6935. )
  6936. );
  6937. $this->assertTags($result, $expected);
  6938. $result = $this->Form->input('1.id');
  6939. $this->assertTags($result, array('input' => array(
  6940. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6941. 'id' => 'Contact1Id'
  6942. )));
  6943. $result = $this->Form->input("1.name");
  6944. $expected = array(
  6945. 'div' => array('class' => 'input text'),
  6946. 'label' => array('for' => 'Contact1Name'),
  6947. 'Name',
  6948. '/label',
  6949. 'input' => array(
  6950. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  6951. 'id' => 'Contact1Name', 'maxlength' => '255'
  6952. ),
  6953. '/div'
  6954. );
  6955. $this->assertTags($result, $expected);
  6956. $result = $this->Form->input('Contact.1.id');
  6957. $this->assertTags($result, array(
  6958. 'input' => array(
  6959. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6960. 'id' => 'Contact1Id'
  6961. )
  6962. ));
  6963. $result = $this->Form->input("Model.1.name");
  6964. $expected = array(
  6965. 'div' => array('class' => 'input text'),
  6966. 'label' => array('for' => 'Model1Name'),
  6967. 'Name',
  6968. '/label',
  6969. 'input' => array(
  6970. 'type' => 'text', 'name' => 'data[Model][1][name]',
  6971. 'id' => 'Model1Name'
  6972. ),
  6973. '/div'
  6974. );
  6975. $this->assertTags($result, $expected);
  6976. }
  6977. /**
  6978. * testFormEnd method
  6979. *
  6980. * @return void
  6981. */
  6982. public function testFormEnd() {
  6983. $this->assertEquals('</form>', $this->Form->end());
  6984. $result = $this->Form->end('');
  6985. $expected = array(
  6986. 'div' => array('class' => 'submit'),
  6987. 'input' => array('type' => 'submit', 'value' => ''),
  6988. '/div',
  6989. '/form'
  6990. );
  6991. $this->assertTags($result, $expected);
  6992. $result = $this->Form->end(array('label' => ''));
  6993. $expected = array(
  6994. 'div' => array('class' => 'submit'),
  6995. 'input' => array('type' => 'submit', 'value' => ''),
  6996. '/div',
  6997. '/form'
  6998. );
  6999. $this->assertTags($result, $expected);
  7000. $result = $this->Form->end('save');
  7001. $expected = array(
  7002. 'div' => array('class' => 'submit'),
  7003. 'input' => array('type' => 'submit', 'value' => 'save'),
  7004. '/div',
  7005. '/form'
  7006. );
  7007. $this->assertTags($result, $expected);
  7008. $result = $this->Form->end(array('label' => 'save'));
  7009. $expected = array(
  7010. 'div' => array('class' => 'submit'),
  7011. 'input' => array('type' => 'submit', 'value' => 'save'),
  7012. '/div',
  7013. '/form'
  7014. );
  7015. $this->assertTags($result, $expected);
  7016. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  7017. $expected = array(
  7018. 'div' => array('class' => 'submit'),
  7019. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7020. '/div',
  7021. '/form'
  7022. );
  7023. $this->assertTags($result, $expected);
  7024. $result = $this->Form->end(array('name' => 'Whatever'));
  7025. $expected = array(
  7026. 'div' => array('class' => 'submit'),
  7027. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  7028. '/div',
  7029. '/form'
  7030. );
  7031. $this->assertTags($result, $expected);
  7032. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  7033. $expected = array(
  7034. 'div' => array('class' => 'good'),
  7035. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7036. '/div',
  7037. '/form'
  7038. );
  7039. $this->assertTags($result, $expected);
  7040. $result = $this->Form->end(array(
  7041. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  7042. ));
  7043. $expected = array(
  7044. 'div' => array('class' => 'good'),
  7045. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7046. '/div',
  7047. '/form'
  7048. );
  7049. $this->assertTags($result, $expected);
  7050. }
  7051. /**
  7052. * testMultipleFormWithIdFields method
  7053. *
  7054. * @return void
  7055. */
  7056. public function testMultipleFormWithIdFields() {
  7057. $this->Form->create('UserForm');
  7058. $result = $this->Form->input('id');
  7059. $this->assertTags($result, array('input' => array(
  7060. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  7061. )));
  7062. $result = $this->Form->input('ValidateItem.id');
  7063. $this->assertTags($result, array('input' => array(
  7064. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  7065. 'id' => 'ValidateItemId'
  7066. )));
  7067. $result = $this->Form->input('ValidateUser.id');
  7068. $this->assertTags($result, array('input' => array(
  7069. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  7070. 'id' => 'ValidateUserId'
  7071. )));
  7072. }
  7073. /**
  7074. * testDbLessModel method
  7075. *
  7076. * @return void
  7077. */
  7078. public function testDbLessModel() {
  7079. $this->Form->create('TestMail');
  7080. $result = $this->Form->input('name');
  7081. $expected = array(
  7082. 'div' => array('class' => 'input text'),
  7083. 'label' => array('for' => 'TestMailName'),
  7084. 'Name',
  7085. '/label',
  7086. 'input' => array(
  7087. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7088. 'id' => 'TestMailName'
  7089. ),
  7090. '/div'
  7091. );
  7092. $this->assertTags($result, $expected);
  7093. ClassRegistry::init('TestMail');
  7094. $this->Form->create('TestMail');
  7095. $result = $this->Form->input('name');
  7096. $expected = array(
  7097. 'div' => array('class' => 'input text'),
  7098. 'label' => array('for' => 'TestMailName'),
  7099. 'Name',
  7100. '/label',
  7101. 'input' => array(
  7102. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7103. 'id' => 'TestMailName'
  7104. ),
  7105. '/div'
  7106. );
  7107. $this->assertTags($result, $expected);
  7108. }
  7109. /**
  7110. * testBrokenness method
  7111. *
  7112. * @return void
  7113. */
  7114. public function testBrokenness() {
  7115. /*
  7116. * #4 This test has two parents and four children. By default (as of r7117) both
  7117. * parents are show but the first parent is missing a child. This is the inconsistency
  7118. * in the default behaviour - one parent has all children, the other does not - dependent
  7119. * on the data values.
  7120. */
  7121. $result = $this->Form->select('Model.field', array(
  7122. 'Fred' => array(
  7123. 'freds_son_1' => 'Fred',
  7124. 'freds_son_2' => 'Freddie'
  7125. ),
  7126. 'Bert' => array(
  7127. 'berts_son_1' => 'Albert',
  7128. 'berts_son_2' => 'Bertie')
  7129. ),
  7130. array('showParents' => true, 'empty' => false)
  7131. );
  7132. $expected = array(
  7133. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7134. array('optgroup' => array('label' => 'Fred')),
  7135. array('option' => array('value' => 'freds_son_1')),
  7136. 'Fred',
  7137. '/option',
  7138. array('option' => array('value' => 'freds_son_2')),
  7139. 'Freddie',
  7140. '/option',
  7141. '/optgroup',
  7142. array('optgroup' => array('label' => 'Bert')),
  7143. array('option' => array('value' => 'berts_son_1')),
  7144. 'Albert',
  7145. '/option',
  7146. array('option' => array('value' => 'berts_son_2')),
  7147. 'Bertie',
  7148. '/option',
  7149. '/optgroup',
  7150. '/select'
  7151. );
  7152. $this->assertTags($result, $expected);
  7153. /*
  7154. * #2 This is structurally identical to the test above (#1) - only the parent name has
  7155. * changed, so we should expect the same select list data, just with a different name
  7156. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  7157. * This is where data corruption can occur, because when a select value is missing from
  7158. * a list a form will substitute the first value in the list - without the user knowing.
  7159. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  7160. * affect the availability of 3 => 'Three' as a valid option.
  7161. */
  7162. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  7163. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  7164. ));
  7165. $result = $this->Form->select(
  7166. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  7167. );
  7168. $expected = array(
  7169. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7170. array('option' => array('value' => 1)),
  7171. 'One',
  7172. '/option',
  7173. array('option' => array('value' => 2)),
  7174. 'Two',
  7175. '/option',
  7176. array('optgroup' => array('label' => 'Three')),
  7177. array('option' => array('value' => 3)),
  7178. 'Three',
  7179. '/option',
  7180. array('option' => array('value' => 4)),
  7181. 'Four',
  7182. '/option',
  7183. array('option' => array('value' => 5)),
  7184. 'Five',
  7185. '/option',
  7186. '/optgroup',
  7187. '/select'
  7188. );
  7189. $this->assertTags($result, $expected);
  7190. }
  7191. /**
  7192. * Test the generation of fields for a multi record form.
  7193. *
  7194. * @return void
  7195. */
  7196. public function testMultiRecordForm() {
  7197. $this->Form->create('ValidateProfile');
  7198. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  7199. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  7200. $expected = array(
  7201. 'div' => array('class' => 'input textarea'),
  7202. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  7203. 'Name',
  7204. '/label',
  7205. 'textarea' => array(
  7206. 'id' => 'ValidateProfile1ValidateItem2Name',
  7207. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  7208. 'cols' => 30,
  7209. 'rows' => 6
  7210. ),
  7211. 'Value',
  7212. '/textarea',
  7213. '/div'
  7214. );
  7215. $this->assertTags($result, $expected);
  7216. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
  7217. $expected = array(
  7218. 'div' => array('class' => 'input date'),
  7219. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7220. 'Created',
  7221. '/label',
  7222. array('select' => array(
  7223. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  7224. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7225. )
  7226. ),
  7227. array('option' => array('value' => '')), '/option',
  7228. $this->dateRegex['monthsRegex'],
  7229. '/select', '-',
  7230. array('select' => array(
  7231. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  7232. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7233. )
  7234. ),
  7235. array('option' => array('value' => '')), '/option',
  7236. $this->dateRegex['daysRegex'],
  7237. '/select', '-',
  7238. array('select' => array(
  7239. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  7240. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7241. )
  7242. ),
  7243. array('option' => array('value' => '')), '/option',
  7244. $this->dateRegex['yearsRegex'],
  7245. '/select',
  7246. '/div'
  7247. );
  7248. $this->assertTags($result, $expected);
  7249. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7250. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7251. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7252. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7253. $expected = array(
  7254. 'div' => array('class' => 'input select error'),
  7255. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7256. 'Profile',
  7257. '/label',
  7258. 'select' => array(
  7259. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7260. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7261. 'class' => 'form-error'
  7262. ),
  7263. '/select',
  7264. array('div' => array('class' => 'error-message')),
  7265. 'Error',
  7266. '/div',
  7267. '/div'
  7268. );
  7269. $this->assertTags($result, $expected);
  7270. }
  7271. /**
  7272. * test the correct display of multi-record form validation errors.
  7273. *
  7274. * @return void
  7275. */
  7276. public function testMultiRecordFormValidationErrors() {
  7277. $this->Form->create('ValidateProfile');
  7278. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7279. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7280. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7281. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7282. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7283. $result = $this->Form->error('ValidateProfile.2.city');
  7284. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7285. $result = $this->Form->error('2.city');
  7286. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7287. }
  7288. /**
  7289. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7290. *
  7291. * @return void
  7292. */
  7293. public function testInputTemplate() {
  7294. $result = $this->Form->input('Contact.email', array(
  7295. 'type' => 'text', 'format' => array('input')
  7296. ));
  7297. $expected = array(
  7298. 'div' => array('class' => 'input text'),
  7299. 'input' => array(
  7300. 'type' => 'text', 'name' => 'data[Contact][email]',
  7301. 'id' => 'ContactEmail'
  7302. ),
  7303. '/div'
  7304. );
  7305. $this->assertTags($result, $expected);
  7306. $result = $this->Form->input('Contact.email', array(
  7307. 'type' => 'text', 'format' => array('input', 'label'),
  7308. 'label' => '<em>Email (required)</em>'
  7309. ));
  7310. $expected = array(
  7311. 'div' => array('class' => 'input text'),
  7312. array('input' => array(
  7313. 'type' => 'text', 'name' => 'data[Contact][email]',
  7314. 'id' => 'ContactEmail'
  7315. )),
  7316. 'label' => array('for' => 'ContactEmail'),
  7317. 'em' => array(),
  7318. 'Email (required)',
  7319. '/em',
  7320. '/label',
  7321. '/div'
  7322. );
  7323. $this->assertTags($result, $expected);
  7324. $result = $this->Form->input('Contact.email', array(
  7325. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7326. 'between' => '<div>Something in the middle</div>',
  7327. 'after' => '<span>Some text at the end</span>'
  7328. ));
  7329. $expected = array(
  7330. 'div' => array('class' => 'input text'),
  7331. array('input' => array(
  7332. 'type' => 'text', 'name' => 'data[Contact][email]',
  7333. 'id' => 'ContactEmail'
  7334. )),
  7335. array('div' => array()),
  7336. 'Something in the middle',
  7337. '/div',
  7338. 'label' => array('for' => 'ContactEmail'),
  7339. 'Email',
  7340. '/label',
  7341. 'span' => array(),
  7342. 'Some text at the end',
  7343. '/span',
  7344. '/div'
  7345. );
  7346. $this->assertTags($result, $expected);
  7347. $result = $this->Form->input('Contact.method', array(
  7348. 'type' => 'radio',
  7349. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7350. 'between' => 'I am between',
  7351. ));
  7352. $expected = array(
  7353. 'div' => array('class' => 'input radio'),
  7354. 'fieldset' => array(),
  7355. 'legend' => array(),
  7356. 'Method',
  7357. '/legend',
  7358. 'I am between',
  7359. 'input' => array(
  7360. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  7361. 'value' => '', 'id' => 'ContactMethod_'
  7362. ),
  7363. array('input' => array(
  7364. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7365. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7366. )),
  7367. array('label' => array('for' => 'ContactMethodEmail')),
  7368. 'Email',
  7369. '/label',
  7370. array('input' => array(
  7371. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7372. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7373. )),
  7374. array('label' => array('for' => 'ContactMethodPigeon')),
  7375. 'Pigeon',
  7376. '/label',
  7377. '/fieldset',
  7378. '/div',
  7379. );
  7380. $this->assertTags($result, $expected);
  7381. }
  7382. /**
  7383. * test that some html5 inputs + FormHelper::__call() work
  7384. *
  7385. * @return void
  7386. */
  7387. public function testHtml5Inputs() {
  7388. $result = $this->Form->email('User.email');
  7389. $expected = array(
  7390. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  7391. );
  7392. $this->assertTags($result, $expected);
  7393. $result = $this->Form->search('User.query');
  7394. $expected = array(
  7395. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  7396. );
  7397. $this->assertTags($result, $expected);
  7398. $result = $this->Form->search('User.query', array('value' => 'test'));
  7399. $expected = array(
  7400. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7401. );
  7402. $this->assertTags($result, $expected);
  7403. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7404. $expected = array(
  7405. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7406. );
  7407. $this->assertTags($result, $expected);
  7408. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  7409. $expected = array(
  7410. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7411. );
  7412. $this->assertTags($result, $expected);
  7413. }
  7414. /**
  7415. *
  7416. * @expectedException CakeException
  7417. * @return void
  7418. */
  7419. public function testHtml5InputException() {
  7420. $this->Form->email();
  7421. }
  7422. /**
  7423. * Tests that a model can be loaded from the model names passed in the request object
  7424. *
  7425. * @return void
  7426. */
  7427. public function testIntrospectModelFromRequest() {
  7428. $this->loadFixtures('Post');
  7429. App::build(array(
  7430. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  7431. ));
  7432. CakePlugin::load('TestPlugin');
  7433. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  7434. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  7435. $this->Form->create('TestPluginPost');
  7436. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  7437. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  7438. CakePlugin::unload();
  7439. App::build();
  7440. }
  7441. /**
  7442. * Tests that it is possible to set the validation errors directly in the helper for a field
  7443. *
  7444. * @return void
  7445. */
  7446. public function testCustomValidationErrors() {
  7447. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7448. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7449. $this->assertEquals('Badness!', $result);
  7450. }
  7451. /**
  7452. * Tests that the 'on' key validates as expected on create
  7453. *
  7454. * @return void
  7455. */
  7456. public function testRequiredOnCreate() {
  7457. $this->Form->create('Contact');
  7458. $result = $this->Form->input('Contact.imrequiredonupdate');
  7459. $expected = array(
  7460. 'div' => array('class' => 'input text'),
  7461. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7462. 'Imrequiredonupdate',
  7463. '/label',
  7464. 'input' => array(
  7465. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7466. 'id' => 'ContactImrequiredonupdate'
  7467. ),
  7468. '/div'
  7469. );
  7470. $this->assertTags($result, $expected);
  7471. $result = $this->Form->input('Contact.imrequiredoncreate');
  7472. $expected = array(
  7473. 'div' => array('class' => 'input text required'),
  7474. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7475. 'Imrequiredoncreate',
  7476. '/label',
  7477. 'input' => array(
  7478. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7479. 'id' => 'ContactImrequiredoncreate'
  7480. ),
  7481. '/div'
  7482. );
  7483. $this->assertTags($result, $expected);
  7484. $result = $this->Form->input('Contact.imrequiredonboth');
  7485. $expected = array(
  7486. 'div' => array('class' => 'input text required'),
  7487. 'label' => array('for' => 'ContactImrequiredonboth'),
  7488. 'Imrequiredonboth',
  7489. '/label',
  7490. 'input' => array(
  7491. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7492. 'id' => 'ContactImrequiredonboth'
  7493. ),
  7494. '/div'
  7495. );
  7496. $this->assertTags($result, $expected);
  7497. $result = $this->Form->input('Contact.imrequired');
  7498. $expected = array(
  7499. 'div' => array('class' => 'input text required'),
  7500. 'label' => array('for' => 'ContactImrequired'),
  7501. 'Imrequired',
  7502. '/label',
  7503. 'input' => array(
  7504. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7505. 'id' => 'ContactImrequired'
  7506. ),
  7507. '/div'
  7508. );
  7509. $this->assertTags($result, $expected);
  7510. }
  7511. /**
  7512. * Tests that the 'on' key validates as expected on update
  7513. *
  7514. * @return void
  7515. */
  7516. public function testRequiredOnUpdate() {
  7517. $this->Form->request->data['Contact']['id'] = 1;
  7518. $this->Form->create('Contact');
  7519. $result = $this->Form->input('Contact.imrequiredonupdate');
  7520. $expected = array(
  7521. 'div' => array('class' => 'input text required'),
  7522. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7523. 'Imrequiredonupdate',
  7524. '/label',
  7525. 'input' => array(
  7526. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7527. 'id' => 'ContactImrequiredonupdate'
  7528. ),
  7529. '/div'
  7530. );
  7531. $this->assertTags($result, $expected);
  7532. $result = $this->Form->input('Contact.imrequiredoncreate');
  7533. $expected = array(
  7534. 'div' => array('class' => 'input text'),
  7535. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7536. 'Imrequiredoncreate',
  7537. '/label',
  7538. 'input' => array(
  7539. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7540. 'id' => 'ContactImrequiredoncreate'
  7541. ),
  7542. '/div'
  7543. );
  7544. $this->assertTags($result, $expected);
  7545. $result = $this->Form->input('Contact.imrequiredonboth');
  7546. $expected = array(
  7547. 'div' => array('class' => 'input text required'),
  7548. 'label' => array('for' => 'ContactImrequiredonboth'),
  7549. 'Imrequiredonboth',
  7550. '/label',
  7551. 'input' => array(
  7552. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7553. 'id' => 'ContactImrequiredonboth'
  7554. ),
  7555. '/div'
  7556. );
  7557. $this->assertTags($result, $expected);
  7558. $result = $this->Form->input('Contact.imrequired');
  7559. $expected = array(
  7560. 'div' => array('class' => 'input text required'),
  7561. 'label' => array('for' => 'ContactImrequired'),
  7562. 'Imrequired',
  7563. '/label',
  7564. 'input' => array(
  7565. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7566. 'id' => 'ContactImrequired'
  7567. ),
  7568. '/div'
  7569. );
  7570. $this->assertTags($result, $expected);
  7571. }
  7572. /**
  7573. * Test inputDefaults setter and getter
  7574. *
  7575. * @return void
  7576. */
  7577. public function testInputDefaults() {
  7578. $this->Form->create('Contact');
  7579. $this->Form->inputDefaults(array(
  7580. 'label' => false,
  7581. 'div' => array(
  7582. 'style' => 'color: #000;'
  7583. )
  7584. ));
  7585. $result = $this->Form->input('Contact.field1');
  7586. $expected = array(
  7587. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  7588. 'input' => array(
  7589. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7590. 'id' => 'ContactField1'
  7591. ),
  7592. '/div'
  7593. );
  7594. $this->assertTags($result, $expected);
  7595. $this->Form->inputDefaults(array(
  7596. 'div' => false,
  7597. 'label' => 'Label',
  7598. ));
  7599. $result = $this->Form->input('Contact.field1');
  7600. $expected = array(
  7601. 'label' => array('for' => 'ContactField1'),
  7602. 'Label',
  7603. '/label',
  7604. 'input' => array(
  7605. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7606. 'id' => 'ContactField1'
  7607. ),
  7608. );
  7609. $this->assertTags($result, $expected);
  7610. $this->Form->inputDefaults(array(
  7611. 'label' => false,
  7612. ), true);
  7613. $result = $this->Form->input('Contact.field1');
  7614. $expected = array(
  7615. 'input' => array(
  7616. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7617. 'id' => 'ContactField1'
  7618. ),
  7619. );
  7620. $this->assertTags($result, $expected);
  7621. $result = $this->Form->inputDefaults();
  7622. $expected = array(
  7623. 'div' => false,
  7624. 'label' => false,
  7625. );
  7626. $this->assertEqual($result, $expected);
  7627. }
  7628. }