FormHelperTest.php 263 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc.
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc.
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.View.Helper
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('ClassRegistry', 'Utility');
  20. App::uses('Controller', 'Controller');
  21. App::uses('View', 'View');
  22. App::uses('Model', 'Model');
  23. App::uses('Security', 'Utility');
  24. App::uses('CakeRequest', 'Network');
  25. App::uses('HtmlHelper', 'View/Helper');
  26. App::uses('FormHelper', 'View/Helper');
  27. App::uses('Router', 'Routing');
  28. /**
  29. * ContactTestController class
  30. *
  31. * @package cake
  32. * @package Cake.Test.Case.View.Helper
  33. */
  34. class ContactTestController extends Controller {
  35. /**
  36. * name property
  37. *
  38. * @var string 'ContactTest'
  39. */
  40. public $name = 'ContactTest';
  41. /**
  42. * uses property
  43. *
  44. * @var mixed null
  45. */
  46. public $uses = null;
  47. }
  48. /**
  49. * Contact class
  50. *
  51. * @package cake
  52. * @package Cake.Test.Case.View.Helper
  53. */
  54. class Contact extends CakeTestModel {
  55. /**
  56. * primaryKey property
  57. *
  58. * @var string 'id'
  59. */
  60. public $primaryKey = 'id';
  61. /**
  62. * useTable property
  63. *
  64. * @var bool false
  65. */
  66. public $useTable = false;
  67. /**
  68. * name property
  69. *
  70. * @var string 'Contact'
  71. */
  72. public $name = 'Contact';
  73. /**
  74. * Default schema
  75. *
  76. * @var array
  77. */
  78. protected $_schema = array(
  79. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  80. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  81. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  82. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  83. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  84. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  85. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  86. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  87. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  88. );
  89. /**
  90. * validate property
  91. *
  92. * @var array
  93. */
  94. public $validate = array(
  95. 'non_existing' => array(),
  96. 'idontexist' => array(),
  97. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  98. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  99. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  100. 'imrequiredonboth' => array(
  101. 'required' => array('rule' => 'alphaNumeric'),
  102. ),
  103. 'string_required' => 'notEmpty',
  104. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  105. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  106. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  107. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  108. 'imalsonotrequired' => array(
  109. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  110. 'between' => array('rule' => array('between', 5, 30)),
  111. ),
  112. 'imalsonotrequired2' => array(
  113. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  114. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  115. ),
  116. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  117. 'iamrequiredalways' => array(
  118. 'email' => array('rule' => 'email'),
  119. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  120. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  121. ),
  122. );
  123. /**
  124. * schema method
  125. *
  126. * @return void
  127. */
  128. public function setSchema($schema) {
  129. $this->_schema = $schema;
  130. }
  131. /**
  132. * hasAndBelongsToMany property
  133. *
  134. * @var array
  135. */
  136. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  137. /**
  138. * hasAndBelongsToMany property
  139. *
  140. * @var array
  141. */
  142. public $belongsTo = array('User' => array('className' => 'UserForm'));
  143. }
  144. /**
  145. * ContactTagsContact class
  146. *
  147. * @package cake
  148. * @package Cake.Test.Case.View.Helper
  149. */
  150. class ContactTagsContact extends CakeTestModel {
  151. /**
  152. * useTable property
  153. *
  154. * @var bool false
  155. */
  156. public $useTable = false;
  157. /**
  158. * name property
  159. *
  160. * @var string 'Contact'
  161. */
  162. public $name = 'ContactTagsContact';
  163. /**
  164. * Default schema
  165. *
  166. * @var array
  167. */
  168. protected $_schema = array(
  169. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  170. 'contact_tag_id' => array(
  171. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  172. )
  173. );
  174. /**
  175. * schema method
  176. *
  177. * @return void
  178. */
  179. public function setSchema($schema) {
  180. $this->_schema = $schema;
  181. }
  182. }
  183. /**
  184. * ContactNonStandardPk class
  185. *
  186. * @package cake
  187. * @package Cake.Test.Case.View.Helper
  188. */
  189. class ContactNonStandardPk extends Contact {
  190. /**
  191. * primaryKey property
  192. *
  193. * @var string 'pk'
  194. */
  195. public $primaryKey = 'pk';
  196. /**
  197. * name property
  198. *
  199. * @var string 'ContactNonStandardPk'
  200. */
  201. public $name = 'ContactNonStandardPk';
  202. /**
  203. * schema method
  204. *
  205. * @return void
  206. */
  207. public function schema($field = false) {
  208. $this->_schema = parent::schema();
  209. $this->_schema['pk'] = $this->_schema['id'];
  210. unset($this->_schema['id']);
  211. return $this->_schema;
  212. }
  213. }
  214. /**
  215. * ContactTag class
  216. *
  217. * @package cake
  218. * @package Cake.Test.Case.View.Helper
  219. */
  220. class ContactTag extends Model {
  221. /**
  222. * useTable property
  223. *
  224. * @var bool false
  225. */
  226. public $useTable = false;
  227. /**
  228. * schema definition
  229. *
  230. * @var array
  231. */
  232. protected $_schema = array(
  233. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  234. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  235. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  236. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  237. );
  238. }
  239. /**
  240. * UserForm class
  241. *
  242. * @package cake
  243. * @package Cake.Test.Case.View.Helper
  244. */
  245. class UserForm extends CakeTestModel {
  246. /**
  247. * useTable property
  248. *
  249. * @var bool false
  250. */
  251. public $useTable = false;
  252. /**
  253. * primaryKey property
  254. *
  255. * @var string 'id'
  256. */
  257. public $primaryKey = 'id';
  258. /**
  259. * name property
  260. *
  261. * @var string 'UserForm'
  262. */
  263. public $name = 'UserForm';
  264. /**
  265. * hasMany property
  266. *
  267. * @var array
  268. */
  269. public $hasMany = array(
  270. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  271. ));
  272. /**
  273. * schema definition
  274. *
  275. * @var array
  276. */
  277. protected $_schema = array(
  278. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  279. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  280. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  281. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  282. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  283. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  284. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  285. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  286. );
  287. }
  288. /**
  289. * OpenidUrl class
  290. *
  291. * @package cake
  292. * @package Cake.Test.Case.View.Helper
  293. */
  294. class OpenidUrl extends CakeTestModel {
  295. /**
  296. * useTable property
  297. *
  298. * @var bool false
  299. */
  300. public $useTable = false;
  301. /**
  302. * primaryKey property
  303. *
  304. * @var string 'id'
  305. */
  306. public $primaryKey = 'id';
  307. /**
  308. * name property
  309. *
  310. * @var string 'OpenidUrl'
  311. */
  312. public $name = 'OpenidUrl';
  313. /**
  314. * belongsTo property
  315. *
  316. * @var array
  317. */
  318. public $belongsTo = array('UserForm' => array(
  319. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  320. ));
  321. /**
  322. * validate property
  323. *
  324. * @var array
  325. */
  326. public $validate = array('openid_not_registered' => array());
  327. /**
  328. * schema method
  329. *
  330. * @var array
  331. */
  332. protected $_schema = array(
  333. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  334. 'user_form_id' => array(
  335. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  336. ),
  337. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  338. );
  339. /**
  340. * beforeValidate method
  341. *
  342. * @return void
  343. */
  344. public function beforeValidate($options = array()) {
  345. $this->invalidate('openid_not_registered');
  346. return true;
  347. }
  348. }
  349. /**
  350. * ValidateUser class
  351. *
  352. * @package cake
  353. * @package Cake.Test.Case.View.Helper
  354. */
  355. class ValidateUser extends CakeTestModel {
  356. /**
  357. * primaryKey property
  358. *
  359. * @var string 'id'
  360. */
  361. public $primaryKey = 'id';
  362. /**
  363. * useTable property
  364. *
  365. * @var bool false
  366. */
  367. public $useTable = false;
  368. /**
  369. * name property
  370. *
  371. * @var string 'ValidateUser'
  372. */
  373. public $name = 'ValidateUser';
  374. /**
  375. * hasOne property
  376. *
  377. * @var array
  378. */
  379. public $hasOne = array('ValidateProfile' => array(
  380. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  381. ));
  382. /**
  383. * schema method
  384. *
  385. * @var array
  386. */
  387. protected $_schema = array(
  388. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  389. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  390. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  391. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  392. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  393. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  394. );
  395. /**
  396. * beforeValidate method
  397. *
  398. * @return void
  399. */
  400. public function beforeValidate($options = array()) {
  401. $this->invalidate('email');
  402. return false;
  403. }
  404. }
  405. /**
  406. * ValidateProfile class
  407. *
  408. * @package cake
  409. * @package Cake.Test.Case.View.Helper
  410. */
  411. class ValidateProfile extends CakeTestModel {
  412. /**
  413. * primaryKey property
  414. *
  415. * @var string 'id'
  416. */
  417. public $primaryKey = 'id';
  418. /**
  419. * useTable property
  420. *
  421. * @var bool false
  422. */
  423. public $useTable = false;
  424. /**
  425. * schema property
  426. *
  427. * @var array
  428. */
  429. protected $_schema = array(
  430. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  431. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  432. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  433. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  434. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  435. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  436. );
  437. /**
  438. * name property
  439. *
  440. * @var string 'ValidateProfile'
  441. */
  442. public $name = 'ValidateProfile';
  443. /**
  444. * hasOne property
  445. *
  446. * @var array
  447. */
  448. public $hasOne = array('ValidateItem' => array(
  449. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  450. ));
  451. /**
  452. * belongsTo property
  453. *
  454. * @var array
  455. */
  456. public $belongsTo = array('ValidateUser' => array(
  457. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  458. ));
  459. /**
  460. * beforeValidate method
  461. *
  462. * @return void
  463. */
  464. public function beforeValidate($options = array()) {
  465. $this->invalidate('full_name');
  466. $this->invalidate('city');
  467. return false;
  468. }
  469. }
  470. /**
  471. * ValidateItem class
  472. *
  473. * @package cake
  474. * @package Cake.Test.Case.View.Helper
  475. */
  476. class ValidateItem extends CakeTestModel {
  477. /**
  478. * primaryKey property
  479. *
  480. * @var string 'id'
  481. */
  482. public $primaryKey = 'id';
  483. /**
  484. * useTable property
  485. *
  486. * @var bool false
  487. */
  488. public $useTable = false;
  489. /**
  490. * name property
  491. *
  492. * @var string 'ValidateItem'
  493. */
  494. public $name = 'ValidateItem';
  495. /**
  496. * schema property
  497. *
  498. * @var array
  499. */
  500. protected $_schema = array(
  501. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  502. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  503. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  504. 'description' => array(
  505. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  506. ),
  507. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  508. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  509. );
  510. /**
  511. * belongsTo property
  512. *
  513. * @var array
  514. */
  515. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  516. /**
  517. * beforeValidate method
  518. *
  519. * @return void
  520. */
  521. public function beforeValidate($options = array()) {
  522. $this->invalidate('description');
  523. return false;
  524. }
  525. }
  526. /**
  527. * TestMail class
  528. *
  529. * @package cake
  530. * @package Cake.Test.Case.View.Helper
  531. */
  532. class TestMail extends CakeTestModel {
  533. /**
  534. * primaryKey property
  535. *
  536. * @var string 'id'
  537. */
  538. public $primaryKey = 'id';
  539. /**
  540. * useTable property
  541. *
  542. * @var bool false
  543. */
  544. public $useTable = false;
  545. /**
  546. * name property
  547. *
  548. * @var string 'TestMail'
  549. */
  550. public $name = 'TestMail';
  551. }
  552. /**
  553. * FormHelperTest class
  554. *
  555. * @package cake
  556. * @subpackage Cake.Test.Case.View.Helper
  557. * @property FormHelper $Form
  558. */
  559. class FormHelperTest extends CakeTestCase {
  560. /**
  561. * Fixtures to be used
  562. *
  563. * @var array
  564. */
  565. public $fixtures = array('core.post');
  566. /**
  567. * Do not load the fixtures by default
  568. *
  569. * @var boolean
  570. */
  571. public $autoFixtures = false;
  572. /**
  573. * setUp method
  574. *
  575. * @return void
  576. */
  577. public function setUp() {
  578. parent::setUp();
  579. Configure::write('App.base', '');
  580. $this->Controller = new ContactTestController();
  581. $this->View = new View($this->Controller);
  582. $this->Form = new FormHelper($this->View);
  583. $this->Form->Html = new HtmlHelper($this->View);
  584. $this->Form->request = new CakeRequest('contacts/add', false);
  585. $this->Form->request->here = '/contacts/add';
  586. $this->Form->request['action'] = 'add';
  587. $this->Form->request->webroot = '';
  588. $this->Form->request->base = '';
  589. ClassRegistry::addObject('Contact', new Contact());
  590. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  591. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  592. ClassRegistry::addObject('User', new UserForm());
  593. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  594. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  595. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  596. $this->oldSalt = Configure::read('Security.salt');
  597. $this->dateRegex = array(
  598. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  599. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  600. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  601. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  602. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  603. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  604. );
  605. Configure::write('Security.salt', 'foo!');
  606. }
  607. /**
  608. * tearDown method
  609. *
  610. * @return void
  611. */
  612. public function tearDown() {
  613. parent::tearDown();
  614. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  615. Configure::write('Security.salt', $this->oldSalt);
  616. }
  617. /**
  618. * testFormCreateWithSecurity method
  619. *
  620. * Test form->create() with security key.
  621. *
  622. * @return void
  623. */
  624. public function testCreateWithSecurity() {
  625. $this->Form->request['_Token'] = array('key' => 'testKey');
  626. $encoding = strtolower(Configure::read('App.encoding'));
  627. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  628. $expected = array(
  629. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  630. 'div' => array('style' => 'display:none;'),
  631. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  632. array('input' => array(
  633. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  634. )),
  635. '/div'
  636. );
  637. $this->assertTags($result, $expected);
  638. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  639. $expected['form']['id'] = 'MyForm';
  640. $this->assertTags($result, $expected);
  641. }
  642. /**
  643. * test that create() clears the fields property so it starts fresh
  644. *
  645. * @return void
  646. */
  647. public function testCreateClearingFields() {
  648. $this->Form->fields = array('model_id');
  649. $this->Form->create('Contact');
  650. $this->assertEquals(array(), $this->Form->fields);
  651. }
  652. /**
  653. * Tests form hash generation with model-less data
  654. *
  655. * @return void
  656. */
  657. public function testValidateHashNoModel() {
  658. $this->Form->request['_Token'] = array('key' => 'foo');
  659. $result = $this->Form->secure(array('anything'));
  660. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  661. }
  662. /**
  663. * Tests that models with identical field names get resolved properly
  664. *
  665. * @return void
  666. */
  667. public function testDuplicateFieldNameResolution() {
  668. $result = $this->Form->create('ValidateUser');
  669. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  670. $result = $this->Form->input('ValidateItem.name');
  671. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  672. $result = $this->Form->input('ValidateUser.name');
  673. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  674. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  675. $this->assertRegExp('/type="text"/', $result);
  676. $result = $this->Form->input('ValidateItem.name');
  677. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  678. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  679. $this->assertRegExp('/<textarea/', $result);
  680. $result = $this->Form->input('name');
  681. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  682. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  683. $this->assertRegExp('/type="text"/', $result);
  684. }
  685. /**
  686. * Tests that hidden fields generated for checkboxes don't get locked
  687. *
  688. * @return void
  689. */
  690. public function testNoCheckboxLocking() {
  691. $this->Form->request['_Token'] = array('key' => 'foo');
  692. $this->assertSame(array(), $this->Form->fields);
  693. $this->Form->checkbox('check', array('value' => '1'));
  694. $this->assertSame($this->Form->fields, array('check'));
  695. }
  696. /**
  697. * testFormSecurityFields method
  698. *
  699. * Test generation of secure form hash generation.
  700. *
  701. * @return void
  702. */
  703. public function testFormSecurityFields() {
  704. $key = 'testKey';
  705. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  706. $this->Form->request['_Token'] = array('key' => $key);
  707. $result = $this->Form->secure($fields);
  708. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  709. $hash .= ':' . 'Model.valid';
  710. $hash = urlencode($hash);
  711. $expected = array(
  712. 'div' => array('style' => 'display:none;'),
  713. array('input' => array(
  714. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  715. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  716. )),
  717. array('input' => array(
  718. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  719. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  720. )),
  721. '/div'
  722. );
  723. $this->assertTags($result, $expected);
  724. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  725. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  726. $result = $this->Form->secure($fields);
  727. $expected = array(
  728. 'div' => array('class' => 'hidden'),
  729. array('input' => array(
  730. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  731. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  732. )),
  733. array('input' => array(
  734. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  735. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  736. )),
  737. '/div'
  738. );
  739. $this->assertTags($result, $expected);
  740. }
  741. /**
  742. * Tests correct generation of number fields for double and float fields
  743. *
  744. * @return void
  745. */
  746. public function testTextFieldGenerationForFloats() {
  747. $model = ClassRegistry::getObject('Contact');
  748. $model->setSchema(array('foo' => array(
  749. 'type' => 'float',
  750. 'null' => false,
  751. 'default' => null,
  752. 'length' => null
  753. )));
  754. $this->Form->create('Contact');
  755. $result = $this->Form->input('foo');
  756. $expected = array(
  757. 'div' => array('class' => 'input number'),
  758. 'label' => array('for' => 'ContactFoo'),
  759. 'Foo',
  760. '/label',
  761. array('input' => array(
  762. 'type' => 'number',
  763. 'name' => 'data[Contact][foo]',
  764. 'id' => 'ContactFoo',
  765. 'step' => 'any'
  766. )),
  767. '/div'
  768. );
  769. $this->assertTags($result, $expected);
  770. $result = $this->Form->input('foo', array('step' => 0.5));
  771. $expected = array(
  772. 'div' => array('class' => 'input number'),
  773. 'label' => array('for' => 'ContactFoo'),
  774. 'Foo',
  775. '/label',
  776. array('input' => array(
  777. 'type' => 'number',
  778. 'name' => 'data[Contact][foo]',
  779. 'id' => 'ContactFoo',
  780. 'step' => '0.5'
  781. )),
  782. '/div'
  783. );
  784. $this->assertTags($result, $expected);
  785. }
  786. /**
  787. * Tests correct generation of number fields for integer fields
  788. *
  789. * @access public
  790. * @return void
  791. */
  792. public function testTextFieldTypeNumberGenerationForIntegers() {
  793. $model = ClassRegistry::getObject('Contact');
  794. $model->setSchema(array('foo' => array(
  795. 'type' => 'integer',
  796. 'null' => false,
  797. 'default' => null,
  798. 'length' => null
  799. )));
  800. $this->Form->create('Contact');
  801. $result = $this->Form->input('foo');
  802. $expected = array(
  803. 'div' => array('class' => 'input number'),
  804. 'label' => array('for' => 'ContactFoo'),
  805. 'Foo',
  806. '/label',
  807. array('input' => array(
  808. 'type' => 'number', 'name' => 'data[Contact][foo]',
  809. 'id' => 'ContactFoo'
  810. )),
  811. '/div'
  812. );
  813. $this->assertTags($result, $expected);
  814. }
  815. /**
  816. * testFormSecurityMultipleFields method
  817. *
  818. * Test secure() with multiple row form. Ensure hash is correct.
  819. *
  820. * @return void
  821. */
  822. public function testFormSecurityMultipleFields() {
  823. $key = 'testKey';
  824. $fields = array(
  825. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  826. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  827. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  828. );
  829. $this->Form->request['_Token'] = array('key' => $key);
  830. $result = $this->Form->secure($fields);
  831. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  832. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  833. $expected = array(
  834. 'div' => array('style' => 'display:none;'),
  835. array('input' => array(
  836. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  837. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  838. )),
  839. array('input' => array(
  840. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  841. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  842. )),
  843. '/div'
  844. );
  845. $this->assertTags($result, $expected);
  846. }
  847. /**
  848. * testFormSecurityMultipleSubmitButtons
  849. *
  850. * test form submit generation and ensure that _Token is only created on end()
  851. *
  852. * @return void
  853. */
  854. public function testFormSecurityMultipleSubmitButtons() {
  855. $key = 'testKey';
  856. $this->Form->request['_Token'] = array('key' => $key);
  857. $this->Form->create('Addresses');
  858. $this->Form->input('Address.title');
  859. $this->Form->input('Address.first_name');
  860. $result = $this->Form->submit('Save', array('name' => 'save'));
  861. $expected = array(
  862. 'div' => array('class' => 'submit'),
  863. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  864. '/div',
  865. );
  866. $this->assertTags($result, $expected);
  867. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  868. $expected = array(
  869. 'div' => array('class' => 'submit'),
  870. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  871. '/div',
  872. );
  873. $this->assertTags($result, $expected);
  874. $result = $this->Form->end(null);
  875. $expected = array(
  876. 'div' => array('style' => 'display:none;'),
  877. array('input' => array(
  878. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  879. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  880. )),
  881. array('input' => array(
  882. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  883. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  884. )),
  885. '/div'
  886. );
  887. $this->assertTags($result, $expected);
  888. }
  889. /**
  890. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  891. *
  892. * @return void
  893. */
  894. public function testSecurityButtonNestedNamed() {
  895. $key = 'testKey';
  896. $this->Form->request['_Token'] = array('key' => $key);
  897. $this->Form->create('Addresses');
  898. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  899. $result = $this->Form->unlockField();
  900. $this->assertEquals(array('Address.button'), $result);
  901. }
  902. /**
  903. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  904. *
  905. * @return void
  906. */
  907. public function testSecuritySubmitNestedNamed() {
  908. $key = 'testKey';
  909. $this->Form->request['_Token'] = array('key' => $key);
  910. $this->Form->create('Addresses');
  911. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  912. $result = $this->Form->unlockField();
  913. $this->assertEquals(array('Address.button'), $result);
  914. }
  915. /**
  916. * Test that the correct fields are unlocked for image submits with no names.
  917. *
  918. * @return void
  919. */
  920. public function testSecuritySubmitImageNoName() {
  921. $key = 'testKey';
  922. $this->Form->request['_Token'] = array('key' => $key);
  923. $this->Form->create('User');
  924. $result = $this->Form->submit('save.png');
  925. $expected = array(
  926. 'div' => array('class' => 'submit'),
  927. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  928. '/div'
  929. );
  930. $this->assertTags($result, $expected);
  931. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  932. }
  933. /**
  934. * Test that the correct fields are unlocked for image submits with names.
  935. *
  936. * @return void
  937. */
  938. public function testSecuritySubmitImageName() {
  939. $key = 'testKey';
  940. $this->Form->request['_Token'] = array('key' => $key);
  941. $this->Form->create('User');
  942. $result = $this->Form->submit('save.png', array('name' => 'test'));
  943. $expected = array(
  944. 'div' => array('class' => 'submit'),
  945. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  946. '/div'
  947. );
  948. $this->assertTags($result, $expected);
  949. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  950. }
  951. /**
  952. * testFormSecurityMultipleInputFields method
  953. *
  954. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  955. *
  956. * @return void
  957. */
  958. public function testFormSecurityMultipleInputFields() {
  959. $key = 'testKey';
  960. $this->Form->request['_Token'] = array('key' => $key);
  961. $this->Form->create('Addresses');
  962. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  963. $this->Form->input('Addresses.0.title');
  964. $this->Form->input('Addresses.0.first_name');
  965. $this->Form->input('Addresses.0.last_name');
  966. $this->Form->input('Addresses.0.address');
  967. $this->Form->input('Addresses.0.city');
  968. $this->Form->input('Addresses.0.phone');
  969. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  970. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  971. $this->Form->input('Addresses.1.title');
  972. $this->Form->input('Addresses.1.first_name');
  973. $this->Form->input('Addresses.1.last_name');
  974. $this->Form->input('Addresses.1.address');
  975. $this->Form->input('Addresses.1.city');
  976. $this->Form->input('Addresses.1.phone');
  977. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  978. $result = $this->Form->secure($this->Form->fields);
  979. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  980. $expected = array(
  981. 'div' => array('style' => 'display:none;'),
  982. array('input' => array(
  983. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  984. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  985. )),
  986. array('input' => array(
  987. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  988. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  989. )),
  990. '/div'
  991. );
  992. $this->assertTags($result, $expected);
  993. }
  994. /**
  995. * Test form security with Model.field.0 style inputs
  996. *
  997. * @return void
  998. */
  999. public function testFormSecurityArrayFields() {
  1000. $key = 'testKey';
  1001. $this->Form->request->params['_Token']['key'] = $key;
  1002. $this->Form->create('Address');
  1003. $this->Form->input('Address.primary.1');
  1004. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1005. $this->Form->input('Address.secondary.1.0');
  1006. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1007. }
  1008. /**
  1009. * testFormSecurityMultipleInputDisabledFields method
  1010. *
  1011. * test secure form generation with multiple records and disabled fields.
  1012. *
  1013. * @return void
  1014. */
  1015. public function testFormSecurityMultipleInputDisabledFields() {
  1016. $key = 'testKey';
  1017. $this->Form->request->params['_Token'] = array(
  1018. 'key' => $key,
  1019. 'unlockedFields' => array('first_name', 'address')
  1020. );
  1021. $this->Form->create();
  1022. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1023. $this->Form->input('Addresses.0.title');
  1024. $this->Form->input('Addresses.0.first_name');
  1025. $this->Form->input('Addresses.0.last_name');
  1026. $this->Form->input('Addresses.0.address');
  1027. $this->Form->input('Addresses.0.city');
  1028. $this->Form->input('Addresses.0.phone');
  1029. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1030. $this->Form->input('Addresses.1.title');
  1031. $this->Form->input('Addresses.1.first_name');
  1032. $this->Form->input('Addresses.1.last_name');
  1033. $this->Form->input('Addresses.1.address');
  1034. $this->Form->input('Addresses.1.city');
  1035. $this->Form->input('Addresses.1.phone');
  1036. $result = $this->Form->secure($this->Form->fields);
  1037. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  1038. $expected = array(
  1039. 'div' => array('style' => 'display:none;'),
  1040. array('input' => array(
  1041. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1042. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1043. )),
  1044. array('input' => array(
  1045. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1046. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1047. )),
  1048. '/div'
  1049. );
  1050. $this->assertTags($result, $expected);
  1051. }
  1052. /**
  1053. * testFormSecurityInputDisabledFields method
  1054. *
  1055. * Test single record form with disabled fields.
  1056. *
  1057. * @return void
  1058. */
  1059. public function testFormSecurityInputUnlockedFields() {
  1060. $key = 'testKey';
  1061. $this->Form->request['_Token'] = array(
  1062. 'key' => $key,
  1063. 'unlockedFields' => array('first_name', 'address')
  1064. );
  1065. $this->Form->create();
  1066. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1067. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1068. $this->Form->input('Addresses.title');
  1069. $this->Form->input('Addresses.first_name');
  1070. $this->Form->input('Addresses.last_name');
  1071. $this->Form->input('Addresses.address');
  1072. $this->Form->input('Addresses.city');
  1073. $this->Form->input('Addresses.phone');
  1074. $result = $this->Form->fields;
  1075. $expected = array(
  1076. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1077. 'Addresses.city', 'Addresses.phone'
  1078. );
  1079. $this->assertEquals($expected, $result);
  1080. $result = $this->Form->secure($expected);
  1081. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1082. $expected = array(
  1083. 'div' => array('style' => 'display:none;'),
  1084. array('input' => array(
  1085. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1086. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1087. )),
  1088. array('input' => array(
  1089. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1090. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1091. )),
  1092. '/div'
  1093. );
  1094. $this->assertTags($result, $expected);
  1095. }
  1096. /**
  1097. * test securing inputs with custom name attributes.
  1098. *
  1099. * @return void
  1100. */
  1101. public function testFormSecureWithCustomNameAttribute() {
  1102. $this->Form->request->params['_Token']['key'] = 'testKey';
  1103. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1104. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1105. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1106. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1107. }
  1108. /**
  1109. * testFormSecuredInput method
  1110. *
  1111. * Test generation of entire secure form, assertions made on input() output.
  1112. *
  1113. * @return void
  1114. */
  1115. public function testFormSecuredInput() {
  1116. $this->Form->request['_Token'] = array('key' => 'testKey');
  1117. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1118. $encoding = strtolower(Configure::read('App.encoding'));
  1119. $expected = array(
  1120. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1121. 'div' => array('style' => 'display:none;'),
  1122. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1123. array('input' => array(
  1124. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1125. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1126. )),
  1127. '/div'
  1128. );
  1129. $this->assertTags($result, $expected);
  1130. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1131. $expected = array(
  1132. 'div' => array('class' => 'input text'),
  1133. 'label' => array('for' => 'UserFormPublished'),
  1134. 'Published',
  1135. '/label',
  1136. array('input' => array(
  1137. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1138. 'id' => 'UserFormPublished'
  1139. )),
  1140. '/div'
  1141. );
  1142. $this->assertTags($result, $expected);
  1143. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1144. $expected = array(
  1145. 'div' => array('class' => 'input text'),
  1146. 'label' => array('for' => 'UserFormOther'),
  1147. 'Other',
  1148. '/label',
  1149. array('input' => array(
  1150. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1151. 'id' => 'UserFormOther'
  1152. )),
  1153. '/div'
  1154. );
  1155. $this->assertTags($result, $expected);
  1156. $result = $this->Form->hidden('UserForm.stuff');
  1157. $expected = array('input' => array(
  1158. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1159. 'id' => 'UserFormStuff'
  1160. ));
  1161. $this->assertTags($result, $expected);
  1162. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1163. $expected = array('input' => array(
  1164. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1165. 'value' => '0', 'id' => 'UserFormHidden'
  1166. ));
  1167. $this->assertTags($result, $expected);
  1168. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1169. $expected = array(
  1170. 'div' => array('class' => 'input checkbox'),
  1171. array('input' => array(
  1172. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1173. 'value' => '0', 'id' => 'UserFormSomething_'
  1174. )),
  1175. array('input' => array(
  1176. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1177. 'value' => '1', 'id' => 'UserFormSomething'
  1178. )),
  1179. 'label' => array('for' => 'UserFormSomething'),
  1180. 'Something',
  1181. '/label',
  1182. '/div'
  1183. );
  1184. $this->assertTags($result, $expected);
  1185. $result = $this->Form->fields;
  1186. $expected = array(
  1187. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1188. 'UserForm.hidden' => '0', 'UserForm.something'
  1189. );
  1190. $this->assertEquals($expected, $result);
  1191. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1192. $result = $this->Form->secure($this->Form->fields);
  1193. $expected = array(
  1194. 'div' => array('style' => 'display:none;'),
  1195. array('input' => array(
  1196. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1197. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1198. )),
  1199. array('input' => array(
  1200. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1201. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1202. )),
  1203. '/div'
  1204. );
  1205. $this->assertTags($result, $expected);
  1206. }
  1207. /**
  1208. * Tests that the correct keys are added to the field hash index
  1209. *
  1210. * @return void
  1211. */
  1212. public function testFormSecuredFileInput() {
  1213. $this->Form->request['_Token'] = array('key' => 'testKey');
  1214. $this->assertEquals(array(), $this->Form->fields);
  1215. $result = $this->Form->file('Attachment.file');
  1216. $expected = array(
  1217. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1218. 'Attachment.file.error', 'Attachment.file.size'
  1219. );
  1220. $this->assertEquals($expected, $this->Form->fields);
  1221. }
  1222. /**
  1223. * test that multiple selects keys are added to field hash
  1224. *
  1225. * @return void
  1226. */
  1227. public function testFormSecuredMultipleSelect() {
  1228. $this->Form->request['_Token'] = array('key' => 'testKey');
  1229. $this->assertEquals(array(), $this->Form->fields);
  1230. $options = array('1' => 'one', '2' => 'two');
  1231. $this->Form->select('Model.select', $options);
  1232. $expected = array('Model.select');
  1233. $this->assertEquals($expected, $this->Form->fields);
  1234. $this->Form->fields = array();
  1235. $this->Form->select('Model.select', $options, array('multiple' => true));
  1236. $this->assertEquals($expected, $this->Form->fields);
  1237. }
  1238. /**
  1239. * testFormSecuredRadio method
  1240. *
  1241. * @return void
  1242. */
  1243. public function testFormSecuredRadio() {
  1244. $this->Form->request['_Token'] = array('key' => 'testKey');
  1245. $this->assertEquals(array(), $this->Form->fields);
  1246. $options = array('1' => 'option1', '2' => 'option2');
  1247. $this->Form->radio('Test.test', $options);
  1248. $expected = array('Test.test');
  1249. $this->assertEquals($expected, $this->Form->fields);
  1250. }
  1251. /**
  1252. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1253. * hashing.
  1254. *
  1255. * @return void
  1256. */
  1257. public function testFormSecuredAndDisabled() {
  1258. $this->Form->request['_Token'] = array('key' => 'testKey');
  1259. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1260. $this->Form->text('Model.text', array('disabled' => true));
  1261. $this->Form->password('Model.text', array('disabled' => true));
  1262. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1263. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1264. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1265. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1266. $this->Form->month('Model.month', array('disabled' => true));
  1267. $this->Form->day('Model.day', array('disabled' => true));
  1268. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1269. $this->Form->minute('Model.minute', array('disabled' => true));
  1270. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1271. $expected = array(
  1272. 'Model.radio' => ''
  1273. );
  1274. $this->assertEquals($expected, $this->Form->fields);
  1275. }
  1276. /**
  1277. * testDisableSecurityUsingForm method
  1278. *
  1279. * @return void
  1280. */
  1281. public function testDisableSecurityUsingForm() {
  1282. $this->Form->request['_Token'] = array(
  1283. 'key' => 'testKey',
  1284. 'disabledFields' => array()
  1285. );
  1286. $this->Form->create();
  1287. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1288. $this->Form->input('Addresses.title');
  1289. $this->Form->input('Addresses.first_name', array('secure' => false));
  1290. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1291. $this->Form->input('Addresses.zip', array(
  1292. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1293. ));
  1294. $result = $this->Form->fields;
  1295. $expected = array(
  1296. 'Addresses.id' => '123456', 'Addresses.title',
  1297. );
  1298. $this->assertEquals($expected, $result);
  1299. }
  1300. /**
  1301. * test disableField
  1302. *
  1303. * @return void
  1304. */
  1305. public function testUnlockFieldAddsToList() {
  1306. $this->Form->request['_Token'] = array(
  1307. 'key' => 'testKey',
  1308. 'unlockedFields' => array()
  1309. );
  1310. $this->Form->create('Contact');
  1311. $this->Form->unlockField('Contact.name');
  1312. $this->Form->text('Contact.name');
  1313. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1314. $this->assertEquals(array(), $this->Form->fields);
  1315. }
  1316. /**
  1317. * test unlockField removing from fields array.
  1318. *
  1319. * @return void
  1320. */
  1321. public function testUnlockFieldRemovingFromFields() {
  1322. $this->Form->request['_Token'] = array(
  1323. 'key' => 'testKey',
  1324. 'unlockedFields' => array()
  1325. );
  1326. $this->Form->create('Contact');
  1327. $this->Form->hidden('Contact.id', array('value' => 1));
  1328. $this->Form->text('Contact.name');
  1329. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1330. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1331. $this->Form->unlockField('Contact.name');
  1332. $this->Form->unlockField('Contact.id');
  1333. $this->assertEquals(array(), $this->Form->fields);
  1334. }
  1335. /**
  1336. * testTagIsInvalid method
  1337. *
  1338. * @return void
  1339. */
  1340. public function testTagIsInvalid() {
  1341. $Contact = ClassRegistry::getObject('Contact');
  1342. $Contact->validationErrors[0]['email'] = array('Please provide an email');
  1343. $this->Form->setEntity('Contact.0.email');
  1344. $result = $this->Form->tagIsInvalid();
  1345. $expected = array('Please provide an email');
  1346. $this->assertEquals($expected, $result);
  1347. $this->Form->setEntity('Contact.1.email');
  1348. $result = $this->Form->tagIsInvalid();
  1349. $expected = false;
  1350. $this->assertSame($expected, $result);
  1351. $this->Form->setEntity('Contact.0.name');
  1352. $result = $this->Form->tagIsInvalid();
  1353. $expected = false;
  1354. $this->assertSame($expected, $result);
  1355. }
  1356. /**
  1357. * Test validation errors.
  1358. *
  1359. * @return void
  1360. */
  1361. public function testPasswordValidation() {
  1362. $Contact = ClassRegistry::getObject('Contact');
  1363. $Contact->validationErrors['password'] = array('Please provide a password');
  1364. $result = $this->Form->input('Contact.password');
  1365. $expected = array(
  1366. 'div' => array('class' => 'input password error'),
  1367. 'label' => array('for' => 'ContactPassword'),
  1368. 'Password',
  1369. '/label',
  1370. 'input' => array(
  1371. 'type' => 'password', 'name' => 'data[Contact][password]',
  1372. 'id' => 'ContactPassword', 'class' => 'form-error'
  1373. ),
  1374. array('div' => array('class' => 'error-message')),
  1375. 'Please provide a password',
  1376. '/div',
  1377. '/div'
  1378. );
  1379. $this->assertTags($result, $expected);
  1380. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1381. $expected = array(
  1382. 'div' => array('class' => 'input password error'),
  1383. 'label' => array('for' => 'ContactPassword'),
  1384. 'Password',
  1385. '/label',
  1386. 'input' => array(
  1387. 'type' => 'password', 'name' => 'data[Contact][password]',
  1388. 'id' => 'ContactPassword', 'class' => 'form-error'
  1389. ),
  1390. '/div'
  1391. );
  1392. $this->assertTags($result, $expected);
  1393. }
  1394. /**
  1395. * Test validation errors, when validation message is an empty string.
  1396. *
  1397. * @access public
  1398. * @return void
  1399. */
  1400. public function testEmptyErrorValidation() {
  1401. $this->Form->validationErrors['Contact']['password'] = '';
  1402. $result = $this->Form->input('Contact.password');
  1403. $expected = array(
  1404. 'div' => array('class' => 'input password error'),
  1405. 'label' => array('for' => 'ContactPassword'),
  1406. 'Password',
  1407. '/label',
  1408. 'input' => array(
  1409. 'type' => 'password', 'name' => 'data[Contact][password]',
  1410. 'id' => 'ContactPassword', 'class' => 'form-error'
  1411. ),
  1412. array('div' => array('class' => 'error-message')),
  1413. array(),
  1414. '/div',
  1415. '/div'
  1416. );
  1417. $this->assertTags($result, $expected);
  1418. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1419. $expected = array(
  1420. 'div' => array('class' => 'input password error'),
  1421. 'label' => array('for' => 'ContactPassword'),
  1422. 'Password',
  1423. '/label',
  1424. 'input' => array(
  1425. 'type' => 'password', 'name' => 'data[Contact][password]',
  1426. 'id' => 'ContactPassword', 'class' => 'form-error'
  1427. ),
  1428. '/div'
  1429. );
  1430. $this->assertTags($result, $expected);
  1431. }
  1432. /**
  1433. * Test validation errors, when calling input() overriding validation message by an empty string.
  1434. *
  1435. * @access public
  1436. * @return void
  1437. */
  1438. public function testEmptyInputErrorValidation() {
  1439. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1440. $result = $this->Form->input('Contact.password', array('error' => ''));
  1441. $expected = array(
  1442. 'div' => array('class' => 'input password error'),
  1443. 'label' => array('for' => 'ContactPassword'),
  1444. 'Password',
  1445. '/label',
  1446. 'input' => array(
  1447. 'type' => 'password', 'name' => 'data[Contact][password]',
  1448. 'id' => 'ContactPassword', 'class' => 'form-error'
  1449. ),
  1450. array('div' => array('class' => 'error-message')),
  1451. array(),
  1452. '/div',
  1453. '/div'
  1454. );
  1455. $this->assertTags($result, $expected);
  1456. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1457. $expected = array(
  1458. 'div' => array('class' => 'input password error'),
  1459. 'label' => array('for' => 'ContactPassword'),
  1460. 'Password',
  1461. '/label',
  1462. 'input' => array(
  1463. 'type' => 'password', 'name' => 'data[Contact][password]',
  1464. 'id' => 'ContactPassword', 'class' => 'form-error'
  1465. ),
  1466. '/div'
  1467. );
  1468. $this->assertTags($result, $expected);
  1469. }
  1470. /**
  1471. * testFormValidationAssociated method
  1472. *
  1473. * test display of form errors in conjunction with model::validates.
  1474. *
  1475. * @return void
  1476. */
  1477. public function testFormValidationAssociated() {
  1478. $this->UserForm = ClassRegistry::getObject('UserForm');
  1479. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1480. $data = array(
  1481. 'UserForm' => array('name' => 'user'),
  1482. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1483. );
  1484. $result = $this->UserForm->OpenidUrl->create($data);
  1485. $this->assertFalse(empty($result));
  1486. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1487. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1488. $encoding = strtolower(Configure::read('App.encoding'));
  1489. $expected = array(
  1490. 'form' => array(
  1491. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1492. 'accept-charset' => $encoding
  1493. ),
  1494. 'div' => array('style' => 'display:none;'),
  1495. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1496. '/div'
  1497. );
  1498. $this->assertTags($result, $expected);
  1499. $result = $this->Form->error(
  1500. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1501. );
  1502. $this->assertEquals('Error, not registered', $result);
  1503. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1504. }
  1505. /**
  1506. * testFormValidationAssociatedFirstLevel method
  1507. *
  1508. * test form error display with associated model.
  1509. *
  1510. * @return void
  1511. */
  1512. public function testFormValidationAssociatedFirstLevel() {
  1513. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1514. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1515. $data = array(
  1516. 'ValidateUser' => array('name' => 'mariano'),
  1517. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1518. );
  1519. $result = $this->ValidateUser->create($data);
  1520. $this->assertFalse(empty($result));
  1521. $this->assertFalse($this->ValidateUser->validates());
  1522. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1523. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1524. $encoding = strtolower(Configure::read('App.encoding'));
  1525. $expected = array(
  1526. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1527. 'div' => array('style' => 'display:none;'),
  1528. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1529. '/div'
  1530. );
  1531. $this->assertTags($result, $expected);
  1532. $result = $this->Form->error(
  1533. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1534. );
  1535. $this->assertEquals('Invalid email', $result);
  1536. $result = $this->Form->error(
  1537. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1538. );
  1539. $this->assertEquals('Invalid name', $result);
  1540. $result = $this->Form->error(
  1541. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1542. );
  1543. $this->assertEquals('Invalid city', $result);
  1544. unset($this->ValidateUser->ValidateProfile);
  1545. unset($this->ValidateUser);
  1546. }
  1547. /**
  1548. * testFormValidationAssociatedSecondLevel method
  1549. *
  1550. * test form error display with associated model.
  1551. *
  1552. * @return void
  1553. */
  1554. public function testFormValidationAssociatedSecondLevel() {
  1555. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1556. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1557. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1558. $data = array(
  1559. 'ValidateUser' => array('name' => 'mariano'),
  1560. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1561. 'ValidateItem' => array('name' => 'Item')
  1562. );
  1563. $result = $this->ValidateUser->create($data);
  1564. $this->assertFalse(empty($result));
  1565. $this->assertFalse($this->ValidateUser->validates());
  1566. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1567. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1568. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1569. $encoding = strtolower(Configure::read('App.encoding'));
  1570. $expected = array(
  1571. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1572. 'div' => array('style' => 'display:none;'),
  1573. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1574. '/div'
  1575. );
  1576. $this->assertTags($result, $expected);
  1577. $result = $this->Form->error(
  1578. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1579. );
  1580. $this->assertEquals('Invalid email', $result);
  1581. $result = $this->Form->error(
  1582. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1583. );
  1584. $this->assertEquals('Invalid name', $result);
  1585. $result = $this->Form->error(
  1586. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1587. );
  1588. $result = $this->Form->error(
  1589. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1590. );
  1591. $this->assertEquals('Invalid description', $result);
  1592. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1593. unset($this->ValidateUser->ValidateProfile);
  1594. unset($this->ValidateUser);
  1595. }
  1596. /**
  1597. * testFormValidationMultiRecord method
  1598. *
  1599. * test form error display with multiple records.
  1600. *
  1601. * @return void
  1602. */
  1603. public function testFormValidationMultiRecord() {
  1604. $Contact = ClassRegistry::getObject('Contact');
  1605. $Contact->validationErrors[2] = array(
  1606. 'name' => array('This field cannot be left blank')
  1607. );
  1608. $result = $this->Form->input('Contact.2.name');
  1609. $expected = array(
  1610. 'div' => array('class' => 'input text error'),
  1611. 'label' => array('for' => 'Contact2Name'),
  1612. 'Name',
  1613. '/label',
  1614. 'input' => array(
  1615. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1616. 'class' => 'form-error', 'maxlength' => 255
  1617. ),
  1618. array('div' => array('class' => 'error-message')),
  1619. 'This field cannot be left blank',
  1620. '/div',
  1621. '/div'
  1622. );
  1623. $this->assertTags($result, $expected);
  1624. }
  1625. /**
  1626. * testMultipleInputValidation method
  1627. *
  1628. * test multiple record form validation error display.
  1629. *
  1630. * @return void
  1631. */
  1632. public function testMultipleInputValidation() {
  1633. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1634. $Address->validationErrors[0] = array(
  1635. 'title' => array('This field cannot be empty'),
  1636. 'first_name' => array('This field cannot be empty')
  1637. );
  1638. $Address->validationErrors[1] = array(
  1639. 'last_name' => array('You must have a last name')
  1640. );
  1641. $this->Form->create();
  1642. $result = $this->Form->input('Address.0.title');
  1643. $expected = array(
  1644. 'div' => array('class'),
  1645. 'label' => array('for'),
  1646. 'preg:/[^<]+/',
  1647. '/label',
  1648. 'input' => array(
  1649. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1650. ),
  1651. array('div' => array('class' => 'error-message')),
  1652. 'This field cannot be empty',
  1653. '/div',
  1654. '/div'
  1655. );
  1656. $this->assertTags($result, $expected);
  1657. $result = $this->Form->input('Address.0.first_name');
  1658. $expected = array(
  1659. 'div' => array('class'),
  1660. 'label' => array('for'),
  1661. 'preg:/[^<]+/',
  1662. '/label',
  1663. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1664. array('div' => array('class' => 'error-message')),
  1665. 'This field cannot be empty',
  1666. '/div',
  1667. '/div'
  1668. );
  1669. $this->assertTags($result, $expected);
  1670. $result = $this->Form->input('Address.0.last_name');
  1671. $expected = array(
  1672. 'div' => array('class'),
  1673. 'label' => array('for'),
  1674. 'preg:/[^<]+/',
  1675. '/label',
  1676. 'input' => array('type' => 'text', 'name', 'id'),
  1677. '/div'
  1678. );
  1679. $this->assertTags($result, $expected);
  1680. $result = $this->Form->input('Address.1.last_name');
  1681. $expected = array(
  1682. 'div' => array('class'),
  1683. 'label' => array('for'),
  1684. 'preg:/[^<]+/',
  1685. '/label',
  1686. 'input' => array(
  1687. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1688. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1689. ),
  1690. array('div' => array('class' => 'error-message')),
  1691. 'You must have a last name',
  1692. '/div',
  1693. '/div'
  1694. );
  1695. $this->assertTags($result, $expected);
  1696. }
  1697. /**
  1698. * testInput method
  1699. *
  1700. * Test various incarnations of input().
  1701. *
  1702. * @return void
  1703. */
  1704. public function testInput() {
  1705. $result = $this->Form->input('ValidateUser.balance');
  1706. $expected = array(
  1707. 'div' => array('class'),
  1708. 'label' => array('for'),
  1709. 'Balance',
  1710. '/label',
  1711. 'input' => array('name', 'type' => 'number', 'maxlength' => 8, 'id'),
  1712. '/div',
  1713. );
  1714. $this->assertTags($result, $expected);
  1715. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1716. $expected = array(
  1717. 'div' => array('class' => 'input text'),
  1718. 'label' => array('for' => 'custom'),
  1719. 'Email',
  1720. '/label',
  1721. array('input' => array(
  1722. 'type' => 'text', 'name' => 'data[Contact][email]',
  1723. 'id' => 'custom', 'maxlength' => 255
  1724. )),
  1725. '/div'
  1726. );
  1727. $this->assertTags($result, $expected);
  1728. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1729. $expected = array(
  1730. '<div',
  1731. 'label' => array('for' => 'ContactEmail'),
  1732. 'Email',
  1733. '/label',
  1734. array('input' => array(
  1735. 'type' => 'text', 'name' => 'data[Contact][email]',
  1736. 'id' => 'ContactEmail', 'maxlength' => 255
  1737. )),
  1738. '/div'
  1739. );
  1740. $this->assertTags($result, $expected);
  1741. $result = $this->Form->hidden('Contact.idontexist');
  1742. $expected = array('input' => array(
  1743. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1744. 'id' => 'ContactIdontexist'
  1745. ));
  1746. $this->assertTags($result, $expected);
  1747. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1748. $expected = array(
  1749. 'div' => array('class' => 'input text'),
  1750. 'label' => array('for' => 'ContactEmail'),
  1751. 'Email',
  1752. '/label',
  1753. array('input' => array(
  1754. 'type' => 'text', 'name' => 'data[Contact][email]',
  1755. 'id' => 'ContactEmail'
  1756. )),
  1757. '/div'
  1758. );
  1759. $this->assertTags($result, $expected);
  1760. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1761. $expected = array(
  1762. 'div' => array('class' => 'input text'),
  1763. 'label' => array('for' => 'Contact5Email'),
  1764. 'Email',
  1765. '/label',
  1766. array('input' => array(
  1767. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1768. 'id' => 'Contact5Email'
  1769. )),
  1770. '/div'
  1771. );
  1772. $this->assertTags($result, $expected);
  1773. $result = $this->Form->input('Contact.password');
  1774. $expected = array(
  1775. 'div' => array('class' => 'input password'),
  1776. 'label' => array('for' => 'ContactPassword'),
  1777. 'Password',
  1778. '/label',
  1779. array('input' => array(
  1780. 'type' => 'password', 'name' => 'data[Contact][password]',
  1781. 'id' => 'ContactPassword'
  1782. )),
  1783. '/div'
  1784. );
  1785. $this->assertTags($result, $expected);
  1786. $result = $this->Form->input('Contact.email', array(
  1787. 'type' => 'file', 'class' => 'textbox'
  1788. ));
  1789. $expected = array(
  1790. 'div' => array('class' => 'input file'),
  1791. 'label' => array('for' => 'ContactEmail'),
  1792. 'Email',
  1793. '/label',
  1794. array('input' => array(
  1795. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1796. 'id' => 'ContactEmail'
  1797. )),
  1798. '/div'
  1799. );
  1800. $this->assertTags($result, $expected);
  1801. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1802. $result = $this->Form->input('Contact.phone');
  1803. $expected = array(
  1804. 'div' => array('class' => 'input text'),
  1805. 'label' => array('for' => 'ContactPhone'),
  1806. 'Phone',
  1807. '/label',
  1808. array('input' => array(
  1809. 'type' => 'text', 'name' => 'data[Contact][phone]',
  1810. 'value' => 'Hello &amp; World &gt; weird chars',
  1811. 'id' => 'ContactPhone', 'maxlength' => 255
  1812. )),
  1813. '/div'
  1814. );
  1815. $this->assertTags($result, $expected);
  1816. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1817. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1818. $expected = array(
  1819. 'div' => array('class' => 'input text'),
  1820. 'label' => array('for' => 'myId'),
  1821. 'Field',
  1822. '/label',
  1823. 'input' => array(
  1824. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1825. 'value' => 'My value', 'id' => 'myId'
  1826. ),
  1827. '/div'
  1828. );
  1829. $this->assertTags($result, $expected);
  1830. unset($this->Form->request->data);
  1831. $Contact = ClassRegistry::getObject('Contact');
  1832. $Contact->validationErrors['field'] = array('Badness!');
  1833. $result = $this->Form->input('Contact.field');
  1834. $expected = array(
  1835. 'div' => array('class' => 'input text error'),
  1836. 'label' => array('for' => 'ContactField'),
  1837. 'Field',
  1838. '/label',
  1839. 'input' => array(
  1840. 'type' => 'text', 'name' => 'data[Contact][field]',
  1841. 'id' => 'ContactField', 'class' => 'form-error'
  1842. ),
  1843. array('div' => array('class' => 'error-message')),
  1844. 'Badness!',
  1845. '/div',
  1846. '/div'
  1847. );
  1848. $this->assertTags($result, $expected);
  1849. $result = $this->Form->input('Contact.field', array(
  1850. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1851. ));
  1852. $expected = array(
  1853. 'label' => array('for' => 'ContactField'),
  1854. 'Field',
  1855. '/label',
  1856. 'input' => array(
  1857. 'type' => 'text', 'name' => 'data[Contact][field]',
  1858. 'id' => 'ContactField', 'class' => 'form-error'
  1859. ),
  1860. array('span' => array('class' => 'error-message')),
  1861. 'Badness!',
  1862. '/span'
  1863. );
  1864. $this->assertTags($result, $expected);
  1865. $result = $this->Form->input('Contact.field', array(
  1866. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1867. ));
  1868. $expected = array(
  1869. 'div' => array('class' => 'input text error'),
  1870. 'label' => array('for' => 'ContactField'),
  1871. 'Field',
  1872. '/label',
  1873. 'input' => array(
  1874. 'type' => 'text', 'name' => 'data[Contact][field]',
  1875. 'id' => 'ContactField', 'class' => 'form-error'
  1876. ),
  1877. array('div' => array('class' => 'error')),
  1878. 'Badness!',
  1879. '/div'
  1880. );
  1881. $this->assertTags($result, $expected);
  1882. $result = $this->Form->input('Contact.field', array(
  1883. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1884. ));
  1885. $expected = array(
  1886. 'span' => array('class' => 'input text error'),
  1887. 'label' => array('for' => 'ContactField'),
  1888. 'Field',
  1889. '/label',
  1890. 'input' => array(
  1891. 'type' => 'text', 'name' => 'data[Contact][field]',
  1892. 'id' => 'ContactField', 'class' => 'form-error'
  1893. ),
  1894. 'Badness!',
  1895. '/span'
  1896. );
  1897. $this->assertTags($result, $expected);
  1898. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1899. $expected = array(
  1900. 'div' => array('class' => 'input text error'),
  1901. 'label' => array('for' => 'ContactField'),
  1902. 'Field',
  1903. '/label',
  1904. 'input' => array(
  1905. 'type' => 'text', 'name' => 'data[Contact][field]',
  1906. 'id' => 'ContactField', 'class' => 'form-error'
  1907. ),
  1908. 'A message to you, Rudy',
  1909. array('div' => array('class' => 'error-message')),
  1910. 'Badness!',
  1911. '/div',
  1912. '/div'
  1913. );
  1914. $this->assertTags($result, $expected);
  1915. $this->Form->setEntity(null);
  1916. $this->Form->setEntity('Contact.field');
  1917. $result = $this->Form->input('Contact.field', array(
  1918. 'after' => 'A message to you, Rudy', 'error' => false
  1919. ));
  1920. $expected = array(
  1921. 'div' => array('class' => 'input text'),
  1922. 'label' => array('for' => 'ContactField'),
  1923. 'Field',
  1924. '/label',
  1925. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1926. 'A message to you, Rudy',
  1927. '/div'
  1928. );
  1929. $this->assertTags($result, $expected);
  1930. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1931. $expected = array(
  1932. 'div' => array('class' => 'input text'),
  1933. 'label' => array('for' => 'ObjectField'),
  1934. 'Field',
  1935. '/label',
  1936. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1937. 'A message to you, Rudy',
  1938. '/div'
  1939. );
  1940. $this->assertTags($result, $expected);
  1941. $Contact->validationErrors['field'] = array('minLength');
  1942. $result = $this->Form->input('Contact.field', array(
  1943. 'error' => array(
  1944. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1945. 'maxLength' => 'login too large'
  1946. )
  1947. ));
  1948. $expected = array(
  1949. 'div' => array('class' => 'input text error'),
  1950. 'label' => array('for' => 'ContactField'),
  1951. 'Field',
  1952. '/label',
  1953. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1954. array('div' => array('class' => 'error-message')),
  1955. 'Le login doit contenir au moins 2 caractères',
  1956. '/div',
  1957. '/div'
  1958. );
  1959. $this->assertTags($result, $expected);
  1960. $Contact->validationErrors['field'] = array('maxLength');
  1961. $result = $this->Form->input('Contact.field', array(
  1962. 'error' => array(
  1963. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1964. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1965. 'maxLength' => 'login too large',
  1966. )
  1967. ));
  1968. $expected = array(
  1969. 'div' => array('class' => 'input text error'),
  1970. 'label' => array('for' => 'ContactField'),
  1971. 'Field',
  1972. '/label',
  1973. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1974. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1975. 'login too large',
  1976. '/span',
  1977. '/div'
  1978. );
  1979. $this->assertTags($result, $expected);
  1980. }
  1981. /**
  1982. * test input() with checkbox creation
  1983. *
  1984. * @return void
  1985. */
  1986. public function testInputCheckbox() {
  1987. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1988. $expected = array(
  1989. 'div' => array('class' => 'input checkbox'),
  1990. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1991. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1992. '/div'
  1993. );
  1994. $this->assertTags($result, $expected);
  1995. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1996. $expected = array(
  1997. 'div' => array('class' => 'input checkbox'),
  1998. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1999. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2000. '/div'
  2001. );
  2002. $this->assertTags($result, $expected);
  2003. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  2004. $expected = array(
  2005. 'div' => array('class' => 'input checkbox'),
  2006. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  2007. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2008. '/div'
  2009. );
  2010. $this->assertTags($result, $expected);
  2011. }
  2012. /**
  2013. * test form->input() with time types.
  2014. *
  2015. */
  2016. public function testInputTime() {
  2017. extract($this->dateRegex);
  2018. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2019. $result = explode(':', $result);
  2020. $this->assertRegExp('/option value="23"/', $result[0]);
  2021. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2022. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2023. $result = explode(':', $result);
  2024. $this->assertRegExp('/option value="23"/', $result[0]);
  2025. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2026. $result = $this->Form->input('Model.field', array(
  2027. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2028. ));
  2029. $result = explode(':', $result);
  2030. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2031. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2032. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2033. $result = $this->Form->input('Model.field', array(
  2034. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2035. ));
  2036. $result = explode(':', $result);
  2037. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2038. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2039. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2040. $result = $this->Form->input('prueba', array(
  2041. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  2042. 'maxYear' => date('Y') + 1 ,'interval' => 15
  2043. ));
  2044. $result = explode(':', $result);
  2045. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2046. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2047. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2048. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2049. $result = $this->Form->input('Random.start_time', array(
  2050. 'type' => 'time',
  2051. 'selected' => '18:15'
  2052. ));
  2053. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2054. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2055. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2056. }
  2057. /**
  2058. * Test interval + selected near the hour roll over.
  2059. *
  2060. * @return void
  2061. */
  2062. public function testTimeSelectedWithInterval() {
  2063. $result = $this->Form->input('Model.start_time', array(
  2064. 'type' => 'time',
  2065. 'interval' => 15,
  2066. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2067. ));
  2068. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2069. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2070. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2071. $result = $this->Form->input('Model.start_time', array(
  2072. 'type' => 'time',
  2073. 'interval' => 15,
  2074. 'selected' => '2012-10-23 15:57:00'
  2075. ));
  2076. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2077. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2078. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2079. $result = $this->Form->input('Model.start_time', array(
  2080. 'timeFormat' => 24,
  2081. 'type' => 'time',
  2082. 'interval' => 15,
  2083. 'selected' => '15:57'
  2084. ));
  2085. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2086. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2087. $result = $this->Form->input('Model.start_time', array(
  2088. 'timeFormat' => 24,
  2089. 'type' => 'time',
  2090. 'interval' => 15,
  2091. 'selected' => '23:57'
  2092. ));
  2093. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2094. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2095. $result = $this->Form->input('Model.created', array(
  2096. 'timeFormat' => 24,
  2097. 'type' => 'datetime',
  2098. 'interval' => 15,
  2099. 'selected' => '2012-09-30 23:56'
  2100. ));
  2101. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2102. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2103. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2104. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2105. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2106. }
  2107. /**
  2108. * test form->input() with datetime, date and time types
  2109. *
  2110. * @return void
  2111. */
  2112. public function testInputDatetime() {
  2113. extract($this->dateRegex);
  2114. $result = $this->Form->input('prueba', array(
  2115. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2116. 'maxYear' => date('Y') + 1, 'interval' => 15
  2117. ));
  2118. $result = explode(':', $result);
  2119. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2120. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2121. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2122. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2123. //related to ticket #5013
  2124. $result = $this->Form->input('Contact.date', array(
  2125. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2126. ));
  2127. $this->assertRegExp('/class="customClass"/', $result);
  2128. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2129. $result = $this->Form->input('Contact.date', array(
  2130. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2131. ));
  2132. $this->assertRegExp('/id="customIdDay"/', $result);
  2133. $this->assertRegExp('/id="customIdMonth"/', $result);
  2134. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2135. $result = $this->Form->input('Model.field', array(
  2136. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2137. ));
  2138. $this->assertRegExp('/id="customIDDay"/', $result);
  2139. $this->assertRegExp('/id="customIDHour"/', $result);
  2140. $result = explode('</select><select', $result);
  2141. $result = explode(':', $result[1]);
  2142. $this->assertRegExp('/option value="23"/', $result[0]);
  2143. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2144. $result = $this->Form->input('Model.field', array(
  2145. 'type' => 'datetime', 'timeFormat' => 12
  2146. ));
  2147. $result = explode('</select><select', $result);
  2148. $result = explode(':', $result[1]);
  2149. $this->assertRegExp('/option value="12"/', $result[0]);
  2150. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2151. $this->Form->request->data = array('Contact' => array('created' => null));
  2152. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2153. $expected = array(
  2154. 'div' => array('class' => 'input date'),
  2155. 'label' => array('for' => 'ContactCreatedMonth'),
  2156. 'Created',
  2157. '/label',
  2158. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2159. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2160. $monthsRegex,
  2161. '/select', '-',
  2162. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2163. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2164. $daysRegex,
  2165. '/select', '-',
  2166. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2167. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2168. $yearsRegex,
  2169. '/select',
  2170. '/div'
  2171. );
  2172. $this->assertTags($result, $expected);
  2173. $this->Form->request->data = array('Contact' => array('created' => null));
  2174. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2175. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2176. $this->Form->request->data = array('Contact' => array('created' => null));
  2177. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2178. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2179. $result = $this->Form->input('Contact.created', array(
  2180. 'type' => 'date',
  2181. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2182. 'timeFormat' => 'NONE'
  2183. ));
  2184. $this->assertRegExp('/for\="created-month"/', $result);
  2185. }
  2186. /**
  2187. * Test generating checkboxes in a loop.
  2188. *
  2189. * @return void
  2190. */
  2191. public function testInputCheckboxesInLoop() {
  2192. for ($i = 1; $i < 5; $i++) {
  2193. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2194. $expected = array(
  2195. 'div' => array('class' => 'input checkbox'),
  2196. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2197. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2198. 'label' => array('for' => "Contact{$i}Email"),
  2199. 'Email',
  2200. '/label',
  2201. '/div'
  2202. );
  2203. $this->assertTags($result, $expected);
  2204. }
  2205. }
  2206. /**
  2207. * Test generating checkboxes with disabled elements.
  2208. *
  2209. * @return void
  2210. */
  2211. public function testInputCheckboxWithDisabledElements() {
  2212. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2213. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2214. $expected = array(
  2215. array('div' => array('class' => 'input select')),
  2216. array('label' => array('for' => "ContactMultiple")),
  2217. 'Multiple',
  2218. '/label',
  2219. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2220. array('div' => array('class' => 'checkbox')),
  2221. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2222. array('label' => array('for' => "ContactMultiple1")),
  2223. 'One',
  2224. '/label',
  2225. '/div',
  2226. array('div' => array('class' => 'checkbox')),
  2227. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2228. array('label' => array('for' => "ContactMultiple2")),
  2229. 'Two',
  2230. '/label',
  2231. '/div',
  2232. array('div' => array('class' => 'checkbox')),
  2233. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2234. array('label' => array('for' => "ContactMultiple3")),
  2235. 'Three',
  2236. '/label',
  2237. '/div',
  2238. '/div'
  2239. );
  2240. $this->assertTags($result, $expected);
  2241. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2242. $this->assertTags($result, $expected);
  2243. $disabled = array('2', 3);
  2244. $expected = array(
  2245. array('div' => array('class' => 'input select')),
  2246. array('label' => array('for' => "ContactMultiple")),
  2247. 'Multiple',
  2248. '/label',
  2249. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2250. array('div' => array('class' => 'checkbox')),
  2251. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2252. array('label' => array('for' => "ContactMultiple1")),
  2253. 'One',
  2254. '/label',
  2255. '/div',
  2256. array('div' => array('class' => 'checkbox')),
  2257. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2258. array('label' => array('for' => "ContactMultiple2")),
  2259. 'Two',
  2260. '/label',
  2261. '/div',
  2262. array('div' => array('class' => 'checkbox')),
  2263. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2264. array('label' => array('for' => "ContactMultiple3")),
  2265. 'Three',
  2266. '/label',
  2267. '/div',
  2268. '/div'
  2269. );
  2270. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2271. $this->assertTags($result, $expected);
  2272. }
  2273. /**
  2274. * test input name with leading integer, ensure attributes are generated correctly.
  2275. *
  2276. * @return void
  2277. */
  2278. public function testInputWithLeadingInteger() {
  2279. $result = $this->Form->text('0.Node.title');
  2280. $expected = array(
  2281. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2282. );
  2283. $this->assertTags($result, $expected);
  2284. }
  2285. /**
  2286. * test form->input() with select type inputs.
  2287. *
  2288. * @return void
  2289. */
  2290. public function testInputSelectType() {
  2291. $result = $this->Form->input('email', array(
  2292. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2293. );
  2294. $expected = array(
  2295. 'div' => array('class' => 'input select'),
  2296. 'label' => array('for' => 'email'),
  2297. 'Email',
  2298. '/label',
  2299. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2300. array('option' => array('value' => '')),
  2301. '/option',
  2302. array('option' => array('value' => 'è')),
  2303. 'Firést',
  2304. '/option',
  2305. array('option' => array('value' => 'é')),
  2306. 'Secoènd',
  2307. '/option',
  2308. '/select',
  2309. '/div'
  2310. );
  2311. $this->assertTags($result, $expected);
  2312. $result = $this->Form->input('email', array(
  2313. 'options' => array('First', 'Second'), 'empty' => true)
  2314. );
  2315. $expected = array(
  2316. 'div' => array('class' => 'input select'),
  2317. 'label' => array('for' => 'email'),
  2318. 'Email',
  2319. '/label',
  2320. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2321. array('option' => array('value' => '')),
  2322. '/option',
  2323. array('option' => array('value' => '0')),
  2324. 'First',
  2325. '/option',
  2326. array('option' => array('value' => '1')),
  2327. 'Second',
  2328. '/option',
  2329. '/select',
  2330. '/div'
  2331. );
  2332. $this->assertTags($result, $expected);
  2333. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2334. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2335. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2336. $expected = array(
  2337. 'div' => array('class' => 'input select'),
  2338. 'label' => array('for' => 'ModelUserId'),
  2339. 'User',
  2340. '/label',
  2341. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2342. array('option' => array('value' => '')),
  2343. '/option',
  2344. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2345. 'good',
  2346. '/option',
  2347. array('option' => array('value' => 'other')),
  2348. 'bad',
  2349. '/option',
  2350. '/select',
  2351. '/div'
  2352. );
  2353. $this->assertTags($result, $expected);
  2354. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2355. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2356. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2357. $expected = array(
  2358. 'div' => array('class' => 'input select'),
  2359. 'label' => array('for' => 'ThingUserId'),
  2360. 'User',
  2361. '/label',
  2362. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2363. array('option' => array('value' => '')),
  2364. 'Some Empty',
  2365. '/option',
  2366. array('option' => array('value' => 'value')),
  2367. 'good',
  2368. '/option',
  2369. array('option' => array('value' => 'other')),
  2370. 'bad',
  2371. '/option',
  2372. '/select',
  2373. '/div'
  2374. );
  2375. $this->assertTags($result, $expected);
  2376. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2377. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2378. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2379. $expected = array(
  2380. 'div' => array('class' => 'input select'),
  2381. 'label' => array('for' => 'ThingUserId'),
  2382. 'User',
  2383. '/label',
  2384. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2385. array('option' => array('value' => '')),
  2386. 'Some Empty',
  2387. '/option',
  2388. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2389. 'good',
  2390. '/option',
  2391. array('option' => array('value' => 'other')),
  2392. 'bad',
  2393. '/option',
  2394. '/select',
  2395. '/div'
  2396. );
  2397. $this->assertTags($result, $expected);
  2398. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2399. $this->Form->request->data = array('User' => array('User' => array('value')));
  2400. $result = $this->Form->input('User.User', array('empty' => true));
  2401. $expected = array(
  2402. 'div' => array('class' => 'input select'),
  2403. 'label' => array('for' => 'UserUser'),
  2404. 'User',
  2405. '/label',
  2406. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2407. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2408. array('option' => array('value' => '')),
  2409. '/option',
  2410. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2411. 'good',
  2412. '/option',
  2413. array('option' => array('value' => 'other')),
  2414. 'bad',
  2415. '/option',
  2416. '/select',
  2417. '/div'
  2418. );
  2419. $this->assertTags($result, $expected);
  2420. $this->Form->data = array();
  2421. $result = $this->Form->input('Publisher.id', array(
  2422. 'label' => 'Publisher',
  2423. 'type' => 'select',
  2424. 'multiple' => 'checkbox',
  2425. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2426. ));
  2427. $expected = array(
  2428. array('div' => array('class' => 'input select')),
  2429. array('label' => array('for' => 'PublisherId')),
  2430. 'Publisher',
  2431. '/label',
  2432. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2433. array('div' => array('class' => 'checkbox')),
  2434. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2435. array('label' => array('for' => 'PublisherIdValue1')),
  2436. 'Label 1',
  2437. '/label',
  2438. '/div',
  2439. array('div' => array('class' => 'checkbox')),
  2440. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2441. array('label' => array('for' => 'PublisherIdValue2')),
  2442. 'Label 2',
  2443. '/label',
  2444. '/div',
  2445. '/div'
  2446. );
  2447. $this->assertTags($result, $expected);
  2448. }
  2449. /**
  2450. * test that input() and a non standard primary key makes a hidden input by default.
  2451. *
  2452. * @return void
  2453. */
  2454. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2455. $this->Form->create('User');
  2456. $this->Form->fieldset = array(
  2457. 'User' => array(
  2458. 'fields' => array(
  2459. 'model_id' => array('type' => 'integer')
  2460. ),
  2461. 'validates' => array(),
  2462. 'key' => 'model_id'
  2463. )
  2464. );
  2465. $result = $this->Form->input('model_id');
  2466. $expected = array(
  2467. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2468. );
  2469. $this->assertTags($result, $expected);
  2470. }
  2471. /**
  2472. * test that overriding the magic select type widget is possible
  2473. *
  2474. * @return void
  2475. */
  2476. public function testInputOverridingMagicSelectType() {
  2477. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2478. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2479. $expected = array(
  2480. 'div' => array('class' => 'input text'),
  2481. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2482. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2483. '/div'
  2484. );
  2485. $this->assertTags($result, $expected);
  2486. //Check that magic types still work for plural/singular vars
  2487. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2488. $result = $this->Form->input('Model.type');
  2489. $expected = array(
  2490. 'div' => array('class' => 'input select'),
  2491. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2492. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2493. array('option' => array('value' => 'value')), 'good', '/option',
  2494. array('option' => array('value' => 'other')), 'bad', '/option',
  2495. '/select',
  2496. '/div'
  2497. );
  2498. $this->assertTags($result, $expected);
  2499. }
  2500. /**
  2501. * Test that magic input() selects are created for type=number
  2502. *
  2503. * @return void
  2504. */
  2505. public function testInputMagicSelectForTypeNumber() {
  2506. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2507. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2508. $result = $this->Form->input('ValidateUser.balance');
  2509. $expected = array(
  2510. 'div' => array('class' => 'input select'),
  2511. 'label' => array('for' => 'ValidateUserBalance'),
  2512. 'Balance',
  2513. '/label',
  2514. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2515. array('option' => array('value' => '0')),
  2516. 'nothing',
  2517. '/option',
  2518. array('option' => array('value' => '1', 'selected' => 'selected')),
  2519. 'some',
  2520. '/option',
  2521. array('option' => array('value' => '100')),
  2522. 'a lot',
  2523. '/option',
  2524. '/select',
  2525. '/div'
  2526. );
  2527. $this->assertTags($result, $expected);
  2528. }
  2529. /**
  2530. * Test that magic input() selects can easily be converted into radio types without error.
  2531. *
  2532. * @return void
  2533. */
  2534. public function testInputMagicSelectChangeToRadio() {
  2535. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2536. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2537. $this->assertRegExp('/input type="radio"/', $result);
  2538. }
  2539. /**
  2540. * fields with the same name as the model should work.
  2541. *
  2542. * @return void
  2543. */
  2544. public function testInputWithMatchingFieldAndModelName() {
  2545. $this->Form->create('User');
  2546. $this->Form->fieldset = array(
  2547. 'User' => array(
  2548. 'fields' => array(
  2549. 'User' => array('type' => 'text')
  2550. ),
  2551. 'validates' => array(),
  2552. 'key' => 'id'
  2553. )
  2554. );
  2555. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2556. $result = $this->Form->input('User', array('type' => 'text'));
  2557. $expected = array(
  2558. 'div' => array('class' => 'input text'),
  2559. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2560. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2561. '/div'
  2562. );
  2563. $this->assertTags($result, $expected);
  2564. }
  2565. /**
  2566. * testFormInputs method
  2567. *
  2568. * test correct results from form::inputs().
  2569. *
  2570. * @return void
  2571. */
  2572. public function testFormInputs() {
  2573. $this->Form->create('Contact');
  2574. $result = $this->Form->inputs('The Legend');
  2575. $expected = array(
  2576. '<fieldset',
  2577. '<legend',
  2578. 'The Legend',
  2579. '/legend',
  2580. '*/fieldset',
  2581. );
  2582. $this->assertTags($result, $expected);
  2583. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2584. $expected = array(
  2585. 'fieldset' => array('class' => 'classy-stuff'),
  2586. '<legend',
  2587. 'Field of Dreams',
  2588. '/legend',
  2589. '*/fieldset'
  2590. );
  2591. $this->assertTags($result, $expected);
  2592. $this->Form->create('Contact');
  2593. $this->Form->request['prefix'] = 'admin';
  2594. $this->Form->request['action'] = 'admin_edit';
  2595. $result = $this->Form->inputs();
  2596. $expected = array(
  2597. '<fieldset',
  2598. '<legend',
  2599. 'Edit Contact',
  2600. '/legend',
  2601. '*/fieldset',
  2602. );
  2603. $this->assertTags($result, $expected);
  2604. $this->Form->create('Contact');
  2605. $result = $this->Form->inputs(false);
  2606. $expected = array(
  2607. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2608. array('div' => array('class' => 'input text')),
  2609. '*/div',
  2610. array('div' => array('class' => 'input text')),
  2611. '*/div',
  2612. array('div' => array('class' => 'input text')),
  2613. '*/div',
  2614. array('div' => array('class' => 'input password')),
  2615. '*/div',
  2616. array('div' => array('class' => 'input date')),
  2617. '*/div',
  2618. array('div' => array('class' => 'input date')),
  2619. '*/div',
  2620. array('div' => array('class' => 'input datetime')),
  2621. '*/div',
  2622. array('div' => array('class' => 'input number')),
  2623. '*/div',
  2624. array('div' => array('class' => 'input select')),
  2625. '*/div',
  2626. );
  2627. $this->assertTags($result, $expected);
  2628. $this->Form->create('Contact');
  2629. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2630. $expected = array(
  2631. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2632. array('div' => array('class' => 'input text')),
  2633. '*/div',
  2634. array('div' => array('class' => 'input text')),
  2635. '*/div',
  2636. array('div' => array('class' => 'input text')),
  2637. '*/div',
  2638. array('div' => array('class' => 'input password')),
  2639. '*/div',
  2640. array('div' => array('class' => 'input date')),
  2641. '*/div',
  2642. array('div' => array('class' => 'input date')),
  2643. '*/div',
  2644. array('div' => array('class' => 'input datetime')),
  2645. '*/div',
  2646. array('div' => array('class' => 'input number')),
  2647. '*/div',
  2648. array('div' => array('class' => 'input select')),
  2649. '*/div',
  2650. );
  2651. $this->assertTags($result, $expected);
  2652. $this->Form->create('Contact');
  2653. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2654. $expected = array(
  2655. 'fieldset' => array(),
  2656. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2657. array('div' => array('class' => 'input text')),
  2658. '*/div',
  2659. array('div' => array('class' => 'input text')),
  2660. '*/div',
  2661. array('div' => array('class' => 'input text')),
  2662. '*/div',
  2663. array('div' => array('class' => 'input password')),
  2664. '*/div',
  2665. array('div' => array('class' => 'input date')),
  2666. '*/div',
  2667. array('div' => array('class' => 'input date')),
  2668. '*/div',
  2669. array('div' => array('class' => 'input datetime')),
  2670. '*/div',
  2671. array('div' => array('class' => 'input number')),
  2672. '*/div',
  2673. array('div' => array('class' => 'input select')),
  2674. '*/div',
  2675. '/fieldset'
  2676. );
  2677. $this->assertTags($result, $expected);
  2678. $this->Form->create('Contact');
  2679. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2680. $expected = array(
  2681. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2682. array('div' => array('class' => 'input text')),
  2683. '*/div',
  2684. array('div' => array('class' => 'input text')),
  2685. '*/div',
  2686. array('div' => array('class' => 'input text')),
  2687. '*/div',
  2688. array('div' => array('class' => 'input password')),
  2689. '*/div',
  2690. array('div' => array('class' => 'input date')),
  2691. '*/div',
  2692. array('div' => array('class' => 'input date')),
  2693. '*/div',
  2694. array('div' => array('class' => 'input datetime')),
  2695. '*/div',
  2696. array('div' => array('class' => 'input number')),
  2697. '*/div',
  2698. array('div' => array('class' => 'input select')),
  2699. '*/div',
  2700. );
  2701. $this->assertTags($result, $expected);
  2702. $this->Form->create('Contact');
  2703. $result = $this->Form->inputs('Hello');
  2704. $expected = array(
  2705. 'fieldset' => array(),
  2706. 'legend' => array(),
  2707. 'Hello',
  2708. '/legend',
  2709. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2710. array('div' => array('class' => 'input text')),
  2711. '*/div',
  2712. array('div' => array('class' => 'input text')),
  2713. '*/div',
  2714. array('div' => array('class' => 'input text')),
  2715. '*/div',
  2716. array('div' => array('class' => 'input password')),
  2717. '*/div',
  2718. array('div' => array('class' => 'input date')),
  2719. '*/div',
  2720. array('div' => array('class' => 'input date')),
  2721. '*/div',
  2722. array('div' => array('class' => 'input datetime')),
  2723. '*/div',
  2724. array('div' => array('class' => 'input number')),
  2725. '*/div',
  2726. array('div' => array('class' => 'input select')),
  2727. '*/div',
  2728. '/fieldset'
  2729. );
  2730. $this->assertTags($result, $expected);
  2731. $this->Form->create('Contact');
  2732. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2733. $expected = array(
  2734. 'fieldset' => array(),
  2735. 'legend' => array(),
  2736. 'Hello',
  2737. '/legend',
  2738. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2739. array('div' => array('class' => 'input text')),
  2740. '*/div',
  2741. array('div' => array('class' => 'input text')),
  2742. '*/div',
  2743. array('div' => array('class' => 'input text')),
  2744. '*/div',
  2745. array('div' => array('class' => 'input password')),
  2746. '*/div',
  2747. array('div' => array('class' => 'input date')),
  2748. '*/div',
  2749. array('div' => array('class' => 'input date')),
  2750. '*/div',
  2751. array('div' => array('class' => 'input datetime')),
  2752. '*/div',
  2753. array('div' => array('class' => 'input number')),
  2754. '*/div',
  2755. array('div' => array('class' => 'input select')),
  2756. '*/div',
  2757. '/fieldset'
  2758. );
  2759. $this->assertTags($result, $expected);
  2760. }
  2761. /**
  2762. * testSelectAsCheckbox method
  2763. *
  2764. * test multi-select widget with checkbox formatting.
  2765. *
  2766. * @return void
  2767. */
  2768. public function testSelectAsCheckbox() {
  2769. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2770. $expected = array(
  2771. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2772. array('div' => array('class' => 'checkbox')),
  2773. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2774. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2775. 'first',
  2776. '/label',
  2777. '/div',
  2778. array('div' => array('class' => 'checkbox')),
  2779. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2780. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2781. 'second',
  2782. '/label',
  2783. '/div',
  2784. array('div' => array('class' => 'checkbox')),
  2785. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2786. array('label' => array('for' => 'ModelMultiField2')),
  2787. 'third',
  2788. '/label',
  2789. '/div',
  2790. );
  2791. $this->assertTags($result, $expected);
  2792. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2793. $expected = array(
  2794. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2795. array('div' => array('class' => 'checkbox')),
  2796. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2797. array('label' => array('for' => 'ModelMultiField12')),
  2798. 'half',
  2799. '/label',
  2800. '/div',
  2801. );
  2802. $this->assertTags($result, $expected);
  2803. }
  2804. /**
  2805. * testLabel method
  2806. *
  2807. * test label generation.
  2808. *
  2809. * @return void
  2810. */
  2811. public function testLabel() {
  2812. $this->Form->text('Person.name');
  2813. $result = $this->Form->label();
  2814. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2815. $this->Form->text('Person.name');
  2816. $result = $this->Form->label();
  2817. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2818. $result = $this->Form->label('Person.first_name');
  2819. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2820. $result = $this->Form->label('Person.first_name', 'Your first name');
  2821. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2822. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2823. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2824. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2825. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2826. $result = $this->Form->label('Person.first_name', '');
  2827. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2828. $result = $this->Form->label('Person.2.name', '');
  2829. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2830. }
  2831. /**
  2832. * testTextbox method
  2833. *
  2834. * test textbox element generation
  2835. *
  2836. * @return void
  2837. */
  2838. public function testTextbox() {
  2839. $result = $this->Form->text('Model.field');
  2840. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2841. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2842. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2843. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2844. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2845. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2846. $result = $this->Form->text('Model.text');
  2847. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2848. $Contact = ClassRegistry::getObject('Contact');
  2849. $Contact->validationErrors['text'] = array(true);
  2850. $this->Form->request->data['Contact']['text'] = 'test';
  2851. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2852. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2853. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2854. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2855. $expected = array(
  2856. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2857. );
  2858. $this->assertTags($result, $expected);
  2859. }
  2860. /**
  2861. * testDefaultValue method
  2862. *
  2863. * Test default value setting
  2864. *
  2865. * @return void
  2866. */
  2867. public function testDefaultValue() {
  2868. $this->Form->request->data['Model']['field'] = 'test';
  2869. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2870. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  2871. unset($this->Form->request->data['Model']['field']);
  2872. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2873. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  2874. }
  2875. /**
  2876. * testCheckboxDefaultValue method
  2877. *
  2878. * Test default value setting on checkbox() method
  2879. *
  2880. * @return void
  2881. */
  2882. public function testCheckboxDefaultValue() {
  2883. $this->Form->request->data['Model']['field'] = false;
  2884. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2885. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2886. unset($this->Form->request->data['Model']['field']);
  2887. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2888. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2889. $this->Form->request->data['Model']['field'] = true;
  2890. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2891. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2892. unset($this->Form->request->data['Model']['field']);
  2893. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2894. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2895. }
  2896. /**
  2897. * testError method
  2898. *
  2899. * Test field error generation
  2900. *
  2901. * @return void
  2902. */
  2903. public function testError() {
  2904. $Contact = ClassRegistry::getObject('Contact');
  2905. $Contact->validationErrors['field'] = array(1);
  2906. $result = $this->Form->error('Contact.field');
  2907. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  2908. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2909. $this->assertEquals('Error in field Field', $result);
  2910. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2911. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2912. $this->assertEquals('This field contains invalid input', $result);
  2913. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2914. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  2915. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  2916. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  2917. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  2918. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  2919. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2920. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  2921. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2922. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  2923. $this->assertEquals('<strong>Badness!</strong>', $result);
  2924. $Contact->validationErrors['field'] = array("email");
  2925. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  2926. $expected = array(
  2927. 'div' => array('class' => 'field-error'),
  2928. 'No good!',
  2929. '/div'
  2930. );
  2931. $this->assertTags($result, $expected);
  2932. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  2933. $result = $this->Form->error('Contact.field', array(
  2934. 'notEmpty' => 'Cannot be empty',
  2935. 'email' => 'No good!'
  2936. ));
  2937. $expected = array(
  2938. 'div' => array('class' => 'error-message'),
  2939. 'ul' => array(),
  2940. '<li', 'Cannot be empty', '/li',
  2941. '<li', 'No good!', '/li',
  2942. '<li', 'Something else', '/li',
  2943. '/ul',
  2944. '/div'
  2945. );
  2946. $this->assertTags($result, $expected);
  2947. /** Testing error messages list options **/
  2948. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  2949. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  2950. $expected = array(
  2951. 'div' => array('class' => 'error-message'),
  2952. 'ol' => array(),
  2953. '<li', 'notEmpty', '/li',
  2954. '<li', 'email', '/li',
  2955. '/ol',
  2956. '/div'
  2957. );
  2958. $this->assertTags($result, $expected);
  2959. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  2960. $expected = array(
  2961. 'div' => array('class' => 'error-message'),
  2962. 'ol' => array(),
  2963. '<li', 'notEmpty', '/li',
  2964. '<li', 'email', '/li',
  2965. '/ol',
  2966. '/div'
  2967. );
  2968. $this->assertTags($result, $expected);
  2969. $result = $this->Form->error('Contact.field', null, array(
  2970. 'listOptions' => array(
  2971. 'class' => 'ul-class',
  2972. 'itemOptions' => array(
  2973. 'class' => 'li-class'
  2974. )
  2975. )
  2976. ));
  2977. $expected = array(
  2978. 'div' => array('class' => 'error-message'),
  2979. 'ul' => array('class' => 'ul-class'),
  2980. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  2981. array('li' => array('class' => 'li-class')), 'email', '/li',
  2982. '/ul',
  2983. '/div'
  2984. );
  2985. $this->assertTags($result, $expected);
  2986. }
  2987. /**
  2988. * test error options when using form->input();
  2989. *
  2990. * @return void
  2991. */
  2992. public function testInputErrorEscape() {
  2993. $this->Form->create('ValidateProfile');
  2994. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  2995. $ValidateProfile->validationErrors['city'] = array('required<br>');
  2996. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
  2997. $this->assertRegExp('/required&lt;br&gt;/', $result);
  2998. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => false))));
  2999. $this->assertRegExp('/required<br>/', $result);
  3000. }
  3001. /**
  3002. * testPassword method
  3003. *
  3004. * Test password element generation
  3005. *
  3006. * @return void
  3007. */
  3008. public function testPassword() {
  3009. $Contact = ClassRegistry::getObject('Contact');
  3010. $result = $this->Form->password('Contact.field');
  3011. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  3012. $Contact->validationErrors['passwd'] = 1;
  3013. $this->Form->request->data['Contact']['passwd'] = 'test';
  3014. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3015. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3016. }
  3017. /**
  3018. * testRadio method
  3019. *
  3020. * Test radio element set generation
  3021. *
  3022. * @return void
  3023. */
  3024. public function testRadio() {
  3025. $result = $this->Form->radio('Model.field', array('option A'));
  3026. $expected = array(
  3027. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3028. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3029. 'label' => array('for' => 'ModelField0'),
  3030. 'option A',
  3031. '/label'
  3032. );
  3033. $this->assertTags($result, $expected);
  3034. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3035. $expected = array(
  3036. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3037. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  3038. 'label' => array('for' => 'ModelField12'),
  3039. 'half',
  3040. '/label'
  3041. );
  3042. $this->assertTags($result, $expected);
  3043. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3044. $expected = array(
  3045. 'fieldset' => array(),
  3046. 'legend' => array(),
  3047. 'Field',
  3048. '/legend',
  3049. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3050. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3051. array('label' => array('for' => 'ModelField0')),
  3052. 'option A',
  3053. '/label',
  3054. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3055. array('label' => array('for' => 'ModelField1')),
  3056. 'option B',
  3057. '/label',
  3058. '/fieldset'
  3059. );
  3060. $this->assertTags($result, $expected);
  3061. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3062. $expected = array(
  3063. 'fieldset' => array(),
  3064. 'legend' => array(),
  3065. 'Field',
  3066. '/legend',
  3067. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3068. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3069. array('label' => array('for' => 'ModelField0')),
  3070. 'option A',
  3071. '/label',
  3072. 'br' => array(),
  3073. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3074. array('label' => array('for' => 'ModelField1')),
  3075. 'option B',
  3076. '/label',
  3077. '/fieldset'
  3078. );
  3079. $this->assertTags($result, $expected);
  3080. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3081. $expected = array(
  3082. 'div' => array('class' => 'input radio'),
  3083. 'fieldset' => array(),
  3084. 'legend' => array(),
  3085. 'Legend title',
  3086. '/legend',
  3087. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3088. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3089. array('label' => array('for' => 'NewsletterSubscribe0')),
  3090. 'Unsubscribe',
  3091. '/label',
  3092. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3093. array('label' => array('for' => 'NewsletterSubscribe1')),
  3094. 'Subscribe',
  3095. '/label',
  3096. '/fieldset',
  3097. '/div'
  3098. );
  3099. $this->assertTags($result, $expected);
  3100. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3101. $expected = array(
  3102. 'div' => array('class' => 'input radio'),
  3103. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3104. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3105. array('label' => array('for' => 'NewsletterSubscribe0')),
  3106. 'Unsubscribe',
  3107. '/label',
  3108. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3109. array('label' => array('for' => 'NewsletterSubscribe1')),
  3110. 'Subscribe',
  3111. '/label',
  3112. '/div'
  3113. );
  3114. $this->assertTags($result, $expected);
  3115. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3116. $expected = array(
  3117. 'div' => array('class' => 'input radio'),
  3118. 'fieldset' => array(),
  3119. 'legend' => array(),
  3120. 'Legend title',
  3121. '/legend',
  3122. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3123. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3124. 'Unsubscribe',
  3125. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3126. 'Subscribe',
  3127. '/fieldset',
  3128. '/div'
  3129. );
  3130. $this->assertTags($result, $expected);
  3131. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3132. $expected = array(
  3133. 'div' => array('class' => 'input radio'),
  3134. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3135. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3136. 'Unsubscribe',
  3137. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3138. 'Subscribe',
  3139. '/div'
  3140. );
  3141. $this->assertTags($result, $expected);
  3142. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'value' => '1', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3143. $expected = array(
  3144. 'div' => array('class' => 'input radio'),
  3145. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3146. 'Unsubscribe',
  3147. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1', 'checked' => 'checked')),
  3148. 'Subscribe',
  3149. '/div'
  3150. );
  3151. $this->assertTags($result, $expected);
  3152. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3153. $expected = array(
  3154. 'fieldset' => array(),
  3155. 'legend' => array(),
  3156. 'Gender',
  3157. '/legend',
  3158. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3159. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3160. array('label' => array('for' => 'EmployeeGenderMale')),
  3161. 'Male',
  3162. '/label',
  3163. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3164. array('label' => array('for' => 'EmployeeGenderFemale')),
  3165. 'Female',
  3166. '/label',
  3167. '/fieldset',
  3168. );
  3169. $this->assertTags($result, $expected);
  3170. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3171. $expected = array(
  3172. 'fieldset' => array(),
  3173. 'legend' => array(),
  3174. 'Gender',
  3175. '/legend',
  3176. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3177. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3178. array('label' => array('for' => 'OfficerGenderMale')),
  3179. 'Male',
  3180. '/label',
  3181. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3182. array('label' => array('for' => 'OfficerGenderFemale')),
  3183. 'Female',
  3184. '/label',
  3185. '/fieldset',
  3186. );
  3187. $this->assertTags($result, $expected);
  3188. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3189. $expected = array(
  3190. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3191. array('input' => array('type' => 'radio', 'name' => 'data[Contact][1][imrequired]', 'value' => '0', 'id' => 'Contact1Imrequired0')),
  3192. 'label' => array('for' => 'Contact1Imrequired0'),
  3193. 'option A',
  3194. '/label'
  3195. );
  3196. $this->assertTags($result, $expected);
  3197. $result = $this->Form->radio('Model.1.field', array('option A'));
  3198. $expected = array(
  3199. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3200. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3201. 'label' => array('for' => 'Model1Field0'),
  3202. 'option A',
  3203. '/label'
  3204. );
  3205. $this->assertTags($result, $expected);
  3206. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3207. $expected = array(
  3208. 'fieldset' => array(),
  3209. 'legend' => array(),
  3210. 'Field',
  3211. '/legend',
  3212. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3213. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3214. array('label' => array('for' => 'ModelField0')),
  3215. 'option A',
  3216. '/label',
  3217. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3218. array('label' => array('for' => 'ModelField1')),
  3219. 'option B',
  3220. '/label',
  3221. '/fieldset'
  3222. );
  3223. $this->assertTags($result, $expected);
  3224. $result = $this->Form->radio(
  3225. 'Model.field',
  3226. array('option A', 'option B'),
  3227. array('between' => 'I am between')
  3228. );
  3229. $expected = array(
  3230. 'fieldset' => array(),
  3231. 'legend' => array(),
  3232. 'Field',
  3233. '/legend',
  3234. 'I am between',
  3235. 'input' => array(
  3236. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3237. 'value' => '', 'id' => 'ModelField_'
  3238. ),
  3239. array('input' => array(
  3240. 'type' => 'radio', 'name' => 'data[Model][field]',
  3241. 'value' => '0', 'id' => 'ModelField0'
  3242. )),
  3243. array('label' => array('for' => 'ModelField0')),
  3244. 'option A',
  3245. '/label',
  3246. array('input' => array(
  3247. 'type' => 'radio', 'name' => 'data[Model][field]',
  3248. 'value' => '1', 'id' => 'ModelField1'
  3249. )),
  3250. array('label' => array('for' => 'ModelField1')),
  3251. 'option B',
  3252. '/label',
  3253. '/fieldset'
  3254. );
  3255. $this->assertTags($result, $expected);
  3256. }
  3257. /**
  3258. * Test that radios with a 0 value are selected under the correct conditions.
  3259. *
  3260. * @return void
  3261. */
  3262. public function testRadioOptionWithZeroValue() {
  3263. $expected = array(
  3264. 'fieldset' => array(),
  3265. 'legend' => array(),
  3266. 'Field',
  3267. '/legend',
  3268. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3269. array('label' => array('for' => 'ModelField1')),
  3270. 'Yes',
  3271. '/label',
  3272. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3273. array('label' => array('for' => 'ModelField0')),
  3274. 'No',
  3275. '/label',
  3276. '/fieldset'
  3277. );
  3278. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3279. $this->assertTags($result, $expected);
  3280. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3281. $this->assertTags($result, $expected);
  3282. $expected = array(
  3283. 'fieldset' => array(),
  3284. 'legend' => array(),
  3285. 'Field',
  3286. '/legend',
  3287. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3288. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3289. array('label' => array('for' => 'ModelField1')),
  3290. 'Yes',
  3291. '/label',
  3292. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3293. array('label' => array('for' => 'ModelField0')),
  3294. 'No',
  3295. '/label',
  3296. '/fieldset'
  3297. );
  3298. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3299. $this->assertTags($result, $expected);
  3300. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3301. $this->assertTags($result, $expected);
  3302. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3303. $this->assertTags($result, $expected);
  3304. }
  3305. /**
  3306. * test disabled radio options
  3307. *
  3308. * @return void
  3309. */
  3310. public function testRadioDisabled() {
  3311. $result = $this->Form->radio(
  3312. 'Model.field',
  3313. array('option A', 'option B'),
  3314. array('disabled' => array('option A'), 'value' => '0')
  3315. );
  3316. $expected = array(
  3317. 'fieldset' => array(),
  3318. 'legend' => array(),
  3319. 'Field',
  3320. '/legend',
  3321. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3322. array('label' => array('for' => 'ModelField0')),
  3323. 'option A',
  3324. '/label',
  3325. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3326. array('label' => array('for' => 'ModelField1')),
  3327. 'option B',
  3328. '/label',
  3329. '/fieldset'
  3330. );
  3331. $this->assertTags($result, $expected);
  3332. $result = $this->Form->radio(
  3333. 'Model.field',
  3334. array('option A', 'option B'),
  3335. array('disabled' => true, 'value' => '0')
  3336. );
  3337. $expected = array(
  3338. 'fieldset' => array(),
  3339. 'legend' => array(),
  3340. 'Field',
  3341. '/legend',
  3342. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3343. array('label' => array('for' => 'ModelField0')),
  3344. 'option A',
  3345. '/label',
  3346. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3347. array('label' => array('for' => 'ModelField1')),
  3348. 'option B',
  3349. '/label',
  3350. '/fieldset'
  3351. );
  3352. $this->assertTags($result, $expected);
  3353. $result = $this->Form->radio(
  3354. 'Model.field',
  3355. array('option A', 'option B'),
  3356. array('disabled' => 'disabled', 'value' => '0')
  3357. );
  3358. $expected = array(
  3359. 'fieldset' => array(),
  3360. 'legend' => array(),
  3361. 'Field',
  3362. '/legend',
  3363. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3364. array('label' => array('for' => 'ModelField0')),
  3365. 'option A',
  3366. '/label',
  3367. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3368. array('label' => array('for' => 'ModelField1')),
  3369. 'option B',
  3370. '/label',
  3371. '/fieldset'
  3372. );
  3373. $this->assertTags($result, $expected);
  3374. }
  3375. /**
  3376. * test disabling the hidden input for radio buttons
  3377. *
  3378. * @return void
  3379. */
  3380. public function testRadioHiddenInputDisabling() {
  3381. $result = $this->Form->input('Model.1.field', array(
  3382. 'type' => 'radio',
  3383. 'options' => array('option A'),
  3384. 'hiddenField' => false
  3385. )
  3386. );
  3387. $expected = array(
  3388. 'div' => array('class' => 'input radio'),
  3389. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3390. 'label' => array('for' => 'Model1Field0'),
  3391. 'option A',
  3392. '/label',
  3393. '/div'
  3394. );
  3395. $this->assertTags($result, $expected);
  3396. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3397. $expected = array(
  3398. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3399. 'label' => array('for' => 'Model1Field0'),
  3400. 'option A',
  3401. '/label'
  3402. );
  3403. $this->assertTags($result, $expected);
  3404. }
  3405. /**
  3406. * test adding an empty option for radio buttons
  3407. *
  3408. * @return void
  3409. */
  3410. public function testRadioAddEmptyOption() {
  3411. $result = $this->Form->input('Model.1.field', array(
  3412. 'type' => 'radio',
  3413. 'options' => array('option A'),
  3414. 'empty' => true,
  3415. 'hiddenField' => false
  3416. ));
  3417. $expected = array(
  3418. 'div' => array('class' => 'input radio'),
  3419. 'fieldset' => array(),
  3420. 'legend' => array(),
  3421. 'Field',
  3422. '/legend',
  3423. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3424. array('label' => array('for' => 'Model1Field')),
  3425. __('empty'),
  3426. '/label',
  3427. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3428. array('label' => array('for' => 'Model1Field0')),
  3429. 'option A',
  3430. '/label',
  3431. '/fieldset',
  3432. '/div'
  3433. );
  3434. $this->assertTags($result, $expected);
  3435. $result = $this->Form->input('Model.1.field', array(
  3436. 'type' => 'radio',
  3437. 'options' => array('option A'),
  3438. 'empty' => 'CustomEmptyLabel',
  3439. 'hiddenField' => false
  3440. ));
  3441. $expected = array(
  3442. 'div' => array('class' => 'input radio'),
  3443. 'fieldset' => array(),
  3444. 'legend' => array(),
  3445. 'Field',
  3446. '/legend',
  3447. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3448. array('label' => array('for' => 'Model1Field')),
  3449. 'CustomEmptyLabel',
  3450. '/label',
  3451. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3452. array('label' => array('for' => 'Model1Field0')),
  3453. 'option A',
  3454. '/label',
  3455. '/fieldset',
  3456. '/div'
  3457. );
  3458. $this->assertTags($result, $expected);
  3459. $result = $this->Form->input('Model.1.field', array(
  3460. 'type' => 'radio',
  3461. 'options' => array('option A'),
  3462. 'empty' => false,
  3463. 'hiddenField' => false
  3464. ));
  3465. $this->assertTextNotContains('"Model1Field"', $result);
  3466. }
  3467. /**
  3468. * testSelect method
  3469. *
  3470. * Test select element generation.
  3471. *
  3472. * @return void
  3473. */
  3474. public function testSelect() {
  3475. $result = $this->Form->select('Model.field', array());
  3476. $expected = array(
  3477. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3478. array('option' => array('value' => '')),
  3479. '/option',
  3480. '/select'
  3481. );
  3482. $this->assertTags($result, $expected);
  3483. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3484. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3485. $expected = array(
  3486. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3487. array('option' => array('value' => '')),
  3488. '/option',
  3489. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3490. 'good',
  3491. '/option',
  3492. array('option' => array('value' => 'other')),
  3493. 'bad',
  3494. '/option',
  3495. '/select'
  3496. );
  3497. $this->assertTags($result, $expected);
  3498. $this->Form->request->data = array();
  3499. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3500. $expected = array(
  3501. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3502. array('option' => array('value' => '')),
  3503. '/option',
  3504. array('option' => array('value' => 'value')),
  3505. 'good',
  3506. '/option',
  3507. array('option' => array('value' => 'other')),
  3508. 'bad',
  3509. '/option',
  3510. '/select'
  3511. );
  3512. $this->assertTags($result, $expected);
  3513. $result = $this->Form->select(
  3514. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3515. array('empty' => false)
  3516. );
  3517. $expected = array(
  3518. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3519. array('option' => array('value' => 'first')),
  3520. 'first &quot;html&quot; &lt;chars&gt;',
  3521. '/option',
  3522. array('option' => array('value' => 'second')),
  3523. 'value',
  3524. '/option',
  3525. '/select'
  3526. );
  3527. $this->assertTags($result, $expected);
  3528. $result = $this->Form->select(
  3529. 'Model.field',
  3530. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3531. array('escape' => false, 'empty' => false)
  3532. );
  3533. $expected = array(
  3534. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3535. array('option' => array('value' => 'first')),
  3536. 'first "html" <chars>',
  3537. '/option',
  3538. array('option' => array('value' => 'second')),
  3539. 'value',
  3540. '/option',
  3541. '/select'
  3542. );
  3543. $this->assertTags($result, $expected);
  3544. $options = array(
  3545. array('value' => 'first', 'name' => 'First'),
  3546. array('value' => 'first', 'name' => 'Another First'),
  3547. );
  3548. $result = $this->Form->select(
  3549. 'Model.field',
  3550. $options,
  3551. array('escape' => false, 'empty' => false)
  3552. );
  3553. $expected = array(
  3554. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3555. array('option' => array('value' => 'first')),
  3556. 'First',
  3557. '/option',
  3558. array('option' => array('value' => 'first')),
  3559. 'Another First',
  3560. '/option',
  3561. '/select'
  3562. );
  3563. $this->assertTags($result, $expected);
  3564. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3565. $result = $this->Form->select(
  3566. 'Model.contact_id',
  3567. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3568. array('escape' => false, 'empty' => 'pick something')
  3569. );
  3570. $expected = array(
  3571. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3572. array('option' => array('value' => '')), 'pick something', '/option',
  3573. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3574. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3575. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3576. '/select'
  3577. );
  3578. $this->assertTags($result, $expected);
  3579. $this->Form->request->data['Model']['field'] = 0;
  3580. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3581. $expected = array(
  3582. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3583. array('option' => array('value' => '')), '/option',
  3584. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3585. array('option' => array('value' => '1')), 'Yes', '/option',
  3586. '/select'
  3587. );
  3588. $this->assertTags($result, $expected);
  3589. }
  3590. /**
  3591. * test that select() with optiongroups listens to the escape param.
  3592. *
  3593. * @return void
  3594. */
  3595. public function testSelectOptionGroupEscaping() {
  3596. $options = array(
  3597. '>< Key' => array(
  3598. 1 => 'One',
  3599. 2 => 'Two'
  3600. )
  3601. );
  3602. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3603. $expected = array(
  3604. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3605. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3606. array('option' => array('value' => '1')), 'One', '/option',
  3607. array('option' => array('value' => '2')), 'Two', '/option',
  3608. '/optgroup',
  3609. '/select'
  3610. );
  3611. $this->assertTags($result, $expected);
  3612. $options = array(
  3613. '>< Key' => array(
  3614. 1 => 'One',
  3615. 2 => 'Two'
  3616. )
  3617. );
  3618. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3619. $expected = array(
  3620. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3621. 'optgroup' => array('label' => '>< Key'),
  3622. array('option' => array('value' => '1')), 'One', '/option',
  3623. array('option' => array('value' => '2')), 'Two', '/option',
  3624. '/optgroup',
  3625. '/select'
  3626. );
  3627. $this->assertTags($result, $expected);
  3628. }
  3629. /**
  3630. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3631. *
  3632. * @return void
  3633. */
  3634. public function testSelectWithNullAttributes() {
  3635. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3636. $expected = array(
  3637. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3638. array('option' => array('value' => '0')),
  3639. 'first',
  3640. '/option',
  3641. array('option' => array('value' => '1')),
  3642. 'second',
  3643. '/option',
  3644. '/select'
  3645. );
  3646. $this->assertTags($result, $expected);
  3647. }
  3648. /**
  3649. * testNestedSelect method
  3650. *
  3651. * test select element generation with optgroups
  3652. *
  3653. * @return void
  3654. */
  3655. public function testNestedSelect() {
  3656. $result = $this->Form->select(
  3657. 'Model.field',
  3658. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3659. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3660. )), array('empty' => false)
  3661. );
  3662. $expected = array(
  3663. 'select' => array('name' => 'data[Model][field]',
  3664. 'id' => 'ModelField'),
  3665. array('option' => array('value' => 1)),
  3666. 'One',
  3667. '/option',
  3668. array('option' => array('value' => 2)),
  3669. 'Two',
  3670. '/option',
  3671. array('optgroup' => array('label' => 'Three')),
  3672. array('option' => array('value' => 4)),
  3673. 'Four',
  3674. '/option',
  3675. array('option' => array('value' => 5)),
  3676. 'Five',
  3677. '/option',
  3678. '/optgroup',
  3679. '/select'
  3680. );
  3681. $this->assertTags($result, $expected);
  3682. $result = $this->Form->select(
  3683. 'Model.field',
  3684. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3685. array('showParents' => true, 'empty' => false)
  3686. );
  3687. $expected = array(
  3688. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3689. array('option' => array('value' => 1)),
  3690. 'One',
  3691. '/option',
  3692. array('option' => array('value' => 2)),
  3693. 'Two',
  3694. '/option',
  3695. array('optgroup' => array('label' => 'Three')),
  3696. array('option' => array('value' => 3)),
  3697. 'Three',
  3698. '/option',
  3699. array('option' => array('value' => 4)),
  3700. 'Four',
  3701. '/option',
  3702. '/optgroup',
  3703. '/select'
  3704. );
  3705. $this->assertTags($result, $expected);
  3706. }
  3707. /**
  3708. * testSelectMultiple method
  3709. *
  3710. * test generation of multiple select elements
  3711. *
  3712. * @return void
  3713. */
  3714. public function testSelectMultiple() {
  3715. $options = array('first', 'second', 'third');
  3716. $result = $this->Form->select(
  3717. 'Model.multi_field', $options, array('multiple' => true)
  3718. );
  3719. $expected = array(
  3720. 'input' => array(
  3721. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3722. ),
  3723. 'select' => array(
  3724. 'name' => 'data[Model][multi_field][]',
  3725. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3726. ),
  3727. array('option' => array('value' => '0')),
  3728. 'first',
  3729. '/option',
  3730. array('option' => array('value' => '1')),
  3731. 'second',
  3732. '/option',
  3733. array('option' => array('value' => '2')),
  3734. 'third',
  3735. '/option',
  3736. '/select'
  3737. );
  3738. $this->assertTags($result, $expected);
  3739. $result = $this->Form->select(
  3740. 'Model.multi_field', $options, array('multiple' => 'multiple')
  3741. );
  3742. $expected = array(
  3743. 'input' => array(
  3744. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3745. ),
  3746. 'select' => array(
  3747. 'name' => 'data[Model][multi_field][]',
  3748. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3749. ),
  3750. array('option' => array('value' => '0')),
  3751. 'first',
  3752. '/option',
  3753. array('option' => array('value' => '1')),
  3754. 'second',
  3755. '/option',
  3756. array('option' => array('value' => '2')),
  3757. 'third',
  3758. '/option',
  3759. '/select'
  3760. );
  3761. $this->assertTags($result, $expected);
  3762. $result = $this->Form->select(
  3763. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  3764. );
  3765. $expected = array(
  3766. 'input' => array(
  3767. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3768. ),
  3769. 'select' => array(
  3770. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  3771. 'multiple' => 'multiple'
  3772. ),
  3773. array('option' => array('value' => '0', 'selected' => 'selected')),
  3774. 'first',
  3775. '/option',
  3776. array('option' => array('value' => '1', 'selected' => 'selected')),
  3777. 'second',
  3778. '/option',
  3779. array('option' => array('value' => '2')),
  3780. 'third',
  3781. '/option',
  3782. '/select'
  3783. );
  3784. $this->assertTags($result, $expected);
  3785. $result = $this->Form->select(
  3786. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  3787. );
  3788. $expected = array(
  3789. 'select' => array(
  3790. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  3791. ),
  3792. array('option' => array('value' => '0', 'selected' => 'selected')),
  3793. 'first',
  3794. '/option',
  3795. array('option' => array('value' => '1', 'selected' => 'selected')),
  3796. 'second',
  3797. '/option',
  3798. array('option' => array('value' => '2')),
  3799. 'third',
  3800. '/option',
  3801. '/select'
  3802. );
  3803. $this->assertTags($result, $expected);
  3804. }
  3805. /**
  3806. * test generation of habtm select boxes.
  3807. *
  3808. * @return void
  3809. */
  3810. public function testHabtmSelectBox() {
  3811. $this->View->viewVars['contactTags'] = array(
  3812. 1 => 'blue',
  3813. 2 => 'red',
  3814. 3 => 'green'
  3815. );
  3816. $this->Form->request->data = array(
  3817. 'Contact' => array(),
  3818. 'ContactTag' => array(
  3819. array(
  3820. 'id' => 1,
  3821. 'name' => 'blue'
  3822. ),
  3823. array(
  3824. 'id' => 3,
  3825. 'name' => 'green'
  3826. )
  3827. )
  3828. );
  3829. $this->Form->create('Contact');
  3830. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3831. $expected = array(
  3832. 'input' => array(
  3833. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3834. ),
  3835. 'select' => array(
  3836. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  3837. 'multiple' => 'multiple'
  3838. ),
  3839. array('option' => array('value' => '1', 'selected' => 'selected')),
  3840. 'blue',
  3841. '/option',
  3842. array('option' => array('value' => '2')),
  3843. 'red',
  3844. '/option',
  3845. array('option' => array('value' => '3', 'selected' => 'selected')),
  3846. 'green',
  3847. '/option',
  3848. '/select'
  3849. );
  3850. $this->assertTags($result, $expected);
  3851. }
  3852. /**
  3853. * test generation of multi select elements in checkbox format
  3854. *
  3855. * @return void
  3856. */
  3857. public function testSelectMultipleCheckboxes() {
  3858. $result = $this->Form->select(
  3859. 'Model.multi_field',
  3860. array('first', 'second', 'third'),
  3861. array('multiple' => 'checkbox')
  3862. );
  3863. $expected = array(
  3864. 'input' => array(
  3865. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3866. ),
  3867. array('div' => array('class' => 'checkbox')),
  3868. array('input' => array(
  3869. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3870. 'value' => '0', 'id' => 'ModelMultiField0'
  3871. )),
  3872. array('label' => array('for' => 'ModelMultiField0')),
  3873. 'first',
  3874. '/label',
  3875. '/div',
  3876. array('div' => array('class' => 'checkbox')),
  3877. array('input' => array(
  3878. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3879. 'value' => '1', 'id' => 'ModelMultiField1'
  3880. )),
  3881. array('label' => array('for' => 'ModelMultiField1')),
  3882. 'second',
  3883. '/label',
  3884. '/div',
  3885. array('div' => array('class' => 'checkbox')),
  3886. array('input' => array(
  3887. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3888. 'value' => '2', 'id' => 'ModelMultiField2'
  3889. )),
  3890. array('label' => array('for' => 'ModelMultiField2')),
  3891. 'third',
  3892. '/label',
  3893. '/div'
  3894. );
  3895. $this->assertTags($result, $expected);
  3896. $result = $this->Form->select(
  3897. 'Model.multi_field',
  3898. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3899. array('multiple' => 'checkbox')
  3900. );
  3901. $expected = array(
  3902. 'input' => array(
  3903. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3904. ),
  3905. array('div' => array('class' => 'checkbox')),
  3906. array('input' => array(
  3907. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3908. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  3909. )),
  3910. array('label' => array('for' => 'ModelMultiFieldA')),
  3911. 'first',
  3912. '/label',
  3913. '/div',
  3914. array('div' => array('class' => 'checkbox')),
  3915. array('input' => array(
  3916. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3917. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  3918. )),
  3919. array('label' => array('for' => 'ModelMultiFieldB')),
  3920. 'second',
  3921. '/label',
  3922. '/div',
  3923. array('div' => array('class' => 'checkbox')),
  3924. array('input' => array(
  3925. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3926. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  3927. )),
  3928. array('label' => array('for' => 'ModelMultiFieldC')),
  3929. 'third',
  3930. '/label',
  3931. '/div'
  3932. );
  3933. $this->assertTags($result, $expected);
  3934. $result = $this->Form->select(
  3935. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  3936. );
  3937. $expected = array(
  3938. 'input' => array(
  3939. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3940. ),
  3941. array('div' => array('class' => 'checkbox')),
  3942. array('input' => array(
  3943. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3944. 'value' => '1', 'id' => 'ModelMultiField1'
  3945. )),
  3946. array('label' => array('for' => 'ModelMultiField1')),
  3947. 'first',
  3948. '/label',
  3949. '/div'
  3950. );
  3951. $this->assertTags($result, $expected);
  3952. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3953. $result = $this->Form->select(
  3954. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3955. );
  3956. $expected = array(
  3957. 'input' => array(
  3958. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3959. ),
  3960. array('div' => array('class' => 'checkbox')),
  3961. array('input' => array(
  3962. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3963. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  3964. )),
  3965. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  3966. 'first',
  3967. '/label',
  3968. '/div',
  3969. array('div' => array('class' => 'checkbox')),
  3970. array('input' => array(
  3971. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3972. 'value' => 'Array', 'id' => 'ModelTagsArray'
  3973. )),
  3974. array('label' => array('for' => 'ModelTagsArray')),
  3975. 'Array',
  3976. '/label',
  3977. '/div'
  3978. );
  3979. $this->assertTags($result, $expected);
  3980. }
  3981. /**
  3982. * test multiple checkboxes with div styles.
  3983. *
  3984. * @return void
  3985. */
  3986. public function testSelectMultipleCheckboxDiv() {
  3987. $result = $this->Form->select(
  3988. 'Model.tags',
  3989. array('first', 'second'),
  3990. array('multiple' => 'checkbox', 'class' => 'my-class')
  3991. );
  3992. $expected = array(
  3993. 'input' => array(
  3994. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3995. ),
  3996. array('div' => array('class' => 'my-class')),
  3997. array('input' => array(
  3998. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3999. 'value' => '0', 'id' => 'ModelTags0'
  4000. )),
  4001. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4002. '/div',
  4003. array('div' => array('class' => 'my-class')),
  4004. array('input' => array(
  4005. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4006. 'value' => '1', 'id' => 'ModelTags1'
  4007. )),
  4008. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4009. '/div'
  4010. );
  4011. $this->assertTags($result, $expected);
  4012. $result = $this->Form->input('Model.tags', array(
  4013. 'options' => array('first', 'second'),
  4014. 'multiple' => 'checkbox',
  4015. 'class' => 'my-class',
  4016. 'div' => false,
  4017. 'label' => false
  4018. ));
  4019. $this->assertTags($result, $expected);
  4020. $Contact = ClassRegistry::getObject('Contact');
  4021. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4022. $result = $this->Form->input('Contact.tags', array(
  4023. 'options' => array('one'),
  4024. 'multiple' => 'checkbox',
  4025. 'label' => false,
  4026. 'div' => false
  4027. ));
  4028. $expected = array(
  4029. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4030. array('div' => array('class' => 'checkbox form-error')),
  4031. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4032. array('label' => array('for' => 'ContactTags0')),
  4033. 'one',
  4034. '/label',
  4035. '/div'
  4036. );
  4037. $this->assertTags($result, $expected);
  4038. $result = $this->Form->input('Contact.tags', array(
  4039. 'options' => array('one'),
  4040. 'multiple' => 'checkbox',
  4041. 'class' => 'mycheckbox',
  4042. 'label' => false,
  4043. 'div' => false
  4044. ));
  4045. $expected = array(
  4046. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4047. array('div' => array('class' => 'mycheckbox form-error')),
  4048. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4049. array('label' => array('for' => 'ContactTags0')),
  4050. 'one',
  4051. '/label',
  4052. '/div'
  4053. );
  4054. $this->assertTags($result, $expected);
  4055. }
  4056. /**
  4057. * Checks the security hash array generated for multiple-input checkbox elements
  4058. *
  4059. * @return void
  4060. */
  4061. public function testSelectMultipleCheckboxSecurity() {
  4062. $this->Form->request['_Token'] = array('key' => 'testKey');
  4063. $this->assertEquals(array(), $this->Form->fields);
  4064. $result = $this->Form->select(
  4065. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4066. array('multiple' => 'checkbox')
  4067. );
  4068. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4069. $result = $this->Form->secure($this->Form->fields);
  4070. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4071. $this->assertRegExp('/"' . $key . '"/', $result);
  4072. }
  4073. /**
  4074. * Multiple select elements should always be secured as they always participate
  4075. * in the POST data.
  4076. *
  4077. * @return void
  4078. */
  4079. public function testSelectMultipleSecureWithNoOptions() {
  4080. $this->Form->request['_Token'] = array('key' => 'testkey');
  4081. $this->assertEquals(array(), $this->Form->fields);
  4082. $result = $this->Form->select(
  4083. 'Model.select',
  4084. array(),
  4085. array('multiple' => true)
  4086. );
  4087. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4088. }
  4089. /**
  4090. * When a select box has no options it should not be added to the fields list
  4091. * as it always fail post validation.
  4092. *
  4093. * @return void
  4094. */
  4095. public function testSelectNoSecureWithNoOptions() {
  4096. $this->Form->request['_Token'] = array('key' => 'testkey');
  4097. $this->assertEquals(array(), $this->Form->fields);
  4098. $this->Form->select(
  4099. 'Model.select',
  4100. array()
  4101. );
  4102. $this->assertEquals(array(), $this->Form->fields);
  4103. $this->Form->select(
  4104. 'Model.select',
  4105. array(),
  4106. array('empty' => true)
  4107. );
  4108. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4109. }
  4110. /**
  4111. * testInputMultipleCheckboxes method
  4112. *
  4113. * test input() resulting in multi select elements being generated.
  4114. *
  4115. * @return void
  4116. */
  4117. public function testInputMultipleCheckboxes() {
  4118. $result = $this->Form->input('Model.multi_field', array(
  4119. 'options' => array('first', 'second', 'third'),
  4120. 'multiple' => 'checkbox'
  4121. ));
  4122. $expected = array(
  4123. array('div' => array('class' => 'input select')),
  4124. array('label' => array('for' => 'ModelMultiField')),
  4125. 'Multi Field',
  4126. '/label',
  4127. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4128. array('div' => array('class' => 'checkbox')),
  4129. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4130. array('label' => array('for' => 'ModelMultiField0')),
  4131. 'first',
  4132. '/label',
  4133. '/div',
  4134. array('div' => array('class' => 'checkbox')),
  4135. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4136. array('label' => array('for' => 'ModelMultiField1')),
  4137. 'second',
  4138. '/label',
  4139. '/div',
  4140. array('div' => array('class' => 'checkbox')),
  4141. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4142. array('label' => array('for' => 'ModelMultiField2')),
  4143. 'third',
  4144. '/label',
  4145. '/div',
  4146. '/div'
  4147. );
  4148. $this->assertTags($result, $expected);
  4149. $result = $this->Form->input('Model.multi_field', array(
  4150. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4151. 'multiple' => 'checkbox'
  4152. ));
  4153. $expected = array(
  4154. array('div' => array('class' => 'input select')),
  4155. array('label' => array('for' => 'ModelMultiField')),
  4156. 'Multi Field',
  4157. '/label',
  4158. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4159. array('div' => array('class' => 'checkbox')),
  4160. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4161. array('label' => array('for' => 'ModelMultiFieldA')),
  4162. 'first',
  4163. '/label',
  4164. '/div',
  4165. array('div' => array('class' => 'checkbox')),
  4166. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4167. array('label' => array('for' => 'ModelMultiFieldB')),
  4168. 'second',
  4169. '/label',
  4170. '/div',
  4171. array('div' => array('class' => 'checkbox')),
  4172. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4173. array('label' => array('for' => 'ModelMultiFieldC')),
  4174. 'third',
  4175. '/label',
  4176. '/div',
  4177. '/div'
  4178. );
  4179. $this->assertTags($result, $expected);
  4180. $result = $this->Form->input('Model.multi_field', array(
  4181. 'options' => array('1' => 'first'),
  4182. 'multiple' => 'checkbox',
  4183. 'label' => false,
  4184. 'div' => false
  4185. ));
  4186. $expected = array(
  4187. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4188. array('div' => array('class' => 'checkbox')),
  4189. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4190. array('label' => array('for' => 'ModelMultiField1')),
  4191. 'first',
  4192. '/label',
  4193. '/div'
  4194. );
  4195. $this->assertTags($result, $expected);
  4196. $result = $this->Form->input('Model.multi_field', array(
  4197. 'options' => array('2' => 'second'),
  4198. 'multiple' => 'checkbox',
  4199. 'label' => false,
  4200. 'div' => false
  4201. ));
  4202. $expected = array(
  4203. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4204. array('div' => array('class' => 'checkbox')),
  4205. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4206. array('label' => array('for' => 'ModelMultiField2')),
  4207. 'second',
  4208. '/label',
  4209. '/div'
  4210. );
  4211. $this->assertTags($result, $expected);
  4212. }
  4213. /**
  4214. * testSelectHiddenFieldOmission method
  4215. *
  4216. * test that select() with 'hiddenField' => false omits the hidden field
  4217. *
  4218. * @return void
  4219. */
  4220. public function testSelectHiddenFieldOmission() {
  4221. $result = $this->Form->select('Model.multi_field',
  4222. array('first', 'second'),
  4223. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4224. );
  4225. $expected = array(
  4226. array('div' => array('class' => 'checkbox')),
  4227. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4228. array('label' => array('for' => 'ModelMultiField0')),
  4229. 'first',
  4230. '/label',
  4231. '/div',
  4232. array('div' => array('class' => 'checkbox')),
  4233. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4234. array('label' => array('for' => 'ModelMultiField1')),
  4235. 'second',
  4236. '/label',
  4237. '/div'
  4238. );
  4239. $this->assertTags($result, $expected);
  4240. $result = $this->Form->input('Model.multi_field', array(
  4241. 'options' => array('first', 'second'),
  4242. 'multiple' => 'checkbox',
  4243. 'hiddenField' => false
  4244. ));
  4245. $expected = array(
  4246. array('div' => array('class' => 'input select')),
  4247. array('label' => array('for' => 'ModelMultiField')),
  4248. 'Multi Field',
  4249. '/label',
  4250. array('div' => array('class' => 'checkbox')),
  4251. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4252. array('label' => array('for' => 'ModelMultiField0')),
  4253. 'first',
  4254. '/label',
  4255. '/div',
  4256. array('div' => array('class' => 'checkbox')),
  4257. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4258. array('label' => array('for' => 'ModelMultiField1')),
  4259. 'second',
  4260. '/label',
  4261. '/div',
  4262. '/div'
  4263. );
  4264. $this->assertTags($result, $expected);
  4265. }
  4266. /**
  4267. * test that select() with multiple = checkbox works with overriding name attribute.
  4268. *
  4269. * @return void
  4270. */
  4271. public function testSelectCheckboxMultipleOverrideName() {
  4272. $result = $this->Form->input('category', array(
  4273. 'type' => 'select',
  4274. 'multiple' => 'checkbox',
  4275. 'name' => 'data[fish]',
  4276. 'options' => array('1', '2'),
  4277. 'div' => false,
  4278. 'label' => false,
  4279. ));
  4280. $expected = array(
  4281. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4282. array('div' => array('class' => 'checkbox')),
  4283. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4284. array('label' => array('for' => 'Category0')), '1', '/label',
  4285. '/div',
  4286. array('div' => array('class' => 'checkbox')),
  4287. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4288. array('label' => array('for' => 'Category1')), '2', '/label',
  4289. '/div'
  4290. );
  4291. $this->assertTags($result, $expected);
  4292. }
  4293. /**
  4294. * Test that 'id' overrides all the checkbox id's as well.
  4295. *
  4296. * @return void
  4297. */
  4298. public function testSelectCheckboxMultipleId() {
  4299. $result = $this->Form->select(
  4300. 'Model.multi_field',
  4301. array('first', 'second', 'third'),
  4302. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4303. );
  4304. $expected = array(
  4305. 'input' => array(
  4306. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4307. ),
  4308. array('div' => array('class' => 'checkbox')),
  4309. array('input' => array(
  4310. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4311. 'value' => '0', 'id' => 'CustomId0'
  4312. )),
  4313. array('label' => array('for' => 'CustomId0')),
  4314. 'first',
  4315. '/label',
  4316. '/div',
  4317. array('div' => array('class' => 'checkbox')),
  4318. array('input' => array(
  4319. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4320. 'value' => '1', 'id' => 'CustomId1'
  4321. )),
  4322. array('label' => array('for' => 'CustomId1')),
  4323. 'second',
  4324. '/label',
  4325. '/div',
  4326. array('div' => array('class' => 'checkbox')),
  4327. array('input' => array(
  4328. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4329. 'value' => '2', 'id' => 'CustomId2'
  4330. )),
  4331. array('label' => array('for' => 'CustomId2')),
  4332. 'third',
  4333. '/label',
  4334. '/div'
  4335. );
  4336. $this->assertTags($result, $expected);
  4337. }
  4338. /**
  4339. * testCheckbox method
  4340. *
  4341. * Test generation of checkboxes
  4342. *
  4343. * @return void
  4344. */
  4345. public function testCheckbox() {
  4346. $result = $this->Form->checkbox('Model.field');
  4347. $expected = array(
  4348. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4349. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4350. );
  4351. $this->assertTags($result, $expected);
  4352. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4353. $expected = array(
  4354. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4355. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4356. );
  4357. $this->assertTags($result, $expected);
  4358. $Contact = ClassRegistry::getObject('Contact');
  4359. $Contact->validationErrors['field'] = 1;
  4360. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4361. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4362. $expected = array(
  4363. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4364. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4365. );
  4366. $this->assertTags($result, $expected);
  4367. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4368. $expected = array(
  4369. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4370. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4371. );
  4372. $this->assertTags($result, $expected);
  4373. $this->Form->request->data['Contact']['field'] = '';
  4374. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4375. $expected = array(
  4376. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4377. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4378. );
  4379. $this->assertTags($result, $expected);
  4380. $Contact->validationErrors = array();
  4381. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4382. $expected = array(
  4383. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4384. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4385. );
  4386. $this->assertTags($result, $expected);
  4387. $this->Form->request->data['Contact']['published'] = 1;
  4388. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4389. $expected = array(
  4390. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4391. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4392. );
  4393. $this->assertTags($result, $expected);
  4394. $this->Form->request->data['Contact']['published'] = 0;
  4395. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4396. $expected = array(
  4397. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4398. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4399. );
  4400. $this->assertTags($result, $expected);
  4401. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4402. $expected = array(
  4403. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4404. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4405. );
  4406. $this->assertTags($result, $expected);
  4407. $result = $this->Form->checkbox('CustomField.1.value');
  4408. $expected = array(
  4409. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4410. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4411. );
  4412. $this->assertTags($result, $expected);
  4413. }
  4414. /**
  4415. * test checkbox() with a custom name attribute
  4416. *
  4417. * @return void
  4418. */
  4419. public function testCheckboxCustomNameAttribute() {
  4420. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4421. $expected = array(
  4422. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4423. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4424. );
  4425. $this->assertTags($result, $expected);
  4426. }
  4427. /**
  4428. * test the checked option for checkboxes.
  4429. *
  4430. * @return void
  4431. */
  4432. public function testCheckboxCheckedOption() {
  4433. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4434. $expected = array(
  4435. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4436. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4437. );
  4438. $this->assertTags($result, $expected);
  4439. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4440. $expected = array(
  4441. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4442. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4443. );
  4444. $this->assertTags($result, $expected);
  4445. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4446. $expected = array(
  4447. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4448. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4449. );
  4450. $this->assertTags($result, $expected);
  4451. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4452. $expected = array(
  4453. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4454. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4455. );
  4456. $this->assertTags($result, $expected);
  4457. $this->Form->request->data['Model']['field'] = 1;
  4458. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4459. $expected = array(
  4460. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4461. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4462. );
  4463. $this->assertTags($result, $expected);
  4464. }
  4465. /**
  4466. * Test that disabled attribute works on both the checkbox and hidden input.
  4467. *
  4468. * @return void
  4469. */
  4470. public function testCheckboxDisabling() {
  4471. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4472. $expected = array(
  4473. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4474. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4475. );
  4476. $this->assertTags($result, $expected);
  4477. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4478. $expected = array(
  4479. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4480. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4481. );
  4482. $this->assertTags($result, $expected);
  4483. }
  4484. /**
  4485. * Test that the hidden input for checkboxes can be omitted or set to a
  4486. * specific value.
  4487. *
  4488. * @return void
  4489. */
  4490. public function testCheckboxHiddenField() {
  4491. $result = $this->Form->input('UserForm.something', array(
  4492. 'type' => 'checkbox',
  4493. 'hiddenField' => false
  4494. ));
  4495. $expected = array(
  4496. 'div' => array('class' => 'input checkbox'),
  4497. array('input' => array(
  4498. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4499. 'value' => '1', 'id' => 'UserFormSomething'
  4500. )),
  4501. 'label' => array('for' => 'UserFormSomething'),
  4502. 'Something',
  4503. '/label',
  4504. '/div'
  4505. );
  4506. $this->assertTags($result, $expected);
  4507. $result = $this->Form->input('UserForm.something', array(
  4508. 'type' => 'checkbox',
  4509. 'value' => 'Y',
  4510. 'hiddenField' => 'N',
  4511. ));
  4512. $expected = array(
  4513. 'div' => array('class' => 'input checkbox'),
  4514. array('input' => array(
  4515. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  4516. 'value' => 'N', 'id' => 'UserFormSomething_'
  4517. )),
  4518. array('input' => array(
  4519. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4520. 'value' => 'Y', 'id' => 'UserFormSomething'
  4521. )),
  4522. 'label' => array('for' => 'UserFormSomething'),
  4523. 'Something',
  4524. '/label',
  4525. '/div'
  4526. );
  4527. $this->assertTags($result, $expected);
  4528. }
  4529. /**
  4530. * testDateTime method
  4531. *
  4532. * Test generation of date/time select elements
  4533. *
  4534. * @return void
  4535. */
  4536. public function testDateTime() {
  4537. extract($this->dateRegex);
  4538. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4539. $now = strtotime('now');
  4540. $expected = array(
  4541. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4542. $daysRegex,
  4543. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4544. date('j', $now),
  4545. '/option',
  4546. '*/select',
  4547. '-',
  4548. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4549. $monthsRegex,
  4550. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4551. date('F', $now),
  4552. '/option',
  4553. '*/select',
  4554. '-',
  4555. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4556. $yearsRegex,
  4557. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4558. date('Y', $now),
  4559. '/option',
  4560. '*/select',
  4561. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4562. $hoursRegex,
  4563. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4564. date('g', $now),
  4565. '/option',
  4566. '*/select',
  4567. ':',
  4568. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4569. $minutesRegex,
  4570. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4571. date('i', $now),
  4572. '/option',
  4573. '*/select',
  4574. ' ',
  4575. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4576. $meridianRegex,
  4577. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4578. date('a', $now),
  4579. '/option',
  4580. '*/select'
  4581. );
  4582. $this->assertTags($result, $expected);
  4583. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4584. $expected = array(
  4585. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4586. $daysRegex,
  4587. array('option' => array('value' => '')),
  4588. '/option',
  4589. '*/select',
  4590. '-',
  4591. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4592. $monthsRegex,
  4593. array('option' => array('value' => '')),
  4594. '/option',
  4595. '*/select',
  4596. '-',
  4597. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4598. $yearsRegex,
  4599. array('option' => array('value' => '')),
  4600. '/option',
  4601. '*/select',
  4602. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4603. $hoursRegex,
  4604. array('option' => array('value' => '')),
  4605. '/option',
  4606. '*/select',
  4607. ':',
  4608. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4609. $minutesRegex,
  4610. array('option' => array('value' => '')),
  4611. '/option',
  4612. '*/select',
  4613. ' ',
  4614. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4615. $meridianRegex,
  4616. array('option' => array('value' => '')),
  4617. '/option',
  4618. '*/select'
  4619. );
  4620. $this->assertTags($result, $expected);
  4621. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4622. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  4623. $this->assertTags($result, $expected);
  4624. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4625. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  4626. $this->assertTags($result, $expected);
  4627. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4628. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  4629. $expected = array(
  4630. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4631. $daysRegex,
  4632. array('option' => array('value' => '')),
  4633. '/option',
  4634. '*/select',
  4635. '-',
  4636. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4637. $monthsRegex,
  4638. array('option' => array('value' => '')),
  4639. '/option',
  4640. '*/select',
  4641. '-',
  4642. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4643. $yearsRegex,
  4644. array('option' => array('value' => '')),
  4645. '/option',
  4646. '*/select',
  4647. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4648. $hoursRegex,
  4649. array('option' => array('value' => '')),
  4650. '/option',
  4651. '*/select',
  4652. ':',
  4653. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4654. $minutesRegex,
  4655. array('option' => array('value' => '')),
  4656. '/option',
  4657. array('option' => array('value' => '00')),
  4658. '00',
  4659. '/option',
  4660. array('option' => array('value' => '05')),
  4661. '05',
  4662. '/option',
  4663. array('option' => array('value' => '10')),
  4664. '10',
  4665. '/option',
  4666. '*/select',
  4667. ' ',
  4668. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4669. $meridianRegex,
  4670. array('option' => array('value' => '')),
  4671. '/option',
  4672. '*/select'
  4673. );
  4674. $this->assertTags($result, $expected);
  4675. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4676. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4677. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4678. $this->Form->request->data['Contact']['data'] = null;
  4679. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4680. $expected = array(
  4681. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4682. $daysRegex,
  4683. array('option' => array('value' => '')),
  4684. '/option',
  4685. '*/select',
  4686. '-',
  4687. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4688. $monthsRegex,
  4689. array('option' => array('value' => '')),
  4690. '/option',
  4691. '*/select',
  4692. '-',
  4693. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4694. $yearsRegex,
  4695. array('option' => array('value' => '')),
  4696. '/option',
  4697. '*/select',
  4698. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4699. $hoursRegex,
  4700. array('option' => array('value' => '')),
  4701. '/option',
  4702. '*/select',
  4703. ':',
  4704. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4705. $minutesRegex,
  4706. array('option' => array('value' => '')),
  4707. '/option',
  4708. '*/select',
  4709. ' ',
  4710. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4711. $meridianRegex,
  4712. array('option' => array('value' => '')),
  4713. '/option',
  4714. '*/select'
  4715. );
  4716. $this->assertTags($result, $expected);
  4717. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4718. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  4719. $now = strtotime($this->Form->data['Model']['field']);
  4720. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  4721. $expected = array(
  4722. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4723. $daysRegex,
  4724. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4725. date('j', $now),
  4726. '/option',
  4727. '*/select',
  4728. '-',
  4729. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4730. $monthsRegex,
  4731. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4732. date('F', $now),
  4733. '/option',
  4734. '*/select',
  4735. '-',
  4736. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  4737. $yearsRegex,
  4738. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4739. date('Y', $now),
  4740. '/option',
  4741. '*/select',
  4742. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4743. $hoursRegex,
  4744. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4745. date('g', $now),
  4746. '/option',
  4747. '*/select',
  4748. ':',
  4749. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4750. $minutesRegex,
  4751. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4752. date('i', $now),
  4753. '/option',
  4754. '*/select',
  4755. ' ',
  4756. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  4757. $meridianRegex,
  4758. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4759. date('a', $now),
  4760. '/option',
  4761. '*/select'
  4762. );
  4763. $this->assertTags($result, $expected);
  4764. $selected = strtotime('2008-10-26 12:33:00');
  4765. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  4766. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  4767. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  4768. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  4769. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  4770. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  4771. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  4772. $this->Form->create('Contact');
  4773. $result = $this->Form->input('published');
  4774. $now = strtotime('now');
  4775. $expected = array(
  4776. 'div' => array('class' => 'input date'),
  4777. 'label' => array('for' => 'ContactPublishedMonth'),
  4778. 'Published',
  4779. '/label',
  4780. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4781. $monthsRegex,
  4782. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4783. date('F', $now),
  4784. '/option',
  4785. '*/select',
  4786. '-',
  4787. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4788. $daysRegex,
  4789. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4790. date('j', $now),
  4791. '/option',
  4792. '*/select',
  4793. '-',
  4794. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4795. $yearsRegex,
  4796. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4797. date('Y', $now),
  4798. '/option',
  4799. '*/select',
  4800. '/div'
  4801. );
  4802. $this->assertTags($result, $expected);
  4803. $result = $this->Form->input('published2', array('type' => 'date'));
  4804. $now = strtotime('now');
  4805. $expected = array(
  4806. 'div' => array('class' => 'input date'),
  4807. 'label' => array('for' => 'ContactPublished2Month'),
  4808. 'Published2',
  4809. '/label',
  4810. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  4811. $monthsRegex,
  4812. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4813. date('F', $now),
  4814. '/option',
  4815. '*/select',
  4816. '-',
  4817. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  4818. $daysRegex,
  4819. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4820. date('j', $now),
  4821. '/option',
  4822. '*/select',
  4823. '-',
  4824. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  4825. $yearsRegex,
  4826. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4827. date('Y', $now),
  4828. '/option',
  4829. '*/select',
  4830. '/div'
  4831. );
  4832. $this->assertTags($result, $expected);
  4833. $this->Form->create('Contact');
  4834. $result = $this->Form->input('published', array('monthNames' => false));
  4835. $now = strtotime('now');
  4836. $expected = array(
  4837. 'div' => array('class' => 'input date'),
  4838. 'label' => array('for' => 'ContactPublishedMonth'),
  4839. 'Published',
  4840. '/label',
  4841. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4842. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4843. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4844. date('m', $now),
  4845. '/option',
  4846. '*/select',
  4847. '-',
  4848. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4849. $daysRegex,
  4850. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4851. date('j', $now),
  4852. '/option',
  4853. '*/select',
  4854. '-',
  4855. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4856. $yearsRegex,
  4857. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4858. date('Y', $now),
  4859. '/option',
  4860. '*/select',
  4861. '/div'
  4862. );
  4863. $this->assertTags($result, $expected);
  4864. $result = $this->Form->input('published', array('type' => 'time'));
  4865. $now = strtotime('now');
  4866. $expected = array(
  4867. 'div' => array('class' => 'input time'),
  4868. 'label' => array('for' => 'ContactPublishedHour'),
  4869. 'Published',
  4870. '/label',
  4871. array('select' => array('name' => 'data[Contact][published][hour]', 'id' => 'ContactPublishedHour')),
  4872. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4873. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4874. date('g', $now),
  4875. '/option',
  4876. '*/select',
  4877. ':',
  4878. );
  4879. $this->assertTags($result, $expected);
  4880. $result = $this->Form->input('published', array(
  4881. 'timeFormat' => 24,
  4882. 'interval' => 5,
  4883. 'selected' => strtotime('2009-09-03 13:37:00'),
  4884. 'type' => 'datetime'
  4885. ));
  4886. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4887. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  4888. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  4889. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  4890. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  4891. $this->assertNoErrors();
  4892. $this->Form->request->data['Contact'] = array(
  4893. 'date' => array(
  4894. 'day' => '',
  4895. 'month' => '',
  4896. 'year' => '',
  4897. 'hour' => '',
  4898. 'min' => '',
  4899. 'meridian' => ''
  4900. )
  4901. );
  4902. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4903. }
  4904. /**
  4905. * test that datetime() and default values work.
  4906. *
  4907. * @return void
  4908. */
  4909. public function testDatetimeWithDefault() {
  4910. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  4911. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4912. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4913. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4914. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  4915. 'default' => '2009-06-01 11:15:30'
  4916. ));
  4917. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4918. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4919. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4920. }
  4921. /**
  4922. * test that bogus non-date time data doesn't cause errors.
  4923. *
  4924. * @return void
  4925. */
  4926. public function testDateTimeWithBogusData() {
  4927. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  4928. $this->assertNotRegExp('/selected="selected">\d/', $result);
  4929. }
  4930. /**
  4931. * testDateTimeEmptyAsArray
  4932. *
  4933. * @return void
  4934. */
  4935. public function testDateTimeEmptyAsArray() {
  4936. $result = $this->Form->dateTime('Contact.date',
  4937. 'DMY',
  4938. '12',
  4939. array(
  4940. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  4941. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  4942. )
  4943. )
  4944. );
  4945. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4946. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4947. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4948. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4949. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4950. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4951. $result = $this->Form->dateTime('Contact.date',
  4952. 'DMY',
  4953. '12',
  4954. array(
  4955. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4956. )
  4957. );
  4958. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4959. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4960. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4961. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  4962. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  4963. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  4964. }
  4965. /**
  4966. * testFormDateTimeMulti method
  4967. *
  4968. * test multiple datetime element generation
  4969. *
  4970. * @return void
  4971. */
  4972. public function testFormDateTimeMulti() {
  4973. extract($this->dateRegex);
  4974. $result = $this->Form->dateTime('Contact.1.updated');
  4975. $expected = array(
  4976. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  4977. $daysRegex,
  4978. array('option' => array('value' => '')),
  4979. '/option',
  4980. '*/select',
  4981. '-',
  4982. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  4983. $monthsRegex,
  4984. array('option' => array('value' => '')),
  4985. '/option',
  4986. '*/select',
  4987. '-',
  4988. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  4989. $yearsRegex,
  4990. array('option' => array('value' => '')),
  4991. '/option',
  4992. '*/select',
  4993. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  4994. $hoursRegex,
  4995. array('option' => array('value' => '')),
  4996. '/option',
  4997. '*/select',
  4998. ':',
  4999. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5000. $minutesRegex,
  5001. array('option' => array('value' => '')),
  5002. '/option',
  5003. '*/select',
  5004. ' ',
  5005. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5006. $meridianRegex,
  5007. array('option' => array('value' => '')),
  5008. '/option',
  5009. '*/select'
  5010. );
  5011. $this->assertTags($result, $expected);
  5012. $result = $this->Form->dateTime('Contact.2.updated');
  5013. $expected = array(
  5014. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5015. $daysRegex,
  5016. array('option' => array('value' => '')),
  5017. '/option',
  5018. '*/select',
  5019. '-',
  5020. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5021. $monthsRegex,
  5022. array('option' => array('value' => '')),
  5023. '/option',
  5024. '*/select',
  5025. '-',
  5026. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5027. $yearsRegex,
  5028. array('option' => array('value' => '')),
  5029. '/option',
  5030. '*/select',
  5031. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5032. $hoursRegex,
  5033. array('option' => array('value' => '')),
  5034. '/option',
  5035. '*/select',
  5036. ':',
  5037. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5038. $minutesRegex,
  5039. array('option' => array('value' => '')),
  5040. '/option',
  5041. '*/select',
  5042. ' ',
  5043. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5044. $meridianRegex,
  5045. array('option' => array('value' => '')),
  5046. '/option',
  5047. '*/select'
  5048. );
  5049. $this->assertTags($result, $expected);
  5050. }
  5051. /**
  5052. * When changing the date format, the label should always focus the first select box when
  5053. * clicked.
  5054. *
  5055. * @return void
  5056. */
  5057. public function testDateTimeLabelIdMatchesFirstInput() {
  5058. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5059. $this->assertContains('label for="ModelDateMonth"', $result);
  5060. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5061. $this->assertContains('label for="ModelDateDay"', $result);
  5062. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5063. $this->assertContains('label for="ModelDateYear"', $result);
  5064. }
  5065. /**
  5066. * testMonth method
  5067. *
  5068. * @return void
  5069. */
  5070. public function testMonth() {
  5071. $result = $this->Form->month('Model.field');
  5072. $expected = array(
  5073. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5074. array('option' => array('value' => '')),
  5075. '/option',
  5076. array('option' => array('value' => '01')),
  5077. date('F', strtotime('2008-01-01 00:00:00')),
  5078. '/option',
  5079. array('option' => array('value' => '02')),
  5080. date('F', strtotime('2008-02-01 00:00:00')),
  5081. '/option',
  5082. '*/select',
  5083. );
  5084. $this->assertTags($result, $expected);
  5085. $result = $this->Form->month('Model.field', array('empty' => true));
  5086. $expected = array(
  5087. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5088. array('option' => array('value' => '')),
  5089. '/option',
  5090. array('option' => array('value' => '01')),
  5091. date('F', strtotime('2008-01-01 00:00:00')),
  5092. '/option',
  5093. array('option' => array('value' => '02')),
  5094. date('F', strtotime('2008-02-01 00:00:00')),
  5095. '/option',
  5096. '*/select',
  5097. );
  5098. $this->assertTags($result, $expected);
  5099. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5100. $expected = array(
  5101. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5102. array('option' => array('value' => '')),
  5103. '/option',
  5104. array('option' => array('value' => '01')),
  5105. '01',
  5106. '/option',
  5107. array('option' => array('value' => '02')),
  5108. '02',
  5109. '/option',
  5110. '*/select',
  5111. );
  5112. $this->assertTags($result, $expected);
  5113. $monthNames = array(
  5114. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5115. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5116. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5117. $expected = array(
  5118. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5119. array('option' => array('value' => '')),
  5120. '/option',
  5121. array('option' => array('value' => '01')),
  5122. 'Jan',
  5123. '/option',
  5124. array('option' => array('value' => '02')),
  5125. 'Feb',
  5126. '/option',
  5127. '*/select',
  5128. );
  5129. $this->assertTags($result, $expected);
  5130. }
  5131. /**
  5132. * testDay method
  5133. *
  5134. * @return void
  5135. */
  5136. public function testDay() {
  5137. extract($this->dateRegex);
  5138. $result = $this->Form->day('Model.field', array('value' => false));
  5139. $expected = array(
  5140. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5141. array('option' => array('value' => '')),
  5142. '/option',
  5143. array('option' => array('value' => '01')),
  5144. '1',
  5145. '/option',
  5146. array('option' => array('value' => '02')),
  5147. '2',
  5148. '/option',
  5149. $daysRegex,
  5150. '/select',
  5151. );
  5152. $this->assertTags($result, $expected);
  5153. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5154. $result = $this->Form->day('Model.field');
  5155. $expected = array(
  5156. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5157. array('option' => array('value' => '')),
  5158. '/option',
  5159. array('option' => array('value' => '01')),
  5160. '1',
  5161. '/option',
  5162. array('option' => array('value' => '02')),
  5163. '2',
  5164. '/option',
  5165. $daysRegex,
  5166. array('option' => array('value' => '10', 'selected' => 'selected')),
  5167. '10',
  5168. '/option',
  5169. $daysRegex,
  5170. '/select',
  5171. );
  5172. $this->assertTags($result, $expected);
  5173. $this->Form->request->data['Model']['field'] = '';
  5174. $result = $this->Form->day('Model.field', array('value' => '10'));
  5175. $expected = array(
  5176. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5177. array('option' => array('value' => '')),
  5178. '/option',
  5179. array('option' => array('value' => '01')),
  5180. '1',
  5181. '/option',
  5182. array('option' => array('value' => '02')),
  5183. '2',
  5184. '/option',
  5185. $daysRegex,
  5186. array('option' => array('value' => '10', 'selected' => 'selected')),
  5187. '10',
  5188. '/option',
  5189. $daysRegex,
  5190. '/select',
  5191. );
  5192. $this->assertTags($result, $expected);
  5193. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5194. $result = $this->Form->day('Model.field', array('value' => true));
  5195. $expected = array(
  5196. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5197. array('option' => array('value' => '')),
  5198. '/option',
  5199. array('option' => array('value' => '01')),
  5200. '1',
  5201. '/option',
  5202. array('option' => array('value' => '02')),
  5203. '2',
  5204. '/option',
  5205. $daysRegex,
  5206. array('option' => array('value' => '10', 'selected' => 'selected')),
  5207. '10',
  5208. '/option',
  5209. $daysRegex,
  5210. '/select',
  5211. );
  5212. $this->assertTags($result, $expected);
  5213. }
  5214. /**
  5215. * testMinute method
  5216. *
  5217. * @return void
  5218. */
  5219. public function testMinute() {
  5220. extract($this->dateRegex);
  5221. $result = $this->Form->minute('Model.field');
  5222. $expected = array(
  5223. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5224. array('option' => array('value' => '')),
  5225. '/option',
  5226. array('option' => array('value' => '00')),
  5227. '00',
  5228. '/option',
  5229. array('option' => array('value' => '01')),
  5230. '01',
  5231. '/option',
  5232. array('option' => array('value' => '02')),
  5233. '02',
  5234. '/option',
  5235. $minutesRegex,
  5236. '/select',
  5237. );
  5238. $this->assertTags($result, $expected);
  5239. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5240. $result = $this->Form->minute('Model.field');
  5241. $expected = array(
  5242. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5243. array('option' => array('value' => '')),
  5244. '/option',
  5245. array('option' => array('value' => '00')),
  5246. '00',
  5247. '/option',
  5248. array('option' => array('value' => '01')),
  5249. '01',
  5250. '/option',
  5251. array('option' => array('value' => '02')),
  5252. '02',
  5253. '/option',
  5254. $minutesRegex,
  5255. array('option' => array('value' => '12', 'selected' => 'selected')),
  5256. '12',
  5257. '/option',
  5258. $minutesRegex,
  5259. '/select',
  5260. );
  5261. $this->assertTags($result, $expected);
  5262. $this->Form->request->data['Model']['field'] = '';
  5263. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5264. $expected = array(
  5265. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5266. array('option' => array('value' => '')),
  5267. '/option',
  5268. array('option' => array('value' => '00')),
  5269. '00',
  5270. '/option',
  5271. array('option' => array('value' => '05')),
  5272. '05',
  5273. '/option',
  5274. array('option' => array('value' => '10')),
  5275. '10',
  5276. '/option',
  5277. $minutesRegex,
  5278. '/select',
  5279. );
  5280. $this->assertTags($result, $expected);
  5281. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5282. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5283. $expected = array(
  5284. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5285. array('option' => array('value' => '')),
  5286. '/option',
  5287. array('option' => array('value' => '00')),
  5288. '00',
  5289. '/option',
  5290. array('option' => array('value' => '05')),
  5291. '05',
  5292. '/option',
  5293. array('option' => array('value' => '10', 'selected' => 'selected')),
  5294. '10',
  5295. '/option',
  5296. $minutesRegex,
  5297. '/select',
  5298. );
  5299. $this->assertTags($result, $expected);
  5300. }
  5301. /**
  5302. * testHour method
  5303. *
  5304. * @return void
  5305. */
  5306. public function testHour() {
  5307. extract($this->dateRegex);
  5308. $result = $this->Form->hour('Model.field', false);
  5309. $expected = array(
  5310. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5311. array('option' => array('value' => '')),
  5312. '/option',
  5313. array('option' => array('value' => '01')),
  5314. '1',
  5315. '/option',
  5316. array('option' => array('value' => '02')),
  5317. '2',
  5318. '/option',
  5319. $hoursRegex,
  5320. '/select',
  5321. );
  5322. $this->assertTags($result, $expected);
  5323. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5324. $result = $this->Form->hour('Model.field', false);
  5325. $expected = array(
  5326. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5327. array('option' => array('value' => '')),
  5328. '/option',
  5329. array('option' => array('value' => '01')),
  5330. '1',
  5331. '/option',
  5332. array('option' => array('value' => '02')),
  5333. '2',
  5334. '/option',
  5335. $hoursRegex,
  5336. array('option' => array('value' => '12', 'selected' => 'selected')),
  5337. '12',
  5338. '/option',
  5339. '/select',
  5340. );
  5341. $this->assertTags($result, $expected);
  5342. $this->Form->request->data['Model']['field'] = '';
  5343. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5344. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5345. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  5346. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5347. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5348. $result = $this->Form->hour('Model.field', true);
  5349. $expected = array(
  5350. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5351. array('option' => array('value' => '')),
  5352. '/option',
  5353. array('option' => array('value' => '00', 'selected' => 'selected')),
  5354. '0',
  5355. '/option',
  5356. array('option' => array('value' => '01')),
  5357. '1',
  5358. '/option',
  5359. array('option' => array('value' => '02')),
  5360. '2',
  5361. '/option',
  5362. $hoursRegex,
  5363. '/select',
  5364. );
  5365. $this->assertTags($result, $expected);
  5366. unset($this->Form->request->data['Model']['field']);
  5367. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5368. $thisHour = date('H');
  5369. $optValue = date('G');
  5370. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5371. }
  5372. /**
  5373. * testYear method
  5374. *
  5375. * @return void
  5376. */
  5377. public function testYear() {
  5378. $result = $this->Form->year('Model.field', 2006, 2007);
  5379. $expected = array(
  5380. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5381. array('option' => array('value' => '')),
  5382. '/option',
  5383. array('option' => array('value' => '2007')),
  5384. '2007',
  5385. '/option',
  5386. array('option' => array('value' => '2006')),
  5387. '2006',
  5388. '/option',
  5389. '/select',
  5390. );
  5391. $this->assertTags($result, $expected);
  5392. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5393. $expected = array(
  5394. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5395. array('option' => array('value' => '')),
  5396. '/option',
  5397. array('option' => array('value' => '2006')),
  5398. '2006',
  5399. '/option',
  5400. array('option' => array('value' => '2007')),
  5401. '2007',
  5402. '/option',
  5403. '/select',
  5404. );
  5405. $this->assertTags($result, $expected);
  5406. $this->request->data['Contact']['published'] = '';
  5407. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5408. $expected = array(
  5409. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5410. array('option' => array('value' => '')),
  5411. '/option',
  5412. array('option' => array('value' => '2007')),
  5413. '2007',
  5414. '/option',
  5415. array('option' => array('value' => '2006')),
  5416. '2006',
  5417. '/option',
  5418. '/select',
  5419. );
  5420. $this->assertTags($result, $expected);
  5421. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5422. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5423. $expected = array(
  5424. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5425. array('option' => array('value' => '2007')),
  5426. '2007',
  5427. '/option',
  5428. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5429. '2006',
  5430. '/option',
  5431. '/select',
  5432. );
  5433. $this->assertTags($result, $expected);
  5434. $this->Form->request->data['Contact']['published'] = '';
  5435. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5436. $expected = array(
  5437. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5438. array('option' => array('value' => '')),
  5439. '/option',
  5440. array('option' => array('value' => '2007')),
  5441. '2007',
  5442. '/option',
  5443. array('option' => array('value' => '2006')),
  5444. '2006',
  5445. '/option',
  5446. '/select',
  5447. );
  5448. $this->assertTags($result, $expected);
  5449. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5450. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5451. $expected = array(
  5452. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5453. array('option' => array('value' => '2007')),
  5454. '2007',
  5455. '/option',
  5456. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5457. '2006',
  5458. '/option',
  5459. '/select',
  5460. );
  5461. $this->assertTags($result, $expected);
  5462. $this->Form->request->data['Contact']['published'] = '';
  5463. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5464. $expected = array(
  5465. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5466. array('option' => array('value' => '')),
  5467. '/option',
  5468. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5469. '2007',
  5470. '/option',
  5471. array('option' => array('value' => '2006')),
  5472. '2006',
  5473. '/option',
  5474. '/select',
  5475. );
  5476. $this->assertTags($result, $expected);
  5477. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5478. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5479. $expected = array(
  5480. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5481. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5482. '2007',
  5483. '/option',
  5484. array('option' => array('value' => '2006')),
  5485. '2006',
  5486. '/option',
  5487. '/select',
  5488. );
  5489. $this->assertTags($result, $expected);
  5490. $this->Form->request->data['Contact']['published'] = '';
  5491. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5492. $expected = array(
  5493. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5494. array('option' => array('value' => '2008')),
  5495. '2008',
  5496. '/option',
  5497. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5498. '2007',
  5499. '/option',
  5500. array('option' => array('value' => '2006')),
  5501. '2006',
  5502. '/option',
  5503. '/select',
  5504. );
  5505. $this->assertTags($result, $expected);
  5506. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5507. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5508. $expected = array(
  5509. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5510. array('option' => array('value' => '2008')),
  5511. '2008',
  5512. '/option',
  5513. array('option' => array('value' => '2007')),
  5514. '2007',
  5515. '/option',
  5516. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5517. '2006',
  5518. '/option',
  5519. '/select',
  5520. );
  5521. $this->assertTags($result, $expected);
  5522. $this->Form->request->data = array();
  5523. $this->Form->create('Contact');
  5524. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5525. $expected = array(
  5526. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5527. array('option' => array('value' => '2008')),
  5528. '2008',
  5529. '/option',
  5530. array('option' => array('value' => '2007')),
  5531. '2007',
  5532. '/option',
  5533. array('option' => array('value' => '2006')),
  5534. '2006',
  5535. '/option',
  5536. '/select',
  5537. );
  5538. $this->assertTags($result, $expected);
  5539. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  5540. $this->assertContains('data[Contact][published][year]', $result);
  5541. }
  5542. /**
  5543. * testTextArea method
  5544. *
  5545. * @return void
  5546. */
  5547. public function testTextArea() {
  5548. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  5549. $result = $this->Form->textarea('Model.field');
  5550. $expected = array(
  5551. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5552. 'some test data',
  5553. '/textarea',
  5554. );
  5555. $this->assertTags($result, $expected);
  5556. $result = $this->Form->textarea('Model.tmp');
  5557. $expected = array(
  5558. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  5559. '/textarea',
  5560. );
  5561. $this->assertTags($result, $expected);
  5562. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5563. $result = $this->Form->textarea('Model.field');
  5564. $expected = array(
  5565. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5566. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5567. '/textarea',
  5568. );
  5569. $this->assertTags($result, $expected);
  5570. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5571. $result = $this->Form->textarea('Model.field', array('escape' => false));
  5572. $expected = array(
  5573. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5574. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5575. '/textarea',
  5576. );
  5577. $this->assertTags($result, $expected);
  5578. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  5579. $result = $this->Form->textarea('Model.0.OtherModel.field');
  5580. $expected = array(
  5581. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  5582. '/textarea'
  5583. );
  5584. $this->assertTags($result, $expected);
  5585. }
  5586. /**
  5587. * testTextAreaWithStupidCharacters method
  5588. *
  5589. * test text area with non-ascii characters
  5590. *
  5591. * @return void
  5592. */
  5593. public function testTextAreaWithStupidCharacters() {
  5594. $this->loadFixtures('Post');
  5595. $result = $this->Form->input('Post.content', array(
  5596. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  5597. ));
  5598. $expected = array(
  5599. 'div' => array('class' => 'input text'),
  5600. 'label' => array('for' => 'PostContent'),
  5601. 'Current Text',
  5602. '/label',
  5603. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  5604. 'GREAT®',
  5605. '/textarea',
  5606. '/div'
  5607. );
  5608. $this->assertTags($result, $expected);
  5609. }
  5610. /**
  5611. * testHiddenField method
  5612. *
  5613. * @return void
  5614. */
  5615. public function testHiddenField() {
  5616. $Contact = ClassRegistry::getObject('Contact');
  5617. $Contact->validationErrors['field'] = 1;
  5618. $this->Form->request->data['Contact']['field'] = 'test';
  5619. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  5620. $this->assertTags($result, array(
  5621. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  5622. );
  5623. }
  5624. /**
  5625. * testFileUploadField method
  5626. *
  5627. * @return void
  5628. */
  5629. public function testFileUploadField() {
  5630. $result = $this->Form->file('Model.upload');
  5631. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5632. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  5633. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  5634. $expected = array(
  5635. 'div' => array('class' => 'input file'),
  5636. 'label' => array('for' => 'ModelUpload'),
  5637. 'Upload',
  5638. '/label',
  5639. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  5640. '/div'
  5641. );
  5642. $this->assertTags($result, $expected);
  5643. }
  5644. /**
  5645. * test File upload input on a model not used in create();
  5646. *
  5647. * @return void
  5648. */
  5649. public function testFileUploadOnOtherModel() {
  5650. $this->Form->create('ValidateUser', array('type' => 'file'));
  5651. $result = $this->Form->file('ValidateProfile.city');
  5652. $expected = array(
  5653. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  5654. );
  5655. $this->assertTags($result, $expected);
  5656. }
  5657. /**
  5658. * testButton method
  5659. *
  5660. * @return void
  5661. */
  5662. public function testButton() {
  5663. $result = $this->Form->button('Hi');
  5664. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5665. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5666. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5667. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5668. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5669. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5670. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5671. $result = $this->Form->button('No type', array('type' => false));
  5672. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5673. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  5674. $this->assertNotRegExp('/\&039/', $result);
  5675. }
  5676. /**
  5677. * Test that button() makes unlocked fields by default.
  5678. *
  5679. * @return void
  5680. */
  5681. public function testButtonUnlockedByDefault() {
  5682. $this->Form->request->params['_Token']['key'] = 'secured';
  5683. $this->Form->button('Save', array('name' => 'save'));
  5684. $this->Form->button('Clear');
  5685. $result = $this->Form->unlockField();
  5686. $this->assertEquals(array('save'), $result);
  5687. }
  5688. /**
  5689. * testPostButton method
  5690. *
  5691. * @return void
  5692. */
  5693. public function testPostButton() {
  5694. $result = $this->Form->postButton('Hi', '/controller/action');
  5695. $this->assertTags($result, array(
  5696. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5697. 'div' => array('style' => 'display:none;'),
  5698. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5699. '/div',
  5700. 'button' => array('type' => 'submit'),
  5701. 'Hi',
  5702. '/button',
  5703. '/form'
  5704. ));
  5705. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5706. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  5707. }
  5708. /**
  5709. * Test that postButton adds _Token fields.
  5710. *
  5711. * @return void
  5712. */
  5713. public function testSecurePostButton() {
  5714. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5715. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5716. $expected = array(
  5717. 'form' => array(
  5718. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5719. ),
  5720. array('div' => array('style' => 'display:none;')),
  5721. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5722. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5723. '/div',
  5724. 'button' => array('type' => 'submit'),
  5725. 'Delete',
  5726. '/button',
  5727. array('div' => array('style' => 'display:none;')),
  5728. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5729. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5730. '/div',
  5731. '/form',
  5732. );
  5733. $this->assertTags($result, $expected);
  5734. }
  5735. /**
  5736. * testPostLink method
  5737. *
  5738. * @return void
  5739. */
  5740. public function testPostLink() {
  5741. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5742. $this->assertTags($result, array(
  5743. 'form' => array(
  5744. 'method' => 'post', 'action' => '/posts/delete/1',
  5745. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5746. ),
  5747. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5748. '/form',
  5749. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5750. 'Delete',
  5751. '/a'
  5752. ));
  5753. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  5754. $this->assertTags($result, array(
  5755. 'form' => array(
  5756. 'method' => 'post', 'action' => '/posts/delete/1',
  5757. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5758. ),
  5759. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  5760. '/form',
  5761. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5762. 'Delete',
  5763. '/a'
  5764. ));
  5765. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5766. $this->assertTags($result, array(
  5767. 'form' => array(
  5768. 'method' => 'post', 'action' => '/posts/delete/1',
  5769. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5770. ),
  5771. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5772. '/form',
  5773. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5774. 'Delete',
  5775. '/a'
  5776. ));
  5777. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5778. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  5779. }
  5780. /**
  5781. * Test that postLink adds _Token fields.
  5782. *
  5783. * @return void
  5784. */
  5785. public function testSecurePostLink() {
  5786. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5787. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5788. $expected = array(
  5789. 'form' => array(
  5790. 'method' => 'post', 'action' => '/posts/delete/1',
  5791. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5792. ),
  5793. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5794. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5795. 'div' => array('style' => 'display:none;'),
  5796. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5797. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5798. '/div',
  5799. '/form',
  5800. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5801. 'Delete',
  5802. '/a'
  5803. );
  5804. $this->assertTags($result, $expected);
  5805. }
  5806. /**
  5807. * testSubmitButton method
  5808. *
  5809. * @return void
  5810. */
  5811. public function testSubmitButton() {
  5812. $result = $this->Form->submit('');
  5813. $expected = array(
  5814. 'div' => array('class' => 'submit'),
  5815. 'input' => array('type' => 'submit', 'value' => ''),
  5816. '/div'
  5817. );
  5818. $this->assertTags($result, $expected);
  5819. $result = $this->Form->submit('Test Submit');
  5820. $expected = array(
  5821. 'div' => array('class' => 'submit'),
  5822. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5823. '/div'
  5824. );
  5825. $this->assertTags($result, $expected);
  5826. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5827. $expected = array(
  5828. 'span' => array('class' => 'submit'),
  5829. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5830. '/span'
  5831. );
  5832. $this->assertTags($result, $expected);
  5833. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5834. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5835. $this->assertTags($result, $expected);
  5836. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5837. $expected = array(
  5838. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5839. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5840. '/div'
  5841. );
  5842. $this->assertTags($result, $expected);
  5843. $result = $this->Form->submit('Next >');
  5844. $expected = array(
  5845. 'div' => array('class' => 'submit'),
  5846. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5847. '/div'
  5848. );
  5849. $this->assertTags($result, $expected);
  5850. $result = $this->Form->submit('Next >', array('escape' => false));
  5851. $expected = array(
  5852. 'div' => array('class' => 'submit'),
  5853. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5854. '/div'
  5855. );
  5856. $this->assertTags($result, $expected);
  5857. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5858. $expected = array(
  5859. 'div' => array('class' => 'submit'),
  5860. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5861. '/div'
  5862. );
  5863. $this->assertTags($result, $expected);
  5864. $before = '--before--';
  5865. $after = '--after--';
  5866. $result = $this->Form->submit('Test', array('before' => $before));
  5867. $expected = array(
  5868. 'div' => array('class' => 'submit'),
  5869. '--before--',
  5870. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5871. '/div'
  5872. );
  5873. $this->assertTags($result, $expected);
  5874. $result = $this->Form->submit('Test', array('after' => $after));
  5875. $expected = array(
  5876. 'div' => array('class' => 'submit'),
  5877. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5878. '--after--',
  5879. '/div'
  5880. );
  5881. $this->assertTags($result, $expected);
  5882. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5883. $expected = array(
  5884. 'div' => array('class' => 'submit'),
  5885. '--before--',
  5886. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5887. '--after--',
  5888. '/div'
  5889. );
  5890. $this->assertTags($result, $expected);
  5891. }
  5892. /**
  5893. * test image submit types.
  5894. *
  5895. * @return void
  5896. */
  5897. public function testSubmitImage() {
  5898. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5899. $expected = array(
  5900. 'div' => array('class' => 'submit'),
  5901. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5902. '/div'
  5903. );
  5904. $this->assertTags($result, $expected);
  5905. $result = $this->Form->submit('/relative/cake.power.gif');
  5906. $expected = array(
  5907. 'div' => array('class' => 'submit'),
  5908. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5909. '/div'
  5910. );
  5911. $this->assertTags($result, $expected);
  5912. $result = $this->Form->submit('cake.power.gif');
  5913. $expected = array(
  5914. 'div' => array('class' => 'submit'),
  5915. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5916. '/div'
  5917. );
  5918. $this->assertTags($result, $expected);
  5919. $result = $this->Form->submit('Not.an.image');
  5920. $expected = array(
  5921. 'div' => array('class' => 'submit'),
  5922. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5923. '/div'
  5924. );
  5925. $this->assertTags($result, $expected);
  5926. $after = '--after--';
  5927. $before = '--before--';
  5928. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5929. $expected = array(
  5930. 'div' => array('class' => 'submit'),
  5931. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5932. '--after--',
  5933. '/div'
  5934. );
  5935. $this->assertTags($result, $expected);
  5936. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5937. $expected = array(
  5938. 'div' => array('class' => 'submit'),
  5939. '--before--',
  5940. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5941. '/div'
  5942. );
  5943. $this->assertTags($result, $expected);
  5944. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5945. $expected = array(
  5946. 'div' => array('class' => 'submit'),
  5947. '--before--',
  5948. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5949. '--after--',
  5950. '/div'
  5951. );
  5952. $this->assertTags($result, $expected);
  5953. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  5954. $expected = array(
  5955. 'div' => array('class' => 'submit'),
  5956. '--before--',
  5957. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5958. '--after--',
  5959. '/div'
  5960. );
  5961. $this->assertTags($result, $expected);
  5962. }
  5963. /**
  5964. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5965. * be submitted at a time.
  5966. *
  5967. * @return void
  5968. */
  5969. public function testSubmitUnlockedByDefault() {
  5970. $this->Form->request->params['_Token']['key'] = 'secured';
  5971. $this->Form->submit('Go go');
  5972. $this->Form->submit('Save', array('name' => 'save'));
  5973. $result = $this->Form->unlockField();
  5974. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5975. }
  5976. /**
  5977. * Test submit image with timestamps.
  5978. *
  5979. * @return void
  5980. */
  5981. public function testSubmitImageTimestamp() {
  5982. Configure::write('Asset.timestamp', 'force');
  5983. $result = $this->Form->submit('cake.power.gif');
  5984. $expected = array(
  5985. 'div' => array('class' => 'submit'),
  5986. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  5987. '/div'
  5988. );
  5989. $this->assertTags($result, $expected);
  5990. }
  5991. /**
  5992. * test the create() method
  5993. *
  5994. * @return void
  5995. */
  5996. public function testCreate() {
  5997. $result = $this->Form->create('Contact');
  5998. $encoding = strtolower(Configure::read('App.encoding'));
  5999. $expected = array(
  6000. 'form' => array(
  6001. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6002. 'accept-charset' => $encoding
  6003. ),
  6004. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  6005. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6006. '/div'
  6007. );
  6008. $this->assertTags($result, $expected);
  6009. $result = $this->Form->create('Contact', array('type' => 'GET'));
  6010. $expected = array('form' => array(
  6011. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6012. 'accept-charset' => $encoding
  6013. ));
  6014. $this->assertTags($result, $expected);
  6015. $result = $this->Form->create('Contact', array('type' => 'get'));
  6016. $expected = array('form' => array(
  6017. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6018. 'accept-charset' => $encoding
  6019. ));
  6020. $this->assertTags($result, $expected);
  6021. $result = $this->Form->create('Contact', array('type' => 'put'));
  6022. $expected = array(
  6023. 'form' => array(
  6024. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6025. 'accept-charset' => $encoding
  6026. ),
  6027. 'div' => array('style' => 'display:none;'),
  6028. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6029. '/div'
  6030. );
  6031. $this->assertTags($result, $expected);
  6032. $result = $this->Form->create('Contact', array('type' => 'file'));
  6033. $expected = array(
  6034. 'form' => array(
  6035. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6036. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6037. ),
  6038. 'div' => array('style' => 'display:none;'),
  6039. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6040. '/div'
  6041. );
  6042. $this->assertTags($result, $expected);
  6043. $this->Form->request->data['Contact']['id'] = 1;
  6044. $this->Form->request->here = '/contacts/edit/1';
  6045. $this->Form->request['action'] = 'edit';
  6046. $result = $this->Form->create('Contact');
  6047. $expected = array(
  6048. 'form' => array(
  6049. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6050. 'accept-charset' => $encoding
  6051. ),
  6052. 'div' => array('style' => 'display:none;'),
  6053. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6054. '/div'
  6055. );
  6056. $this->assertTags($result, $expected);
  6057. $this->Form->request->data['Contact']['id'] = 1;
  6058. $this->Form->request->here = '/contacts/edit/1';
  6059. $this->Form->request['action'] = 'edit';
  6060. $result = $this->Form->create('Contact', array('type' => 'file'));
  6061. $expected = array(
  6062. 'form' => array(
  6063. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6064. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6065. ),
  6066. 'div' => array('style' => 'display:none;'),
  6067. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6068. '/div'
  6069. );
  6070. $this->assertTags($result, $expected);
  6071. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  6072. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  6073. $expected = array(
  6074. 'form' => array(
  6075. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  6076. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  6077. ),
  6078. 'div' => array('style' => 'display:none;'),
  6079. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6080. '/div'
  6081. );
  6082. $this->assertTags($result, $expected);
  6083. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  6084. $expected = array(
  6085. 'form' => array(
  6086. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  6087. 'accept-charset' => $encoding
  6088. ),
  6089. 'div' => array('style' => 'display:none;'),
  6090. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6091. '/div'
  6092. );
  6093. $this->assertTags($result, $expected);
  6094. $this->Form->request['action'] = 'add';
  6095. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  6096. $expected = array(
  6097. 'form' => array(
  6098. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  6099. 'accept-charset' => $encoding
  6100. ),
  6101. 'div' => array('style' => 'display:none;'),
  6102. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6103. '/div'
  6104. );
  6105. $this->assertTags($result, $expected);
  6106. $result = $this->Form->create('User', array('action' => 'login'));
  6107. $expected = array(
  6108. 'form' => array(
  6109. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  6110. 'accept-charset' => $encoding
  6111. ),
  6112. 'div' => array('style' => 'display:none;'),
  6113. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6114. '/div'
  6115. );
  6116. $this->assertTags($result, $expected);
  6117. $result = $this->Form->create('User', array('url' => '/users/login'));
  6118. $expected = array(
  6119. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  6120. 'div' => array('style' => 'display:none;'),
  6121. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6122. '/div'
  6123. );
  6124. $this->assertTags($result, $expected);
  6125. $this->Form->request['controller'] = 'pages';
  6126. $result = $this->Form->create('User', array('action' => 'signup'));
  6127. $expected = array(
  6128. 'form' => array(
  6129. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  6130. 'accept-charset' => $encoding
  6131. ),
  6132. 'div' => array('style' => 'display:none;'),
  6133. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6134. '/div'
  6135. );
  6136. $this->assertTags($result, $expected);
  6137. $this->Form->request->data = array();
  6138. $this->Form->request['controller'] = 'contacts';
  6139. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6140. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  6141. $expected = array(
  6142. 'form' => array(
  6143. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  6144. 'accept-charset' => 'utf-8'
  6145. ),
  6146. 'div' => array('style' => 'display:none;'),
  6147. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6148. '/div'
  6149. );
  6150. $this->assertTags($result, $expected);
  6151. }
  6152. /**
  6153. * Test the onsubmit option for create()
  6154. *
  6155. * @return void
  6156. */
  6157. public function testCreateOnSubmit() {
  6158. $this->Form->request->data = array();
  6159. $this->Form->request['controller'] = 'contacts';
  6160. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6161. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  6162. $expected = array(
  6163. 'form' => array(
  6164. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  6165. 'accept-charset' => 'utf-8'
  6166. ),
  6167. 'div' => array('style' => 'display:none;'),
  6168. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6169. '/div'
  6170. );
  6171. $this->assertTags($result, $expected);
  6172. $this->Form->request->data = array();
  6173. $this->Form->request['controller'] = 'contacts';
  6174. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6175. $result = $this->Form->create(array(
  6176. 'url' => array('action' => 'index', 'param'),
  6177. 'default' => false,
  6178. 'onsubmit' => 'someFunction();'
  6179. ));
  6180. $expected = array(
  6181. 'form' => array(
  6182. 'id' => 'ContactAddForm', 'method' => 'post',
  6183. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6184. 'action' => '/contacts/index/param',
  6185. 'accept-charset' => 'utf-8'
  6186. ),
  6187. 'div' => array('style' => 'display:none;'),
  6188. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6189. '/div'
  6190. );
  6191. $this->assertTags($result, $expected);
  6192. }
  6193. /**
  6194. * test create() with automatic url generation
  6195. *
  6196. * @return void
  6197. */
  6198. public function testCreateAutoUrl() {
  6199. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6200. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6201. $this->Form->request->base = '/base_url';
  6202. $result = $this->Form->create('Contact');
  6203. $expected = array(
  6204. 'form' => array(
  6205. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6206. 'accept-charset' => 'utf-8'
  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. $this->Form->request['action'] = 'delete';
  6214. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6215. $this->Form->request->base = '/base_url';
  6216. $result = $this->Form->create('Contact');
  6217. $expected = array(
  6218. 'form' => array(
  6219. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6220. 'accept-charset' => 'utf-8'
  6221. ),
  6222. 'div' => array('style' => 'display:none;'),
  6223. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6224. '/div'
  6225. );
  6226. $this->assertTags($result, $expected);
  6227. }
  6228. /**
  6229. * test create() with a custom route
  6230. *
  6231. * @return void
  6232. */
  6233. public function testCreateCustomRoute() {
  6234. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6235. $encoding = strtolower(Configure::read('App.encoding'));
  6236. $result = $this->Form->create('User', array('action' => 'login'));
  6237. $expected = array(
  6238. 'form' => array(
  6239. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6240. 'accept-charset' => $encoding
  6241. ),
  6242. 'div' => array('style' => 'display:none;'),
  6243. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6244. '/div'
  6245. );
  6246. $this->assertTags($result, $expected);
  6247. }
  6248. /**
  6249. * test that inputDefaults are stored and used.
  6250. *
  6251. * @return void
  6252. */
  6253. public function testCreateWithInputDefaults() {
  6254. $this->Form->create('User', array(
  6255. 'inputDefaults' => array(
  6256. 'div' => false,
  6257. 'label' => false,
  6258. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6259. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6260. )
  6261. ));
  6262. $result = $this->Form->input('username');
  6263. $expected = array(
  6264. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6265. );
  6266. $this->assertTags($result, $expected);
  6267. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6268. $expected = array(
  6269. 'div' => array('class' => 'input text'),
  6270. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6271. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6272. '/div'
  6273. );
  6274. $this->assertTags($result, $expected);
  6275. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  6276. $expected = array(
  6277. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6278. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  6279. );
  6280. $this->assertTags($result, $expected);
  6281. $this->Form->create('User', array(
  6282. 'inputDefaults' => array(
  6283. 'div' => false,
  6284. 'label' => array('class' => 'nice', 'for' => 'changed'),
  6285. )
  6286. ));
  6287. $result = $this->Form->input('username', array('div' => true));
  6288. $expected = array(
  6289. 'div' => array('class' => 'input text'),
  6290. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  6291. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6292. '/div'
  6293. );
  6294. $this->assertTags($result, $expected);
  6295. }
  6296. /**
  6297. * test automatic accept-charset overriding
  6298. *
  6299. * @return void
  6300. */
  6301. public function testCreateWithAcceptCharset() {
  6302. $result = $this->Form->create('UserForm', array(
  6303. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  6304. )
  6305. );
  6306. $expected = array(
  6307. 'form' => array(
  6308. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6309. 'accept-charset' => 'iso-8859-1'
  6310. ),
  6311. 'div' => array('style' => 'display:none;'),
  6312. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6313. '/div'
  6314. );
  6315. $this->assertTags($result, $expected);
  6316. }
  6317. /**
  6318. * Test base form url when url param is passed with multiple parameters (&)
  6319. *
  6320. */
  6321. public function testCreateQuerystringrequest() {
  6322. $encoding = strtolower(Configure::read('App.encoding'));
  6323. $result = $this->Form->create('Contact', array(
  6324. 'type' => 'post',
  6325. 'escape' => false,
  6326. 'url' => array(
  6327. 'controller' => 'controller',
  6328. 'action' => 'action',
  6329. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6330. )
  6331. ));
  6332. $expected = array(
  6333. 'form' => array(
  6334. 'id' => 'ContactAddForm',
  6335. 'method' => 'post',
  6336. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6337. 'accept-charset' => $encoding
  6338. ),
  6339. 'div' => array('style' => 'display:none;'),
  6340. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6341. '/div'
  6342. );
  6343. $this->assertTags($result, $expected);
  6344. $result = $this->Form->create('Contact', array(
  6345. 'type' => 'post',
  6346. 'url' => array(
  6347. 'controller' => 'controller',
  6348. 'action' => 'action',
  6349. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6350. )
  6351. ));
  6352. $expected = array(
  6353. 'form' => array(
  6354. 'id' => 'ContactAddForm',
  6355. 'method' => 'post',
  6356. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6357. 'accept-charset' => $encoding
  6358. ),
  6359. 'div' => array('style' => 'display:none;'),
  6360. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6361. '/div'
  6362. );
  6363. $this->assertTags($result, $expected);
  6364. }
  6365. /**
  6366. * test that create() doesn't cause errors by multiple id's being in the primary key
  6367. * as could happen with multiple select or checkboxes.
  6368. *
  6369. * @return void
  6370. */
  6371. public function testCreateWithMultipleIdInData() {
  6372. $encoding = strtolower(Configure::read('App.encoding'));
  6373. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6374. $result = $this->Form->create('Contact');
  6375. $expected = array(
  6376. 'form' => array(
  6377. 'id' => 'ContactAddForm',
  6378. 'method' => 'post',
  6379. 'action' => '/contacts/add',
  6380. 'accept-charset' => $encoding
  6381. ),
  6382. 'div' => array('style' => 'display:none;'),
  6383. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6384. '/div'
  6385. );
  6386. $this->assertTags($result, $expected);
  6387. }
  6388. /**
  6389. * test that create() doesn't add in extra passed params.
  6390. *
  6391. * @return void
  6392. */
  6393. public function testCreatePassedArgs() {
  6394. $encoding = strtolower(Configure::read('App.encoding'));
  6395. $this->Form->request->data['Contact']['id'] = 1;
  6396. $result = $this->Form->create('Contact', array(
  6397. 'type' => 'post',
  6398. 'escape' => false,
  6399. 'url' => array(
  6400. 'action' => 'edit',
  6401. 'myparam'
  6402. )
  6403. ));
  6404. $expected = array(
  6405. 'form' => array(
  6406. 'id' => 'ContactAddForm',
  6407. 'method' => 'post',
  6408. 'action' => '/contacts/edit/myparam',
  6409. 'accept-charset' => $encoding
  6410. ),
  6411. 'div' => array('style' => 'display:none;'),
  6412. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6413. '/div'
  6414. );
  6415. $this->assertTags($result, $expected);
  6416. }
  6417. /**
  6418. * test creating a get form, and get form inputs.
  6419. *
  6420. * @return void
  6421. */
  6422. public function testGetFormCreate() {
  6423. $encoding = strtolower(Configure::read('App.encoding'));
  6424. $result = $this->Form->create('Contact', array('type' => 'get'));
  6425. $this->assertTags($result, array('form' => array(
  6426. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6427. 'accept-charset' => $encoding
  6428. )));
  6429. $result = $this->Form->text('Contact.name');
  6430. $this->assertTags($result, array('input' => array(
  6431. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6432. )));
  6433. $result = $this->Form->password('password');
  6434. $this->assertTags($result, array('input' => array(
  6435. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6436. )));
  6437. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6438. $result = $this->Form->text('user_form');
  6439. $this->assertTags($result, array('input' => array(
  6440. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6441. )));
  6442. }
  6443. /**
  6444. * test get form, and inputs when the model param is false
  6445. *
  6446. * @return void
  6447. */
  6448. public function testGetFormWithFalseModel() {
  6449. $encoding = strtolower(Configure::read('App.encoding'));
  6450. $this->Form->request['controller'] = 'contact_test';
  6451. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6452. $expected = array('form' => array(
  6453. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6454. 'accept-charset' => $encoding
  6455. ));
  6456. $this->assertTags($result, $expected);
  6457. $result = $this->Form->text('reason');
  6458. $expected = array(
  6459. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6460. );
  6461. $this->assertTags($result, $expected);
  6462. }
  6463. /**
  6464. * test that datetime() works with GET style forms.
  6465. *
  6466. * @return void
  6467. */
  6468. public function testDateTimeWithGetForms() {
  6469. extract($this->dateRegex);
  6470. $this->Form->create('Contact', array('type' => 'get'));
  6471. $result = $this->Form->datetime('created');
  6472. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6473. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6474. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6475. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6476. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6477. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6478. }
  6479. /**
  6480. * testEditFormWithData method
  6481. *
  6482. * test auto populating form elements from submitted data.
  6483. *
  6484. * @return void
  6485. */
  6486. public function testEditFormWithData() {
  6487. $this->Form->request->data = array('Person' => array(
  6488. 'id' => 1,
  6489. 'first_name' => 'Nate',
  6490. 'last_name' => 'Abele',
  6491. 'email' => 'nate@example.com'
  6492. ));
  6493. $this->Form->request->addParams(array(
  6494. 'models' => array('Person'),
  6495. 'controller' => 'people',
  6496. 'action' => 'add'
  6497. ));
  6498. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6499. $this->Form->create();
  6500. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6501. $expected = array(
  6502. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6503. 'select' => array(
  6504. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6505. ),
  6506. array('option' => array('value' => 1)), 'Nate', '/option',
  6507. array('option' => array('value' => 2)), 'Garrett', '/option',
  6508. array('option' => array('value' => 3)), 'Larry', '/option',
  6509. '/select'
  6510. );
  6511. $this->assertTags($result, $expected);
  6512. }
  6513. /**
  6514. * Test that required fields are created for various types of validation.
  6515. *
  6516. * @return void
  6517. */
  6518. public function testFormInputRequiredDetection() {
  6519. $this->Form->create('Contact');
  6520. $result = $this->Form->input('Contact.non_existing');
  6521. $expected = array(
  6522. 'div' => array('class' => 'input text'),
  6523. 'label' => array('for' => 'ContactNonExisting'),
  6524. 'Non Existing',
  6525. '/label',
  6526. 'input' => array(
  6527. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  6528. 'id' => 'ContactNonExisting'
  6529. ),
  6530. '/div'
  6531. );
  6532. $this->assertTags($result, $expected);
  6533. $result = $this->Form->input('Contact.imrequired');
  6534. $expected = array(
  6535. 'div' => array('class' => 'input text required'),
  6536. 'label' => array('for' => 'ContactImrequired'),
  6537. 'Imrequired',
  6538. '/label',
  6539. 'input' => array(
  6540. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  6541. 'id' => 'ContactImrequired'
  6542. ),
  6543. '/div'
  6544. );
  6545. $this->assertTags($result, $expected);
  6546. $result = $this->Form->input('Contact.imalsorequired');
  6547. $expected = array(
  6548. 'div' => array('class' => 'input text required'),
  6549. 'label' => array('for' => 'ContactImalsorequired'),
  6550. 'Imalsorequired',
  6551. '/label',
  6552. 'input' => array(
  6553. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  6554. 'id' => 'ContactImalsorequired'
  6555. ),
  6556. '/div'
  6557. );
  6558. $this->assertTags($result, $expected);
  6559. $result = $this->Form->input('Contact.imrequiredtoo');
  6560. $expected = array(
  6561. 'div' => array('class' => 'input text required'),
  6562. 'label' => array('for' => 'ContactImrequiredtoo'),
  6563. 'Imrequiredtoo',
  6564. '/label',
  6565. 'input' => array(
  6566. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  6567. 'id' => 'ContactImrequiredtoo'
  6568. ),
  6569. '/div'
  6570. );
  6571. $this->assertTags($result, $expected);
  6572. $result = $this->Form->input('Contact.required_one');
  6573. $expected = array(
  6574. 'div' => array('class' => 'input text required'),
  6575. 'label' => array('for' => 'ContactRequiredOne'),
  6576. 'Required One',
  6577. '/label',
  6578. 'input' => array(
  6579. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  6580. 'id' => 'ContactRequiredOne'
  6581. ),
  6582. '/div'
  6583. );
  6584. $this->assertTags($result, $expected);
  6585. $result = $this->Form->input('Contact.string_required');
  6586. $expected = array(
  6587. 'div' => array('class' => 'input text required'),
  6588. 'label' => array('for' => 'ContactStringRequired'),
  6589. 'String Required',
  6590. '/label',
  6591. 'input' => array(
  6592. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  6593. 'id' => 'ContactStringRequired'
  6594. ),
  6595. '/div'
  6596. );
  6597. $this->assertTags($result, $expected);
  6598. $result = $this->Form->input('Contact.imnotrequired');
  6599. $expected = array(
  6600. 'div' => array('class' => 'input text'),
  6601. 'label' => array('for' => 'ContactImnotrequired'),
  6602. 'Imnotrequired',
  6603. '/label',
  6604. 'input' => array(
  6605. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  6606. 'id' => 'ContactImnotrequired'
  6607. ),
  6608. '/div'
  6609. );
  6610. $this->assertTags($result, $expected);
  6611. $result = $this->Form->input('Contact.imalsonotrequired');
  6612. $expected = array(
  6613. 'div' => array('class' => 'input text'),
  6614. 'label' => array('for' => 'ContactImalsonotrequired'),
  6615. 'Imalsonotrequired',
  6616. '/label',
  6617. 'input' => array(
  6618. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  6619. 'id' => 'ContactImalsonotrequired'
  6620. ),
  6621. '/div'
  6622. );
  6623. $this->assertTags($result, $expected);
  6624. $result = $this->Form->input('Contact.imalsonotrequired2');
  6625. $expected = array(
  6626. 'div' => array('class' => 'input text'),
  6627. 'label' => array('for' => 'ContactImalsonotrequired2'),
  6628. 'Imalsonotrequired2',
  6629. '/label',
  6630. 'input' => array(
  6631. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  6632. 'id' => 'ContactImalsonotrequired2'
  6633. ),
  6634. '/div'
  6635. );
  6636. $this->assertTags($result, $expected);
  6637. $result = $this->Form->input('Contact.imnotrequiredeither');
  6638. $expected = array(
  6639. 'div' => array('class' => 'input text'),
  6640. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6641. 'Imnotrequiredeither',
  6642. '/label',
  6643. 'input' => array(
  6644. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  6645. 'id' => 'ContactImnotrequiredeither'
  6646. ),
  6647. '/div'
  6648. );
  6649. $this->assertTags($result, $expected);
  6650. $result = $this->Form->input('Contact.iamrequiredalways');
  6651. $expected = array(
  6652. 'div' => array('class' => 'input text required'),
  6653. 'label' => array('for' => 'ContactIamrequiredalways'),
  6654. 'Iamrequiredalways',
  6655. '/label',
  6656. 'input' => array(
  6657. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  6658. 'id' => 'ContactIamrequiredalways'
  6659. ),
  6660. '/div'
  6661. );
  6662. $this->assertTags($result, $expected);
  6663. }
  6664. /**
  6665. * testFormMagicInput method
  6666. *
  6667. * @return void
  6668. */
  6669. public function testFormMagicInput() {
  6670. $encoding = strtolower(Configure::read('App.encoding'));
  6671. $result = $this->Form->create('Contact');
  6672. $expected = array(
  6673. 'form' => array(
  6674. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6675. 'accept-charset' => $encoding
  6676. ),
  6677. 'div' => array('style' => 'display:none;'),
  6678. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6679. '/div'
  6680. );
  6681. $this->assertTags($result, $expected);
  6682. $result = $this->Form->input('name');
  6683. $expected = array(
  6684. 'div' => array('class' => 'input text'),
  6685. 'label' => array('for' => 'ContactName'),
  6686. 'Name',
  6687. '/label',
  6688. 'input' => array(
  6689. 'type' => 'text', 'name' => 'data[Contact][name]',
  6690. 'id' => 'ContactName', 'maxlength' => '255'
  6691. ),
  6692. '/div'
  6693. );
  6694. $this->assertTags($result, $expected);
  6695. $result = $this->Form->input('non_existing_field_in_contact_model');
  6696. $expected = array(
  6697. 'div' => array('class' => 'input text'),
  6698. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  6699. 'Non Existing Field In Contact Model',
  6700. '/label',
  6701. 'input' => array(
  6702. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  6703. 'id' => 'ContactNonExistingFieldInContactModel'
  6704. ),
  6705. '/div'
  6706. );
  6707. $this->assertTags($result, $expected);
  6708. $result = $this->Form->input('Address.street');
  6709. $expected = array(
  6710. 'div' => array('class' => 'input text'),
  6711. 'label' => array('for' => 'AddressStreet'),
  6712. 'Street',
  6713. '/label',
  6714. 'input' => array(
  6715. 'type' => 'text', 'name' => 'data[Address][street]',
  6716. 'id' => 'AddressStreet'
  6717. ),
  6718. '/div'
  6719. );
  6720. $this->assertTags($result, $expected);
  6721. $result = $this->Form->input('Address.non_existing_field_in_model');
  6722. $expected = array(
  6723. 'div' => array('class' => 'input text'),
  6724. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  6725. 'Non Existing Field In Model',
  6726. '/label',
  6727. 'input' => array(
  6728. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  6729. 'id' => 'AddressNonExistingFieldInModel'
  6730. ),
  6731. '/div'
  6732. );
  6733. $this->assertTags($result, $expected);
  6734. $result = $this->Form->input('name', array('div' => false));
  6735. $expected = array(
  6736. 'label' => array('for' => 'ContactName'),
  6737. 'Name',
  6738. '/label',
  6739. 'input' => array(
  6740. 'type' => 'text', 'name' => 'data[Contact][name]',
  6741. 'id' => 'ContactName', 'maxlength' => '255'
  6742. )
  6743. );
  6744. $this->assertTags($result, $expected);
  6745. extract($this->dateRegex);
  6746. $now = strtotime('now');
  6747. $result = $this->Form->input('Contact.published', array('div' => false));
  6748. $expected = array(
  6749. 'label' => array('for' => 'ContactPublishedMonth'),
  6750. 'Published',
  6751. '/label',
  6752. array('select' => array(
  6753. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  6754. )),
  6755. $monthsRegex,
  6756. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6757. date('F', $now),
  6758. '/option',
  6759. '*/select',
  6760. '-',
  6761. array('select' => array(
  6762. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  6763. )),
  6764. $daysRegex,
  6765. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6766. date('j', $now),
  6767. '/option',
  6768. '*/select',
  6769. '-',
  6770. array('select' => array(
  6771. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  6772. )),
  6773. $yearsRegex,
  6774. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6775. date('Y', $now),
  6776. '*/select'
  6777. );
  6778. $this->assertTags($result, $expected);
  6779. $result = $this->Form->input('Contact.updated', array('div' => false));
  6780. $expected = array(
  6781. 'label' => array('for' => 'ContactUpdatedMonth'),
  6782. 'Updated',
  6783. '/label',
  6784. array('select' => array(
  6785. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  6786. )),
  6787. $monthsRegex,
  6788. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6789. date('F', $now),
  6790. '/option',
  6791. '*/select',
  6792. '-',
  6793. array('select' => array(
  6794. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  6795. )),
  6796. $daysRegex,
  6797. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6798. date('j', $now),
  6799. '/option',
  6800. '*/select',
  6801. '-',
  6802. array('select' => array(
  6803. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  6804. )),
  6805. $yearsRegex,
  6806. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6807. date('Y', $now),
  6808. '*/select'
  6809. );
  6810. $this->assertTags($result, $expected);
  6811. $result = $this->Form->input('UserForm.stuff');
  6812. $expected = array(
  6813. 'div' => array('class' => 'input text'),
  6814. 'label' => array('for' => 'UserFormStuff'),
  6815. 'Stuff',
  6816. '/label',
  6817. 'input' => array(
  6818. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  6819. 'id' => 'UserFormStuff', 'maxlength' => 10
  6820. ),
  6821. '/div'
  6822. );
  6823. $this->assertTags($result, $expected);
  6824. }
  6825. /**
  6826. * testForMagicInputNonExistingNorValidated method
  6827. *
  6828. * @return void
  6829. */
  6830. public function testForMagicInputNonExistingNorValidated() {
  6831. $encoding = strtolower(Configure::read('App.encoding'));
  6832. $result = $this->Form->create('Contact');
  6833. $expected = array(
  6834. 'form' => array(
  6835. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6836. 'accept-charset' => $encoding
  6837. ),
  6838. 'div' => array('style' => 'display:none;'),
  6839. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6840. '/div'
  6841. );
  6842. $this->assertTags($result, $expected);
  6843. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6844. $expected = array(
  6845. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6846. 'Non Existing Nor Validated',
  6847. '/label',
  6848. 'input' => array(
  6849. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6850. 'id' => 'ContactNonExistingNorValidated'
  6851. )
  6852. );
  6853. $this->assertTags($result, $expected);
  6854. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6855. 'div' => false, 'value' => 'my value'
  6856. ));
  6857. $expected = array(
  6858. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6859. 'Non Existing Nor Validated',
  6860. '/label',
  6861. 'input' => array(
  6862. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6863. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6864. )
  6865. );
  6866. $this->assertTags($result, $expected);
  6867. $this->Form->request->data = array(
  6868. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6869. ));
  6870. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6871. $expected = array(
  6872. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6873. 'Non Existing Nor Validated',
  6874. '/label',
  6875. 'input' => array(
  6876. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6877. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6878. )
  6879. );
  6880. $this->assertTags($result, $expected);
  6881. }
  6882. /**
  6883. * testFormMagicInputLabel method
  6884. *
  6885. * @return void
  6886. */
  6887. public function testFormMagicInputLabel() {
  6888. $encoding = strtolower(Configure::read('App.encoding'));
  6889. $result = $this->Form->create('Contact');
  6890. $expected = array(
  6891. 'form' => array(
  6892. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6893. 'accept-charset' => $encoding
  6894. ),
  6895. 'div' => array('style' => 'display:none;'),
  6896. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6897. '/div'
  6898. );
  6899. $this->assertTags($result, $expected);
  6900. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  6901. $this->assertTags($result, array('input' => array(
  6902. 'name' => 'data[Contact][name]', 'type' => 'text',
  6903. 'id' => 'ContactName', 'maxlength' => '255')
  6904. ));
  6905. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  6906. $expected = array(
  6907. 'label' => array('for' => 'ContactName'),
  6908. 'My label',
  6909. '/label',
  6910. 'input' => array(
  6911. 'type' => 'text', 'name' => 'data[Contact][name]',
  6912. 'id' => 'ContactName', 'maxlength' => '255'
  6913. )
  6914. );
  6915. $this->assertTags($result, $expected);
  6916. $result = $this->Form->input('Contact.name', array(
  6917. 'div' => false, 'label' => array('class' => 'mandatory')
  6918. ));
  6919. $expected = array(
  6920. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6921. 'Name',
  6922. '/label',
  6923. 'input' => array(
  6924. 'type' => 'text', 'name' => 'data[Contact][name]',
  6925. 'id' => 'ContactName', 'maxlength' => '255'
  6926. )
  6927. );
  6928. $this->assertTags($result, $expected);
  6929. $result = $this->Form->input('Contact.name', array(
  6930. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  6931. ));
  6932. $expected = array(
  6933. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6934. 'My label',
  6935. '/label',
  6936. 'input' => array(
  6937. 'type' => 'text', 'name' => 'data[Contact][name]',
  6938. 'id' => 'ContactName', 'maxlength' => '255'
  6939. )
  6940. );
  6941. $this->assertTags($result, $expected);
  6942. $result = $this->Form->input('Contact.name', array(
  6943. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  6944. ));
  6945. $expected = array(
  6946. 'label' => array('for' => 'my_id'),
  6947. 'Name',
  6948. '/label',
  6949. 'input' => array(
  6950. 'type' => 'text', 'name' => 'data[Contact][name]',
  6951. 'id' => 'my_id', 'maxlength' => '255'
  6952. )
  6953. );
  6954. $this->assertTags($result, $expected);
  6955. $result = $this->Form->input('1.id');
  6956. $this->assertTags($result, array('input' => array(
  6957. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6958. 'id' => 'Contact1Id'
  6959. )));
  6960. $result = $this->Form->input("1.name");
  6961. $expected = array(
  6962. 'div' => array('class' => 'input text'),
  6963. 'label' => array('for' => 'Contact1Name'),
  6964. 'Name',
  6965. '/label',
  6966. 'input' => array(
  6967. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  6968. 'id' => 'Contact1Name', 'maxlength' => '255'
  6969. ),
  6970. '/div'
  6971. );
  6972. $this->assertTags($result, $expected);
  6973. $result = $this->Form->input('Contact.1.id');
  6974. $this->assertTags($result, array(
  6975. 'input' => array(
  6976. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6977. 'id' => 'Contact1Id'
  6978. )
  6979. ));
  6980. $result = $this->Form->input("Model.1.name");
  6981. $expected = array(
  6982. 'div' => array('class' => 'input text'),
  6983. 'label' => array('for' => 'Model1Name'),
  6984. 'Name',
  6985. '/label',
  6986. 'input' => array(
  6987. 'type' => 'text', 'name' => 'data[Model][1][name]',
  6988. 'id' => 'Model1Name'
  6989. ),
  6990. '/div'
  6991. );
  6992. $this->assertTags($result, $expected);
  6993. }
  6994. /**
  6995. * testFormEnd method
  6996. *
  6997. * @return void
  6998. */
  6999. public function testFormEnd() {
  7000. $this->assertEquals('</form>', $this->Form->end());
  7001. $result = $this->Form->end('');
  7002. $expected = array(
  7003. 'div' => array('class' => 'submit'),
  7004. 'input' => array('type' => 'submit', 'value' => ''),
  7005. '/div',
  7006. '/form'
  7007. );
  7008. $this->assertTags($result, $expected);
  7009. $result = $this->Form->end(array('label' => ''));
  7010. $expected = array(
  7011. 'div' => array('class' => 'submit'),
  7012. 'input' => array('type' => 'submit', 'value' => ''),
  7013. '/div',
  7014. '/form'
  7015. );
  7016. $this->assertTags($result, $expected);
  7017. $result = $this->Form->end('save');
  7018. $expected = array(
  7019. 'div' => array('class' => 'submit'),
  7020. 'input' => array('type' => 'submit', 'value' => 'save'),
  7021. '/div',
  7022. '/form'
  7023. );
  7024. $this->assertTags($result, $expected);
  7025. $result = $this->Form->end(array('label' => 'save'));
  7026. $expected = array(
  7027. 'div' => array('class' => 'submit'),
  7028. 'input' => array('type' => 'submit', 'value' => 'save'),
  7029. '/div',
  7030. '/form'
  7031. );
  7032. $this->assertTags($result, $expected);
  7033. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  7034. $expected = array(
  7035. 'div' => array('class' => 'submit'),
  7036. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7037. '/div',
  7038. '/form'
  7039. );
  7040. $this->assertTags($result, $expected);
  7041. $result = $this->Form->end(array('name' => 'Whatever'));
  7042. $expected = array(
  7043. 'div' => array('class' => 'submit'),
  7044. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  7045. '/div',
  7046. '/form'
  7047. );
  7048. $this->assertTags($result, $expected);
  7049. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  7050. $expected = array(
  7051. 'div' => array('class' => 'good'),
  7052. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7053. '/div',
  7054. '/form'
  7055. );
  7056. $this->assertTags($result, $expected);
  7057. $result = $this->Form->end(array(
  7058. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  7059. ));
  7060. $expected = array(
  7061. 'div' => array('class' => 'good'),
  7062. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7063. '/div',
  7064. '/form'
  7065. );
  7066. $this->assertTags($result, $expected);
  7067. }
  7068. /**
  7069. * testMultipleFormWithIdFields method
  7070. *
  7071. * @return void
  7072. */
  7073. public function testMultipleFormWithIdFields() {
  7074. $this->Form->create('UserForm');
  7075. $result = $this->Form->input('id');
  7076. $this->assertTags($result, array('input' => array(
  7077. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  7078. )));
  7079. $result = $this->Form->input('ValidateItem.id');
  7080. $this->assertTags($result, array('input' => array(
  7081. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  7082. 'id' => 'ValidateItemId'
  7083. )));
  7084. $result = $this->Form->input('ValidateUser.id');
  7085. $this->assertTags($result, array('input' => array(
  7086. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  7087. 'id' => 'ValidateUserId'
  7088. )));
  7089. }
  7090. /**
  7091. * testDbLessModel method
  7092. *
  7093. * @return void
  7094. */
  7095. public function testDbLessModel() {
  7096. $this->Form->create('TestMail');
  7097. $result = $this->Form->input('name');
  7098. $expected = array(
  7099. 'div' => array('class' => 'input text'),
  7100. 'label' => array('for' => 'TestMailName'),
  7101. 'Name',
  7102. '/label',
  7103. 'input' => array(
  7104. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7105. 'id' => 'TestMailName'
  7106. ),
  7107. '/div'
  7108. );
  7109. $this->assertTags($result, $expected);
  7110. ClassRegistry::init('TestMail');
  7111. $this->Form->create('TestMail');
  7112. $result = $this->Form->input('name');
  7113. $expected = array(
  7114. 'div' => array('class' => 'input text'),
  7115. 'label' => array('for' => 'TestMailName'),
  7116. 'Name',
  7117. '/label',
  7118. 'input' => array(
  7119. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7120. 'id' => 'TestMailName'
  7121. ),
  7122. '/div'
  7123. );
  7124. $this->assertTags($result, $expected);
  7125. }
  7126. /**
  7127. * testBrokenness method
  7128. *
  7129. * @return void
  7130. */
  7131. public function testBrokenness() {
  7132. /*
  7133. * #4 This test has two parents and four children. By default (as of r7117) both
  7134. * parents are show but the first parent is missing a child. This is the inconsistency
  7135. * in the default behaviour - one parent has all children, the other does not - dependent
  7136. * on the data values.
  7137. */
  7138. $result = $this->Form->select('Model.field', array(
  7139. 'Fred' => array(
  7140. 'freds_son_1' => 'Fred',
  7141. 'freds_son_2' => 'Freddie'
  7142. ),
  7143. 'Bert' => array(
  7144. 'berts_son_1' => 'Albert',
  7145. 'berts_son_2' => 'Bertie')
  7146. ),
  7147. array('showParents' => true, 'empty' => false)
  7148. );
  7149. $expected = array(
  7150. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7151. array('optgroup' => array('label' => 'Fred')),
  7152. array('option' => array('value' => 'freds_son_1')),
  7153. 'Fred',
  7154. '/option',
  7155. array('option' => array('value' => 'freds_son_2')),
  7156. 'Freddie',
  7157. '/option',
  7158. '/optgroup',
  7159. array('optgroup' => array('label' => 'Bert')),
  7160. array('option' => array('value' => 'berts_son_1')),
  7161. 'Albert',
  7162. '/option',
  7163. array('option' => array('value' => 'berts_son_2')),
  7164. 'Bertie',
  7165. '/option',
  7166. '/optgroup',
  7167. '/select'
  7168. );
  7169. $this->assertTags($result, $expected);
  7170. /*
  7171. * #2 This is structurally identical to the test above (#1) - only the parent name has
  7172. * changed, so we should expect the same select list data, just with a different name
  7173. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  7174. * This is where data corruption can occur, because when a select value is missing from
  7175. * a list a form will substitute the first value in the list - without the user knowing.
  7176. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  7177. * affect the availability of 3 => 'Three' as a valid option.
  7178. */
  7179. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  7180. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  7181. ));
  7182. $result = $this->Form->select(
  7183. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  7184. );
  7185. $expected = array(
  7186. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7187. array('option' => array('value' => 1)),
  7188. 'One',
  7189. '/option',
  7190. array('option' => array('value' => 2)),
  7191. 'Two',
  7192. '/option',
  7193. array('optgroup' => array('label' => 'Three')),
  7194. array('option' => array('value' => 3)),
  7195. 'Three',
  7196. '/option',
  7197. array('option' => array('value' => 4)),
  7198. 'Four',
  7199. '/option',
  7200. array('option' => array('value' => 5)),
  7201. 'Five',
  7202. '/option',
  7203. '/optgroup',
  7204. '/select'
  7205. );
  7206. $this->assertTags($result, $expected);
  7207. }
  7208. /**
  7209. * Test the generation of fields for a multi record form.
  7210. *
  7211. * @return void
  7212. */
  7213. public function testMultiRecordForm() {
  7214. $this->Form->create('ValidateProfile');
  7215. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  7216. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  7217. $expected = array(
  7218. 'div' => array('class' => 'input textarea'),
  7219. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  7220. 'Name',
  7221. '/label',
  7222. 'textarea' => array(
  7223. 'id' => 'ValidateProfile1ValidateItem2Name',
  7224. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  7225. 'cols' => 30,
  7226. 'rows' => 6
  7227. ),
  7228. 'Value',
  7229. '/textarea',
  7230. '/div'
  7231. );
  7232. $this->assertTags($result, $expected);
  7233. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
  7234. $expected = array(
  7235. 'div' => array('class' => 'input date'),
  7236. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7237. 'Created',
  7238. '/label',
  7239. array('select' => array(
  7240. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  7241. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7242. )
  7243. ),
  7244. array('option' => array('value' => '')), '/option',
  7245. $this->dateRegex['monthsRegex'],
  7246. '/select', '-',
  7247. array('select' => array(
  7248. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  7249. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7250. )
  7251. ),
  7252. array('option' => array('value' => '')), '/option',
  7253. $this->dateRegex['daysRegex'],
  7254. '/select', '-',
  7255. array('select' => array(
  7256. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  7257. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7258. )
  7259. ),
  7260. array('option' => array('value' => '')), '/option',
  7261. $this->dateRegex['yearsRegex'],
  7262. '/select',
  7263. '/div'
  7264. );
  7265. $this->assertTags($result, $expected);
  7266. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7267. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7268. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7269. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7270. $expected = array(
  7271. 'div' => array('class' => 'input select error'),
  7272. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7273. 'Profile',
  7274. '/label',
  7275. 'select' => array(
  7276. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7277. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7278. 'class' => 'form-error'
  7279. ),
  7280. '/select',
  7281. array('div' => array('class' => 'error-message')),
  7282. 'Error',
  7283. '/div',
  7284. '/div'
  7285. );
  7286. $this->assertTags($result, $expected);
  7287. }
  7288. /**
  7289. * test the correct display of multi-record form validation errors.
  7290. *
  7291. * @return void
  7292. */
  7293. public function testMultiRecordFormValidationErrors() {
  7294. $this->Form->create('ValidateProfile');
  7295. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7296. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7297. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7298. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7299. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7300. $result = $this->Form->error('ValidateProfile.2.city');
  7301. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7302. $result = $this->Form->error('2.city');
  7303. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7304. }
  7305. /**
  7306. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7307. *
  7308. * @return void
  7309. */
  7310. public function testInputTemplate() {
  7311. $result = $this->Form->input('Contact.email', array(
  7312. 'type' => 'text', 'format' => array('input')
  7313. ));
  7314. $expected = array(
  7315. 'div' => array('class' => 'input text'),
  7316. 'input' => array(
  7317. 'type' => 'text', 'name' => 'data[Contact][email]',
  7318. 'id' => 'ContactEmail'
  7319. ),
  7320. '/div'
  7321. );
  7322. $this->assertTags($result, $expected);
  7323. $result = $this->Form->input('Contact.email', array(
  7324. 'type' => 'text', 'format' => array('input', 'label'),
  7325. 'label' => '<em>Email (required)</em>'
  7326. ));
  7327. $expected = array(
  7328. 'div' => array('class' => 'input text'),
  7329. array('input' => array(
  7330. 'type' => 'text', 'name' => 'data[Contact][email]',
  7331. 'id' => 'ContactEmail'
  7332. )),
  7333. 'label' => array('for' => 'ContactEmail'),
  7334. 'em' => array(),
  7335. 'Email (required)',
  7336. '/em',
  7337. '/label',
  7338. '/div'
  7339. );
  7340. $this->assertTags($result, $expected);
  7341. $result = $this->Form->input('Contact.email', array(
  7342. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7343. 'between' => '<div>Something in the middle</div>',
  7344. 'after' => '<span>Some text at the end</span>'
  7345. ));
  7346. $expected = array(
  7347. 'div' => array('class' => 'input text'),
  7348. array('input' => array(
  7349. 'type' => 'text', 'name' => 'data[Contact][email]',
  7350. 'id' => 'ContactEmail'
  7351. )),
  7352. array('div' => array()),
  7353. 'Something in the middle',
  7354. '/div',
  7355. 'label' => array('for' => 'ContactEmail'),
  7356. 'Email',
  7357. '/label',
  7358. 'span' => array(),
  7359. 'Some text at the end',
  7360. '/span',
  7361. '/div'
  7362. );
  7363. $this->assertTags($result, $expected);
  7364. $result = $this->Form->input('Contact.method', array(
  7365. 'type' => 'radio',
  7366. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7367. 'between' => 'I am between',
  7368. ));
  7369. $expected = array(
  7370. 'div' => array('class' => 'input radio'),
  7371. 'fieldset' => array(),
  7372. 'legend' => array(),
  7373. 'Method',
  7374. '/legend',
  7375. 'I am between',
  7376. 'input' => array(
  7377. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  7378. 'value' => '', 'id' => 'ContactMethod_'
  7379. ),
  7380. array('input' => array(
  7381. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7382. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7383. )),
  7384. array('label' => array('for' => 'ContactMethodEmail')),
  7385. 'Email',
  7386. '/label',
  7387. array('input' => array(
  7388. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7389. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7390. )),
  7391. array('label' => array('for' => 'ContactMethodPigeon')),
  7392. 'Pigeon',
  7393. '/label',
  7394. '/fieldset',
  7395. '/div',
  7396. );
  7397. $this->assertTags($result, $expected);
  7398. }
  7399. /**
  7400. * test that some html5 inputs + FormHelper::__call() work
  7401. *
  7402. * @return void
  7403. */
  7404. public function testHtml5Inputs() {
  7405. $result = $this->Form->email('User.email');
  7406. $expected = array(
  7407. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  7408. );
  7409. $this->assertTags($result, $expected);
  7410. $result = $this->Form->search('User.query');
  7411. $expected = array(
  7412. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  7413. );
  7414. $this->assertTags($result, $expected);
  7415. $result = $this->Form->search('User.query', array('value' => 'test'));
  7416. $expected = array(
  7417. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7418. );
  7419. $this->assertTags($result, $expected);
  7420. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7421. $expected = array(
  7422. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7423. );
  7424. $this->assertTags($result, $expected);
  7425. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  7426. $expected = array(
  7427. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7428. );
  7429. $this->assertTags($result, $expected);
  7430. }
  7431. /**
  7432. *
  7433. * @expectedException CakeException
  7434. * @return void
  7435. */
  7436. public function testHtml5InputException() {
  7437. $this->Form->email();
  7438. }
  7439. /**
  7440. * Tests that a model can be loaded from the model names passed in the request object
  7441. *
  7442. * @return void
  7443. */
  7444. public function testIntrospectModelFromRequest() {
  7445. $this->loadFixtures('Post');
  7446. App::build(array(
  7447. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  7448. ));
  7449. CakePlugin::load('TestPlugin');
  7450. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  7451. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  7452. $this->Form->create('TestPluginPost');
  7453. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  7454. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  7455. CakePlugin::unload();
  7456. App::build();
  7457. }
  7458. /**
  7459. * Tests that it is possible to set the validation errors directly in the helper for a field
  7460. *
  7461. * @return void
  7462. */
  7463. public function testCustomValidationErrors() {
  7464. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7465. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7466. $this->assertEquals('Badness!', $result);
  7467. }
  7468. /**
  7469. * Tests that the 'on' key validates as expected on create
  7470. *
  7471. * @return void
  7472. */
  7473. public function testRequiredOnCreate() {
  7474. $this->Form->create('Contact');
  7475. $result = $this->Form->input('Contact.imrequiredonupdate');
  7476. $expected = array(
  7477. 'div' => array('class' => 'input text'),
  7478. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7479. 'Imrequiredonupdate',
  7480. '/label',
  7481. 'input' => array(
  7482. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7483. 'id' => 'ContactImrequiredonupdate'
  7484. ),
  7485. '/div'
  7486. );
  7487. $this->assertTags($result, $expected);
  7488. $result = $this->Form->input('Contact.imrequiredoncreate');
  7489. $expected = array(
  7490. 'div' => array('class' => 'input text required'),
  7491. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7492. 'Imrequiredoncreate',
  7493. '/label',
  7494. 'input' => array(
  7495. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7496. 'id' => 'ContactImrequiredoncreate'
  7497. ),
  7498. '/div'
  7499. );
  7500. $this->assertTags($result, $expected);
  7501. $result = $this->Form->input('Contact.imrequiredonboth');
  7502. $expected = array(
  7503. 'div' => array('class' => 'input text required'),
  7504. 'label' => array('for' => 'ContactImrequiredonboth'),
  7505. 'Imrequiredonboth',
  7506. '/label',
  7507. 'input' => array(
  7508. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7509. 'id' => 'ContactImrequiredonboth'
  7510. ),
  7511. '/div'
  7512. );
  7513. $this->assertTags($result, $expected);
  7514. $result = $this->Form->input('Contact.imrequired');
  7515. $expected = array(
  7516. 'div' => array('class' => 'input text required'),
  7517. 'label' => array('for' => 'ContactImrequired'),
  7518. 'Imrequired',
  7519. '/label',
  7520. 'input' => array(
  7521. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7522. 'id' => 'ContactImrequired'
  7523. ),
  7524. '/div'
  7525. );
  7526. $this->assertTags($result, $expected);
  7527. }
  7528. /**
  7529. * Tests that the 'on' key validates as expected on update
  7530. *
  7531. * @return void
  7532. */
  7533. public function testRequiredOnUpdate() {
  7534. $this->Form->request->data['Contact']['id'] = 1;
  7535. $this->Form->create('Contact');
  7536. $result = $this->Form->input('Contact.imrequiredonupdate');
  7537. $expected = array(
  7538. 'div' => array('class' => 'input text required'),
  7539. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7540. 'Imrequiredonupdate',
  7541. '/label',
  7542. 'input' => array(
  7543. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7544. 'id' => 'ContactImrequiredonupdate'
  7545. ),
  7546. '/div'
  7547. );
  7548. $this->assertTags($result, $expected);
  7549. $result = $this->Form->input('Contact.imrequiredoncreate');
  7550. $expected = array(
  7551. 'div' => array('class' => 'input text'),
  7552. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7553. 'Imrequiredoncreate',
  7554. '/label',
  7555. 'input' => array(
  7556. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7557. 'id' => 'ContactImrequiredoncreate'
  7558. ),
  7559. '/div'
  7560. );
  7561. $this->assertTags($result, $expected);
  7562. $result = $this->Form->input('Contact.imrequiredonboth');
  7563. $expected = array(
  7564. 'div' => array('class' => 'input text required'),
  7565. 'label' => array('for' => 'ContactImrequiredonboth'),
  7566. 'Imrequiredonboth',
  7567. '/label',
  7568. 'input' => array(
  7569. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7570. 'id' => 'ContactImrequiredonboth'
  7571. ),
  7572. '/div'
  7573. );
  7574. $this->assertTags($result, $expected);
  7575. $result = $this->Form->input('Contact.imrequired');
  7576. $expected = array(
  7577. 'div' => array('class' => 'input text required'),
  7578. 'label' => array('for' => 'ContactImrequired'),
  7579. 'Imrequired',
  7580. '/label',
  7581. 'input' => array(
  7582. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7583. 'id' => 'ContactImrequired'
  7584. ),
  7585. '/div'
  7586. );
  7587. $this->assertTags($result, $expected);
  7588. }
  7589. /**
  7590. * Test inputDefaults setter and getter
  7591. *
  7592. * @return void
  7593. */
  7594. public function testInputDefaults() {
  7595. $this->Form->create('Contact');
  7596. $this->Form->inputDefaults(array(
  7597. 'label' => false,
  7598. 'div' => array(
  7599. 'style' => 'color: #000;'
  7600. )
  7601. ));
  7602. $result = $this->Form->input('Contact.field1');
  7603. $expected = array(
  7604. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  7605. 'input' => array(
  7606. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7607. 'id' => 'ContactField1'
  7608. ),
  7609. '/div'
  7610. );
  7611. $this->assertTags($result, $expected);
  7612. $this->Form->inputDefaults(array(
  7613. 'div' => false,
  7614. 'label' => 'Label',
  7615. ));
  7616. $result = $this->Form->input('Contact.field1');
  7617. $expected = array(
  7618. 'label' => array('for' => 'ContactField1'),
  7619. 'Label',
  7620. '/label',
  7621. 'input' => array(
  7622. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7623. 'id' => 'ContactField1'
  7624. ),
  7625. );
  7626. $this->assertTags($result, $expected);
  7627. $this->Form->inputDefaults(array(
  7628. 'label' => false,
  7629. ), true);
  7630. $result = $this->Form->input('Contact.field1');
  7631. $expected = array(
  7632. 'input' => array(
  7633. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7634. 'id' => 'ContactField1'
  7635. ),
  7636. );
  7637. $this->assertTags($result, $expected);
  7638. $result = $this->Form->inputDefaults();
  7639. $expected = array(
  7640. 'div' => false,
  7641. 'label' => false,
  7642. );
  7643. $this->assertEqual($result, $expected);
  7644. }
  7645. }