FormHelperTest.php 256 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123
  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), 'allowEmpty' => true),
  111. ),
  112. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  113. );
  114. /**
  115. * schema method
  116. *
  117. * @return void
  118. */
  119. public function setSchema($schema) {
  120. $this->_schema = $schema;
  121. }
  122. /**
  123. * hasAndBelongsToMany property
  124. *
  125. * @var array
  126. */
  127. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  128. /**
  129. * hasAndBelongsToMany property
  130. *
  131. * @var array
  132. */
  133. public $belongsTo = array('User' => array('className' => 'UserForm'));
  134. }
  135. /**
  136. * ContactTagsContact class
  137. *
  138. * @package cake
  139. * @package Cake.Test.Case.View.Helper
  140. */
  141. class ContactTagsContact extends CakeTestModel {
  142. /**
  143. * useTable property
  144. *
  145. * @var bool false
  146. */
  147. public $useTable = false;
  148. /**
  149. * name property
  150. *
  151. * @var string 'Contact'
  152. */
  153. public $name = 'ContactTagsContact';
  154. /**
  155. * Default schema
  156. *
  157. * @var array
  158. */
  159. protected $_schema = array(
  160. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  161. 'contact_tag_id' => array(
  162. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  163. )
  164. );
  165. /**
  166. * schema method
  167. *
  168. * @return void
  169. */
  170. public function setSchema($schema) {
  171. $this->_schema = $schema;
  172. }
  173. }
  174. /**
  175. * ContactNonStandardPk class
  176. *
  177. * @package cake
  178. * @package Cake.Test.Case.View.Helper
  179. */
  180. class ContactNonStandardPk extends Contact {
  181. /**
  182. * primaryKey property
  183. *
  184. * @var string 'pk'
  185. */
  186. public $primaryKey = 'pk';
  187. /**
  188. * name property
  189. *
  190. * @var string 'ContactNonStandardPk'
  191. */
  192. public $name = 'ContactNonStandardPk';
  193. /**
  194. * schema method
  195. *
  196. * @return void
  197. */
  198. public function schema($field = false) {
  199. $this->_schema = parent::schema();
  200. $this->_schema['pk'] = $this->_schema['id'];
  201. unset($this->_schema['id']);
  202. return $this->_schema;
  203. }
  204. }
  205. /**
  206. * ContactTag class
  207. *
  208. * @package cake
  209. * @package Cake.Test.Case.View.Helper
  210. */
  211. class ContactTag extends Model {
  212. /**
  213. * useTable property
  214. *
  215. * @var bool false
  216. */
  217. public $useTable = false;
  218. /**
  219. * schema definition
  220. *
  221. * @var array
  222. */
  223. protected $_schema = array(
  224. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  225. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  226. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  227. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  228. );
  229. }
  230. /**
  231. * UserForm class
  232. *
  233. * @package cake
  234. * @package Cake.Test.Case.View.Helper
  235. */
  236. class UserForm extends CakeTestModel {
  237. /**
  238. * useTable property
  239. *
  240. * @var bool false
  241. */
  242. public $useTable = false;
  243. /**
  244. * primaryKey property
  245. *
  246. * @var string 'id'
  247. */
  248. public $primaryKey = 'id';
  249. /**
  250. * name property
  251. *
  252. * @var string 'UserForm'
  253. */
  254. public $name = 'UserForm';
  255. /**
  256. * hasMany property
  257. *
  258. * @var array
  259. */
  260. public $hasMany = array(
  261. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  262. ));
  263. /**
  264. * schema definition
  265. *
  266. * @var array
  267. */
  268. protected $_schema = array(
  269. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  270. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  271. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  272. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  273. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  274. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  275. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  276. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  277. );
  278. }
  279. /**
  280. * OpenidUrl class
  281. *
  282. * @package cake
  283. * @package Cake.Test.Case.View.Helper
  284. */
  285. class OpenidUrl extends CakeTestModel {
  286. /**
  287. * useTable property
  288. *
  289. * @var bool false
  290. */
  291. public $useTable = false;
  292. /**
  293. * primaryKey property
  294. *
  295. * @var string 'id'
  296. */
  297. public $primaryKey = 'id';
  298. /**
  299. * name property
  300. *
  301. * @var string 'OpenidUrl'
  302. */
  303. public $name = 'OpenidUrl';
  304. /**
  305. * belongsTo property
  306. *
  307. * @var array
  308. */
  309. public $belongsTo = array('UserForm' => array(
  310. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  311. ));
  312. /**
  313. * validate property
  314. *
  315. * @var array
  316. */
  317. public $validate = array('openid_not_registered' => array());
  318. /**
  319. * schema method
  320. *
  321. * @var array
  322. */
  323. protected $_schema = array(
  324. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  325. 'user_form_id' => array(
  326. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  327. ),
  328. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  329. );
  330. /**
  331. * beforeValidate method
  332. *
  333. * @return void
  334. */
  335. public function beforeValidate($options = array()) {
  336. $this->invalidate('openid_not_registered');
  337. return true;
  338. }
  339. }
  340. /**
  341. * ValidateUser class
  342. *
  343. * @package cake
  344. * @package Cake.Test.Case.View.Helper
  345. */
  346. class ValidateUser extends CakeTestModel {
  347. /**
  348. * primaryKey property
  349. *
  350. * @var string 'id'
  351. */
  352. public $primaryKey = 'id';
  353. /**
  354. * useTable property
  355. *
  356. * @var bool false
  357. */
  358. public $useTable = false;
  359. /**
  360. * name property
  361. *
  362. * @var string 'ValidateUser'
  363. */
  364. public $name = 'ValidateUser';
  365. /**
  366. * hasOne property
  367. *
  368. * @var array
  369. */
  370. public $hasOne = array('ValidateProfile' => array(
  371. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  372. ));
  373. /**
  374. * schema method
  375. *
  376. * @var array
  377. */
  378. protected $_schema = array(
  379. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  380. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  381. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  382. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  383. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  384. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  385. );
  386. /**
  387. * beforeValidate method
  388. *
  389. * @return void
  390. */
  391. public function beforeValidate($options = array()) {
  392. $this->invalidate('email');
  393. return false;
  394. }
  395. }
  396. /**
  397. * ValidateProfile class
  398. *
  399. * @package cake
  400. * @package Cake.Test.Case.View.Helper
  401. */
  402. class ValidateProfile extends CakeTestModel {
  403. /**
  404. * primaryKey property
  405. *
  406. * @var string 'id'
  407. */
  408. public $primaryKey = 'id';
  409. /**
  410. * useTable property
  411. *
  412. * @var bool false
  413. */
  414. public $useTable = false;
  415. /**
  416. * schema property
  417. *
  418. * @var array
  419. */
  420. protected $_schema = array(
  421. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  422. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  423. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  424. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  425. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  426. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  427. );
  428. /**
  429. * name property
  430. *
  431. * @var string 'ValidateProfile'
  432. */
  433. public $name = 'ValidateProfile';
  434. /**
  435. * hasOne property
  436. *
  437. * @var array
  438. */
  439. public $hasOne = array('ValidateItem' => array(
  440. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  441. ));
  442. /**
  443. * belongsTo property
  444. *
  445. * @var array
  446. */
  447. public $belongsTo = array('ValidateUser' => array(
  448. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  449. ));
  450. /**
  451. * beforeValidate method
  452. *
  453. * @return void
  454. */
  455. public function beforeValidate($options = array()) {
  456. $this->invalidate('full_name');
  457. $this->invalidate('city');
  458. return false;
  459. }
  460. }
  461. /**
  462. * ValidateItem class
  463. *
  464. * @package cake
  465. * @package Cake.Test.Case.View.Helper
  466. */
  467. class ValidateItem extends CakeTestModel {
  468. /**
  469. * primaryKey property
  470. *
  471. * @var string 'id'
  472. */
  473. public $primaryKey = 'id';
  474. /**
  475. * useTable property
  476. *
  477. * @var bool false
  478. */
  479. public $useTable = false;
  480. /**
  481. * name property
  482. *
  483. * @var string 'ValidateItem'
  484. */
  485. public $name = 'ValidateItem';
  486. /**
  487. * schema property
  488. *
  489. * @var array
  490. */
  491. protected $_schema = array(
  492. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  493. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  494. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  495. 'description' => array(
  496. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  497. ),
  498. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  499. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  500. );
  501. /**
  502. * belongsTo property
  503. *
  504. * @var array
  505. */
  506. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  507. /**
  508. * beforeValidate method
  509. *
  510. * @return void
  511. */
  512. public function beforeValidate($options = array()) {
  513. $this->invalidate('description');
  514. return false;
  515. }
  516. }
  517. /**
  518. * TestMail class
  519. *
  520. * @package cake
  521. * @package Cake.Test.Case.View.Helper
  522. */
  523. class TestMail extends CakeTestModel {
  524. /**
  525. * primaryKey property
  526. *
  527. * @var string 'id'
  528. */
  529. public $primaryKey = 'id';
  530. /**
  531. * useTable property
  532. *
  533. * @var bool false
  534. */
  535. public $useTable = false;
  536. /**
  537. * name property
  538. *
  539. * @var string 'TestMail'
  540. */
  541. public $name = 'TestMail';
  542. }
  543. /**
  544. * FormHelperTest class
  545. *
  546. * @package cake
  547. * @subpackage Cake.Test.Case.View.Helper
  548. * @property FormHelper $Form
  549. */
  550. class FormHelperTest extends CakeTestCase {
  551. /**
  552. * Fixtures to be used
  553. *
  554. * @var array
  555. */
  556. public $fixtures = array('core.post');
  557. /**
  558. * Do not load the fixtures by default
  559. *
  560. * @var boolean
  561. */
  562. public $autoFixtures = false;
  563. /**
  564. * setUp method
  565. *
  566. * @return void
  567. */
  568. public function setUp() {
  569. parent::setUp();
  570. Configure::write('App.base', '');
  571. $this->Controller = new ContactTestController();
  572. $this->View = new View($this->Controller);
  573. $this->Form = new FormHelper($this->View);
  574. $this->Form->Html = new HtmlHelper($this->View);
  575. $this->Form->request = new CakeRequest('contacts/add', false);
  576. $this->Form->request->here = '/contacts/add';
  577. $this->Form->request['action'] = 'add';
  578. $this->Form->request->webroot = '';
  579. $this->Form->request->base = '';
  580. ClassRegistry::addObject('Contact', new Contact());
  581. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  582. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  583. ClassRegistry::addObject('User', new UserForm());
  584. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  585. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  586. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  587. $this->oldSalt = Configure::read('Security.salt');
  588. $this->dateRegex = array(
  589. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  590. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  591. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  592. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  593. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  594. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  595. );
  596. Configure::write('Security.salt', 'foo!');
  597. }
  598. /**
  599. * tearDown method
  600. *
  601. * @return void
  602. */
  603. public function tearDown() {
  604. parent::tearDown();
  605. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  606. Configure::write('Security.salt', $this->oldSalt);
  607. }
  608. /**
  609. * testFormCreateWithSecurity method
  610. *
  611. * Test form->create() with security key.
  612. *
  613. * @return void
  614. */
  615. public function testCreateWithSecurity() {
  616. $this->Form->request['_Token'] = array('key' => 'testKey');
  617. $encoding = strtolower(Configure::read('App.encoding'));
  618. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  619. $expected = array(
  620. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  621. 'div' => array('style' => 'display:none;'),
  622. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  623. array('input' => array(
  624. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  625. )),
  626. '/div'
  627. );
  628. $this->assertTags($result, $expected);
  629. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  630. $expected['form']['id'] = 'MyForm';
  631. $this->assertTags($result, $expected);
  632. }
  633. /**
  634. * test that create() clears the fields property so it starts fresh
  635. *
  636. * @return void
  637. */
  638. public function testCreateClearingFields() {
  639. $this->Form->fields = array('model_id');
  640. $this->Form->create('Contact');
  641. $this->assertEquals(array(), $this->Form->fields);
  642. }
  643. /**
  644. * Tests form hash generation with model-less data
  645. *
  646. * @return void
  647. */
  648. public function testValidateHashNoModel() {
  649. $this->Form->request['_Token'] = array('key' => 'foo');
  650. $result = $this->Form->secure(array('anything'));
  651. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  652. }
  653. /**
  654. * Tests that models with identical field names get resolved properly
  655. *
  656. * @return void
  657. */
  658. public function testDuplicateFieldNameResolution() {
  659. $result = $this->Form->create('ValidateUser');
  660. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  661. $result = $this->Form->input('ValidateItem.name');
  662. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  663. $result = $this->Form->input('ValidateUser.name');
  664. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  665. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  666. $this->assertRegExp('/type="text"/', $result);
  667. $result = $this->Form->input('ValidateItem.name');
  668. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  669. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  670. $this->assertRegExp('/<textarea/', $result);
  671. $result = $this->Form->input('name');
  672. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  673. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  674. $this->assertRegExp('/type="text"/', $result);
  675. }
  676. /**
  677. * Tests that hidden fields generated for checkboxes don't get locked
  678. *
  679. * @return void
  680. */
  681. public function testNoCheckboxLocking() {
  682. $this->Form->request['_Token'] = array('key' => 'foo');
  683. $this->assertSame(array(), $this->Form->fields);
  684. $this->Form->checkbox('check', array('value' => '1'));
  685. $this->assertSame($this->Form->fields, array('check'));
  686. }
  687. /**
  688. * testFormSecurityFields method
  689. *
  690. * Test generation of secure form hash generation.
  691. *
  692. * @return void
  693. */
  694. public function testFormSecurityFields() {
  695. $key = 'testKey';
  696. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  697. $this->Form->request['_Token'] = array('key' => $key);
  698. $result = $this->Form->secure($fields);
  699. $expected = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  700. $expected .= ':' . 'Model.valid';
  701. $expected = array(
  702. 'div' => array('style' => 'display:none;'),
  703. array('input' => array(
  704. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  705. 'value' => urlencode($expected), 'id' => 'preg:/TokenFields\d+/'
  706. )),
  707. array('input' => array(
  708. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  709. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  710. )),
  711. '/div'
  712. );
  713. $this->assertTags($result, $expected);
  714. }
  715. /**
  716. * Tests correct generation of number fields for double and float fields
  717. *
  718. * @return void
  719. */
  720. public function testTextFieldGenerationForFloats() {
  721. $model = ClassRegistry::getObject('Contact');
  722. $model->setSchema(array('foo' => array(
  723. 'type' => 'float',
  724. 'null' => false,
  725. 'default' => null,
  726. 'length' => null
  727. )));
  728. $this->Form->create('Contact');
  729. $result = $this->Form->input('foo');
  730. $expected = array(
  731. 'div' => array('class' => 'input number'),
  732. 'label' => array('for' => 'ContactFoo'),
  733. 'Foo',
  734. '/label',
  735. array('input' => array(
  736. 'type' => 'number',
  737. 'name' => 'data[Contact][foo]',
  738. 'id' => 'ContactFoo',
  739. 'step' => 'any'
  740. )),
  741. '/div'
  742. );
  743. $this->assertTags($result, $expected);
  744. $result = $this->Form->input('foo', array('step' => 0.5));
  745. $expected = array(
  746. 'div' => array('class' => 'input number'),
  747. 'label' => array('for' => 'ContactFoo'),
  748. 'Foo',
  749. '/label',
  750. array('input' => array(
  751. 'type' => 'number',
  752. 'name' => 'data[Contact][foo]',
  753. 'id' => 'ContactFoo',
  754. 'step' => '0.5'
  755. )),
  756. '/div'
  757. );
  758. $this->assertTags($result, $expected);
  759. }
  760. /**
  761. * Tests correct generation of number fields for integer fields
  762. *
  763. * @access public
  764. * @return void
  765. */
  766. public function testTextFieldTypeNumberGenerationForIntegers() {
  767. $model = ClassRegistry::getObject('Contact');
  768. $model->setSchema(array('foo' => array(
  769. 'type' => 'integer',
  770. 'null' => false,
  771. 'default' => null,
  772. 'length' => null
  773. )));
  774. $this->Form->create('Contact');
  775. $result = $this->Form->input('foo');
  776. $expected = array(
  777. 'div' => array('class' => 'input number'),
  778. 'label' => array('for' => 'ContactFoo'),
  779. 'Foo',
  780. '/label',
  781. array('input' => array(
  782. 'type' => 'number', 'name' => 'data[Contact][foo]',
  783. 'id' => 'ContactFoo'
  784. )),
  785. '/div'
  786. );
  787. $this->assertTags($result, $expected);
  788. }
  789. /**
  790. * testFormSecurityMultipleFields method
  791. *
  792. * Test secure() with multiple row form. Ensure hash is correct.
  793. *
  794. * @return void
  795. */
  796. public function testFormSecurityMultipleFields() {
  797. $key = 'testKey';
  798. $fields = array(
  799. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  800. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  801. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  802. );
  803. $this->Form->request['_Token'] = array('key' => $key);
  804. $result = $this->Form->secure($fields);
  805. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  806. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  807. $expected = array(
  808. 'div' => array('style' => 'display:none;'),
  809. array('input' => array(
  810. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  811. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  812. )),
  813. array('input' => array(
  814. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  815. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  816. )),
  817. '/div'
  818. );
  819. $this->assertTags($result, $expected);
  820. }
  821. /**
  822. * testFormSecurityMultipleSubmitButtons
  823. *
  824. * test form submit generation and ensure that _Token is only created on end()
  825. *
  826. * @return void
  827. */
  828. public function testFormSecurityMultipleSubmitButtons() {
  829. $key = 'testKey';
  830. $this->Form->request['_Token'] = array('key' => $key);
  831. $this->Form->create('Addresses');
  832. $this->Form->input('Address.title');
  833. $this->Form->input('Address.first_name');
  834. $result = $this->Form->submit('Save', array('name' => 'save'));
  835. $expected = array(
  836. 'div' => array('class' => 'submit'),
  837. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  838. '/div',
  839. );
  840. $this->assertTags($result, $expected);
  841. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  842. $expected = array(
  843. 'div' => array('class' => 'submit'),
  844. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  845. '/div',
  846. );
  847. $this->assertTags($result, $expected);
  848. $result = $this->Form->end(null);
  849. $expected = array(
  850. 'div' => array('style' => 'display:none;'),
  851. array('input' => array(
  852. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  853. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  854. )),
  855. array('input' => array(
  856. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  857. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  858. )),
  859. '/div'
  860. );
  861. $this->assertTags($result, $expected);
  862. }
  863. /**
  864. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  865. *
  866. * @return void
  867. */
  868. public function testSecurityButtonNestedNamed() {
  869. $key = 'testKey';
  870. $this->Form->request['_Token'] = array('key' => $key);
  871. $this->Form->create('Addresses');
  872. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  873. $result = $this->Form->unlockField();
  874. $this->assertEquals(array('Address.button'), $result);
  875. }
  876. /**
  877. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  878. *
  879. * @return void
  880. */
  881. public function testSecuritySubmitNestedNamed() {
  882. $key = 'testKey';
  883. $this->Form->request['_Token'] = array('key' => $key);
  884. $this->Form->create('Addresses');
  885. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  886. $result = $this->Form->unlockField();
  887. $this->assertEquals(array('Address.button'), $result);
  888. }
  889. /**
  890. * Test that the correct fields are unlocked for image submits with no names.
  891. *
  892. * @return void
  893. */
  894. public function testSecuritySubmitImageNoName() {
  895. $key = 'testKey';
  896. $this->Form->request['_Token'] = array('key' => $key);
  897. $this->Form->create('User');
  898. $result = $this->Form->submit('save.png');
  899. $expected = array(
  900. 'div' => array('class' => 'submit'),
  901. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  902. '/div'
  903. );
  904. $this->assertTags($result, $expected);
  905. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  906. }
  907. /**
  908. * Test that the correct fields are unlocked for image submits with names.
  909. *
  910. * @return void
  911. */
  912. public function testSecuritySubmitImageName() {
  913. $key = 'testKey';
  914. $this->Form->request['_Token'] = array('key' => $key);
  915. $this->Form->create('User');
  916. $result = $this->Form->submit('save.png', array('name' => 'test'));
  917. $expected = array(
  918. 'div' => array('class' => 'submit'),
  919. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  920. '/div'
  921. );
  922. $this->assertTags($result, $expected);
  923. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  924. }
  925. /**
  926. * testFormSecurityMultipleInputFields method
  927. *
  928. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  929. *
  930. * @return void
  931. */
  932. public function testFormSecurityMultipleInputFields() {
  933. $key = 'testKey';
  934. $this->Form->request['_Token'] = array('key' => $key);
  935. $this->Form->create('Addresses');
  936. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  937. $this->Form->input('Addresses.0.title');
  938. $this->Form->input('Addresses.0.first_name');
  939. $this->Form->input('Addresses.0.last_name');
  940. $this->Form->input('Addresses.0.address');
  941. $this->Form->input('Addresses.0.city');
  942. $this->Form->input('Addresses.0.phone');
  943. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  944. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  945. $this->Form->input('Addresses.1.title');
  946. $this->Form->input('Addresses.1.first_name');
  947. $this->Form->input('Addresses.1.last_name');
  948. $this->Form->input('Addresses.1.address');
  949. $this->Form->input('Addresses.1.city');
  950. $this->Form->input('Addresses.1.phone');
  951. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  952. $result = $this->Form->secure($this->Form->fields);
  953. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  954. $expected = array(
  955. 'div' => array('style' => 'display:none;'),
  956. array('input' => array(
  957. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  958. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  959. )),
  960. array('input' => array(
  961. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  962. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  963. )),
  964. '/div'
  965. );
  966. $this->assertTags($result, $expected);
  967. }
  968. /**
  969. * Test form security with Model.field.0 style inputs
  970. *
  971. * @return void
  972. */
  973. public function testFormSecurityArrayFields() {
  974. $key = 'testKey';
  975. $this->Form->request->params['_Token']['key'] = $key;
  976. $this->Form->create('Address');
  977. $this->Form->input('Address.primary.1');
  978. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  979. $this->Form->input('Address.secondary.1.0');
  980. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  981. }
  982. /**
  983. * testFormSecurityMultipleInputDisabledFields method
  984. *
  985. * test secure form generation with multiple records and disabled fields.
  986. *
  987. * @return void
  988. */
  989. public function testFormSecurityMultipleInputDisabledFields() {
  990. $key = 'testKey';
  991. $this->Form->request->params['_Token'] = array(
  992. 'key' => $key,
  993. 'unlockedFields' => array('first_name', 'address')
  994. );
  995. $this->Form->create();
  996. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  997. $this->Form->input('Addresses.0.title');
  998. $this->Form->input('Addresses.0.first_name');
  999. $this->Form->input('Addresses.0.last_name');
  1000. $this->Form->input('Addresses.0.address');
  1001. $this->Form->input('Addresses.0.city');
  1002. $this->Form->input('Addresses.0.phone');
  1003. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1004. $this->Form->input('Addresses.1.title');
  1005. $this->Form->input('Addresses.1.first_name');
  1006. $this->Form->input('Addresses.1.last_name');
  1007. $this->Form->input('Addresses.1.address');
  1008. $this->Form->input('Addresses.1.city');
  1009. $this->Form->input('Addresses.1.phone');
  1010. $result = $this->Form->secure($this->Form->fields);
  1011. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  1012. $expected = array(
  1013. 'div' => array('style' => 'display:none;'),
  1014. array('input' => array(
  1015. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1016. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1017. )),
  1018. array('input' => array(
  1019. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1020. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1021. )),
  1022. '/div'
  1023. );
  1024. $this->assertTags($result, $expected);
  1025. }
  1026. /**
  1027. * testFormSecurityInputDisabledFields method
  1028. *
  1029. * Test single record form with disabled fields.
  1030. *
  1031. * @return void
  1032. */
  1033. public function testFormSecurityInputUnlockedFields() {
  1034. $key = 'testKey';
  1035. $this->Form->request['_Token'] = array(
  1036. 'key' => $key,
  1037. 'unlockedFields' => array('first_name', 'address')
  1038. );
  1039. $this->Form->create();
  1040. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1041. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1042. $this->Form->input('Addresses.title');
  1043. $this->Form->input('Addresses.first_name');
  1044. $this->Form->input('Addresses.last_name');
  1045. $this->Form->input('Addresses.address');
  1046. $this->Form->input('Addresses.city');
  1047. $this->Form->input('Addresses.phone');
  1048. $result = $this->Form->fields;
  1049. $expected = array(
  1050. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1051. 'Addresses.city', 'Addresses.phone'
  1052. );
  1053. $this->assertEquals($expected, $result);
  1054. $result = $this->Form->secure($expected);
  1055. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1056. $expected = array(
  1057. 'div' => array('style' => 'display:none;'),
  1058. array('input' => array(
  1059. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1060. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1061. )),
  1062. array('input' => array(
  1063. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1064. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1065. )),
  1066. '/div'
  1067. );
  1068. $this->assertTags($result, $expected);
  1069. }
  1070. /**
  1071. * test securing inputs with custom name attributes.
  1072. *
  1073. * @return void
  1074. */
  1075. public function testFormSecureWithCustomNameAttribute() {
  1076. $this->Form->request->params['_Token']['key'] = 'testKey';
  1077. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1078. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1079. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1080. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1081. }
  1082. /**
  1083. * testFormSecuredInput method
  1084. *
  1085. * Test generation of entire secure form, assertions made on input() output.
  1086. *
  1087. * @return void
  1088. */
  1089. public function testFormSecuredInput() {
  1090. $this->Form->request['_Token'] = array('key' => 'testKey');
  1091. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1092. $encoding = strtolower(Configure::read('App.encoding'));
  1093. $expected = array(
  1094. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1095. 'div' => array('style' => 'display:none;'),
  1096. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1097. array('input' => array(
  1098. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1099. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1100. )),
  1101. '/div'
  1102. );
  1103. $this->assertTags($result, $expected);
  1104. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1105. $expected = array(
  1106. 'div' => array('class' => 'input text'),
  1107. 'label' => array('for' => 'UserFormPublished'),
  1108. 'Published',
  1109. '/label',
  1110. array('input' => array(
  1111. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1112. 'id' => 'UserFormPublished'
  1113. )),
  1114. '/div'
  1115. );
  1116. $this->assertTags($result, $expected);
  1117. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1118. $expected = array(
  1119. 'div' => array('class' => 'input text'),
  1120. 'label' => array('for' => 'UserFormOther'),
  1121. 'Other',
  1122. '/label',
  1123. array('input' => array(
  1124. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1125. 'id' => 'UserFormOther'
  1126. )),
  1127. '/div'
  1128. );
  1129. $this->assertTags($result, $expected);
  1130. $result = $this->Form->hidden('UserForm.stuff');
  1131. $expected = array('input' => array(
  1132. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1133. 'id' => 'UserFormStuff'
  1134. ));
  1135. $this->assertTags($result, $expected);
  1136. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1137. $expected = array('input' => array(
  1138. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1139. 'value' => '0', 'id' => 'UserFormHidden'
  1140. ));
  1141. $this->assertTags($result, $expected);
  1142. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1143. $expected = array(
  1144. 'div' => array('class' => 'input checkbox'),
  1145. array('input' => array(
  1146. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1147. 'value' => '0', 'id' => 'UserFormSomething_'
  1148. )),
  1149. array('input' => array(
  1150. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1151. 'value' => '1', 'id' => 'UserFormSomething'
  1152. )),
  1153. 'label' => array('for' => 'UserFormSomething'),
  1154. 'Something',
  1155. '/label',
  1156. '/div'
  1157. );
  1158. $this->assertTags($result, $expected);
  1159. $result = $this->Form->fields;
  1160. $expected = array(
  1161. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1162. 'UserForm.hidden' => '0', 'UserForm.something'
  1163. );
  1164. $this->assertEquals($expected, $result);
  1165. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1166. $result = $this->Form->secure($this->Form->fields);
  1167. $expected = array(
  1168. 'div' => array('style' => 'display:none;'),
  1169. array('input' => array(
  1170. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1171. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1172. )),
  1173. array('input' => array(
  1174. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1175. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1176. )),
  1177. '/div'
  1178. );
  1179. $this->assertTags($result, $expected);
  1180. }
  1181. /**
  1182. * Tests that the correct keys are added to the field hash index
  1183. *
  1184. * @return void
  1185. */
  1186. public function testFormSecuredFileInput() {
  1187. $this->Form->request['_Token'] = array('key' => 'testKey');
  1188. $this->assertEquals(array(), $this->Form->fields);
  1189. $result = $this->Form->file('Attachment.file');
  1190. $expected = array(
  1191. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1192. 'Attachment.file.error', 'Attachment.file.size'
  1193. );
  1194. $this->assertEquals($expected, $this->Form->fields);
  1195. }
  1196. /**
  1197. * test that multiple selects keys are added to field hash
  1198. *
  1199. * @return void
  1200. */
  1201. public function testFormSecuredMultipleSelect() {
  1202. $this->Form->request['_Token'] = array('key' => 'testKey');
  1203. $this->assertEquals(array(), $this->Form->fields);
  1204. $options = array('1' => 'one', '2' => 'two');
  1205. $this->Form->select('Model.select', $options);
  1206. $expected = array('Model.select');
  1207. $this->assertEquals($expected, $this->Form->fields);
  1208. $this->Form->fields = array();
  1209. $this->Form->select('Model.select', $options, array('multiple' => true));
  1210. $this->assertEquals($expected, $this->Form->fields);
  1211. }
  1212. /**
  1213. * testFormSecuredRadio method
  1214. *
  1215. * @return void
  1216. */
  1217. public function testFormSecuredRadio() {
  1218. $this->Form->request['_Token'] = array('key' => 'testKey');
  1219. $this->assertEquals(array(), $this->Form->fields);
  1220. $options = array('1' => 'option1', '2' => 'option2');
  1221. $this->Form->radio('Test.test', $options);
  1222. $expected = array('Test.test');
  1223. $this->assertEquals($expected, $this->Form->fields);
  1224. }
  1225. /**
  1226. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1227. * hashing.
  1228. *
  1229. * @return void
  1230. */
  1231. public function testFormSecuredAndDisabled() {
  1232. $this->Form->request['_Token'] = array('key' => 'testKey');
  1233. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1234. $this->Form->text('Model.text', array('disabled' => true));
  1235. $this->Form->password('Model.text', array('disabled' => true));
  1236. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1237. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1238. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1239. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1240. $this->Form->month('Model.month', array('disabled' => true));
  1241. $this->Form->day('Model.day', array('disabled' => true));
  1242. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1243. $this->Form->minute('Model.minute', array('disabled' => true));
  1244. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1245. $expected = array(
  1246. 'Model.radio' => ''
  1247. );
  1248. $this->assertEquals($expected, $this->Form->fields);
  1249. }
  1250. /**
  1251. * testDisableSecurityUsingForm method
  1252. *
  1253. * @return void
  1254. */
  1255. public function testDisableSecurityUsingForm() {
  1256. $this->Form->request['_Token'] = array(
  1257. 'key' => 'testKey',
  1258. 'disabledFields' => array()
  1259. );
  1260. $this->Form->create();
  1261. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1262. $this->Form->input('Addresses.title');
  1263. $this->Form->input('Addresses.first_name', array('secure' => false));
  1264. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1265. $this->Form->input('Addresses.zip', array(
  1266. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1267. ));
  1268. $result = $this->Form->fields;
  1269. $expected = array(
  1270. 'Addresses.id' => '123456', 'Addresses.title',
  1271. );
  1272. $this->assertEquals($expected, $result);
  1273. }
  1274. /**
  1275. * test disableField
  1276. *
  1277. * @return void
  1278. */
  1279. public function testUnlockFieldAddsToList() {
  1280. $this->Form->request['_Token'] = array(
  1281. 'key' => 'testKey',
  1282. 'unlockedFields' => array()
  1283. );
  1284. $this->Form->create('Contact');
  1285. $this->Form->unlockField('Contact.name');
  1286. $this->Form->text('Contact.name');
  1287. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1288. $this->assertEquals(array(), $this->Form->fields);
  1289. }
  1290. /**
  1291. * test unlockField removing from fields array.
  1292. *
  1293. * @return void
  1294. */
  1295. public function testUnlockFieldRemovingFromFields() {
  1296. $this->Form->request['_Token'] = array(
  1297. 'key' => 'testKey',
  1298. 'unlockedFields' => array()
  1299. );
  1300. $this->Form->create('Contact');
  1301. $this->Form->hidden('Contact.id', array('value' => 1));
  1302. $this->Form->text('Contact.name');
  1303. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1304. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1305. $this->Form->unlockField('Contact.name');
  1306. $this->Form->unlockField('Contact.id');
  1307. $this->assertEquals(array(), $this->Form->fields);
  1308. }
  1309. /**
  1310. * testTagIsInvalid method
  1311. *
  1312. * @return void
  1313. */
  1314. public function testTagIsInvalid() {
  1315. $Contact = ClassRegistry::getObject('Contact');
  1316. $Contact->validationErrors[0]['email'] = array('Please provide an email');
  1317. $this->Form->setEntity('Contact.0.email');
  1318. $result = $this->Form->tagIsInvalid();
  1319. $expected = array('Please provide an email');
  1320. $this->assertEquals($expected, $result);
  1321. $this->Form->setEntity('Contact.1.email');
  1322. $result = $this->Form->tagIsInvalid();
  1323. $expected = false;
  1324. $this->assertSame($expected, $result);
  1325. $this->Form->setEntity('Contact.0.name');
  1326. $result = $this->Form->tagIsInvalid();
  1327. $expected = false;
  1328. $this->assertSame($expected, $result);
  1329. }
  1330. /**
  1331. * testPasswordValidation method
  1332. *
  1333. * test validation errors on password input.
  1334. *
  1335. * @return void
  1336. */
  1337. public function testPasswordValidation() {
  1338. $Contact = ClassRegistry::getObject('Contact');
  1339. $Contact->validationErrors['password'] = array('Please provide a password');
  1340. $result = $this->Form->input('Contact.password');
  1341. $expected = array(
  1342. 'div' => array('class' => 'input password error'),
  1343. 'label' => array('for' => 'ContactPassword'),
  1344. 'Password',
  1345. '/label',
  1346. 'input' => array(
  1347. 'type' => 'password', 'name' => 'data[Contact][password]',
  1348. 'id' => 'ContactPassword', 'class' => 'form-error'
  1349. ),
  1350. array('div' => array('class' => 'error-message')),
  1351. 'Please provide a password',
  1352. '/div',
  1353. '/div'
  1354. );
  1355. $this->assertTags($result, $expected);
  1356. }
  1357. /**
  1358. * testEmptyErrorValidation method
  1359. *
  1360. * test validation error div when validation message is an empty string
  1361. *
  1362. * @access public
  1363. * @return void
  1364. */
  1365. public function testEmptyErrorValidation() {
  1366. $this->Form->validationErrors['Contact']['password'] = '';
  1367. $result = $this->Form->input('Contact.password');
  1368. $expected = array(
  1369. 'div' => array('class' => 'input password error'),
  1370. 'label' => array('for' => 'ContactPassword'),
  1371. 'Password',
  1372. '/label',
  1373. 'input' => array(
  1374. 'type' => 'password', 'name' => 'data[Contact][password]',
  1375. 'id' => 'ContactPassword', 'class' => 'form-error'
  1376. ),
  1377. array('div' => array('class' => 'error-message')),
  1378. array(),
  1379. '/div',
  1380. '/div'
  1381. );
  1382. $this->assertTags($result, $expected);
  1383. }
  1384. /**
  1385. * testEmptyInputErrorValidation method
  1386. *
  1387. * test validation error div when validation message is overridden by an empty string when calling input()
  1388. *
  1389. * @access public
  1390. * @return void
  1391. */
  1392. public function testEmptyInputErrorValidation() {
  1393. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1394. $result = $this->Form->input('Contact.password', array('error' => ''));
  1395. $expected = array(
  1396. 'div' => array('class' => 'input password error'),
  1397. 'label' => array('for' => 'ContactPassword'),
  1398. 'Password',
  1399. '/label',
  1400. 'input' => array(
  1401. 'type' => 'password', 'name' => 'data[Contact][password]',
  1402. 'id' => 'ContactPassword', 'class' => 'form-error'
  1403. ),
  1404. array('div' => array('class' => 'error-message')),
  1405. array(),
  1406. '/div',
  1407. '/div'
  1408. );
  1409. $this->assertTags($result, $expected);
  1410. }
  1411. /**
  1412. * testFormValidationAssociated method
  1413. *
  1414. * test display of form errors in conjunction with model::validates.
  1415. *
  1416. * @return void
  1417. */
  1418. public function testFormValidationAssociated() {
  1419. $this->UserForm = ClassRegistry::getObject('UserForm');
  1420. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1421. $data = array(
  1422. 'UserForm' => array('name' => 'user'),
  1423. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1424. );
  1425. $result = $this->UserForm->OpenidUrl->create($data);
  1426. $this->assertFalse(empty($result));
  1427. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1428. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1429. $encoding = strtolower(Configure::read('App.encoding'));
  1430. $expected = array(
  1431. 'form' => array(
  1432. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1433. 'accept-charset' => $encoding
  1434. ),
  1435. 'div' => array('style' => 'display:none;'),
  1436. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1437. '/div'
  1438. );
  1439. $this->assertTags($result, $expected);
  1440. $result = $this->Form->error(
  1441. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1442. );
  1443. $this->assertEquals('Error, not registered', $result);
  1444. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1445. }
  1446. /**
  1447. * testFormValidationAssociatedFirstLevel method
  1448. *
  1449. * test form error display with associated model.
  1450. *
  1451. * @return void
  1452. */
  1453. public function testFormValidationAssociatedFirstLevel() {
  1454. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1455. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1456. $data = array(
  1457. 'ValidateUser' => array('name' => 'mariano'),
  1458. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1459. );
  1460. $result = $this->ValidateUser->create($data);
  1461. $this->assertFalse(empty($result));
  1462. $this->assertFalse($this->ValidateUser->validates());
  1463. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1464. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1465. $encoding = strtolower(Configure::read('App.encoding'));
  1466. $expected = array(
  1467. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1468. 'div' => array('style' => 'display:none;'),
  1469. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1470. '/div'
  1471. );
  1472. $this->assertTags($result, $expected);
  1473. $result = $this->Form->error(
  1474. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1475. );
  1476. $this->assertEquals('Invalid email', $result);
  1477. $result = $this->Form->error(
  1478. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1479. );
  1480. $this->assertEquals('Invalid name', $result);
  1481. $result = $this->Form->error(
  1482. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1483. );
  1484. $this->assertEquals('Invalid city', $result);
  1485. unset($this->ValidateUser->ValidateProfile);
  1486. unset($this->ValidateUser);
  1487. }
  1488. /**
  1489. * testFormValidationAssociatedSecondLevel method
  1490. *
  1491. * test form error display with associated model.
  1492. *
  1493. * @return void
  1494. */
  1495. public function testFormValidationAssociatedSecondLevel() {
  1496. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1497. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1498. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1499. $data = array(
  1500. 'ValidateUser' => array('name' => 'mariano'),
  1501. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1502. 'ValidateItem' => array('name' => 'Item')
  1503. );
  1504. $result = $this->ValidateUser->create($data);
  1505. $this->assertFalse(empty($result));
  1506. $this->assertFalse($this->ValidateUser->validates());
  1507. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1508. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1509. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1510. $encoding = strtolower(Configure::read('App.encoding'));
  1511. $expected = array(
  1512. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1513. 'div' => array('style' => 'display:none;'),
  1514. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1515. '/div'
  1516. );
  1517. $this->assertTags($result, $expected);
  1518. $result = $this->Form->error(
  1519. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1520. );
  1521. $this->assertEquals('Invalid email', $result);
  1522. $result = $this->Form->error(
  1523. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1524. );
  1525. $this->assertEquals('Invalid name', $result);
  1526. $result = $this->Form->error(
  1527. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1528. );
  1529. $result = $this->Form->error(
  1530. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1531. );
  1532. $this->assertEquals('Invalid description', $result);
  1533. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1534. unset($this->ValidateUser->ValidateProfile);
  1535. unset($this->ValidateUser);
  1536. }
  1537. /**
  1538. * testFormValidationMultiRecord method
  1539. *
  1540. * test form error display with multiple records.
  1541. *
  1542. * @return void
  1543. */
  1544. public function testFormValidationMultiRecord() {
  1545. $Contact = ClassRegistry::getObject('Contact');
  1546. $Contact->validationErrors[2] = array(
  1547. 'name' => array('This field cannot be left blank')
  1548. );
  1549. $result = $this->Form->input('Contact.2.name');
  1550. $expected = array(
  1551. 'div' => array('class' => 'input text error'),
  1552. 'label' => array('for' => 'Contact2Name'),
  1553. 'Name',
  1554. '/label',
  1555. 'input' => array(
  1556. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1557. 'class' => 'form-error', 'maxlength' => 255
  1558. ),
  1559. array('div' => array('class' => 'error-message')),
  1560. 'This field cannot be left blank',
  1561. '/div',
  1562. '/div'
  1563. );
  1564. $this->assertTags($result, $expected);
  1565. }
  1566. /**
  1567. * testMultipleInputValidation method
  1568. *
  1569. * test multiple record form validation error display.
  1570. *
  1571. * @return void
  1572. */
  1573. public function testMultipleInputValidation() {
  1574. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1575. $Address->validationErrors[0] = array(
  1576. 'title' => array('This field cannot be empty'),
  1577. 'first_name' => array('This field cannot be empty')
  1578. );
  1579. $Address->validationErrors[1] = array(
  1580. 'last_name' => array('You must have a last name')
  1581. );
  1582. $this->Form->create();
  1583. $result = $this->Form->input('Address.0.title');
  1584. $expected = array(
  1585. 'div' => array('class'),
  1586. 'label' => array('for'),
  1587. 'preg:/[^<]+/',
  1588. '/label',
  1589. 'input' => array(
  1590. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1591. ),
  1592. array('div' => array('class' => 'error-message')),
  1593. 'This field cannot be empty',
  1594. '/div',
  1595. '/div'
  1596. );
  1597. $this->assertTags($result, $expected);
  1598. $result = $this->Form->input('Address.0.first_name');
  1599. $expected = array(
  1600. 'div' => array('class'),
  1601. 'label' => array('for'),
  1602. 'preg:/[^<]+/',
  1603. '/label',
  1604. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1605. array('div' => array('class' => 'error-message')),
  1606. 'This field cannot be empty',
  1607. '/div',
  1608. '/div'
  1609. );
  1610. $this->assertTags($result, $expected);
  1611. $result = $this->Form->input('Address.0.last_name');
  1612. $expected = array(
  1613. 'div' => array('class'),
  1614. 'label' => array('for'),
  1615. 'preg:/[^<]+/',
  1616. '/label',
  1617. 'input' => array('type' => 'text', 'name', 'id'),
  1618. '/div'
  1619. );
  1620. $this->assertTags($result, $expected);
  1621. $result = $this->Form->input('Address.1.last_name');
  1622. $expected = array(
  1623. 'div' => array('class'),
  1624. 'label' => array('for'),
  1625. 'preg:/[^<]+/',
  1626. '/label',
  1627. 'input' => array(
  1628. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1629. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1630. ),
  1631. array('div' => array('class' => 'error-message')),
  1632. 'You must have a last name',
  1633. '/div',
  1634. '/div'
  1635. );
  1636. $this->assertTags($result, $expected);
  1637. }
  1638. /**
  1639. * testInput method
  1640. *
  1641. * Test various incarnations of input().
  1642. *
  1643. * @return void
  1644. */
  1645. public function testInput() {
  1646. $result = $this->Form->input('ValidateUser.balance');
  1647. $expected = array(
  1648. 'div' => array('class'),
  1649. 'label' => array('for'),
  1650. 'Balance',
  1651. '/label',
  1652. 'input' => array('name', 'type' => 'number', 'maxlength' => 8, 'id'),
  1653. '/div',
  1654. );
  1655. $this->assertTags($result, $expected);
  1656. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1657. $expected = array(
  1658. 'div' => array('class' => 'input text'),
  1659. 'label' => array('for' => 'custom'),
  1660. 'Email',
  1661. '/label',
  1662. array('input' => array(
  1663. 'type' => 'text', 'name' => 'data[Contact][email]',
  1664. 'id' => 'custom', 'maxlength' => 255
  1665. )),
  1666. '/div'
  1667. );
  1668. $this->assertTags($result, $expected);
  1669. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1670. $expected = array(
  1671. '<div',
  1672. 'label' => array('for' => 'ContactEmail'),
  1673. 'Email',
  1674. '/label',
  1675. array('input' => array(
  1676. 'type' => 'text', 'name' => 'data[Contact][email]',
  1677. 'id' => 'ContactEmail', 'maxlength' => 255
  1678. )),
  1679. '/div'
  1680. );
  1681. $this->assertTags($result, $expected);
  1682. $result = $this->Form->hidden('Contact.idontexist');
  1683. $expected = array('input' => array(
  1684. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1685. 'id' => 'ContactIdontexist'
  1686. ));
  1687. $this->assertTags($result, $expected);
  1688. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1689. $expected = array(
  1690. 'div' => array('class' => 'input text'),
  1691. 'label' => array('for' => 'ContactEmail'),
  1692. 'Email',
  1693. '/label',
  1694. array('input' => array(
  1695. 'type' => 'text', 'name' => 'data[Contact][email]',
  1696. 'id' => 'ContactEmail'
  1697. )),
  1698. '/div'
  1699. );
  1700. $this->assertTags($result, $expected);
  1701. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1702. $expected = array(
  1703. 'div' => array('class' => 'input text'),
  1704. 'label' => array('for' => 'Contact5Email'),
  1705. 'Email',
  1706. '/label',
  1707. array('input' => array(
  1708. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1709. 'id' => 'Contact5Email'
  1710. )),
  1711. '/div'
  1712. );
  1713. $this->assertTags($result, $expected);
  1714. $result = $this->Form->input('Contact.password');
  1715. $expected = array(
  1716. 'div' => array('class' => 'input password'),
  1717. 'label' => array('for' => 'ContactPassword'),
  1718. 'Password',
  1719. '/label',
  1720. array('input' => array(
  1721. 'type' => 'password', 'name' => 'data[Contact][password]',
  1722. 'id' => 'ContactPassword'
  1723. )),
  1724. '/div'
  1725. );
  1726. $this->assertTags($result, $expected);
  1727. $result = $this->Form->input('Contact.email', array(
  1728. 'type' => 'file', 'class' => 'textbox'
  1729. ));
  1730. $expected = array(
  1731. 'div' => array('class' => 'input file'),
  1732. 'label' => array('for' => 'ContactEmail'),
  1733. 'Email',
  1734. '/label',
  1735. array('input' => array(
  1736. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1737. 'id' => 'ContactEmail'
  1738. )),
  1739. '/div'
  1740. );
  1741. $this->assertTags($result, $expected);
  1742. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1743. $result = $this->Form->input('Contact.phone');
  1744. $expected = array(
  1745. 'div' => array('class' => 'input text'),
  1746. 'label' => array('for' => 'ContactPhone'),
  1747. 'Phone',
  1748. '/label',
  1749. array('input' => array(
  1750. 'type' => 'text', 'name' => 'data[Contact][phone]',
  1751. 'value' => 'Hello &amp; World &gt; weird chars',
  1752. 'id' => 'ContactPhone', 'maxlength' => 255
  1753. )),
  1754. '/div'
  1755. );
  1756. $this->assertTags($result, $expected);
  1757. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1758. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1759. $expected = array(
  1760. 'div' => array('class' => 'input text'),
  1761. 'label' => array('for' => 'myId'),
  1762. 'Field',
  1763. '/label',
  1764. 'input' => array(
  1765. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1766. 'value' => 'My value', 'id' => 'myId'
  1767. ),
  1768. '/div'
  1769. );
  1770. $this->assertTags($result, $expected);
  1771. unset($this->Form->request->data);
  1772. $Contact = ClassRegistry::getObject('Contact');
  1773. $Contact->validationErrors['field'] = array('Badness!');
  1774. $result = $this->Form->input('Contact.field');
  1775. $expected = array(
  1776. 'div' => array('class' => 'input text error'),
  1777. 'label' => array('for' => 'ContactField'),
  1778. 'Field',
  1779. '/label',
  1780. 'input' => array(
  1781. 'type' => 'text', 'name' => 'data[Contact][field]',
  1782. 'id' => 'ContactField', 'class' => 'form-error'
  1783. ),
  1784. array('div' => array('class' => 'error-message')),
  1785. 'Badness!',
  1786. '/div',
  1787. '/div'
  1788. );
  1789. $this->assertTags($result, $expected);
  1790. $result = $this->Form->input('Contact.field', array(
  1791. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1792. ));
  1793. $expected = array(
  1794. 'label' => array('for' => 'ContactField'),
  1795. 'Field',
  1796. '/label',
  1797. 'input' => array(
  1798. 'type' => 'text', 'name' => 'data[Contact][field]',
  1799. 'id' => 'ContactField', 'class' => 'form-error'
  1800. ),
  1801. array('span' => array('class' => 'error-message')),
  1802. 'Badness!',
  1803. '/span'
  1804. );
  1805. $this->assertTags($result, $expected);
  1806. $result = $this->Form->input('Contact.field', array(
  1807. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1808. ));
  1809. $expected = array(
  1810. 'div' => array('class' => 'input text error'),
  1811. 'label' => array('for' => 'ContactField'),
  1812. 'Field',
  1813. '/label',
  1814. 'input' => array(
  1815. 'type' => 'text', 'name' => 'data[Contact][field]',
  1816. 'id' => 'ContactField', 'class' => 'form-error'
  1817. ),
  1818. array('div' => array('class' => 'error')),
  1819. 'Badness!',
  1820. '/div'
  1821. );
  1822. $this->assertTags($result, $expected);
  1823. $result = $this->Form->input('Contact.field', array(
  1824. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1825. ));
  1826. $expected = array(
  1827. 'span' => array('class' => 'input text error'),
  1828. 'label' => array('for' => 'ContactField'),
  1829. 'Field',
  1830. '/label',
  1831. 'input' => array(
  1832. 'type' => 'text', 'name' => 'data[Contact][field]',
  1833. 'id' => 'ContactField', 'class' => 'form-error'
  1834. ),
  1835. 'Badness!',
  1836. '/span'
  1837. );
  1838. $this->assertTags($result, $expected);
  1839. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1840. $expected = array(
  1841. 'div' => array('class' => 'input text error'),
  1842. 'label' => array('for' => 'ContactField'),
  1843. 'Field',
  1844. '/label',
  1845. 'input' => array(
  1846. 'type' => 'text', 'name' => 'data[Contact][field]',
  1847. 'id' => 'ContactField', 'class' => 'form-error'
  1848. ),
  1849. 'A message to you, Rudy',
  1850. array('div' => array('class' => 'error-message')),
  1851. 'Badness!',
  1852. '/div',
  1853. '/div'
  1854. );
  1855. $this->assertTags($result, $expected);
  1856. $this->Form->setEntity(null);
  1857. $this->Form->setEntity('Contact.field');
  1858. $result = $this->Form->input('Contact.field', array(
  1859. 'after' => 'A message to you, Rudy', 'error' => false
  1860. ));
  1861. $expected = array(
  1862. 'div' => array('class' => 'input text'),
  1863. 'label' => array('for' => 'ContactField'),
  1864. 'Field',
  1865. '/label',
  1866. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1867. 'A message to you, Rudy',
  1868. '/div'
  1869. );
  1870. $this->assertTags($result, $expected);
  1871. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1872. $expected = array(
  1873. 'div' => array('class' => 'input text'),
  1874. 'label' => array('for' => 'ObjectField'),
  1875. 'Field',
  1876. '/label',
  1877. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1878. 'A message to you, Rudy',
  1879. '/div'
  1880. );
  1881. $this->assertTags($result, $expected);
  1882. $Contact->validationErrors['field'] = array('minLength');
  1883. $result = $this->Form->input('Contact.field', array(
  1884. 'error' => array(
  1885. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1886. 'maxLength' => 'login too large'
  1887. )
  1888. ));
  1889. $expected = array(
  1890. 'div' => array('class' => 'input text error'),
  1891. 'label' => array('for' => 'ContactField'),
  1892. 'Field',
  1893. '/label',
  1894. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1895. array('div' => array('class' => 'error-message')),
  1896. 'Le login doit contenir au moins 2 caractères',
  1897. '/div',
  1898. '/div'
  1899. );
  1900. $this->assertTags($result, $expected);
  1901. $Contact->validationErrors['field'] = array('maxLength');
  1902. $result = $this->Form->input('Contact.field', array(
  1903. 'error' => array(
  1904. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1905. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1906. 'maxLength' => 'login too large',
  1907. )
  1908. ));
  1909. $expected = array(
  1910. 'div' => array('class' => 'input text error'),
  1911. 'label' => array('for' => 'ContactField'),
  1912. 'Field',
  1913. '/label',
  1914. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1915. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1916. 'login too large',
  1917. '/span',
  1918. '/div'
  1919. );
  1920. $this->assertTags($result, $expected);
  1921. }
  1922. /**
  1923. * test input() with checkbox creation
  1924. *
  1925. * @return void
  1926. */
  1927. public function testInputCheckbox() {
  1928. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1929. $expected = array(
  1930. 'div' => array('class' => 'input checkbox'),
  1931. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1932. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1933. '/div'
  1934. );
  1935. $this->assertTags($result, $expected);
  1936. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1937. $expected = array(
  1938. 'div' => array('class' => 'input checkbox'),
  1939. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1940. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1941. '/div'
  1942. );
  1943. $this->assertTags($result, $expected);
  1944. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1945. $expected = array(
  1946. 'div' => array('class' => 'input checkbox'),
  1947. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1948. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1949. '/div'
  1950. );
  1951. $this->assertTags($result, $expected);
  1952. }
  1953. /**
  1954. * test form->input() with time types.
  1955. *
  1956. */
  1957. public function testInputTime() {
  1958. extract($this->dateRegex);
  1959. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1960. $result = explode(':', $result);
  1961. $this->assertRegExp('/option value="23"/', $result[0]);
  1962. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1963. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1964. $result = explode(':', $result);
  1965. $this->assertRegExp('/option value="23"/', $result[0]);
  1966. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1967. $result = $this->Form->input('Model.field', array(
  1968. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  1969. ));
  1970. $result = explode(':', $result);
  1971. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1972. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1973. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1974. $result = $this->Form->input('Model.field', array(
  1975. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  1976. ));
  1977. $result = explode(':', $result);
  1978. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1979. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1980. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1981. $result = $this->Form->input('prueba', array(
  1982. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  1983. 'maxYear' => date('Y') + 1 ,'interval' => 15
  1984. ));
  1985. $result = explode(':', $result);
  1986. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1987. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1988. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1989. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  1990. $result = $this->Form->input('Random.start_time', array(
  1991. 'type' => 'time',
  1992. 'selected' => '18:15'
  1993. ));
  1994. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  1995. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  1996. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  1997. }
  1998. /**
  1999. * Test interval + selected near the hour roll over.
  2000. *
  2001. * @return void
  2002. */
  2003. public function testTimeSelectedWithInterval() {
  2004. $result = $this->Form->input('Model.start_time', array(
  2005. 'type' => 'time',
  2006. 'interval' => 15,
  2007. 'selected' => '2012-10-23 15:57:00'
  2008. ));
  2009. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2010. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2011. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2012. $result = $this->Form->input('Model.start_time', array(
  2013. 'timeFormat' => 24,
  2014. 'type' => 'time',
  2015. 'interval' => 15,
  2016. 'selected' => '15:57'
  2017. ));
  2018. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2019. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2020. $result = $this->Form->input('Model.start_time', array(
  2021. 'timeFormat' => 24,
  2022. 'type' => 'time',
  2023. 'interval' => 15,
  2024. 'selected' => '23:57'
  2025. ));
  2026. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2027. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2028. $result = $this->Form->input('Model.created', array(
  2029. 'timeFormat' => 24,
  2030. 'type' => 'datetime',
  2031. 'interval' => 15,
  2032. 'selected' => '2012-09-30 23:56'
  2033. ));
  2034. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2035. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2036. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2037. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2038. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2039. }
  2040. /**
  2041. * test form->input() with datetime, date and time types
  2042. *
  2043. * @return void
  2044. */
  2045. public function testInputDatetime() {
  2046. extract($this->dateRegex);
  2047. $result = $this->Form->input('prueba', array(
  2048. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2049. 'maxYear' => date('Y') + 1, 'interval' => 15
  2050. ));
  2051. $result = explode(':', $result);
  2052. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2053. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2054. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2055. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2056. //related to ticket #5013
  2057. $result = $this->Form->input('Contact.date', array(
  2058. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2059. ));
  2060. $this->assertRegExp('/class="customClass"/', $result);
  2061. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2062. $result = $this->Form->input('Contact.date', array(
  2063. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2064. ));
  2065. $this->assertRegExp('/id="customIdDay"/', $result);
  2066. $this->assertRegExp('/id="customIdMonth"/', $result);
  2067. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2068. $result = $this->Form->input('Model.field', array(
  2069. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2070. ));
  2071. $this->assertRegExp('/id="customIDDay"/', $result);
  2072. $this->assertRegExp('/id="customIDHour"/', $result);
  2073. $result = explode('</select><select', $result);
  2074. $result = explode(':', $result[1]);
  2075. $this->assertRegExp('/option value="23"/', $result[0]);
  2076. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2077. $result = $this->Form->input('Model.field', array(
  2078. 'type' => 'datetime', 'timeFormat' => 12
  2079. ));
  2080. $result = explode('</select><select', $result);
  2081. $result = explode(':', $result[1]);
  2082. $this->assertRegExp('/option value="12"/', $result[0]);
  2083. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2084. $this->Form->request->data = array('Contact' => array('created' => null));
  2085. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2086. $expected = array(
  2087. 'div' => array('class' => 'input date'),
  2088. 'label' => array('for' => 'ContactCreatedMonth'),
  2089. 'Created',
  2090. '/label',
  2091. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2092. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2093. $monthsRegex,
  2094. '/select', '-',
  2095. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2096. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2097. $daysRegex,
  2098. '/select', '-',
  2099. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2100. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2101. $yearsRegex,
  2102. '/select',
  2103. '/div'
  2104. );
  2105. $this->assertTags($result, $expected);
  2106. $this->Form->request->data = array('Contact' => array('created' => null));
  2107. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2108. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2109. $this->Form->request->data = array('Contact' => array('created' => null));
  2110. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2111. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2112. $result = $this->Form->input('Contact.created', array(
  2113. 'type' => 'date',
  2114. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2115. 'timeFormat' => 'NONE'
  2116. ));
  2117. $this->assertRegExp('/for\="created-month"/', $result);
  2118. }
  2119. /**
  2120. * Test generating checkboxes in a loop.
  2121. *
  2122. * @return void
  2123. */
  2124. public function testInputCheckboxesInLoop() {
  2125. for ($i = 1; $i < 5; $i++) {
  2126. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2127. $expected = array(
  2128. 'div' => array('class' => 'input checkbox'),
  2129. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2130. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2131. 'label' => array('for' => "Contact{$i}Email"),
  2132. 'Email',
  2133. '/label',
  2134. '/div'
  2135. );
  2136. $this->assertTags($result, $expected);
  2137. }
  2138. }
  2139. /**
  2140. * test input name with leading integer, ensure attributes are generated correctly.
  2141. *
  2142. * @return void
  2143. */
  2144. public function testInputWithLeadingInteger() {
  2145. $result = $this->Form->text('0.Node.title');
  2146. $expected = array(
  2147. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2148. );
  2149. $this->assertTags($result, $expected);
  2150. }
  2151. /**
  2152. * test form->input() with select type inputs.
  2153. *
  2154. * @return void
  2155. */
  2156. public function testInputSelectType() {
  2157. $result = $this->Form->input('email', array(
  2158. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2159. );
  2160. $expected = array(
  2161. 'div' => array('class' => 'input select'),
  2162. 'label' => array('for' => 'email'),
  2163. 'Email',
  2164. '/label',
  2165. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2166. array('option' => array('value' => '')),
  2167. '/option',
  2168. array('option' => array('value' => 'è')),
  2169. 'Firést',
  2170. '/option',
  2171. array('option' => array('value' => 'é')),
  2172. 'Secoènd',
  2173. '/option',
  2174. '/select',
  2175. '/div'
  2176. );
  2177. $this->assertTags($result, $expected);
  2178. $result = $this->Form->input('email', array(
  2179. 'options' => array('First', 'Second'), 'empty' => true)
  2180. );
  2181. $expected = array(
  2182. 'div' => array('class' => 'input select'),
  2183. 'label' => array('for' => 'email'),
  2184. 'Email',
  2185. '/label',
  2186. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2187. array('option' => array('value' => '')),
  2188. '/option',
  2189. array('option' => array('value' => '0')),
  2190. 'First',
  2191. '/option',
  2192. array('option' => array('value' => '1')),
  2193. 'Second',
  2194. '/option',
  2195. '/select',
  2196. '/div'
  2197. );
  2198. $this->assertTags($result, $expected);
  2199. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2200. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2201. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2202. $expected = array(
  2203. 'div' => array('class' => 'input select'),
  2204. 'label' => array('for' => 'ModelUserId'),
  2205. 'User',
  2206. '/label',
  2207. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2208. array('option' => array('value' => '')),
  2209. '/option',
  2210. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2211. 'good',
  2212. '/option',
  2213. array('option' => array('value' => 'other')),
  2214. 'bad',
  2215. '/option',
  2216. '/select',
  2217. '/div'
  2218. );
  2219. $this->assertTags($result, $expected);
  2220. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2221. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2222. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2223. $expected = array(
  2224. 'div' => array('class' => 'input select'),
  2225. 'label' => array('for' => 'ThingUserId'),
  2226. 'User',
  2227. '/label',
  2228. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2229. array('option' => array('value' => '')),
  2230. 'Some Empty',
  2231. '/option',
  2232. array('option' => array('value' => 'value')),
  2233. 'good',
  2234. '/option',
  2235. array('option' => array('value' => 'other')),
  2236. 'bad',
  2237. '/option',
  2238. '/select',
  2239. '/div'
  2240. );
  2241. $this->assertTags($result, $expected);
  2242. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2243. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2244. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2245. $expected = array(
  2246. 'div' => array('class' => 'input select'),
  2247. 'label' => array('for' => 'ThingUserId'),
  2248. 'User',
  2249. '/label',
  2250. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2251. array('option' => array('value' => '')),
  2252. 'Some Empty',
  2253. '/option',
  2254. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2255. 'good',
  2256. '/option',
  2257. array('option' => array('value' => 'other')),
  2258. 'bad',
  2259. '/option',
  2260. '/select',
  2261. '/div'
  2262. );
  2263. $this->assertTags($result, $expected);
  2264. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2265. $this->Form->request->data = array('User' => array('User' => array('value')));
  2266. $result = $this->Form->input('User.User', array('empty' => true));
  2267. $expected = array(
  2268. 'div' => array('class' => 'input select'),
  2269. 'label' => array('for' => 'UserUser'),
  2270. 'User',
  2271. '/label',
  2272. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2273. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2274. array('option' => array('value' => '')),
  2275. '/option',
  2276. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2277. 'good',
  2278. '/option',
  2279. array('option' => array('value' => 'other')),
  2280. 'bad',
  2281. '/option',
  2282. '/select',
  2283. '/div'
  2284. );
  2285. $this->assertTags($result, $expected);
  2286. $this->Form->data = array();
  2287. $result = $this->Form->input('Publisher.id', array(
  2288. 'label' => 'Publisher',
  2289. 'type' => 'select',
  2290. 'multiple' => 'checkbox',
  2291. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2292. ));
  2293. $expected = array(
  2294. array('div' => array('class' => 'input select')),
  2295. array('label' => array('for' => 'PublisherId')),
  2296. 'Publisher',
  2297. '/label',
  2298. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2299. array('div' => array('class' => 'checkbox')),
  2300. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2301. array('label' => array('for' => 'PublisherIdValue1')),
  2302. 'Label 1',
  2303. '/label',
  2304. '/div',
  2305. array('div' => array('class' => 'checkbox')),
  2306. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2307. array('label' => array('for' => 'PublisherIdValue2')),
  2308. 'Label 2',
  2309. '/label',
  2310. '/div',
  2311. '/div'
  2312. );
  2313. $this->assertTags($result, $expected);
  2314. }
  2315. /**
  2316. * test that input() and a non standard primary key makes a hidden input by default.
  2317. *
  2318. * @return void
  2319. */
  2320. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2321. $this->Form->create('User');
  2322. $this->Form->fieldset = array(
  2323. 'User' => array(
  2324. 'fields' => array(
  2325. 'model_id' => array('type' => 'integer')
  2326. ),
  2327. 'validates' => array(),
  2328. 'key' => 'model_id'
  2329. )
  2330. );
  2331. $result = $this->Form->input('model_id');
  2332. $expected = array(
  2333. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2334. );
  2335. $this->assertTags($result, $expected);
  2336. }
  2337. /**
  2338. * test that overriding the magic select type widget is possible
  2339. *
  2340. * @return void
  2341. */
  2342. public function testInputOverridingMagicSelectType() {
  2343. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2344. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2345. $expected = array(
  2346. 'div' => array('class' => 'input text'),
  2347. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2348. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2349. '/div'
  2350. );
  2351. $this->assertTags($result, $expected);
  2352. //Check that magic types still work for plural/singular vars
  2353. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2354. $result = $this->Form->input('Model.type');
  2355. $expected = array(
  2356. 'div' => array('class' => 'input select'),
  2357. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2358. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2359. array('option' => array('value' => 'value')), 'good', '/option',
  2360. array('option' => array('value' => 'other')), 'bad', '/option',
  2361. '/select',
  2362. '/div'
  2363. );
  2364. $this->assertTags($result, $expected);
  2365. }
  2366. /**
  2367. * Test that magic input() selects are created for type=number
  2368. *
  2369. * @return void
  2370. */
  2371. public function testInputMagicSelectForTypeNumber() {
  2372. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2373. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2374. $result = $this->Form->input('ValidateUser.balance');
  2375. $expected = array(
  2376. 'div' => array('class' => 'input select'),
  2377. 'label' => array('for' => 'ValidateUserBalance'),
  2378. 'Balance',
  2379. '/label',
  2380. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2381. array('option' => array('value' => '0')),
  2382. 'nothing',
  2383. '/option',
  2384. array('option' => array('value' => '1', 'selected' => 'selected')),
  2385. 'some',
  2386. '/option',
  2387. array('option' => array('value' => '100')),
  2388. 'a lot',
  2389. '/option',
  2390. '/select',
  2391. '/div'
  2392. );
  2393. $this->assertTags($result, $expected);
  2394. }
  2395. /**
  2396. * Test that magic input() selects can easily be converted into radio types without error.
  2397. *
  2398. * @return void
  2399. */
  2400. public function testInputMagicSelectChangeToRadio() {
  2401. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2402. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2403. $this->assertRegExp('/input type="radio"/', $result);
  2404. }
  2405. /**
  2406. * fields with the same name as the model should work.
  2407. *
  2408. * @return void
  2409. */
  2410. public function testInputWithMatchingFieldAndModelName() {
  2411. $this->Form->create('User');
  2412. $this->Form->fieldset = array(
  2413. 'User' => array(
  2414. 'fields' => array(
  2415. 'User' => array('type' => 'text')
  2416. ),
  2417. 'validates' => array(),
  2418. 'key' => 'id'
  2419. )
  2420. );
  2421. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2422. $result = $this->Form->input('User', array('type' => 'text'));
  2423. $expected = array(
  2424. 'div' => array('class' => 'input text'),
  2425. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2426. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2427. '/div'
  2428. );
  2429. $this->assertTags($result, $expected);
  2430. }
  2431. /**
  2432. * testFormInputs method
  2433. *
  2434. * test correct results from form::inputs().
  2435. *
  2436. * @return void
  2437. */
  2438. public function testFormInputs() {
  2439. $this->Form->create('Contact');
  2440. $result = $this->Form->inputs('The Legend');
  2441. $expected = array(
  2442. '<fieldset',
  2443. '<legend',
  2444. 'The Legend',
  2445. '/legend',
  2446. '*/fieldset',
  2447. );
  2448. $this->assertTags($result, $expected);
  2449. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2450. $expected = array(
  2451. 'fieldset' => array('class' => 'classy-stuff'),
  2452. '<legend',
  2453. 'Field of Dreams',
  2454. '/legend',
  2455. '*/fieldset'
  2456. );
  2457. $this->assertTags($result, $expected);
  2458. $this->Form->create('Contact');
  2459. $this->Form->request['prefix'] = 'admin';
  2460. $this->Form->request['action'] = 'admin_edit';
  2461. $result = $this->Form->inputs();
  2462. $expected = array(
  2463. '<fieldset',
  2464. '<legend',
  2465. 'Edit Contact',
  2466. '/legend',
  2467. '*/fieldset',
  2468. );
  2469. $this->assertTags($result, $expected);
  2470. $this->Form->create('Contact');
  2471. $result = $this->Form->inputs(false);
  2472. $expected = array(
  2473. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2474. array('div' => array('class' => 'input text')),
  2475. '*/div',
  2476. array('div' => array('class' => 'input text')),
  2477. '*/div',
  2478. array('div' => array('class' => 'input text')),
  2479. '*/div',
  2480. array('div' => array('class' => 'input password')),
  2481. '*/div',
  2482. array('div' => array('class' => 'input date')),
  2483. '*/div',
  2484. array('div' => array('class' => 'input date')),
  2485. '*/div',
  2486. array('div' => array('class' => 'input datetime')),
  2487. '*/div',
  2488. array('div' => array('class' => 'input number')),
  2489. '*/div',
  2490. array('div' => array('class' => 'input select')),
  2491. '*/div',
  2492. );
  2493. $this->assertTags($result, $expected);
  2494. $this->Form->create('Contact');
  2495. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2496. $expected = array(
  2497. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2498. array('div' => array('class' => 'input text')),
  2499. '*/div',
  2500. array('div' => array('class' => 'input text')),
  2501. '*/div',
  2502. array('div' => array('class' => 'input text')),
  2503. '*/div',
  2504. array('div' => array('class' => 'input password')),
  2505. '*/div',
  2506. array('div' => array('class' => 'input date')),
  2507. '*/div',
  2508. array('div' => array('class' => 'input date')),
  2509. '*/div',
  2510. array('div' => array('class' => 'input datetime')),
  2511. '*/div',
  2512. array('div' => array('class' => 'input number')),
  2513. '*/div',
  2514. array('div' => array('class' => 'input select')),
  2515. '*/div',
  2516. );
  2517. $this->assertTags($result, $expected);
  2518. $this->Form->create('Contact');
  2519. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2520. $expected = array(
  2521. 'fieldset' => array(),
  2522. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2523. array('div' => array('class' => 'input text')),
  2524. '*/div',
  2525. array('div' => array('class' => 'input text')),
  2526. '*/div',
  2527. array('div' => array('class' => 'input text')),
  2528. '*/div',
  2529. array('div' => array('class' => 'input password')),
  2530. '*/div',
  2531. array('div' => array('class' => 'input date')),
  2532. '*/div',
  2533. array('div' => array('class' => 'input date')),
  2534. '*/div',
  2535. array('div' => array('class' => 'input datetime')),
  2536. '*/div',
  2537. array('div' => array('class' => 'input number')),
  2538. '*/div',
  2539. array('div' => array('class' => 'input select')),
  2540. '*/div',
  2541. '/fieldset'
  2542. );
  2543. $this->assertTags($result, $expected);
  2544. $this->Form->create('Contact');
  2545. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2546. $expected = array(
  2547. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2548. array('div' => array('class' => 'input text')),
  2549. '*/div',
  2550. array('div' => array('class' => 'input text')),
  2551. '*/div',
  2552. array('div' => array('class' => 'input text')),
  2553. '*/div',
  2554. array('div' => array('class' => 'input password')),
  2555. '*/div',
  2556. array('div' => array('class' => 'input date')),
  2557. '*/div',
  2558. array('div' => array('class' => 'input date')),
  2559. '*/div',
  2560. array('div' => array('class' => 'input datetime')),
  2561. '*/div',
  2562. array('div' => array('class' => 'input number')),
  2563. '*/div',
  2564. array('div' => array('class' => 'input select')),
  2565. '*/div',
  2566. );
  2567. $this->assertTags($result, $expected);
  2568. $this->Form->create('Contact');
  2569. $result = $this->Form->inputs('Hello');
  2570. $expected = array(
  2571. 'fieldset' => array(),
  2572. 'legend' => array(),
  2573. 'Hello',
  2574. '/legend',
  2575. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2576. array('div' => array('class' => 'input text')),
  2577. '*/div',
  2578. array('div' => array('class' => 'input text')),
  2579. '*/div',
  2580. array('div' => array('class' => 'input text')),
  2581. '*/div',
  2582. array('div' => array('class' => 'input password')),
  2583. '*/div',
  2584. array('div' => array('class' => 'input date')),
  2585. '*/div',
  2586. array('div' => array('class' => 'input date')),
  2587. '*/div',
  2588. array('div' => array('class' => 'input datetime')),
  2589. '*/div',
  2590. array('div' => array('class' => 'input number')),
  2591. '*/div',
  2592. array('div' => array('class' => 'input select')),
  2593. '*/div',
  2594. '/fieldset'
  2595. );
  2596. $this->assertTags($result, $expected);
  2597. $this->Form->create('Contact');
  2598. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2599. $expected = array(
  2600. 'fieldset' => array(),
  2601. 'legend' => array(),
  2602. 'Hello',
  2603. '/legend',
  2604. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2605. array('div' => array('class' => 'input text')),
  2606. '*/div',
  2607. array('div' => array('class' => 'input text')),
  2608. '*/div',
  2609. array('div' => array('class' => 'input text')),
  2610. '*/div',
  2611. array('div' => array('class' => 'input password')),
  2612. '*/div',
  2613. array('div' => array('class' => 'input date')),
  2614. '*/div',
  2615. array('div' => array('class' => 'input date')),
  2616. '*/div',
  2617. array('div' => array('class' => 'input datetime')),
  2618. '*/div',
  2619. array('div' => array('class' => 'input number')),
  2620. '*/div',
  2621. array('div' => array('class' => 'input select')),
  2622. '*/div',
  2623. '/fieldset'
  2624. );
  2625. $this->assertTags($result, $expected);
  2626. }
  2627. /**
  2628. * testSelectAsCheckbox method
  2629. *
  2630. * test multi-select widget with checkbox formatting.
  2631. *
  2632. * @return void
  2633. */
  2634. public function testSelectAsCheckbox() {
  2635. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2636. $expected = array(
  2637. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2638. array('div' => array('class' => 'checkbox')),
  2639. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2640. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2641. 'first',
  2642. '/label',
  2643. '/div',
  2644. array('div' => array('class' => 'checkbox')),
  2645. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2646. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2647. 'second',
  2648. '/label',
  2649. '/div',
  2650. array('div' => array('class' => 'checkbox')),
  2651. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2652. array('label' => array('for' => 'ModelMultiField2')),
  2653. 'third',
  2654. '/label',
  2655. '/div',
  2656. );
  2657. $this->assertTags($result, $expected);
  2658. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2659. $expected = array(
  2660. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2661. array('div' => array('class' => 'checkbox')),
  2662. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2663. array('label' => array('for' => 'ModelMultiField12')),
  2664. 'half',
  2665. '/label',
  2666. '/div',
  2667. );
  2668. $this->assertTags($result, $expected);
  2669. }
  2670. /**
  2671. * testLabel method
  2672. *
  2673. * test label generation.
  2674. *
  2675. * @return void
  2676. */
  2677. public function testLabel() {
  2678. $this->Form->text('Person.name');
  2679. $result = $this->Form->label();
  2680. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2681. $this->Form->text('Person.name');
  2682. $result = $this->Form->label();
  2683. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2684. $result = $this->Form->label('Person.first_name');
  2685. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2686. $result = $this->Form->label('Person.first_name', 'Your first name');
  2687. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2688. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2689. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2690. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2691. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2692. $result = $this->Form->label('Person.first_name', '');
  2693. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2694. $result = $this->Form->label('Person.2.name', '');
  2695. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2696. }
  2697. /**
  2698. * testTextbox method
  2699. *
  2700. * test textbox element generation
  2701. *
  2702. * @return void
  2703. */
  2704. public function testTextbox() {
  2705. $result = $this->Form->text('Model.field');
  2706. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2707. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2708. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2709. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2710. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2711. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2712. $result = $this->Form->text('Model.text');
  2713. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2714. $Contact = ClassRegistry::getObject('Contact');
  2715. $Contact->validationErrors['text'] = array(true);
  2716. $this->Form->request->data['Contact']['text'] = 'test';
  2717. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2718. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2719. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2720. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2721. $expected = array(
  2722. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2723. );
  2724. $this->assertTags($result, $expected);
  2725. }
  2726. /**
  2727. * testDefaultValue method
  2728. *
  2729. * Test default value setting
  2730. *
  2731. * @return void
  2732. */
  2733. public function testDefaultValue() {
  2734. $this->Form->request->data['Model']['field'] = 'test';
  2735. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2736. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  2737. unset($this->Form->request->data['Model']['field']);
  2738. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2739. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  2740. }
  2741. /**
  2742. * testCheckboxDefaultValue method
  2743. *
  2744. * Test default value setting on checkbox() method
  2745. *
  2746. * @return void
  2747. */
  2748. public function testCheckboxDefaultValue() {
  2749. $this->Form->request->data['Model']['field'] = false;
  2750. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2751. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2752. unset($this->Form->request->data['Model']['field']);
  2753. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2754. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2755. $this->Form->request->data['Model']['field'] = true;
  2756. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2757. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2758. unset($this->Form->request->data['Model']['field']);
  2759. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2760. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2761. }
  2762. /**
  2763. * testError method
  2764. *
  2765. * Test field error generation
  2766. *
  2767. * @return void
  2768. */
  2769. public function testError() {
  2770. $Contact = ClassRegistry::getObject('Contact');
  2771. $Contact->validationErrors['field'] = array(1);
  2772. $result = $this->Form->error('Contact.field');
  2773. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  2774. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2775. $this->assertEquals('Error in field Field', $result);
  2776. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2777. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2778. $this->assertEquals('This field contains invalid input', $result);
  2779. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2780. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  2781. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  2782. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  2783. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  2784. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  2785. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2786. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  2787. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2788. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  2789. $this->assertEquals('<strong>Badness!</strong>', $result);
  2790. $Contact->validationErrors['field'] = array("email");
  2791. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  2792. $expected = array(
  2793. 'div' => array('class' => 'field-error'),
  2794. 'No good!',
  2795. '/div'
  2796. );
  2797. $this->assertTags($result, $expected);
  2798. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  2799. $result = $this->Form->error('Contact.field', array(
  2800. 'notEmpty' => 'Cannot be empty',
  2801. 'email' => 'No good!'
  2802. ));
  2803. $expected = array(
  2804. 'div' => array('class' => 'error-message'),
  2805. 'ul' => array(),
  2806. '<li', 'Cannot be empty', '/li',
  2807. '<li', 'No good!', '/li',
  2808. '<li', 'Something else', '/li',
  2809. '/ul',
  2810. '/div'
  2811. );
  2812. $this->assertTags($result, $expected);
  2813. /** Testing error messages list options **/
  2814. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  2815. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  2816. $expected = array(
  2817. 'div' => array('class' => 'error-message'),
  2818. 'ol' => array(),
  2819. '<li', 'notEmpty', '/li',
  2820. '<li', 'email', '/li',
  2821. '/ol',
  2822. '/div'
  2823. );
  2824. $this->assertTags($result, $expected);
  2825. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  2826. $expected = array(
  2827. 'div' => array('class' => 'error-message'),
  2828. 'ol' => array(),
  2829. '<li', 'notEmpty', '/li',
  2830. '<li', 'email', '/li',
  2831. '/ol',
  2832. '/div'
  2833. );
  2834. $this->assertTags($result, $expected);
  2835. $result = $this->Form->error('Contact.field', null, array(
  2836. 'listOptions' => array(
  2837. 'class' => 'ul-class',
  2838. 'itemOptions' => array(
  2839. 'class' => 'li-class'
  2840. )
  2841. )
  2842. ));
  2843. $expected = array(
  2844. 'div' => array('class' => 'error-message'),
  2845. 'ul' => array('class' => 'ul-class'),
  2846. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  2847. array('li' => array('class' => 'li-class')), 'email', '/li',
  2848. '/ul',
  2849. '/div'
  2850. );
  2851. $this->assertTags($result, $expected);
  2852. }
  2853. /**
  2854. * test error options when using form->input();
  2855. *
  2856. * @return void
  2857. */
  2858. public function testInputErrorEscape() {
  2859. $this->Form->create('ValidateProfile');
  2860. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  2861. $ValidateProfile->validationErrors['city'] = array('required<br>');
  2862. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
  2863. $this->assertRegExp('/required&lt;br&gt;/', $result);
  2864. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => false))));
  2865. $this->assertRegExp('/required<br>/', $result);
  2866. }
  2867. /**
  2868. * testPassword method
  2869. *
  2870. * Test password element generation
  2871. *
  2872. * @return void
  2873. */
  2874. public function testPassword() {
  2875. $Contact = ClassRegistry::getObject('Contact');
  2876. $result = $this->Form->password('Contact.field');
  2877. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  2878. $Contact->validationErrors['passwd'] = 1;
  2879. $this->Form->request->data['Contact']['passwd'] = 'test';
  2880. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  2881. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2882. }
  2883. /**
  2884. * testRadio method
  2885. *
  2886. * Test radio element set generation
  2887. *
  2888. * @return void
  2889. */
  2890. public function testRadio() {
  2891. $result = $this->Form->radio('Model.field', array('option A'));
  2892. $expected = array(
  2893. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2894. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2895. 'label' => array('for' => 'ModelField0'),
  2896. 'option A',
  2897. '/label'
  2898. );
  2899. $this->assertTags($result, $expected);
  2900. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  2901. $expected = array(
  2902. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2903. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  2904. 'label' => array('for' => 'ModelField12'),
  2905. 'half',
  2906. '/label'
  2907. );
  2908. $this->assertTags($result, $expected);
  2909. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  2910. $expected = array(
  2911. 'fieldset' => array(),
  2912. 'legend' => array(),
  2913. 'Field',
  2914. '/legend',
  2915. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2916. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2917. array('label' => array('for' => 'ModelField0')),
  2918. 'option A',
  2919. '/label',
  2920. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2921. array('label' => array('for' => 'ModelField1')),
  2922. 'option B',
  2923. '/label',
  2924. '/fieldset'
  2925. );
  2926. $this->assertTags($result, $expected);
  2927. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  2928. $expected = array(
  2929. 'fieldset' => array(),
  2930. 'legend' => array(),
  2931. 'Field',
  2932. '/legend',
  2933. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2934. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2935. array('label' => array('for' => 'ModelField0')),
  2936. 'option A',
  2937. '/label',
  2938. 'br' => array(),
  2939. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2940. array('label' => array('for' => 'ModelField1')),
  2941. 'option B',
  2942. '/label',
  2943. '/fieldset'
  2944. );
  2945. $this->assertTags($result, $expected);
  2946. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2947. $expected = array(
  2948. 'div' => array('class' => 'input radio'),
  2949. 'fieldset' => array(),
  2950. 'legend' => array(),
  2951. 'Legend title',
  2952. '/legend',
  2953. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2954. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2955. array('label' => array('for' => 'NewsletterSubscribe0')),
  2956. 'Unsubscribe',
  2957. '/label',
  2958. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2959. array('label' => array('for' => 'NewsletterSubscribe1')),
  2960. 'Subscribe',
  2961. '/label',
  2962. '/fieldset',
  2963. '/div'
  2964. );
  2965. $this->assertTags($result, $expected);
  2966. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2967. $expected = array(
  2968. 'div' => array('class' => 'input radio'),
  2969. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2970. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2971. array('label' => array('for' => 'NewsletterSubscribe0')),
  2972. 'Unsubscribe',
  2973. '/label',
  2974. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2975. array('label' => array('for' => 'NewsletterSubscribe1')),
  2976. 'Subscribe',
  2977. '/label',
  2978. '/div'
  2979. );
  2980. $this->assertTags($result, $expected);
  2981. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2982. $expected = array(
  2983. 'div' => array('class' => 'input radio'),
  2984. 'fieldset' => array(),
  2985. 'legend' => array(),
  2986. 'Legend title',
  2987. '/legend',
  2988. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2989. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2990. 'Unsubscribe',
  2991. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2992. 'Subscribe',
  2993. '/fieldset',
  2994. '/div'
  2995. );
  2996. $this->assertTags($result, $expected);
  2997. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2998. $expected = array(
  2999. 'div' => array('class' => 'input radio'),
  3000. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3001. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3002. 'Unsubscribe',
  3003. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3004. 'Subscribe',
  3005. '/div'
  3006. );
  3007. $this->assertTags($result, $expected);
  3008. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'value' => '1', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3009. $expected = array(
  3010. 'div' => array('class' => 'input radio'),
  3011. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3012. 'Unsubscribe',
  3013. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1', 'checked' => 'checked')),
  3014. 'Subscribe',
  3015. '/div'
  3016. );
  3017. $this->assertTags($result, $expected);
  3018. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3019. $expected = array(
  3020. 'fieldset' => array(),
  3021. 'legend' => array(),
  3022. 'Gender',
  3023. '/legend',
  3024. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3025. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3026. array('label' => array('for' => 'EmployeeGenderMale')),
  3027. 'Male',
  3028. '/label',
  3029. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3030. array('label' => array('for' => 'EmployeeGenderFemale')),
  3031. 'Female',
  3032. '/label',
  3033. '/fieldset',
  3034. );
  3035. $this->assertTags($result, $expected);
  3036. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3037. $expected = array(
  3038. 'fieldset' => array(),
  3039. 'legend' => array(),
  3040. 'Gender',
  3041. '/legend',
  3042. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3043. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3044. array('label' => array('for' => 'OfficerGenderMale')),
  3045. 'Male',
  3046. '/label',
  3047. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3048. array('label' => array('for' => 'OfficerGenderFemale')),
  3049. 'Female',
  3050. '/label',
  3051. '/fieldset',
  3052. );
  3053. $this->assertTags($result, $expected);
  3054. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3055. $expected = array(
  3056. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3057. array('input' => array('type' => 'radio', 'name' => 'data[Contact][1][imrequired]', 'value' => '0', 'id' => 'Contact1Imrequired0')),
  3058. 'label' => array('for' => 'Contact1Imrequired0'),
  3059. 'option A',
  3060. '/label'
  3061. );
  3062. $this->assertTags($result, $expected);
  3063. $result = $this->Form->radio('Model.1.field', array('option A'));
  3064. $expected = array(
  3065. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3066. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3067. 'label' => array('for' => 'Model1Field0'),
  3068. 'option A',
  3069. '/label'
  3070. );
  3071. $this->assertTags($result, $expected);
  3072. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3073. $expected = array(
  3074. 'fieldset' => array(),
  3075. 'legend' => array(),
  3076. 'Field',
  3077. '/legend',
  3078. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3079. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3080. array('label' => array('for' => 'ModelField0')),
  3081. 'option A',
  3082. '/label',
  3083. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3084. array('label' => array('for' => 'ModelField1')),
  3085. 'option B',
  3086. '/label',
  3087. '/fieldset'
  3088. );
  3089. $this->assertTags($result, $expected);
  3090. $result = $this->Form->radio(
  3091. 'Model.field',
  3092. array('option A', 'option B'),
  3093. array('between' => 'I am between')
  3094. );
  3095. $expected = array(
  3096. 'fieldset' => array(),
  3097. 'legend' => array(),
  3098. 'Field',
  3099. '/legend',
  3100. 'I am between',
  3101. 'input' => array(
  3102. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3103. 'value' => '', 'id' => 'ModelField_'
  3104. ),
  3105. array('input' => array(
  3106. 'type' => 'radio', 'name' => 'data[Model][field]',
  3107. 'value' => '0', 'id' => 'ModelField0'
  3108. )),
  3109. array('label' => array('for' => 'ModelField0')),
  3110. 'option A',
  3111. '/label',
  3112. array('input' => array(
  3113. 'type' => 'radio', 'name' => 'data[Model][field]',
  3114. 'value' => '1', 'id' => 'ModelField1'
  3115. )),
  3116. array('label' => array('for' => 'ModelField1')),
  3117. 'option B',
  3118. '/label',
  3119. '/fieldset'
  3120. );
  3121. $this->assertTags($result, $expected);
  3122. }
  3123. /**
  3124. * Test that radios with a 0 value are selected under the correct conditions.
  3125. *
  3126. * @return void
  3127. */
  3128. public function testRadioOptionWithZeroValue() {
  3129. $expected = array(
  3130. 'fieldset' => array(),
  3131. 'legend' => array(),
  3132. 'Field',
  3133. '/legend',
  3134. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3135. array('label' => array('for' => 'ModelField1')),
  3136. 'Yes',
  3137. '/label',
  3138. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3139. array('label' => array('for' => 'ModelField0')),
  3140. 'No',
  3141. '/label',
  3142. '/fieldset'
  3143. );
  3144. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3145. $this->assertTags($result, $expected);
  3146. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3147. $this->assertTags($result, $expected);
  3148. $expected = array(
  3149. 'fieldset' => array(),
  3150. 'legend' => array(),
  3151. 'Field',
  3152. '/legend',
  3153. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3154. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3155. array('label' => array('for' => 'ModelField1')),
  3156. 'Yes',
  3157. '/label',
  3158. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3159. array('label' => array('for' => 'ModelField0')),
  3160. 'No',
  3161. '/label',
  3162. '/fieldset'
  3163. );
  3164. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3165. $this->assertTags($result, $expected);
  3166. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3167. $this->assertTags($result, $expected);
  3168. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3169. $this->assertTags($result, $expected);
  3170. }
  3171. /**
  3172. * test disabled radio options
  3173. *
  3174. * @return void
  3175. */
  3176. public function testRadioDisabled() {
  3177. $result = $this->Form->radio(
  3178. 'Model.field',
  3179. array('option A', 'option B'),
  3180. array('disabled' => array('option A'), 'value' => '0')
  3181. );
  3182. $expected = array(
  3183. 'fieldset' => array(),
  3184. 'legend' => array(),
  3185. 'Field',
  3186. '/legend',
  3187. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3188. array('label' => array('for' => 'ModelField0')),
  3189. 'option A',
  3190. '/label',
  3191. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3192. array('label' => array('for' => 'ModelField1')),
  3193. 'option B',
  3194. '/label',
  3195. '/fieldset'
  3196. );
  3197. $this->assertTags($result, $expected);
  3198. $result = $this->Form->radio(
  3199. 'Model.field',
  3200. array('option A', 'option B'),
  3201. array('disabled' => true, 'value' => '0')
  3202. );
  3203. $expected = array(
  3204. 'fieldset' => array(),
  3205. 'legend' => array(),
  3206. 'Field',
  3207. '/legend',
  3208. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3209. array('label' => array('for' => 'ModelField0')),
  3210. 'option A',
  3211. '/label',
  3212. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3213. array('label' => array('for' => 'ModelField1')),
  3214. 'option B',
  3215. '/label',
  3216. '/fieldset'
  3217. );
  3218. $this->assertTags($result, $expected);
  3219. $result = $this->Form->radio(
  3220. 'Model.field',
  3221. array('option A', 'option B'),
  3222. array('disabled' => 'disabled', 'value' => '0')
  3223. );
  3224. $expected = array(
  3225. 'fieldset' => array(),
  3226. 'legend' => array(),
  3227. 'Field',
  3228. '/legend',
  3229. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3230. array('label' => array('for' => 'ModelField0')),
  3231. 'option A',
  3232. '/label',
  3233. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3234. array('label' => array('for' => 'ModelField1')),
  3235. 'option B',
  3236. '/label',
  3237. '/fieldset'
  3238. );
  3239. $this->assertTags($result, $expected);
  3240. }
  3241. /**
  3242. * test disabling the hidden input for radio buttons
  3243. *
  3244. * @return void
  3245. */
  3246. public function testRadioHiddenInputDisabling() {
  3247. $result = $this->Form->input('Model.1.field', array(
  3248. 'type' => 'radio',
  3249. 'options' => array('option A'),
  3250. 'hiddenField' => false
  3251. )
  3252. );
  3253. $expected = array(
  3254. 'div' => array('class' => 'input radio'),
  3255. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3256. 'label' => array('for' => 'Model1Field0'),
  3257. 'option A',
  3258. '/label',
  3259. '/div'
  3260. );
  3261. $this->assertTags($result, $expected);
  3262. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3263. $expected = array(
  3264. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3265. 'label' => array('for' => 'Model1Field0'),
  3266. 'option A',
  3267. '/label'
  3268. );
  3269. $this->assertTags($result, $expected);
  3270. }
  3271. /**
  3272. * test adding an empty option for radio buttons
  3273. *
  3274. * @return void
  3275. */
  3276. public function testRadioAddEmptyOption() {
  3277. $result = $this->Form->input('Model.1.field', array(
  3278. 'type' => 'radio',
  3279. 'options' => array('option A'),
  3280. 'empty' => true,
  3281. 'hiddenField' => false
  3282. ));
  3283. $expected = array(
  3284. 'div' => array('class' => 'input radio'),
  3285. 'fieldset' => array(),
  3286. 'legend' => array(),
  3287. 'Field',
  3288. '/legend',
  3289. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3290. array('label' => array('for' => 'Model1Field')),
  3291. __('empty'),
  3292. '/label',
  3293. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3294. array('label' => array('for' => 'Model1Field0')),
  3295. 'option A',
  3296. '/label',
  3297. '/fieldset',
  3298. '/div'
  3299. );
  3300. $this->assertTags($result, $expected);
  3301. $result = $this->Form->input('Model.1.field', array(
  3302. 'type' => 'radio',
  3303. 'options' => array('option A'),
  3304. 'empty' => 'CustomEmptyLabel',
  3305. 'hiddenField' => false
  3306. ));
  3307. $expected = array(
  3308. 'div' => array('class' => 'input radio'),
  3309. 'fieldset' => array(),
  3310. 'legend' => array(),
  3311. 'Field',
  3312. '/legend',
  3313. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3314. array('label' => array('for' => 'Model1Field')),
  3315. 'CustomEmptyLabel',
  3316. '/label',
  3317. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3318. array('label' => array('for' => 'Model1Field0')),
  3319. 'option A',
  3320. '/label',
  3321. '/fieldset',
  3322. '/div'
  3323. );
  3324. $this->assertTags($result, $expected);
  3325. $result = $this->Form->input('Model.1.field', array(
  3326. 'type' => 'radio',
  3327. 'options' => array('option A'),
  3328. 'empty' => false,
  3329. 'hiddenField' => false
  3330. ));
  3331. $this->assertTextNotContains('"Model1Field"', $result);
  3332. }
  3333. /**
  3334. * testSelect method
  3335. *
  3336. * Test select element generation.
  3337. *
  3338. * @return void
  3339. */
  3340. public function testSelect() {
  3341. $result = $this->Form->select('Model.field', array());
  3342. $expected = array(
  3343. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3344. array('option' => array('value' => '')),
  3345. '/option',
  3346. '/select'
  3347. );
  3348. $this->assertTags($result, $expected);
  3349. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3350. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3351. $expected = array(
  3352. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3353. array('option' => array('value' => '')),
  3354. '/option',
  3355. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3356. 'good',
  3357. '/option',
  3358. array('option' => array('value' => 'other')),
  3359. 'bad',
  3360. '/option',
  3361. '/select'
  3362. );
  3363. $this->assertTags($result, $expected);
  3364. $this->Form->request->data = array();
  3365. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3366. $expected = array(
  3367. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3368. array('option' => array('value' => '')),
  3369. '/option',
  3370. array('option' => array('value' => 'value')),
  3371. 'good',
  3372. '/option',
  3373. array('option' => array('value' => 'other')),
  3374. 'bad',
  3375. '/option',
  3376. '/select'
  3377. );
  3378. $this->assertTags($result, $expected);
  3379. $result = $this->Form->select(
  3380. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3381. array('empty' => false)
  3382. );
  3383. $expected = array(
  3384. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3385. array('option' => array('value' => 'first')),
  3386. 'first &quot;html&quot; &lt;chars&gt;',
  3387. '/option',
  3388. array('option' => array('value' => 'second')),
  3389. 'value',
  3390. '/option',
  3391. '/select'
  3392. );
  3393. $this->assertTags($result, $expected);
  3394. $result = $this->Form->select(
  3395. 'Model.field',
  3396. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3397. array('escape' => false, 'empty' => false)
  3398. );
  3399. $expected = array(
  3400. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3401. array('option' => array('value' => 'first')),
  3402. 'first "html" <chars>',
  3403. '/option',
  3404. array('option' => array('value' => 'second')),
  3405. 'value',
  3406. '/option',
  3407. '/select'
  3408. );
  3409. $this->assertTags($result, $expected);
  3410. $options = array(
  3411. array('value' => 'first', 'name' => 'First'),
  3412. array('value' => 'first', 'name' => 'Another First'),
  3413. );
  3414. $result = $this->Form->select(
  3415. 'Model.field',
  3416. $options,
  3417. array('escape' => false, 'empty' => false)
  3418. );
  3419. $expected = array(
  3420. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3421. array('option' => array('value' => 'first')),
  3422. 'First',
  3423. '/option',
  3424. array('option' => array('value' => 'first')),
  3425. 'Another First',
  3426. '/option',
  3427. '/select'
  3428. );
  3429. $this->assertTags($result, $expected);
  3430. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3431. $result = $this->Form->select(
  3432. 'Model.contact_id',
  3433. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3434. array('escape' => false, 'empty' => 'pick something')
  3435. );
  3436. $expected = array(
  3437. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3438. array('option' => array('value' => '')), 'pick something', '/option',
  3439. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3440. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3441. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3442. '/select'
  3443. );
  3444. $this->assertTags($result, $expected);
  3445. $this->Form->request->data['Model']['field'] = 0;
  3446. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3447. $expected = array(
  3448. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3449. array('option' => array('value' => '')), '/option',
  3450. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3451. array('option' => array('value' => '1')), 'Yes', '/option',
  3452. '/select'
  3453. );
  3454. $this->assertTags($result, $expected);
  3455. }
  3456. /**
  3457. * test that select() with optiongroups listens to the escape param.
  3458. *
  3459. * @return void
  3460. */
  3461. public function testSelectOptionGroupEscaping() {
  3462. $options = array(
  3463. '>< Key' => array(
  3464. 1 => 'One',
  3465. 2 => 'Two'
  3466. )
  3467. );
  3468. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3469. $expected = array(
  3470. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3471. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3472. array('option' => array('value' => '1')), 'One', '/option',
  3473. array('option' => array('value' => '2')), 'Two', '/option',
  3474. '/optgroup',
  3475. '/select'
  3476. );
  3477. $this->assertTags($result, $expected);
  3478. $options = array(
  3479. '>< Key' => array(
  3480. 1 => 'One',
  3481. 2 => 'Two'
  3482. )
  3483. );
  3484. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3485. $expected = array(
  3486. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3487. 'optgroup' => array('label' => '>< Key'),
  3488. array('option' => array('value' => '1')), 'One', '/option',
  3489. array('option' => array('value' => '2')), 'Two', '/option',
  3490. '/optgroup',
  3491. '/select'
  3492. );
  3493. $this->assertTags($result, $expected);
  3494. }
  3495. /**
  3496. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3497. *
  3498. * @return void
  3499. */
  3500. public function testSelectWithNullAttributes() {
  3501. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3502. $expected = array(
  3503. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3504. array('option' => array('value' => '0')),
  3505. 'first',
  3506. '/option',
  3507. array('option' => array('value' => '1')),
  3508. 'second',
  3509. '/option',
  3510. '/select'
  3511. );
  3512. $this->assertTags($result, $expected);
  3513. }
  3514. /**
  3515. * testNestedSelect method
  3516. *
  3517. * test select element generation with optgroups
  3518. *
  3519. * @return void
  3520. */
  3521. public function testNestedSelect() {
  3522. $result = $this->Form->select(
  3523. 'Model.field',
  3524. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3525. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3526. )), array('empty' => false)
  3527. );
  3528. $expected = array(
  3529. 'select' => array('name' => 'data[Model][field]',
  3530. 'id' => 'ModelField'),
  3531. array('option' => array('value' => 1)),
  3532. 'One',
  3533. '/option',
  3534. array('option' => array('value' => 2)),
  3535. 'Two',
  3536. '/option',
  3537. array('optgroup' => array('label' => 'Three')),
  3538. array('option' => array('value' => 4)),
  3539. 'Four',
  3540. '/option',
  3541. array('option' => array('value' => 5)),
  3542. 'Five',
  3543. '/option',
  3544. '/optgroup',
  3545. '/select'
  3546. );
  3547. $this->assertTags($result, $expected);
  3548. $result = $this->Form->select(
  3549. 'Model.field',
  3550. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3551. array('showParents' => true, 'empty' => false)
  3552. );
  3553. $expected = array(
  3554. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3555. array('option' => array('value' => 1)),
  3556. 'One',
  3557. '/option',
  3558. array('option' => array('value' => 2)),
  3559. 'Two',
  3560. '/option',
  3561. array('optgroup' => array('label' => 'Three')),
  3562. array('option' => array('value' => 3)),
  3563. 'Three',
  3564. '/option',
  3565. array('option' => array('value' => 4)),
  3566. 'Four',
  3567. '/option',
  3568. '/optgroup',
  3569. '/select'
  3570. );
  3571. $this->assertTags($result, $expected);
  3572. }
  3573. /**
  3574. * testSelectMultiple method
  3575. *
  3576. * test generation of multiple select elements
  3577. *
  3578. * @return void
  3579. */
  3580. public function testSelectMultiple() {
  3581. $options = array('first', 'second', 'third');
  3582. $result = $this->Form->select(
  3583. 'Model.multi_field', $options, array('multiple' => true)
  3584. );
  3585. $expected = array(
  3586. 'input' => array(
  3587. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3588. ),
  3589. 'select' => array(
  3590. 'name' => 'data[Model][multi_field][]',
  3591. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3592. ),
  3593. array('option' => array('value' => '0')),
  3594. 'first',
  3595. '/option',
  3596. array('option' => array('value' => '1')),
  3597. 'second',
  3598. '/option',
  3599. array('option' => array('value' => '2')),
  3600. 'third',
  3601. '/option',
  3602. '/select'
  3603. );
  3604. $this->assertTags($result, $expected);
  3605. $result = $this->Form->select(
  3606. 'Model.multi_field', $options, array('multiple' => 'multiple')
  3607. );
  3608. $expected = array(
  3609. 'input' => array(
  3610. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3611. ),
  3612. 'select' => array(
  3613. 'name' => 'data[Model][multi_field][]',
  3614. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3615. ),
  3616. array('option' => array('value' => '0')),
  3617. 'first',
  3618. '/option',
  3619. array('option' => array('value' => '1')),
  3620. 'second',
  3621. '/option',
  3622. array('option' => array('value' => '2')),
  3623. 'third',
  3624. '/option',
  3625. '/select'
  3626. );
  3627. $this->assertTags($result, $expected);
  3628. $result = $this->Form->select(
  3629. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  3630. );
  3631. $expected = array(
  3632. 'input' => array(
  3633. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3634. ),
  3635. 'select' => array(
  3636. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  3637. 'multiple' => 'multiple'
  3638. ),
  3639. array('option' => array('value' => '0', 'selected' => 'selected')),
  3640. 'first',
  3641. '/option',
  3642. array('option' => array('value' => '1', 'selected' => 'selected')),
  3643. 'second',
  3644. '/option',
  3645. array('option' => array('value' => '2')),
  3646. 'third',
  3647. '/option',
  3648. '/select'
  3649. );
  3650. $this->assertTags($result, $expected);
  3651. $result = $this->Form->select(
  3652. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  3653. );
  3654. $expected = array(
  3655. 'select' => array(
  3656. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  3657. ),
  3658. array('option' => array('value' => '0', 'selected' => 'selected')),
  3659. 'first',
  3660. '/option',
  3661. array('option' => array('value' => '1', 'selected' => 'selected')),
  3662. 'second',
  3663. '/option',
  3664. array('option' => array('value' => '2')),
  3665. 'third',
  3666. '/option',
  3667. '/select'
  3668. );
  3669. $this->assertTags($result, $expected);
  3670. }
  3671. /**
  3672. * test generation of habtm select boxes.
  3673. *
  3674. * @return void
  3675. */
  3676. public function testHabtmSelectBox() {
  3677. $this->View->viewVars['contactTags'] = array(
  3678. 1 => 'blue',
  3679. 2 => 'red',
  3680. 3 => 'green'
  3681. );
  3682. $this->Form->request->data = array(
  3683. 'Contact' => array(),
  3684. 'ContactTag' => array(
  3685. array(
  3686. 'id' => 1,
  3687. 'name' => 'blue'
  3688. ),
  3689. array(
  3690. 'id' => 3,
  3691. 'name' => 'green'
  3692. )
  3693. )
  3694. );
  3695. $this->Form->create('Contact');
  3696. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3697. $expected = array(
  3698. 'input' => array(
  3699. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3700. ),
  3701. 'select' => array(
  3702. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  3703. 'multiple' => 'multiple'
  3704. ),
  3705. array('option' => array('value' => '1', 'selected' => 'selected')),
  3706. 'blue',
  3707. '/option',
  3708. array('option' => array('value' => '2')),
  3709. 'red',
  3710. '/option',
  3711. array('option' => array('value' => '3', 'selected' => 'selected')),
  3712. 'green',
  3713. '/option',
  3714. '/select'
  3715. );
  3716. $this->assertTags($result, $expected);
  3717. }
  3718. /**
  3719. * test generation of multi select elements in checkbox format
  3720. *
  3721. * @return void
  3722. */
  3723. public function testSelectMultipleCheckboxes() {
  3724. $result = $this->Form->select(
  3725. 'Model.multi_field',
  3726. array('first', 'second', 'third'),
  3727. array('multiple' => 'checkbox')
  3728. );
  3729. $expected = array(
  3730. 'input' => array(
  3731. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3732. ),
  3733. array('div' => array('class' => 'checkbox')),
  3734. array('input' => array(
  3735. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3736. 'value' => '0', 'id' => 'ModelMultiField0'
  3737. )),
  3738. array('label' => array('for' => 'ModelMultiField0')),
  3739. 'first',
  3740. '/label',
  3741. '/div',
  3742. array('div' => array('class' => 'checkbox')),
  3743. array('input' => array(
  3744. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3745. 'value' => '1', 'id' => 'ModelMultiField1'
  3746. )),
  3747. array('label' => array('for' => 'ModelMultiField1')),
  3748. 'second',
  3749. '/label',
  3750. '/div',
  3751. array('div' => array('class' => 'checkbox')),
  3752. array('input' => array(
  3753. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3754. 'value' => '2', 'id' => 'ModelMultiField2'
  3755. )),
  3756. array('label' => array('for' => 'ModelMultiField2')),
  3757. 'third',
  3758. '/label',
  3759. '/div'
  3760. );
  3761. $this->assertTags($result, $expected);
  3762. $result = $this->Form->select(
  3763. 'Model.multi_field',
  3764. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3765. array('multiple' => 'checkbox')
  3766. );
  3767. $expected = array(
  3768. 'input' => array(
  3769. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3770. ),
  3771. array('div' => array('class' => 'checkbox')),
  3772. array('input' => array(
  3773. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3774. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  3775. )),
  3776. array('label' => array('for' => 'ModelMultiFieldA')),
  3777. 'first',
  3778. '/label',
  3779. '/div',
  3780. array('div' => array('class' => 'checkbox')),
  3781. array('input' => array(
  3782. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3783. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  3784. )),
  3785. array('label' => array('for' => 'ModelMultiFieldB')),
  3786. 'second',
  3787. '/label',
  3788. '/div',
  3789. array('div' => array('class' => 'checkbox')),
  3790. array('input' => array(
  3791. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3792. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  3793. )),
  3794. array('label' => array('for' => 'ModelMultiFieldC')),
  3795. 'third',
  3796. '/label',
  3797. '/div'
  3798. );
  3799. $this->assertTags($result, $expected);
  3800. $result = $this->Form->select(
  3801. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  3802. );
  3803. $expected = array(
  3804. 'input' => array(
  3805. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3806. ),
  3807. array('div' => array('class' => 'checkbox')),
  3808. array('input' => array(
  3809. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3810. 'value' => '1', 'id' => 'ModelMultiField1'
  3811. )),
  3812. array('label' => array('for' => 'ModelMultiField1')),
  3813. 'first',
  3814. '/label',
  3815. '/div'
  3816. );
  3817. $this->assertTags($result, $expected);
  3818. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3819. $result = $this->Form->select(
  3820. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3821. );
  3822. $expected = array(
  3823. 'input' => array(
  3824. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3825. ),
  3826. array('div' => array('class' => 'checkbox')),
  3827. array('input' => array(
  3828. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3829. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  3830. )),
  3831. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  3832. 'first',
  3833. '/label',
  3834. '/div',
  3835. array('div' => array('class' => 'checkbox')),
  3836. array('input' => array(
  3837. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3838. 'value' => 'Array', 'id' => 'ModelTagsArray'
  3839. )),
  3840. array('label' => array('for' => 'ModelTagsArray')),
  3841. 'Array',
  3842. '/label',
  3843. '/div'
  3844. );
  3845. $this->assertTags($result, $expected);
  3846. }
  3847. /**
  3848. * test multiple checkboxes with div styles.
  3849. *
  3850. * @return void
  3851. */
  3852. public function testSelectMultipleCheckboxDiv() {
  3853. $result = $this->Form->select(
  3854. 'Model.tags',
  3855. array('first', 'second'),
  3856. array('multiple' => 'checkbox', 'class' => 'my-class')
  3857. );
  3858. $expected = array(
  3859. 'input' => array(
  3860. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3861. ),
  3862. array('div' => array('class' => 'my-class')),
  3863. array('input' => array(
  3864. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3865. 'value' => '0', 'id' => 'ModelTags0'
  3866. )),
  3867. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  3868. '/div',
  3869. array('div' => array('class' => 'my-class')),
  3870. array('input' => array(
  3871. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3872. 'value' => '1', 'id' => 'ModelTags1'
  3873. )),
  3874. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  3875. '/div'
  3876. );
  3877. $this->assertTags($result, $expected);
  3878. $result = $this->Form->input('Model.tags', array(
  3879. 'options' => array('first', 'second'),
  3880. 'multiple' => 'checkbox',
  3881. 'class' => 'my-class',
  3882. 'div' => false,
  3883. 'label' => false
  3884. ));
  3885. $this->assertTags($result, $expected);
  3886. $Contact = ClassRegistry::getObject('Contact');
  3887. $Contact->validationErrors['tags'] = 'Select atleast one option';
  3888. $result = $this->Form->input('Contact.tags', array(
  3889. 'options' => array('one'),
  3890. 'multiple' => 'checkbox',
  3891. 'label' => false,
  3892. 'div' => false
  3893. ));
  3894. $expected = array(
  3895. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3896. array('div' => array('class' => 'checkbox form-error')),
  3897. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3898. array('label' => array('for' => 'ContactTags0')),
  3899. 'one',
  3900. '/label',
  3901. '/div'
  3902. );
  3903. $this->assertTags($result, $expected);
  3904. $result = $this->Form->input('Contact.tags', array(
  3905. 'options' => array('one'),
  3906. 'multiple' => 'checkbox',
  3907. 'class' => 'mycheckbox',
  3908. 'label' => false,
  3909. 'div' => false
  3910. ));
  3911. $expected = array(
  3912. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3913. array('div' => array('class' => 'mycheckbox form-error')),
  3914. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3915. array('label' => array('for' => 'ContactTags0')),
  3916. 'one',
  3917. '/label',
  3918. '/div'
  3919. );
  3920. $this->assertTags($result, $expected);
  3921. }
  3922. /**
  3923. * Checks the security hash array generated for multiple-input checkbox elements
  3924. *
  3925. * @return void
  3926. */
  3927. public function testSelectMultipleCheckboxSecurity() {
  3928. $this->Form->request['_Token'] = array('key' => 'testKey');
  3929. $this->assertEquals(array(), $this->Form->fields);
  3930. $result = $this->Form->select(
  3931. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  3932. array('multiple' => 'checkbox')
  3933. );
  3934. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  3935. $result = $this->Form->secure($this->Form->fields);
  3936. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  3937. $this->assertRegExp('/"' . $key . '"/', $result);
  3938. }
  3939. /**
  3940. * Multiple select elements should always be secured as they always participate
  3941. * in the POST data.
  3942. *
  3943. * @return void
  3944. */
  3945. public function testSelectMultipleSecureWithNoOptions() {
  3946. $this->Form->request['_Token'] = array('key' => 'testkey');
  3947. $this->assertEquals(array(), $this->Form->fields);
  3948. $result = $this->Form->select(
  3949. 'Model.select',
  3950. array(),
  3951. array('multiple' => true)
  3952. );
  3953. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3954. }
  3955. /**
  3956. * When a select box has no options it should not be added to the fields list
  3957. * as it always fail post validation.
  3958. *
  3959. * @return void
  3960. */
  3961. public function testSelectNoSecureWithNoOptions() {
  3962. $this->Form->request['_Token'] = array('key' => 'testkey');
  3963. $this->assertEquals(array(), $this->Form->fields);
  3964. $this->Form->select(
  3965. 'Model.select',
  3966. array()
  3967. );
  3968. $this->assertEquals(array(), $this->Form->fields);
  3969. $this->Form->select(
  3970. 'Model.select',
  3971. array(),
  3972. array('empty' => true)
  3973. );
  3974. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3975. }
  3976. /**
  3977. * testInputMultipleCheckboxes method
  3978. *
  3979. * test input() resulting in multi select elements being generated.
  3980. *
  3981. * @return void
  3982. */
  3983. public function testInputMultipleCheckboxes() {
  3984. $result = $this->Form->input('Model.multi_field', array(
  3985. 'options' => array('first', 'second', 'third'),
  3986. 'multiple' => 'checkbox'
  3987. ));
  3988. $expected = array(
  3989. array('div' => array('class' => 'input select')),
  3990. array('label' => array('for' => 'ModelMultiField')),
  3991. 'Multi Field',
  3992. '/label',
  3993. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3994. array('div' => array('class' => 'checkbox')),
  3995. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  3996. array('label' => array('for' => 'ModelMultiField0')),
  3997. 'first',
  3998. '/label',
  3999. '/div',
  4000. array('div' => array('class' => 'checkbox')),
  4001. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4002. array('label' => array('for' => 'ModelMultiField1')),
  4003. 'second',
  4004. '/label',
  4005. '/div',
  4006. array('div' => array('class' => 'checkbox')),
  4007. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4008. array('label' => array('for' => 'ModelMultiField2')),
  4009. 'third',
  4010. '/label',
  4011. '/div',
  4012. '/div'
  4013. );
  4014. $this->assertTags($result, $expected);
  4015. $result = $this->Form->input('Model.multi_field', array(
  4016. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4017. 'multiple' => 'checkbox'
  4018. ));
  4019. $expected = array(
  4020. array('div' => array('class' => 'input select')),
  4021. array('label' => array('for' => 'ModelMultiField')),
  4022. 'Multi Field',
  4023. '/label',
  4024. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4025. array('div' => array('class' => 'checkbox')),
  4026. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4027. array('label' => array('for' => 'ModelMultiFieldA')),
  4028. 'first',
  4029. '/label',
  4030. '/div',
  4031. array('div' => array('class' => 'checkbox')),
  4032. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4033. array('label' => array('for' => 'ModelMultiFieldB')),
  4034. 'second',
  4035. '/label',
  4036. '/div',
  4037. array('div' => array('class' => 'checkbox')),
  4038. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4039. array('label' => array('for' => 'ModelMultiFieldC')),
  4040. 'third',
  4041. '/label',
  4042. '/div',
  4043. '/div'
  4044. );
  4045. $this->assertTags($result, $expected);
  4046. $result = $this->Form->input('Model.multi_field', array(
  4047. 'options' => array('1' => 'first'),
  4048. 'multiple' => 'checkbox',
  4049. 'label' => false,
  4050. 'div' => false
  4051. ));
  4052. $expected = array(
  4053. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4054. array('div' => array('class' => 'checkbox')),
  4055. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4056. array('label' => array('for' => 'ModelMultiField1')),
  4057. 'first',
  4058. '/label',
  4059. '/div'
  4060. );
  4061. $this->assertTags($result, $expected);
  4062. $result = $this->Form->input('Model.multi_field', array(
  4063. 'options' => array('2' => 'second'),
  4064. 'multiple' => 'checkbox',
  4065. 'label' => false,
  4066. 'div' => false
  4067. ));
  4068. $expected = array(
  4069. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4070. array('div' => array('class' => 'checkbox')),
  4071. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4072. array('label' => array('for' => 'ModelMultiField2')),
  4073. 'second',
  4074. '/label',
  4075. '/div'
  4076. );
  4077. $this->assertTags($result, $expected);
  4078. }
  4079. /**
  4080. * testSelectHiddenFieldOmission method
  4081. *
  4082. * test that select() with 'hiddenField' => false omits the hidden field
  4083. *
  4084. * @return void
  4085. */
  4086. public function testSelectHiddenFieldOmission() {
  4087. $result = $this->Form->select('Model.multi_field',
  4088. array('first', 'second'),
  4089. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4090. );
  4091. $expected = array(
  4092. array('div' => array('class' => 'checkbox')),
  4093. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4094. array('label' => array('for' => 'ModelMultiField0')),
  4095. 'first',
  4096. '/label',
  4097. '/div',
  4098. array('div' => array('class' => 'checkbox')),
  4099. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4100. array('label' => array('for' => 'ModelMultiField1')),
  4101. 'second',
  4102. '/label',
  4103. '/div'
  4104. );
  4105. $this->assertTags($result, $expected);
  4106. $result = $this->Form->input('Model.multi_field', array(
  4107. 'options' => array('first', 'second'),
  4108. 'multiple' => 'checkbox',
  4109. 'hiddenField' => false
  4110. ));
  4111. $expected = array(
  4112. array('div' => array('class' => 'input select')),
  4113. array('label' => array('for' => 'ModelMultiField')),
  4114. 'Multi Field',
  4115. '/label',
  4116. array('div' => array('class' => 'checkbox')),
  4117. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4118. array('label' => array('for' => 'ModelMultiField0')),
  4119. 'first',
  4120. '/label',
  4121. '/div',
  4122. array('div' => array('class' => 'checkbox')),
  4123. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4124. array('label' => array('for' => 'ModelMultiField1')),
  4125. 'second',
  4126. '/label',
  4127. '/div',
  4128. '/div'
  4129. );
  4130. $this->assertTags($result, $expected);
  4131. }
  4132. /**
  4133. * test that select() with multiple = checkbox works with overriding name attribute.
  4134. *
  4135. * @return void
  4136. */
  4137. public function testSelectCheckboxMultipleOverrideName() {
  4138. $result = $this->Form->input('category', array(
  4139. 'type' => 'select',
  4140. 'multiple' => 'checkbox',
  4141. 'name' => 'data[fish]',
  4142. 'options' => array('1', '2'),
  4143. 'div' => false,
  4144. 'label' => false,
  4145. ));
  4146. $expected = array(
  4147. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4148. array('div' => array('class' => 'checkbox')),
  4149. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4150. array('label' => array('for' => 'Category0')), '1', '/label',
  4151. '/div',
  4152. array('div' => array('class' => 'checkbox')),
  4153. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4154. array('label' => array('for' => 'Category1')), '2', '/label',
  4155. '/div'
  4156. );
  4157. $this->assertTags($result, $expected);
  4158. }
  4159. /**
  4160. * Test that 'id' overrides all the checkbox id's as well.
  4161. *
  4162. * @return void
  4163. */
  4164. public function testSelectCheckboxMultipleId() {
  4165. $result = $this->Form->select(
  4166. 'Model.multi_field',
  4167. array('first', 'second', 'third'),
  4168. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4169. );
  4170. $expected = array(
  4171. 'input' => array(
  4172. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4173. ),
  4174. array('div' => array('class' => 'checkbox')),
  4175. array('input' => array(
  4176. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4177. 'value' => '0', 'id' => 'CustomId0'
  4178. )),
  4179. array('label' => array('for' => 'CustomId0')),
  4180. 'first',
  4181. '/label',
  4182. '/div',
  4183. array('div' => array('class' => 'checkbox')),
  4184. array('input' => array(
  4185. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4186. 'value' => '1', 'id' => 'CustomId1'
  4187. )),
  4188. array('label' => array('for' => 'CustomId1')),
  4189. 'second',
  4190. '/label',
  4191. '/div',
  4192. array('div' => array('class' => 'checkbox')),
  4193. array('input' => array(
  4194. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4195. 'value' => '2', 'id' => 'CustomId2'
  4196. )),
  4197. array('label' => array('for' => 'CustomId2')),
  4198. 'third',
  4199. '/label',
  4200. '/div'
  4201. );
  4202. $this->assertTags($result, $expected);
  4203. }
  4204. /**
  4205. * testCheckbox method
  4206. *
  4207. * Test generation of checkboxes
  4208. *
  4209. * @return void
  4210. */
  4211. public function testCheckbox() {
  4212. $result = $this->Form->checkbox('Model.field');
  4213. $expected = array(
  4214. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4215. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4216. );
  4217. $this->assertTags($result, $expected);
  4218. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4219. $expected = array(
  4220. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4221. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4222. );
  4223. $this->assertTags($result, $expected);
  4224. $Contact = ClassRegistry::getObject('Contact');
  4225. $Contact->validationErrors['field'] = 1;
  4226. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4227. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4228. $expected = array(
  4229. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4230. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4231. );
  4232. $this->assertTags($result, $expected);
  4233. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4234. $expected = array(
  4235. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4236. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4237. );
  4238. $this->assertTags($result, $expected);
  4239. $this->Form->request->data['Contact']['field'] = '';
  4240. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4241. $expected = array(
  4242. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4243. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4244. );
  4245. $this->assertTags($result, $expected);
  4246. $Contact->validationErrors = array();
  4247. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4248. $expected = array(
  4249. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4250. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4251. );
  4252. $this->assertTags($result, $expected);
  4253. $this->Form->request->data['Contact']['published'] = 1;
  4254. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4255. $expected = array(
  4256. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4257. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4258. );
  4259. $this->assertTags($result, $expected);
  4260. $this->Form->request->data['Contact']['published'] = 0;
  4261. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4262. $expected = array(
  4263. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4264. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4265. );
  4266. $this->assertTags($result, $expected);
  4267. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4268. $expected = array(
  4269. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4270. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4271. );
  4272. $this->assertTags($result, $expected);
  4273. $result = $this->Form->checkbox('CustomField.1.value');
  4274. $expected = array(
  4275. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4276. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4277. );
  4278. $this->assertTags($result, $expected);
  4279. }
  4280. /**
  4281. * test checkbox() with a custom name attribute
  4282. *
  4283. * @return void
  4284. */
  4285. public function testCheckboxCustomNameAttribute() {
  4286. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4287. $expected = array(
  4288. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4289. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4290. );
  4291. $this->assertTags($result, $expected);
  4292. }
  4293. /**
  4294. * test the checked option for checkboxes.
  4295. *
  4296. * @return void
  4297. */
  4298. public function testCheckboxCheckedOption() {
  4299. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4300. $expected = array(
  4301. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4302. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4303. );
  4304. $this->assertTags($result, $expected);
  4305. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4306. $expected = array(
  4307. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4308. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4309. );
  4310. $this->assertTags($result, $expected);
  4311. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4312. $expected = array(
  4313. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4314. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4315. );
  4316. $this->assertTags($result, $expected);
  4317. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4318. $expected = array(
  4319. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4320. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4321. );
  4322. $this->assertTags($result, $expected);
  4323. $this->Form->request->data['Model']['field'] = 1;
  4324. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4325. $expected = array(
  4326. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4327. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4328. );
  4329. $this->assertTags($result, $expected);
  4330. }
  4331. /**
  4332. * Test that disabled attribute works on both the checkbox and hidden input.
  4333. *
  4334. * @return void
  4335. */
  4336. public function testCheckboxDisabling() {
  4337. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4338. $expected = array(
  4339. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4340. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4341. );
  4342. $this->assertTags($result, $expected);
  4343. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4344. $expected = array(
  4345. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4346. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4347. );
  4348. $this->assertTags($result, $expected);
  4349. }
  4350. /**
  4351. * Test that the hidden input for checkboxes can be omitted or set to a
  4352. * specific value.
  4353. *
  4354. * @return void
  4355. */
  4356. public function testCheckboxHiddenField() {
  4357. $result = $this->Form->input('UserForm.something', array(
  4358. 'type' => 'checkbox',
  4359. 'hiddenField' => false
  4360. ));
  4361. $expected = array(
  4362. 'div' => array('class' => 'input checkbox'),
  4363. array('input' => array(
  4364. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4365. 'value' => '1', 'id' => 'UserFormSomething'
  4366. )),
  4367. 'label' => array('for' => 'UserFormSomething'),
  4368. 'Something',
  4369. '/label',
  4370. '/div'
  4371. );
  4372. $this->assertTags($result, $expected);
  4373. $result = $this->Form->input('UserForm.something', array(
  4374. 'type' => 'checkbox',
  4375. 'value' => 'Y',
  4376. 'hiddenField' => 'N',
  4377. ));
  4378. $expected = array(
  4379. 'div' => array('class' => 'input checkbox'),
  4380. array('input' => array(
  4381. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  4382. 'value' => 'N', 'id' => 'UserFormSomething_'
  4383. )),
  4384. array('input' => array(
  4385. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4386. 'value' => 'Y', 'id' => 'UserFormSomething'
  4387. )),
  4388. 'label' => array('for' => 'UserFormSomething'),
  4389. 'Something',
  4390. '/label',
  4391. '/div'
  4392. );
  4393. $this->assertTags($result, $expected);
  4394. }
  4395. /**
  4396. * testDateTime method
  4397. *
  4398. * Test generation of date/time select elements
  4399. *
  4400. * @return void
  4401. */
  4402. public function testDateTime() {
  4403. extract($this->dateRegex);
  4404. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4405. $now = strtotime('now');
  4406. $expected = array(
  4407. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4408. $daysRegex,
  4409. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4410. date('j', $now),
  4411. '/option',
  4412. '*/select',
  4413. '-',
  4414. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4415. $monthsRegex,
  4416. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4417. date('F', $now),
  4418. '/option',
  4419. '*/select',
  4420. '-',
  4421. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4422. $yearsRegex,
  4423. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4424. date('Y', $now),
  4425. '/option',
  4426. '*/select',
  4427. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4428. $hoursRegex,
  4429. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4430. date('g', $now),
  4431. '/option',
  4432. '*/select',
  4433. ':',
  4434. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4435. $minutesRegex,
  4436. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4437. date('i', $now),
  4438. '/option',
  4439. '*/select',
  4440. ' ',
  4441. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4442. $meridianRegex,
  4443. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4444. date('a', $now),
  4445. '/option',
  4446. '*/select'
  4447. );
  4448. $this->assertTags($result, $expected);
  4449. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4450. $expected = array(
  4451. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4452. $daysRegex,
  4453. array('option' => array('value' => '')),
  4454. '/option',
  4455. '*/select',
  4456. '-',
  4457. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4458. $monthsRegex,
  4459. array('option' => array('value' => '')),
  4460. '/option',
  4461. '*/select',
  4462. '-',
  4463. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4464. $yearsRegex,
  4465. array('option' => array('value' => '')),
  4466. '/option',
  4467. '*/select',
  4468. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4469. $hoursRegex,
  4470. array('option' => array('value' => '')),
  4471. '/option',
  4472. '*/select',
  4473. ':',
  4474. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4475. $minutesRegex,
  4476. array('option' => array('value' => '')),
  4477. '/option',
  4478. '*/select',
  4479. ' ',
  4480. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4481. $meridianRegex,
  4482. array('option' => array('value' => '')),
  4483. '/option',
  4484. '*/select'
  4485. );
  4486. $this->assertTags($result, $expected);
  4487. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4488. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  4489. $this->assertTags($result, $expected);
  4490. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4491. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  4492. $this->assertTags($result, $expected);
  4493. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4494. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  4495. $expected = array(
  4496. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4497. $daysRegex,
  4498. array('option' => array('value' => '')),
  4499. '/option',
  4500. '*/select',
  4501. '-',
  4502. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4503. $monthsRegex,
  4504. array('option' => array('value' => '')),
  4505. '/option',
  4506. '*/select',
  4507. '-',
  4508. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4509. $yearsRegex,
  4510. array('option' => array('value' => '')),
  4511. '/option',
  4512. '*/select',
  4513. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4514. $hoursRegex,
  4515. array('option' => array('value' => '')),
  4516. '/option',
  4517. '*/select',
  4518. ':',
  4519. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4520. $minutesRegex,
  4521. array('option' => array('value' => '')),
  4522. '/option',
  4523. array('option' => array('value' => '00')),
  4524. '00',
  4525. '/option',
  4526. array('option' => array('value' => '05')),
  4527. '05',
  4528. '/option',
  4529. array('option' => array('value' => '10')),
  4530. '10',
  4531. '/option',
  4532. '*/select',
  4533. ' ',
  4534. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4535. $meridianRegex,
  4536. array('option' => array('value' => '')),
  4537. '/option',
  4538. '*/select'
  4539. );
  4540. $this->assertTags($result, $expected);
  4541. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4542. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4543. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4544. $this->Form->request->data['Contact']['data'] = null;
  4545. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4546. $expected = array(
  4547. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4548. $daysRegex,
  4549. array('option' => array('value' => '')),
  4550. '/option',
  4551. '*/select',
  4552. '-',
  4553. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4554. $monthsRegex,
  4555. array('option' => array('value' => '')),
  4556. '/option',
  4557. '*/select',
  4558. '-',
  4559. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4560. $yearsRegex,
  4561. array('option' => array('value' => '')),
  4562. '/option',
  4563. '*/select',
  4564. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4565. $hoursRegex,
  4566. array('option' => array('value' => '')),
  4567. '/option',
  4568. '*/select',
  4569. ':',
  4570. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4571. $minutesRegex,
  4572. array('option' => array('value' => '')),
  4573. '/option',
  4574. '*/select',
  4575. ' ',
  4576. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4577. $meridianRegex,
  4578. array('option' => array('value' => '')),
  4579. '/option',
  4580. '*/select'
  4581. );
  4582. $this->assertTags($result, $expected);
  4583. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4584. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  4585. $now = strtotime($this->Form->data['Model']['field']);
  4586. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  4587. $expected = array(
  4588. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4589. $daysRegex,
  4590. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4591. date('j', $now),
  4592. '/option',
  4593. '*/select',
  4594. '-',
  4595. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4596. $monthsRegex,
  4597. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4598. date('F', $now),
  4599. '/option',
  4600. '*/select',
  4601. '-',
  4602. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  4603. $yearsRegex,
  4604. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4605. date('Y', $now),
  4606. '/option',
  4607. '*/select',
  4608. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4609. $hoursRegex,
  4610. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4611. date('g', $now),
  4612. '/option',
  4613. '*/select',
  4614. ':',
  4615. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4616. $minutesRegex,
  4617. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4618. date('i', $now),
  4619. '/option',
  4620. '*/select',
  4621. ' ',
  4622. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  4623. $meridianRegex,
  4624. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4625. date('a', $now),
  4626. '/option',
  4627. '*/select'
  4628. );
  4629. $this->assertTags($result, $expected);
  4630. $selected = strtotime('2008-10-26 12:33:00');
  4631. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  4632. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  4633. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  4634. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  4635. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  4636. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  4637. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  4638. $this->Form->create('Contact');
  4639. $result = $this->Form->input('published');
  4640. $now = strtotime('now');
  4641. $expected = array(
  4642. 'div' => array('class' => 'input date'),
  4643. 'label' => array('for' => 'ContactPublishedMonth'),
  4644. 'Published',
  4645. '/label',
  4646. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4647. $monthsRegex,
  4648. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4649. date('F', $now),
  4650. '/option',
  4651. '*/select',
  4652. '-',
  4653. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4654. $daysRegex,
  4655. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4656. date('j', $now),
  4657. '/option',
  4658. '*/select',
  4659. '-',
  4660. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4661. $yearsRegex,
  4662. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4663. date('Y', $now),
  4664. '/option',
  4665. '*/select',
  4666. '/div'
  4667. );
  4668. $this->assertTags($result, $expected);
  4669. $result = $this->Form->input('published2', array('type' => 'date'));
  4670. $now = strtotime('now');
  4671. $expected = array(
  4672. 'div' => array('class' => 'input date'),
  4673. 'label' => array('for' => 'ContactPublished2Month'),
  4674. 'Published2',
  4675. '/label',
  4676. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  4677. $monthsRegex,
  4678. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4679. date('F', $now),
  4680. '/option',
  4681. '*/select',
  4682. '-',
  4683. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  4684. $daysRegex,
  4685. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4686. date('j', $now),
  4687. '/option',
  4688. '*/select',
  4689. '-',
  4690. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  4691. $yearsRegex,
  4692. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4693. date('Y', $now),
  4694. '/option',
  4695. '*/select',
  4696. '/div'
  4697. );
  4698. $this->assertTags($result, $expected);
  4699. $this->Form->create('Contact');
  4700. $result = $this->Form->input('published', array('monthNames' => false));
  4701. $now = strtotime('now');
  4702. $expected = array(
  4703. 'div' => array('class' => 'input date'),
  4704. 'label' => array('for' => 'ContactPublishedMonth'),
  4705. 'Published',
  4706. '/label',
  4707. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4708. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4709. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4710. date('m', $now),
  4711. '/option',
  4712. '*/select',
  4713. '-',
  4714. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4715. $daysRegex,
  4716. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4717. date('j', $now),
  4718. '/option',
  4719. '*/select',
  4720. '-',
  4721. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4722. $yearsRegex,
  4723. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4724. date('Y', $now),
  4725. '/option',
  4726. '*/select',
  4727. '/div'
  4728. );
  4729. $this->assertTags($result, $expected);
  4730. $result = $this->Form->input('published', array('type' => 'time'));
  4731. $now = strtotime('now');
  4732. $expected = array(
  4733. 'div' => array('class' => 'input time'),
  4734. 'label' => array('for' => 'ContactPublishedHour'),
  4735. 'Published',
  4736. '/label',
  4737. array('select' => array('name' => 'data[Contact][published][hour]', 'id' => 'ContactPublishedHour')),
  4738. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4739. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4740. date('g', $now),
  4741. '/option',
  4742. '*/select',
  4743. ':',
  4744. );
  4745. $this->assertTags($result, $expected);
  4746. $result = $this->Form->input('published', array(
  4747. 'timeFormat' => 24,
  4748. 'interval' => 5,
  4749. 'selected' => strtotime('2009-09-03 13:37:00'),
  4750. 'type' => 'datetime'
  4751. ));
  4752. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4753. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  4754. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  4755. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  4756. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  4757. $this->assertNoErrors();
  4758. $this->Form->request->data['Contact'] = array(
  4759. 'date' => array(
  4760. 'day' => '',
  4761. 'month' => '',
  4762. 'year' => '',
  4763. 'hour' => '',
  4764. 'min' => '',
  4765. 'meridian' => ''
  4766. )
  4767. );
  4768. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4769. }
  4770. /**
  4771. * test that datetime() and default values work.
  4772. *
  4773. * @return void
  4774. */
  4775. public function testDatetimeWithDefault() {
  4776. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  4777. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4778. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4779. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4780. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  4781. 'default' => '2009-06-01 11:15:30'
  4782. ));
  4783. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4784. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4785. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4786. }
  4787. /**
  4788. * test that bogus non-date time data doesn't cause errors.
  4789. *
  4790. * @return void
  4791. */
  4792. public function testDateTimeWithBogusData() {
  4793. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  4794. $this->assertNotRegExp('/selected="selected">\d/', $result);
  4795. }
  4796. /**
  4797. * testDateTimeEmptyAsArray
  4798. *
  4799. * @return void
  4800. */
  4801. public function testDateTimeEmptyAsArray() {
  4802. $result = $this->Form->dateTime('Contact.date',
  4803. 'DMY',
  4804. '12',
  4805. array(
  4806. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  4807. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  4808. )
  4809. )
  4810. );
  4811. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4812. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4813. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4814. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4815. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4816. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4817. $result = $this->Form->dateTime('Contact.date',
  4818. 'DMY',
  4819. '12',
  4820. array(
  4821. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4822. )
  4823. );
  4824. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4825. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4826. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4827. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  4828. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  4829. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  4830. }
  4831. /**
  4832. * testFormDateTimeMulti method
  4833. *
  4834. * test multiple datetime element generation
  4835. *
  4836. * @return void
  4837. */
  4838. public function testFormDateTimeMulti() {
  4839. extract($this->dateRegex);
  4840. $result = $this->Form->dateTime('Contact.1.updated');
  4841. $expected = array(
  4842. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  4843. $daysRegex,
  4844. array('option' => array('value' => '')),
  4845. '/option',
  4846. '*/select',
  4847. '-',
  4848. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  4849. $monthsRegex,
  4850. array('option' => array('value' => '')),
  4851. '/option',
  4852. '*/select',
  4853. '-',
  4854. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  4855. $yearsRegex,
  4856. array('option' => array('value' => '')),
  4857. '/option',
  4858. '*/select',
  4859. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  4860. $hoursRegex,
  4861. array('option' => array('value' => '')),
  4862. '/option',
  4863. '*/select',
  4864. ':',
  4865. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  4866. $minutesRegex,
  4867. array('option' => array('value' => '')),
  4868. '/option',
  4869. '*/select',
  4870. ' ',
  4871. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  4872. $meridianRegex,
  4873. array('option' => array('value' => '')),
  4874. '/option',
  4875. '*/select'
  4876. );
  4877. $this->assertTags($result, $expected);
  4878. $result = $this->Form->dateTime('Contact.2.updated');
  4879. $expected = array(
  4880. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  4881. $daysRegex,
  4882. array('option' => array('value' => '')),
  4883. '/option',
  4884. '*/select',
  4885. '-',
  4886. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  4887. $monthsRegex,
  4888. array('option' => array('value' => '')),
  4889. '/option',
  4890. '*/select',
  4891. '-',
  4892. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  4893. $yearsRegex,
  4894. array('option' => array('value' => '')),
  4895. '/option',
  4896. '*/select',
  4897. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  4898. $hoursRegex,
  4899. array('option' => array('value' => '')),
  4900. '/option',
  4901. '*/select',
  4902. ':',
  4903. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  4904. $minutesRegex,
  4905. array('option' => array('value' => '')),
  4906. '/option',
  4907. '*/select',
  4908. ' ',
  4909. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  4910. $meridianRegex,
  4911. array('option' => array('value' => '')),
  4912. '/option',
  4913. '*/select'
  4914. );
  4915. $this->assertTags($result, $expected);
  4916. }
  4917. /**
  4918. * When changing the date format, the label should always focus the first select box when
  4919. * clicked.
  4920. *
  4921. * @return void
  4922. */
  4923. public function testDateTimeLabelIdMatchesFirstInput() {
  4924. $result = $this->Form->input('Model.date', array('type' => 'date'));
  4925. $this->assertContains('label for="ModelDateMonth"', $result);
  4926. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  4927. $this->assertContains('label for="ModelDateDay"', $result);
  4928. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  4929. $this->assertContains('label for="ModelDateYear"', $result);
  4930. }
  4931. /**
  4932. * testMonth method
  4933. *
  4934. * @return void
  4935. */
  4936. public function testMonth() {
  4937. $result = $this->Form->month('Model.field');
  4938. $expected = array(
  4939. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4940. array('option' => array('value' => '')),
  4941. '/option',
  4942. array('option' => array('value' => '01')),
  4943. date('F', strtotime('2008-01-01 00:00:00')),
  4944. '/option',
  4945. array('option' => array('value' => '02')),
  4946. date('F', strtotime('2008-02-01 00:00:00')),
  4947. '/option',
  4948. '*/select',
  4949. );
  4950. $this->assertTags($result, $expected);
  4951. $result = $this->Form->month('Model.field', array('empty' => true));
  4952. $expected = array(
  4953. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4954. array('option' => array('value' => '')),
  4955. '/option',
  4956. array('option' => array('value' => '01')),
  4957. date('F', strtotime('2008-01-01 00:00:00')),
  4958. '/option',
  4959. array('option' => array('value' => '02')),
  4960. date('F', strtotime('2008-02-01 00:00:00')),
  4961. '/option',
  4962. '*/select',
  4963. );
  4964. $this->assertTags($result, $expected);
  4965. $result = $this->Form->month('Model.field', array('monthNames' => false));
  4966. $expected = array(
  4967. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4968. array('option' => array('value' => '')),
  4969. '/option',
  4970. array('option' => array('value' => '01')),
  4971. '01',
  4972. '/option',
  4973. array('option' => array('value' => '02')),
  4974. '02',
  4975. '/option',
  4976. '*/select',
  4977. );
  4978. $this->assertTags($result, $expected);
  4979. $monthNames = array(
  4980. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  4981. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  4982. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  4983. $expected = array(
  4984. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4985. array('option' => array('value' => '')),
  4986. '/option',
  4987. array('option' => array('value' => '01')),
  4988. 'Jan',
  4989. '/option',
  4990. array('option' => array('value' => '02')),
  4991. 'Feb',
  4992. '/option',
  4993. '*/select',
  4994. );
  4995. $this->assertTags($result, $expected);
  4996. }
  4997. /**
  4998. * testDay method
  4999. *
  5000. * @return void
  5001. */
  5002. public function testDay() {
  5003. extract($this->dateRegex);
  5004. $result = $this->Form->day('Model.field', array('value' => false));
  5005. $expected = array(
  5006. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5007. array('option' => array('value' => '')),
  5008. '/option',
  5009. array('option' => array('value' => '01')),
  5010. '1',
  5011. '/option',
  5012. array('option' => array('value' => '02')),
  5013. '2',
  5014. '/option',
  5015. $daysRegex,
  5016. '/select',
  5017. );
  5018. $this->assertTags($result, $expected);
  5019. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5020. $result = $this->Form->day('Model.field');
  5021. $expected = array(
  5022. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5023. array('option' => array('value' => '')),
  5024. '/option',
  5025. array('option' => array('value' => '01')),
  5026. '1',
  5027. '/option',
  5028. array('option' => array('value' => '02')),
  5029. '2',
  5030. '/option',
  5031. $daysRegex,
  5032. array('option' => array('value' => '10', 'selected' => 'selected')),
  5033. '10',
  5034. '/option',
  5035. $daysRegex,
  5036. '/select',
  5037. );
  5038. $this->assertTags($result, $expected);
  5039. $this->Form->request->data['Model']['field'] = '';
  5040. $result = $this->Form->day('Model.field', array('value' => '10'));
  5041. $expected = array(
  5042. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5043. array('option' => array('value' => '')),
  5044. '/option',
  5045. array('option' => array('value' => '01')),
  5046. '1',
  5047. '/option',
  5048. array('option' => array('value' => '02')),
  5049. '2',
  5050. '/option',
  5051. $daysRegex,
  5052. array('option' => array('value' => '10', 'selected' => 'selected')),
  5053. '10',
  5054. '/option',
  5055. $daysRegex,
  5056. '/select',
  5057. );
  5058. $this->assertTags($result, $expected);
  5059. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5060. $result = $this->Form->day('Model.field', array('value' => true));
  5061. $expected = array(
  5062. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5063. array('option' => array('value' => '')),
  5064. '/option',
  5065. array('option' => array('value' => '01')),
  5066. '1',
  5067. '/option',
  5068. array('option' => array('value' => '02')),
  5069. '2',
  5070. '/option',
  5071. $daysRegex,
  5072. array('option' => array('value' => '10', 'selected' => 'selected')),
  5073. '10',
  5074. '/option',
  5075. $daysRegex,
  5076. '/select',
  5077. );
  5078. $this->assertTags($result, $expected);
  5079. }
  5080. /**
  5081. * testMinute method
  5082. *
  5083. * @return void
  5084. */
  5085. public function testMinute() {
  5086. extract($this->dateRegex);
  5087. $result = $this->Form->minute('Model.field');
  5088. $expected = array(
  5089. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5090. array('option' => array('value' => '')),
  5091. '/option',
  5092. array('option' => array('value' => '00')),
  5093. '00',
  5094. '/option',
  5095. array('option' => array('value' => '01')),
  5096. '01',
  5097. '/option',
  5098. array('option' => array('value' => '02')),
  5099. '02',
  5100. '/option',
  5101. $minutesRegex,
  5102. '/select',
  5103. );
  5104. $this->assertTags($result, $expected);
  5105. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5106. $result = $this->Form->minute('Model.field');
  5107. $expected = array(
  5108. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5109. array('option' => array('value' => '')),
  5110. '/option',
  5111. array('option' => array('value' => '00')),
  5112. '00',
  5113. '/option',
  5114. array('option' => array('value' => '01')),
  5115. '01',
  5116. '/option',
  5117. array('option' => array('value' => '02')),
  5118. '02',
  5119. '/option',
  5120. $minutesRegex,
  5121. array('option' => array('value' => '12', 'selected' => 'selected')),
  5122. '12',
  5123. '/option',
  5124. $minutesRegex,
  5125. '/select',
  5126. );
  5127. $this->assertTags($result, $expected);
  5128. $this->Form->request->data['Model']['field'] = '';
  5129. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5130. $expected = array(
  5131. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5132. array('option' => array('value' => '')),
  5133. '/option',
  5134. array('option' => array('value' => '00')),
  5135. '00',
  5136. '/option',
  5137. array('option' => array('value' => '05')),
  5138. '05',
  5139. '/option',
  5140. array('option' => array('value' => '10')),
  5141. '10',
  5142. '/option',
  5143. $minutesRegex,
  5144. '/select',
  5145. );
  5146. $this->assertTags($result, $expected);
  5147. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5148. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5149. $expected = array(
  5150. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5151. array('option' => array('value' => '')),
  5152. '/option',
  5153. array('option' => array('value' => '00')),
  5154. '00',
  5155. '/option',
  5156. array('option' => array('value' => '05')),
  5157. '05',
  5158. '/option',
  5159. array('option' => array('value' => '10', 'selected' => 'selected')),
  5160. '10',
  5161. '/option',
  5162. $minutesRegex,
  5163. '/select',
  5164. );
  5165. $this->assertTags($result, $expected);
  5166. }
  5167. /**
  5168. * testHour method
  5169. *
  5170. * @return void
  5171. */
  5172. public function testHour() {
  5173. extract($this->dateRegex);
  5174. $result = $this->Form->hour('Model.field', false);
  5175. $expected = array(
  5176. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  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. $hoursRegex,
  5186. '/select',
  5187. );
  5188. $this->assertTags($result, $expected);
  5189. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5190. $result = $this->Form->hour('Model.field', false);
  5191. $expected = array(
  5192. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5193. array('option' => array('value' => '')),
  5194. '/option',
  5195. array('option' => array('value' => '01')),
  5196. '1',
  5197. '/option',
  5198. array('option' => array('value' => '02')),
  5199. '2',
  5200. '/option',
  5201. $hoursRegex,
  5202. array('option' => array('value' => '12', 'selected' => 'selected')),
  5203. '12',
  5204. '/option',
  5205. '/select',
  5206. );
  5207. $this->assertTags($result, $expected);
  5208. $this->Form->request->data['Model']['field'] = '';
  5209. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5210. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5211. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  5212. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5213. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5214. $result = $this->Form->hour('Model.field', true);
  5215. $expected = array(
  5216. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5217. array('option' => array('value' => '')),
  5218. '/option',
  5219. array('option' => array('value' => '00', 'selected' => 'selected')),
  5220. '0',
  5221. '/option',
  5222. array('option' => array('value' => '01')),
  5223. '1',
  5224. '/option',
  5225. array('option' => array('value' => '02')),
  5226. '2',
  5227. '/option',
  5228. $hoursRegex,
  5229. '/select',
  5230. );
  5231. $this->assertTags($result, $expected);
  5232. unset($this->Form->request->data['Model']['field']);
  5233. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5234. $thisHour = date('H');
  5235. $optValue = date('G');
  5236. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5237. }
  5238. /**
  5239. * testYear method
  5240. *
  5241. * @return void
  5242. */
  5243. public function testYear() {
  5244. $result = $this->Form->year('Model.field', 2006, 2007);
  5245. $expected = array(
  5246. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5247. array('option' => array('value' => '')),
  5248. '/option',
  5249. array('option' => array('value' => '2007')),
  5250. '2007',
  5251. '/option',
  5252. array('option' => array('value' => '2006')),
  5253. '2006',
  5254. '/option',
  5255. '/select',
  5256. );
  5257. $this->assertTags($result, $expected);
  5258. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5259. $expected = array(
  5260. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5261. array('option' => array('value' => '')),
  5262. '/option',
  5263. array('option' => array('value' => '2006')),
  5264. '2006',
  5265. '/option',
  5266. array('option' => array('value' => '2007')),
  5267. '2007',
  5268. '/option',
  5269. '/select',
  5270. );
  5271. $this->assertTags($result, $expected);
  5272. $this->request->data['Contact']['published'] = '';
  5273. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5274. $expected = array(
  5275. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5276. array('option' => array('value' => '')),
  5277. '/option',
  5278. array('option' => array('value' => '2007')),
  5279. '2007',
  5280. '/option',
  5281. array('option' => array('value' => '2006')),
  5282. '2006',
  5283. '/option',
  5284. '/select',
  5285. );
  5286. $this->assertTags($result, $expected);
  5287. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5288. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5289. $expected = array(
  5290. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5291. array('option' => array('value' => '2007')),
  5292. '2007',
  5293. '/option',
  5294. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5295. '2006',
  5296. '/option',
  5297. '/select',
  5298. );
  5299. $this->assertTags($result, $expected);
  5300. $this->Form->request->data['Contact']['published'] = '';
  5301. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5302. $expected = array(
  5303. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5304. array('option' => array('value' => '')),
  5305. '/option',
  5306. array('option' => array('value' => '2007')),
  5307. '2007',
  5308. '/option',
  5309. array('option' => array('value' => '2006')),
  5310. '2006',
  5311. '/option',
  5312. '/select',
  5313. );
  5314. $this->assertTags($result, $expected);
  5315. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5316. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5317. $expected = array(
  5318. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5319. array('option' => array('value' => '2007')),
  5320. '2007',
  5321. '/option',
  5322. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5323. '2006',
  5324. '/option',
  5325. '/select',
  5326. );
  5327. $this->assertTags($result, $expected);
  5328. $this->Form->request->data['Contact']['published'] = '';
  5329. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5330. $expected = array(
  5331. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5332. array('option' => array('value' => '')),
  5333. '/option',
  5334. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5335. '2007',
  5336. '/option',
  5337. array('option' => array('value' => '2006')),
  5338. '2006',
  5339. '/option',
  5340. '/select',
  5341. );
  5342. $this->assertTags($result, $expected);
  5343. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5344. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5345. $expected = array(
  5346. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5347. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5348. '2007',
  5349. '/option',
  5350. array('option' => array('value' => '2006')),
  5351. '2006',
  5352. '/option',
  5353. '/select',
  5354. );
  5355. $this->assertTags($result, $expected);
  5356. $this->Form->request->data['Contact']['published'] = '';
  5357. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5358. $expected = array(
  5359. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5360. array('option' => array('value' => '2008')),
  5361. '2008',
  5362. '/option',
  5363. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5364. '2007',
  5365. '/option',
  5366. array('option' => array('value' => '2006')),
  5367. '2006',
  5368. '/option',
  5369. '/select',
  5370. );
  5371. $this->assertTags($result, $expected);
  5372. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5373. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5374. $expected = array(
  5375. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5376. array('option' => array('value' => '2008')),
  5377. '2008',
  5378. '/option',
  5379. array('option' => array('value' => '2007')),
  5380. '2007',
  5381. '/option',
  5382. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5383. '2006',
  5384. '/option',
  5385. '/select',
  5386. );
  5387. $this->assertTags($result, $expected);
  5388. $this->Form->request->data = array();
  5389. $this->Form->create('Contact');
  5390. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5391. $expected = array(
  5392. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5393. array('option' => array('value' => '2008')),
  5394. '2008',
  5395. '/option',
  5396. array('option' => array('value' => '2007')),
  5397. '2007',
  5398. '/option',
  5399. array('option' => array('value' => '2006')),
  5400. '2006',
  5401. '/option',
  5402. '/select',
  5403. );
  5404. $this->assertTags($result, $expected);
  5405. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  5406. $this->assertContains('data[Contact][published][year]', $result);
  5407. }
  5408. /**
  5409. * testTextArea method
  5410. *
  5411. * @return void
  5412. */
  5413. public function testTextArea() {
  5414. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  5415. $result = $this->Form->textarea('Model.field');
  5416. $expected = array(
  5417. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5418. 'some test data',
  5419. '/textarea',
  5420. );
  5421. $this->assertTags($result, $expected);
  5422. $result = $this->Form->textarea('Model.tmp');
  5423. $expected = array(
  5424. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  5425. '/textarea',
  5426. );
  5427. $this->assertTags($result, $expected);
  5428. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5429. $result = $this->Form->textarea('Model.field');
  5430. $expected = array(
  5431. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5432. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5433. '/textarea',
  5434. );
  5435. $this->assertTags($result, $expected);
  5436. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5437. $result = $this->Form->textarea('Model.field', array('escape' => false));
  5438. $expected = array(
  5439. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5440. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5441. '/textarea',
  5442. );
  5443. $this->assertTags($result, $expected);
  5444. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  5445. $result = $this->Form->textarea('Model.0.OtherModel.field');
  5446. $expected = array(
  5447. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  5448. '/textarea'
  5449. );
  5450. $this->assertTags($result, $expected);
  5451. }
  5452. /**
  5453. * testTextAreaWithStupidCharacters method
  5454. *
  5455. * test text area with non-ascii characters
  5456. *
  5457. * @return void
  5458. */
  5459. public function testTextAreaWithStupidCharacters() {
  5460. $this->loadFixtures('Post');
  5461. $result = $this->Form->input('Post.content', array(
  5462. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  5463. ));
  5464. $expected = array(
  5465. 'div' => array('class' => 'input text'),
  5466. 'label' => array('for' => 'PostContent'),
  5467. 'Current Text',
  5468. '/label',
  5469. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  5470. 'GREAT®',
  5471. '/textarea',
  5472. '/div'
  5473. );
  5474. $this->assertTags($result, $expected);
  5475. }
  5476. /**
  5477. * testHiddenField method
  5478. *
  5479. * @return void
  5480. */
  5481. public function testHiddenField() {
  5482. $Contact = ClassRegistry::getObject('Contact');
  5483. $Contact->validationErrors['field'] = 1;
  5484. $this->Form->request->data['Contact']['field'] = 'test';
  5485. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  5486. $this->assertTags($result, array(
  5487. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  5488. );
  5489. }
  5490. /**
  5491. * testFileUploadField method
  5492. *
  5493. * @return void
  5494. */
  5495. public function testFileUploadField() {
  5496. $result = $this->Form->file('Model.upload');
  5497. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5498. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  5499. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  5500. $expected = array(
  5501. 'div' => array('class' => 'input file'),
  5502. 'label' => array('for' => 'ModelUpload'),
  5503. 'Upload',
  5504. '/label',
  5505. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  5506. '/div'
  5507. );
  5508. $this->assertTags($result, $expected);
  5509. }
  5510. /**
  5511. * test File upload input on a model not used in create();
  5512. *
  5513. * @return void
  5514. */
  5515. public function testFileUploadOnOtherModel() {
  5516. $this->Form->create('ValidateUser', array('type' => 'file'));
  5517. $result = $this->Form->file('ValidateProfile.city');
  5518. $expected = array(
  5519. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  5520. );
  5521. $this->assertTags($result, $expected);
  5522. }
  5523. /**
  5524. * testButton method
  5525. *
  5526. * @return void
  5527. */
  5528. public function testButton() {
  5529. $result = $this->Form->button('Hi');
  5530. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5531. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5532. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5533. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5534. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5535. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5536. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5537. $result = $this->Form->button('No type', array('type' => false));
  5538. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5539. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  5540. $this->assertNotRegExp('/\&039/', $result);
  5541. }
  5542. /**
  5543. * Test that button() makes unlocked fields by default.
  5544. *
  5545. * @return void
  5546. */
  5547. public function testButtonUnlockedByDefault() {
  5548. $this->Form->request->params['_Token']['key'] = 'secured';
  5549. $this->Form->button('Save', array('name' => 'save'));
  5550. $this->Form->button('Clear');
  5551. $result = $this->Form->unlockField();
  5552. $this->assertEquals(array('save'), $result);
  5553. }
  5554. /**
  5555. * testPostButton method
  5556. *
  5557. * @return void
  5558. */
  5559. public function testPostButton() {
  5560. $result = $this->Form->postButton('Hi', '/controller/action');
  5561. $this->assertTags($result, array(
  5562. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5563. 'div' => array('style' => 'display:none;'),
  5564. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5565. '/div',
  5566. 'button' => array('type' => 'submit'),
  5567. 'Hi',
  5568. '/button',
  5569. '/form'
  5570. ));
  5571. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5572. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  5573. }
  5574. /**
  5575. * Test that postButton adds _Token fields.
  5576. *
  5577. * @return void
  5578. */
  5579. public function testSecurePostButton() {
  5580. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5581. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5582. $expected = array(
  5583. 'form' => array(
  5584. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5585. ),
  5586. array('div' => array('style' => 'display:none;')),
  5587. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5588. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5589. '/div',
  5590. 'button' => array('type' => 'submit'),
  5591. 'Delete',
  5592. '/button',
  5593. array('div' => array('style' => 'display:none;')),
  5594. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5595. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5596. '/div',
  5597. '/form',
  5598. );
  5599. $this->assertTags($result, $expected);
  5600. }
  5601. /**
  5602. * testPostLink method
  5603. *
  5604. * @return void
  5605. */
  5606. public function testPostLink() {
  5607. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5608. $this->assertTags($result, array(
  5609. 'form' => array(
  5610. 'method' => 'post', 'action' => '/posts/delete/1',
  5611. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5612. ),
  5613. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5614. '/form',
  5615. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5616. 'Delete',
  5617. '/a'
  5618. ));
  5619. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5620. $this->assertTags($result, array(
  5621. 'form' => array(
  5622. 'method' => 'post', 'action' => '/posts/delete/1',
  5623. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5624. ),
  5625. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5626. '/form',
  5627. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5628. 'Delete',
  5629. '/a'
  5630. ));
  5631. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5632. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  5633. }
  5634. /**
  5635. * Test that postLink adds _Token fields.
  5636. *
  5637. * @return void
  5638. */
  5639. public function testSecurePostLink() {
  5640. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5641. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5642. $expected = array(
  5643. 'form' => array(
  5644. 'method' => 'post', 'action' => '/posts/delete/1',
  5645. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5646. ),
  5647. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5648. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5649. 'div' => array('style' => 'display:none;'),
  5650. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5651. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5652. '/div',
  5653. '/form',
  5654. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5655. 'Delete',
  5656. '/a'
  5657. );
  5658. $this->assertTags($result, $expected);
  5659. }
  5660. /**
  5661. * testSubmitButton method
  5662. *
  5663. * @return void
  5664. */
  5665. public function testSubmitButton() {
  5666. $result = $this->Form->submit('');
  5667. $expected = array(
  5668. 'div' => array('class' => 'submit'),
  5669. 'input' => array('type' => 'submit', 'value' => ''),
  5670. '/div'
  5671. );
  5672. $this->assertTags($result, $expected);
  5673. $result = $this->Form->submit('Test Submit');
  5674. $expected = array(
  5675. 'div' => array('class' => 'submit'),
  5676. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5677. '/div'
  5678. );
  5679. $this->assertTags($result, $expected);
  5680. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5681. $expected = array(
  5682. 'span' => array('class' => 'submit'),
  5683. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5684. '/span'
  5685. );
  5686. $this->assertTags($result, $expected);
  5687. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5688. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5689. $this->assertTags($result, $expected);
  5690. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5691. $expected = array(
  5692. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5693. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5694. '/div'
  5695. );
  5696. $this->assertTags($result, $expected);
  5697. $result = $this->Form->submit('Next >');
  5698. $expected = array(
  5699. 'div' => array('class' => 'submit'),
  5700. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5701. '/div'
  5702. );
  5703. $this->assertTags($result, $expected);
  5704. $result = $this->Form->submit('Next >', array('escape' => false));
  5705. $expected = array(
  5706. 'div' => array('class' => 'submit'),
  5707. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5708. '/div'
  5709. );
  5710. $this->assertTags($result, $expected);
  5711. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5712. $expected = array(
  5713. 'div' => array('class' => 'submit'),
  5714. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5715. '/div'
  5716. );
  5717. $this->assertTags($result, $expected);
  5718. $before = '--before--';
  5719. $after = '--after--';
  5720. $result = $this->Form->submit('Test', array('before' => $before));
  5721. $expected = array(
  5722. 'div' => array('class' => 'submit'),
  5723. '--before--',
  5724. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5725. '/div'
  5726. );
  5727. $this->assertTags($result, $expected);
  5728. $result = $this->Form->submit('Test', array('after' => $after));
  5729. $expected = array(
  5730. 'div' => array('class' => 'submit'),
  5731. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5732. '--after--',
  5733. '/div'
  5734. );
  5735. $this->assertTags($result, $expected);
  5736. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5737. $expected = array(
  5738. 'div' => array('class' => 'submit'),
  5739. '--before--',
  5740. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5741. '--after--',
  5742. '/div'
  5743. );
  5744. $this->assertTags($result, $expected);
  5745. }
  5746. /**
  5747. * test image submit types.
  5748. *
  5749. * @return void
  5750. */
  5751. public function testSubmitImage() {
  5752. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5753. $expected = array(
  5754. 'div' => array('class' => 'submit'),
  5755. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5756. '/div'
  5757. );
  5758. $this->assertTags($result, $expected);
  5759. $result = $this->Form->submit('/relative/cake.power.gif');
  5760. $expected = array(
  5761. 'div' => array('class' => 'submit'),
  5762. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5763. '/div'
  5764. );
  5765. $this->assertTags($result, $expected);
  5766. $result = $this->Form->submit('cake.power.gif');
  5767. $expected = array(
  5768. 'div' => array('class' => 'submit'),
  5769. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5770. '/div'
  5771. );
  5772. $this->assertTags($result, $expected);
  5773. $result = $this->Form->submit('Not.an.image');
  5774. $expected = array(
  5775. 'div' => array('class' => 'submit'),
  5776. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5777. '/div'
  5778. );
  5779. $this->assertTags($result, $expected);
  5780. $after = '--after--';
  5781. $before = '--before--';
  5782. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5783. $expected = array(
  5784. 'div' => array('class' => 'submit'),
  5785. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5786. '--after--',
  5787. '/div'
  5788. );
  5789. $this->assertTags($result, $expected);
  5790. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5791. $expected = array(
  5792. 'div' => array('class' => 'submit'),
  5793. '--before--',
  5794. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5795. '/div'
  5796. );
  5797. $this->assertTags($result, $expected);
  5798. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5799. $expected = array(
  5800. 'div' => array('class' => 'submit'),
  5801. '--before--',
  5802. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5803. '--after--',
  5804. '/div'
  5805. );
  5806. $this->assertTags($result, $expected);
  5807. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  5808. $expected = array(
  5809. 'div' => array('class' => 'submit'),
  5810. '--before--',
  5811. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5812. '--after--',
  5813. '/div'
  5814. );
  5815. $this->assertTags($result, $expected);
  5816. }
  5817. /**
  5818. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5819. * be submitted at a time.
  5820. *
  5821. * @return void
  5822. */
  5823. public function testSubmitUnlockedByDefault() {
  5824. $this->Form->request->params['_Token']['key'] = 'secured';
  5825. $this->Form->submit('Go go');
  5826. $this->Form->submit('Save', array('name' => 'save'));
  5827. $result = $this->Form->unlockField();
  5828. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5829. }
  5830. /**
  5831. * Test submit image with timestamps.
  5832. *
  5833. * @return void
  5834. */
  5835. public function testSubmitImageTimestamp() {
  5836. Configure::write('Asset.timestamp', 'force');
  5837. $result = $this->Form->submit('cake.power.gif');
  5838. $expected = array(
  5839. 'div' => array('class' => 'submit'),
  5840. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  5841. '/div'
  5842. );
  5843. $this->assertTags($result, $expected);
  5844. }
  5845. /**
  5846. * test the create() method
  5847. *
  5848. * @return void
  5849. */
  5850. public function testCreate() {
  5851. $result = $this->Form->create('Contact');
  5852. $encoding = strtolower(Configure::read('App.encoding'));
  5853. $expected = array(
  5854. 'form' => array(
  5855. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5856. 'accept-charset' => $encoding
  5857. ),
  5858. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  5859. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5860. '/div'
  5861. );
  5862. $this->assertTags($result, $expected);
  5863. $result = $this->Form->create('Contact', array('type' => 'GET'));
  5864. $expected = array('form' => array(
  5865. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5866. 'accept-charset' => $encoding
  5867. ));
  5868. $this->assertTags($result, $expected);
  5869. $result = $this->Form->create('Contact', array('type' => 'get'));
  5870. $expected = array('form' => array(
  5871. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5872. 'accept-charset' => $encoding
  5873. ));
  5874. $this->assertTags($result, $expected);
  5875. $result = $this->Form->create('Contact', array('type' => 'put'));
  5876. $expected = array(
  5877. 'form' => array(
  5878. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5879. 'accept-charset' => $encoding
  5880. ),
  5881. 'div' => array('style' => 'display:none;'),
  5882. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5883. '/div'
  5884. );
  5885. $this->assertTags($result, $expected);
  5886. $result = $this->Form->create('Contact', array('type' => 'file'));
  5887. $expected = array(
  5888. 'form' => array(
  5889. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5890. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5891. ),
  5892. 'div' => array('style' => 'display:none;'),
  5893. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5894. '/div'
  5895. );
  5896. $this->assertTags($result, $expected);
  5897. $this->Form->request->data['Contact']['id'] = 1;
  5898. $this->Form->request->here = '/contacts/edit/1';
  5899. $this->Form->request['action'] = 'edit';
  5900. $result = $this->Form->create('Contact');
  5901. $expected = array(
  5902. 'form' => array(
  5903. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5904. 'accept-charset' => $encoding
  5905. ),
  5906. 'div' => array('style' => 'display:none;'),
  5907. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5908. '/div'
  5909. );
  5910. $this->assertTags($result, $expected);
  5911. $this->Form->request->data['Contact']['id'] = 1;
  5912. $this->Form->request->here = '/contacts/edit/1';
  5913. $this->Form->request['action'] = 'edit';
  5914. $result = $this->Form->create('Contact', array('type' => 'file'));
  5915. $expected = array(
  5916. 'form' => array(
  5917. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5918. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5919. ),
  5920. 'div' => array('style' => 'display:none;'),
  5921. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5922. '/div'
  5923. );
  5924. $this->assertTags($result, $expected);
  5925. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  5926. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  5927. $expected = array(
  5928. 'form' => array(
  5929. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  5930. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  5931. ),
  5932. 'div' => array('style' => 'display:none;'),
  5933. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5934. '/div'
  5935. );
  5936. $this->assertTags($result, $expected);
  5937. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  5938. $expected = array(
  5939. 'form' => array(
  5940. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  5941. 'accept-charset' => $encoding
  5942. ),
  5943. 'div' => array('style' => 'display:none;'),
  5944. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5945. '/div'
  5946. );
  5947. $this->assertTags($result, $expected);
  5948. $this->Form->request['action'] = 'add';
  5949. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  5950. $expected = array(
  5951. 'form' => array(
  5952. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  5953. 'accept-charset' => $encoding
  5954. ),
  5955. 'div' => array('style' => 'display:none;'),
  5956. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5957. '/div'
  5958. );
  5959. $this->assertTags($result, $expected);
  5960. $result = $this->Form->create('User', array('action' => 'login'));
  5961. $expected = array(
  5962. 'form' => array(
  5963. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  5964. 'accept-charset' => $encoding
  5965. ),
  5966. 'div' => array('style' => 'display:none;'),
  5967. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5968. '/div'
  5969. );
  5970. $this->assertTags($result, $expected);
  5971. $result = $this->Form->create('User', array('url' => '/users/login'));
  5972. $expected = array(
  5973. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  5974. 'div' => array('style' => 'display:none;'),
  5975. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5976. '/div'
  5977. );
  5978. $this->assertTags($result, $expected);
  5979. $this->Form->request['controller'] = 'pages';
  5980. $result = $this->Form->create('User', array('action' => 'signup'));
  5981. $expected = array(
  5982. 'form' => array(
  5983. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  5984. 'accept-charset' => $encoding
  5985. ),
  5986. 'div' => array('style' => 'display:none;'),
  5987. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5988. '/div'
  5989. );
  5990. $this->assertTags($result, $expected);
  5991. $this->Form->request->data = array();
  5992. $this->Form->request['controller'] = 'contacts';
  5993. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  5994. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  5995. $expected = array(
  5996. 'form' => array(
  5997. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  5998. 'accept-charset' => 'utf-8'
  5999. ),
  6000. 'div' => array('style' => 'display:none;'),
  6001. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6002. '/div'
  6003. );
  6004. $this->assertTags($result, $expected);
  6005. }
  6006. /**
  6007. * Test the onsubmit option for create()
  6008. *
  6009. * @return void
  6010. */
  6011. public function testCreateOnSubmit() {
  6012. $this->Form->request->data = array();
  6013. $this->Form->request['controller'] = 'contacts';
  6014. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6015. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  6016. $expected = array(
  6017. 'form' => array(
  6018. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  6019. 'accept-charset' => 'utf-8'
  6020. ),
  6021. 'div' => array('style' => 'display:none;'),
  6022. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6023. '/div'
  6024. );
  6025. $this->assertTags($result, $expected);
  6026. $this->Form->request->data = array();
  6027. $this->Form->request['controller'] = 'contacts';
  6028. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6029. $result = $this->Form->create(array(
  6030. 'url' => array('action' => 'index', 'param'),
  6031. 'default' => false,
  6032. 'onsubmit' => 'someFunction();'
  6033. ));
  6034. $expected = array(
  6035. 'form' => array(
  6036. 'id' => 'ContactAddForm', 'method' => 'post',
  6037. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6038. 'action' => '/contacts/index/param',
  6039. 'accept-charset' => 'utf-8'
  6040. ),
  6041. 'div' => array('style' => 'display:none;'),
  6042. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6043. '/div'
  6044. );
  6045. $this->assertTags($result, $expected);
  6046. }
  6047. /**
  6048. * test create() with automatic url generation
  6049. *
  6050. * @return void
  6051. */
  6052. public function testCreateAutoUrl() {
  6053. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6054. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6055. $this->Form->request->base = '/base_url';
  6056. $result = $this->Form->create('Contact');
  6057. $expected = array(
  6058. 'form' => array(
  6059. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6060. 'accept-charset' => 'utf-8'
  6061. ),
  6062. 'div' => array('style' => 'display:none;'),
  6063. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6064. '/div'
  6065. );
  6066. $this->assertTags($result, $expected);
  6067. $this->Form->request['action'] = 'delete';
  6068. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6069. $this->Form->request->base = '/base_url';
  6070. $result = $this->Form->create('Contact');
  6071. $expected = array(
  6072. 'form' => array(
  6073. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6074. 'accept-charset' => 'utf-8'
  6075. ),
  6076. 'div' => array('style' => 'display:none;'),
  6077. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6078. '/div'
  6079. );
  6080. $this->assertTags($result, $expected);
  6081. }
  6082. /**
  6083. * test create() with a custom route
  6084. *
  6085. * @return void
  6086. */
  6087. public function testCreateCustomRoute() {
  6088. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6089. $encoding = strtolower(Configure::read('App.encoding'));
  6090. $result = $this->Form->create('User', array('action' => 'login'));
  6091. $expected = array(
  6092. 'form' => array(
  6093. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6094. 'accept-charset' => $encoding
  6095. ),
  6096. 'div' => array('style' => 'display:none;'),
  6097. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6098. '/div'
  6099. );
  6100. $this->assertTags($result, $expected);
  6101. }
  6102. /**
  6103. * test that inputDefaults are stored and used.
  6104. *
  6105. * @return void
  6106. */
  6107. public function testCreateWithInputDefaults() {
  6108. $this->Form->create('User', array(
  6109. 'inputDefaults' => array(
  6110. 'div' => false,
  6111. 'label' => false,
  6112. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6113. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6114. )
  6115. ));
  6116. $result = $this->Form->input('username');
  6117. $expected = array(
  6118. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6119. );
  6120. $this->assertTags($result, $expected);
  6121. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6122. $expected = array(
  6123. 'div' => array('class' => 'input text'),
  6124. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6125. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6126. '/div'
  6127. );
  6128. $this->assertTags($result, $expected);
  6129. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  6130. $expected = array(
  6131. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6132. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  6133. );
  6134. $this->assertTags($result, $expected);
  6135. $this->Form->create('User', array(
  6136. 'inputDefaults' => array(
  6137. 'div' => false,
  6138. 'label' => array('class' => 'nice', 'for' => 'changed'),
  6139. )
  6140. ));
  6141. $result = $this->Form->input('username', array('div' => true));
  6142. $expected = array(
  6143. 'div' => array('class' => 'input text'),
  6144. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  6145. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6146. '/div'
  6147. );
  6148. $this->assertTags($result, $expected);
  6149. }
  6150. /**
  6151. * test automatic accept-charset overriding
  6152. *
  6153. * @return void
  6154. */
  6155. public function testCreateWithAcceptCharset() {
  6156. $result = $this->Form->create('UserForm', array(
  6157. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  6158. )
  6159. );
  6160. $expected = array(
  6161. 'form' => array(
  6162. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6163. 'accept-charset' => 'iso-8859-1'
  6164. ),
  6165. 'div' => array('style' => 'display:none;'),
  6166. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6167. '/div'
  6168. );
  6169. $this->assertTags($result, $expected);
  6170. }
  6171. /**
  6172. * Test base form url when url param is passed with multiple parameters (&)
  6173. *
  6174. */
  6175. public function testCreateQuerystringrequest() {
  6176. $encoding = strtolower(Configure::read('App.encoding'));
  6177. $result = $this->Form->create('Contact', array(
  6178. 'type' => 'post',
  6179. 'escape' => false,
  6180. 'url' => array(
  6181. 'controller' => 'controller',
  6182. 'action' => 'action',
  6183. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6184. )
  6185. ));
  6186. $expected = array(
  6187. 'form' => array(
  6188. 'id' => 'ContactAddForm',
  6189. 'method' => 'post',
  6190. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6191. 'accept-charset' => $encoding
  6192. ),
  6193. 'div' => array('style' => 'display:none;'),
  6194. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6195. '/div'
  6196. );
  6197. $this->assertTags($result, $expected);
  6198. $result = $this->Form->create('Contact', array(
  6199. 'type' => 'post',
  6200. 'url' => array(
  6201. 'controller' => 'controller',
  6202. 'action' => 'action',
  6203. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6204. )
  6205. ));
  6206. $expected = array(
  6207. 'form' => array(
  6208. 'id' => 'ContactAddForm',
  6209. 'method' => 'post',
  6210. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6211. 'accept-charset' => $encoding
  6212. ),
  6213. 'div' => array('style' => 'display:none;'),
  6214. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6215. '/div'
  6216. );
  6217. $this->assertTags($result, $expected);
  6218. }
  6219. /**
  6220. * test that create() doesn't cause errors by multiple id's being in the primary key
  6221. * as could happen with multiple select or checkboxes.
  6222. *
  6223. * @return void
  6224. */
  6225. public function testCreateWithMultipleIdInData() {
  6226. $encoding = strtolower(Configure::read('App.encoding'));
  6227. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6228. $result = $this->Form->create('Contact');
  6229. $expected = array(
  6230. 'form' => array(
  6231. 'id' => 'ContactAddForm',
  6232. 'method' => 'post',
  6233. 'action' => '/contacts/add',
  6234. 'accept-charset' => $encoding
  6235. ),
  6236. 'div' => array('style' => 'display:none;'),
  6237. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6238. '/div'
  6239. );
  6240. $this->assertTags($result, $expected);
  6241. }
  6242. /**
  6243. * test that create() doesn't add in extra passed params.
  6244. *
  6245. * @return void
  6246. */
  6247. public function testCreatePassedArgs() {
  6248. $encoding = strtolower(Configure::read('App.encoding'));
  6249. $this->Form->request->data['Contact']['id'] = 1;
  6250. $result = $this->Form->create('Contact', array(
  6251. 'type' => 'post',
  6252. 'escape' => false,
  6253. 'url' => array(
  6254. 'action' => 'edit',
  6255. 'myparam'
  6256. )
  6257. ));
  6258. $expected = array(
  6259. 'form' => array(
  6260. 'id' => 'ContactAddForm',
  6261. 'method' => 'post',
  6262. 'action' => '/contacts/edit/myparam',
  6263. 'accept-charset' => $encoding
  6264. ),
  6265. 'div' => array('style' => 'display:none;'),
  6266. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6267. '/div'
  6268. );
  6269. $this->assertTags($result, $expected);
  6270. }
  6271. /**
  6272. * test creating a get form, and get form inputs.
  6273. *
  6274. * @return void
  6275. */
  6276. public function testGetFormCreate() {
  6277. $encoding = strtolower(Configure::read('App.encoding'));
  6278. $result = $this->Form->create('Contact', array('type' => 'get'));
  6279. $this->assertTags($result, array('form' => array(
  6280. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6281. 'accept-charset' => $encoding
  6282. )));
  6283. $result = $this->Form->text('Contact.name');
  6284. $this->assertTags($result, array('input' => array(
  6285. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6286. )));
  6287. $result = $this->Form->password('password');
  6288. $this->assertTags($result, array('input' => array(
  6289. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6290. )));
  6291. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6292. $result = $this->Form->text('user_form');
  6293. $this->assertTags($result, array('input' => array(
  6294. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6295. )));
  6296. }
  6297. /**
  6298. * test get form, and inputs when the model param is false
  6299. *
  6300. * @return void
  6301. */
  6302. public function testGetFormWithFalseModel() {
  6303. $encoding = strtolower(Configure::read('App.encoding'));
  6304. $this->Form->request['controller'] = 'contact_test';
  6305. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6306. $expected = array('form' => array(
  6307. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6308. 'accept-charset' => $encoding
  6309. ));
  6310. $this->assertTags($result, $expected);
  6311. $result = $this->Form->text('reason');
  6312. $expected = array(
  6313. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6314. );
  6315. $this->assertTags($result, $expected);
  6316. }
  6317. /**
  6318. * test that datetime() works with GET style forms.
  6319. *
  6320. * @return void
  6321. */
  6322. public function testDateTimeWithGetForms() {
  6323. extract($this->dateRegex);
  6324. $this->Form->create('Contact', array('type' => 'get'));
  6325. $result = $this->Form->datetime('created');
  6326. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6327. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6328. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6329. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6330. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6331. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6332. }
  6333. /**
  6334. * testEditFormWithData method
  6335. *
  6336. * test auto populating form elements from submitted data.
  6337. *
  6338. * @return void
  6339. */
  6340. public function testEditFormWithData() {
  6341. $this->Form->request->data = array('Person' => array(
  6342. 'id' => 1,
  6343. 'first_name' => 'Nate',
  6344. 'last_name' => 'Abele',
  6345. 'email' => 'nate@example.com'
  6346. ));
  6347. $this->Form->request->addParams(array(
  6348. 'models' => array('Person'),
  6349. 'controller' => 'people',
  6350. 'action' => 'add'
  6351. ));
  6352. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6353. $this->Form->create();
  6354. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6355. $expected = array(
  6356. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6357. 'select' => array(
  6358. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6359. ),
  6360. array('option' => array('value' => 1)), 'Nate', '/option',
  6361. array('option' => array('value' => 2)), 'Garrett', '/option',
  6362. array('option' => array('value' => 3)), 'Larry', '/option',
  6363. '/select'
  6364. );
  6365. $this->assertTags($result, $expected);
  6366. }
  6367. /**
  6368. * Test that required fields are created for various types of validation.
  6369. *
  6370. * @return void
  6371. */
  6372. public function testFormInputRequiredDetection() {
  6373. $this->Form->create('Contact');
  6374. $result = $this->Form->input('Contact.non_existing');
  6375. $expected = array(
  6376. 'div' => array('class' => 'input text'),
  6377. 'label' => array('for' => 'ContactNonExisting'),
  6378. 'Non Existing',
  6379. '/label',
  6380. 'input' => array(
  6381. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  6382. 'id' => 'ContactNonExisting'
  6383. ),
  6384. '/div'
  6385. );
  6386. $this->assertTags($result, $expected);
  6387. $result = $this->Form->input('Contact.imrequired');
  6388. $expected = array(
  6389. 'div' => array('class' => 'input text required'),
  6390. 'label' => array('for' => 'ContactImrequired'),
  6391. 'Imrequired',
  6392. '/label',
  6393. 'input' => array(
  6394. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  6395. 'id' => 'ContactImrequired'
  6396. ),
  6397. '/div'
  6398. );
  6399. $this->assertTags($result, $expected);
  6400. $result = $this->Form->input('Contact.imalsorequired');
  6401. $expected = array(
  6402. 'div' => array('class' => 'input text required'),
  6403. 'label' => array('for' => 'ContactImalsorequired'),
  6404. 'Imalsorequired',
  6405. '/label',
  6406. 'input' => array(
  6407. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  6408. 'id' => 'ContactImalsorequired'
  6409. ),
  6410. '/div'
  6411. );
  6412. $this->assertTags($result, $expected);
  6413. $result = $this->Form->input('Contact.imrequiredtoo');
  6414. $expected = array(
  6415. 'div' => array('class' => 'input text required'),
  6416. 'label' => array('for' => 'ContactImrequiredtoo'),
  6417. 'Imrequiredtoo',
  6418. '/label',
  6419. 'input' => array(
  6420. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  6421. 'id' => 'ContactImrequiredtoo'
  6422. ),
  6423. '/div'
  6424. );
  6425. $this->assertTags($result, $expected);
  6426. $result = $this->Form->input('Contact.required_one');
  6427. $expected = array(
  6428. 'div' => array('class' => 'input text required'),
  6429. 'label' => array('for' => 'ContactRequiredOne'),
  6430. 'Required One',
  6431. '/label',
  6432. 'input' => array(
  6433. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  6434. 'id' => 'ContactRequiredOne'
  6435. ),
  6436. '/div'
  6437. );
  6438. $this->assertTags($result, $expected);
  6439. $result = $this->Form->input('Contact.string_required');
  6440. $expected = array(
  6441. 'div' => array('class' => 'input text required'),
  6442. 'label' => array('for' => 'ContactStringRequired'),
  6443. 'String Required',
  6444. '/label',
  6445. 'input' => array(
  6446. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  6447. 'id' => 'ContactStringRequired'
  6448. ),
  6449. '/div'
  6450. );
  6451. $this->assertTags($result, $expected);
  6452. $result = $this->Form->input('Contact.imnotrequired');
  6453. $expected = array(
  6454. 'div' => array('class' => 'input text'),
  6455. 'label' => array('for' => 'ContactImnotrequired'),
  6456. 'Imnotrequired',
  6457. '/label',
  6458. 'input' => array(
  6459. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  6460. 'id' => 'ContactImnotrequired'
  6461. ),
  6462. '/div'
  6463. );
  6464. $this->assertTags($result, $expected);
  6465. $result = $this->Form->input('Contact.imalsonotrequired');
  6466. $expected = array(
  6467. 'div' => array('class' => 'input text'),
  6468. 'label' => array('for' => 'ContactImalsonotrequired'),
  6469. 'Imalsonotrequired',
  6470. '/label',
  6471. 'input' => array(
  6472. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  6473. 'id' => 'ContactImalsonotrequired'
  6474. ),
  6475. '/div'
  6476. );
  6477. $this->assertTags($result, $expected);
  6478. $result = $this->Form->input('Contact.imnotrequiredeither');
  6479. $expected = array(
  6480. 'div' => array('class' => 'input text'),
  6481. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6482. 'Imnotrequiredeither',
  6483. '/label',
  6484. 'input' => array(
  6485. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  6486. 'id' => 'ContactImnotrequiredeither'
  6487. ),
  6488. '/div'
  6489. );
  6490. $this->assertTags($result, $expected);
  6491. }
  6492. /**
  6493. * testFormMagicInput method
  6494. *
  6495. * @return void
  6496. */
  6497. public function testFormMagicInput() {
  6498. $encoding = strtolower(Configure::read('App.encoding'));
  6499. $result = $this->Form->create('Contact');
  6500. $expected = array(
  6501. 'form' => array(
  6502. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6503. 'accept-charset' => $encoding
  6504. ),
  6505. 'div' => array('style' => 'display:none;'),
  6506. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6507. '/div'
  6508. );
  6509. $this->assertTags($result, $expected);
  6510. $result = $this->Form->input('name');
  6511. $expected = array(
  6512. 'div' => array('class' => 'input text'),
  6513. 'label' => array('for' => 'ContactName'),
  6514. 'Name',
  6515. '/label',
  6516. 'input' => array(
  6517. 'type' => 'text', 'name' => 'data[Contact][name]',
  6518. 'id' => 'ContactName', 'maxlength' => '255'
  6519. ),
  6520. '/div'
  6521. );
  6522. $this->assertTags($result, $expected);
  6523. $result = $this->Form->input('non_existing_field_in_contact_model');
  6524. $expected = array(
  6525. 'div' => array('class' => 'input text'),
  6526. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  6527. 'Non Existing Field In Contact Model',
  6528. '/label',
  6529. 'input' => array(
  6530. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  6531. 'id' => 'ContactNonExistingFieldInContactModel'
  6532. ),
  6533. '/div'
  6534. );
  6535. $this->assertTags($result, $expected);
  6536. $result = $this->Form->input('Address.street');
  6537. $expected = array(
  6538. 'div' => array('class' => 'input text'),
  6539. 'label' => array('for' => 'AddressStreet'),
  6540. 'Street',
  6541. '/label',
  6542. 'input' => array(
  6543. 'type' => 'text', 'name' => 'data[Address][street]',
  6544. 'id' => 'AddressStreet'
  6545. ),
  6546. '/div'
  6547. );
  6548. $this->assertTags($result, $expected);
  6549. $result = $this->Form->input('Address.non_existing_field_in_model');
  6550. $expected = array(
  6551. 'div' => array('class' => 'input text'),
  6552. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  6553. 'Non Existing Field In Model',
  6554. '/label',
  6555. 'input' => array(
  6556. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  6557. 'id' => 'AddressNonExistingFieldInModel'
  6558. ),
  6559. '/div'
  6560. );
  6561. $this->assertTags($result, $expected);
  6562. $result = $this->Form->input('name', array('div' => false));
  6563. $expected = array(
  6564. 'label' => array('for' => 'ContactName'),
  6565. 'Name',
  6566. '/label',
  6567. 'input' => array(
  6568. 'type' => 'text', 'name' => 'data[Contact][name]',
  6569. 'id' => 'ContactName', 'maxlength' => '255'
  6570. )
  6571. );
  6572. $this->assertTags($result, $expected);
  6573. extract($this->dateRegex);
  6574. $now = strtotime('now');
  6575. $result = $this->Form->input('Contact.published', array('div' => false));
  6576. $expected = array(
  6577. 'label' => array('for' => 'ContactPublishedMonth'),
  6578. 'Published',
  6579. '/label',
  6580. array('select' => array(
  6581. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  6582. )),
  6583. $monthsRegex,
  6584. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6585. date('F', $now),
  6586. '/option',
  6587. '*/select',
  6588. '-',
  6589. array('select' => array(
  6590. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  6591. )),
  6592. $daysRegex,
  6593. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6594. date('j', $now),
  6595. '/option',
  6596. '*/select',
  6597. '-',
  6598. array('select' => array(
  6599. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  6600. )),
  6601. $yearsRegex,
  6602. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6603. date('Y', $now),
  6604. '*/select'
  6605. );
  6606. $this->assertTags($result, $expected);
  6607. $result = $this->Form->input('Contact.updated', array('div' => false));
  6608. $expected = array(
  6609. 'label' => array('for' => 'ContactUpdatedMonth'),
  6610. 'Updated',
  6611. '/label',
  6612. array('select' => array(
  6613. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  6614. )),
  6615. $monthsRegex,
  6616. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6617. date('F', $now),
  6618. '/option',
  6619. '*/select',
  6620. '-',
  6621. array('select' => array(
  6622. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  6623. )),
  6624. $daysRegex,
  6625. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6626. date('j', $now),
  6627. '/option',
  6628. '*/select',
  6629. '-',
  6630. array('select' => array(
  6631. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  6632. )),
  6633. $yearsRegex,
  6634. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6635. date('Y', $now),
  6636. '*/select'
  6637. );
  6638. $this->assertTags($result, $expected);
  6639. $result = $this->Form->input('UserForm.stuff');
  6640. $expected = array(
  6641. 'div' => array('class' => 'input text'),
  6642. 'label' => array('for' => 'UserFormStuff'),
  6643. 'Stuff',
  6644. '/label',
  6645. 'input' => array(
  6646. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  6647. 'id' => 'UserFormStuff', 'maxlength' => 10
  6648. ),
  6649. '/div'
  6650. );
  6651. $this->assertTags($result, $expected);
  6652. }
  6653. /**
  6654. * testForMagicInputNonExistingNorValidated method
  6655. *
  6656. * @return void
  6657. */
  6658. public function testForMagicInputNonExistingNorValidated() {
  6659. $encoding = strtolower(Configure::read('App.encoding'));
  6660. $result = $this->Form->create('Contact');
  6661. $expected = array(
  6662. 'form' => array(
  6663. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6664. 'accept-charset' => $encoding
  6665. ),
  6666. 'div' => array('style' => 'display:none;'),
  6667. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6668. '/div'
  6669. );
  6670. $this->assertTags($result, $expected);
  6671. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6672. $expected = array(
  6673. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6674. 'Non Existing Nor Validated',
  6675. '/label',
  6676. 'input' => array(
  6677. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6678. 'id' => 'ContactNonExistingNorValidated'
  6679. )
  6680. );
  6681. $this->assertTags($result, $expected);
  6682. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6683. 'div' => false, 'value' => 'my value'
  6684. ));
  6685. $expected = array(
  6686. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6687. 'Non Existing Nor Validated',
  6688. '/label',
  6689. 'input' => array(
  6690. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6691. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6692. )
  6693. );
  6694. $this->assertTags($result, $expected);
  6695. $this->Form->request->data = array(
  6696. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6697. ));
  6698. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6699. $expected = array(
  6700. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6701. 'Non Existing Nor Validated',
  6702. '/label',
  6703. 'input' => array(
  6704. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6705. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6706. )
  6707. );
  6708. $this->assertTags($result, $expected);
  6709. }
  6710. /**
  6711. * testFormMagicInputLabel method
  6712. *
  6713. * @return void
  6714. */
  6715. public function testFormMagicInputLabel() {
  6716. $encoding = strtolower(Configure::read('App.encoding'));
  6717. $result = $this->Form->create('Contact');
  6718. $expected = array(
  6719. 'form' => array(
  6720. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6721. 'accept-charset' => $encoding
  6722. ),
  6723. 'div' => array('style' => 'display:none;'),
  6724. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6725. '/div'
  6726. );
  6727. $this->assertTags($result, $expected);
  6728. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  6729. $this->assertTags($result, array('input' => array(
  6730. 'name' => 'data[Contact][name]', 'type' => 'text',
  6731. 'id' => 'ContactName', 'maxlength' => '255')
  6732. ));
  6733. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  6734. $expected = array(
  6735. 'label' => array('for' => 'ContactName'),
  6736. 'My label',
  6737. '/label',
  6738. 'input' => array(
  6739. 'type' => 'text', 'name' => 'data[Contact][name]',
  6740. 'id' => 'ContactName', 'maxlength' => '255'
  6741. )
  6742. );
  6743. $this->assertTags($result, $expected);
  6744. $result = $this->Form->input('Contact.name', array(
  6745. 'div' => false, 'label' => array('class' => 'mandatory')
  6746. ));
  6747. $expected = array(
  6748. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6749. 'Name',
  6750. '/label',
  6751. 'input' => array(
  6752. 'type' => 'text', 'name' => 'data[Contact][name]',
  6753. 'id' => 'ContactName', 'maxlength' => '255'
  6754. )
  6755. );
  6756. $this->assertTags($result, $expected);
  6757. $result = $this->Form->input('Contact.name', array(
  6758. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  6759. ));
  6760. $expected = array(
  6761. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6762. 'My label',
  6763. '/label',
  6764. 'input' => array(
  6765. 'type' => 'text', 'name' => 'data[Contact][name]',
  6766. 'id' => 'ContactName', 'maxlength' => '255'
  6767. )
  6768. );
  6769. $this->assertTags($result, $expected);
  6770. $result = $this->Form->input('Contact.name', array(
  6771. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  6772. ));
  6773. $expected = array(
  6774. 'label' => array('for' => 'my_id'),
  6775. 'Name',
  6776. '/label',
  6777. 'input' => array(
  6778. 'type' => 'text', 'name' => 'data[Contact][name]',
  6779. 'id' => 'my_id', 'maxlength' => '255'
  6780. )
  6781. );
  6782. $this->assertTags($result, $expected);
  6783. $result = $this->Form->input('1.id');
  6784. $this->assertTags($result, array('input' => array(
  6785. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6786. 'id' => 'Contact1Id'
  6787. )));
  6788. $result = $this->Form->input("1.name");
  6789. $expected = array(
  6790. 'div' => array('class' => 'input text'),
  6791. 'label' => array('for' => 'Contact1Name'),
  6792. 'Name',
  6793. '/label',
  6794. 'input' => array(
  6795. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  6796. 'id' => 'Contact1Name', 'maxlength' => '255'
  6797. ),
  6798. '/div'
  6799. );
  6800. $this->assertTags($result, $expected);
  6801. $result = $this->Form->input('Contact.1.id');
  6802. $this->assertTags($result, array(
  6803. 'input' => array(
  6804. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6805. 'id' => 'Contact1Id'
  6806. )
  6807. ));
  6808. $result = $this->Form->input("Model.1.name");
  6809. $expected = array(
  6810. 'div' => array('class' => 'input text'),
  6811. 'label' => array('for' => 'Model1Name'),
  6812. 'Name',
  6813. '/label',
  6814. 'input' => array(
  6815. 'type' => 'text', 'name' => 'data[Model][1][name]',
  6816. 'id' => 'Model1Name'
  6817. ),
  6818. '/div'
  6819. );
  6820. $this->assertTags($result, $expected);
  6821. }
  6822. /**
  6823. * testFormEnd method
  6824. *
  6825. * @return void
  6826. */
  6827. public function testFormEnd() {
  6828. $this->assertEquals('</form>', $this->Form->end());
  6829. $result = $this->Form->end('');
  6830. $expected = array(
  6831. 'div' => array('class' => 'submit'),
  6832. 'input' => array('type' => 'submit', 'value' => ''),
  6833. '/div',
  6834. '/form'
  6835. );
  6836. $this->assertTags($result, $expected);
  6837. $result = $this->Form->end(array('label' => ''));
  6838. $expected = array(
  6839. 'div' => array('class' => 'submit'),
  6840. 'input' => array('type' => 'submit', 'value' => ''),
  6841. '/div',
  6842. '/form'
  6843. );
  6844. $this->assertTags($result, $expected);
  6845. $result = $this->Form->end('save');
  6846. $expected = array(
  6847. 'div' => array('class' => 'submit'),
  6848. 'input' => array('type' => 'submit', 'value' => 'save'),
  6849. '/div',
  6850. '/form'
  6851. );
  6852. $this->assertTags($result, $expected);
  6853. $result = $this->Form->end(array('label' => 'save'));
  6854. $expected = array(
  6855. 'div' => array('class' => 'submit'),
  6856. 'input' => array('type' => 'submit', 'value' => 'save'),
  6857. '/div',
  6858. '/form'
  6859. );
  6860. $this->assertTags($result, $expected);
  6861. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  6862. $expected = array(
  6863. 'div' => array('class' => 'submit'),
  6864. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6865. '/div',
  6866. '/form'
  6867. );
  6868. $this->assertTags($result, $expected);
  6869. $result = $this->Form->end(array('name' => 'Whatever'));
  6870. $expected = array(
  6871. 'div' => array('class' => 'submit'),
  6872. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  6873. '/div',
  6874. '/form'
  6875. );
  6876. $this->assertTags($result, $expected);
  6877. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  6878. $expected = array(
  6879. 'div' => array('class' => 'good'),
  6880. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6881. '/div',
  6882. '/form'
  6883. );
  6884. $this->assertTags($result, $expected);
  6885. $result = $this->Form->end(array(
  6886. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  6887. ));
  6888. $expected = array(
  6889. 'div' => array('class' => 'good'),
  6890. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6891. '/div',
  6892. '/form'
  6893. );
  6894. $this->assertTags($result, $expected);
  6895. }
  6896. /**
  6897. * testMultipleFormWithIdFields method
  6898. *
  6899. * @return void
  6900. */
  6901. public function testMultipleFormWithIdFields() {
  6902. $this->Form->create('UserForm');
  6903. $result = $this->Form->input('id');
  6904. $this->assertTags($result, array('input' => array(
  6905. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  6906. )));
  6907. $result = $this->Form->input('ValidateItem.id');
  6908. $this->assertTags($result, array('input' => array(
  6909. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  6910. 'id' => 'ValidateItemId'
  6911. )));
  6912. $result = $this->Form->input('ValidateUser.id');
  6913. $this->assertTags($result, array('input' => array(
  6914. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  6915. 'id' => 'ValidateUserId'
  6916. )));
  6917. }
  6918. /**
  6919. * testDbLessModel method
  6920. *
  6921. * @return void
  6922. */
  6923. public function testDbLessModel() {
  6924. $this->Form->create('TestMail');
  6925. $result = $this->Form->input('name');
  6926. $expected = array(
  6927. 'div' => array('class' => 'input text'),
  6928. 'label' => array('for' => 'TestMailName'),
  6929. 'Name',
  6930. '/label',
  6931. 'input' => array(
  6932. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6933. 'id' => 'TestMailName'
  6934. ),
  6935. '/div'
  6936. );
  6937. $this->assertTags($result, $expected);
  6938. ClassRegistry::init('TestMail');
  6939. $this->Form->create('TestMail');
  6940. $result = $this->Form->input('name');
  6941. $expected = array(
  6942. 'div' => array('class' => 'input text'),
  6943. 'label' => array('for' => 'TestMailName'),
  6944. 'Name',
  6945. '/label',
  6946. 'input' => array(
  6947. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6948. 'id' => 'TestMailName'
  6949. ),
  6950. '/div'
  6951. );
  6952. $this->assertTags($result, $expected);
  6953. }
  6954. /**
  6955. * testBrokenness method
  6956. *
  6957. * @return void
  6958. */
  6959. public function testBrokenness() {
  6960. /*
  6961. * #4 This test has two parents and four children. By default (as of r7117) both
  6962. * parents are show but the first parent is missing a child. This is the inconsistency
  6963. * in the default behaviour - one parent has all children, the other does not - dependent
  6964. * on the data values.
  6965. */
  6966. $result = $this->Form->select('Model.field', array(
  6967. 'Fred' => array(
  6968. 'freds_son_1' => 'Fred',
  6969. 'freds_son_2' => 'Freddie'
  6970. ),
  6971. 'Bert' => array(
  6972. 'berts_son_1' => 'Albert',
  6973. 'berts_son_2' => 'Bertie')
  6974. ),
  6975. array('showParents' => true, 'empty' => false)
  6976. );
  6977. $expected = array(
  6978. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6979. array('optgroup' => array('label' => 'Fred')),
  6980. array('option' => array('value' => 'freds_son_1')),
  6981. 'Fred',
  6982. '/option',
  6983. array('option' => array('value' => 'freds_son_2')),
  6984. 'Freddie',
  6985. '/option',
  6986. '/optgroup',
  6987. array('optgroup' => array('label' => 'Bert')),
  6988. array('option' => array('value' => 'berts_son_1')),
  6989. 'Albert',
  6990. '/option',
  6991. array('option' => array('value' => 'berts_son_2')),
  6992. 'Bertie',
  6993. '/option',
  6994. '/optgroup',
  6995. '/select'
  6996. );
  6997. $this->assertTags($result, $expected);
  6998. /*
  6999. * #2 This is structurally identical to the test above (#1) - only the parent name has
  7000. * changed, so we should expect the same select list data, just with a different name
  7001. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  7002. * This is where data corruption can occur, because when a select value is missing from
  7003. * a list a form will substitute the first value in the list - without the user knowing.
  7004. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  7005. * affect the availability of 3 => 'Three' as a valid option.
  7006. */
  7007. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  7008. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  7009. ));
  7010. $result = $this->Form->select(
  7011. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  7012. );
  7013. $expected = array(
  7014. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7015. array('option' => array('value' => 1)),
  7016. 'One',
  7017. '/option',
  7018. array('option' => array('value' => 2)),
  7019. 'Two',
  7020. '/option',
  7021. array('optgroup' => array('label' => 'Three')),
  7022. array('option' => array('value' => 3)),
  7023. 'Three',
  7024. '/option',
  7025. array('option' => array('value' => 4)),
  7026. 'Four',
  7027. '/option',
  7028. array('option' => array('value' => 5)),
  7029. 'Five',
  7030. '/option',
  7031. '/optgroup',
  7032. '/select'
  7033. );
  7034. $this->assertTags($result, $expected);
  7035. }
  7036. /**
  7037. * Test the generation of fields for a multi record form.
  7038. *
  7039. * @return void
  7040. */
  7041. public function testMultiRecordForm() {
  7042. $this->Form->create('ValidateProfile');
  7043. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  7044. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  7045. $expected = array(
  7046. 'div' => array('class' => 'input textarea'),
  7047. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  7048. 'Name',
  7049. '/label',
  7050. 'textarea' => array(
  7051. 'id' => 'ValidateProfile1ValidateItem2Name',
  7052. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  7053. 'cols' => 30,
  7054. 'rows' => 6
  7055. ),
  7056. 'Value',
  7057. '/textarea',
  7058. '/div'
  7059. );
  7060. $this->assertTags($result, $expected);
  7061. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
  7062. $expected = array(
  7063. 'div' => array('class' => 'input date'),
  7064. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7065. 'Created',
  7066. '/label',
  7067. array('select' => array(
  7068. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  7069. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7070. )
  7071. ),
  7072. array('option' => array('value' => '')), '/option',
  7073. $this->dateRegex['monthsRegex'],
  7074. '/select', '-',
  7075. array('select' => array(
  7076. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  7077. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7078. )
  7079. ),
  7080. array('option' => array('value' => '')), '/option',
  7081. $this->dateRegex['daysRegex'],
  7082. '/select', '-',
  7083. array('select' => array(
  7084. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  7085. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7086. )
  7087. ),
  7088. array('option' => array('value' => '')), '/option',
  7089. $this->dateRegex['yearsRegex'],
  7090. '/select',
  7091. '/div'
  7092. );
  7093. $this->assertTags($result, $expected);
  7094. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7095. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7096. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7097. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7098. $expected = array(
  7099. 'div' => array('class' => 'input select error'),
  7100. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7101. 'Profile',
  7102. '/label',
  7103. 'select' => array(
  7104. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7105. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7106. 'class' => 'form-error'
  7107. ),
  7108. '/select',
  7109. array('div' => array('class' => 'error-message')),
  7110. 'Error',
  7111. '/div',
  7112. '/div'
  7113. );
  7114. $this->assertTags($result, $expected);
  7115. }
  7116. /**
  7117. * test the correct display of multi-record form validation errors.
  7118. *
  7119. * @return void
  7120. */
  7121. public function testMultiRecordFormValidationErrors() {
  7122. $this->Form->create('ValidateProfile');
  7123. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7124. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7125. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7126. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7127. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7128. $result = $this->Form->error('ValidateProfile.2.city');
  7129. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7130. $result = $this->Form->error('2.city');
  7131. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7132. }
  7133. /**
  7134. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7135. *
  7136. * @return void
  7137. */
  7138. public function testInputTemplate() {
  7139. $result = $this->Form->input('Contact.email', array(
  7140. 'type' => 'text', 'format' => array('input')
  7141. ));
  7142. $expected = array(
  7143. 'div' => array('class' => 'input text'),
  7144. 'input' => array(
  7145. 'type' => 'text', 'name' => 'data[Contact][email]',
  7146. 'id' => 'ContactEmail'
  7147. ),
  7148. '/div'
  7149. );
  7150. $this->assertTags($result, $expected);
  7151. $result = $this->Form->input('Contact.email', array(
  7152. 'type' => 'text', 'format' => array('input', 'label'),
  7153. 'label' => '<em>Email (required)</em>'
  7154. ));
  7155. $expected = array(
  7156. 'div' => array('class' => 'input text'),
  7157. array('input' => array(
  7158. 'type' => 'text', 'name' => 'data[Contact][email]',
  7159. 'id' => 'ContactEmail'
  7160. )),
  7161. 'label' => array('for' => 'ContactEmail'),
  7162. 'em' => array(),
  7163. 'Email (required)',
  7164. '/em',
  7165. '/label',
  7166. '/div'
  7167. );
  7168. $this->assertTags($result, $expected);
  7169. $result = $this->Form->input('Contact.email', array(
  7170. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7171. 'between' => '<div>Something in the middle</div>',
  7172. 'after' => '<span>Some text at the end</span>'
  7173. ));
  7174. $expected = array(
  7175. 'div' => array('class' => 'input text'),
  7176. array('input' => array(
  7177. 'type' => 'text', 'name' => 'data[Contact][email]',
  7178. 'id' => 'ContactEmail'
  7179. )),
  7180. array('div' => array()),
  7181. 'Something in the middle',
  7182. '/div',
  7183. 'label' => array('for' => 'ContactEmail'),
  7184. 'Email',
  7185. '/label',
  7186. 'span' => array(),
  7187. 'Some text at the end',
  7188. '/span',
  7189. '/div'
  7190. );
  7191. $this->assertTags($result, $expected);
  7192. $result = $this->Form->input('Contact.method', array(
  7193. 'type' => 'radio',
  7194. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7195. 'between' => 'I am between',
  7196. ));
  7197. $expected = array(
  7198. 'div' => array('class' => 'input radio'),
  7199. 'fieldset' => array(),
  7200. 'legend' => array(),
  7201. 'Method',
  7202. '/legend',
  7203. 'I am between',
  7204. 'input' => array(
  7205. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  7206. 'value' => '', 'id' => 'ContactMethod_'
  7207. ),
  7208. array('input' => array(
  7209. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7210. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7211. )),
  7212. array('label' => array('for' => 'ContactMethodEmail')),
  7213. 'Email',
  7214. '/label',
  7215. array('input' => array(
  7216. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7217. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7218. )),
  7219. array('label' => array('for' => 'ContactMethodPigeon')),
  7220. 'Pigeon',
  7221. '/label',
  7222. '/fieldset',
  7223. '/div',
  7224. );
  7225. $this->assertTags($result, $expected);
  7226. }
  7227. /**
  7228. * test that some html5 inputs + FormHelper::__call() work
  7229. *
  7230. * @return void
  7231. */
  7232. public function testHtml5Inputs() {
  7233. $result = $this->Form->email('User.email');
  7234. $expected = array(
  7235. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  7236. );
  7237. $this->assertTags($result, $expected);
  7238. $result = $this->Form->search('User.query');
  7239. $expected = array(
  7240. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  7241. );
  7242. $this->assertTags($result, $expected);
  7243. $result = $this->Form->search('User.query', array('value' => 'test'));
  7244. $expected = array(
  7245. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7246. );
  7247. $this->assertTags($result, $expected);
  7248. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7249. $expected = array(
  7250. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7251. );
  7252. $this->assertTags($result, $expected);
  7253. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  7254. $expected = array(
  7255. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7256. );
  7257. $this->assertTags($result, $expected);
  7258. }
  7259. /**
  7260. *
  7261. * @expectedException CakeException
  7262. * @return void
  7263. */
  7264. public function testHtml5InputException() {
  7265. $this->Form->email();
  7266. }
  7267. /**
  7268. * Tests that a model can be loaded from the model names passed in the request object
  7269. *
  7270. * @return void
  7271. */
  7272. public function testIntrospectModelFromRequest() {
  7273. $this->loadFixtures('Post');
  7274. App::build(array(
  7275. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  7276. ));
  7277. CakePlugin::load('TestPlugin');
  7278. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  7279. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  7280. $this->Form->create('TestPluginPost');
  7281. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  7282. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  7283. CakePlugin::unload();
  7284. App::build();
  7285. }
  7286. /**
  7287. * Tests that it is possible to set the validation errors directly in the helper for a field
  7288. *
  7289. * @return void
  7290. */
  7291. public function testCustomValidationErrors() {
  7292. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7293. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7294. $this->assertEquals('Badness!', $result);
  7295. }
  7296. /**
  7297. * Tests that the 'on' key validates as expected on create
  7298. *
  7299. * @return void
  7300. */
  7301. public function testRequiredOnCreate() {
  7302. $this->Form->create('Contact');
  7303. $result = $this->Form->input('Contact.imrequiredonupdate');
  7304. $expected = array(
  7305. 'div' => array('class' => 'input text'),
  7306. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7307. 'Imrequiredonupdate',
  7308. '/label',
  7309. 'input' => array(
  7310. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7311. 'id' => 'ContactImrequiredonupdate'
  7312. ),
  7313. '/div'
  7314. );
  7315. $this->assertTags($result, $expected);
  7316. $result = $this->Form->input('Contact.imrequiredoncreate');
  7317. $expected = array(
  7318. 'div' => array('class' => 'input text required'),
  7319. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7320. 'Imrequiredoncreate',
  7321. '/label',
  7322. 'input' => array(
  7323. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7324. 'id' => 'ContactImrequiredoncreate'
  7325. ),
  7326. '/div'
  7327. );
  7328. $this->assertTags($result, $expected);
  7329. $result = $this->Form->input('Contact.imrequiredonboth');
  7330. $expected = array(
  7331. 'div' => array('class' => 'input text required'),
  7332. 'label' => array('for' => 'ContactImrequiredonboth'),
  7333. 'Imrequiredonboth',
  7334. '/label',
  7335. 'input' => array(
  7336. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7337. 'id' => 'ContactImrequiredonboth'
  7338. ),
  7339. '/div'
  7340. );
  7341. $this->assertTags($result, $expected);
  7342. $result = $this->Form->input('Contact.imrequired');
  7343. $expected = array(
  7344. 'div' => array('class' => 'input text required'),
  7345. 'label' => array('for' => 'ContactImrequired'),
  7346. 'Imrequired',
  7347. '/label',
  7348. 'input' => array(
  7349. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7350. 'id' => 'ContactImrequired'
  7351. ),
  7352. '/div'
  7353. );
  7354. $this->assertTags($result, $expected);
  7355. }
  7356. /**
  7357. * Tests that the 'on' key validates as expected on update
  7358. *
  7359. * @return void
  7360. */
  7361. public function testRequiredOnUpdate() {
  7362. $this->Form->request->data['Contact']['id'] = 1;
  7363. $this->Form->create('Contact');
  7364. $result = $this->Form->input('Contact.imrequiredonupdate');
  7365. $expected = array(
  7366. 'div' => array('class' => 'input text required'),
  7367. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7368. 'Imrequiredonupdate',
  7369. '/label',
  7370. 'input' => array(
  7371. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7372. 'id' => 'ContactImrequiredonupdate'
  7373. ),
  7374. '/div'
  7375. );
  7376. $this->assertTags($result, $expected);
  7377. $result = $this->Form->input('Contact.imrequiredoncreate');
  7378. $expected = array(
  7379. 'div' => array('class' => 'input text'),
  7380. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7381. 'Imrequiredoncreate',
  7382. '/label',
  7383. 'input' => array(
  7384. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7385. 'id' => 'ContactImrequiredoncreate'
  7386. ),
  7387. '/div'
  7388. );
  7389. $this->assertTags($result, $expected);
  7390. $result = $this->Form->input('Contact.imrequiredonboth');
  7391. $expected = array(
  7392. 'div' => array('class' => 'input text required'),
  7393. 'label' => array('for' => 'ContactImrequiredonboth'),
  7394. 'Imrequiredonboth',
  7395. '/label',
  7396. 'input' => array(
  7397. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7398. 'id' => 'ContactImrequiredonboth'
  7399. ),
  7400. '/div'
  7401. );
  7402. $this->assertTags($result, $expected);
  7403. $result = $this->Form->input('Contact.imrequired');
  7404. $expected = array(
  7405. 'div' => array('class' => 'input text required'),
  7406. 'label' => array('for' => 'ContactImrequired'),
  7407. 'Imrequired',
  7408. '/label',
  7409. 'input' => array(
  7410. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7411. 'id' => 'ContactImrequired'
  7412. ),
  7413. '/div'
  7414. );
  7415. $this->assertTags($result, $expected);
  7416. }
  7417. /**
  7418. * Test inputDefaults setter and getter
  7419. *
  7420. * @return void
  7421. */
  7422. public function testInputDefaults() {
  7423. $this->Form->create('Contact');
  7424. $this->Form->inputDefaults(array(
  7425. 'label' => false,
  7426. 'div' => array(
  7427. 'style' => 'color: #000;'
  7428. )
  7429. ));
  7430. $result = $this->Form->input('Contact.field1');
  7431. $expected = array(
  7432. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  7433. 'input' => array(
  7434. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7435. 'id' => 'ContactField1'
  7436. ),
  7437. '/div'
  7438. );
  7439. $this->assertTags($result, $expected);
  7440. $this->Form->inputDefaults(array(
  7441. 'div' => false,
  7442. 'label' => 'Label',
  7443. ));
  7444. $result = $this->Form->input('Contact.field1');
  7445. $expected = array(
  7446. 'label' => array('for' => 'ContactField1'),
  7447. 'Label',
  7448. '/label',
  7449. 'input' => array(
  7450. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7451. 'id' => 'ContactField1'
  7452. ),
  7453. );
  7454. $this->assertTags($result, $expected);
  7455. $this->Form->inputDefaults(array(
  7456. 'label' => false,
  7457. ), true);
  7458. $result = $this->Form->input('Contact.field1');
  7459. $expected = array(
  7460. 'input' => array(
  7461. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7462. 'id' => 'ContactField1'
  7463. ),
  7464. );
  7465. $this->assertTags($result, $expected);
  7466. $result = $this->Form->inputDefaults();
  7467. $expected = array(
  7468. 'div' => false,
  7469. 'label' => false,
  7470. );
  7471. $this->assertEqual($result, $expected);
  7472. }
  7473. }