FormHelperTest.php 254 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc.
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc.
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package Cake.Test.Case.View.Helper
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('ClassRegistry', 'Utility');
  20. App::uses('Controller', 'Controller');
  21. App::uses('View', 'View');
  22. App::uses('Model', 'Model');
  23. App::uses('Security', 'Utility');
  24. App::uses('CakeRequest', 'Network');
  25. App::uses('HtmlHelper', 'View/Helper');
  26. App::uses('FormHelper', 'View/Helper');
  27. App::uses('Router', 'Routing');
  28. /**
  29. * ContactTestController class
  30. *
  31. * @package cake
  32. * @package Cake.Test.Case.View.Helper
  33. */
  34. class ContactTestController extends Controller {
  35. /**
  36. * name property
  37. *
  38. * @var string 'ContactTest'
  39. */
  40. public $name = 'ContactTest';
  41. /**
  42. * uses property
  43. *
  44. * @var mixed null
  45. */
  46. public $uses = null;
  47. }
  48. /**
  49. * Contact class
  50. *
  51. * @package cake
  52. * @package Cake.Test.Case.View.Helper
  53. */
  54. class Contact extends CakeTestModel {
  55. /**
  56. * primaryKey property
  57. *
  58. * @var string 'id'
  59. */
  60. public $primaryKey = 'id';
  61. /**
  62. * useTable property
  63. *
  64. * @var bool false
  65. */
  66. public $useTable = false;
  67. /**
  68. * name property
  69. *
  70. * @var string 'Contact'
  71. */
  72. public $name = 'Contact';
  73. /**
  74. * Default schema
  75. *
  76. * @var array
  77. */
  78. protected $_schema = array(
  79. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  80. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  81. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  82. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  83. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  84. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  85. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  86. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  87. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  88. );
  89. /**
  90. * validate property
  91. *
  92. * @var array
  93. */
  94. public $validate = array(
  95. 'non_existing' => array(),
  96. 'idontexist' => array(),
  97. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  98. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  99. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  100. 'imrequiredonboth' => array(
  101. 'required' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  102. 'check' => array('rule' => 'alphaNumeric')
  103. ),
  104. 'string_required' => 'notEmpty',
  105. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  106. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  107. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  108. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  109. 'imalsonotrequired' => array(
  110. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  111. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  112. ),
  113. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  114. );
  115. /**
  116. * schema method
  117. *
  118. * @return void
  119. */
  120. public function setSchema($schema) {
  121. $this->_schema = $schema;
  122. }
  123. /**
  124. * hasAndBelongsToMany property
  125. *
  126. * @var array
  127. */
  128. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  129. /**
  130. * hasAndBelongsToMany property
  131. *
  132. * @var array
  133. */
  134. public $belongsTo = array('User' => array('className' => 'UserForm'));
  135. }
  136. /**
  137. * ContactTagsContact class
  138. *
  139. * @package cake
  140. * @package Cake.Test.Case.View.Helper
  141. */
  142. class ContactTagsContact extends CakeTestModel {
  143. /**
  144. * useTable property
  145. *
  146. * @var bool false
  147. */
  148. public $useTable = false;
  149. /**
  150. * name property
  151. *
  152. * @var string 'Contact'
  153. */
  154. public $name = 'ContactTagsContact';
  155. /**
  156. * Default schema
  157. *
  158. * @var array
  159. */
  160. protected $_schema = array(
  161. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  162. 'contact_tag_id' => array(
  163. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  164. )
  165. );
  166. /**
  167. * schema method
  168. *
  169. * @return void
  170. */
  171. public function setSchema($schema) {
  172. $this->_schema = $schema;
  173. }
  174. }
  175. /**
  176. * ContactNonStandardPk class
  177. *
  178. * @package cake
  179. * @package Cake.Test.Case.View.Helper
  180. */
  181. class ContactNonStandardPk extends Contact {
  182. /**
  183. * primaryKey property
  184. *
  185. * @var string 'pk'
  186. */
  187. public $primaryKey = 'pk';
  188. /**
  189. * name property
  190. *
  191. * @var string 'ContactNonStandardPk'
  192. */
  193. public $name = 'ContactNonStandardPk';
  194. /**
  195. * schema method
  196. *
  197. * @return void
  198. */
  199. public function schema($field = false) {
  200. $this->_schema = parent::schema();
  201. $this->_schema['pk'] = $this->_schema['id'];
  202. unset($this->_schema['id']);
  203. return $this->_schema;
  204. }
  205. }
  206. /**
  207. * ContactTag class
  208. *
  209. * @package cake
  210. * @package Cake.Test.Case.View.Helper
  211. */
  212. class ContactTag extends Model {
  213. /**
  214. * useTable property
  215. *
  216. * @var bool false
  217. */
  218. public $useTable = false;
  219. /**
  220. * schema definition
  221. *
  222. * @var array
  223. */
  224. protected $_schema = array(
  225. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  226. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  227. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  228. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  229. );
  230. }
  231. /**
  232. * UserForm class
  233. *
  234. * @package cake
  235. * @package Cake.Test.Case.View.Helper
  236. */
  237. class UserForm extends CakeTestModel {
  238. /**
  239. * useTable property
  240. *
  241. * @var bool false
  242. */
  243. public $useTable = false;
  244. /**
  245. * primaryKey property
  246. *
  247. * @var string 'id'
  248. */
  249. public $primaryKey = 'id';
  250. /**
  251. * name property
  252. *
  253. * @var string 'UserForm'
  254. */
  255. public $name = 'UserForm';
  256. /**
  257. * hasMany property
  258. *
  259. * @var array
  260. */
  261. public $hasMany = array(
  262. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  263. ));
  264. /**
  265. * schema definition
  266. *
  267. * @var array
  268. */
  269. protected $_schema = array(
  270. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  271. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  272. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  273. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  274. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  275. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  276. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  277. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  278. );
  279. }
  280. /**
  281. * OpenidUrl class
  282. *
  283. * @package cake
  284. * @package Cake.Test.Case.View.Helper
  285. */
  286. class OpenidUrl extends CakeTestModel {
  287. /**
  288. * useTable property
  289. *
  290. * @var bool false
  291. */
  292. public $useTable = false;
  293. /**
  294. * primaryKey property
  295. *
  296. * @var string 'id'
  297. */
  298. public $primaryKey = 'id';
  299. /**
  300. * name property
  301. *
  302. * @var string 'OpenidUrl'
  303. */
  304. public $name = 'OpenidUrl';
  305. /**
  306. * belongsTo property
  307. *
  308. * @var array
  309. */
  310. public $belongsTo = array('UserForm' => array(
  311. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  312. ));
  313. /**
  314. * validate property
  315. *
  316. * @var array
  317. */
  318. public $validate = array('openid_not_registered' => array());
  319. /**
  320. * schema method
  321. *
  322. * @var array
  323. */
  324. protected $_schema = array(
  325. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  326. 'user_form_id' => array(
  327. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  328. ),
  329. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  330. );
  331. /**
  332. * beforeValidate method
  333. *
  334. * @return void
  335. */
  336. public function beforeValidate($options = array()) {
  337. $this->invalidate('openid_not_registered');
  338. return true;
  339. }
  340. }
  341. /**
  342. * ValidateUser class
  343. *
  344. * @package cake
  345. * @package Cake.Test.Case.View.Helper
  346. */
  347. class ValidateUser extends CakeTestModel {
  348. /**
  349. * primaryKey property
  350. *
  351. * @var string 'id'
  352. */
  353. public $primaryKey = 'id';
  354. /**
  355. * useTable property
  356. *
  357. * @var bool false
  358. */
  359. public $useTable = false;
  360. /**
  361. * name property
  362. *
  363. * @var string 'ValidateUser'
  364. */
  365. public $name = 'ValidateUser';
  366. /**
  367. * hasOne property
  368. *
  369. * @var array
  370. */
  371. public $hasOne = array('ValidateProfile' => array(
  372. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  373. ));
  374. /**
  375. * schema method
  376. *
  377. * @var array
  378. */
  379. protected $_schema = array(
  380. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  381. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  382. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  383. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  384. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  385. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  386. );
  387. /**
  388. * beforeValidate method
  389. *
  390. * @return void
  391. */
  392. public function beforeValidate($options = array()) {
  393. $this->invalidate('email');
  394. return false;
  395. }
  396. }
  397. /**
  398. * ValidateProfile class
  399. *
  400. * @package cake
  401. * @package Cake.Test.Case.View.Helper
  402. */
  403. class ValidateProfile extends CakeTestModel {
  404. /**
  405. * primaryKey property
  406. *
  407. * @var string 'id'
  408. */
  409. public $primaryKey = 'id';
  410. /**
  411. * useTable property
  412. *
  413. * @var bool false
  414. */
  415. public $useTable = false;
  416. /**
  417. * schema property
  418. *
  419. * @var array
  420. */
  421. protected $_schema = array(
  422. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  423. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  424. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  425. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  426. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  427. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  428. );
  429. /**
  430. * name property
  431. *
  432. * @var string 'ValidateProfile'
  433. */
  434. public $name = 'ValidateProfile';
  435. /**
  436. * hasOne property
  437. *
  438. * @var array
  439. */
  440. public $hasOne = array('ValidateItem' => array(
  441. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  442. ));
  443. /**
  444. * belongsTo property
  445. *
  446. * @var array
  447. */
  448. public $belongsTo = array('ValidateUser' => array(
  449. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  450. ));
  451. /**
  452. * beforeValidate method
  453. *
  454. * @return void
  455. */
  456. public function beforeValidate($options = array()) {
  457. $this->invalidate('full_name');
  458. $this->invalidate('city');
  459. return false;
  460. }
  461. }
  462. /**
  463. * ValidateItem class
  464. *
  465. * @package cake
  466. * @package Cake.Test.Case.View.Helper
  467. */
  468. class ValidateItem extends CakeTestModel {
  469. /**
  470. * primaryKey property
  471. *
  472. * @var string 'id'
  473. */
  474. public $primaryKey = 'id';
  475. /**
  476. * useTable property
  477. *
  478. * @var bool false
  479. */
  480. public $useTable = false;
  481. /**
  482. * name property
  483. *
  484. * @var string 'ValidateItem'
  485. */
  486. public $name = 'ValidateItem';
  487. /**
  488. * schema property
  489. *
  490. * @var array
  491. */
  492. protected $_schema = array(
  493. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  494. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  495. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  496. 'description' => array(
  497. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  498. ),
  499. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  500. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  501. );
  502. /**
  503. * belongsTo property
  504. *
  505. * @var array
  506. */
  507. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  508. /**
  509. * beforeValidate method
  510. *
  511. * @return void
  512. */
  513. public function beforeValidate($options = array()) {
  514. $this->invalidate('description');
  515. return false;
  516. }
  517. }
  518. /**
  519. * TestMail class
  520. *
  521. * @package cake
  522. * @package Cake.Test.Case.View.Helper
  523. */
  524. class TestMail extends CakeTestModel {
  525. /**
  526. * primaryKey property
  527. *
  528. * @var string 'id'
  529. */
  530. public $primaryKey = 'id';
  531. /**
  532. * useTable property
  533. *
  534. * @var bool false
  535. */
  536. public $useTable = false;
  537. /**
  538. * name property
  539. *
  540. * @var string 'TestMail'
  541. */
  542. public $name = 'TestMail';
  543. }
  544. /**
  545. * FormHelperTest class
  546. *
  547. * @package cake
  548. * @subpackage Cake.Test.Case.View.Helper
  549. * @property FormHelper $Form
  550. */
  551. class FormHelperTest extends CakeTestCase {
  552. /**
  553. * Fixtures to be used
  554. *
  555. * @var array
  556. */
  557. public $fixtures = array('core.post');
  558. /**
  559. * Do not load the fixtures by default
  560. *
  561. * @var boolean
  562. */
  563. public $autoFixtures = false;
  564. /**
  565. * setUp method
  566. *
  567. * @return void
  568. */
  569. public function setUp() {
  570. parent::setUp();
  571. Configure::write('App.base', '');
  572. $this->Controller = new ContactTestController();
  573. $this->View = new View($this->Controller);
  574. $this->Form = new FormHelper($this->View);
  575. $this->Form->Html = new HtmlHelper($this->View);
  576. $this->Form->request = new CakeRequest('contacts/add', false);
  577. $this->Form->request->here = '/contacts/add';
  578. $this->Form->request['action'] = 'add';
  579. $this->Form->request->webroot = '';
  580. $this->Form->request->base = '';
  581. ClassRegistry::addObject('Contact', new Contact());
  582. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  583. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  584. ClassRegistry::addObject('User', new UserForm());
  585. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  586. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  587. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  588. $this->oldSalt = Configure::read('Security.salt');
  589. $this->dateRegex = array(
  590. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  591. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  592. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  593. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  594. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  595. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  596. );
  597. Configure::write('Security.salt', 'foo!');
  598. }
  599. /**
  600. * tearDown method
  601. *
  602. * @return void
  603. */
  604. public function tearDown() {
  605. parent::tearDown();
  606. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  607. Configure::write('Security.salt', $this->oldSalt);
  608. }
  609. /**
  610. * testFormCreateWithSecurity method
  611. *
  612. * Test form->create() with security key.
  613. *
  614. * @return void
  615. */
  616. public function testCreateWithSecurity() {
  617. $this->Form->request['_Token'] = array('key' => 'testKey');
  618. $encoding = strtolower(Configure::read('App.encoding'));
  619. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  620. $expected = array(
  621. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  622. 'div' => array('style' => 'display:none;'),
  623. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  624. array('input' => array(
  625. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  626. )),
  627. '/div'
  628. );
  629. $this->assertTags($result, $expected);
  630. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  631. $expected['form']['id'] = 'MyForm';
  632. $this->assertTags($result, $expected);
  633. }
  634. /**
  635. * test that create() clears the fields property so it starts fresh
  636. *
  637. * @return void
  638. */
  639. public function testCreateClearingFields() {
  640. $this->Form->fields = array('model_id');
  641. $this->Form->create('Contact');
  642. $this->assertEquals(array(), $this->Form->fields);
  643. }
  644. /**
  645. * Tests form hash generation with model-less data
  646. *
  647. * @return void
  648. */
  649. public function testValidateHashNoModel() {
  650. $this->Form->request['_Token'] = array('key' => 'foo');
  651. $result = $this->Form->secure(array('anything'));
  652. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  653. }
  654. /**
  655. * Tests that models with identical field names get resolved properly
  656. *
  657. * @return void
  658. */
  659. public function testDuplicateFieldNameResolution() {
  660. $result = $this->Form->create('ValidateUser');
  661. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  662. $result = $this->Form->input('ValidateItem.name');
  663. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  664. $result = $this->Form->input('ValidateUser.name');
  665. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  666. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  667. $this->assertRegExp('/type="text"/', $result);
  668. $result = $this->Form->input('ValidateItem.name');
  669. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  670. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  671. $this->assertRegExp('/<textarea/', $result);
  672. $result = $this->Form->input('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. }
  677. /**
  678. * Tests that hidden fields generated for checkboxes don't get locked
  679. *
  680. * @return void
  681. */
  682. public function testNoCheckboxLocking() {
  683. $this->Form->request['_Token'] = array('key' => 'foo');
  684. $this->assertSame(array(), $this->Form->fields);
  685. $this->Form->checkbox('check', array('value' => '1'));
  686. $this->assertSame($this->Form->fields, array('check'));
  687. }
  688. /**
  689. * testFormSecurityFields method
  690. *
  691. * Test generation of secure form hash generation.
  692. *
  693. * @return void
  694. */
  695. public function testFormSecurityFields() {
  696. $key = 'testKey';
  697. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  698. $this->Form->request['_Token'] = array('key' => $key);
  699. $result = $this->Form->secure($fields);
  700. $expected = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  701. $expected .= ':' . 'Model.valid';
  702. $expected = array(
  703. 'div' => array('style' => 'display:none;'),
  704. array('input' => array(
  705. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  706. 'value' => urlencode($expected), 'id' => 'preg:/TokenFields\d+/'
  707. )),
  708. array('input' => array(
  709. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  710. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  711. )),
  712. '/div'
  713. );
  714. $this->assertTags($result, $expected);
  715. }
  716. /**
  717. * Tests correct generation of number fields for double and float fields
  718. *
  719. * @return void
  720. */
  721. public function testTextFieldGenerationForFloats() {
  722. $model = ClassRegistry::getObject('Contact');
  723. $model->setSchema(array('foo' => array(
  724. 'type' => 'float',
  725. 'null' => false,
  726. 'default' => null,
  727. 'length' => null
  728. )));
  729. $this->Form->create('Contact');
  730. $result = $this->Form->input('foo');
  731. $expected = array(
  732. 'div' => array('class' => 'input number'),
  733. 'label' => array('for' => 'ContactFoo'),
  734. 'Foo',
  735. '/label',
  736. array('input' => array(
  737. 'type' => 'number',
  738. 'name' => 'data[Contact][foo]',
  739. 'id' => 'ContactFoo',
  740. 'step' => 'any'
  741. )),
  742. '/div'
  743. );
  744. $this->assertTags($result, $expected);
  745. $result = $this->Form->input('foo', array('step' => 0.5));
  746. $expected = array(
  747. 'div' => array('class' => 'input number'),
  748. 'label' => array('for' => 'ContactFoo'),
  749. 'Foo',
  750. '/label',
  751. array('input' => array(
  752. 'type' => 'number',
  753. 'name' => 'data[Contact][foo]',
  754. 'id' => 'ContactFoo',
  755. 'step' => '0.5'
  756. )),
  757. '/div'
  758. );
  759. $this->assertTags($result, $expected);
  760. }
  761. /**
  762. * Tests correct generation of number fields for integer fields
  763. *
  764. * @access public
  765. * @return void
  766. */
  767. public function testTextFieldTypeNumberGenerationForIntegers() {
  768. $model = ClassRegistry::getObject('Contact');
  769. $model->setSchema(array('foo' => array(
  770. 'type' => 'integer',
  771. 'null' => false,
  772. 'default' => null,
  773. 'length' => null
  774. )));
  775. $this->Form->create('Contact');
  776. $result = $this->Form->input('foo');
  777. $expected = array(
  778. 'div' => array('class' => 'input number'),
  779. 'label' => array('for' => 'ContactFoo'),
  780. 'Foo',
  781. '/label',
  782. array('input' => array(
  783. 'type' => 'number', 'name' => 'data[Contact][foo]',
  784. 'id' => 'ContactFoo'
  785. )),
  786. '/div'
  787. );
  788. $this->assertTags($result, $expected);
  789. }
  790. /**
  791. * testFormSecurityMultipleFields method
  792. *
  793. * Test secure() with multiple row form. Ensure hash is correct.
  794. *
  795. * @return void
  796. */
  797. public function testFormSecurityMultipleFields() {
  798. $key = 'testKey';
  799. $fields = array(
  800. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  801. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  802. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  803. );
  804. $this->Form->request['_Token'] = array('key' => $key);
  805. $result = $this->Form->secure($fields);
  806. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  807. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  808. $expected = array(
  809. 'div' => array('style' => 'display:none;'),
  810. array('input' => array(
  811. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  812. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  813. )),
  814. array('input' => array(
  815. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  816. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  817. )),
  818. '/div'
  819. );
  820. $this->assertTags($result, $expected);
  821. }
  822. /**
  823. * testFormSecurityMultipleSubmitButtons
  824. *
  825. * test form submit generation and ensure that _Token is only created on end()
  826. *
  827. * @return void
  828. */
  829. public function testFormSecurityMultipleSubmitButtons() {
  830. $key = 'testKey';
  831. $this->Form->request['_Token'] = array('key' => $key);
  832. $this->Form->create('Addresses');
  833. $this->Form->input('Address.title');
  834. $this->Form->input('Address.first_name');
  835. $result = $this->Form->submit('Save', array('name' => 'save'));
  836. $expected = array(
  837. 'div' => array('class' => 'submit'),
  838. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  839. '/div',
  840. );
  841. $this->assertTags($result, $expected);
  842. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  843. $expected = array(
  844. 'div' => array('class' => 'submit'),
  845. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  846. '/div',
  847. );
  848. $this->assertTags($result, $expected);
  849. $result = $this->Form->end(null);
  850. $expected = array(
  851. 'div' => array('style' => 'display:none;'),
  852. array('input' => array(
  853. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  854. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  855. )),
  856. array('input' => array(
  857. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  858. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  859. )),
  860. '/div'
  861. );
  862. $this->assertTags($result, $expected);
  863. }
  864. /**
  865. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  866. *
  867. * @return void
  868. */
  869. public function testSecurityButtonNestedNamed() {
  870. $key = 'testKey';
  871. $this->Form->request['_Token'] = array('key' => $key);
  872. $this->Form->create('Addresses');
  873. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  874. $result = $this->Form->unlockField();
  875. $this->assertEquals(array('Address.button'), $result);
  876. }
  877. /**
  878. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  879. *
  880. * @return void
  881. */
  882. public function testSecuritySubmitNestedNamed() {
  883. $key = 'testKey';
  884. $this->Form->request['_Token'] = array('key' => $key);
  885. $this->Form->create('Addresses');
  886. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  887. $result = $this->Form->unlockField();
  888. $this->assertEquals(array('Address.button'), $result);
  889. }
  890. /**
  891. * Test that the correct fields are unlocked for image submits with no names.
  892. *
  893. * @return void
  894. */
  895. public function testSecuritySubmitImageNoName() {
  896. $key = 'testKey';
  897. $this->Form->request['_Token'] = array('key' => $key);
  898. $this->Form->create('User');
  899. $result = $this->Form->submit('save.png');
  900. $expected = array(
  901. 'div' => array('class' => 'submit'),
  902. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  903. '/div'
  904. );
  905. $this->assertTags($result, $expected);
  906. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  907. }
  908. /**
  909. * Test that the correct fields are unlocked for image submits with names.
  910. *
  911. * @return void
  912. */
  913. public function testSecuritySubmitImageName() {
  914. $key = 'testKey';
  915. $this->Form->request['_Token'] = array('key' => $key);
  916. $this->Form->create('User');
  917. $result = $this->Form->submit('save.png', array('name' => 'test'));
  918. $expected = array(
  919. 'div' => array('class' => 'submit'),
  920. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  921. '/div'
  922. );
  923. $this->assertTags($result, $expected);
  924. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  925. }
  926. /**
  927. * testFormSecurityMultipleInputFields method
  928. *
  929. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  930. *
  931. * @return void
  932. */
  933. public function testFormSecurityMultipleInputFields() {
  934. $key = 'testKey';
  935. $this->Form->request['_Token'] = array('key' => $key);
  936. $this->Form->create('Addresses');
  937. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  938. $this->Form->input('Addresses.0.title');
  939. $this->Form->input('Addresses.0.first_name');
  940. $this->Form->input('Addresses.0.last_name');
  941. $this->Form->input('Addresses.0.address');
  942. $this->Form->input('Addresses.0.city');
  943. $this->Form->input('Addresses.0.phone');
  944. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  945. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  946. $this->Form->input('Addresses.1.title');
  947. $this->Form->input('Addresses.1.first_name');
  948. $this->Form->input('Addresses.1.last_name');
  949. $this->Form->input('Addresses.1.address');
  950. $this->Form->input('Addresses.1.city');
  951. $this->Form->input('Addresses.1.phone');
  952. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  953. $result = $this->Form->secure($this->Form->fields);
  954. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  955. $expected = array(
  956. 'div' => array('style' => 'display:none;'),
  957. array('input' => array(
  958. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  959. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  960. )),
  961. array('input' => array(
  962. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  963. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  964. )),
  965. '/div'
  966. );
  967. $this->assertTags($result, $expected);
  968. }
  969. /**
  970. * Test form security with Model.field.0 style inputs
  971. *
  972. * @return void
  973. */
  974. public function testFormSecurityArrayFields() {
  975. $key = 'testKey';
  976. $this->Form->request->params['_Token']['key'] = $key;
  977. $this->Form->create('Address');
  978. $this->Form->input('Address.primary.1');
  979. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  980. $this->Form->input('Address.secondary.1.0');
  981. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  982. }
  983. /**
  984. * testFormSecurityMultipleInputDisabledFields method
  985. *
  986. * test secure form generation with multiple records and disabled fields.
  987. *
  988. * @return void
  989. */
  990. public function testFormSecurityMultipleInputDisabledFields() {
  991. $key = 'testKey';
  992. $this->Form->request->params['_Token'] = array(
  993. 'key' => $key,
  994. 'unlockedFields' => array('first_name', 'address')
  995. );
  996. $this->Form->create();
  997. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  998. $this->Form->input('Addresses.0.title');
  999. $this->Form->input('Addresses.0.first_name');
  1000. $this->Form->input('Addresses.0.last_name');
  1001. $this->Form->input('Addresses.0.address');
  1002. $this->Form->input('Addresses.0.city');
  1003. $this->Form->input('Addresses.0.phone');
  1004. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1005. $this->Form->input('Addresses.1.title');
  1006. $this->Form->input('Addresses.1.first_name');
  1007. $this->Form->input('Addresses.1.last_name');
  1008. $this->Form->input('Addresses.1.address');
  1009. $this->Form->input('Addresses.1.city');
  1010. $this->Form->input('Addresses.1.phone');
  1011. $result = $this->Form->secure($this->Form->fields);
  1012. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  1013. $expected = array(
  1014. 'div' => array('style' => 'display:none;'),
  1015. array('input' => array(
  1016. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1017. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1018. )),
  1019. array('input' => array(
  1020. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1021. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1022. )),
  1023. '/div'
  1024. );
  1025. $this->assertTags($result, $expected);
  1026. }
  1027. /**
  1028. * testFormSecurityInputDisabledFields method
  1029. *
  1030. * Test single record form with disabled fields.
  1031. *
  1032. * @return void
  1033. */
  1034. public function testFormSecurityInputUnlockedFields() {
  1035. $key = 'testKey';
  1036. $this->Form->request['_Token'] = array(
  1037. 'key' => $key,
  1038. 'unlockedFields' => array('first_name', 'address')
  1039. );
  1040. $this->Form->create();
  1041. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1042. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1043. $this->Form->input('Addresses.title');
  1044. $this->Form->input('Addresses.first_name');
  1045. $this->Form->input('Addresses.last_name');
  1046. $this->Form->input('Addresses.address');
  1047. $this->Form->input('Addresses.city');
  1048. $this->Form->input('Addresses.phone');
  1049. $result = $this->Form->fields;
  1050. $expected = array(
  1051. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1052. 'Addresses.city', 'Addresses.phone'
  1053. );
  1054. $this->assertEquals($expected, $result);
  1055. $result = $this->Form->secure($expected);
  1056. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1057. $expected = array(
  1058. 'div' => array('style' => 'display:none;'),
  1059. array('input' => array(
  1060. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1061. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1062. )),
  1063. array('input' => array(
  1064. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1065. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1066. )),
  1067. '/div'
  1068. );
  1069. $this->assertTags($result, $expected);
  1070. }
  1071. /**
  1072. * test securing inputs with custom name attributes.
  1073. *
  1074. * @return void
  1075. */
  1076. public function testFormSecureWithCustomNameAttribute() {
  1077. $this->Form->request->params['_Token']['key'] = 'testKey';
  1078. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1079. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1080. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1081. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1082. }
  1083. /**
  1084. * testFormSecuredInput method
  1085. *
  1086. * Test generation of entire secure form, assertions made on input() output.
  1087. *
  1088. * @return void
  1089. */
  1090. public function testFormSecuredInput() {
  1091. $this->Form->request['_Token'] = array('key' => 'testKey');
  1092. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1093. $encoding = strtolower(Configure::read('App.encoding'));
  1094. $expected = array(
  1095. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1096. 'div' => array('style' => 'display:none;'),
  1097. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1098. array('input' => array(
  1099. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1100. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1101. )),
  1102. '/div'
  1103. );
  1104. $this->assertTags($result, $expected);
  1105. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1106. $expected = array(
  1107. 'div' => array('class' => 'input text'),
  1108. 'label' => array('for' => 'UserFormPublished'),
  1109. 'Published',
  1110. '/label',
  1111. array('input' => array(
  1112. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1113. 'id' => 'UserFormPublished'
  1114. )),
  1115. '/div'
  1116. );
  1117. $this->assertTags($result, $expected);
  1118. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1119. $expected = array(
  1120. 'div' => array('class' => 'input text'),
  1121. 'label' => array('for' => 'UserFormOther'),
  1122. 'Other',
  1123. '/label',
  1124. array('input' => array(
  1125. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1126. 'id' => 'UserFormOther'
  1127. )),
  1128. '/div'
  1129. );
  1130. $this->assertTags($result, $expected);
  1131. $result = $this->Form->hidden('UserForm.stuff');
  1132. $expected = array('input' => array(
  1133. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1134. 'id' => 'UserFormStuff'
  1135. ));
  1136. $this->assertTags($result, $expected);
  1137. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1138. $expected = array('input' => array(
  1139. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1140. 'value' => '0', 'id' => 'UserFormHidden'
  1141. ));
  1142. $this->assertTags($result, $expected);
  1143. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1144. $expected = array(
  1145. 'div' => array('class' => 'input checkbox'),
  1146. array('input' => array(
  1147. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1148. 'value' => '0', 'id' => 'UserFormSomething_'
  1149. )),
  1150. array('input' => array(
  1151. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1152. 'value' => '1', 'id' => 'UserFormSomething'
  1153. )),
  1154. 'label' => array('for' => 'UserFormSomething'),
  1155. 'Something',
  1156. '/label',
  1157. '/div'
  1158. );
  1159. $this->assertTags($result, $expected);
  1160. $result = $this->Form->fields;
  1161. $expected = array(
  1162. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1163. 'UserForm.hidden' => '0', 'UserForm.something'
  1164. );
  1165. $this->assertEquals($expected, $result);
  1166. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1167. $result = $this->Form->secure($this->Form->fields);
  1168. $expected = array(
  1169. 'div' => array('style' => 'display:none;'),
  1170. array('input' => array(
  1171. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1172. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1173. )),
  1174. array('input' => array(
  1175. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1176. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1177. )),
  1178. '/div'
  1179. );
  1180. $this->assertTags($result, $expected);
  1181. }
  1182. /**
  1183. * Tests that the correct keys are added to the field hash index
  1184. *
  1185. * @return void
  1186. */
  1187. public function testFormSecuredFileInput() {
  1188. $this->Form->request['_Token'] = array('key' => 'testKey');
  1189. $this->assertEquals(array(), $this->Form->fields);
  1190. $result = $this->Form->file('Attachment.file');
  1191. $expected = array(
  1192. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1193. 'Attachment.file.error', 'Attachment.file.size'
  1194. );
  1195. $this->assertEquals($expected, $this->Form->fields);
  1196. }
  1197. /**
  1198. * test that multiple selects keys are added to field hash
  1199. *
  1200. * @return void
  1201. */
  1202. public function testFormSecuredMultipleSelect() {
  1203. $this->Form->request['_Token'] = array('key' => 'testKey');
  1204. $this->assertEquals(array(), $this->Form->fields);
  1205. $options = array('1' => 'one', '2' => 'two');
  1206. $this->Form->select('Model.select', $options);
  1207. $expected = array('Model.select');
  1208. $this->assertEquals($expected, $this->Form->fields);
  1209. $this->Form->fields = array();
  1210. $this->Form->select('Model.select', $options, array('multiple' => true));
  1211. $this->assertEquals($expected, $this->Form->fields);
  1212. }
  1213. /**
  1214. * testFormSecuredRadio method
  1215. *
  1216. * @return void
  1217. */
  1218. public function testFormSecuredRadio() {
  1219. $this->Form->request['_Token'] = array('key' => 'testKey');
  1220. $this->assertEquals(array(), $this->Form->fields);
  1221. $options = array('1' => 'option1', '2' => 'option2');
  1222. $this->Form->radio('Test.test', $options);
  1223. $expected = array('Test.test');
  1224. $this->assertEquals($expected, $this->Form->fields);
  1225. }
  1226. /**
  1227. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1228. * hashing.
  1229. *
  1230. * @return void
  1231. */
  1232. public function testFormSecuredAndDisabled() {
  1233. $this->Form->request['_Token'] = array('key' => 'testKey');
  1234. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1235. $this->Form->text('Model.text', array('disabled' => true));
  1236. $this->Form->password('Model.text', array('disabled' => true));
  1237. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1238. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1239. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1240. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1241. $this->Form->month('Model.month', array('disabled' => true));
  1242. $this->Form->day('Model.day', array('disabled' => true));
  1243. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1244. $this->Form->minute('Model.minute', array('disabled' => true));
  1245. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1246. $expected = array(
  1247. 'Model.radio' => ''
  1248. );
  1249. $this->assertEquals($expected, $this->Form->fields);
  1250. }
  1251. /**
  1252. * testDisableSecurityUsingForm method
  1253. *
  1254. * @return void
  1255. */
  1256. public function testDisableSecurityUsingForm() {
  1257. $this->Form->request['_Token'] = array(
  1258. 'key' => 'testKey',
  1259. 'disabledFields' => array()
  1260. );
  1261. $this->Form->create();
  1262. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1263. $this->Form->input('Addresses.title');
  1264. $this->Form->input('Addresses.first_name', array('secure' => false));
  1265. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1266. $this->Form->input('Addresses.zip', array(
  1267. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1268. ));
  1269. $result = $this->Form->fields;
  1270. $expected = array(
  1271. 'Addresses.id' => '123456', 'Addresses.title',
  1272. );
  1273. $this->assertEquals($expected, $result);
  1274. }
  1275. /**
  1276. * test disableField
  1277. *
  1278. * @return void
  1279. */
  1280. public function testUnlockFieldAddsToList() {
  1281. $this->Form->request['_Token'] = array(
  1282. 'key' => 'testKey',
  1283. 'unlockedFields' => array()
  1284. );
  1285. $this->Form->create('Contact');
  1286. $this->Form->unlockField('Contact.name');
  1287. $this->Form->text('Contact.name');
  1288. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1289. $this->assertEquals(array(), $this->Form->fields);
  1290. }
  1291. /**
  1292. * test unlockField removing from fields array.
  1293. *
  1294. * @return void
  1295. */
  1296. public function testUnlockFieldRemovingFromFields() {
  1297. $this->Form->request['_Token'] = array(
  1298. 'key' => 'testKey',
  1299. 'unlockedFields' => array()
  1300. );
  1301. $this->Form->create('Contact');
  1302. $this->Form->hidden('Contact.id', array('value' => 1));
  1303. $this->Form->text('Contact.name');
  1304. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1305. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1306. $this->Form->unlockField('Contact.name');
  1307. $this->Form->unlockField('Contact.id');
  1308. $this->assertEquals(array(), $this->Form->fields);
  1309. }
  1310. /**
  1311. * testTagIsInvalid method
  1312. *
  1313. * @return void
  1314. */
  1315. public function testTagIsInvalid() {
  1316. $Contact = ClassRegistry::getObject('Contact');
  1317. $Contact->validationErrors[0]['email'] = array('Please provide an email');
  1318. $this->Form->setEntity('Contact.0.email');
  1319. $result = $this->Form->tagIsInvalid();
  1320. $expected = array('Please provide an email');
  1321. $this->assertEquals($expected, $result);
  1322. $this->Form->setEntity('Contact.1.email');
  1323. $result = $this->Form->tagIsInvalid();
  1324. $expected = false;
  1325. $this->assertSame($expected, $result);
  1326. $this->Form->setEntity('Contact.0.name');
  1327. $result = $this->Form->tagIsInvalid();
  1328. $expected = false;
  1329. $this->assertSame($expected, $result);
  1330. }
  1331. /**
  1332. * testPasswordValidation method
  1333. *
  1334. * test validation errors on password input.
  1335. *
  1336. * @return void
  1337. */
  1338. public function testPasswordValidation() {
  1339. $Contact = ClassRegistry::getObject('Contact');
  1340. $Contact->validationErrors['password'] = array('Please provide a password');
  1341. $result = $this->Form->input('Contact.password');
  1342. $expected = array(
  1343. 'div' => array('class' => 'input password error'),
  1344. 'label' => array('for' => 'ContactPassword'),
  1345. 'Password',
  1346. '/label',
  1347. 'input' => array(
  1348. 'type' => 'password', 'name' => 'data[Contact][password]',
  1349. 'id' => 'ContactPassword', 'class' => 'form-error'
  1350. ),
  1351. array('div' => array('class' => 'error-message')),
  1352. 'Please provide a password',
  1353. '/div',
  1354. '/div'
  1355. );
  1356. $this->assertTags($result, $expected);
  1357. }
  1358. /**
  1359. * testEmptyErrorValidation method
  1360. *
  1361. * test validation error div when validation message is an empty string
  1362. *
  1363. * @access public
  1364. * @return void
  1365. */
  1366. public function testEmptyErrorValidation() {
  1367. $this->Form->validationErrors['Contact']['password'] = '';
  1368. $result = $this->Form->input('Contact.password');
  1369. $expected = array(
  1370. 'div' => array('class' => 'input password error'),
  1371. 'label' => array('for' => 'ContactPassword'),
  1372. 'Password',
  1373. '/label',
  1374. 'input' => array(
  1375. 'type' => 'password', 'name' => 'data[Contact][password]',
  1376. 'id' => 'ContactPassword', 'class' => 'form-error'
  1377. ),
  1378. array('div' => array('class' => 'error-message')),
  1379. array(),
  1380. '/div',
  1381. '/div'
  1382. );
  1383. $this->assertTags($result, $expected);
  1384. }
  1385. /**
  1386. * testEmptyInputErrorValidation method
  1387. *
  1388. * test validation error div when validation message is overridden by an empty string when calling input()
  1389. *
  1390. * @access public
  1391. * @return void
  1392. */
  1393. public function testEmptyInputErrorValidation() {
  1394. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1395. $result = $this->Form->input('Contact.password', array('error' => ''));
  1396. $expected = array(
  1397. 'div' => array('class' => 'input password error'),
  1398. 'label' => array('for' => 'ContactPassword'),
  1399. 'Password',
  1400. '/label',
  1401. 'input' => array(
  1402. 'type' => 'password', 'name' => 'data[Contact][password]',
  1403. 'id' => 'ContactPassword', 'class' => 'form-error'
  1404. ),
  1405. array('div' => array('class' => 'error-message')),
  1406. array(),
  1407. '/div',
  1408. '/div'
  1409. );
  1410. $this->assertTags($result, $expected);
  1411. }
  1412. /**
  1413. * testFormValidationAssociated method
  1414. *
  1415. * test display of form errors in conjunction with model::validates.
  1416. *
  1417. * @return void
  1418. */
  1419. public function testFormValidationAssociated() {
  1420. $this->UserForm = ClassRegistry::getObject('UserForm');
  1421. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1422. $data = array(
  1423. 'UserForm' => array('name' => 'user'),
  1424. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1425. );
  1426. $result = $this->UserForm->OpenidUrl->create($data);
  1427. $this->assertFalse(empty($result));
  1428. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1429. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1430. $encoding = strtolower(Configure::read('App.encoding'));
  1431. $expected = array(
  1432. 'form' => array(
  1433. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1434. 'accept-charset' => $encoding
  1435. ),
  1436. 'div' => array('style' => 'display:none;'),
  1437. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1438. '/div'
  1439. );
  1440. $this->assertTags($result, $expected);
  1441. $result = $this->Form->error(
  1442. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1443. );
  1444. $this->assertEquals('Error, not registered', $result);
  1445. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1446. }
  1447. /**
  1448. * testFormValidationAssociatedFirstLevel method
  1449. *
  1450. * test form error display with associated model.
  1451. *
  1452. * @return void
  1453. */
  1454. public function testFormValidationAssociatedFirstLevel() {
  1455. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1456. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1457. $data = array(
  1458. 'ValidateUser' => array('name' => 'mariano'),
  1459. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1460. );
  1461. $result = $this->ValidateUser->create($data);
  1462. $this->assertFalse(empty($result));
  1463. $this->assertFalse($this->ValidateUser->validates());
  1464. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1465. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1466. $encoding = strtolower(Configure::read('App.encoding'));
  1467. $expected = array(
  1468. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1469. 'div' => array('style' => 'display:none;'),
  1470. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1471. '/div'
  1472. );
  1473. $this->assertTags($result, $expected);
  1474. $result = $this->Form->error(
  1475. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1476. );
  1477. $this->assertEquals('Invalid email', $result);
  1478. $result = $this->Form->error(
  1479. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1480. );
  1481. $this->assertEquals('Invalid name', $result);
  1482. $result = $this->Form->error(
  1483. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1484. );
  1485. $this->assertEquals('Invalid city', $result);
  1486. unset($this->ValidateUser->ValidateProfile);
  1487. unset($this->ValidateUser);
  1488. }
  1489. /**
  1490. * testFormValidationAssociatedSecondLevel method
  1491. *
  1492. * test form error display with associated model.
  1493. *
  1494. * @return void
  1495. */
  1496. public function testFormValidationAssociatedSecondLevel() {
  1497. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1498. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1499. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1500. $data = array(
  1501. 'ValidateUser' => array('name' => 'mariano'),
  1502. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1503. 'ValidateItem' => array('name' => 'Item')
  1504. );
  1505. $result = $this->ValidateUser->create($data);
  1506. $this->assertFalse(empty($result));
  1507. $this->assertFalse($this->ValidateUser->validates());
  1508. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1509. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1510. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1511. $encoding = strtolower(Configure::read('App.encoding'));
  1512. $expected = array(
  1513. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1514. 'div' => array('style' => 'display:none;'),
  1515. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1516. '/div'
  1517. );
  1518. $this->assertTags($result, $expected);
  1519. $result = $this->Form->error(
  1520. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1521. );
  1522. $this->assertEquals('Invalid email', $result);
  1523. $result = $this->Form->error(
  1524. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1525. );
  1526. $this->assertEquals('Invalid name', $result);
  1527. $result = $this->Form->error(
  1528. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1529. );
  1530. $result = $this->Form->error(
  1531. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1532. );
  1533. $this->assertEquals('Invalid description', $result);
  1534. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1535. unset($this->ValidateUser->ValidateProfile);
  1536. unset($this->ValidateUser);
  1537. }
  1538. /**
  1539. * testFormValidationMultiRecord method
  1540. *
  1541. * test form error display with multiple records.
  1542. *
  1543. * @return void
  1544. */
  1545. public function testFormValidationMultiRecord() {
  1546. $Contact = ClassRegistry::getObject('Contact');
  1547. $Contact->validationErrors[2] = array(
  1548. 'name' => array('This field cannot be left blank')
  1549. );
  1550. $result = $this->Form->input('Contact.2.name');
  1551. $expected = array(
  1552. 'div' => array('class' => 'input text error'),
  1553. 'label' => array('for' => 'Contact2Name'),
  1554. 'Name',
  1555. '/label',
  1556. 'input' => array(
  1557. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1558. 'class' => 'form-error', 'maxlength' => 255
  1559. ),
  1560. array('div' => array('class' => 'error-message')),
  1561. 'This field cannot be left blank',
  1562. '/div',
  1563. '/div'
  1564. );
  1565. $this->assertTags($result, $expected);
  1566. }
  1567. /**
  1568. * testMultipleInputValidation method
  1569. *
  1570. * test multiple record form validation error display.
  1571. *
  1572. * @return void
  1573. */
  1574. public function testMultipleInputValidation() {
  1575. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1576. $Address->validationErrors[0] = array(
  1577. 'title' => array('This field cannot be empty'),
  1578. 'first_name' => array('This field cannot be empty')
  1579. );
  1580. $Address->validationErrors[1] = array(
  1581. 'last_name' => array('You must have a last name')
  1582. );
  1583. $this->Form->create();
  1584. $result = $this->Form->input('Address.0.title');
  1585. $expected = array(
  1586. 'div' => array('class'),
  1587. 'label' => array('for'),
  1588. 'preg:/[^<]+/',
  1589. '/label',
  1590. 'input' => array(
  1591. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1592. ),
  1593. array('div' => array('class' => 'error-message')),
  1594. 'This field cannot be empty',
  1595. '/div',
  1596. '/div'
  1597. );
  1598. $this->assertTags($result, $expected);
  1599. $result = $this->Form->input('Address.0.first_name');
  1600. $expected = array(
  1601. 'div' => array('class'),
  1602. 'label' => array('for'),
  1603. 'preg:/[^<]+/',
  1604. '/label',
  1605. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1606. array('div' => array('class' => 'error-message')),
  1607. 'This field cannot be empty',
  1608. '/div',
  1609. '/div'
  1610. );
  1611. $this->assertTags($result, $expected);
  1612. $result = $this->Form->input('Address.0.last_name');
  1613. $expected = array(
  1614. 'div' => array('class'),
  1615. 'label' => array('for'),
  1616. 'preg:/[^<]+/',
  1617. '/label',
  1618. 'input' => array('type' => 'text', 'name', 'id'),
  1619. '/div'
  1620. );
  1621. $this->assertTags($result, $expected);
  1622. $result = $this->Form->input('Address.1.last_name');
  1623. $expected = array(
  1624. 'div' => array('class'),
  1625. 'label' => array('for'),
  1626. 'preg:/[^<]+/',
  1627. '/label',
  1628. 'input' => array(
  1629. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1630. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1631. ),
  1632. array('div' => array('class' => 'error-message')),
  1633. 'You must have a last name',
  1634. '/div',
  1635. '/div'
  1636. );
  1637. $this->assertTags($result, $expected);
  1638. }
  1639. /**
  1640. * testInput method
  1641. *
  1642. * Test various incarnations of input().
  1643. *
  1644. * @return void
  1645. */
  1646. public function testInput() {
  1647. $result = $this->Form->input('ValidateUser.balance');
  1648. $expected = array(
  1649. 'div' => array('class'),
  1650. 'label' => array('for'),
  1651. 'Balance',
  1652. '/label',
  1653. 'input' => array('name', 'type' => 'number', 'maxlength' => 8, 'id'),
  1654. '/div',
  1655. );
  1656. $this->assertTags($result, $expected);
  1657. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1658. $expected = array(
  1659. 'div' => array('class' => 'input text'),
  1660. 'label' => array('for' => 'custom'),
  1661. 'Email',
  1662. '/label',
  1663. array('input' => array(
  1664. 'type' => 'text', 'name' => 'data[Contact][email]',
  1665. 'id' => 'custom', 'maxlength' => 255
  1666. )),
  1667. '/div'
  1668. );
  1669. $this->assertTags($result, $expected);
  1670. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1671. $expected = array(
  1672. '<div',
  1673. 'label' => array('for' => 'ContactEmail'),
  1674. 'Email',
  1675. '/label',
  1676. array('input' => array(
  1677. 'type' => 'text', 'name' => 'data[Contact][email]',
  1678. 'id' => 'ContactEmail', 'maxlength' => 255
  1679. )),
  1680. '/div'
  1681. );
  1682. $this->assertTags($result, $expected);
  1683. $result = $this->Form->hidden('Contact.idontexist');
  1684. $expected = array('input' => array(
  1685. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1686. 'id' => 'ContactIdontexist'
  1687. ));
  1688. $this->assertTags($result, $expected);
  1689. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1690. $expected = array(
  1691. 'div' => array('class' => 'input text'),
  1692. 'label' => array('for' => 'ContactEmail'),
  1693. 'Email',
  1694. '/label',
  1695. array('input' => array(
  1696. 'type' => 'text', 'name' => 'data[Contact][email]',
  1697. 'id' => 'ContactEmail'
  1698. )),
  1699. '/div'
  1700. );
  1701. $this->assertTags($result, $expected);
  1702. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1703. $expected = array(
  1704. 'div' => array('class' => 'input text'),
  1705. 'label' => array('for' => 'Contact5Email'),
  1706. 'Email',
  1707. '/label',
  1708. array('input' => array(
  1709. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1710. 'id' => 'Contact5Email'
  1711. )),
  1712. '/div'
  1713. );
  1714. $this->assertTags($result, $expected);
  1715. $result = $this->Form->input('Contact.password');
  1716. $expected = array(
  1717. 'div' => array('class' => 'input password'),
  1718. 'label' => array('for' => 'ContactPassword'),
  1719. 'Password',
  1720. '/label',
  1721. array('input' => array(
  1722. 'type' => 'password', 'name' => 'data[Contact][password]',
  1723. 'id' => 'ContactPassword'
  1724. )),
  1725. '/div'
  1726. );
  1727. $this->assertTags($result, $expected);
  1728. $result = $this->Form->input('Contact.email', array(
  1729. 'type' => 'file', 'class' => 'textbox'
  1730. ));
  1731. $expected = array(
  1732. 'div' => array('class' => 'input file'),
  1733. 'label' => array('for' => 'ContactEmail'),
  1734. 'Email',
  1735. '/label',
  1736. array('input' => array(
  1737. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1738. 'id' => 'ContactEmail'
  1739. )),
  1740. '/div'
  1741. );
  1742. $this->assertTags($result, $expected);
  1743. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1744. $result = $this->Form->input('Contact.phone');
  1745. $expected = array(
  1746. 'div' => array('class' => 'input text'),
  1747. 'label' => array('for' => 'ContactPhone'),
  1748. 'Phone',
  1749. '/label',
  1750. array('input' => array(
  1751. 'type' => 'text', 'name' => 'data[Contact][phone]',
  1752. 'value' => 'Hello &amp; World &gt; weird chars',
  1753. 'id' => 'ContactPhone', 'maxlength' => 255
  1754. )),
  1755. '/div'
  1756. );
  1757. $this->assertTags($result, $expected);
  1758. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1759. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1760. $expected = array(
  1761. 'div' => array('class' => 'input text'),
  1762. 'label' => array('for' => 'myId'),
  1763. 'Field',
  1764. '/label',
  1765. 'input' => array(
  1766. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1767. 'value' => 'My value', 'id' => 'myId'
  1768. ),
  1769. '/div'
  1770. );
  1771. $this->assertTags($result, $expected);
  1772. unset($this->Form->request->data);
  1773. $Contact = ClassRegistry::getObject('Contact');
  1774. $Contact->validationErrors['field'] = array('Badness!');
  1775. $result = $this->Form->input('Contact.field');
  1776. $expected = array(
  1777. 'div' => array('class' => 'input text error'),
  1778. 'label' => array('for' => 'ContactField'),
  1779. 'Field',
  1780. '/label',
  1781. 'input' => array(
  1782. 'type' => 'text', 'name' => 'data[Contact][field]',
  1783. 'id' => 'ContactField', 'class' => 'form-error'
  1784. ),
  1785. array('div' => array('class' => 'error-message')),
  1786. 'Badness!',
  1787. '/div',
  1788. '/div'
  1789. );
  1790. $this->assertTags($result, $expected);
  1791. $result = $this->Form->input('Contact.field', array(
  1792. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1793. ));
  1794. $expected = array(
  1795. 'label' => array('for' => 'ContactField'),
  1796. 'Field',
  1797. '/label',
  1798. 'input' => array(
  1799. 'type' => 'text', 'name' => 'data[Contact][field]',
  1800. 'id' => 'ContactField', 'class' => 'form-error'
  1801. ),
  1802. array('span' => array('class' => 'error-message')),
  1803. 'Badness!',
  1804. '/span'
  1805. );
  1806. $this->assertTags($result, $expected);
  1807. $result = $this->Form->input('Contact.field', array(
  1808. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1809. ));
  1810. $expected = array(
  1811. 'div' => array('class' => 'input text error'),
  1812. 'label' => array('for' => 'ContactField'),
  1813. 'Field',
  1814. '/label',
  1815. 'input' => array(
  1816. 'type' => 'text', 'name' => 'data[Contact][field]',
  1817. 'id' => 'ContactField', 'class' => 'form-error'
  1818. ),
  1819. array('div' => array('class' => 'error')),
  1820. 'Badness!',
  1821. '/div'
  1822. );
  1823. $this->assertTags($result, $expected);
  1824. $result = $this->Form->input('Contact.field', array(
  1825. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1826. ));
  1827. $expected = array(
  1828. 'span' => array('class' => 'input text error'),
  1829. 'label' => array('for' => 'ContactField'),
  1830. 'Field',
  1831. '/label',
  1832. 'input' => array(
  1833. 'type' => 'text', 'name' => 'data[Contact][field]',
  1834. 'id' => 'ContactField', 'class' => 'form-error'
  1835. ),
  1836. 'Badness!',
  1837. '/span'
  1838. );
  1839. $this->assertTags($result, $expected);
  1840. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1841. $expected = array(
  1842. 'div' => array('class' => 'input text error'),
  1843. 'label' => array('for' => 'ContactField'),
  1844. 'Field',
  1845. '/label',
  1846. 'input' => array(
  1847. 'type' => 'text', 'name' => 'data[Contact][field]',
  1848. 'id' => 'ContactField', 'class' => 'form-error'
  1849. ),
  1850. 'A message to you, Rudy',
  1851. array('div' => array('class' => 'error-message')),
  1852. 'Badness!',
  1853. '/div',
  1854. '/div'
  1855. );
  1856. $this->assertTags($result, $expected);
  1857. $this->Form->setEntity(null);
  1858. $this->Form->setEntity('Contact.field');
  1859. $result = $this->Form->input('Contact.field', array(
  1860. 'after' => 'A message to you, Rudy', 'error' => false
  1861. ));
  1862. $expected = array(
  1863. 'div' => array('class' => 'input text'),
  1864. 'label' => array('for' => 'ContactField'),
  1865. 'Field',
  1866. '/label',
  1867. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1868. 'A message to you, Rudy',
  1869. '/div'
  1870. );
  1871. $this->assertTags($result, $expected);
  1872. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1873. $expected = array(
  1874. 'div' => array('class' => 'input text'),
  1875. 'label' => array('for' => 'ObjectField'),
  1876. 'Field',
  1877. '/label',
  1878. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1879. 'A message to you, Rudy',
  1880. '/div'
  1881. );
  1882. $this->assertTags($result, $expected);
  1883. $Contact->validationErrors['field'] = array('minLength');
  1884. $result = $this->Form->input('Contact.field', array(
  1885. 'error' => array(
  1886. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1887. 'maxLength' => 'login too large'
  1888. )
  1889. ));
  1890. $expected = array(
  1891. 'div' => array('class' => 'input text error'),
  1892. 'label' => array('for' => 'ContactField'),
  1893. 'Field',
  1894. '/label',
  1895. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1896. array('div' => array('class' => 'error-message')),
  1897. 'Le login doit contenir au moins 2 caractères',
  1898. '/div',
  1899. '/div'
  1900. );
  1901. $this->assertTags($result, $expected);
  1902. $Contact->validationErrors['field'] = array('maxLength');
  1903. $result = $this->Form->input('Contact.field', array(
  1904. 'error' => array(
  1905. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1906. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1907. 'maxLength' => 'login too large',
  1908. )
  1909. ));
  1910. $expected = array(
  1911. 'div' => array('class' => 'input text error'),
  1912. 'label' => array('for' => 'ContactField'),
  1913. 'Field',
  1914. '/label',
  1915. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1916. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1917. 'login too large',
  1918. '/span',
  1919. '/div'
  1920. );
  1921. $this->assertTags($result, $expected);
  1922. }
  1923. /**
  1924. * test input() with checkbox creation
  1925. *
  1926. * @return void
  1927. */
  1928. public function testInputCheckbox() {
  1929. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1930. $expected = array(
  1931. 'div' => array('class' => 'input checkbox'),
  1932. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1933. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1934. '/div'
  1935. );
  1936. $this->assertTags($result, $expected);
  1937. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1938. $expected = array(
  1939. 'div' => array('class' => 'input checkbox'),
  1940. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1941. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1942. '/div'
  1943. );
  1944. $this->assertTags($result, $expected);
  1945. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1946. $expected = array(
  1947. 'div' => array('class' => 'input checkbox'),
  1948. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1949. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1950. '/div'
  1951. );
  1952. $this->assertTags($result, $expected);
  1953. }
  1954. /**
  1955. * test form->input() with time types.
  1956. *
  1957. */
  1958. public function testInputTime() {
  1959. extract($this->dateRegex);
  1960. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1961. $result = explode(':', $result);
  1962. $this->assertRegExp('/option value="23"/', $result[0]);
  1963. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1964. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1965. $result = explode(':', $result);
  1966. $this->assertRegExp('/option value="23"/', $result[0]);
  1967. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1968. $result = $this->Form->input('Model.field', array(
  1969. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  1970. ));
  1971. $result = explode(':', $result);
  1972. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1973. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1974. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1975. $result = $this->Form->input('Model.field', array(
  1976. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  1977. ));
  1978. $result = explode(':', $result);
  1979. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1980. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1981. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1982. $result = $this->Form->input('prueba', array(
  1983. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  1984. 'maxYear' => date('Y') + 1 ,'interval' => 15
  1985. ));
  1986. $result = explode(':', $result);
  1987. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1988. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1989. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1990. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  1991. $result = $this->Form->input('Random.start_time', array(
  1992. 'type' => 'time',
  1993. 'selected' => '18:15'
  1994. ));
  1995. $this->assertRegExp('#<option value="06"[^>]*>6</option>#', $result);
  1996. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result);
  1997. $this->assertRegExp('#<option value="pm"[^>]*>pm</option>#', $result);
  1998. }
  1999. /**
  2000. * test form->input() with datetime, date and time types
  2001. *
  2002. * @return void
  2003. */
  2004. public function testInputDatetime() {
  2005. extract($this->dateRegex);
  2006. $result = $this->Form->input('prueba', array(
  2007. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2008. 'maxYear' => date('Y') + 1, 'interval' => 15
  2009. ));
  2010. $result = explode(':', $result);
  2011. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2012. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2013. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2014. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2015. //related to ticket #5013
  2016. $result = $this->Form->input('Contact.date', array(
  2017. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2018. ));
  2019. $this->assertRegExp('/class="customClass"/', $result);
  2020. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2021. $result = $this->Form->input('Contact.date', array(
  2022. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2023. ));
  2024. $this->assertRegExp('/id="customIdDay"/', $result);
  2025. $this->assertRegExp('/id="customIdMonth"/', $result);
  2026. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2027. $result = $this->Form->input('Model.field', array(
  2028. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2029. ));
  2030. $this->assertRegExp('/id="customIDDay"/', $result);
  2031. $this->assertRegExp('/id="customIDHour"/', $result);
  2032. $result = explode('</select><select', $result);
  2033. $result = explode(':', $result[1]);
  2034. $this->assertRegExp('/option value="23"/', $result[0]);
  2035. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2036. $result = $this->Form->input('Model.field', array(
  2037. 'type' => 'datetime', 'timeFormat' => 12
  2038. ));
  2039. $result = explode('</select><select', $result);
  2040. $result = explode(':', $result[1]);
  2041. $this->assertRegExp('/option value="12"/', $result[0]);
  2042. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2043. $this->Form->request->data = array('Contact' => array('created' => null));
  2044. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2045. $expected = array(
  2046. 'div' => array('class' => 'input date'),
  2047. 'label' => array('for' => 'ContactCreatedMonth'),
  2048. 'Created',
  2049. '/label',
  2050. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2051. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2052. $monthsRegex,
  2053. '/select', '-',
  2054. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2055. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2056. $daysRegex,
  2057. '/select', '-',
  2058. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2059. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2060. $yearsRegex,
  2061. '/select',
  2062. '/div'
  2063. );
  2064. $this->assertTags($result, $expected);
  2065. $this->Form->request->data = array('Contact' => array('created' => null));
  2066. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2067. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2068. $this->Form->request->data = array('Contact' => array('created' => null));
  2069. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2070. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2071. $result = $this->Form->input('Contact.created', array(
  2072. 'type' => 'date',
  2073. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2074. 'timeFormat' => 'NONE'
  2075. ));
  2076. $this->assertRegExp('/for\="created-month"/', $result);
  2077. }
  2078. /**
  2079. * Test generating checkboxes in a loop.
  2080. *
  2081. * @return void
  2082. */
  2083. public function testInputCheckboxesInLoop() {
  2084. for ($i = 1; $i < 5; $i++) {
  2085. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2086. $expected = array(
  2087. 'div' => array('class' => 'input checkbox'),
  2088. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2089. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2090. 'label' => array('for' => "Contact{$i}Email"),
  2091. 'Email',
  2092. '/label',
  2093. '/div'
  2094. );
  2095. $this->assertTags($result, $expected);
  2096. }
  2097. }
  2098. /**
  2099. * test input name with leading integer, ensure attributes are generated correctly.
  2100. *
  2101. * @return void
  2102. */
  2103. public function testInputWithLeadingInteger() {
  2104. $result = $this->Form->text('0.Node.title');
  2105. $expected = array(
  2106. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2107. );
  2108. $this->assertTags($result, $expected);
  2109. }
  2110. /**
  2111. * test form->input() with select type inputs.
  2112. *
  2113. * @return void
  2114. */
  2115. public function testInputSelectType() {
  2116. $result = $this->Form->input('email', array(
  2117. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2118. );
  2119. $expected = array(
  2120. 'div' => array('class' => 'input select'),
  2121. 'label' => array('for' => 'email'),
  2122. 'Email',
  2123. '/label',
  2124. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2125. array('option' => array('value' => '')),
  2126. '/option',
  2127. array('option' => array('value' => 'è')),
  2128. 'Firést',
  2129. '/option',
  2130. array('option' => array('value' => 'é')),
  2131. 'Secoènd',
  2132. '/option',
  2133. '/select',
  2134. '/div'
  2135. );
  2136. $this->assertTags($result, $expected);
  2137. $result = $this->Form->input('email', array(
  2138. 'options' => array('First', 'Second'), 'empty' => true)
  2139. );
  2140. $expected = array(
  2141. 'div' => array('class' => 'input select'),
  2142. 'label' => array('for' => 'email'),
  2143. 'Email',
  2144. '/label',
  2145. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2146. array('option' => array('value' => '')),
  2147. '/option',
  2148. array('option' => array('value' => '0')),
  2149. 'First',
  2150. '/option',
  2151. array('option' => array('value' => '1')),
  2152. 'Second',
  2153. '/option',
  2154. '/select',
  2155. '/div'
  2156. );
  2157. $this->assertTags($result, $expected);
  2158. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2159. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2160. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2161. $expected = array(
  2162. 'div' => array('class' => 'input select'),
  2163. 'label' => array('for' => 'ModelUserId'),
  2164. 'User',
  2165. '/label',
  2166. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2167. array('option' => array('value' => '')),
  2168. '/option',
  2169. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2170. 'good',
  2171. '/option',
  2172. array('option' => array('value' => 'other')),
  2173. 'bad',
  2174. '/option',
  2175. '/select',
  2176. '/div'
  2177. );
  2178. $this->assertTags($result, $expected);
  2179. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2180. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2181. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2182. $expected = array(
  2183. 'div' => array('class' => 'input select'),
  2184. 'label' => array('for' => 'ThingUserId'),
  2185. 'User',
  2186. '/label',
  2187. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2188. array('option' => array('value' => '')),
  2189. 'Some Empty',
  2190. '/option',
  2191. array('option' => array('value' => 'value')),
  2192. 'good',
  2193. '/option',
  2194. array('option' => array('value' => 'other')),
  2195. 'bad',
  2196. '/option',
  2197. '/select',
  2198. '/div'
  2199. );
  2200. $this->assertTags($result, $expected);
  2201. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2202. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2203. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2204. $expected = array(
  2205. 'div' => array('class' => 'input select'),
  2206. 'label' => array('for' => 'ThingUserId'),
  2207. 'User',
  2208. '/label',
  2209. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2210. array('option' => array('value' => '')),
  2211. 'Some Empty',
  2212. '/option',
  2213. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2214. 'good',
  2215. '/option',
  2216. array('option' => array('value' => 'other')),
  2217. 'bad',
  2218. '/option',
  2219. '/select',
  2220. '/div'
  2221. );
  2222. $this->assertTags($result, $expected);
  2223. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2224. $this->Form->request->data = array('User' => array('User' => array('value')));
  2225. $result = $this->Form->input('User.User', array('empty' => true));
  2226. $expected = array(
  2227. 'div' => array('class' => 'input select'),
  2228. 'label' => array('for' => 'UserUser'),
  2229. 'User',
  2230. '/label',
  2231. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2232. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2233. array('option' => array('value' => '')),
  2234. '/option',
  2235. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2236. 'good',
  2237. '/option',
  2238. array('option' => array('value' => 'other')),
  2239. 'bad',
  2240. '/option',
  2241. '/select',
  2242. '/div'
  2243. );
  2244. $this->assertTags($result, $expected);
  2245. $this->Form->data = array();
  2246. $result = $this->Form->input('Publisher.id', array(
  2247. 'label' => 'Publisher',
  2248. 'type' => 'select',
  2249. 'multiple' => 'checkbox',
  2250. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2251. ));
  2252. $expected = array(
  2253. array('div' => array('class' => 'input select')),
  2254. array('label' => array('for' => 'PublisherId')),
  2255. 'Publisher',
  2256. '/label',
  2257. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2258. array('div' => array('class' => 'checkbox')),
  2259. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2260. array('label' => array('for' => 'PublisherIdValue1')),
  2261. 'Label 1',
  2262. '/label',
  2263. '/div',
  2264. array('div' => array('class' => 'checkbox')),
  2265. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2266. array('label' => array('for' => 'PublisherIdValue2')),
  2267. 'Label 2',
  2268. '/label',
  2269. '/div',
  2270. '/div'
  2271. );
  2272. $this->assertTags($result, $expected);
  2273. }
  2274. /**
  2275. * test that input() and a non standard primary key makes a hidden input by default.
  2276. *
  2277. * @return void
  2278. */
  2279. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2280. $this->Form->create('User');
  2281. $this->Form->fieldset = array(
  2282. 'User' => array(
  2283. 'fields' => array(
  2284. 'model_id' => array('type' => 'integer')
  2285. ),
  2286. 'validates' => array(),
  2287. 'key' => 'model_id'
  2288. )
  2289. );
  2290. $result = $this->Form->input('model_id');
  2291. $expected = array(
  2292. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2293. );
  2294. $this->assertTags($result, $expected);
  2295. }
  2296. /**
  2297. * test that overriding the magic select type widget is possible
  2298. *
  2299. * @return void
  2300. */
  2301. public function testInputOverridingMagicSelectType() {
  2302. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2303. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2304. $expected = array(
  2305. 'div' => array('class' => 'input text'),
  2306. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2307. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2308. '/div'
  2309. );
  2310. $this->assertTags($result, $expected);
  2311. //Check that magic types still work for plural/singular vars
  2312. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2313. $result = $this->Form->input('Model.type');
  2314. $expected = array(
  2315. 'div' => array('class' => 'input select'),
  2316. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2317. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2318. array('option' => array('value' => 'value')), 'good', '/option',
  2319. array('option' => array('value' => 'other')), 'bad', '/option',
  2320. '/select',
  2321. '/div'
  2322. );
  2323. $this->assertTags($result, $expected);
  2324. }
  2325. /**
  2326. * Test that magic input() selects can easily be converted into radio types without error.
  2327. *
  2328. * @return void
  2329. */
  2330. public function testInputMagicSelectChangeToRadio() {
  2331. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2332. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2333. $this->assertRegExp('/input type="radio"/', $result);
  2334. }
  2335. /**
  2336. * fields with the same name as the model should work.
  2337. *
  2338. * @return void
  2339. */
  2340. public function testInputWithMatchingFieldAndModelName() {
  2341. $this->Form->create('User');
  2342. $this->Form->fieldset = array(
  2343. 'User' => array(
  2344. 'fields' => array(
  2345. 'User' => array('type' => 'text')
  2346. ),
  2347. 'validates' => array(),
  2348. 'key' => 'id'
  2349. )
  2350. );
  2351. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2352. $result = $this->Form->input('User', array('type' => 'text'));
  2353. $expected = array(
  2354. 'div' => array('class' => 'input text'),
  2355. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2356. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2357. '/div'
  2358. );
  2359. $this->assertTags($result, $expected);
  2360. }
  2361. /**
  2362. * testFormInputs method
  2363. *
  2364. * test correct results from form::inputs().
  2365. *
  2366. * @return void
  2367. */
  2368. public function testFormInputs() {
  2369. $this->Form->create('Contact');
  2370. $result = $this->Form->inputs('The Legend');
  2371. $expected = array(
  2372. '<fieldset',
  2373. '<legend',
  2374. 'The Legend',
  2375. '/legend',
  2376. '*/fieldset',
  2377. );
  2378. $this->assertTags($result, $expected);
  2379. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2380. $expected = array(
  2381. 'fieldset' => array('class' => 'classy-stuff'),
  2382. '<legend',
  2383. 'Field of Dreams',
  2384. '/legend',
  2385. '*/fieldset'
  2386. );
  2387. $this->assertTags($result, $expected);
  2388. $this->Form->create('Contact');
  2389. $this->Form->request['prefix'] = 'admin';
  2390. $this->Form->request['action'] = 'admin_edit';
  2391. $result = $this->Form->inputs();
  2392. $expected = array(
  2393. '<fieldset',
  2394. '<legend',
  2395. 'Edit Contact',
  2396. '/legend',
  2397. '*/fieldset',
  2398. );
  2399. $this->assertTags($result, $expected);
  2400. $this->Form->create('Contact');
  2401. $result = $this->Form->inputs(false);
  2402. $expected = array(
  2403. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2404. array('div' => array('class' => 'input text')),
  2405. '*/div',
  2406. array('div' => array('class' => 'input text')),
  2407. '*/div',
  2408. array('div' => array('class' => 'input text')),
  2409. '*/div',
  2410. array('div' => array('class' => 'input password')),
  2411. '*/div',
  2412. array('div' => array('class' => 'input date')),
  2413. '*/div',
  2414. array('div' => array('class' => 'input date')),
  2415. '*/div',
  2416. array('div' => array('class' => 'input datetime')),
  2417. '*/div',
  2418. array('div' => array('class' => 'input number')),
  2419. '*/div',
  2420. array('div' => array('class' => 'input select')),
  2421. '*/div',
  2422. );
  2423. $this->assertTags($result, $expected);
  2424. $this->Form->create('Contact');
  2425. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2426. $expected = array(
  2427. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2428. array('div' => array('class' => 'input text')),
  2429. '*/div',
  2430. array('div' => array('class' => 'input text')),
  2431. '*/div',
  2432. array('div' => array('class' => 'input text')),
  2433. '*/div',
  2434. array('div' => array('class' => 'input password')),
  2435. '*/div',
  2436. array('div' => array('class' => 'input date')),
  2437. '*/div',
  2438. array('div' => array('class' => 'input date')),
  2439. '*/div',
  2440. array('div' => array('class' => 'input datetime')),
  2441. '*/div',
  2442. array('div' => array('class' => 'input number')),
  2443. '*/div',
  2444. array('div' => array('class' => 'input select')),
  2445. '*/div',
  2446. );
  2447. $this->assertTags($result, $expected);
  2448. $this->Form->create('Contact');
  2449. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2450. $expected = array(
  2451. 'fieldset' => array(),
  2452. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2453. array('div' => array('class' => 'input text')),
  2454. '*/div',
  2455. array('div' => array('class' => 'input text')),
  2456. '*/div',
  2457. array('div' => array('class' => 'input text')),
  2458. '*/div',
  2459. array('div' => array('class' => 'input password')),
  2460. '*/div',
  2461. array('div' => array('class' => 'input date')),
  2462. '*/div',
  2463. array('div' => array('class' => 'input date')),
  2464. '*/div',
  2465. array('div' => array('class' => 'input datetime')),
  2466. '*/div',
  2467. array('div' => array('class' => 'input number')),
  2468. '*/div',
  2469. array('div' => array('class' => 'input select')),
  2470. '*/div',
  2471. '/fieldset'
  2472. );
  2473. $this->assertTags($result, $expected);
  2474. $this->Form->create('Contact');
  2475. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2476. $expected = array(
  2477. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2478. array('div' => array('class' => 'input text')),
  2479. '*/div',
  2480. array('div' => array('class' => 'input text')),
  2481. '*/div',
  2482. array('div' => array('class' => 'input text')),
  2483. '*/div',
  2484. array('div' => array('class' => 'input password')),
  2485. '*/div',
  2486. array('div' => array('class' => 'input date')),
  2487. '*/div',
  2488. array('div' => array('class' => 'input date')),
  2489. '*/div',
  2490. array('div' => array('class' => 'input datetime')),
  2491. '*/div',
  2492. array('div' => array('class' => 'input number')),
  2493. '*/div',
  2494. array('div' => array('class' => 'input select')),
  2495. '*/div',
  2496. );
  2497. $this->assertTags($result, $expected);
  2498. $this->Form->create('Contact');
  2499. $result = $this->Form->inputs('Hello');
  2500. $expected = array(
  2501. 'fieldset' => array(),
  2502. 'legend' => array(),
  2503. 'Hello',
  2504. '/legend',
  2505. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2506. array('div' => array('class' => 'input text')),
  2507. '*/div',
  2508. array('div' => array('class' => 'input text')),
  2509. '*/div',
  2510. array('div' => array('class' => 'input text')),
  2511. '*/div',
  2512. array('div' => array('class' => 'input password')),
  2513. '*/div',
  2514. array('div' => array('class' => 'input date')),
  2515. '*/div',
  2516. array('div' => array('class' => 'input date')),
  2517. '*/div',
  2518. array('div' => array('class' => 'input datetime')),
  2519. '*/div',
  2520. array('div' => array('class' => 'input number')),
  2521. '*/div',
  2522. array('div' => array('class' => 'input select')),
  2523. '*/div',
  2524. '/fieldset'
  2525. );
  2526. $this->assertTags($result, $expected);
  2527. $this->Form->create('Contact');
  2528. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2529. $expected = array(
  2530. 'fieldset' => array(),
  2531. 'legend' => array(),
  2532. 'Hello',
  2533. '/legend',
  2534. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2535. array('div' => array('class' => 'input text')),
  2536. '*/div',
  2537. array('div' => array('class' => 'input text')),
  2538. '*/div',
  2539. array('div' => array('class' => 'input text')),
  2540. '*/div',
  2541. array('div' => array('class' => 'input password')),
  2542. '*/div',
  2543. array('div' => array('class' => 'input date')),
  2544. '*/div',
  2545. array('div' => array('class' => 'input date')),
  2546. '*/div',
  2547. array('div' => array('class' => 'input datetime')),
  2548. '*/div',
  2549. array('div' => array('class' => 'input number')),
  2550. '*/div',
  2551. array('div' => array('class' => 'input select')),
  2552. '*/div',
  2553. '/fieldset'
  2554. );
  2555. $this->assertTags($result, $expected);
  2556. }
  2557. /**
  2558. * testSelectAsCheckbox method
  2559. *
  2560. * test multi-select widget with checkbox formatting.
  2561. *
  2562. * @return void
  2563. */
  2564. public function testSelectAsCheckbox() {
  2565. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2566. $expected = array(
  2567. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2568. array('div' => array('class' => 'checkbox')),
  2569. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2570. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2571. 'first',
  2572. '/label',
  2573. '/div',
  2574. array('div' => array('class' => 'checkbox')),
  2575. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2576. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2577. 'second',
  2578. '/label',
  2579. '/div',
  2580. array('div' => array('class' => 'checkbox')),
  2581. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2582. array('label' => array('for' => 'ModelMultiField2')),
  2583. 'third',
  2584. '/label',
  2585. '/div',
  2586. );
  2587. $this->assertTags($result, $expected);
  2588. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2589. $expected = array(
  2590. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2591. array('div' => array('class' => 'checkbox')),
  2592. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2593. array('label' => array('for' => 'ModelMultiField12')),
  2594. 'half',
  2595. '/label',
  2596. '/div',
  2597. );
  2598. $this->assertTags($result, $expected);
  2599. }
  2600. /**
  2601. * testLabel method
  2602. *
  2603. * test label generation.
  2604. *
  2605. * @return void
  2606. */
  2607. public function testLabel() {
  2608. $this->Form->text('Person.name');
  2609. $result = $this->Form->label();
  2610. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2611. $this->Form->text('Person.name');
  2612. $result = $this->Form->label();
  2613. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2614. $result = $this->Form->label('Person.first_name');
  2615. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2616. $result = $this->Form->label('Person.first_name', 'Your first name');
  2617. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2618. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2619. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2620. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2621. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2622. $result = $this->Form->label('Person.first_name', '');
  2623. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2624. $result = $this->Form->label('Person.2.name', '');
  2625. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2626. }
  2627. /**
  2628. * testTextbox method
  2629. *
  2630. * test textbox element generation
  2631. *
  2632. * @return void
  2633. */
  2634. public function testTextbox() {
  2635. $result = $this->Form->text('Model.field');
  2636. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2637. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2638. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2639. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2640. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2641. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2642. $result = $this->Form->text('Model.text');
  2643. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2644. $Contact = ClassRegistry::getObject('Contact');
  2645. $Contact->validationErrors['text'] = array(true);
  2646. $this->Form->request->data['Contact']['text'] = 'test';
  2647. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2648. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2649. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2650. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2651. $expected = array(
  2652. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2653. );
  2654. $this->assertTags($result, $expected);
  2655. }
  2656. /**
  2657. * testDefaultValue method
  2658. *
  2659. * Test default value setting
  2660. *
  2661. * @return void
  2662. */
  2663. public function testDefaultValue() {
  2664. $this->Form->request->data['Model']['field'] = 'test';
  2665. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2666. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  2667. unset($this->Form->request->data['Model']['field']);
  2668. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2669. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  2670. }
  2671. /**
  2672. * testCheckboxDefaultValue method
  2673. *
  2674. * Test default value setting on checkbox() method
  2675. *
  2676. * @return void
  2677. */
  2678. public function testCheckboxDefaultValue() {
  2679. $this->Form->request->data['Model']['field'] = false;
  2680. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2681. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2682. unset($this->Form->request->data['Model']['field']);
  2683. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2684. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2685. $this->Form->request->data['Model']['field'] = true;
  2686. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2687. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2688. unset($this->Form->request->data['Model']['field']);
  2689. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2690. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2691. }
  2692. /**
  2693. * testError method
  2694. *
  2695. * Test field error generation
  2696. *
  2697. * @return void
  2698. */
  2699. public function testError() {
  2700. $Contact = ClassRegistry::getObject('Contact');
  2701. $Contact->validationErrors['field'] = array(1);
  2702. $result = $this->Form->error('Contact.field');
  2703. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  2704. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2705. $this->assertEquals('Error in field Field', $result);
  2706. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2707. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2708. $this->assertEquals('This field contains invalid input', $result);
  2709. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2710. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  2711. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  2712. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  2713. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  2714. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  2715. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2716. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  2717. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2718. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  2719. $this->assertEquals('<strong>Badness!</strong>', $result);
  2720. $Contact->validationErrors['field'] = array("email");
  2721. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  2722. $expected = array(
  2723. 'div' => array('class' => 'field-error'),
  2724. 'No good!',
  2725. '/div'
  2726. );
  2727. $this->assertTags($result, $expected);
  2728. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  2729. $result = $this->Form->error('Contact.field', array(
  2730. 'notEmpty' => 'Cannot be empty',
  2731. 'email' => 'No good!'
  2732. ));
  2733. $expected = array(
  2734. 'div' => array('class' => 'error-message'),
  2735. 'ul' => array(),
  2736. '<li', 'Cannot be empty', '/li',
  2737. '<li', 'No good!', '/li',
  2738. '<li', 'Something else', '/li',
  2739. '/ul',
  2740. '/div'
  2741. );
  2742. $this->assertTags($result, $expected);
  2743. /** Testing error messages list options **/
  2744. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  2745. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  2746. $expected = array(
  2747. 'div' => array('class' => 'error-message'),
  2748. 'ol' => array(),
  2749. '<li', 'notEmpty', '/li',
  2750. '<li', 'email', '/li',
  2751. '/ol',
  2752. '/div'
  2753. );
  2754. $this->assertTags($result, $expected);
  2755. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  2756. $expected = array(
  2757. 'div' => array('class' => 'error-message'),
  2758. 'ol' => array(),
  2759. '<li', 'notEmpty', '/li',
  2760. '<li', 'email', '/li',
  2761. '/ol',
  2762. '/div'
  2763. );
  2764. $this->assertTags($result, $expected);
  2765. $result = $this->Form->error('Contact.field', null, array(
  2766. 'listOptions' => array(
  2767. 'class' => 'ul-class',
  2768. 'itemOptions' => array(
  2769. 'class' => 'li-class'
  2770. )
  2771. )
  2772. ));
  2773. $expected = array(
  2774. 'div' => array('class' => 'error-message'),
  2775. 'ul' => array('class' => 'ul-class'),
  2776. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  2777. array('li' => array('class' => 'li-class')), 'email', '/li',
  2778. '/ul',
  2779. '/div'
  2780. );
  2781. $this->assertTags($result, $expected);
  2782. }
  2783. /**
  2784. * test error options when using form->input();
  2785. *
  2786. * @return void
  2787. */
  2788. public function testInputErrorEscape() {
  2789. $this->Form->create('ValidateProfile');
  2790. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  2791. $ValidateProfile->validationErrors['city'] = array('required<br>');
  2792. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
  2793. $this->assertRegExp('/required&lt;br&gt;/', $result);
  2794. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => false))));
  2795. $this->assertRegExp('/required<br>/', $result);
  2796. }
  2797. /**
  2798. * testPassword method
  2799. *
  2800. * Test password element generation
  2801. *
  2802. * @return void
  2803. */
  2804. public function testPassword() {
  2805. $Contact = ClassRegistry::getObject('Contact');
  2806. $result = $this->Form->password('Contact.field');
  2807. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  2808. $Contact->validationErrors['passwd'] = 1;
  2809. $this->Form->request->data['Contact']['passwd'] = 'test';
  2810. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  2811. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2812. }
  2813. /**
  2814. * testRadio method
  2815. *
  2816. * Test radio element set generation
  2817. *
  2818. * @return void
  2819. */
  2820. public function testRadio() {
  2821. $result = $this->Form->radio('Model.field', array('option A'));
  2822. $expected = array(
  2823. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2824. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2825. 'label' => array('for' => 'ModelField0'),
  2826. 'option A',
  2827. '/label'
  2828. );
  2829. $this->assertTags($result, $expected);
  2830. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  2831. $expected = array(
  2832. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2833. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  2834. 'label' => array('for' => 'ModelField12'),
  2835. 'half',
  2836. '/label'
  2837. );
  2838. $this->assertTags($result, $expected);
  2839. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  2840. $expected = array(
  2841. 'fieldset' => array(),
  2842. 'legend' => array(),
  2843. 'Field',
  2844. '/legend',
  2845. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2846. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2847. array('label' => array('for' => 'ModelField0')),
  2848. 'option A',
  2849. '/label',
  2850. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2851. array('label' => array('for' => 'ModelField1')),
  2852. 'option B',
  2853. '/label',
  2854. '/fieldset'
  2855. );
  2856. $this->assertTags($result, $expected);
  2857. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  2858. $expected = array(
  2859. 'fieldset' => array(),
  2860. 'legend' => array(),
  2861. 'Field',
  2862. '/legend',
  2863. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2864. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2865. array('label' => array('for' => 'ModelField0')),
  2866. 'option A',
  2867. '/label',
  2868. 'br' => array(),
  2869. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2870. array('label' => array('for' => 'ModelField1')),
  2871. 'option B',
  2872. '/label',
  2873. '/fieldset'
  2874. );
  2875. $this->assertTags($result, $expected);
  2876. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  2877. $expected = array(
  2878. 'fieldset' => array(),
  2879. 'legend' => array(),
  2880. 'Field',
  2881. '/legend',
  2882. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'checked' => 'checked')),
  2883. array('label' => array('for' => 'ModelField1')),
  2884. 'Yes',
  2885. '/label',
  2886. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2887. array('label' => array('for' => 'ModelField0')),
  2888. 'No',
  2889. '/label',
  2890. '/fieldset'
  2891. );
  2892. $this->assertTags($result, $expected);
  2893. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  2894. $expected = array(
  2895. 'fieldset' => array(),
  2896. 'legend' => array(),
  2897. 'Field',
  2898. '/legend',
  2899. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2900. array('label' => array('for' => 'ModelField1')),
  2901. 'Yes',
  2902. '/label',
  2903. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  2904. array('label' => array('for' => 'ModelField0')),
  2905. 'No',
  2906. '/label',
  2907. '/fieldset'
  2908. );
  2909. $this->assertTags($result, $expected);
  2910. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  2911. $expected = array(
  2912. 'fieldset' => array(),
  2913. 'legend' => array(),
  2914. 'Field',
  2915. '/legend',
  2916. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2917. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2918. array('label' => array('for' => 'ModelField1')),
  2919. 'Yes',
  2920. '/label',
  2921. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2922. array('label' => array('for' => 'ModelField0')),
  2923. 'No',
  2924. '/label',
  2925. '/fieldset'
  2926. );
  2927. $this->assertTags($result, $expected);
  2928. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  2929. $expected = array(
  2930. 'fieldset' => array(),
  2931. 'legend' => array(),
  2932. 'Field',
  2933. '/legend',
  2934. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2935. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2936. array('label' => array('for' => 'ModelField1')),
  2937. 'Yes',
  2938. '/label',
  2939. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2940. array('label' => array('for' => 'ModelField0')),
  2941. 'No',
  2942. '/label',
  2943. '/fieldset'
  2944. );
  2945. $this->assertTags($result, $expected);
  2946. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2947. $expected = array(
  2948. 'div' => array('class' => 'input radio'),
  2949. 'fieldset' => array(),
  2950. 'legend' => array(),
  2951. 'Legend title',
  2952. '/legend',
  2953. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2954. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2955. array('label' => array('for' => 'NewsletterSubscribe0')),
  2956. 'Unsubscribe',
  2957. '/label',
  2958. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2959. array('label' => array('for' => 'NewsletterSubscribe1')),
  2960. 'Subscribe',
  2961. '/label',
  2962. '/fieldset',
  2963. '/div'
  2964. );
  2965. $this->assertTags($result, $expected);
  2966. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2967. $expected = array(
  2968. 'div' => array('class' => 'input radio'),
  2969. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2970. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2971. array('label' => array('for' => 'NewsletterSubscribe0')),
  2972. 'Unsubscribe',
  2973. '/label',
  2974. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2975. array('label' => array('for' => 'NewsletterSubscribe1')),
  2976. 'Subscribe',
  2977. '/label',
  2978. '/div'
  2979. );
  2980. $this->assertTags($result, $expected);
  2981. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2982. $expected = array(
  2983. 'div' => array('class' => 'input radio'),
  2984. 'fieldset' => array(),
  2985. 'legend' => array(),
  2986. 'Legend title',
  2987. '/legend',
  2988. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2989. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2990. 'Unsubscribe',
  2991. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2992. 'Subscribe',
  2993. '/fieldset',
  2994. '/div'
  2995. );
  2996. $this->assertTags($result, $expected);
  2997. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2998. $expected = array(
  2999. 'div' => array('class' => 'input radio'),
  3000. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3001. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3002. 'Unsubscribe',
  3003. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3004. 'Subscribe',
  3005. '/div'
  3006. );
  3007. $this->assertTags($result, $expected);
  3008. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'value' => '1', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3009. $expected = array(
  3010. 'div' => array('class' => 'input radio'),
  3011. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3012. 'Unsubscribe',
  3013. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1', 'checked' => 'checked')),
  3014. 'Subscribe',
  3015. '/div'
  3016. );
  3017. $this->assertTags($result, $expected);
  3018. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3019. $expected = array(
  3020. 'fieldset' => array(),
  3021. 'legend' => array(),
  3022. 'Gender',
  3023. '/legend',
  3024. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3025. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3026. array('label' => array('for' => 'EmployeeGenderMale')),
  3027. 'Male',
  3028. '/label',
  3029. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3030. array('label' => array('for' => 'EmployeeGenderFemale')),
  3031. 'Female',
  3032. '/label',
  3033. '/fieldset',
  3034. );
  3035. $this->assertTags($result, $expected);
  3036. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3037. $expected = array(
  3038. 'fieldset' => array(),
  3039. 'legend' => array(),
  3040. 'Gender',
  3041. '/legend',
  3042. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3043. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3044. array('label' => array('for' => 'OfficerGenderMale')),
  3045. 'Male',
  3046. '/label',
  3047. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3048. array('label' => array('for' => 'OfficerGenderFemale')),
  3049. 'Female',
  3050. '/label',
  3051. '/fieldset',
  3052. );
  3053. $this->assertTags($result, $expected);
  3054. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3055. $expected = array(
  3056. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3057. array('input' => array('type' => 'radio', 'name' => 'data[Contact][1][imrequired]', 'value' => '0', 'id' => 'Contact1Imrequired0')),
  3058. 'label' => array('for' => 'Contact1Imrequired0'),
  3059. 'option A',
  3060. '/label'
  3061. );
  3062. $this->assertTags($result, $expected);
  3063. $result = $this->Form->radio('Model.1.field', array('option A'));
  3064. $expected = array(
  3065. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3066. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3067. 'label' => array('for' => 'Model1Field0'),
  3068. 'option A',
  3069. '/label'
  3070. );
  3071. $this->assertTags($result, $expected);
  3072. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3073. $expected = array(
  3074. 'fieldset' => array(),
  3075. 'legend' => array(),
  3076. 'Field',
  3077. '/legend',
  3078. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3079. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3080. array('label' => array('for' => 'ModelField0')),
  3081. 'option A',
  3082. '/label',
  3083. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3084. array('label' => array('for' => 'ModelField1')),
  3085. 'option B',
  3086. '/label',
  3087. '/fieldset'
  3088. );
  3089. $this->assertTags($result, $expected);
  3090. $result = $this->Form->radio(
  3091. 'Model.field',
  3092. array('option A', 'option B'),
  3093. array('between' => 'I am between')
  3094. );
  3095. $expected = array(
  3096. 'fieldset' => array(),
  3097. 'legend' => array(),
  3098. 'Field',
  3099. '/legend',
  3100. 'I am between',
  3101. 'input' => array(
  3102. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3103. 'value' => '', 'id' => 'ModelField_'
  3104. ),
  3105. array('input' => array(
  3106. 'type' => 'radio', 'name' => 'data[Model][field]',
  3107. 'value' => '0', 'id' => 'ModelField0'
  3108. )),
  3109. array('label' => array('for' => 'ModelField0')),
  3110. 'option A',
  3111. '/label',
  3112. array('input' => array(
  3113. 'type' => 'radio', 'name' => 'data[Model][field]',
  3114. 'value' => '1', 'id' => 'ModelField1'
  3115. )),
  3116. array('label' => array('for' => 'ModelField1')),
  3117. 'option B',
  3118. '/label',
  3119. '/fieldset'
  3120. );
  3121. $this->assertTags($result, $expected);
  3122. }
  3123. /**
  3124. * test disabled radio options
  3125. *
  3126. * @return void
  3127. */
  3128. public function testRadioDisabled() {
  3129. $result = $this->Form->radio(
  3130. 'Model.field',
  3131. array('option A', 'option B'),
  3132. array('disabled' => array('option A'), 'value' => 'option A')
  3133. );
  3134. $expected = array(
  3135. 'fieldset' => array(),
  3136. 'legend' => array(),
  3137. 'Field',
  3138. '/legend',
  3139. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3140. array('label' => array('for' => 'ModelField0')),
  3141. 'option A',
  3142. '/label',
  3143. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3144. array('label' => array('for' => 'ModelField1')),
  3145. 'option B',
  3146. '/label',
  3147. '/fieldset'
  3148. );
  3149. $this->assertTags($result, $expected);
  3150. $result = $this->Form->radio(
  3151. 'Model.field',
  3152. array('option A', 'option B'),
  3153. array('disabled' => true, 'value' => 'option A')
  3154. );
  3155. $expected = array(
  3156. 'fieldset' => array(),
  3157. 'legend' => array(),
  3158. 'Field',
  3159. '/legend',
  3160. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3161. array('label' => array('for' => 'ModelField0')),
  3162. 'option A',
  3163. '/label',
  3164. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3165. array('label' => array('for' => 'ModelField1')),
  3166. 'option B',
  3167. '/label',
  3168. '/fieldset'
  3169. );
  3170. $this->assertTags($result, $expected);
  3171. $result = $this->Form->radio(
  3172. 'Model.field',
  3173. array('option A', 'option B'),
  3174. array('disabled' => 'disabled', 'value' => 'option A')
  3175. );
  3176. $expected = array(
  3177. 'fieldset' => array(),
  3178. 'legend' => array(),
  3179. 'Field',
  3180. '/legend',
  3181. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3182. array('label' => array('for' => 'ModelField0')),
  3183. 'option A',
  3184. '/label',
  3185. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3186. array('label' => array('for' => 'ModelField1')),
  3187. 'option B',
  3188. '/label',
  3189. '/fieldset'
  3190. );
  3191. $this->assertTags($result, $expected);
  3192. }
  3193. /**
  3194. * test disabling the hidden input for radio buttons
  3195. *
  3196. * @return void
  3197. */
  3198. public function testRadioHiddenInputDisabling() {
  3199. $result = $this->Form->input('Model.1.field', array(
  3200. 'type' => 'radio',
  3201. 'options' => array('option A'),
  3202. 'hiddenField' => false
  3203. )
  3204. );
  3205. $expected = array(
  3206. 'div' => array('class' => 'input radio'),
  3207. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3208. 'label' => array('for' => 'Model1Field0'),
  3209. 'option A',
  3210. '/label',
  3211. '/div'
  3212. );
  3213. $this->assertTags($result, $expected);
  3214. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3215. $expected = array(
  3216. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3217. 'label' => array('for' => 'Model1Field0'),
  3218. 'option A',
  3219. '/label'
  3220. );
  3221. $this->assertTags($result, $expected);
  3222. }
  3223. /**
  3224. * test adding an empty option for radio buttons
  3225. *
  3226. * @return void
  3227. */
  3228. public function testRadioAddEmptyOption() {
  3229. $result = $this->Form->input('Model.1.field', array(
  3230. 'type' => 'radio',
  3231. 'options' => array('option A'),
  3232. 'empty' => true,
  3233. 'hiddenField' => false
  3234. ));
  3235. $expected = array(
  3236. 'div' => array('class' => 'input radio'),
  3237. 'fieldset' => array(),
  3238. 'legend' => array(),
  3239. 'Field',
  3240. '/legend',
  3241. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3242. array('label' => array('for' => 'Model1Field')),
  3243. __('empty'),
  3244. '/label',
  3245. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3246. array('label' => array('for' => 'Model1Field0')),
  3247. 'option A',
  3248. '/label',
  3249. '/fieldset',
  3250. '/div'
  3251. );
  3252. $this->assertTags($result, $expected);
  3253. $result = $this->Form->input('Model.1.field', array(
  3254. 'type' => 'radio',
  3255. 'options' => array('option A'),
  3256. 'empty' => 'CustomEmptyLabel',
  3257. 'hiddenField' => false
  3258. ));
  3259. $expected = array(
  3260. 'div' => array('class' => 'input radio'),
  3261. 'fieldset' => array(),
  3262. 'legend' => array(),
  3263. 'Field',
  3264. '/legend',
  3265. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3266. array('label' => array('for' => 'Model1Field')),
  3267. 'CustomEmptyLabel',
  3268. '/label',
  3269. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3270. array('label' => array('for' => 'Model1Field0')),
  3271. 'option A',
  3272. '/label',
  3273. '/fieldset',
  3274. '/div'
  3275. );
  3276. $this->assertTags($result, $expected);
  3277. $result = $this->Form->input('Model.1.field', array(
  3278. 'type' => 'radio',
  3279. 'options' => array('option A'),
  3280. 'empty' => false,
  3281. 'hiddenField' => false
  3282. ));
  3283. $this->assertTextNotContains('"Model1Field"', $result);
  3284. }
  3285. /**
  3286. * testSelect method
  3287. *
  3288. * Test select element generation.
  3289. *
  3290. * @return void
  3291. */
  3292. public function testSelect() {
  3293. $result = $this->Form->select('Model.field', array());
  3294. $expected = array(
  3295. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3296. array('option' => array('value' => '')),
  3297. '/option',
  3298. '/select'
  3299. );
  3300. $this->assertTags($result, $expected);
  3301. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3302. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3303. $expected = array(
  3304. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3305. array('option' => array('value' => '')),
  3306. '/option',
  3307. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3308. 'good',
  3309. '/option',
  3310. array('option' => array('value' => 'other')),
  3311. 'bad',
  3312. '/option',
  3313. '/select'
  3314. );
  3315. $this->assertTags($result, $expected);
  3316. $this->Form->request->data = array();
  3317. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3318. $expected = array(
  3319. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3320. array('option' => array('value' => '')),
  3321. '/option',
  3322. array('option' => array('value' => 'value')),
  3323. 'good',
  3324. '/option',
  3325. array('option' => array('value' => 'other')),
  3326. 'bad',
  3327. '/option',
  3328. '/select'
  3329. );
  3330. $this->assertTags($result, $expected);
  3331. $result = $this->Form->select(
  3332. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3333. array('empty' => false)
  3334. );
  3335. $expected = array(
  3336. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3337. array('option' => array('value' => 'first')),
  3338. 'first &quot;html&quot; &lt;chars&gt;',
  3339. '/option',
  3340. array('option' => array('value' => 'second')),
  3341. 'value',
  3342. '/option',
  3343. '/select'
  3344. );
  3345. $this->assertTags($result, $expected);
  3346. $result = $this->Form->select(
  3347. 'Model.field',
  3348. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3349. array('escape' => false, 'empty' => false)
  3350. );
  3351. $expected = array(
  3352. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3353. array('option' => array('value' => 'first')),
  3354. 'first "html" <chars>',
  3355. '/option',
  3356. array('option' => array('value' => 'second')),
  3357. 'value',
  3358. '/option',
  3359. '/select'
  3360. );
  3361. $this->assertTags($result, $expected);
  3362. $options = array(
  3363. array('value' => 'first', 'name' => 'First'),
  3364. array('value' => 'first', 'name' => 'Another First'),
  3365. );
  3366. $result = $this->Form->select(
  3367. 'Model.field',
  3368. $options,
  3369. array('escape' => false, 'empty' => false)
  3370. );
  3371. $expected = array(
  3372. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3373. array('option' => array('value' => 'first')),
  3374. 'First',
  3375. '/option',
  3376. array('option' => array('value' => 'first')),
  3377. 'Another First',
  3378. '/option',
  3379. '/select'
  3380. );
  3381. $this->assertTags($result, $expected);
  3382. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3383. $result = $this->Form->select(
  3384. 'Model.contact_id',
  3385. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3386. array('escape' => false, 'empty' => 'pick something')
  3387. );
  3388. $expected = array(
  3389. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3390. array('option' => array('value' => '')), 'pick something', '/option',
  3391. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3392. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3393. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3394. '/select'
  3395. );
  3396. $this->assertTags($result, $expected);
  3397. $this->Form->request->data['Model']['field'] = 0;
  3398. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3399. $expected = array(
  3400. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3401. array('option' => array('value' => '')), '/option',
  3402. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3403. array('option' => array('value' => '1')), 'Yes', '/option',
  3404. '/select'
  3405. );
  3406. $this->assertTags($result, $expected);
  3407. }
  3408. /**
  3409. * test that select() with optiongroups listens to the escape param.
  3410. *
  3411. * @return void
  3412. */
  3413. public function testSelectOptionGroupEscaping() {
  3414. $options = array(
  3415. '>< Key' => array(
  3416. 1 => 'One',
  3417. 2 => 'Two'
  3418. )
  3419. );
  3420. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3421. $expected = array(
  3422. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3423. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3424. array('option' => array('value' => '1')), 'One', '/option',
  3425. array('option' => array('value' => '2')), 'Two', '/option',
  3426. '/optgroup',
  3427. '/select'
  3428. );
  3429. $this->assertTags($result, $expected);
  3430. $options = array(
  3431. '>< Key' => array(
  3432. 1 => 'One',
  3433. 2 => 'Two'
  3434. )
  3435. );
  3436. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3437. $expected = array(
  3438. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3439. 'optgroup' => array('label' => '>< Key'),
  3440. array('option' => array('value' => '1')), 'One', '/option',
  3441. array('option' => array('value' => '2')), 'Two', '/option',
  3442. '/optgroup',
  3443. '/select'
  3444. );
  3445. $this->assertTags($result, $expected);
  3446. }
  3447. /**
  3448. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3449. *
  3450. * @return void
  3451. */
  3452. public function testSelectWithNullAttributes() {
  3453. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3454. $expected = array(
  3455. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3456. array('option' => array('value' => '0')),
  3457. 'first',
  3458. '/option',
  3459. array('option' => array('value' => '1')),
  3460. 'second',
  3461. '/option',
  3462. '/select'
  3463. );
  3464. $this->assertTags($result, $expected);
  3465. }
  3466. /**
  3467. * testNestedSelect method
  3468. *
  3469. * test select element generation with optgroups
  3470. *
  3471. * @return void
  3472. */
  3473. public function testNestedSelect() {
  3474. $result = $this->Form->select(
  3475. 'Model.field',
  3476. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3477. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3478. )), array('empty' => false)
  3479. );
  3480. $expected = array(
  3481. 'select' => array('name' => 'data[Model][field]',
  3482. 'id' => 'ModelField'),
  3483. array('option' => array('value' => 1)),
  3484. 'One',
  3485. '/option',
  3486. array('option' => array('value' => 2)),
  3487. 'Two',
  3488. '/option',
  3489. array('optgroup' => array('label' => 'Three')),
  3490. array('option' => array('value' => 4)),
  3491. 'Four',
  3492. '/option',
  3493. array('option' => array('value' => 5)),
  3494. 'Five',
  3495. '/option',
  3496. '/optgroup',
  3497. '/select'
  3498. );
  3499. $this->assertTags($result, $expected);
  3500. $result = $this->Form->select(
  3501. 'Model.field',
  3502. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3503. array('showParents' => true, 'empty' => false)
  3504. );
  3505. $expected = array(
  3506. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3507. array('option' => array('value' => 1)),
  3508. 'One',
  3509. '/option',
  3510. array('option' => array('value' => 2)),
  3511. 'Two',
  3512. '/option',
  3513. array('optgroup' => array('label' => 'Three')),
  3514. array('option' => array('value' => 3)),
  3515. 'Three',
  3516. '/option',
  3517. array('option' => array('value' => 4)),
  3518. 'Four',
  3519. '/option',
  3520. '/optgroup',
  3521. '/select'
  3522. );
  3523. $this->assertTags($result, $expected);
  3524. }
  3525. /**
  3526. * testSelectMultiple method
  3527. *
  3528. * test generation of multiple select elements
  3529. *
  3530. * @return void
  3531. */
  3532. public function testSelectMultiple() {
  3533. $options = array('first', 'second', 'third');
  3534. $result = $this->Form->select(
  3535. 'Model.multi_field', $options, array('multiple' => true)
  3536. );
  3537. $expected = array(
  3538. 'input' => array(
  3539. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3540. ),
  3541. 'select' => array(
  3542. 'name' => 'data[Model][multi_field][]',
  3543. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3544. ),
  3545. array('option' => array('value' => '0')),
  3546. 'first',
  3547. '/option',
  3548. array('option' => array('value' => '1')),
  3549. 'second',
  3550. '/option',
  3551. array('option' => array('value' => '2')),
  3552. 'third',
  3553. '/option',
  3554. '/select'
  3555. );
  3556. $this->assertTags($result, $expected);
  3557. $result = $this->Form->select(
  3558. 'Model.multi_field', $options, array('multiple' => 'multiple')
  3559. );
  3560. $expected = array(
  3561. 'input' => array(
  3562. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3563. ),
  3564. 'select' => array(
  3565. 'name' => 'data[Model][multi_field][]',
  3566. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3567. ),
  3568. array('option' => array('value' => '0')),
  3569. 'first',
  3570. '/option',
  3571. array('option' => array('value' => '1')),
  3572. 'second',
  3573. '/option',
  3574. array('option' => array('value' => '2')),
  3575. 'third',
  3576. '/option',
  3577. '/select'
  3578. );
  3579. $this->assertTags($result, $expected);
  3580. $result = $this->Form->select(
  3581. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  3582. );
  3583. $expected = array(
  3584. 'input' => array(
  3585. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3586. ),
  3587. 'select' => array(
  3588. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  3589. 'multiple' => 'multiple'
  3590. ),
  3591. array('option' => array('value' => '0', 'selected' => 'selected')),
  3592. 'first',
  3593. '/option',
  3594. array('option' => array('value' => '1', 'selected' => 'selected')),
  3595. 'second',
  3596. '/option',
  3597. array('option' => array('value' => '2')),
  3598. 'third',
  3599. '/option',
  3600. '/select'
  3601. );
  3602. $this->assertTags($result, $expected);
  3603. $result = $this->Form->select(
  3604. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  3605. );
  3606. $expected = array(
  3607. 'select' => array(
  3608. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  3609. ),
  3610. array('option' => array('value' => '0', 'selected' => 'selected')),
  3611. 'first',
  3612. '/option',
  3613. array('option' => array('value' => '1', 'selected' => 'selected')),
  3614. 'second',
  3615. '/option',
  3616. array('option' => array('value' => '2')),
  3617. 'third',
  3618. '/option',
  3619. '/select'
  3620. );
  3621. $this->assertTags($result, $expected);
  3622. }
  3623. /**
  3624. * test generation of habtm select boxes.
  3625. *
  3626. * @return void
  3627. */
  3628. public function testHabtmSelectBox() {
  3629. $this->View->viewVars['contactTags'] = array(
  3630. 1 => 'blue',
  3631. 2 => 'red',
  3632. 3 => 'green'
  3633. );
  3634. $this->Form->request->data = array(
  3635. 'Contact' => array(),
  3636. 'ContactTag' => array(
  3637. array(
  3638. 'id' => 1,
  3639. 'name' => 'blue'
  3640. ),
  3641. array(
  3642. 'id' => 3,
  3643. 'name' => 'green'
  3644. )
  3645. )
  3646. );
  3647. $this->Form->create('Contact');
  3648. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3649. $expected = array(
  3650. 'input' => array(
  3651. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3652. ),
  3653. 'select' => array(
  3654. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  3655. 'multiple' => 'multiple'
  3656. ),
  3657. array('option' => array('value' => '1', 'selected' => 'selected')),
  3658. 'blue',
  3659. '/option',
  3660. array('option' => array('value' => '2')),
  3661. 'red',
  3662. '/option',
  3663. array('option' => array('value' => '3', 'selected' => 'selected')),
  3664. 'green',
  3665. '/option',
  3666. '/select'
  3667. );
  3668. $this->assertTags($result, $expected);
  3669. }
  3670. /**
  3671. * test generation of multi select elements in checkbox format
  3672. *
  3673. * @return void
  3674. */
  3675. public function testSelectMultipleCheckboxes() {
  3676. $result = $this->Form->select(
  3677. 'Model.multi_field',
  3678. array('first', 'second', 'third'),
  3679. array('multiple' => 'checkbox')
  3680. );
  3681. $expected = array(
  3682. 'input' => array(
  3683. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3684. ),
  3685. array('div' => array('class' => 'checkbox')),
  3686. array('input' => array(
  3687. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3688. 'value' => '0', 'id' => 'ModelMultiField0'
  3689. )),
  3690. array('label' => array('for' => 'ModelMultiField0')),
  3691. 'first',
  3692. '/label',
  3693. '/div',
  3694. array('div' => array('class' => 'checkbox')),
  3695. array('input' => array(
  3696. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3697. 'value' => '1', 'id' => 'ModelMultiField1'
  3698. )),
  3699. array('label' => array('for' => 'ModelMultiField1')),
  3700. 'second',
  3701. '/label',
  3702. '/div',
  3703. array('div' => array('class' => 'checkbox')),
  3704. array('input' => array(
  3705. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3706. 'value' => '2', 'id' => 'ModelMultiField2'
  3707. )),
  3708. array('label' => array('for' => 'ModelMultiField2')),
  3709. 'third',
  3710. '/label',
  3711. '/div'
  3712. );
  3713. $this->assertTags($result, $expected);
  3714. $result = $this->Form->select(
  3715. 'Model.multi_field',
  3716. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3717. array('multiple' => 'checkbox')
  3718. );
  3719. $expected = array(
  3720. 'input' => array(
  3721. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3722. ),
  3723. array('div' => array('class' => 'checkbox')),
  3724. array('input' => array(
  3725. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3726. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  3727. )),
  3728. array('label' => array('for' => 'ModelMultiFieldA')),
  3729. 'first',
  3730. '/label',
  3731. '/div',
  3732. array('div' => array('class' => 'checkbox')),
  3733. array('input' => array(
  3734. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3735. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  3736. )),
  3737. array('label' => array('for' => 'ModelMultiFieldB')),
  3738. 'second',
  3739. '/label',
  3740. '/div',
  3741. array('div' => array('class' => 'checkbox')),
  3742. array('input' => array(
  3743. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3744. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  3745. )),
  3746. array('label' => array('for' => 'ModelMultiFieldC')),
  3747. 'third',
  3748. '/label',
  3749. '/div'
  3750. );
  3751. $this->assertTags($result, $expected);
  3752. $result = $this->Form->select(
  3753. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  3754. );
  3755. $expected = array(
  3756. 'input' => array(
  3757. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3758. ),
  3759. array('div' => array('class' => 'checkbox')),
  3760. array('input' => array(
  3761. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3762. 'value' => '1', 'id' => 'ModelMultiField1'
  3763. )),
  3764. array('label' => array('for' => 'ModelMultiField1')),
  3765. 'first',
  3766. '/label',
  3767. '/div'
  3768. );
  3769. $this->assertTags($result, $expected);
  3770. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3771. $result = $this->Form->select(
  3772. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3773. );
  3774. $expected = array(
  3775. 'input' => array(
  3776. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3777. ),
  3778. array('div' => array('class' => 'checkbox')),
  3779. array('input' => array(
  3780. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3781. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  3782. )),
  3783. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  3784. 'first',
  3785. '/label',
  3786. '/div',
  3787. array('div' => array('class' => 'checkbox')),
  3788. array('input' => array(
  3789. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3790. 'value' => 'Array', 'id' => 'ModelTagsArray'
  3791. )),
  3792. array('label' => array('for' => 'ModelTagsArray')),
  3793. 'Array',
  3794. '/label',
  3795. '/div'
  3796. );
  3797. $this->assertTags($result, $expected);
  3798. }
  3799. /**
  3800. * test multiple checkboxes with div styles.
  3801. *
  3802. * @return void
  3803. */
  3804. public function testSelectMultipleCheckboxDiv() {
  3805. $result = $this->Form->select(
  3806. 'Model.tags',
  3807. array('first', 'second'),
  3808. array('multiple' => 'checkbox', 'class' => 'my-class')
  3809. );
  3810. $expected = array(
  3811. 'input' => array(
  3812. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3813. ),
  3814. array('div' => array('class' => 'my-class')),
  3815. array('input' => array(
  3816. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3817. 'value' => '0', 'id' => 'ModelTags0'
  3818. )),
  3819. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  3820. '/div',
  3821. array('div' => array('class' => 'my-class')),
  3822. array('input' => array(
  3823. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3824. 'value' => '1', 'id' => 'ModelTags1'
  3825. )),
  3826. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  3827. '/div'
  3828. );
  3829. $this->assertTags($result, $expected);
  3830. $result = $this->Form->input('Model.tags', array(
  3831. 'options' => array('first', 'second'),
  3832. 'multiple' => 'checkbox',
  3833. 'class' => 'my-class',
  3834. 'div' => false,
  3835. 'label' => false
  3836. ));
  3837. $this->assertTags($result, $expected);
  3838. $Contact = ClassRegistry::getObject('Contact');
  3839. $Contact->validationErrors['tags'] = 'Select atleast one option';
  3840. $result = $this->Form->input('Contact.tags', array(
  3841. 'options' => array('one'),
  3842. 'multiple' => 'checkbox',
  3843. 'label' => false,
  3844. 'div' => false
  3845. ));
  3846. $expected = array(
  3847. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3848. array('div' => array('class' => 'checkbox form-error')),
  3849. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3850. array('label' => array('for' => 'ContactTags0')),
  3851. 'one',
  3852. '/label',
  3853. '/div'
  3854. );
  3855. $this->assertTags($result, $expected);
  3856. $result = $this->Form->input('Contact.tags', array(
  3857. 'options' => array('one'),
  3858. 'multiple' => 'checkbox',
  3859. 'class' => 'mycheckbox',
  3860. 'label' => false,
  3861. 'div' => false
  3862. ));
  3863. $expected = array(
  3864. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3865. array('div' => array('class' => 'mycheckbox form-error')),
  3866. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3867. array('label' => array('for' => 'ContactTags0')),
  3868. 'one',
  3869. '/label',
  3870. '/div'
  3871. );
  3872. $this->assertTags($result, $expected);
  3873. }
  3874. /**
  3875. * Checks the security hash array generated for multiple-input checkbox elements
  3876. *
  3877. * @return void
  3878. */
  3879. public function testSelectMultipleCheckboxSecurity() {
  3880. $this->Form->request['_Token'] = array('key' => 'testKey');
  3881. $this->assertEquals(array(), $this->Form->fields);
  3882. $result = $this->Form->select(
  3883. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  3884. array('multiple' => 'checkbox')
  3885. );
  3886. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  3887. $result = $this->Form->secure($this->Form->fields);
  3888. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  3889. $this->assertRegExp('/"' . $key . '"/', $result);
  3890. }
  3891. /**
  3892. * testInputMultipleCheckboxes method
  3893. *
  3894. * test input() resulting in multi select elements being generated.
  3895. *
  3896. * @return void
  3897. */
  3898. public function testInputMultipleCheckboxes() {
  3899. $result = $this->Form->input('Model.multi_field', array(
  3900. 'options' => array('first', 'second', 'third'),
  3901. 'multiple' => 'checkbox'
  3902. ));
  3903. $expected = array(
  3904. array('div' => array('class' => 'input select')),
  3905. array('label' => array('for' => 'ModelMultiField')),
  3906. 'Multi Field',
  3907. '/label',
  3908. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3909. array('div' => array('class' => 'checkbox')),
  3910. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  3911. array('label' => array('for' => 'ModelMultiField0')),
  3912. 'first',
  3913. '/label',
  3914. '/div',
  3915. array('div' => array('class' => 'checkbox')),
  3916. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  3917. array('label' => array('for' => 'ModelMultiField1')),
  3918. 'second',
  3919. '/label',
  3920. '/div',
  3921. array('div' => array('class' => 'checkbox')),
  3922. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  3923. array('label' => array('for' => 'ModelMultiField2')),
  3924. 'third',
  3925. '/label',
  3926. '/div',
  3927. '/div'
  3928. );
  3929. $this->assertTags($result, $expected);
  3930. $result = $this->Form->input('Model.multi_field', array(
  3931. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3932. 'multiple' => 'checkbox'
  3933. ));
  3934. $expected = array(
  3935. array('div' => array('class' => 'input select')),
  3936. array('label' => array('for' => 'ModelMultiField')),
  3937. 'Multi Field',
  3938. '/label',
  3939. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3940. array('div' => array('class' => 'checkbox')),
  3941. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  3942. array('label' => array('for' => 'ModelMultiFieldA')),
  3943. 'first',
  3944. '/label',
  3945. '/div',
  3946. array('div' => array('class' => 'checkbox')),
  3947. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  3948. array('label' => array('for' => 'ModelMultiFieldB')),
  3949. 'second',
  3950. '/label',
  3951. '/div',
  3952. array('div' => array('class' => 'checkbox')),
  3953. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  3954. array('label' => array('for' => 'ModelMultiFieldC')),
  3955. 'third',
  3956. '/label',
  3957. '/div',
  3958. '/div'
  3959. );
  3960. $this->assertTags($result, $expected);
  3961. $result = $this->Form->input('Model.multi_field', array(
  3962. 'options' => array('1' => 'first'),
  3963. 'multiple' => 'checkbox',
  3964. 'label' => false,
  3965. 'div' => false
  3966. ));
  3967. $expected = array(
  3968. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3969. array('div' => array('class' => 'checkbox')),
  3970. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  3971. array('label' => array('for' => 'ModelMultiField1')),
  3972. 'first',
  3973. '/label',
  3974. '/div'
  3975. );
  3976. $this->assertTags($result, $expected);
  3977. $result = $this->Form->input('Model.multi_field', array(
  3978. 'options' => array('2' => 'second'),
  3979. 'multiple' => 'checkbox',
  3980. 'label' => false,
  3981. 'div' => false
  3982. ));
  3983. $expected = array(
  3984. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3985. array('div' => array('class' => 'checkbox')),
  3986. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  3987. array('label' => array('for' => 'ModelMultiField2')),
  3988. 'second',
  3989. '/label',
  3990. '/div'
  3991. );
  3992. $this->assertTags($result, $expected);
  3993. }
  3994. /**
  3995. * testSelectHiddenFieldOmission method
  3996. *
  3997. * test that select() with 'hiddenField' => false omits the hidden field
  3998. *
  3999. * @return void
  4000. */
  4001. public function testSelectHiddenFieldOmission() {
  4002. $result = $this->Form->select('Model.multi_field',
  4003. array('first', 'second'),
  4004. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4005. );
  4006. $expected = array(
  4007. array('div' => array('class' => 'checkbox')),
  4008. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4009. array('label' => array('for' => 'ModelMultiField0')),
  4010. 'first',
  4011. '/label',
  4012. '/div',
  4013. array('div' => array('class' => 'checkbox')),
  4014. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4015. array('label' => array('for' => 'ModelMultiField1')),
  4016. 'second',
  4017. '/label',
  4018. '/div'
  4019. );
  4020. $this->assertTags($result, $expected);
  4021. $result = $this->Form->input('Model.multi_field', array(
  4022. 'options' => array('first', 'second'),
  4023. 'multiple' => 'checkbox',
  4024. 'hiddenField' => false
  4025. ));
  4026. $expected = array(
  4027. array('div' => array('class' => 'input select')),
  4028. array('label' => array('for' => 'ModelMultiField')),
  4029. 'Multi Field',
  4030. '/label',
  4031. array('div' => array('class' => 'checkbox')),
  4032. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4033. array('label' => array('for' => 'ModelMultiField0')),
  4034. 'first',
  4035. '/label',
  4036. '/div',
  4037. array('div' => array('class' => 'checkbox')),
  4038. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4039. array('label' => array('for' => 'ModelMultiField1')),
  4040. 'second',
  4041. '/label',
  4042. '/div',
  4043. '/div'
  4044. );
  4045. $this->assertTags($result, $expected);
  4046. }
  4047. /**
  4048. * test that select() with multiple = checkbox works with overriding name attribute.
  4049. *
  4050. * @return void
  4051. */
  4052. public function testSelectCheckboxMultipleOverrideName() {
  4053. $result = $this->Form->input('category', array(
  4054. 'type' => 'select',
  4055. 'multiple' => 'checkbox',
  4056. 'name' => 'data[fish]',
  4057. 'options' => array('1', '2'),
  4058. 'div' => false,
  4059. 'label' => false,
  4060. ));
  4061. $expected = array(
  4062. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4063. array('div' => array('class' => 'checkbox')),
  4064. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4065. array('label' => array('for' => 'Category0')), '1', '/label',
  4066. '/div',
  4067. array('div' => array('class' => 'checkbox')),
  4068. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4069. array('label' => array('for' => 'Category1')), '2', '/label',
  4070. '/div'
  4071. );
  4072. $this->assertTags($result, $expected);
  4073. }
  4074. /**
  4075. * Test that 'id' overrides all the checkbox id's as well.
  4076. *
  4077. * @return void
  4078. */
  4079. public function testSelectCheckboxMultipleId() {
  4080. $result = $this->Form->select(
  4081. 'Model.multi_field',
  4082. array('first', 'second', 'third'),
  4083. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4084. );
  4085. $expected = array(
  4086. 'input' => array(
  4087. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4088. ),
  4089. array('div' => array('class' => 'checkbox')),
  4090. array('input' => array(
  4091. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4092. 'value' => '0', 'id' => 'CustomId0'
  4093. )),
  4094. array('label' => array('for' => 'CustomId0')),
  4095. 'first',
  4096. '/label',
  4097. '/div',
  4098. array('div' => array('class' => 'checkbox')),
  4099. array('input' => array(
  4100. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4101. 'value' => '1', 'id' => 'CustomId1'
  4102. )),
  4103. array('label' => array('for' => 'CustomId1')),
  4104. 'second',
  4105. '/label',
  4106. '/div',
  4107. array('div' => array('class' => 'checkbox')),
  4108. array('input' => array(
  4109. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4110. 'value' => '2', 'id' => 'CustomId2'
  4111. )),
  4112. array('label' => array('for' => 'CustomId2')),
  4113. 'third',
  4114. '/label',
  4115. '/div'
  4116. );
  4117. $this->assertTags($result, $expected);
  4118. }
  4119. /**
  4120. * testCheckbox method
  4121. *
  4122. * Test generation of checkboxes
  4123. *
  4124. * @return void
  4125. */
  4126. public function testCheckbox() {
  4127. $result = $this->Form->checkbox('Model.field');
  4128. $expected = array(
  4129. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4130. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4131. );
  4132. $this->assertTags($result, $expected);
  4133. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4134. $expected = array(
  4135. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4136. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4137. );
  4138. $this->assertTags($result, $expected);
  4139. $Contact = ClassRegistry::getObject('Contact');
  4140. $Contact->validationErrors['field'] = 1;
  4141. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4142. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4143. $expected = array(
  4144. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4145. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4146. );
  4147. $this->assertTags($result, $expected);
  4148. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4149. $expected = array(
  4150. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4151. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4152. );
  4153. $this->assertTags($result, $expected);
  4154. $this->Form->request->data['Contact']['field'] = '';
  4155. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4156. $expected = array(
  4157. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4158. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4159. );
  4160. $this->assertTags($result, $expected);
  4161. $Contact->validationErrors = array();
  4162. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4163. $expected = array(
  4164. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4165. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4166. );
  4167. $this->assertTags($result, $expected);
  4168. $this->Form->request->data['Contact']['published'] = 1;
  4169. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4170. $expected = array(
  4171. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4172. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4173. );
  4174. $this->assertTags($result, $expected);
  4175. $this->Form->request->data['Contact']['published'] = 0;
  4176. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4177. $expected = array(
  4178. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4179. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4180. );
  4181. $this->assertTags($result, $expected);
  4182. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4183. $expected = array(
  4184. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4185. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4186. );
  4187. $this->assertTags($result, $expected);
  4188. $result = $this->Form->checkbox('CustomField.1.value');
  4189. $expected = array(
  4190. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4191. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4192. );
  4193. $this->assertTags($result, $expected);
  4194. }
  4195. /**
  4196. * test checkbox() with a custom name attribute
  4197. *
  4198. * @return void
  4199. */
  4200. public function testCheckboxCustomNameAttribute() {
  4201. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4202. $expected = array(
  4203. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4204. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4205. );
  4206. $this->assertTags($result, $expected);
  4207. }
  4208. /**
  4209. * test the checked option for checkboxes.
  4210. *
  4211. * @return void
  4212. */
  4213. public function testCheckboxCheckedOption() {
  4214. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4215. $expected = array(
  4216. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4217. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4218. );
  4219. $this->assertTags($result, $expected);
  4220. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4221. $expected = array(
  4222. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4223. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4224. );
  4225. $this->assertTags($result, $expected);
  4226. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4227. $expected = array(
  4228. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4229. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4230. );
  4231. $this->assertTags($result, $expected);
  4232. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4233. $expected = array(
  4234. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4235. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4236. );
  4237. $this->assertTags($result, $expected);
  4238. $this->Form->request->data['Model']['field'] = 1;
  4239. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4240. $expected = array(
  4241. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4242. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4243. );
  4244. $this->assertTags($result, $expected);
  4245. }
  4246. /**
  4247. * Test that disabled attribute works on both the checkbox and hidden input.
  4248. *
  4249. * @return void
  4250. */
  4251. public function testCheckboxDisabling() {
  4252. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4253. $expected = array(
  4254. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4255. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4256. );
  4257. $this->assertTags($result, $expected);
  4258. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4259. $expected = array(
  4260. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4261. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4262. );
  4263. $this->assertTags($result, $expected);
  4264. }
  4265. /**
  4266. * Test that the hidden input for checkboxes can be omitted or set to a
  4267. * specific value.
  4268. *
  4269. * @return void
  4270. */
  4271. public function testCheckboxHiddenField() {
  4272. $result = $this->Form->input('UserForm.something', array(
  4273. 'type' => 'checkbox',
  4274. 'hiddenField' => false
  4275. ));
  4276. $expected = array(
  4277. 'div' => array('class' => 'input checkbox'),
  4278. array('input' => array(
  4279. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4280. 'value' => '1', 'id' => 'UserFormSomething'
  4281. )),
  4282. 'label' => array('for' => 'UserFormSomething'),
  4283. 'Something',
  4284. '/label',
  4285. '/div'
  4286. );
  4287. $this->assertTags($result, $expected);
  4288. $result = $this->Form->input('UserForm.something', array(
  4289. 'type' => 'checkbox',
  4290. 'value' => 'Y',
  4291. 'hiddenField' => 'N',
  4292. ));
  4293. $expected = array(
  4294. 'div' => array('class' => 'input checkbox'),
  4295. array('input' => array(
  4296. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  4297. 'value' => 'N', 'id' => 'UserFormSomething_'
  4298. )),
  4299. array('input' => array(
  4300. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4301. 'value' => 'Y', 'id' => 'UserFormSomething'
  4302. )),
  4303. 'label' => array('for' => 'UserFormSomething'),
  4304. 'Something',
  4305. '/label',
  4306. '/div'
  4307. );
  4308. $this->assertTags($result, $expected);
  4309. }
  4310. /**
  4311. * testDateTime method
  4312. *
  4313. * Test generation of date/time select elements
  4314. *
  4315. * @return void
  4316. */
  4317. public function testDateTime() {
  4318. extract($this->dateRegex);
  4319. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4320. $now = strtotime('now');
  4321. $expected = array(
  4322. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4323. $daysRegex,
  4324. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4325. date('j', $now),
  4326. '/option',
  4327. '*/select',
  4328. '-',
  4329. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4330. $monthsRegex,
  4331. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4332. date('F', $now),
  4333. '/option',
  4334. '*/select',
  4335. '-',
  4336. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4337. $yearsRegex,
  4338. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4339. date('Y', $now),
  4340. '/option',
  4341. '*/select',
  4342. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4343. $hoursRegex,
  4344. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4345. date('g', $now),
  4346. '/option',
  4347. '*/select',
  4348. ':',
  4349. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4350. $minutesRegex,
  4351. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4352. date('i', $now),
  4353. '/option',
  4354. '*/select',
  4355. ' ',
  4356. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4357. $meridianRegex,
  4358. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4359. date('a', $now),
  4360. '/option',
  4361. '*/select'
  4362. );
  4363. $this->assertTags($result, $expected);
  4364. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4365. $expected = array(
  4366. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4367. $daysRegex,
  4368. array('option' => array('value' => '')),
  4369. '/option',
  4370. '*/select',
  4371. '-',
  4372. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4373. $monthsRegex,
  4374. array('option' => array('value' => '')),
  4375. '/option',
  4376. '*/select',
  4377. '-',
  4378. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4379. $yearsRegex,
  4380. array('option' => array('value' => '')),
  4381. '/option',
  4382. '*/select',
  4383. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4384. $hoursRegex,
  4385. array('option' => array('value' => '')),
  4386. '/option',
  4387. '*/select',
  4388. ':',
  4389. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4390. $minutesRegex,
  4391. array('option' => array('value' => '')),
  4392. '/option',
  4393. '*/select',
  4394. ' ',
  4395. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4396. $meridianRegex,
  4397. array('option' => array('value' => '')),
  4398. '/option',
  4399. '*/select'
  4400. );
  4401. $this->assertTags($result, $expected);
  4402. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4403. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  4404. $expected = array(
  4405. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4406. $daysRegex,
  4407. array('option' => array('value' => '')),
  4408. '/option',
  4409. '*/select',
  4410. '-',
  4411. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4412. $monthsRegex,
  4413. array('option' => array('value' => '')),
  4414. '/option',
  4415. '*/select',
  4416. '-',
  4417. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4418. $yearsRegex,
  4419. array('option' => array('value' => '')),
  4420. '/option',
  4421. '*/select',
  4422. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4423. $hoursRegex,
  4424. array('option' => array('value' => '')),
  4425. '/option',
  4426. '*/select',
  4427. ':',
  4428. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4429. $minutesRegex,
  4430. array('option' => array('value' => '')),
  4431. '/option',
  4432. '*/select',
  4433. ' ',
  4434. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4435. $meridianRegex,
  4436. array('option' => array('value' => '')),
  4437. '/option',
  4438. '*/select'
  4439. );
  4440. $this->assertTags($result, $expected);
  4441. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4442. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  4443. $expected = array(
  4444. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4445. $daysRegex,
  4446. array('option' => array('value' => '')),
  4447. '/option',
  4448. '*/select',
  4449. '-',
  4450. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4451. $monthsRegex,
  4452. array('option' => array('value' => '')),
  4453. '/option',
  4454. '*/select',
  4455. '-',
  4456. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4457. $yearsRegex,
  4458. array('option' => array('value' => '')),
  4459. '/option',
  4460. '*/select',
  4461. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4462. $hoursRegex,
  4463. array('option' => array('value' => '')),
  4464. '/option',
  4465. '*/select',
  4466. ':',
  4467. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4468. $minutesRegex,
  4469. array('option' => array('value' => '')),
  4470. '/option',
  4471. '*/select',
  4472. ' ',
  4473. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4474. $meridianRegex,
  4475. array('option' => array('value' => '')),
  4476. '/option',
  4477. '*/select'
  4478. );
  4479. $this->assertTags($result, $expected);
  4480. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4481. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  4482. $expected = array(
  4483. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4484. $daysRegex,
  4485. array('option' => array('value' => '')),
  4486. '/option',
  4487. '*/select',
  4488. '-',
  4489. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4490. $monthsRegex,
  4491. array('option' => array('value' => '')),
  4492. '/option',
  4493. '*/select',
  4494. '-',
  4495. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4496. $yearsRegex,
  4497. array('option' => array('value' => '')),
  4498. '/option',
  4499. '*/select',
  4500. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4501. $hoursRegex,
  4502. array('option' => array('value' => '')),
  4503. '/option',
  4504. '*/select',
  4505. ':',
  4506. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4507. $minutesRegex,
  4508. array('option' => array('value' => '')),
  4509. '/option',
  4510. array('option' => array('value' => '00')),
  4511. '00',
  4512. '/option',
  4513. array('option' => array('value' => '05')),
  4514. '05',
  4515. '/option',
  4516. array('option' => array('value' => '10')),
  4517. '10',
  4518. '/option',
  4519. '*/select',
  4520. ' ',
  4521. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4522. $meridianRegex,
  4523. array('option' => array('value' => '')),
  4524. '/option',
  4525. '*/select'
  4526. );
  4527. $this->assertTags($result, $expected);
  4528. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4529. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4530. $expected = array(
  4531. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4532. $daysRegex,
  4533. array('option' => array('value' => '')),
  4534. '/option',
  4535. '*/select',
  4536. '-',
  4537. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4538. $monthsRegex,
  4539. array('option' => array('value' => '')),
  4540. '/option',
  4541. '*/select',
  4542. '-',
  4543. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4544. $yearsRegex,
  4545. array('option' => array('value' => '')),
  4546. '/option',
  4547. '*/select',
  4548. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4549. $hoursRegex,
  4550. array('option' => array('value' => '')),
  4551. '/option',
  4552. '*/select',
  4553. ':',
  4554. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4555. $minutesRegex,
  4556. array('option' => array('value' => '')),
  4557. '/option',
  4558. array('option' => array('value' => '00')),
  4559. '00',
  4560. '/option',
  4561. array('option' => array('value' => '05')),
  4562. '05',
  4563. '/option',
  4564. array('option' => array('value' => '10')),
  4565. '10',
  4566. '/option',
  4567. '*/select',
  4568. ' ',
  4569. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4570. $meridianRegex,
  4571. array('option' => array('value' => '')),
  4572. '/option',
  4573. '*/select'
  4574. );
  4575. $this->assertTags($result, $expected);
  4576. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4577. $this->Form->request->data['Contact']['data'] = null;
  4578. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4579. $expected = array(
  4580. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4581. $daysRegex,
  4582. array('option' => array('value' => '')),
  4583. '/option',
  4584. '*/select',
  4585. '-',
  4586. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4587. $monthsRegex,
  4588. array('option' => array('value' => '')),
  4589. '/option',
  4590. '*/select',
  4591. '-',
  4592. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4593. $yearsRegex,
  4594. array('option' => array('value' => '')),
  4595. '/option',
  4596. '*/select',
  4597. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4598. $hoursRegex,
  4599. array('option' => array('value' => '')),
  4600. '/option',
  4601. '*/select',
  4602. ':',
  4603. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4604. $minutesRegex,
  4605. array('option' => array('value' => '')),
  4606. '/option',
  4607. '*/select',
  4608. ' ',
  4609. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4610. $meridianRegex,
  4611. array('option' => array('value' => '')),
  4612. '/option',
  4613. '*/select'
  4614. );
  4615. $this->assertTags($result, $expected);
  4616. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4617. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  4618. $now = strtotime($this->Form->data['Model']['field']);
  4619. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  4620. $expected = array(
  4621. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4622. $daysRegex,
  4623. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4624. date('j', $now),
  4625. '/option',
  4626. '*/select',
  4627. '-',
  4628. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4629. $monthsRegex,
  4630. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4631. date('F', $now),
  4632. '/option',
  4633. '*/select',
  4634. '-',
  4635. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  4636. $yearsRegex,
  4637. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4638. date('Y', $now),
  4639. '/option',
  4640. '*/select',
  4641. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4642. $hoursRegex,
  4643. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4644. date('g', $now),
  4645. '/option',
  4646. '*/select',
  4647. ':',
  4648. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4649. $minutesRegex,
  4650. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4651. date('i', $now),
  4652. '/option',
  4653. '*/select',
  4654. ' ',
  4655. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  4656. $meridianRegex,
  4657. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4658. date('a', $now),
  4659. '/option',
  4660. '*/select'
  4661. );
  4662. $this->assertTags($result, $expected);
  4663. $selected = strtotime('2008-10-26 12:33:00');
  4664. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  4665. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  4666. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  4667. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  4668. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  4669. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  4670. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  4671. $this->Form->create('Contact');
  4672. $result = $this->Form->input('published');
  4673. $now = strtotime('now');
  4674. $expected = array(
  4675. 'div' => array('class' => 'input date'),
  4676. 'label' => array('for' => 'ContactPublishedMonth'),
  4677. 'Published',
  4678. '/label',
  4679. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4680. $monthsRegex,
  4681. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4682. date('F', $now),
  4683. '/option',
  4684. '*/select',
  4685. '-',
  4686. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4687. $daysRegex,
  4688. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4689. date('j', $now),
  4690. '/option',
  4691. '*/select',
  4692. '-',
  4693. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4694. $yearsRegex,
  4695. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4696. date('Y', $now),
  4697. '/option',
  4698. '*/select',
  4699. '/div'
  4700. );
  4701. $this->assertTags($result, $expected);
  4702. $result = $this->Form->input('published2', array('type' => 'date'));
  4703. $now = strtotime('now');
  4704. $expected = array(
  4705. 'div' => array('class' => 'input date'),
  4706. 'label' => array('for' => 'ContactPublished2Month'),
  4707. 'Published2',
  4708. '/label',
  4709. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  4710. $monthsRegex,
  4711. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4712. date('F', $now),
  4713. '/option',
  4714. '*/select',
  4715. '-',
  4716. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  4717. $daysRegex,
  4718. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4719. date('j', $now),
  4720. '/option',
  4721. '*/select',
  4722. '-',
  4723. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  4724. $yearsRegex,
  4725. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4726. date('Y', $now),
  4727. '/option',
  4728. '*/select',
  4729. '/div'
  4730. );
  4731. $this->assertTags($result, $expected);
  4732. $this->Form->create('Contact');
  4733. $result = $this->Form->input('published', array('monthNames' => false));
  4734. $now = strtotime('now');
  4735. $expected = array(
  4736. 'div' => array('class' => 'input date'),
  4737. 'label' => array('for' => 'ContactPublishedMonth'),
  4738. 'Published',
  4739. '/label',
  4740. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4741. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4742. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4743. date('m', $now),
  4744. '/option',
  4745. '*/select',
  4746. '-',
  4747. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4748. $daysRegex,
  4749. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4750. date('j', $now),
  4751. '/option',
  4752. '*/select',
  4753. '-',
  4754. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4755. $yearsRegex,
  4756. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4757. date('Y', $now),
  4758. '/option',
  4759. '*/select',
  4760. '/div'
  4761. );
  4762. $this->assertTags($result, $expected);
  4763. $result = $this->Form->input('published', array('type' => 'time'));
  4764. $now = strtotime('now');
  4765. $expected = array(
  4766. 'div' => array('class' => 'input time'),
  4767. 'label' => array('for' => 'ContactPublishedHour'),
  4768. 'Published',
  4769. '/label',
  4770. array('select' => array('name' => 'data[Contact][published][hour]', 'id' => 'ContactPublishedHour')),
  4771. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4772. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4773. date('g', $now),
  4774. '/option',
  4775. '*/select',
  4776. ':',
  4777. );
  4778. $this->assertTags($result, $expected);
  4779. $result = $this->Form->input('published', array(
  4780. 'timeFormat' => 24,
  4781. 'interval' => 5,
  4782. 'selected' => strtotime('2009-09-03 13:37:00'),
  4783. 'type' => 'datetime'
  4784. ));
  4785. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4786. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  4787. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  4788. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  4789. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  4790. $this->assertNoErrors();
  4791. $this->Form->request->data['Contact'] = array(
  4792. 'date' => array(
  4793. 'day' => '',
  4794. 'month' => '',
  4795. 'year' => '',
  4796. 'hour' => '',
  4797. 'min' => '',
  4798. 'meridian' => ''
  4799. )
  4800. );
  4801. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4802. }
  4803. /**
  4804. * test that datetime() and default values work.
  4805. *
  4806. * @return void
  4807. */
  4808. public function testDatetimeWithDefault() {
  4809. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  4810. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4811. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4812. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4813. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  4814. 'default' => '2009-06-01 11:15:30'
  4815. ));
  4816. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4817. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4818. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4819. }
  4820. /**
  4821. * test that bogus non-date time data doesn't cause errors.
  4822. *
  4823. * @return void
  4824. */
  4825. public function testDateTimeWithBogusData() {
  4826. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  4827. $this->assertNotRegExp('/selected="selected">\d/', $result);
  4828. }
  4829. /**
  4830. * testFormDateTimeMulti method
  4831. *
  4832. * test multiple datetime element generation
  4833. *
  4834. * @return void
  4835. */
  4836. public function testFormDateTimeMulti() {
  4837. extract($this->dateRegex);
  4838. $result = $this->Form->dateTime('Contact.1.updated');
  4839. $expected = array(
  4840. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  4841. $daysRegex,
  4842. array('option' => array('value' => '')),
  4843. '/option',
  4844. '*/select',
  4845. '-',
  4846. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  4847. $monthsRegex,
  4848. array('option' => array('value' => '')),
  4849. '/option',
  4850. '*/select',
  4851. '-',
  4852. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  4853. $yearsRegex,
  4854. array('option' => array('value' => '')),
  4855. '/option',
  4856. '*/select',
  4857. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  4858. $hoursRegex,
  4859. array('option' => array('value' => '')),
  4860. '/option',
  4861. '*/select',
  4862. ':',
  4863. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  4864. $minutesRegex,
  4865. array('option' => array('value' => '')),
  4866. '/option',
  4867. '*/select',
  4868. ' ',
  4869. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  4870. $meridianRegex,
  4871. array('option' => array('value' => '')),
  4872. '/option',
  4873. '*/select'
  4874. );
  4875. $this->assertTags($result, $expected);
  4876. $result = $this->Form->dateTime('Contact.2.updated');
  4877. $expected = array(
  4878. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  4879. $daysRegex,
  4880. array('option' => array('value' => '')),
  4881. '/option',
  4882. '*/select',
  4883. '-',
  4884. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  4885. $monthsRegex,
  4886. array('option' => array('value' => '')),
  4887. '/option',
  4888. '*/select',
  4889. '-',
  4890. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  4891. $yearsRegex,
  4892. array('option' => array('value' => '')),
  4893. '/option',
  4894. '*/select',
  4895. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  4896. $hoursRegex,
  4897. array('option' => array('value' => '')),
  4898. '/option',
  4899. '*/select',
  4900. ':',
  4901. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  4902. $minutesRegex,
  4903. array('option' => array('value' => '')),
  4904. '/option',
  4905. '*/select',
  4906. ' ',
  4907. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  4908. $meridianRegex,
  4909. array('option' => array('value' => '')),
  4910. '/option',
  4911. '*/select'
  4912. );
  4913. $this->assertTags($result, $expected);
  4914. }
  4915. /**
  4916. * When changing the date format, the label should always focus the first select box when
  4917. * clicked.
  4918. *
  4919. * @return void
  4920. */
  4921. public function testDateTimeLabelIdMatchesFirstInput() {
  4922. $result = $this->Form->input('Model.date', array('type' => 'date'));
  4923. $this->assertContains('label for="ModelDateMonth"', $result);
  4924. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  4925. $this->assertContains('label for="ModelDateDay"', $result);
  4926. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  4927. $this->assertContains('label for="ModelDateYear"', $result);
  4928. }
  4929. /**
  4930. * testMonth method
  4931. *
  4932. * @return void
  4933. */
  4934. public function testMonth() {
  4935. $result = $this->Form->month('Model.field');
  4936. $expected = array(
  4937. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4938. array('option' => array('value' => '')),
  4939. '/option',
  4940. array('option' => array('value' => '01')),
  4941. date('F', strtotime('2008-01-01 00:00:00')),
  4942. '/option',
  4943. array('option' => array('value' => '02')),
  4944. date('F', strtotime('2008-02-01 00:00:00')),
  4945. '/option',
  4946. '*/select',
  4947. );
  4948. $this->assertTags($result, $expected);
  4949. $result = $this->Form->month('Model.field', array('empty' => true));
  4950. $expected = array(
  4951. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4952. array('option' => array('value' => '')),
  4953. '/option',
  4954. array('option' => array('value' => '01')),
  4955. date('F', strtotime('2008-01-01 00:00:00')),
  4956. '/option',
  4957. array('option' => array('value' => '02')),
  4958. date('F', strtotime('2008-02-01 00:00:00')),
  4959. '/option',
  4960. '*/select',
  4961. );
  4962. $this->assertTags($result, $expected);
  4963. $result = $this->Form->month('Model.field', array('monthNames' => false));
  4964. $expected = array(
  4965. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4966. array('option' => array('value' => '')),
  4967. '/option',
  4968. array('option' => array('value' => '01')),
  4969. '01',
  4970. '/option',
  4971. array('option' => array('value' => '02')),
  4972. '02',
  4973. '/option',
  4974. '*/select',
  4975. );
  4976. $this->assertTags($result, $expected);
  4977. $monthNames = array(
  4978. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  4979. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  4980. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  4981. $expected = array(
  4982. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4983. array('option' => array('value' => '')),
  4984. '/option',
  4985. array('option' => array('value' => '01')),
  4986. 'Jan',
  4987. '/option',
  4988. array('option' => array('value' => '02')),
  4989. 'Feb',
  4990. '/option',
  4991. '*/select',
  4992. );
  4993. $this->assertTags($result, $expected);
  4994. }
  4995. /**
  4996. * testDay method
  4997. *
  4998. * @return void
  4999. */
  5000. public function testDay() {
  5001. extract($this->dateRegex);
  5002. $result = $this->Form->day('Model.field', array('value' => false));
  5003. $expected = array(
  5004. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5005. array('option' => array('value' => '')),
  5006. '/option',
  5007. array('option' => array('value' => '01')),
  5008. '1',
  5009. '/option',
  5010. array('option' => array('value' => '02')),
  5011. '2',
  5012. '/option',
  5013. $daysRegex,
  5014. '/select',
  5015. );
  5016. $this->assertTags($result, $expected);
  5017. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5018. $result = $this->Form->day('Model.field');
  5019. $expected = array(
  5020. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5021. array('option' => array('value' => '')),
  5022. '/option',
  5023. array('option' => array('value' => '01')),
  5024. '1',
  5025. '/option',
  5026. array('option' => array('value' => '02')),
  5027. '2',
  5028. '/option',
  5029. $daysRegex,
  5030. array('option' => array('value' => '10', 'selected' => 'selected')),
  5031. '10',
  5032. '/option',
  5033. $daysRegex,
  5034. '/select',
  5035. );
  5036. $this->assertTags($result, $expected);
  5037. $this->Form->request->data['Model']['field'] = '';
  5038. $result = $this->Form->day('Model.field', array('value' => '10'));
  5039. $expected = array(
  5040. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5041. array('option' => array('value' => '')),
  5042. '/option',
  5043. array('option' => array('value' => '01')),
  5044. '1',
  5045. '/option',
  5046. array('option' => array('value' => '02')),
  5047. '2',
  5048. '/option',
  5049. $daysRegex,
  5050. array('option' => array('value' => '10', 'selected' => 'selected')),
  5051. '10',
  5052. '/option',
  5053. $daysRegex,
  5054. '/select',
  5055. );
  5056. $this->assertTags($result, $expected);
  5057. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5058. $result = $this->Form->day('Model.field', array('value' => true));
  5059. $expected = array(
  5060. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5061. array('option' => array('value' => '')),
  5062. '/option',
  5063. array('option' => array('value' => '01')),
  5064. '1',
  5065. '/option',
  5066. array('option' => array('value' => '02')),
  5067. '2',
  5068. '/option',
  5069. $daysRegex,
  5070. array('option' => array('value' => '10', 'selected' => 'selected')),
  5071. '10',
  5072. '/option',
  5073. $daysRegex,
  5074. '/select',
  5075. );
  5076. $this->assertTags($result, $expected);
  5077. }
  5078. /**
  5079. * testMinute method
  5080. *
  5081. * @return void
  5082. */
  5083. public function testMinute() {
  5084. extract($this->dateRegex);
  5085. $result = $this->Form->minute('Model.field');
  5086. $expected = array(
  5087. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5088. array('option' => array('value' => '')),
  5089. '/option',
  5090. array('option' => array('value' => '00')),
  5091. '00',
  5092. '/option',
  5093. array('option' => array('value' => '01')),
  5094. '01',
  5095. '/option',
  5096. array('option' => array('value' => '02')),
  5097. '02',
  5098. '/option',
  5099. $minutesRegex,
  5100. '/select',
  5101. );
  5102. $this->assertTags($result, $expected);
  5103. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5104. $result = $this->Form->minute('Model.field');
  5105. $expected = array(
  5106. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5107. array('option' => array('value' => '')),
  5108. '/option',
  5109. array('option' => array('value' => '00')),
  5110. '00',
  5111. '/option',
  5112. array('option' => array('value' => '01')),
  5113. '01',
  5114. '/option',
  5115. array('option' => array('value' => '02')),
  5116. '02',
  5117. '/option',
  5118. $minutesRegex,
  5119. array('option' => array('value' => '12', 'selected' => 'selected')),
  5120. '12',
  5121. '/option',
  5122. $minutesRegex,
  5123. '/select',
  5124. );
  5125. $this->assertTags($result, $expected);
  5126. $this->Form->request->data['Model']['field'] = '';
  5127. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5128. $expected = array(
  5129. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5130. array('option' => array('value' => '')),
  5131. '/option',
  5132. array('option' => array('value' => '00')),
  5133. '00',
  5134. '/option',
  5135. array('option' => array('value' => '05')),
  5136. '05',
  5137. '/option',
  5138. array('option' => array('value' => '10')),
  5139. '10',
  5140. '/option',
  5141. $minutesRegex,
  5142. '/select',
  5143. );
  5144. $this->assertTags($result, $expected);
  5145. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5146. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5147. $expected = array(
  5148. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5149. array('option' => array('value' => '')),
  5150. '/option',
  5151. array('option' => array('value' => '00')),
  5152. '00',
  5153. '/option',
  5154. array('option' => array('value' => '05')),
  5155. '05',
  5156. '/option',
  5157. array('option' => array('value' => '10', 'selected' => 'selected')),
  5158. '10',
  5159. '/option',
  5160. $minutesRegex,
  5161. '/select',
  5162. );
  5163. $this->assertTags($result, $expected);
  5164. }
  5165. /**
  5166. * testHour method
  5167. *
  5168. * @return void
  5169. */
  5170. public function testHour() {
  5171. extract($this->dateRegex);
  5172. $result = $this->Form->hour('Model.field', false);
  5173. $expected = array(
  5174. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5175. array('option' => array('value' => '')),
  5176. '/option',
  5177. array('option' => array('value' => '01')),
  5178. '1',
  5179. '/option',
  5180. array('option' => array('value' => '02')),
  5181. '2',
  5182. '/option',
  5183. $hoursRegex,
  5184. '/select',
  5185. );
  5186. $this->assertTags($result, $expected);
  5187. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5188. $result = $this->Form->hour('Model.field', false);
  5189. $expected = array(
  5190. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5191. array('option' => array('value' => '')),
  5192. '/option',
  5193. array('option' => array('value' => '01')),
  5194. '1',
  5195. '/option',
  5196. array('option' => array('value' => '02')),
  5197. '2',
  5198. '/option',
  5199. $hoursRegex,
  5200. array('option' => array('value' => '12', 'selected' => 'selected')),
  5201. '12',
  5202. '/option',
  5203. '/select',
  5204. );
  5205. $this->assertTags($result, $expected);
  5206. $this->Form->request->data['Model']['field'] = '';
  5207. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5208. $expected = array(
  5209. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5210. array('option' => array('value' => '')),
  5211. '/option',
  5212. array('option' => array('value' => '00')),
  5213. '0',
  5214. '/option',
  5215. array('option' => array('value' => '01')),
  5216. '1',
  5217. '/option',
  5218. array('option' => array('value' => '02')),
  5219. '2',
  5220. '/option',
  5221. $hoursRegex,
  5222. array('option' => array('value' => '23', 'selected' => 'selected')),
  5223. '23',
  5224. '/option',
  5225. '/select',
  5226. );
  5227. $this->assertTags($result, $expected);
  5228. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5229. $result = $this->Form->hour('Model.field', true);
  5230. $expected = array(
  5231. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5232. array('option' => array('value' => '')),
  5233. '/option',
  5234. array('option' => array('value' => '00', 'selected' => 'selected')),
  5235. '0',
  5236. '/option',
  5237. array('option' => array('value' => '01')),
  5238. '1',
  5239. '/option',
  5240. array('option' => array('value' => '02')),
  5241. '2',
  5242. '/option',
  5243. $hoursRegex,
  5244. '/select',
  5245. );
  5246. $this->assertTags($result, $expected);
  5247. unset($this->Form->request->data['Model']['field']);
  5248. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5249. $thisHour = date('H');
  5250. $optValue = date('G');
  5251. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5252. }
  5253. /**
  5254. * testYear method
  5255. *
  5256. * @return void
  5257. */
  5258. public function testYear() {
  5259. $result = $this->Form->year('Model.field', 2006, 2007);
  5260. $expected = array(
  5261. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5262. array('option' => array('value' => '')),
  5263. '/option',
  5264. array('option' => array('value' => '2007')),
  5265. '2007',
  5266. '/option',
  5267. array('option' => array('value' => '2006')),
  5268. '2006',
  5269. '/option',
  5270. '/select',
  5271. );
  5272. $this->assertTags($result, $expected);
  5273. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5274. $expected = array(
  5275. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5276. array('option' => array('value' => '')),
  5277. '/option',
  5278. array('option' => array('value' => '2006')),
  5279. '2006',
  5280. '/option',
  5281. array('option' => array('value' => '2007')),
  5282. '2007',
  5283. '/option',
  5284. '/select',
  5285. );
  5286. $this->assertTags($result, $expected);
  5287. $this->request->data['Contact']['published'] = '';
  5288. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5289. $expected = array(
  5290. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5291. array('option' => array('value' => '')),
  5292. '/option',
  5293. array('option' => array('value' => '2007')),
  5294. '2007',
  5295. '/option',
  5296. array('option' => array('value' => '2006')),
  5297. '2006',
  5298. '/option',
  5299. '/select',
  5300. );
  5301. $this->assertTags($result, $expected);
  5302. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5303. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5304. $expected = array(
  5305. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5306. array('option' => array('value' => '2007')),
  5307. '2007',
  5308. '/option',
  5309. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5310. '2006',
  5311. '/option',
  5312. '/select',
  5313. );
  5314. $this->assertTags($result, $expected);
  5315. $this->Form->request->data['Contact']['published'] = '';
  5316. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5317. $expected = array(
  5318. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5319. array('option' => array('value' => '')),
  5320. '/option',
  5321. array('option' => array('value' => '2007')),
  5322. '2007',
  5323. '/option',
  5324. array('option' => array('value' => '2006')),
  5325. '2006',
  5326. '/option',
  5327. '/select',
  5328. );
  5329. $this->assertTags($result, $expected);
  5330. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5331. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5332. $expected = array(
  5333. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5334. array('option' => array('value' => '2007')),
  5335. '2007',
  5336. '/option',
  5337. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5338. '2006',
  5339. '/option',
  5340. '/select',
  5341. );
  5342. $this->assertTags($result, $expected);
  5343. $this->Form->request->data['Contact']['published'] = '';
  5344. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5345. $expected = array(
  5346. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5347. array('option' => array('value' => '')),
  5348. '/option',
  5349. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5350. '2007',
  5351. '/option',
  5352. array('option' => array('value' => '2006')),
  5353. '2006',
  5354. '/option',
  5355. '/select',
  5356. );
  5357. $this->assertTags($result, $expected);
  5358. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5359. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5360. $expected = array(
  5361. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5362. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5363. '2007',
  5364. '/option',
  5365. array('option' => array('value' => '2006')),
  5366. '2006',
  5367. '/option',
  5368. '/select',
  5369. );
  5370. $this->assertTags($result, $expected);
  5371. $this->Form->request->data['Contact']['published'] = '';
  5372. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5373. $expected = array(
  5374. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5375. array('option' => array('value' => '2008')),
  5376. '2008',
  5377. '/option',
  5378. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5379. '2007',
  5380. '/option',
  5381. array('option' => array('value' => '2006')),
  5382. '2006',
  5383. '/option',
  5384. '/select',
  5385. );
  5386. $this->assertTags($result, $expected);
  5387. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5388. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5389. $expected = array(
  5390. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5391. array('option' => array('value' => '2008')),
  5392. '2008',
  5393. '/option',
  5394. array('option' => array('value' => '2007')),
  5395. '2007',
  5396. '/option',
  5397. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5398. '2006',
  5399. '/option',
  5400. '/select',
  5401. );
  5402. $this->assertTags($result, $expected);
  5403. $this->Form->request->data = array();
  5404. $this->Form->create('Contact');
  5405. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5406. $expected = array(
  5407. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5408. array('option' => array('value' => '2008')),
  5409. '2008',
  5410. '/option',
  5411. array('option' => array('value' => '2007')),
  5412. '2007',
  5413. '/option',
  5414. array('option' => array('value' => '2006')),
  5415. '2006',
  5416. '/option',
  5417. '/select',
  5418. );
  5419. $this->assertTags($result, $expected);
  5420. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  5421. $this->assertContains('data[Contact][published][year]', $result);
  5422. }
  5423. /**
  5424. * testTextArea method
  5425. *
  5426. * @return void
  5427. */
  5428. public function testTextArea() {
  5429. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  5430. $result = $this->Form->textarea('Model.field');
  5431. $expected = array(
  5432. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5433. 'some test data',
  5434. '/textarea',
  5435. );
  5436. $this->assertTags($result, $expected);
  5437. $result = $this->Form->textarea('Model.tmp');
  5438. $expected = array(
  5439. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  5440. '/textarea',
  5441. );
  5442. $this->assertTags($result, $expected);
  5443. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5444. $result = $this->Form->textarea('Model.field');
  5445. $expected = array(
  5446. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5447. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5448. '/textarea',
  5449. );
  5450. $this->assertTags($result, $expected);
  5451. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5452. $result = $this->Form->textarea('Model.field', array('escape' => false));
  5453. $expected = array(
  5454. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5455. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5456. '/textarea',
  5457. );
  5458. $this->assertTags($result, $expected);
  5459. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  5460. $result = $this->Form->textarea('Model.0.OtherModel.field');
  5461. $expected = array(
  5462. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  5463. '/textarea'
  5464. );
  5465. $this->assertTags($result, $expected);
  5466. }
  5467. /**
  5468. * testTextAreaWithStupidCharacters method
  5469. *
  5470. * test text area with non-ascii characters
  5471. *
  5472. * @return void
  5473. */
  5474. public function testTextAreaWithStupidCharacters() {
  5475. $this->loadFixtures('Post');
  5476. $result = $this->Form->input('Post.content', array(
  5477. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  5478. ));
  5479. $expected = array(
  5480. 'div' => array('class' => 'input text'),
  5481. 'label' => array('for' => 'PostContent'),
  5482. 'Current Text',
  5483. '/label',
  5484. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  5485. 'GREAT®',
  5486. '/textarea',
  5487. '/div'
  5488. );
  5489. $this->assertTags($result, $expected);
  5490. }
  5491. /**
  5492. * testHiddenField method
  5493. *
  5494. * @return void
  5495. */
  5496. public function testHiddenField() {
  5497. $Contact = ClassRegistry::getObject('Contact');
  5498. $Contact->validationErrors['field'] = 1;
  5499. $this->Form->request->data['Contact']['field'] = 'test';
  5500. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  5501. $this->assertTags($result, array(
  5502. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  5503. );
  5504. }
  5505. /**
  5506. * testFileUploadField method
  5507. *
  5508. * @return void
  5509. */
  5510. public function testFileUploadField() {
  5511. $result = $this->Form->file('Model.upload');
  5512. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5513. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  5514. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  5515. $expected = array(
  5516. 'div' => array('class' => 'input file'),
  5517. 'label' => array('for' => 'ModelUpload'),
  5518. 'Upload',
  5519. '/label',
  5520. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  5521. '/div'
  5522. );
  5523. $this->assertTags($result, $expected);
  5524. }
  5525. /**
  5526. * test File upload input on a model not used in create();
  5527. *
  5528. * @return void
  5529. */
  5530. public function testFileUploadOnOtherModel() {
  5531. $this->Form->create('ValidateUser', array('type' => 'file'));
  5532. $result = $this->Form->file('ValidateProfile.city');
  5533. $expected = array(
  5534. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  5535. );
  5536. $this->assertTags($result, $expected);
  5537. }
  5538. /**
  5539. * testButton method
  5540. *
  5541. * @return void
  5542. */
  5543. public function testButton() {
  5544. $result = $this->Form->button('Hi');
  5545. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5546. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5547. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5548. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5549. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5550. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5551. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5552. $result = $this->Form->button('No type', array('type' => false));
  5553. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5554. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  5555. $this->assertNotRegExp('/\&039/', $result);
  5556. }
  5557. /**
  5558. * Test that button() makes unlocked fields by default.
  5559. *
  5560. * @return void
  5561. */
  5562. public function testButtonUnlockedByDefault() {
  5563. $this->Form->request->params['_Token']['key'] = 'secured';
  5564. $this->Form->button('Save', array('name' => 'save'));
  5565. $this->Form->button('Clear');
  5566. $result = $this->Form->unlockField();
  5567. $this->assertEquals(array('save'), $result);
  5568. }
  5569. /**
  5570. * testPostButton method
  5571. *
  5572. * @return void
  5573. */
  5574. public function testPostButton() {
  5575. $result = $this->Form->postButton('Hi', '/controller/action');
  5576. $this->assertTags($result, array(
  5577. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5578. 'div' => array('style' => 'display:none;'),
  5579. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5580. '/div',
  5581. 'button' => array('type' => 'submit'),
  5582. 'Hi',
  5583. '/button',
  5584. '/form'
  5585. ));
  5586. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5587. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  5588. }
  5589. /**
  5590. * Test that postButton adds _Token fields.
  5591. *
  5592. * @return void
  5593. */
  5594. public function testSecurePostButton() {
  5595. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5596. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5597. $expected = array(
  5598. 'form' => array(
  5599. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5600. ),
  5601. array('div' => array('style' => 'display:none;')),
  5602. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5603. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5604. '/div',
  5605. 'button' => array('type' => 'submit'),
  5606. 'Delete',
  5607. '/button',
  5608. array('div' => array('style' => 'display:none;')),
  5609. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5610. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5611. '/div',
  5612. '/form',
  5613. );
  5614. $this->assertTags($result, $expected);
  5615. }
  5616. /**
  5617. * testPostLink method
  5618. *
  5619. * @return void
  5620. */
  5621. public function testPostLink() {
  5622. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5623. $this->assertTags($result, array(
  5624. 'form' => array(
  5625. 'method' => 'post', 'action' => '/posts/delete/1',
  5626. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5627. ),
  5628. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5629. '/form',
  5630. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5631. 'Delete',
  5632. '/a'
  5633. ));
  5634. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5635. $this->assertTags($result, array(
  5636. 'form' => array(
  5637. 'method' => 'post', 'action' => '/posts/delete/1',
  5638. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5639. ),
  5640. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5641. '/form',
  5642. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5643. 'Delete',
  5644. '/a'
  5645. ));
  5646. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5647. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  5648. }
  5649. /**
  5650. * Test that postLink adds _Token fields.
  5651. *
  5652. * @return void
  5653. */
  5654. public function testSecurePostLink() {
  5655. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5656. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5657. $expected = array(
  5658. 'form' => array(
  5659. 'method' => 'post', 'action' => '/posts/delete/1',
  5660. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5661. ),
  5662. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5663. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5664. 'div' => array('style' => 'display:none;'),
  5665. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5666. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5667. '/div',
  5668. '/form',
  5669. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5670. 'Delete',
  5671. '/a'
  5672. );
  5673. $this->assertTags($result, $expected);
  5674. }
  5675. /**
  5676. * testSubmitButton method
  5677. *
  5678. * @return void
  5679. */
  5680. public function testSubmitButton() {
  5681. $result = $this->Form->submit('');
  5682. $expected = array(
  5683. 'div' => array('class' => 'submit'),
  5684. 'input' => array('type' => 'submit', 'value' => ''),
  5685. '/div'
  5686. );
  5687. $this->assertTags($result, $expected);
  5688. $result = $this->Form->submit('Test Submit');
  5689. $expected = array(
  5690. 'div' => array('class' => 'submit'),
  5691. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5692. '/div'
  5693. );
  5694. $this->assertTags($result, $expected);
  5695. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5696. $expected = array(
  5697. 'span' => array('class' => 'submit'),
  5698. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5699. '/span'
  5700. );
  5701. $this->assertTags($result, $expected);
  5702. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5703. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5704. $this->assertTags($result, $expected);
  5705. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5706. $expected = array(
  5707. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5708. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5709. '/div'
  5710. );
  5711. $this->assertTags($result, $expected);
  5712. $result = $this->Form->submit('Next >');
  5713. $expected = array(
  5714. 'div' => array('class' => 'submit'),
  5715. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5716. '/div'
  5717. );
  5718. $this->assertTags($result, $expected);
  5719. $result = $this->Form->submit('Next >', array('escape' => false));
  5720. $expected = array(
  5721. 'div' => array('class' => 'submit'),
  5722. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5723. '/div'
  5724. );
  5725. $this->assertTags($result, $expected);
  5726. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5727. $expected = array(
  5728. 'div' => array('class' => 'submit'),
  5729. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5730. '/div'
  5731. );
  5732. $this->assertTags($result, $expected);
  5733. $before = '--before--';
  5734. $after = '--after--';
  5735. $result = $this->Form->submit('Test', array('before' => $before));
  5736. $expected = array(
  5737. 'div' => array('class' => 'submit'),
  5738. '--before--',
  5739. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5740. '/div'
  5741. );
  5742. $this->assertTags($result, $expected);
  5743. $result = $this->Form->submit('Test', array('after' => $after));
  5744. $expected = array(
  5745. 'div' => array('class' => 'submit'),
  5746. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5747. '--after--',
  5748. '/div'
  5749. );
  5750. $this->assertTags($result, $expected);
  5751. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5752. $expected = array(
  5753. 'div' => array('class' => 'submit'),
  5754. '--before--',
  5755. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5756. '--after--',
  5757. '/div'
  5758. );
  5759. $this->assertTags($result, $expected);
  5760. }
  5761. /**
  5762. * test image submit types.
  5763. *
  5764. * @return void
  5765. */
  5766. public function testSubmitImage() {
  5767. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5768. $expected = array(
  5769. 'div' => array('class' => 'submit'),
  5770. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5771. '/div'
  5772. );
  5773. $this->assertTags($result, $expected);
  5774. $result = $this->Form->submit('/relative/cake.power.gif');
  5775. $expected = array(
  5776. 'div' => array('class' => 'submit'),
  5777. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5778. '/div'
  5779. );
  5780. $this->assertTags($result, $expected);
  5781. $result = $this->Form->submit('cake.power.gif');
  5782. $expected = array(
  5783. 'div' => array('class' => 'submit'),
  5784. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5785. '/div'
  5786. );
  5787. $this->assertTags($result, $expected);
  5788. $result = $this->Form->submit('Not.an.image');
  5789. $expected = array(
  5790. 'div' => array('class' => 'submit'),
  5791. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5792. '/div'
  5793. );
  5794. $this->assertTags($result, $expected);
  5795. $after = '--after--';
  5796. $before = '--before--';
  5797. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5798. $expected = array(
  5799. 'div' => array('class' => 'submit'),
  5800. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5801. '--after--',
  5802. '/div'
  5803. );
  5804. $this->assertTags($result, $expected);
  5805. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5806. $expected = array(
  5807. 'div' => array('class' => 'submit'),
  5808. '--before--',
  5809. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5810. '/div'
  5811. );
  5812. $this->assertTags($result, $expected);
  5813. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5814. $expected = array(
  5815. 'div' => array('class' => 'submit'),
  5816. '--before--',
  5817. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5818. '--after--',
  5819. '/div'
  5820. );
  5821. $this->assertTags($result, $expected);
  5822. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  5823. $expected = array(
  5824. 'div' => array('class' => 'submit'),
  5825. '--before--',
  5826. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5827. '--after--',
  5828. '/div'
  5829. );
  5830. $this->assertTags($result, $expected);
  5831. }
  5832. /**
  5833. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5834. * be submitted at a time.
  5835. *
  5836. * @return void
  5837. */
  5838. public function testSubmitUnlockedByDefault() {
  5839. $this->Form->request->params['_Token']['key'] = 'secured';
  5840. $this->Form->submit('Go go');
  5841. $this->Form->submit('Save', array('name' => 'save'));
  5842. $result = $this->Form->unlockField();
  5843. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5844. }
  5845. /**
  5846. * Test submit image with timestamps.
  5847. *
  5848. * @return void
  5849. */
  5850. public function testSubmitImageTimestamp() {
  5851. Configure::write('Asset.timestamp', 'force');
  5852. $result = $this->Form->submit('cake.power.gif');
  5853. $expected = array(
  5854. 'div' => array('class' => 'submit'),
  5855. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  5856. '/div'
  5857. );
  5858. $this->assertTags($result, $expected);
  5859. }
  5860. /**
  5861. * test the create() method
  5862. *
  5863. * @return void
  5864. */
  5865. public function testCreate() {
  5866. $result = $this->Form->create('Contact');
  5867. $encoding = strtolower(Configure::read('App.encoding'));
  5868. $expected = array(
  5869. 'form' => array(
  5870. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5871. 'accept-charset' => $encoding
  5872. ),
  5873. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  5874. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5875. '/div'
  5876. );
  5877. $this->assertTags($result, $expected);
  5878. $result = $this->Form->create('Contact', array('type' => 'GET'));
  5879. $expected = array('form' => array(
  5880. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5881. 'accept-charset' => $encoding
  5882. ));
  5883. $this->assertTags($result, $expected);
  5884. $result = $this->Form->create('Contact', array('type' => 'get'));
  5885. $expected = array('form' => array(
  5886. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5887. 'accept-charset' => $encoding
  5888. ));
  5889. $this->assertTags($result, $expected);
  5890. $result = $this->Form->create('Contact', array('type' => 'put'));
  5891. $expected = array(
  5892. 'form' => array(
  5893. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5894. 'accept-charset' => $encoding
  5895. ),
  5896. 'div' => array('style' => 'display:none;'),
  5897. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5898. '/div'
  5899. );
  5900. $this->assertTags($result, $expected);
  5901. $result = $this->Form->create('Contact', array('type' => 'file'));
  5902. $expected = array(
  5903. 'form' => array(
  5904. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5905. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5906. ),
  5907. 'div' => array('style' => 'display:none;'),
  5908. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5909. '/div'
  5910. );
  5911. $this->assertTags($result, $expected);
  5912. $this->Form->request->data['Contact']['id'] = 1;
  5913. $this->Form->request->here = '/contacts/edit/1';
  5914. $this->Form->request['action'] = 'edit';
  5915. $result = $this->Form->create('Contact');
  5916. $expected = array(
  5917. 'form' => array(
  5918. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5919. 'accept-charset' => $encoding
  5920. ),
  5921. 'div' => array('style' => 'display:none;'),
  5922. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5923. '/div'
  5924. );
  5925. $this->assertTags($result, $expected);
  5926. $this->Form->request->data['Contact']['id'] = 1;
  5927. $this->Form->request->here = '/contacts/edit/1';
  5928. $this->Form->request['action'] = 'edit';
  5929. $result = $this->Form->create('Contact', array('type' => 'file'));
  5930. $expected = array(
  5931. 'form' => array(
  5932. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5933. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5934. ),
  5935. 'div' => array('style' => 'display:none;'),
  5936. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5937. '/div'
  5938. );
  5939. $this->assertTags($result, $expected);
  5940. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  5941. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  5942. $expected = array(
  5943. 'form' => array(
  5944. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  5945. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  5946. ),
  5947. 'div' => array('style' => 'display:none;'),
  5948. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5949. '/div'
  5950. );
  5951. $this->assertTags($result, $expected);
  5952. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  5953. $expected = array(
  5954. 'form' => array(
  5955. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  5956. 'accept-charset' => $encoding
  5957. ),
  5958. 'div' => array('style' => 'display:none;'),
  5959. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5960. '/div'
  5961. );
  5962. $this->assertTags($result, $expected);
  5963. $this->Form->request['action'] = 'add';
  5964. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  5965. $expected = array(
  5966. 'form' => array(
  5967. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  5968. 'accept-charset' => $encoding
  5969. ),
  5970. 'div' => array('style' => 'display:none;'),
  5971. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5972. '/div'
  5973. );
  5974. $this->assertTags($result, $expected);
  5975. $result = $this->Form->create('User', array('action' => 'login'));
  5976. $expected = array(
  5977. 'form' => array(
  5978. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  5979. 'accept-charset' => $encoding
  5980. ),
  5981. 'div' => array('style' => 'display:none;'),
  5982. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5983. '/div'
  5984. );
  5985. $this->assertTags($result, $expected);
  5986. $result = $this->Form->create('User', array('url' => '/users/login'));
  5987. $expected = array(
  5988. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  5989. 'div' => array('style' => 'display:none;'),
  5990. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5991. '/div'
  5992. );
  5993. $this->assertTags($result, $expected);
  5994. $this->Form->request['controller'] = 'pages';
  5995. $result = $this->Form->create('User', array('action' => 'signup'));
  5996. $expected = array(
  5997. 'form' => array(
  5998. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  5999. 'accept-charset' => $encoding
  6000. ),
  6001. 'div' => array('style' => 'display:none;'),
  6002. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6003. '/div'
  6004. );
  6005. $this->assertTags($result, $expected);
  6006. $this->Form->request->data = array();
  6007. $this->Form->request['controller'] = 'contacts';
  6008. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6009. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  6010. $expected = array(
  6011. 'form' => array(
  6012. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  6013. 'accept-charset' => 'utf-8'
  6014. ),
  6015. 'div' => array('style' => 'display:none;'),
  6016. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6017. '/div'
  6018. );
  6019. $this->assertTags($result, $expected);
  6020. }
  6021. /**
  6022. * Test the onsubmit option for create()
  6023. *
  6024. * @return void
  6025. */
  6026. public function testCreateOnSubmit() {
  6027. $this->Form->request->data = array();
  6028. $this->Form->request['controller'] = 'contacts';
  6029. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6030. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  6031. $expected = array(
  6032. 'form' => array(
  6033. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  6034. 'accept-charset' => 'utf-8'
  6035. ),
  6036. 'div' => array('style' => 'display:none;'),
  6037. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6038. '/div'
  6039. );
  6040. $this->assertTags($result, $expected);
  6041. $this->Form->request->data = array();
  6042. $this->Form->request['controller'] = 'contacts';
  6043. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6044. $result = $this->Form->create(array(
  6045. 'url' => array('action' => 'index', 'param'),
  6046. 'default' => false,
  6047. 'onsubmit' => 'someFunction();'
  6048. ));
  6049. $expected = array(
  6050. 'form' => array(
  6051. 'id' => 'ContactAddForm', 'method' => 'post',
  6052. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6053. 'action' => '/contacts/index/param',
  6054. 'accept-charset' => 'utf-8'
  6055. ),
  6056. 'div' => array('style' => 'display:none;'),
  6057. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6058. '/div'
  6059. );
  6060. $this->assertTags($result, $expected);
  6061. }
  6062. /**
  6063. * test create() with automatic url generation
  6064. *
  6065. * @return void
  6066. */
  6067. public function testCreateAutoUrl() {
  6068. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6069. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6070. $this->Form->request->base = '/base_url';
  6071. $result = $this->Form->create('Contact');
  6072. $expected = array(
  6073. 'form' => array(
  6074. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6075. 'accept-charset' => 'utf-8'
  6076. ),
  6077. 'div' => array('style' => 'display:none;'),
  6078. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6079. '/div'
  6080. );
  6081. $this->assertTags($result, $expected);
  6082. $this->Form->request['action'] = 'delete';
  6083. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6084. $this->Form->request->base = '/base_url';
  6085. $result = $this->Form->create('Contact');
  6086. $expected = array(
  6087. 'form' => array(
  6088. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6089. 'accept-charset' => 'utf-8'
  6090. ),
  6091. 'div' => array('style' => 'display:none;'),
  6092. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6093. '/div'
  6094. );
  6095. $this->assertTags($result, $expected);
  6096. }
  6097. /**
  6098. * test create() with a custom route
  6099. *
  6100. * @return void
  6101. */
  6102. public function testCreateCustomRoute() {
  6103. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6104. $encoding = strtolower(Configure::read('App.encoding'));
  6105. $result = $this->Form->create('User', array('action' => 'login'));
  6106. $expected = array(
  6107. 'form' => array(
  6108. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6109. 'accept-charset' => $encoding
  6110. ),
  6111. 'div' => array('style' => 'display:none;'),
  6112. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6113. '/div'
  6114. );
  6115. $this->assertTags($result, $expected);
  6116. }
  6117. /**
  6118. * test that inputDefaults are stored and used.
  6119. *
  6120. * @return void
  6121. */
  6122. public function testCreateWithInputDefaults() {
  6123. $this->Form->create('User', array(
  6124. 'inputDefaults' => array(
  6125. 'div' => false,
  6126. 'label' => false,
  6127. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6128. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6129. )
  6130. ));
  6131. $result = $this->Form->input('username');
  6132. $expected = array(
  6133. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6134. );
  6135. $this->assertTags($result, $expected);
  6136. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6137. $expected = array(
  6138. 'div' => array('class' => 'input text'),
  6139. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6140. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6141. '/div'
  6142. );
  6143. $this->assertTags($result, $expected);
  6144. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  6145. $expected = array(
  6146. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6147. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  6148. );
  6149. $this->assertTags($result, $expected);
  6150. $this->Form->create('User', array(
  6151. 'inputDefaults' => array(
  6152. 'div' => false,
  6153. 'label' => array('class' => 'nice', 'for' => 'changed'),
  6154. )
  6155. ));
  6156. $result = $this->Form->input('username', array('div' => true));
  6157. $expected = array(
  6158. 'div' => array('class' => 'input text'),
  6159. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  6160. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6161. '/div'
  6162. );
  6163. $this->assertTags($result, $expected);
  6164. }
  6165. /**
  6166. * test automatic accept-charset overriding
  6167. *
  6168. * @return void
  6169. */
  6170. public function testCreateWithAcceptCharset() {
  6171. $result = $this->Form->create('UserForm', array(
  6172. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  6173. )
  6174. );
  6175. $expected = array(
  6176. 'form' => array(
  6177. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6178. 'accept-charset' => 'iso-8859-1'
  6179. ),
  6180. 'div' => array('style' => 'display:none;'),
  6181. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6182. '/div'
  6183. );
  6184. $this->assertTags($result, $expected);
  6185. }
  6186. /**
  6187. * Test base form url when url param is passed with multiple parameters (&)
  6188. *
  6189. */
  6190. public function testCreateQuerystringrequest() {
  6191. $encoding = strtolower(Configure::read('App.encoding'));
  6192. $result = $this->Form->create('Contact', array(
  6193. 'type' => 'post',
  6194. 'escape' => false,
  6195. 'url' => array(
  6196. 'controller' => 'controller',
  6197. 'action' => 'action',
  6198. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6199. )
  6200. ));
  6201. $expected = array(
  6202. 'form' => array(
  6203. 'id' => 'ContactAddForm',
  6204. 'method' => 'post',
  6205. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6206. 'accept-charset' => $encoding
  6207. ),
  6208. 'div' => array('style' => 'display:none;'),
  6209. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6210. '/div'
  6211. );
  6212. $this->assertTags($result, $expected);
  6213. $result = $this->Form->create('Contact', array(
  6214. 'type' => 'post',
  6215. 'url' => array(
  6216. 'controller' => 'controller',
  6217. 'action' => 'action',
  6218. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6219. )
  6220. ));
  6221. $expected = array(
  6222. 'form' => array(
  6223. 'id' => 'ContactAddForm',
  6224. 'method' => 'post',
  6225. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6226. 'accept-charset' => $encoding
  6227. ),
  6228. 'div' => array('style' => 'display:none;'),
  6229. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6230. '/div'
  6231. );
  6232. $this->assertTags($result, $expected);
  6233. }
  6234. /**
  6235. * test that create() doesn't cause errors by multiple id's being in the primary key
  6236. * as could happen with multiple select or checkboxes.
  6237. *
  6238. * @return void
  6239. */
  6240. public function testCreateWithMultipleIdInData() {
  6241. $encoding = strtolower(Configure::read('App.encoding'));
  6242. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6243. $result = $this->Form->create('Contact');
  6244. $expected = array(
  6245. 'form' => array(
  6246. 'id' => 'ContactAddForm',
  6247. 'method' => 'post',
  6248. 'action' => '/contacts/add',
  6249. 'accept-charset' => $encoding
  6250. ),
  6251. 'div' => array('style' => 'display:none;'),
  6252. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6253. '/div'
  6254. );
  6255. $this->assertTags($result, $expected);
  6256. }
  6257. /**
  6258. * test that create() doesn't add in extra passed params.
  6259. *
  6260. * @return void
  6261. */
  6262. public function testCreatePassedArgs() {
  6263. $encoding = strtolower(Configure::read('App.encoding'));
  6264. $this->Form->request->data['Contact']['id'] = 1;
  6265. $result = $this->Form->create('Contact', array(
  6266. 'type' => 'post',
  6267. 'escape' => false,
  6268. 'url' => array(
  6269. 'action' => 'edit',
  6270. 'myparam'
  6271. )
  6272. ));
  6273. $expected = array(
  6274. 'form' => array(
  6275. 'id' => 'ContactAddForm',
  6276. 'method' => 'post',
  6277. 'action' => '/contacts/edit/myparam',
  6278. 'accept-charset' => $encoding
  6279. ),
  6280. 'div' => array('style' => 'display:none;'),
  6281. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6282. '/div'
  6283. );
  6284. $this->assertTags($result, $expected);
  6285. }
  6286. /**
  6287. * test creating a get form, and get form inputs.
  6288. *
  6289. * @return void
  6290. */
  6291. public function testGetFormCreate() {
  6292. $encoding = strtolower(Configure::read('App.encoding'));
  6293. $result = $this->Form->create('Contact', array('type' => 'get'));
  6294. $this->assertTags($result, array('form' => array(
  6295. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6296. 'accept-charset' => $encoding
  6297. )));
  6298. $result = $this->Form->text('Contact.name');
  6299. $this->assertTags($result, array('input' => array(
  6300. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6301. )));
  6302. $result = $this->Form->password('password');
  6303. $this->assertTags($result, array('input' => array(
  6304. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6305. )));
  6306. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6307. $result = $this->Form->text('user_form');
  6308. $this->assertTags($result, array('input' => array(
  6309. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6310. )));
  6311. }
  6312. /**
  6313. * test get form, and inputs when the model param is false
  6314. *
  6315. * @return void
  6316. */
  6317. public function testGetFormWithFalseModel() {
  6318. $encoding = strtolower(Configure::read('App.encoding'));
  6319. $this->Form->request['controller'] = 'contact_test';
  6320. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6321. $expected = array('form' => array(
  6322. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6323. 'accept-charset' => $encoding
  6324. ));
  6325. $this->assertTags($result, $expected);
  6326. $result = $this->Form->text('reason');
  6327. $expected = array(
  6328. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6329. );
  6330. $this->assertTags($result, $expected);
  6331. }
  6332. /**
  6333. * test that datetime() works with GET style forms.
  6334. *
  6335. * @return void
  6336. */
  6337. public function testDateTimeWithGetForms() {
  6338. extract($this->dateRegex);
  6339. $this->Form->create('Contact', array('type' => 'get'));
  6340. $result = $this->Form->datetime('created');
  6341. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6342. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6343. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6344. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6345. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6346. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6347. }
  6348. /**
  6349. * testEditFormWithData method
  6350. *
  6351. * test auto populating form elements from submitted data.
  6352. *
  6353. * @return void
  6354. */
  6355. public function testEditFormWithData() {
  6356. $this->Form->request->data = array('Person' => array(
  6357. 'id' => 1,
  6358. 'first_name' => 'Nate',
  6359. 'last_name' => 'Abele',
  6360. 'email' => 'nate@example.com'
  6361. ));
  6362. $this->Form->request->addParams(array(
  6363. 'models' => array('Person'),
  6364. 'controller' => 'people',
  6365. 'action' => 'add'
  6366. ));
  6367. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6368. $this->Form->create();
  6369. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6370. $expected = array(
  6371. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6372. 'select' => array(
  6373. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6374. ),
  6375. array('option' => array('value' => 1)), 'Nate', '/option',
  6376. array('option' => array('value' => 2)), 'Garrett', '/option',
  6377. array('option' => array('value' => 3)), 'Larry', '/option',
  6378. '/select'
  6379. );
  6380. $this->assertTags($result, $expected);
  6381. }
  6382. /**
  6383. * Test that required fields are created for various types of validation.
  6384. *
  6385. * @return void
  6386. */
  6387. public function testFormInputRequiredDetection() {
  6388. $this->Form->create('Contact');
  6389. $result = $this->Form->input('Contact.non_existing');
  6390. $expected = array(
  6391. 'div' => array('class' => 'input text'),
  6392. 'label' => array('for' => 'ContactNonExisting'),
  6393. 'Non Existing',
  6394. '/label',
  6395. 'input' => array(
  6396. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  6397. 'id' => 'ContactNonExisting'
  6398. ),
  6399. '/div'
  6400. );
  6401. $this->assertTags($result, $expected);
  6402. $result = $this->Form->input('Contact.imrequired');
  6403. $expected = array(
  6404. 'div' => array('class' => 'input text required'),
  6405. 'label' => array('for' => 'ContactImrequired'),
  6406. 'Imrequired',
  6407. '/label',
  6408. 'input' => array(
  6409. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  6410. 'id' => 'ContactImrequired'
  6411. ),
  6412. '/div'
  6413. );
  6414. $this->assertTags($result, $expected);
  6415. $result = $this->Form->input('Contact.imalsorequired');
  6416. $expected = array(
  6417. 'div' => array('class' => 'input text required'),
  6418. 'label' => array('for' => 'ContactImalsorequired'),
  6419. 'Imalsorequired',
  6420. '/label',
  6421. 'input' => array(
  6422. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  6423. 'id' => 'ContactImalsorequired'
  6424. ),
  6425. '/div'
  6426. );
  6427. $this->assertTags($result, $expected);
  6428. $result = $this->Form->input('Contact.imrequiredtoo');
  6429. $expected = array(
  6430. 'div' => array('class' => 'input text required'),
  6431. 'label' => array('for' => 'ContactImrequiredtoo'),
  6432. 'Imrequiredtoo',
  6433. '/label',
  6434. 'input' => array(
  6435. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  6436. 'id' => 'ContactImrequiredtoo'
  6437. ),
  6438. '/div'
  6439. );
  6440. $this->assertTags($result, $expected);
  6441. $result = $this->Form->input('Contact.required_one');
  6442. $expected = array(
  6443. 'div' => array('class' => 'input text required'),
  6444. 'label' => array('for' => 'ContactRequiredOne'),
  6445. 'Required One',
  6446. '/label',
  6447. 'input' => array(
  6448. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  6449. 'id' => 'ContactRequiredOne'
  6450. ),
  6451. '/div'
  6452. );
  6453. $this->assertTags($result, $expected);
  6454. $result = $this->Form->input('Contact.string_required');
  6455. $expected = array(
  6456. 'div' => array('class' => 'input text required'),
  6457. 'label' => array('for' => 'ContactStringRequired'),
  6458. 'String Required',
  6459. '/label',
  6460. 'input' => array(
  6461. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  6462. 'id' => 'ContactStringRequired'
  6463. ),
  6464. '/div'
  6465. );
  6466. $this->assertTags($result, $expected);
  6467. $result = $this->Form->input('Contact.imnotrequired');
  6468. $expected = array(
  6469. 'div' => array('class' => 'input text'),
  6470. 'label' => array('for' => 'ContactImnotrequired'),
  6471. 'Imnotrequired',
  6472. '/label',
  6473. 'input' => array(
  6474. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  6475. 'id' => 'ContactImnotrequired'
  6476. ),
  6477. '/div'
  6478. );
  6479. $this->assertTags($result, $expected);
  6480. $result = $this->Form->input('Contact.imalsonotrequired');
  6481. $expected = array(
  6482. 'div' => array('class' => 'input text'),
  6483. 'label' => array('for' => 'ContactImalsonotrequired'),
  6484. 'Imalsonotrequired',
  6485. '/label',
  6486. 'input' => array(
  6487. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  6488. 'id' => 'ContactImalsonotrequired'
  6489. ),
  6490. '/div'
  6491. );
  6492. $this->assertTags($result, $expected);
  6493. $result = $this->Form->input('Contact.imnotrequiredeither');
  6494. $expected = array(
  6495. 'div' => array('class' => 'input text'),
  6496. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6497. 'Imnotrequiredeither',
  6498. '/label',
  6499. 'input' => array(
  6500. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  6501. 'id' => 'ContactImnotrequiredeither'
  6502. ),
  6503. '/div'
  6504. );
  6505. $this->assertTags($result, $expected);
  6506. }
  6507. /**
  6508. * testFormMagicInput method
  6509. *
  6510. * @return void
  6511. */
  6512. public function testFormMagicInput() {
  6513. $encoding = strtolower(Configure::read('App.encoding'));
  6514. $result = $this->Form->create('Contact');
  6515. $expected = array(
  6516. 'form' => array(
  6517. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6518. 'accept-charset' => $encoding
  6519. ),
  6520. 'div' => array('style' => 'display:none;'),
  6521. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6522. '/div'
  6523. );
  6524. $this->assertTags($result, $expected);
  6525. $result = $this->Form->input('name');
  6526. $expected = array(
  6527. 'div' => array('class' => 'input text'),
  6528. 'label' => array('for' => 'ContactName'),
  6529. 'Name',
  6530. '/label',
  6531. 'input' => array(
  6532. 'type' => 'text', 'name' => 'data[Contact][name]',
  6533. 'id' => 'ContactName', 'maxlength' => '255'
  6534. ),
  6535. '/div'
  6536. );
  6537. $this->assertTags($result, $expected);
  6538. $result = $this->Form->input('non_existing_field_in_contact_model');
  6539. $expected = array(
  6540. 'div' => array('class' => 'input text'),
  6541. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  6542. 'Non Existing Field In Contact Model',
  6543. '/label',
  6544. 'input' => array(
  6545. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  6546. 'id' => 'ContactNonExistingFieldInContactModel'
  6547. ),
  6548. '/div'
  6549. );
  6550. $this->assertTags($result, $expected);
  6551. $result = $this->Form->input('Address.street');
  6552. $expected = array(
  6553. 'div' => array('class' => 'input text'),
  6554. 'label' => array('for' => 'AddressStreet'),
  6555. 'Street',
  6556. '/label',
  6557. 'input' => array(
  6558. 'type' => 'text', 'name' => 'data[Address][street]',
  6559. 'id' => 'AddressStreet'
  6560. ),
  6561. '/div'
  6562. );
  6563. $this->assertTags($result, $expected);
  6564. $result = $this->Form->input('Address.non_existing_field_in_model');
  6565. $expected = array(
  6566. 'div' => array('class' => 'input text'),
  6567. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  6568. 'Non Existing Field In Model',
  6569. '/label',
  6570. 'input' => array(
  6571. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  6572. 'id' => 'AddressNonExistingFieldInModel'
  6573. ),
  6574. '/div'
  6575. );
  6576. $this->assertTags($result, $expected);
  6577. $result = $this->Form->input('name', array('div' => false));
  6578. $expected = array(
  6579. 'label' => array('for' => 'ContactName'),
  6580. 'Name',
  6581. '/label',
  6582. 'input' => array(
  6583. 'type' => 'text', 'name' => 'data[Contact][name]',
  6584. 'id' => 'ContactName', 'maxlength' => '255'
  6585. )
  6586. );
  6587. $this->assertTags($result, $expected);
  6588. extract($this->dateRegex);
  6589. $now = strtotime('now');
  6590. $result = $this->Form->input('Contact.published', array('div' => false));
  6591. $expected = array(
  6592. 'label' => array('for' => 'ContactPublishedMonth'),
  6593. 'Published',
  6594. '/label',
  6595. array('select' => array(
  6596. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  6597. )),
  6598. $monthsRegex,
  6599. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6600. date('F', $now),
  6601. '/option',
  6602. '*/select',
  6603. '-',
  6604. array('select' => array(
  6605. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  6606. )),
  6607. $daysRegex,
  6608. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6609. date('j', $now),
  6610. '/option',
  6611. '*/select',
  6612. '-',
  6613. array('select' => array(
  6614. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  6615. )),
  6616. $yearsRegex,
  6617. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6618. date('Y', $now),
  6619. '*/select'
  6620. );
  6621. $this->assertTags($result, $expected);
  6622. $result = $this->Form->input('Contact.updated', array('div' => false));
  6623. $expected = array(
  6624. 'label' => array('for' => 'ContactUpdatedMonth'),
  6625. 'Updated',
  6626. '/label',
  6627. array('select' => array(
  6628. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  6629. )),
  6630. $monthsRegex,
  6631. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6632. date('F', $now),
  6633. '/option',
  6634. '*/select',
  6635. '-',
  6636. array('select' => array(
  6637. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  6638. )),
  6639. $daysRegex,
  6640. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6641. date('j', $now),
  6642. '/option',
  6643. '*/select',
  6644. '-',
  6645. array('select' => array(
  6646. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  6647. )),
  6648. $yearsRegex,
  6649. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6650. date('Y', $now),
  6651. '*/select'
  6652. );
  6653. $this->assertTags($result, $expected);
  6654. $result = $this->Form->input('UserForm.stuff');
  6655. $expected = array(
  6656. 'div' => array('class' => 'input text'),
  6657. 'label' => array('for' => 'UserFormStuff'),
  6658. 'Stuff',
  6659. '/label',
  6660. 'input' => array(
  6661. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  6662. 'id' => 'UserFormStuff', 'maxlength' => 10
  6663. ),
  6664. '/div'
  6665. );
  6666. $this->assertTags($result, $expected);
  6667. }
  6668. /**
  6669. * testForMagicInputNonExistingNorValidated method
  6670. *
  6671. * @return void
  6672. */
  6673. public function testForMagicInputNonExistingNorValidated() {
  6674. $encoding = strtolower(Configure::read('App.encoding'));
  6675. $result = $this->Form->create('Contact');
  6676. $expected = array(
  6677. 'form' => array(
  6678. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6679. 'accept-charset' => $encoding
  6680. ),
  6681. 'div' => array('style' => 'display:none;'),
  6682. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6683. '/div'
  6684. );
  6685. $this->assertTags($result, $expected);
  6686. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6687. $expected = array(
  6688. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6689. 'Non Existing Nor Validated',
  6690. '/label',
  6691. 'input' => array(
  6692. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6693. 'id' => 'ContactNonExistingNorValidated'
  6694. )
  6695. );
  6696. $this->assertTags($result, $expected);
  6697. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6698. 'div' => false, 'value' => 'my value'
  6699. ));
  6700. $expected = array(
  6701. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6702. 'Non Existing Nor Validated',
  6703. '/label',
  6704. 'input' => array(
  6705. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6706. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6707. )
  6708. );
  6709. $this->assertTags($result, $expected);
  6710. $this->Form->request->data = array(
  6711. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6712. ));
  6713. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6714. $expected = array(
  6715. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6716. 'Non Existing Nor Validated',
  6717. '/label',
  6718. 'input' => array(
  6719. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6720. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6721. )
  6722. );
  6723. $this->assertTags($result, $expected);
  6724. }
  6725. /**
  6726. * testFormMagicInputLabel method
  6727. *
  6728. * @return void
  6729. */
  6730. public function testFormMagicInputLabel() {
  6731. $encoding = strtolower(Configure::read('App.encoding'));
  6732. $result = $this->Form->create('Contact');
  6733. $expected = array(
  6734. 'form' => array(
  6735. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6736. 'accept-charset' => $encoding
  6737. ),
  6738. 'div' => array('style' => 'display:none;'),
  6739. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6740. '/div'
  6741. );
  6742. $this->assertTags($result, $expected);
  6743. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  6744. $this->assertTags($result, array('input' => array(
  6745. 'name' => 'data[Contact][name]', 'type' => 'text',
  6746. 'id' => 'ContactName', 'maxlength' => '255')
  6747. ));
  6748. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  6749. $expected = array(
  6750. 'label' => array('for' => 'ContactName'),
  6751. 'My label',
  6752. '/label',
  6753. 'input' => array(
  6754. 'type' => 'text', 'name' => 'data[Contact][name]',
  6755. 'id' => 'ContactName', 'maxlength' => '255'
  6756. )
  6757. );
  6758. $this->assertTags($result, $expected);
  6759. $result = $this->Form->input('Contact.name', array(
  6760. 'div' => false, 'label' => array('class' => 'mandatory')
  6761. ));
  6762. $expected = array(
  6763. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6764. 'Name',
  6765. '/label',
  6766. 'input' => array(
  6767. 'type' => 'text', 'name' => 'data[Contact][name]',
  6768. 'id' => 'ContactName', 'maxlength' => '255'
  6769. )
  6770. );
  6771. $this->assertTags($result, $expected);
  6772. $result = $this->Form->input('Contact.name', array(
  6773. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  6774. ));
  6775. $expected = array(
  6776. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6777. 'My label',
  6778. '/label',
  6779. 'input' => array(
  6780. 'type' => 'text', 'name' => 'data[Contact][name]',
  6781. 'id' => 'ContactName', 'maxlength' => '255'
  6782. )
  6783. );
  6784. $this->assertTags($result, $expected);
  6785. $result = $this->Form->input('Contact.name', array(
  6786. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  6787. ));
  6788. $expected = array(
  6789. 'label' => array('for' => 'my_id'),
  6790. 'Name',
  6791. '/label',
  6792. 'input' => array(
  6793. 'type' => 'text', 'name' => 'data[Contact][name]',
  6794. 'id' => 'my_id', 'maxlength' => '255'
  6795. )
  6796. );
  6797. $this->assertTags($result, $expected);
  6798. $result = $this->Form->input('1.id');
  6799. $this->assertTags($result, array('input' => array(
  6800. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6801. 'id' => 'Contact1Id'
  6802. )));
  6803. $result = $this->Form->input("1.name");
  6804. $expected = array(
  6805. 'div' => array('class' => 'input text'),
  6806. 'label' => array('for' => 'Contact1Name'),
  6807. 'Name',
  6808. '/label',
  6809. 'input' => array(
  6810. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  6811. 'id' => 'Contact1Name', 'maxlength' => '255'
  6812. ),
  6813. '/div'
  6814. );
  6815. $this->assertTags($result, $expected);
  6816. $result = $this->Form->input('Contact.1.id');
  6817. $this->assertTags($result, array(
  6818. 'input' => array(
  6819. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6820. 'id' => 'Contact1Id'
  6821. )
  6822. ));
  6823. $result = $this->Form->input("Model.1.name");
  6824. $expected = array(
  6825. 'div' => array('class' => 'input text'),
  6826. 'label' => array('for' => 'Model1Name'),
  6827. 'Name',
  6828. '/label',
  6829. 'input' => array(
  6830. 'type' => 'text', 'name' => 'data[Model][1][name]',
  6831. 'id' => 'Model1Name'
  6832. ),
  6833. '/div'
  6834. );
  6835. $this->assertTags($result, $expected);
  6836. }
  6837. /**
  6838. * testFormEnd method
  6839. *
  6840. * @return void
  6841. */
  6842. public function testFormEnd() {
  6843. $this->assertEquals('</form>', $this->Form->end());
  6844. $result = $this->Form->end('');
  6845. $expected = array(
  6846. 'div' => array('class' => 'submit'),
  6847. 'input' => array('type' => 'submit', 'value' => ''),
  6848. '/div',
  6849. '/form'
  6850. );
  6851. $this->assertTags($result, $expected);
  6852. $result = $this->Form->end(array('label' => ''));
  6853. $expected = array(
  6854. 'div' => array('class' => 'submit'),
  6855. 'input' => array('type' => 'submit', 'value' => ''),
  6856. '/div',
  6857. '/form'
  6858. );
  6859. $this->assertTags($result, $expected);
  6860. $result = $this->Form->end('save');
  6861. $expected = array(
  6862. 'div' => array('class' => 'submit'),
  6863. 'input' => array('type' => 'submit', 'value' => 'save'),
  6864. '/div',
  6865. '/form'
  6866. );
  6867. $this->assertTags($result, $expected);
  6868. $result = $this->Form->end(array('label' => 'save'));
  6869. $expected = array(
  6870. 'div' => array('class' => 'submit'),
  6871. 'input' => array('type' => 'submit', 'value' => 'save'),
  6872. '/div',
  6873. '/form'
  6874. );
  6875. $this->assertTags($result, $expected);
  6876. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  6877. $expected = array(
  6878. 'div' => array('class' => 'submit'),
  6879. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6880. '/div',
  6881. '/form'
  6882. );
  6883. $this->assertTags($result, $expected);
  6884. $result = $this->Form->end(array('name' => 'Whatever'));
  6885. $expected = array(
  6886. 'div' => array('class' => 'submit'),
  6887. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  6888. '/div',
  6889. '/form'
  6890. );
  6891. $this->assertTags($result, $expected);
  6892. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  6893. $expected = array(
  6894. 'div' => array('class' => 'good'),
  6895. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6896. '/div',
  6897. '/form'
  6898. );
  6899. $this->assertTags($result, $expected);
  6900. $result = $this->Form->end(array(
  6901. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  6902. ));
  6903. $expected = array(
  6904. 'div' => array('class' => 'good'),
  6905. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6906. '/div',
  6907. '/form'
  6908. );
  6909. $this->assertTags($result, $expected);
  6910. }
  6911. /**
  6912. * testMultipleFormWithIdFields method
  6913. *
  6914. * @return void
  6915. */
  6916. public function testMultipleFormWithIdFields() {
  6917. $this->Form->create('UserForm');
  6918. $result = $this->Form->input('id');
  6919. $this->assertTags($result, array('input' => array(
  6920. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  6921. )));
  6922. $result = $this->Form->input('ValidateItem.id');
  6923. $this->assertTags($result, array('input' => array(
  6924. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  6925. 'id' => 'ValidateItemId'
  6926. )));
  6927. $result = $this->Form->input('ValidateUser.id');
  6928. $this->assertTags($result, array('input' => array(
  6929. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  6930. 'id' => 'ValidateUserId'
  6931. )));
  6932. }
  6933. /**
  6934. * testDbLessModel method
  6935. *
  6936. * @return void
  6937. */
  6938. public function testDbLessModel() {
  6939. $this->Form->create('TestMail');
  6940. $result = $this->Form->input('name');
  6941. $expected = array(
  6942. 'div' => array('class' => 'input text'),
  6943. 'label' => array('for' => 'TestMailName'),
  6944. 'Name',
  6945. '/label',
  6946. 'input' => array(
  6947. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6948. 'id' => 'TestMailName'
  6949. ),
  6950. '/div'
  6951. );
  6952. $this->assertTags($result, $expected);
  6953. ClassRegistry::init('TestMail');
  6954. $this->Form->create('TestMail');
  6955. $result = $this->Form->input('name');
  6956. $expected = array(
  6957. 'div' => array('class' => 'input text'),
  6958. 'label' => array('for' => 'TestMailName'),
  6959. 'Name',
  6960. '/label',
  6961. 'input' => array(
  6962. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6963. 'id' => 'TestMailName'
  6964. ),
  6965. '/div'
  6966. );
  6967. $this->assertTags($result, $expected);
  6968. }
  6969. /**
  6970. * testBrokenness method
  6971. *
  6972. * @return void
  6973. */
  6974. public function testBrokenness() {
  6975. /*
  6976. * #4 This test has two parents and four children. By default (as of r7117) both
  6977. * parents are show but the first parent is missing a child. This is the inconsistency
  6978. * in the default behaviour - one parent has all children, the other does not - dependent
  6979. * on the data values.
  6980. */
  6981. $result = $this->Form->select('Model.field', array(
  6982. 'Fred' => array(
  6983. 'freds_son_1' => 'Fred',
  6984. 'freds_son_2' => 'Freddie'
  6985. ),
  6986. 'Bert' => array(
  6987. 'berts_son_1' => 'Albert',
  6988. 'berts_son_2' => 'Bertie')
  6989. ),
  6990. array('showParents' => true, 'empty' => false)
  6991. );
  6992. $expected = array(
  6993. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6994. array('optgroup' => array('label' => 'Fred')),
  6995. array('option' => array('value' => 'freds_son_1')),
  6996. 'Fred',
  6997. '/option',
  6998. array('option' => array('value' => 'freds_son_2')),
  6999. 'Freddie',
  7000. '/option',
  7001. '/optgroup',
  7002. array('optgroup' => array('label' => 'Bert')),
  7003. array('option' => array('value' => 'berts_son_1')),
  7004. 'Albert',
  7005. '/option',
  7006. array('option' => array('value' => 'berts_son_2')),
  7007. 'Bertie',
  7008. '/option',
  7009. '/optgroup',
  7010. '/select'
  7011. );
  7012. $this->assertTags($result, $expected);
  7013. /*
  7014. * #2 This is structurally identical to the test above (#1) - only the parent name has
  7015. * changed, so we should expect the same select list data, just with a different name
  7016. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  7017. * This is where data corruption can occur, because when a select value is missing from
  7018. * a list a form will substitute the first value in the list - without the user knowing.
  7019. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  7020. * affect the availability of 3 => 'Three' as a valid option.
  7021. */
  7022. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  7023. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  7024. ));
  7025. $result = $this->Form->select(
  7026. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  7027. );
  7028. $expected = array(
  7029. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7030. array('option' => array('value' => 1)),
  7031. 'One',
  7032. '/option',
  7033. array('option' => array('value' => 2)),
  7034. 'Two',
  7035. '/option',
  7036. array('optgroup' => array('label' => 'Three')),
  7037. array('option' => array('value' => 3)),
  7038. 'Three',
  7039. '/option',
  7040. array('option' => array('value' => 4)),
  7041. 'Four',
  7042. '/option',
  7043. array('option' => array('value' => 5)),
  7044. 'Five',
  7045. '/option',
  7046. '/optgroup',
  7047. '/select'
  7048. );
  7049. $this->assertTags($result, $expected);
  7050. }
  7051. /**
  7052. * Test the generation of fields for a multi record form.
  7053. *
  7054. * @return void
  7055. */
  7056. public function testMultiRecordForm() {
  7057. $this->Form->create('ValidateProfile');
  7058. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  7059. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  7060. $expected = array(
  7061. 'div' => array('class' => 'input textarea'),
  7062. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  7063. 'Name',
  7064. '/label',
  7065. 'textarea' => array(
  7066. 'id' => 'ValidateProfile1ValidateItem2Name',
  7067. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  7068. 'cols' => 30,
  7069. 'rows' => 6
  7070. ),
  7071. 'Value',
  7072. '/textarea',
  7073. '/div'
  7074. );
  7075. $this->assertTags($result, $expected);
  7076. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
  7077. $expected = array(
  7078. 'div' => array('class' => 'input date'),
  7079. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7080. 'Created',
  7081. '/label',
  7082. array('select' => array(
  7083. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  7084. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7085. )
  7086. ),
  7087. array('option' => array('value' => '')), '/option',
  7088. $this->dateRegex['monthsRegex'],
  7089. '/select', '-',
  7090. array('select' => array(
  7091. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  7092. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7093. )
  7094. ),
  7095. array('option' => array('value' => '')), '/option',
  7096. $this->dateRegex['daysRegex'],
  7097. '/select', '-',
  7098. array('select' => array(
  7099. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  7100. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7101. )
  7102. ),
  7103. array('option' => array('value' => '')), '/option',
  7104. $this->dateRegex['yearsRegex'],
  7105. '/select',
  7106. '/div'
  7107. );
  7108. $this->assertTags($result, $expected);
  7109. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7110. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7111. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7112. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7113. $expected = array(
  7114. 'div' => array('class' => 'input select error'),
  7115. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7116. 'Profile',
  7117. '/label',
  7118. 'select' => array(
  7119. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7120. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7121. 'class' => 'form-error'
  7122. ),
  7123. '/select',
  7124. array('div' => array('class' => 'error-message')),
  7125. 'Error',
  7126. '/div',
  7127. '/div'
  7128. );
  7129. $this->assertTags($result, $expected);
  7130. }
  7131. /**
  7132. * test the correct display of multi-record form validation errors.
  7133. *
  7134. * @return void
  7135. */
  7136. public function testMultiRecordFormValidationErrors() {
  7137. $this->Form->create('ValidateProfile');
  7138. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7139. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7140. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7141. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7142. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7143. $result = $this->Form->error('ValidateProfile.2.city');
  7144. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7145. $result = $this->Form->error('2.city');
  7146. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7147. }
  7148. /**
  7149. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7150. *
  7151. * @return void
  7152. */
  7153. public function testInputTemplate() {
  7154. $result = $this->Form->input('Contact.email', array(
  7155. 'type' => 'text', 'format' => array('input')
  7156. ));
  7157. $expected = array(
  7158. 'div' => array('class' => 'input text'),
  7159. 'input' => array(
  7160. 'type' => 'text', 'name' => 'data[Contact][email]',
  7161. 'id' => 'ContactEmail'
  7162. ),
  7163. '/div'
  7164. );
  7165. $this->assertTags($result, $expected);
  7166. $result = $this->Form->input('Contact.email', array(
  7167. 'type' => 'text', 'format' => array('input', 'label'),
  7168. 'label' => '<em>Email (required)</em>'
  7169. ));
  7170. $expected = array(
  7171. 'div' => array('class' => 'input text'),
  7172. array('input' => array(
  7173. 'type' => 'text', 'name' => 'data[Contact][email]',
  7174. 'id' => 'ContactEmail'
  7175. )),
  7176. 'label' => array('for' => 'ContactEmail'),
  7177. 'em' => array(),
  7178. 'Email (required)',
  7179. '/em',
  7180. '/label',
  7181. '/div'
  7182. );
  7183. $this->assertTags($result, $expected);
  7184. $result = $this->Form->input('Contact.email', array(
  7185. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7186. 'between' => '<div>Something in the middle</div>',
  7187. 'after' => '<span>Some text at the end</span>'
  7188. ));
  7189. $expected = array(
  7190. 'div' => array('class' => 'input text'),
  7191. array('input' => array(
  7192. 'type' => 'text', 'name' => 'data[Contact][email]',
  7193. 'id' => 'ContactEmail'
  7194. )),
  7195. array('div' => array()),
  7196. 'Something in the middle',
  7197. '/div',
  7198. 'label' => array('for' => 'ContactEmail'),
  7199. 'Email',
  7200. '/label',
  7201. 'span' => array(),
  7202. 'Some text at the end',
  7203. '/span',
  7204. '/div'
  7205. );
  7206. $this->assertTags($result, $expected);
  7207. $result = $this->Form->input('Contact.method', array(
  7208. 'type' => 'radio',
  7209. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7210. 'between' => 'I am between',
  7211. ));
  7212. $expected = array(
  7213. 'div' => array('class' => 'input radio'),
  7214. 'fieldset' => array(),
  7215. 'legend' => array(),
  7216. 'Method',
  7217. '/legend',
  7218. 'I am between',
  7219. 'input' => array(
  7220. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  7221. 'value' => '', 'id' => 'ContactMethod_'
  7222. ),
  7223. array('input' => array(
  7224. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7225. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7226. )),
  7227. array('label' => array('for' => 'ContactMethodEmail')),
  7228. 'Email',
  7229. '/label',
  7230. array('input' => array(
  7231. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7232. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7233. )),
  7234. array('label' => array('for' => 'ContactMethodPigeon')),
  7235. 'Pigeon',
  7236. '/label',
  7237. '/fieldset',
  7238. '/div',
  7239. );
  7240. $this->assertTags($result, $expected);
  7241. }
  7242. /**
  7243. * test that some html5 inputs + FormHelper::__call() work
  7244. *
  7245. * @return void
  7246. */
  7247. public function testHtml5Inputs() {
  7248. $result = $this->Form->email('User.email');
  7249. $expected = array(
  7250. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  7251. );
  7252. $this->assertTags($result, $expected);
  7253. $result = $this->Form->search('User.query');
  7254. $expected = array(
  7255. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  7256. );
  7257. $this->assertTags($result, $expected);
  7258. $result = $this->Form->search('User.query', array('value' => 'test'));
  7259. $expected = array(
  7260. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7261. );
  7262. $this->assertTags($result, $expected);
  7263. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7264. $expected = array(
  7265. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7266. );
  7267. $this->assertTags($result, $expected);
  7268. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  7269. $expected = array(
  7270. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7271. );
  7272. $this->assertTags($result, $expected);
  7273. }
  7274. /**
  7275. *
  7276. * @expectedException CakeException
  7277. * @return void
  7278. */
  7279. public function testHtml5InputException() {
  7280. $this->Form->email();
  7281. }
  7282. /**
  7283. * Tests that a model can be loaded from the model names passed in the request object
  7284. *
  7285. * @return void
  7286. */
  7287. public function testIntrospectModelFromRequest() {
  7288. $this->loadFixtures('Post');
  7289. App::build(array(
  7290. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  7291. ));
  7292. CakePlugin::load('TestPlugin');
  7293. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  7294. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  7295. $this->Form->create('TestPluginPost');
  7296. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  7297. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  7298. CakePlugin::unload();
  7299. App::build();
  7300. }
  7301. /**
  7302. * Tests that it is possible to set the validation errors directly in the helper for a field
  7303. *
  7304. * @return void
  7305. */
  7306. public function testCustomValidationErrors() {
  7307. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7308. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7309. $this->assertEquals('Badness!', $result);
  7310. }
  7311. /**
  7312. * Tests that the 'on' key validates as expected on create
  7313. *
  7314. * @return void
  7315. */
  7316. public function testRequiredOnCreate() {
  7317. $this->Form->create('Contact');
  7318. $result = $this->Form->input('Contact.imrequiredonupdate');
  7319. $expected = array(
  7320. 'div' => array('class' => 'input text'),
  7321. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7322. 'Imrequiredonupdate',
  7323. '/label',
  7324. 'input' => array(
  7325. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7326. 'id' => 'ContactImrequiredonupdate'
  7327. ),
  7328. '/div'
  7329. );
  7330. $this->assertTags($result, $expected);
  7331. $result = $this->Form->input('Contact.imrequiredoncreate');
  7332. $expected = array(
  7333. 'div' => array('class' => 'input text required'),
  7334. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7335. 'Imrequiredoncreate',
  7336. '/label',
  7337. 'input' => array(
  7338. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7339. 'id' => 'ContactImrequiredoncreate'
  7340. ),
  7341. '/div'
  7342. );
  7343. $this->assertTags($result, $expected);
  7344. $result = $this->Form->input('Contact.imrequiredonboth');
  7345. $expected = array(
  7346. 'div' => array('class' => 'input text required'),
  7347. 'label' => array('for' => 'ContactImrequiredonboth'),
  7348. 'Imrequiredonboth',
  7349. '/label',
  7350. 'input' => array(
  7351. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7352. 'id' => 'ContactImrequiredonboth'
  7353. ),
  7354. '/div'
  7355. );
  7356. $this->assertTags($result, $expected);
  7357. $result = $this->Form->input('Contact.imrequired');
  7358. $expected = array(
  7359. 'div' => array('class' => 'input text required'),
  7360. 'label' => array('for' => 'ContactImrequired'),
  7361. 'Imrequired',
  7362. '/label',
  7363. 'input' => array(
  7364. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7365. 'id' => 'ContactImrequired'
  7366. ),
  7367. '/div'
  7368. );
  7369. $this->assertTags($result, $expected);
  7370. }
  7371. /**
  7372. * Tests that the 'on' key validates as expected on update
  7373. *
  7374. * @return void
  7375. */
  7376. public function testRequiredOnUpdate() {
  7377. $this->Form->request->data['Contact']['id'] = 1;
  7378. $this->Form->create('Contact');
  7379. $result = $this->Form->input('Contact.imrequiredonupdate');
  7380. $expected = array(
  7381. 'div' => array('class' => 'input text required'),
  7382. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7383. 'Imrequiredonupdate',
  7384. '/label',
  7385. 'input' => array(
  7386. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7387. 'id' => 'ContactImrequiredonupdate'
  7388. ),
  7389. '/div'
  7390. );
  7391. $this->assertTags($result, $expected);
  7392. $result = $this->Form->input('Contact.imrequiredoncreate');
  7393. $expected = array(
  7394. 'div' => array('class' => 'input text'),
  7395. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7396. 'Imrequiredoncreate',
  7397. '/label',
  7398. 'input' => array(
  7399. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7400. 'id' => 'ContactImrequiredoncreate'
  7401. ),
  7402. '/div'
  7403. );
  7404. $this->assertTags($result, $expected);
  7405. $result = $this->Form->input('Contact.imrequiredonboth');
  7406. $expected = array(
  7407. 'div' => array('class' => 'input text required'),
  7408. 'label' => array('for' => 'ContactImrequiredonboth'),
  7409. 'Imrequiredonboth',
  7410. '/label',
  7411. 'input' => array(
  7412. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7413. 'id' => 'ContactImrequiredonboth'
  7414. ),
  7415. '/div'
  7416. );
  7417. $this->assertTags($result, $expected);
  7418. $result = $this->Form->input('Contact.imrequired');
  7419. $expected = array(
  7420. 'div' => array('class' => 'input text required'),
  7421. 'label' => array('for' => 'ContactImrequired'),
  7422. 'Imrequired',
  7423. '/label',
  7424. 'input' => array(
  7425. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7426. 'id' => 'ContactImrequired'
  7427. ),
  7428. '/div'
  7429. );
  7430. $this->assertTags($result, $expected);
  7431. }
  7432. }