FormHelperTest.php 256 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132
  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' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  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. 'type' => 'time',
  2014. 'interval' => 15,
  2015. 'selected' => '2012-10-23 15:57:00'
  2016. ));
  2017. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2018. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2019. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2020. $result = $this->Form->input('Model.start_time', array(
  2021. 'timeFormat' => 24,
  2022. 'type' => 'time',
  2023. 'interval' => 15,
  2024. 'selected' => '15:57'
  2025. ));
  2026. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2027. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2028. $result = $this->Form->input('Model.start_time', array(
  2029. 'timeFormat' => 24,
  2030. 'type' => 'time',
  2031. 'interval' => 15,
  2032. 'selected' => '23:57'
  2033. ));
  2034. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2035. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2036. $result = $this->Form->input('Model.created', array(
  2037. 'timeFormat' => 24,
  2038. 'type' => 'datetime',
  2039. 'interval' => 15,
  2040. 'selected' => '2012-09-30 23:56'
  2041. ));
  2042. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2043. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2044. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2045. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2046. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2047. }
  2048. /**
  2049. * test form->input() with datetime, date and time types
  2050. *
  2051. * @return void
  2052. */
  2053. public function testInputDatetime() {
  2054. extract($this->dateRegex);
  2055. $result = $this->Form->input('prueba', array(
  2056. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2057. 'maxYear' => date('Y') + 1, 'interval' => 15
  2058. ));
  2059. $result = explode(':', $result);
  2060. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2061. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2062. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2063. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2064. //related to ticket #5013
  2065. $result = $this->Form->input('Contact.date', array(
  2066. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2067. ));
  2068. $this->assertRegExp('/class="customClass"/', $result);
  2069. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2070. $result = $this->Form->input('Contact.date', array(
  2071. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2072. ));
  2073. $this->assertRegExp('/id="customIdDay"/', $result);
  2074. $this->assertRegExp('/id="customIdMonth"/', $result);
  2075. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2076. $result = $this->Form->input('Model.field', array(
  2077. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2078. ));
  2079. $this->assertRegExp('/id="customIDDay"/', $result);
  2080. $this->assertRegExp('/id="customIDHour"/', $result);
  2081. $result = explode('</select><select', $result);
  2082. $result = explode(':', $result[1]);
  2083. $this->assertRegExp('/option value="23"/', $result[0]);
  2084. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2085. $result = $this->Form->input('Model.field', array(
  2086. 'type' => 'datetime', 'timeFormat' => 12
  2087. ));
  2088. $result = explode('</select><select', $result);
  2089. $result = explode(':', $result[1]);
  2090. $this->assertRegExp('/option value="12"/', $result[0]);
  2091. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2092. $this->Form->request->data = array('Contact' => array('created' => null));
  2093. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2094. $expected = array(
  2095. 'div' => array('class' => 'input date'),
  2096. 'label' => array('for' => 'ContactCreatedMonth'),
  2097. 'Created',
  2098. '/label',
  2099. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2100. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2101. $monthsRegex,
  2102. '/select', '-',
  2103. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2104. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2105. $daysRegex,
  2106. '/select', '-',
  2107. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2108. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2109. $yearsRegex,
  2110. '/select',
  2111. '/div'
  2112. );
  2113. $this->assertTags($result, $expected);
  2114. $this->Form->request->data = array('Contact' => array('created' => null));
  2115. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2116. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2117. $this->Form->request->data = array('Contact' => array('created' => null));
  2118. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2119. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2120. $result = $this->Form->input('Contact.created', array(
  2121. 'type' => 'date',
  2122. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2123. 'timeFormat' => 'NONE'
  2124. ));
  2125. $this->assertRegExp('/for\="created-month"/', $result);
  2126. }
  2127. /**
  2128. * Test generating checkboxes in a loop.
  2129. *
  2130. * @return void
  2131. */
  2132. public function testInputCheckboxesInLoop() {
  2133. for ($i = 1; $i < 5; $i++) {
  2134. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2135. $expected = array(
  2136. 'div' => array('class' => 'input checkbox'),
  2137. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2138. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2139. 'label' => array('for' => "Contact{$i}Email"),
  2140. 'Email',
  2141. '/label',
  2142. '/div'
  2143. );
  2144. $this->assertTags($result, $expected);
  2145. }
  2146. }
  2147. /**
  2148. * test input name with leading integer, ensure attributes are generated correctly.
  2149. *
  2150. * @return void
  2151. */
  2152. public function testInputWithLeadingInteger() {
  2153. $result = $this->Form->text('0.Node.title');
  2154. $expected = array(
  2155. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2156. );
  2157. $this->assertTags($result, $expected);
  2158. }
  2159. /**
  2160. * test form->input() with select type inputs.
  2161. *
  2162. * @return void
  2163. */
  2164. public function testInputSelectType() {
  2165. $result = $this->Form->input('email', array(
  2166. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2167. );
  2168. $expected = array(
  2169. 'div' => array('class' => 'input select'),
  2170. 'label' => array('for' => 'email'),
  2171. 'Email',
  2172. '/label',
  2173. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2174. array('option' => array('value' => '')),
  2175. '/option',
  2176. array('option' => array('value' => 'è')),
  2177. 'Firést',
  2178. '/option',
  2179. array('option' => array('value' => 'é')),
  2180. 'Secoènd',
  2181. '/option',
  2182. '/select',
  2183. '/div'
  2184. );
  2185. $this->assertTags($result, $expected);
  2186. $result = $this->Form->input('email', array(
  2187. 'options' => array('First', 'Second'), 'empty' => true)
  2188. );
  2189. $expected = array(
  2190. 'div' => array('class' => 'input select'),
  2191. 'label' => array('for' => 'email'),
  2192. 'Email',
  2193. '/label',
  2194. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2195. array('option' => array('value' => '')),
  2196. '/option',
  2197. array('option' => array('value' => '0')),
  2198. 'First',
  2199. '/option',
  2200. array('option' => array('value' => '1')),
  2201. 'Second',
  2202. '/option',
  2203. '/select',
  2204. '/div'
  2205. );
  2206. $this->assertTags($result, $expected);
  2207. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2208. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2209. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2210. $expected = array(
  2211. 'div' => array('class' => 'input select'),
  2212. 'label' => array('for' => 'ModelUserId'),
  2213. 'User',
  2214. '/label',
  2215. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2216. array('option' => array('value' => '')),
  2217. '/option',
  2218. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2219. 'good',
  2220. '/option',
  2221. array('option' => array('value' => 'other')),
  2222. 'bad',
  2223. '/option',
  2224. '/select',
  2225. '/div'
  2226. );
  2227. $this->assertTags($result, $expected);
  2228. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2229. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2230. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2231. $expected = array(
  2232. 'div' => array('class' => 'input select'),
  2233. 'label' => array('for' => 'ThingUserId'),
  2234. 'User',
  2235. '/label',
  2236. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2237. array('option' => array('value' => '')),
  2238. 'Some Empty',
  2239. '/option',
  2240. array('option' => array('value' => 'value')),
  2241. 'good',
  2242. '/option',
  2243. array('option' => array('value' => 'other')),
  2244. 'bad',
  2245. '/option',
  2246. '/select',
  2247. '/div'
  2248. );
  2249. $this->assertTags($result, $expected);
  2250. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2251. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2252. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2253. $expected = array(
  2254. 'div' => array('class' => 'input select'),
  2255. 'label' => array('for' => 'ThingUserId'),
  2256. 'User',
  2257. '/label',
  2258. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2259. array('option' => array('value' => '')),
  2260. 'Some Empty',
  2261. '/option',
  2262. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2263. 'good',
  2264. '/option',
  2265. array('option' => array('value' => 'other')),
  2266. 'bad',
  2267. '/option',
  2268. '/select',
  2269. '/div'
  2270. );
  2271. $this->assertTags($result, $expected);
  2272. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2273. $this->Form->request->data = array('User' => array('User' => array('value')));
  2274. $result = $this->Form->input('User.User', array('empty' => true));
  2275. $expected = array(
  2276. 'div' => array('class' => 'input select'),
  2277. 'label' => array('for' => 'UserUser'),
  2278. 'User',
  2279. '/label',
  2280. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2281. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2282. array('option' => array('value' => '')),
  2283. '/option',
  2284. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2285. 'good',
  2286. '/option',
  2287. array('option' => array('value' => 'other')),
  2288. 'bad',
  2289. '/option',
  2290. '/select',
  2291. '/div'
  2292. );
  2293. $this->assertTags($result, $expected);
  2294. $this->Form->data = array();
  2295. $result = $this->Form->input('Publisher.id', array(
  2296. 'label' => 'Publisher',
  2297. 'type' => 'select',
  2298. 'multiple' => 'checkbox',
  2299. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2300. ));
  2301. $expected = array(
  2302. array('div' => array('class' => 'input select')),
  2303. array('label' => array('for' => 'PublisherId')),
  2304. 'Publisher',
  2305. '/label',
  2306. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2307. array('div' => array('class' => 'checkbox')),
  2308. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2309. array('label' => array('for' => 'PublisherIdValue1')),
  2310. 'Label 1',
  2311. '/label',
  2312. '/div',
  2313. array('div' => array('class' => 'checkbox')),
  2314. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2315. array('label' => array('for' => 'PublisherIdValue2')),
  2316. 'Label 2',
  2317. '/label',
  2318. '/div',
  2319. '/div'
  2320. );
  2321. $this->assertTags($result, $expected);
  2322. }
  2323. /**
  2324. * test that input() and a non standard primary key makes a hidden input by default.
  2325. *
  2326. * @return void
  2327. */
  2328. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2329. $this->Form->create('User');
  2330. $this->Form->fieldset = array(
  2331. 'User' => array(
  2332. 'fields' => array(
  2333. 'model_id' => array('type' => 'integer')
  2334. ),
  2335. 'validates' => array(),
  2336. 'key' => 'model_id'
  2337. )
  2338. );
  2339. $result = $this->Form->input('model_id');
  2340. $expected = array(
  2341. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2342. );
  2343. $this->assertTags($result, $expected);
  2344. }
  2345. /**
  2346. * test that overriding the magic select type widget is possible
  2347. *
  2348. * @return void
  2349. */
  2350. public function testInputOverridingMagicSelectType() {
  2351. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2352. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2353. $expected = array(
  2354. 'div' => array('class' => 'input text'),
  2355. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2356. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2357. '/div'
  2358. );
  2359. $this->assertTags($result, $expected);
  2360. //Check that magic types still work for plural/singular vars
  2361. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2362. $result = $this->Form->input('Model.type');
  2363. $expected = array(
  2364. 'div' => array('class' => 'input select'),
  2365. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2366. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2367. array('option' => array('value' => 'value')), 'good', '/option',
  2368. array('option' => array('value' => 'other')), 'bad', '/option',
  2369. '/select',
  2370. '/div'
  2371. );
  2372. $this->assertTags($result, $expected);
  2373. }
  2374. /**
  2375. * Test that magic input() selects are created for type=number
  2376. *
  2377. * @return void
  2378. */
  2379. public function testInputMagicSelectForTypeNumber() {
  2380. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2381. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2382. $result = $this->Form->input('ValidateUser.balance');
  2383. $expected = array(
  2384. 'div' => array('class' => 'input select'),
  2385. 'label' => array('for' => 'ValidateUserBalance'),
  2386. 'Balance',
  2387. '/label',
  2388. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2389. array('option' => array('value' => '0')),
  2390. 'nothing',
  2391. '/option',
  2392. array('option' => array('value' => '1', 'selected' => 'selected')),
  2393. 'some',
  2394. '/option',
  2395. array('option' => array('value' => '100')),
  2396. 'a lot',
  2397. '/option',
  2398. '/select',
  2399. '/div'
  2400. );
  2401. $this->assertTags($result, $expected);
  2402. }
  2403. /**
  2404. * Test that magic input() selects can easily be converted into radio types without error.
  2405. *
  2406. * @return void
  2407. */
  2408. public function testInputMagicSelectChangeToRadio() {
  2409. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2410. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2411. $this->assertRegExp('/input type="radio"/', $result);
  2412. }
  2413. /**
  2414. * fields with the same name as the model should work.
  2415. *
  2416. * @return void
  2417. */
  2418. public function testInputWithMatchingFieldAndModelName() {
  2419. $this->Form->create('User');
  2420. $this->Form->fieldset = array(
  2421. 'User' => array(
  2422. 'fields' => array(
  2423. 'User' => array('type' => 'text')
  2424. ),
  2425. 'validates' => array(),
  2426. 'key' => 'id'
  2427. )
  2428. );
  2429. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2430. $result = $this->Form->input('User', array('type' => 'text'));
  2431. $expected = array(
  2432. 'div' => array('class' => 'input text'),
  2433. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2434. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2435. '/div'
  2436. );
  2437. $this->assertTags($result, $expected);
  2438. }
  2439. /**
  2440. * testFormInputs method
  2441. *
  2442. * test correct results from form::inputs().
  2443. *
  2444. * @return void
  2445. */
  2446. public function testFormInputs() {
  2447. $this->Form->create('Contact');
  2448. $result = $this->Form->inputs('The Legend');
  2449. $expected = array(
  2450. '<fieldset',
  2451. '<legend',
  2452. 'The Legend',
  2453. '/legend',
  2454. '*/fieldset',
  2455. );
  2456. $this->assertTags($result, $expected);
  2457. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2458. $expected = array(
  2459. 'fieldset' => array('class' => 'classy-stuff'),
  2460. '<legend',
  2461. 'Field of Dreams',
  2462. '/legend',
  2463. '*/fieldset'
  2464. );
  2465. $this->assertTags($result, $expected);
  2466. $this->Form->create('Contact');
  2467. $this->Form->request['prefix'] = 'admin';
  2468. $this->Form->request['action'] = 'admin_edit';
  2469. $result = $this->Form->inputs();
  2470. $expected = array(
  2471. '<fieldset',
  2472. '<legend',
  2473. 'Edit Contact',
  2474. '/legend',
  2475. '*/fieldset',
  2476. );
  2477. $this->assertTags($result, $expected);
  2478. $this->Form->create('Contact');
  2479. $result = $this->Form->inputs(false);
  2480. $expected = array(
  2481. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2482. array('div' => array('class' => 'input text')),
  2483. '*/div',
  2484. array('div' => array('class' => 'input text')),
  2485. '*/div',
  2486. array('div' => array('class' => 'input text')),
  2487. '*/div',
  2488. array('div' => array('class' => 'input password')),
  2489. '*/div',
  2490. array('div' => array('class' => 'input date')),
  2491. '*/div',
  2492. array('div' => array('class' => 'input date')),
  2493. '*/div',
  2494. array('div' => array('class' => 'input datetime')),
  2495. '*/div',
  2496. array('div' => array('class' => 'input number')),
  2497. '*/div',
  2498. array('div' => array('class' => 'input select')),
  2499. '*/div',
  2500. );
  2501. $this->assertTags($result, $expected);
  2502. $this->Form->create('Contact');
  2503. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2504. $expected = array(
  2505. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2506. array('div' => array('class' => 'input text')),
  2507. '*/div',
  2508. array('div' => array('class' => 'input text')),
  2509. '*/div',
  2510. array('div' => array('class' => 'input text')),
  2511. '*/div',
  2512. array('div' => array('class' => 'input password')),
  2513. '*/div',
  2514. array('div' => array('class' => 'input date')),
  2515. '*/div',
  2516. array('div' => array('class' => 'input date')),
  2517. '*/div',
  2518. array('div' => array('class' => 'input datetime')),
  2519. '*/div',
  2520. array('div' => array('class' => 'input number')),
  2521. '*/div',
  2522. array('div' => array('class' => 'input select')),
  2523. '*/div',
  2524. );
  2525. $this->assertTags($result, $expected);
  2526. $this->Form->create('Contact');
  2527. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2528. $expected = array(
  2529. 'fieldset' => array(),
  2530. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2531. array('div' => array('class' => 'input text')),
  2532. '*/div',
  2533. array('div' => array('class' => 'input text')),
  2534. '*/div',
  2535. array('div' => array('class' => 'input text')),
  2536. '*/div',
  2537. array('div' => array('class' => 'input password')),
  2538. '*/div',
  2539. array('div' => array('class' => 'input date')),
  2540. '*/div',
  2541. array('div' => array('class' => 'input date')),
  2542. '*/div',
  2543. array('div' => array('class' => 'input datetime')),
  2544. '*/div',
  2545. array('div' => array('class' => 'input number')),
  2546. '*/div',
  2547. array('div' => array('class' => 'input select')),
  2548. '*/div',
  2549. '/fieldset'
  2550. );
  2551. $this->assertTags($result, $expected);
  2552. $this->Form->create('Contact');
  2553. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2554. $expected = array(
  2555. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2556. array('div' => array('class' => 'input text')),
  2557. '*/div',
  2558. array('div' => array('class' => 'input text')),
  2559. '*/div',
  2560. array('div' => array('class' => 'input text')),
  2561. '*/div',
  2562. array('div' => array('class' => 'input password')),
  2563. '*/div',
  2564. array('div' => array('class' => 'input date')),
  2565. '*/div',
  2566. array('div' => array('class' => 'input date')),
  2567. '*/div',
  2568. array('div' => array('class' => 'input datetime')),
  2569. '*/div',
  2570. array('div' => array('class' => 'input number')),
  2571. '*/div',
  2572. array('div' => array('class' => 'input select')),
  2573. '*/div',
  2574. );
  2575. $this->assertTags($result, $expected);
  2576. $this->Form->create('Contact');
  2577. $result = $this->Form->inputs('Hello');
  2578. $expected = array(
  2579. 'fieldset' => array(),
  2580. 'legend' => array(),
  2581. 'Hello',
  2582. '/legend',
  2583. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2584. array('div' => array('class' => 'input text')),
  2585. '*/div',
  2586. array('div' => array('class' => 'input text')),
  2587. '*/div',
  2588. array('div' => array('class' => 'input text')),
  2589. '*/div',
  2590. array('div' => array('class' => 'input password')),
  2591. '*/div',
  2592. array('div' => array('class' => 'input date')),
  2593. '*/div',
  2594. array('div' => array('class' => 'input date')),
  2595. '*/div',
  2596. array('div' => array('class' => 'input datetime')),
  2597. '*/div',
  2598. array('div' => array('class' => 'input number')),
  2599. '*/div',
  2600. array('div' => array('class' => 'input select')),
  2601. '*/div',
  2602. '/fieldset'
  2603. );
  2604. $this->assertTags($result, $expected);
  2605. $this->Form->create('Contact');
  2606. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2607. $expected = array(
  2608. 'fieldset' => array(),
  2609. 'legend' => array(),
  2610. 'Hello',
  2611. '/legend',
  2612. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2613. array('div' => array('class' => 'input text')),
  2614. '*/div',
  2615. array('div' => array('class' => 'input text')),
  2616. '*/div',
  2617. array('div' => array('class' => 'input text')),
  2618. '*/div',
  2619. array('div' => array('class' => 'input password')),
  2620. '*/div',
  2621. array('div' => array('class' => 'input date')),
  2622. '*/div',
  2623. array('div' => array('class' => 'input date')),
  2624. '*/div',
  2625. array('div' => array('class' => 'input datetime')),
  2626. '*/div',
  2627. array('div' => array('class' => 'input number')),
  2628. '*/div',
  2629. array('div' => array('class' => 'input select')),
  2630. '*/div',
  2631. '/fieldset'
  2632. );
  2633. $this->assertTags($result, $expected);
  2634. }
  2635. /**
  2636. * testSelectAsCheckbox method
  2637. *
  2638. * test multi-select widget with checkbox formatting.
  2639. *
  2640. * @return void
  2641. */
  2642. public function testSelectAsCheckbox() {
  2643. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2644. $expected = array(
  2645. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2646. array('div' => array('class' => 'checkbox')),
  2647. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2648. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2649. 'first',
  2650. '/label',
  2651. '/div',
  2652. array('div' => array('class' => 'checkbox')),
  2653. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2654. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2655. 'second',
  2656. '/label',
  2657. '/div',
  2658. array('div' => array('class' => 'checkbox')),
  2659. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2660. array('label' => array('for' => 'ModelMultiField2')),
  2661. 'third',
  2662. '/label',
  2663. '/div',
  2664. );
  2665. $this->assertTags($result, $expected);
  2666. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2667. $expected = array(
  2668. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2669. array('div' => array('class' => 'checkbox')),
  2670. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2671. array('label' => array('for' => 'ModelMultiField12')),
  2672. 'half',
  2673. '/label',
  2674. '/div',
  2675. );
  2676. $this->assertTags($result, $expected);
  2677. }
  2678. /**
  2679. * testLabel method
  2680. *
  2681. * test label generation.
  2682. *
  2683. * @return void
  2684. */
  2685. public function testLabel() {
  2686. $this->Form->text('Person.name');
  2687. $result = $this->Form->label();
  2688. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2689. $this->Form->text('Person.name');
  2690. $result = $this->Form->label();
  2691. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2692. $result = $this->Form->label('Person.first_name');
  2693. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2694. $result = $this->Form->label('Person.first_name', 'Your first name');
  2695. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2696. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2697. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2698. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2699. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2700. $result = $this->Form->label('Person.first_name', '');
  2701. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2702. $result = $this->Form->label('Person.2.name', '');
  2703. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2704. }
  2705. /**
  2706. * testTextbox method
  2707. *
  2708. * test textbox element generation
  2709. *
  2710. * @return void
  2711. */
  2712. public function testTextbox() {
  2713. $result = $this->Form->text('Model.field');
  2714. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2715. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2716. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2717. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2718. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2719. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2720. $result = $this->Form->text('Model.text');
  2721. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2722. $Contact = ClassRegistry::getObject('Contact');
  2723. $Contact->validationErrors['text'] = array(true);
  2724. $this->Form->request->data['Contact']['text'] = 'test';
  2725. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2726. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2727. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2728. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2729. $expected = array(
  2730. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2731. );
  2732. $this->assertTags($result, $expected);
  2733. }
  2734. /**
  2735. * testDefaultValue method
  2736. *
  2737. * Test default value setting
  2738. *
  2739. * @return void
  2740. */
  2741. public function testDefaultValue() {
  2742. $this->Form->request->data['Model']['field'] = 'test';
  2743. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2744. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  2745. unset($this->Form->request->data['Model']['field']);
  2746. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2747. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  2748. }
  2749. /**
  2750. * testCheckboxDefaultValue method
  2751. *
  2752. * Test default value setting on checkbox() method
  2753. *
  2754. * @return void
  2755. */
  2756. public function testCheckboxDefaultValue() {
  2757. $this->Form->request->data['Model']['field'] = false;
  2758. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2759. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2760. unset($this->Form->request->data['Model']['field']);
  2761. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2762. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2763. $this->Form->request->data['Model']['field'] = true;
  2764. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2765. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2766. unset($this->Form->request->data['Model']['field']);
  2767. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2768. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2769. }
  2770. /**
  2771. * testError method
  2772. *
  2773. * Test field error generation
  2774. *
  2775. * @return void
  2776. */
  2777. public function testError() {
  2778. $Contact = ClassRegistry::getObject('Contact');
  2779. $Contact->validationErrors['field'] = array(1);
  2780. $result = $this->Form->error('Contact.field');
  2781. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  2782. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2783. $this->assertEquals('Error in field Field', $result);
  2784. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2785. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2786. $this->assertEquals('This field contains invalid input', $result);
  2787. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2788. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  2789. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  2790. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  2791. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  2792. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  2793. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2794. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  2795. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2796. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  2797. $this->assertEquals('<strong>Badness!</strong>', $result);
  2798. $Contact->validationErrors['field'] = array("email");
  2799. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  2800. $expected = array(
  2801. 'div' => array('class' => 'field-error'),
  2802. 'No good!',
  2803. '/div'
  2804. );
  2805. $this->assertTags($result, $expected);
  2806. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  2807. $result = $this->Form->error('Contact.field', array(
  2808. 'notEmpty' => 'Cannot be empty',
  2809. 'email' => 'No good!'
  2810. ));
  2811. $expected = array(
  2812. 'div' => array('class' => 'error-message'),
  2813. 'ul' => array(),
  2814. '<li', 'Cannot be empty', '/li',
  2815. '<li', 'No good!', '/li',
  2816. '<li', 'Something else', '/li',
  2817. '/ul',
  2818. '/div'
  2819. );
  2820. $this->assertTags($result, $expected);
  2821. /** Testing error messages list options **/
  2822. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  2823. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  2824. $expected = array(
  2825. 'div' => array('class' => 'error-message'),
  2826. 'ol' => array(),
  2827. '<li', 'notEmpty', '/li',
  2828. '<li', 'email', '/li',
  2829. '/ol',
  2830. '/div'
  2831. );
  2832. $this->assertTags($result, $expected);
  2833. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  2834. $expected = array(
  2835. 'div' => array('class' => 'error-message'),
  2836. 'ol' => array(),
  2837. '<li', 'notEmpty', '/li',
  2838. '<li', 'email', '/li',
  2839. '/ol',
  2840. '/div'
  2841. );
  2842. $this->assertTags($result, $expected);
  2843. $result = $this->Form->error('Contact.field', null, array(
  2844. 'listOptions' => array(
  2845. 'class' => 'ul-class',
  2846. 'itemOptions' => array(
  2847. 'class' => 'li-class'
  2848. )
  2849. )
  2850. ));
  2851. $expected = array(
  2852. 'div' => array('class' => 'error-message'),
  2853. 'ul' => array('class' => 'ul-class'),
  2854. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  2855. array('li' => array('class' => 'li-class')), 'email', '/li',
  2856. '/ul',
  2857. '/div'
  2858. );
  2859. $this->assertTags($result, $expected);
  2860. }
  2861. /**
  2862. * test error options when using form->input();
  2863. *
  2864. * @return void
  2865. */
  2866. public function testInputErrorEscape() {
  2867. $this->Form->create('ValidateProfile');
  2868. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  2869. $ValidateProfile->validationErrors['city'] = array('required<br>');
  2870. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
  2871. $this->assertRegExp('/required&lt;br&gt;/', $result);
  2872. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => false))));
  2873. $this->assertRegExp('/required<br>/', $result);
  2874. }
  2875. /**
  2876. * testPassword method
  2877. *
  2878. * Test password element generation
  2879. *
  2880. * @return void
  2881. */
  2882. public function testPassword() {
  2883. $Contact = ClassRegistry::getObject('Contact');
  2884. $result = $this->Form->password('Contact.field');
  2885. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  2886. $Contact->validationErrors['passwd'] = 1;
  2887. $this->Form->request->data['Contact']['passwd'] = 'test';
  2888. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  2889. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2890. }
  2891. /**
  2892. * testRadio method
  2893. *
  2894. * Test radio element set generation
  2895. *
  2896. * @return void
  2897. */
  2898. public function testRadio() {
  2899. $result = $this->Form->radio('Model.field', array('option A'));
  2900. $expected = array(
  2901. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2902. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2903. 'label' => array('for' => 'ModelField0'),
  2904. 'option A',
  2905. '/label'
  2906. );
  2907. $this->assertTags($result, $expected);
  2908. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  2909. $expected = array(
  2910. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2911. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  2912. 'label' => array('for' => 'ModelField12'),
  2913. 'half',
  2914. '/label'
  2915. );
  2916. $this->assertTags($result, $expected);
  2917. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  2918. $expected = array(
  2919. 'fieldset' => array(),
  2920. 'legend' => array(),
  2921. 'Field',
  2922. '/legend',
  2923. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2924. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2925. array('label' => array('for' => 'ModelField0')),
  2926. 'option A',
  2927. '/label',
  2928. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2929. array('label' => array('for' => 'ModelField1')),
  2930. 'option B',
  2931. '/label',
  2932. '/fieldset'
  2933. );
  2934. $this->assertTags($result, $expected);
  2935. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  2936. $expected = array(
  2937. 'fieldset' => array(),
  2938. 'legend' => array(),
  2939. 'Field',
  2940. '/legend',
  2941. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2942. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2943. array('label' => array('for' => 'ModelField0')),
  2944. 'option A',
  2945. '/label',
  2946. 'br' => array(),
  2947. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2948. array('label' => array('for' => 'ModelField1')),
  2949. 'option B',
  2950. '/label',
  2951. '/fieldset'
  2952. );
  2953. $this->assertTags($result, $expected);
  2954. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2955. $expected = array(
  2956. 'div' => array('class' => 'input radio'),
  2957. 'fieldset' => array(),
  2958. 'legend' => array(),
  2959. 'Legend title',
  2960. '/legend',
  2961. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2962. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2963. array('label' => array('for' => 'NewsletterSubscribe0')),
  2964. 'Unsubscribe',
  2965. '/label',
  2966. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2967. array('label' => array('for' => 'NewsletterSubscribe1')),
  2968. 'Subscribe',
  2969. '/label',
  2970. '/fieldset',
  2971. '/div'
  2972. );
  2973. $this->assertTags($result, $expected);
  2974. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2975. $expected = array(
  2976. 'div' => array('class' => 'input radio'),
  2977. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2978. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2979. array('label' => array('for' => 'NewsletterSubscribe0')),
  2980. 'Unsubscribe',
  2981. '/label',
  2982. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2983. array('label' => array('for' => 'NewsletterSubscribe1')),
  2984. 'Subscribe',
  2985. '/label',
  2986. '/div'
  2987. );
  2988. $this->assertTags($result, $expected);
  2989. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2990. $expected = array(
  2991. 'div' => array('class' => 'input radio'),
  2992. 'fieldset' => array(),
  2993. 'legend' => array(),
  2994. 'Legend title',
  2995. '/legend',
  2996. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2997. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2998. 'Unsubscribe',
  2999. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3000. 'Subscribe',
  3001. '/fieldset',
  3002. '/div'
  3003. );
  3004. $this->assertTags($result, $expected);
  3005. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3006. $expected = array(
  3007. 'div' => array('class' => 'input radio'),
  3008. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3009. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3010. 'Unsubscribe',
  3011. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3012. 'Subscribe',
  3013. '/div'
  3014. );
  3015. $this->assertTags($result, $expected);
  3016. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'value' => '1', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3017. $expected = array(
  3018. 'div' => array('class' => 'input radio'),
  3019. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3020. 'Unsubscribe',
  3021. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1', 'checked' => 'checked')),
  3022. 'Subscribe',
  3023. '/div'
  3024. );
  3025. $this->assertTags($result, $expected);
  3026. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3027. $expected = array(
  3028. 'fieldset' => array(),
  3029. 'legend' => array(),
  3030. 'Gender',
  3031. '/legend',
  3032. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3033. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3034. array('label' => array('for' => 'EmployeeGenderMale')),
  3035. 'Male',
  3036. '/label',
  3037. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3038. array('label' => array('for' => 'EmployeeGenderFemale')),
  3039. 'Female',
  3040. '/label',
  3041. '/fieldset',
  3042. );
  3043. $this->assertTags($result, $expected);
  3044. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3045. $expected = array(
  3046. 'fieldset' => array(),
  3047. 'legend' => array(),
  3048. 'Gender',
  3049. '/legend',
  3050. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3051. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3052. array('label' => array('for' => 'OfficerGenderMale')),
  3053. 'Male',
  3054. '/label',
  3055. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3056. array('label' => array('for' => 'OfficerGenderFemale')),
  3057. 'Female',
  3058. '/label',
  3059. '/fieldset',
  3060. );
  3061. $this->assertTags($result, $expected);
  3062. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3063. $expected = array(
  3064. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3065. array('input' => array('type' => 'radio', 'name' => 'data[Contact][1][imrequired]', 'value' => '0', 'id' => 'Contact1Imrequired0')),
  3066. 'label' => array('for' => 'Contact1Imrequired0'),
  3067. 'option A',
  3068. '/label'
  3069. );
  3070. $this->assertTags($result, $expected);
  3071. $result = $this->Form->radio('Model.1.field', array('option A'));
  3072. $expected = array(
  3073. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3074. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3075. 'label' => array('for' => 'Model1Field0'),
  3076. 'option A',
  3077. '/label'
  3078. );
  3079. $this->assertTags($result, $expected);
  3080. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3081. $expected = array(
  3082. 'fieldset' => array(),
  3083. 'legend' => array(),
  3084. 'Field',
  3085. '/legend',
  3086. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3087. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3088. array('label' => array('for' => 'ModelField0')),
  3089. 'option A',
  3090. '/label',
  3091. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3092. array('label' => array('for' => 'ModelField1')),
  3093. 'option B',
  3094. '/label',
  3095. '/fieldset'
  3096. );
  3097. $this->assertTags($result, $expected);
  3098. $result = $this->Form->radio(
  3099. 'Model.field',
  3100. array('option A', 'option B'),
  3101. array('between' => 'I am between')
  3102. );
  3103. $expected = array(
  3104. 'fieldset' => array(),
  3105. 'legend' => array(),
  3106. 'Field',
  3107. '/legend',
  3108. 'I am between',
  3109. 'input' => array(
  3110. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3111. 'value' => '', 'id' => 'ModelField_'
  3112. ),
  3113. array('input' => array(
  3114. 'type' => 'radio', 'name' => 'data[Model][field]',
  3115. 'value' => '0', 'id' => 'ModelField0'
  3116. )),
  3117. array('label' => array('for' => 'ModelField0')),
  3118. 'option A',
  3119. '/label',
  3120. array('input' => array(
  3121. 'type' => 'radio', 'name' => 'data[Model][field]',
  3122. 'value' => '1', 'id' => 'ModelField1'
  3123. )),
  3124. array('label' => array('for' => 'ModelField1')),
  3125. 'option B',
  3126. '/label',
  3127. '/fieldset'
  3128. );
  3129. $this->assertTags($result, $expected);
  3130. }
  3131. /**
  3132. * Test that radios with a 0 value are selected under the correct conditions.
  3133. *
  3134. * @return void
  3135. */
  3136. public function testRadioOptionWithZeroValue() {
  3137. $expected = array(
  3138. 'fieldset' => array(),
  3139. 'legend' => array(),
  3140. 'Field',
  3141. '/legend',
  3142. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3143. array('label' => array('for' => 'ModelField1')),
  3144. 'Yes',
  3145. '/label',
  3146. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3147. array('label' => array('for' => 'ModelField0')),
  3148. 'No',
  3149. '/label',
  3150. '/fieldset'
  3151. );
  3152. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3153. $this->assertTags($result, $expected);
  3154. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3155. $this->assertTags($result, $expected);
  3156. $expected = array(
  3157. 'fieldset' => array(),
  3158. 'legend' => array(),
  3159. 'Field',
  3160. '/legend',
  3161. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3162. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3163. array('label' => array('for' => 'ModelField1')),
  3164. 'Yes',
  3165. '/label',
  3166. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3167. array('label' => array('for' => 'ModelField0')),
  3168. 'No',
  3169. '/label',
  3170. '/fieldset'
  3171. );
  3172. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3173. $this->assertTags($result, $expected);
  3174. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3175. $this->assertTags($result, $expected);
  3176. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3177. $this->assertTags($result, $expected);
  3178. }
  3179. /**
  3180. * test disabled radio options
  3181. *
  3182. * @return void
  3183. */
  3184. public function testRadioDisabled() {
  3185. $result = $this->Form->radio(
  3186. 'Model.field',
  3187. array('option A', 'option B'),
  3188. array('disabled' => array('option A'), 'value' => '0')
  3189. );
  3190. $expected = array(
  3191. 'fieldset' => array(),
  3192. 'legend' => array(),
  3193. 'Field',
  3194. '/legend',
  3195. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3196. array('label' => array('for' => 'ModelField0')),
  3197. 'option A',
  3198. '/label',
  3199. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3200. array('label' => array('for' => 'ModelField1')),
  3201. 'option B',
  3202. '/label',
  3203. '/fieldset'
  3204. );
  3205. $this->assertTags($result, $expected);
  3206. $result = $this->Form->radio(
  3207. 'Model.field',
  3208. array('option A', 'option B'),
  3209. array('disabled' => true, 'value' => '0')
  3210. );
  3211. $expected = array(
  3212. 'fieldset' => array(),
  3213. 'legend' => array(),
  3214. 'Field',
  3215. '/legend',
  3216. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3217. array('label' => array('for' => 'ModelField0')),
  3218. 'option A',
  3219. '/label',
  3220. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3221. array('label' => array('for' => 'ModelField1')),
  3222. 'option B',
  3223. '/label',
  3224. '/fieldset'
  3225. );
  3226. $this->assertTags($result, $expected);
  3227. $result = $this->Form->radio(
  3228. 'Model.field',
  3229. array('option A', 'option B'),
  3230. array('disabled' => 'disabled', 'value' => '0')
  3231. );
  3232. $expected = array(
  3233. 'fieldset' => array(),
  3234. 'legend' => array(),
  3235. 'Field',
  3236. '/legend',
  3237. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3238. array('label' => array('for' => 'ModelField0')),
  3239. 'option A',
  3240. '/label',
  3241. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3242. array('label' => array('for' => 'ModelField1')),
  3243. 'option B',
  3244. '/label',
  3245. '/fieldset'
  3246. );
  3247. $this->assertTags($result, $expected);
  3248. }
  3249. /**
  3250. * test disabling the hidden input for radio buttons
  3251. *
  3252. * @return void
  3253. */
  3254. public function testRadioHiddenInputDisabling() {
  3255. $result = $this->Form->input('Model.1.field', array(
  3256. 'type' => 'radio',
  3257. 'options' => array('option A'),
  3258. 'hiddenField' => false
  3259. )
  3260. );
  3261. $expected = array(
  3262. 'div' => array('class' => 'input radio'),
  3263. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3264. 'label' => array('for' => 'Model1Field0'),
  3265. 'option A',
  3266. '/label',
  3267. '/div'
  3268. );
  3269. $this->assertTags($result, $expected);
  3270. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3271. $expected = array(
  3272. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3273. 'label' => array('for' => 'Model1Field0'),
  3274. 'option A',
  3275. '/label'
  3276. );
  3277. $this->assertTags($result, $expected);
  3278. }
  3279. /**
  3280. * test adding an empty option for radio buttons
  3281. *
  3282. * @return void
  3283. */
  3284. public function testRadioAddEmptyOption() {
  3285. $result = $this->Form->input('Model.1.field', array(
  3286. 'type' => 'radio',
  3287. 'options' => array('option A'),
  3288. 'empty' => true,
  3289. 'hiddenField' => false
  3290. ));
  3291. $expected = array(
  3292. 'div' => array('class' => 'input radio'),
  3293. 'fieldset' => array(),
  3294. 'legend' => array(),
  3295. 'Field',
  3296. '/legend',
  3297. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3298. array('label' => array('for' => 'Model1Field')),
  3299. __('empty'),
  3300. '/label',
  3301. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3302. array('label' => array('for' => 'Model1Field0')),
  3303. 'option A',
  3304. '/label',
  3305. '/fieldset',
  3306. '/div'
  3307. );
  3308. $this->assertTags($result, $expected);
  3309. $result = $this->Form->input('Model.1.field', array(
  3310. 'type' => 'radio',
  3311. 'options' => array('option A'),
  3312. 'empty' => 'CustomEmptyLabel',
  3313. 'hiddenField' => false
  3314. ));
  3315. $expected = array(
  3316. 'div' => array('class' => 'input radio'),
  3317. 'fieldset' => array(),
  3318. 'legend' => array(),
  3319. 'Field',
  3320. '/legend',
  3321. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3322. array('label' => array('for' => 'Model1Field')),
  3323. 'CustomEmptyLabel',
  3324. '/label',
  3325. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3326. array('label' => array('for' => 'Model1Field0')),
  3327. 'option A',
  3328. '/label',
  3329. '/fieldset',
  3330. '/div'
  3331. );
  3332. $this->assertTags($result, $expected);
  3333. $result = $this->Form->input('Model.1.field', array(
  3334. 'type' => 'radio',
  3335. 'options' => array('option A'),
  3336. 'empty' => false,
  3337. 'hiddenField' => false
  3338. ));
  3339. $this->assertTextNotContains('"Model1Field"', $result);
  3340. }
  3341. /**
  3342. * testSelect method
  3343. *
  3344. * Test select element generation.
  3345. *
  3346. * @return void
  3347. */
  3348. public function testSelect() {
  3349. $result = $this->Form->select('Model.field', array());
  3350. $expected = array(
  3351. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3352. array('option' => array('value' => '')),
  3353. '/option',
  3354. '/select'
  3355. );
  3356. $this->assertTags($result, $expected);
  3357. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3358. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3359. $expected = array(
  3360. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3361. array('option' => array('value' => '')),
  3362. '/option',
  3363. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3364. 'good',
  3365. '/option',
  3366. array('option' => array('value' => 'other')),
  3367. 'bad',
  3368. '/option',
  3369. '/select'
  3370. );
  3371. $this->assertTags($result, $expected);
  3372. $this->Form->request->data = array();
  3373. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3374. $expected = array(
  3375. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3376. array('option' => array('value' => '')),
  3377. '/option',
  3378. array('option' => array('value' => 'value')),
  3379. 'good',
  3380. '/option',
  3381. array('option' => array('value' => 'other')),
  3382. 'bad',
  3383. '/option',
  3384. '/select'
  3385. );
  3386. $this->assertTags($result, $expected);
  3387. $result = $this->Form->select(
  3388. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3389. array('empty' => false)
  3390. );
  3391. $expected = array(
  3392. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3393. array('option' => array('value' => 'first')),
  3394. 'first &quot;html&quot; &lt;chars&gt;',
  3395. '/option',
  3396. array('option' => array('value' => 'second')),
  3397. 'value',
  3398. '/option',
  3399. '/select'
  3400. );
  3401. $this->assertTags($result, $expected);
  3402. $result = $this->Form->select(
  3403. 'Model.field',
  3404. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3405. array('escape' => false, 'empty' => false)
  3406. );
  3407. $expected = array(
  3408. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3409. array('option' => array('value' => 'first')),
  3410. 'first "html" <chars>',
  3411. '/option',
  3412. array('option' => array('value' => 'second')),
  3413. 'value',
  3414. '/option',
  3415. '/select'
  3416. );
  3417. $this->assertTags($result, $expected);
  3418. $options = array(
  3419. array('value' => 'first', 'name' => 'First'),
  3420. array('value' => 'first', 'name' => 'Another First'),
  3421. );
  3422. $result = $this->Form->select(
  3423. 'Model.field',
  3424. $options,
  3425. array('escape' => false, 'empty' => false)
  3426. );
  3427. $expected = array(
  3428. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3429. array('option' => array('value' => 'first')),
  3430. 'First',
  3431. '/option',
  3432. array('option' => array('value' => 'first')),
  3433. 'Another First',
  3434. '/option',
  3435. '/select'
  3436. );
  3437. $this->assertTags($result, $expected);
  3438. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3439. $result = $this->Form->select(
  3440. 'Model.contact_id',
  3441. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3442. array('escape' => false, 'empty' => 'pick something')
  3443. );
  3444. $expected = array(
  3445. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3446. array('option' => array('value' => '')), 'pick something', '/option',
  3447. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3448. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3449. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3450. '/select'
  3451. );
  3452. $this->assertTags($result, $expected);
  3453. $this->Form->request->data['Model']['field'] = 0;
  3454. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3455. $expected = array(
  3456. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3457. array('option' => array('value' => '')), '/option',
  3458. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3459. array('option' => array('value' => '1')), 'Yes', '/option',
  3460. '/select'
  3461. );
  3462. $this->assertTags($result, $expected);
  3463. }
  3464. /**
  3465. * test that select() with optiongroups listens to the escape param.
  3466. *
  3467. * @return void
  3468. */
  3469. public function testSelectOptionGroupEscaping() {
  3470. $options = array(
  3471. '>< Key' => array(
  3472. 1 => 'One',
  3473. 2 => 'Two'
  3474. )
  3475. );
  3476. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3477. $expected = array(
  3478. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3479. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3480. array('option' => array('value' => '1')), 'One', '/option',
  3481. array('option' => array('value' => '2')), 'Two', '/option',
  3482. '/optgroup',
  3483. '/select'
  3484. );
  3485. $this->assertTags($result, $expected);
  3486. $options = array(
  3487. '>< Key' => array(
  3488. 1 => 'One',
  3489. 2 => 'Two'
  3490. )
  3491. );
  3492. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3493. $expected = array(
  3494. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3495. 'optgroup' => array('label' => '>< Key'),
  3496. array('option' => array('value' => '1')), 'One', '/option',
  3497. array('option' => array('value' => '2')), 'Two', '/option',
  3498. '/optgroup',
  3499. '/select'
  3500. );
  3501. $this->assertTags($result, $expected);
  3502. }
  3503. /**
  3504. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3505. *
  3506. * @return void
  3507. */
  3508. public function testSelectWithNullAttributes() {
  3509. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3510. $expected = array(
  3511. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3512. array('option' => array('value' => '0')),
  3513. 'first',
  3514. '/option',
  3515. array('option' => array('value' => '1')),
  3516. 'second',
  3517. '/option',
  3518. '/select'
  3519. );
  3520. $this->assertTags($result, $expected);
  3521. }
  3522. /**
  3523. * testNestedSelect method
  3524. *
  3525. * test select element generation with optgroups
  3526. *
  3527. * @return void
  3528. */
  3529. public function testNestedSelect() {
  3530. $result = $this->Form->select(
  3531. 'Model.field',
  3532. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3533. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3534. )), array('empty' => false)
  3535. );
  3536. $expected = array(
  3537. 'select' => array('name' => 'data[Model][field]',
  3538. 'id' => 'ModelField'),
  3539. array('option' => array('value' => 1)),
  3540. 'One',
  3541. '/option',
  3542. array('option' => array('value' => 2)),
  3543. 'Two',
  3544. '/option',
  3545. array('optgroup' => array('label' => 'Three')),
  3546. array('option' => array('value' => 4)),
  3547. 'Four',
  3548. '/option',
  3549. array('option' => array('value' => 5)),
  3550. 'Five',
  3551. '/option',
  3552. '/optgroup',
  3553. '/select'
  3554. );
  3555. $this->assertTags($result, $expected);
  3556. $result = $this->Form->select(
  3557. 'Model.field',
  3558. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3559. array('showParents' => true, 'empty' => false)
  3560. );
  3561. $expected = array(
  3562. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3563. array('option' => array('value' => 1)),
  3564. 'One',
  3565. '/option',
  3566. array('option' => array('value' => 2)),
  3567. 'Two',
  3568. '/option',
  3569. array('optgroup' => array('label' => 'Three')),
  3570. array('option' => array('value' => 3)),
  3571. 'Three',
  3572. '/option',
  3573. array('option' => array('value' => 4)),
  3574. 'Four',
  3575. '/option',
  3576. '/optgroup',
  3577. '/select'
  3578. );
  3579. $this->assertTags($result, $expected);
  3580. }
  3581. /**
  3582. * testSelectMultiple method
  3583. *
  3584. * test generation of multiple select elements
  3585. *
  3586. * @return void
  3587. */
  3588. public function testSelectMultiple() {
  3589. $options = array('first', 'second', 'third');
  3590. $result = $this->Form->select(
  3591. 'Model.multi_field', $options, array('multiple' => true)
  3592. );
  3593. $expected = array(
  3594. 'input' => array(
  3595. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3596. ),
  3597. 'select' => array(
  3598. 'name' => 'data[Model][multi_field][]',
  3599. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3600. ),
  3601. array('option' => array('value' => '0')),
  3602. 'first',
  3603. '/option',
  3604. array('option' => array('value' => '1')),
  3605. 'second',
  3606. '/option',
  3607. array('option' => array('value' => '2')),
  3608. 'third',
  3609. '/option',
  3610. '/select'
  3611. );
  3612. $this->assertTags($result, $expected);
  3613. $result = $this->Form->select(
  3614. 'Model.multi_field', $options, array('multiple' => 'multiple')
  3615. );
  3616. $expected = array(
  3617. 'input' => array(
  3618. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3619. ),
  3620. 'select' => array(
  3621. 'name' => 'data[Model][multi_field][]',
  3622. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3623. ),
  3624. array('option' => array('value' => '0')),
  3625. 'first',
  3626. '/option',
  3627. array('option' => array('value' => '1')),
  3628. 'second',
  3629. '/option',
  3630. array('option' => array('value' => '2')),
  3631. 'third',
  3632. '/option',
  3633. '/select'
  3634. );
  3635. $this->assertTags($result, $expected);
  3636. $result = $this->Form->select(
  3637. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  3638. );
  3639. $expected = array(
  3640. 'input' => array(
  3641. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3642. ),
  3643. 'select' => array(
  3644. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  3645. 'multiple' => 'multiple'
  3646. ),
  3647. array('option' => array('value' => '0', 'selected' => 'selected')),
  3648. 'first',
  3649. '/option',
  3650. array('option' => array('value' => '1', 'selected' => 'selected')),
  3651. 'second',
  3652. '/option',
  3653. array('option' => array('value' => '2')),
  3654. 'third',
  3655. '/option',
  3656. '/select'
  3657. );
  3658. $this->assertTags($result, $expected);
  3659. $result = $this->Form->select(
  3660. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  3661. );
  3662. $expected = array(
  3663. 'select' => array(
  3664. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  3665. ),
  3666. array('option' => array('value' => '0', 'selected' => 'selected')),
  3667. 'first',
  3668. '/option',
  3669. array('option' => array('value' => '1', 'selected' => 'selected')),
  3670. 'second',
  3671. '/option',
  3672. array('option' => array('value' => '2')),
  3673. 'third',
  3674. '/option',
  3675. '/select'
  3676. );
  3677. $this->assertTags($result, $expected);
  3678. }
  3679. /**
  3680. * test generation of habtm select boxes.
  3681. *
  3682. * @return void
  3683. */
  3684. public function testHabtmSelectBox() {
  3685. $this->View->viewVars['contactTags'] = array(
  3686. 1 => 'blue',
  3687. 2 => 'red',
  3688. 3 => 'green'
  3689. );
  3690. $this->Form->request->data = array(
  3691. 'Contact' => array(),
  3692. 'ContactTag' => array(
  3693. array(
  3694. 'id' => 1,
  3695. 'name' => 'blue'
  3696. ),
  3697. array(
  3698. 'id' => 3,
  3699. 'name' => 'green'
  3700. )
  3701. )
  3702. );
  3703. $this->Form->create('Contact');
  3704. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3705. $expected = array(
  3706. 'input' => array(
  3707. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3708. ),
  3709. 'select' => array(
  3710. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  3711. 'multiple' => 'multiple'
  3712. ),
  3713. array('option' => array('value' => '1', 'selected' => 'selected')),
  3714. 'blue',
  3715. '/option',
  3716. array('option' => array('value' => '2')),
  3717. 'red',
  3718. '/option',
  3719. array('option' => array('value' => '3', 'selected' => 'selected')),
  3720. 'green',
  3721. '/option',
  3722. '/select'
  3723. );
  3724. $this->assertTags($result, $expected);
  3725. }
  3726. /**
  3727. * test generation of multi select elements in checkbox format
  3728. *
  3729. * @return void
  3730. */
  3731. public function testSelectMultipleCheckboxes() {
  3732. $result = $this->Form->select(
  3733. 'Model.multi_field',
  3734. array('first', 'second', 'third'),
  3735. array('multiple' => 'checkbox')
  3736. );
  3737. $expected = array(
  3738. 'input' => array(
  3739. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3740. ),
  3741. array('div' => array('class' => 'checkbox')),
  3742. array('input' => array(
  3743. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3744. 'value' => '0', 'id' => 'ModelMultiField0'
  3745. )),
  3746. array('label' => array('for' => 'ModelMultiField0')),
  3747. 'first',
  3748. '/label',
  3749. '/div',
  3750. array('div' => array('class' => 'checkbox')),
  3751. array('input' => array(
  3752. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3753. 'value' => '1', 'id' => 'ModelMultiField1'
  3754. )),
  3755. array('label' => array('for' => 'ModelMultiField1')),
  3756. 'second',
  3757. '/label',
  3758. '/div',
  3759. array('div' => array('class' => 'checkbox')),
  3760. array('input' => array(
  3761. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3762. 'value' => '2', 'id' => 'ModelMultiField2'
  3763. )),
  3764. array('label' => array('for' => 'ModelMultiField2')),
  3765. 'third',
  3766. '/label',
  3767. '/div'
  3768. );
  3769. $this->assertTags($result, $expected);
  3770. $result = $this->Form->select(
  3771. 'Model.multi_field',
  3772. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3773. array('multiple' => 'checkbox')
  3774. );
  3775. $expected = array(
  3776. 'input' => array(
  3777. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3778. ),
  3779. array('div' => array('class' => 'checkbox')),
  3780. array('input' => array(
  3781. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3782. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  3783. )),
  3784. array('label' => array('for' => 'ModelMultiFieldA')),
  3785. 'first',
  3786. '/label',
  3787. '/div',
  3788. array('div' => array('class' => 'checkbox')),
  3789. array('input' => array(
  3790. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3791. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  3792. )),
  3793. array('label' => array('for' => 'ModelMultiFieldB')),
  3794. 'second',
  3795. '/label',
  3796. '/div',
  3797. array('div' => array('class' => 'checkbox')),
  3798. array('input' => array(
  3799. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3800. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  3801. )),
  3802. array('label' => array('for' => 'ModelMultiFieldC')),
  3803. 'third',
  3804. '/label',
  3805. '/div'
  3806. );
  3807. $this->assertTags($result, $expected);
  3808. $result = $this->Form->select(
  3809. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  3810. );
  3811. $expected = array(
  3812. 'input' => array(
  3813. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3814. ),
  3815. array('div' => array('class' => 'checkbox')),
  3816. array('input' => array(
  3817. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3818. 'value' => '1', 'id' => 'ModelMultiField1'
  3819. )),
  3820. array('label' => array('for' => 'ModelMultiField1')),
  3821. 'first',
  3822. '/label',
  3823. '/div'
  3824. );
  3825. $this->assertTags($result, $expected);
  3826. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3827. $result = $this->Form->select(
  3828. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3829. );
  3830. $expected = array(
  3831. 'input' => array(
  3832. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3833. ),
  3834. array('div' => array('class' => 'checkbox')),
  3835. array('input' => array(
  3836. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3837. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  3838. )),
  3839. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  3840. 'first',
  3841. '/label',
  3842. '/div',
  3843. array('div' => array('class' => 'checkbox')),
  3844. array('input' => array(
  3845. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3846. 'value' => 'Array', 'id' => 'ModelTagsArray'
  3847. )),
  3848. array('label' => array('for' => 'ModelTagsArray')),
  3849. 'Array',
  3850. '/label',
  3851. '/div'
  3852. );
  3853. $this->assertTags($result, $expected);
  3854. }
  3855. /**
  3856. * test multiple checkboxes with div styles.
  3857. *
  3858. * @return void
  3859. */
  3860. public function testSelectMultipleCheckboxDiv() {
  3861. $result = $this->Form->select(
  3862. 'Model.tags',
  3863. array('first', 'second'),
  3864. array('multiple' => 'checkbox', 'class' => 'my-class')
  3865. );
  3866. $expected = array(
  3867. 'input' => array(
  3868. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3869. ),
  3870. array('div' => array('class' => 'my-class')),
  3871. array('input' => array(
  3872. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3873. 'value' => '0', 'id' => 'ModelTags0'
  3874. )),
  3875. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  3876. '/div',
  3877. array('div' => array('class' => 'my-class')),
  3878. array('input' => array(
  3879. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3880. 'value' => '1', 'id' => 'ModelTags1'
  3881. )),
  3882. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  3883. '/div'
  3884. );
  3885. $this->assertTags($result, $expected);
  3886. $result = $this->Form->input('Model.tags', array(
  3887. 'options' => array('first', 'second'),
  3888. 'multiple' => 'checkbox',
  3889. 'class' => 'my-class',
  3890. 'div' => false,
  3891. 'label' => false
  3892. ));
  3893. $this->assertTags($result, $expected);
  3894. $Contact = ClassRegistry::getObject('Contact');
  3895. $Contact->validationErrors['tags'] = 'Select atleast one option';
  3896. $result = $this->Form->input('Contact.tags', array(
  3897. 'options' => array('one'),
  3898. 'multiple' => 'checkbox',
  3899. 'label' => false,
  3900. 'div' => false
  3901. ));
  3902. $expected = array(
  3903. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3904. array('div' => array('class' => 'checkbox form-error')),
  3905. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3906. array('label' => array('for' => 'ContactTags0')),
  3907. 'one',
  3908. '/label',
  3909. '/div'
  3910. );
  3911. $this->assertTags($result, $expected);
  3912. $result = $this->Form->input('Contact.tags', array(
  3913. 'options' => array('one'),
  3914. 'multiple' => 'checkbox',
  3915. 'class' => 'mycheckbox',
  3916. 'label' => false,
  3917. 'div' => false
  3918. ));
  3919. $expected = array(
  3920. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3921. array('div' => array('class' => 'mycheckbox form-error')),
  3922. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3923. array('label' => array('for' => 'ContactTags0')),
  3924. 'one',
  3925. '/label',
  3926. '/div'
  3927. );
  3928. $this->assertTags($result, $expected);
  3929. }
  3930. /**
  3931. * Checks the security hash array generated for multiple-input checkbox elements
  3932. *
  3933. * @return void
  3934. */
  3935. public function testSelectMultipleCheckboxSecurity() {
  3936. $this->Form->request['_Token'] = array('key' => 'testKey');
  3937. $this->assertEquals(array(), $this->Form->fields);
  3938. $result = $this->Form->select(
  3939. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  3940. array('multiple' => 'checkbox')
  3941. );
  3942. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  3943. $result = $this->Form->secure($this->Form->fields);
  3944. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  3945. $this->assertRegExp('/"' . $key . '"/', $result);
  3946. }
  3947. /**
  3948. * Multiple select elements should always be secured as they always participate
  3949. * in the POST data.
  3950. *
  3951. * @return void
  3952. */
  3953. public function testSelectMultipleSecureWithNoOptions() {
  3954. $this->Form->request['_Token'] = array('key' => 'testkey');
  3955. $this->assertEquals(array(), $this->Form->fields);
  3956. $result = $this->Form->select(
  3957. 'Model.select',
  3958. array(),
  3959. array('multiple' => true)
  3960. );
  3961. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3962. }
  3963. /**
  3964. * When a select box has no options it should not be added to the fields list
  3965. * as it always fail post validation.
  3966. *
  3967. * @return void
  3968. */
  3969. public function testSelectNoSecureWithNoOptions() {
  3970. $this->Form->request['_Token'] = array('key' => 'testkey');
  3971. $this->assertEquals(array(), $this->Form->fields);
  3972. $this->Form->select(
  3973. 'Model.select',
  3974. array()
  3975. );
  3976. $this->assertEquals(array(), $this->Form->fields);
  3977. $this->Form->select(
  3978. 'Model.select',
  3979. array(),
  3980. array('empty' => true)
  3981. );
  3982. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3983. }
  3984. /**
  3985. * testInputMultipleCheckboxes method
  3986. *
  3987. * test input() resulting in multi select elements being generated.
  3988. *
  3989. * @return void
  3990. */
  3991. public function testInputMultipleCheckboxes() {
  3992. $result = $this->Form->input('Model.multi_field', array(
  3993. 'options' => array('first', 'second', 'third'),
  3994. 'multiple' => 'checkbox'
  3995. ));
  3996. $expected = array(
  3997. array('div' => array('class' => 'input select')),
  3998. array('label' => array('for' => 'ModelMultiField')),
  3999. 'Multi Field',
  4000. '/label',
  4001. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4002. array('div' => array('class' => 'checkbox')),
  4003. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4004. array('label' => array('for' => 'ModelMultiField0')),
  4005. 'first',
  4006. '/label',
  4007. '/div',
  4008. array('div' => array('class' => 'checkbox')),
  4009. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4010. array('label' => array('for' => 'ModelMultiField1')),
  4011. 'second',
  4012. '/label',
  4013. '/div',
  4014. array('div' => array('class' => 'checkbox')),
  4015. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4016. array('label' => array('for' => 'ModelMultiField2')),
  4017. 'third',
  4018. '/label',
  4019. '/div',
  4020. '/div'
  4021. );
  4022. $this->assertTags($result, $expected);
  4023. $result = $this->Form->input('Model.multi_field', array(
  4024. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4025. 'multiple' => 'checkbox'
  4026. ));
  4027. $expected = array(
  4028. array('div' => array('class' => 'input select')),
  4029. array('label' => array('for' => 'ModelMultiField')),
  4030. 'Multi Field',
  4031. '/label',
  4032. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4033. array('div' => array('class' => 'checkbox')),
  4034. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4035. array('label' => array('for' => 'ModelMultiFieldA')),
  4036. 'first',
  4037. '/label',
  4038. '/div',
  4039. array('div' => array('class' => 'checkbox')),
  4040. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4041. array('label' => array('for' => 'ModelMultiFieldB')),
  4042. 'second',
  4043. '/label',
  4044. '/div',
  4045. array('div' => array('class' => 'checkbox')),
  4046. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4047. array('label' => array('for' => 'ModelMultiFieldC')),
  4048. 'third',
  4049. '/label',
  4050. '/div',
  4051. '/div'
  4052. );
  4053. $this->assertTags($result, $expected);
  4054. $result = $this->Form->input('Model.multi_field', array(
  4055. 'options' => array('1' => 'first'),
  4056. 'multiple' => 'checkbox',
  4057. 'label' => false,
  4058. 'div' => false
  4059. ));
  4060. $expected = array(
  4061. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4062. array('div' => array('class' => 'checkbox')),
  4063. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4064. array('label' => array('for' => 'ModelMultiField1')),
  4065. 'first',
  4066. '/label',
  4067. '/div'
  4068. );
  4069. $this->assertTags($result, $expected);
  4070. $result = $this->Form->input('Model.multi_field', array(
  4071. 'options' => array('2' => 'second'),
  4072. 'multiple' => 'checkbox',
  4073. 'label' => false,
  4074. 'div' => false
  4075. ));
  4076. $expected = array(
  4077. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4078. array('div' => array('class' => 'checkbox')),
  4079. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4080. array('label' => array('for' => 'ModelMultiField2')),
  4081. 'second',
  4082. '/label',
  4083. '/div'
  4084. );
  4085. $this->assertTags($result, $expected);
  4086. }
  4087. /**
  4088. * testSelectHiddenFieldOmission method
  4089. *
  4090. * test that select() with 'hiddenField' => false omits the hidden field
  4091. *
  4092. * @return void
  4093. */
  4094. public function testSelectHiddenFieldOmission() {
  4095. $result = $this->Form->select('Model.multi_field',
  4096. array('first', 'second'),
  4097. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4098. );
  4099. $expected = array(
  4100. array('div' => array('class' => 'checkbox')),
  4101. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4102. array('label' => array('for' => 'ModelMultiField0')),
  4103. 'first',
  4104. '/label',
  4105. '/div',
  4106. array('div' => array('class' => 'checkbox')),
  4107. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4108. array('label' => array('for' => 'ModelMultiField1')),
  4109. 'second',
  4110. '/label',
  4111. '/div'
  4112. );
  4113. $this->assertTags($result, $expected);
  4114. $result = $this->Form->input('Model.multi_field', array(
  4115. 'options' => array('first', 'second'),
  4116. 'multiple' => 'checkbox',
  4117. 'hiddenField' => false
  4118. ));
  4119. $expected = array(
  4120. array('div' => array('class' => 'input select')),
  4121. array('label' => array('for' => 'ModelMultiField')),
  4122. 'Multi Field',
  4123. '/label',
  4124. array('div' => array('class' => 'checkbox')),
  4125. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4126. array('label' => array('for' => 'ModelMultiField0')),
  4127. 'first',
  4128. '/label',
  4129. '/div',
  4130. array('div' => array('class' => 'checkbox')),
  4131. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4132. array('label' => array('for' => 'ModelMultiField1')),
  4133. 'second',
  4134. '/label',
  4135. '/div',
  4136. '/div'
  4137. );
  4138. $this->assertTags($result, $expected);
  4139. }
  4140. /**
  4141. * test that select() with multiple = checkbox works with overriding name attribute.
  4142. *
  4143. * @return void
  4144. */
  4145. public function testSelectCheckboxMultipleOverrideName() {
  4146. $result = $this->Form->input('category', array(
  4147. 'type' => 'select',
  4148. 'multiple' => 'checkbox',
  4149. 'name' => 'data[fish]',
  4150. 'options' => array('1', '2'),
  4151. 'div' => false,
  4152. 'label' => false,
  4153. ));
  4154. $expected = array(
  4155. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4156. array('div' => array('class' => 'checkbox')),
  4157. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4158. array('label' => array('for' => 'Category0')), '1', '/label',
  4159. '/div',
  4160. array('div' => array('class' => 'checkbox')),
  4161. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4162. array('label' => array('for' => 'Category1')), '2', '/label',
  4163. '/div'
  4164. );
  4165. $this->assertTags($result, $expected);
  4166. }
  4167. /**
  4168. * Test that 'id' overrides all the checkbox id's as well.
  4169. *
  4170. * @return void
  4171. */
  4172. public function testSelectCheckboxMultipleId() {
  4173. $result = $this->Form->select(
  4174. 'Model.multi_field',
  4175. array('first', 'second', 'third'),
  4176. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4177. );
  4178. $expected = array(
  4179. 'input' => array(
  4180. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4181. ),
  4182. array('div' => array('class' => 'checkbox')),
  4183. array('input' => array(
  4184. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4185. 'value' => '0', 'id' => 'CustomId0'
  4186. )),
  4187. array('label' => array('for' => 'CustomId0')),
  4188. 'first',
  4189. '/label',
  4190. '/div',
  4191. array('div' => array('class' => 'checkbox')),
  4192. array('input' => array(
  4193. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4194. 'value' => '1', 'id' => 'CustomId1'
  4195. )),
  4196. array('label' => array('for' => 'CustomId1')),
  4197. 'second',
  4198. '/label',
  4199. '/div',
  4200. array('div' => array('class' => 'checkbox')),
  4201. array('input' => array(
  4202. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4203. 'value' => '2', 'id' => 'CustomId2'
  4204. )),
  4205. array('label' => array('for' => 'CustomId2')),
  4206. 'third',
  4207. '/label',
  4208. '/div'
  4209. );
  4210. $this->assertTags($result, $expected);
  4211. }
  4212. /**
  4213. * testCheckbox method
  4214. *
  4215. * Test generation of checkboxes
  4216. *
  4217. * @return void
  4218. */
  4219. public function testCheckbox() {
  4220. $result = $this->Form->checkbox('Model.field');
  4221. $expected = array(
  4222. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4223. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4224. );
  4225. $this->assertTags($result, $expected);
  4226. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4227. $expected = array(
  4228. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4229. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4230. );
  4231. $this->assertTags($result, $expected);
  4232. $Contact = ClassRegistry::getObject('Contact');
  4233. $Contact->validationErrors['field'] = 1;
  4234. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4235. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4236. $expected = array(
  4237. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4238. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4239. );
  4240. $this->assertTags($result, $expected);
  4241. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4242. $expected = array(
  4243. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4244. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4245. );
  4246. $this->assertTags($result, $expected);
  4247. $this->Form->request->data['Contact']['field'] = '';
  4248. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4249. $expected = array(
  4250. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4251. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4252. );
  4253. $this->assertTags($result, $expected);
  4254. $Contact->validationErrors = array();
  4255. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4256. $expected = array(
  4257. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4258. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4259. );
  4260. $this->assertTags($result, $expected);
  4261. $this->Form->request->data['Contact']['published'] = 1;
  4262. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4263. $expected = array(
  4264. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4265. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4266. );
  4267. $this->assertTags($result, $expected);
  4268. $this->Form->request->data['Contact']['published'] = 0;
  4269. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4270. $expected = array(
  4271. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4272. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4273. );
  4274. $this->assertTags($result, $expected);
  4275. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4276. $expected = array(
  4277. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4278. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4279. );
  4280. $this->assertTags($result, $expected);
  4281. $result = $this->Form->checkbox('CustomField.1.value');
  4282. $expected = array(
  4283. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4284. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4285. );
  4286. $this->assertTags($result, $expected);
  4287. }
  4288. /**
  4289. * test checkbox() with a custom name attribute
  4290. *
  4291. * @return void
  4292. */
  4293. public function testCheckboxCustomNameAttribute() {
  4294. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4295. $expected = array(
  4296. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4297. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4298. );
  4299. $this->assertTags($result, $expected);
  4300. }
  4301. /**
  4302. * test the checked option for checkboxes.
  4303. *
  4304. * @return void
  4305. */
  4306. public function testCheckboxCheckedOption() {
  4307. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4308. $expected = array(
  4309. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4310. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4311. );
  4312. $this->assertTags($result, $expected);
  4313. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4314. $expected = array(
  4315. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4316. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4317. );
  4318. $this->assertTags($result, $expected);
  4319. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4320. $expected = array(
  4321. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4322. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4323. );
  4324. $this->assertTags($result, $expected);
  4325. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4326. $expected = array(
  4327. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4328. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4329. );
  4330. $this->assertTags($result, $expected);
  4331. $this->Form->request->data['Model']['field'] = 1;
  4332. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4333. $expected = array(
  4334. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4335. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4336. );
  4337. $this->assertTags($result, $expected);
  4338. }
  4339. /**
  4340. * Test that disabled attribute works on both the checkbox and hidden input.
  4341. *
  4342. * @return void
  4343. */
  4344. public function testCheckboxDisabling() {
  4345. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4346. $expected = array(
  4347. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4348. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4349. );
  4350. $this->assertTags($result, $expected);
  4351. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4352. $expected = array(
  4353. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4354. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4355. );
  4356. $this->assertTags($result, $expected);
  4357. }
  4358. /**
  4359. * Test that the hidden input for checkboxes can be omitted or set to a
  4360. * specific value.
  4361. *
  4362. * @return void
  4363. */
  4364. public function testCheckboxHiddenField() {
  4365. $result = $this->Form->input('UserForm.something', array(
  4366. 'type' => 'checkbox',
  4367. 'hiddenField' => false
  4368. ));
  4369. $expected = array(
  4370. 'div' => array('class' => 'input checkbox'),
  4371. array('input' => array(
  4372. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4373. 'value' => '1', 'id' => 'UserFormSomething'
  4374. )),
  4375. 'label' => array('for' => 'UserFormSomething'),
  4376. 'Something',
  4377. '/label',
  4378. '/div'
  4379. );
  4380. $this->assertTags($result, $expected);
  4381. $result = $this->Form->input('UserForm.something', array(
  4382. 'type' => 'checkbox',
  4383. 'value' => 'Y',
  4384. 'hiddenField' => 'N',
  4385. ));
  4386. $expected = array(
  4387. 'div' => array('class' => 'input checkbox'),
  4388. array('input' => array(
  4389. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  4390. 'value' => 'N', 'id' => 'UserFormSomething_'
  4391. )),
  4392. array('input' => array(
  4393. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4394. 'value' => 'Y', 'id' => 'UserFormSomething'
  4395. )),
  4396. 'label' => array('for' => 'UserFormSomething'),
  4397. 'Something',
  4398. '/label',
  4399. '/div'
  4400. );
  4401. $this->assertTags($result, $expected);
  4402. }
  4403. /**
  4404. * testDateTime method
  4405. *
  4406. * Test generation of date/time select elements
  4407. *
  4408. * @return void
  4409. */
  4410. public function testDateTime() {
  4411. extract($this->dateRegex);
  4412. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4413. $now = strtotime('now');
  4414. $expected = array(
  4415. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4416. $daysRegex,
  4417. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4418. date('j', $now),
  4419. '/option',
  4420. '*/select',
  4421. '-',
  4422. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4423. $monthsRegex,
  4424. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4425. date('F', $now),
  4426. '/option',
  4427. '*/select',
  4428. '-',
  4429. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4430. $yearsRegex,
  4431. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4432. date('Y', $now),
  4433. '/option',
  4434. '*/select',
  4435. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4436. $hoursRegex,
  4437. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4438. date('g', $now),
  4439. '/option',
  4440. '*/select',
  4441. ':',
  4442. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4443. $minutesRegex,
  4444. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4445. date('i', $now),
  4446. '/option',
  4447. '*/select',
  4448. ' ',
  4449. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4450. $meridianRegex,
  4451. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4452. date('a', $now),
  4453. '/option',
  4454. '*/select'
  4455. );
  4456. $this->assertTags($result, $expected);
  4457. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4458. $expected = array(
  4459. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4460. $daysRegex,
  4461. array('option' => array('value' => '')),
  4462. '/option',
  4463. '*/select',
  4464. '-',
  4465. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4466. $monthsRegex,
  4467. array('option' => array('value' => '')),
  4468. '/option',
  4469. '*/select',
  4470. '-',
  4471. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4472. $yearsRegex,
  4473. array('option' => array('value' => '')),
  4474. '/option',
  4475. '*/select',
  4476. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4477. $hoursRegex,
  4478. array('option' => array('value' => '')),
  4479. '/option',
  4480. '*/select',
  4481. ':',
  4482. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4483. $minutesRegex,
  4484. array('option' => array('value' => '')),
  4485. '/option',
  4486. '*/select',
  4487. ' ',
  4488. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4489. $meridianRegex,
  4490. array('option' => array('value' => '')),
  4491. '/option',
  4492. '*/select'
  4493. );
  4494. $this->assertTags($result, $expected);
  4495. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4496. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  4497. $this->assertTags($result, $expected);
  4498. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4499. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  4500. $this->assertTags($result, $expected);
  4501. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4502. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  4503. $expected = array(
  4504. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4505. $daysRegex,
  4506. array('option' => array('value' => '')),
  4507. '/option',
  4508. '*/select',
  4509. '-',
  4510. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4511. $monthsRegex,
  4512. array('option' => array('value' => '')),
  4513. '/option',
  4514. '*/select',
  4515. '-',
  4516. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4517. $yearsRegex,
  4518. array('option' => array('value' => '')),
  4519. '/option',
  4520. '*/select',
  4521. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4522. $hoursRegex,
  4523. array('option' => array('value' => '')),
  4524. '/option',
  4525. '*/select',
  4526. ':',
  4527. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4528. $minutesRegex,
  4529. array('option' => array('value' => '')),
  4530. '/option',
  4531. array('option' => array('value' => '00')),
  4532. '00',
  4533. '/option',
  4534. array('option' => array('value' => '05')),
  4535. '05',
  4536. '/option',
  4537. array('option' => array('value' => '10')),
  4538. '10',
  4539. '/option',
  4540. '*/select',
  4541. ' ',
  4542. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4543. $meridianRegex,
  4544. array('option' => array('value' => '')),
  4545. '/option',
  4546. '*/select'
  4547. );
  4548. $this->assertTags($result, $expected);
  4549. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4550. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4551. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4552. $this->Form->request->data['Contact']['data'] = null;
  4553. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4554. $expected = array(
  4555. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4556. $daysRegex,
  4557. array('option' => array('value' => '')),
  4558. '/option',
  4559. '*/select',
  4560. '-',
  4561. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4562. $monthsRegex,
  4563. array('option' => array('value' => '')),
  4564. '/option',
  4565. '*/select',
  4566. '-',
  4567. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4568. $yearsRegex,
  4569. array('option' => array('value' => '')),
  4570. '/option',
  4571. '*/select',
  4572. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4573. $hoursRegex,
  4574. array('option' => array('value' => '')),
  4575. '/option',
  4576. '*/select',
  4577. ':',
  4578. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4579. $minutesRegex,
  4580. array('option' => array('value' => '')),
  4581. '/option',
  4582. '*/select',
  4583. ' ',
  4584. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4585. $meridianRegex,
  4586. array('option' => array('value' => '')),
  4587. '/option',
  4588. '*/select'
  4589. );
  4590. $this->assertTags($result, $expected);
  4591. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4592. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  4593. $now = strtotime($this->Form->data['Model']['field']);
  4594. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  4595. $expected = array(
  4596. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4597. $daysRegex,
  4598. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4599. date('j', $now),
  4600. '/option',
  4601. '*/select',
  4602. '-',
  4603. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4604. $monthsRegex,
  4605. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4606. date('F', $now),
  4607. '/option',
  4608. '*/select',
  4609. '-',
  4610. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  4611. $yearsRegex,
  4612. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4613. date('Y', $now),
  4614. '/option',
  4615. '*/select',
  4616. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4617. $hoursRegex,
  4618. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4619. date('g', $now),
  4620. '/option',
  4621. '*/select',
  4622. ':',
  4623. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4624. $minutesRegex,
  4625. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4626. date('i', $now),
  4627. '/option',
  4628. '*/select',
  4629. ' ',
  4630. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  4631. $meridianRegex,
  4632. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4633. date('a', $now),
  4634. '/option',
  4635. '*/select'
  4636. );
  4637. $this->assertTags($result, $expected);
  4638. $selected = strtotime('2008-10-26 12:33:00');
  4639. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  4640. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  4641. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  4642. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  4643. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  4644. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  4645. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  4646. $this->Form->create('Contact');
  4647. $result = $this->Form->input('published');
  4648. $now = strtotime('now');
  4649. $expected = array(
  4650. 'div' => array('class' => 'input date'),
  4651. 'label' => array('for' => 'ContactPublishedMonth'),
  4652. 'Published',
  4653. '/label',
  4654. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4655. $monthsRegex,
  4656. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4657. date('F', $now),
  4658. '/option',
  4659. '*/select',
  4660. '-',
  4661. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4662. $daysRegex,
  4663. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4664. date('j', $now),
  4665. '/option',
  4666. '*/select',
  4667. '-',
  4668. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4669. $yearsRegex,
  4670. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4671. date('Y', $now),
  4672. '/option',
  4673. '*/select',
  4674. '/div'
  4675. );
  4676. $this->assertTags($result, $expected);
  4677. $result = $this->Form->input('published2', array('type' => 'date'));
  4678. $now = strtotime('now');
  4679. $expected = array(
  4680. 'div' => array('class' => 'input date'),
  4681. 'label' => array('for' => 'ContactPublished2Month'),
  4682. 'Published2',
  4683. '/label',
  4684. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  4685. $monthsRegex,
  4686. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4687. date('F', $now),
  4688. '/option',
  4689. '*/select',
  4690. '-',
  4691. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  4692. $daysRegex,
  4693. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4694. date('j', $now),
  4695. '/option',
  4696. '*/select',
  4697. '-',
  4698. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  4699. $yearsRegex,
  4700. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4701. date('Y', $now),
  4702. '/option',
  4703. '*/select',
  4704. '/div'
  4705. );
  4706. $this->assertTags($result, $expected);
  4707. $this->Form->create('Contact');
  4708. $result = $this->Form->input('published', array('monthNames' => false));
  4709. $now = strtotime('now');
  4710. $expected = array(
  4711. 'div' => array('class' => 'input date'),
  4712. 'label' => array('for' => 'ContactPublishedMonth'),
  4713. 'Published',
  4714. '/label',
  4715. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4716. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4717. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4718. date('m', $now),
  4719. '/option',
  4720. '*/select',
  4721. '-',
  4722. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4723. $daysRegex,
  4724. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4725. date('j', $now),
  4726. '/option',
  4727. '*/select',
  4728. '-',
  4729. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4730. $yearsRegex,
  4731. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4732. date('Y', $now),
  4733. '/option',
  4734. '*/select',
  4735. '/div'
  4736. );
  4737. $this->assertTags($result, $expected);
  4738. $result = $this->Form->input('published', array('type' => 'time'));
  4739. $now = strtotime('now');
  4740. $expected = array(
  4741. 'div' => array('class' => 'input time'),
  4742. 'label' => array('for' => 'ContactPublishedHour'),
  4743. 'Published',
  4744. '/label',
  4745. array('select' => array('name' => 'data[Contact][published][hour]', 'id' => 'ContactPublishedHour')),
  4746. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4747. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4748. date('g', $now),
  4749. '/option',
  4750. '*/select',
  4751. ':',
  4752. );
  4753. $this->assertTags($result, $expected);
  4754. $result = $this->Form->input('published', array(
  4755. 'timeFormat' => 24,
  4756. 'interval' => 5,
  4757. 'selected' => strtotime('2009-09-03 13:37:00'),
  4758. 'type' => 'datetime'
  4759. ));
  4760. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4761. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  4762. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  4763. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  4764. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  4765. $this->assertNoErrors();
  4766. $this->Form->request->data['Contact'] = array(
  4767. 'date' => array(
  4768. 'day' => '',
  4769. 'month' => '',
  4770. 'year' => '',
  4771. 'hour' => '',
  4772. 'min' => '',
  4773. 'meridian' => ''
  4774. )
  4775. );
  4776. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4777. }
  4778. /**
  4779. * test that datetime() and default values work.
  4780. *
  4781. * @return void
  4782. */
  4783. public function testDatetimeWithDefault() {
  4784. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  4785. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4786. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4787. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4788. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  4789. 'default' => '2009-06-01 11:15:30'
  4790. ));
  4791. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4792. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4793. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4794. }
  4795. /**
  4796. * test that bogus non-date time data doesn't cause errors.
  4797. *
  4798. * @return void
  4799. */
  4800. public function testDateTimeWithBogusData() {
  4801. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  4802. $this->assertNotRegExp('/selected="selected">\d/', $result);
  4803. }
  4804. /**
  4805. * testDateTimeEmptyAsArray
  4806. *
  4807. * @return void
  4808. */
  4809. public function testDateTimeEmptyAsArray() {
  4810. $result = $this->Form->dateTime('Contact.date',
  4811. 'DMY',
  4812. '12',
  4813. array(
  4814. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  4815. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  4816. )
  4817. )
  4818. );
  4819. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4820. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4821. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4822. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4823. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4824. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4825. $result = $this->Form->dateTime('Contact.date',
  4826. 'DMY',
  4827. '12',
  4828. array(
  4829. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4830. )
  4831. );
  4832. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4833. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4834. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4835. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  4836. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  4837. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  4838. }
  4839. /**
  4840. * testFormDateTimeMulti method
  4841. *
  4842. * test multiple datetime element generation
  4843. *
  4844. * @return void
  4845. */
  4846. public function testFormDateTimeMulti() {
  4847. extract($this->dateRegex);
  4848. $result = $this->Form->dateTime('Contact.1.updated');
  4849. $expected = array(
  4850. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  4851. $daysRegex,
  4852. array('option' => array('value' => '')),
  4853. '/option',
  4854. '*/select',
  4855. '-',
  4856. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  4857. $monthsRegex,
  4858. array('option' => array('value' => '')),
  4859. '/option',
  4860. '*/select',
  4861. '-',
  4862. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  4863. $yearsRegex,
  4864. array('option' => array('value' => '')),
  4865. '/option',
  4866. '*/select',
  4867. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  4868. $hoursRegex,
  4869. array('option' => array('value' => '')),
  4870. '/option',
  4871. '*/select',
  4872. ':',
  4873. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  4874. $minutesRegex,
  4875. array('option' => array('value' => '')),
  4876. '/option',
  4877. '*/select',
  4878. ' ',
  4879. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  4880. $meridianRegex,
  4881. array('option' => array('value' => '')),
  4882. '/option',
  4883. '*/select'
  4884. );
  4885. $this->assertTags($result, $expected);
  4886. $result = $this->Form->dateTime('Contact.2.updated');
  4887. $expected = array(
  4888. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  4889. $daysRegex,
  4890. array('option' => array('value' => '')),
  4891. '/option',
  4892. '*/select',
  4893. '-',
  4894. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  4895. $monthsRegex,
  4896. array('option' => array('value' => '')),
  4897. '/option',
  4898. '*/select',
  4899. '-',
  4900. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  4901. $yearsRegex,
  4902. array('option' => array('value' => '')),
  4903. '/option',
  4904. '*/select',
  4905. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  4906. $hoursRegex,
  4907. array('option' => array('value' => '')),
  4908. '/option',
  4909. '*/select',
  4910. ':',
  4911. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  4912. $minutesRegex,
  4913. array('option' => array('value' => '')),
  4914. '/option',
  4915. '*/select',
  4916. ' ',
  4917. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  4918. $meridianRegex,
  4919. array('option' => array('value' => '')),
  4920. '/option',
  4921. '*/select'
  4922. );
  4923. $this->assertTags($result, $expected);
  4924. }
  4925. /**
  4926. * When changing the date format, the label should always focus the first select box when
  4927. * clicked.
  4928. *
  4929. * @return void
  4930. */
  4931. public function testDateTimeLabelIdMatchesFirstInput() {
  4932. $result = $this->Form->input('Model.date', array('type' => 'date'));
  4933. $this->assertContains('label for="ModelDateMonth"', $result);
  4934. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  4935. $this->assertContains('label for="ModelDateDay"', $result);
  4936. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  4937. $this->assertContains('label for="ModelDateYear"', $result);
  4938. }
  4939. /**
  4940. * testMonth method
  4941. *
  4942. * @return void
  4943. */
  4944. public function testMonth() {
  4945. $result = $this->Form->month('Model.field');
  4946. $expected = array(
  4947. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4948. array('option' => array('value' => '')),
  4949. '/option',
  4950. array('option' => array('value' => '01')),
  4951. date('F', strtotime('2008-01-01 00:00:00')),
  4952. '/option',
  4953. array('option' => array('value' => '02')),
  4954. date('F', strtotime('2008-02-01 00:00:00')),
  4955. '/option',
  4956. '*/select',
  4957. );
  4958. $this->assertTags($result, $expected);
  4959. $result = $this->Form->month('Model.field', array('empty' => true));
  4960. $expected = array(
  4961. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4962. array('option' => array('value' => '')),
  4963. '/option',
  4964. array('option' => array('value' => '01')),
  4965. date('F', strtotime('2008-01-01 00:00:00')),
  4966. '/option',
  4967. array('option' => array('value' => '02')),
  4968. date('F', strtotime('2008-02-01 00:00:00')),
  4969. '/option',
  4970. '*/select',
  4971. );
  4972. $this->assertTags($result, $expected);
  4973. $result = $this->Form->month('Model.field', array('monthNames' => false));
  4974. $expected = array(
  4975. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4976. array('option' => array('value' => '')),
  4977. '/option',
  4978. array('option' => array('value' => '01')),
  4979. '01',
  4980. '/option',
  4981. array('option' => array('value' => '02')),
  4982. '02',
  4983. '/option',
  4984. '*/select',
  4985. );
  4986. $this->assertTags($result, $expected);
  4987. $monthNames = array(
  4988. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  4989. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  4990. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  4991. $expected = array(
  4992. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4993. array('option' => array('value' => '')),
  4994. '/option',
  4995. array('option' => array('value' => '01')),
  4996. 'Jan',
  4997. '/option',
  4998. array('option' => array('value' => '02')),
  4999. 'Feb',
  5000. '/option',
  5001. '*/select',
  5002. );
  5003. $this->assertTags($result, $expected);
  5004. }
  5005. /**
  5006. * testDay method
  5007. *
  5008. * @return void
  5009. */
  5010. public function testDay() {
  5011. extract($this->dateRegex);
  5012. $result = $this->Form->day('Model.field', array('value' => false));
  5013. $expected = array(
  5014. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5015. array('option' => array('value' => '')),
  5016. '/option',
  5017. array('option' => array('value' => '01')),
  5018. '1',
  5019. '/option',
  5020. array('option' => array('value' => '02')),
  5021. '2',
  5022. '/option',
  5023. $daysRegex,
  5024. '/select',
  5025. );
  5026. $this->assertTags($result, $expected);
  5027. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5028. $result = $this->Form->day('Model.field');
  5029. $expected = array(
  5030. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5031. array('option' => array('value' => '')),
  5032. '/option',
  5033. array('option' => array('value' => '01')),
  5034. '1',
  5035. '/option',
  5036. array('option' => array('value' => '02')),
  5037. '2',
  5038. '/option',
  5039. $daysRegex,
  5040. array('option' => array('value' => '10', 'selected' => 'selected')),
  5041. '10',
  5042. '/option',
  5043. $daysRegex,
  5044. '/select',
  5045. );
  5046. $this->assertTags($result, $expected);
  5047. $this->Form->request->data['Model']['field'] = '';
  5048. $result = $this->Form->day('Model.field', array('value' => '10'));
  5049. $expected = array(
  5050. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5051. array('option' => array('value' => '')),
  5052. '/option',
  5053. array('option' => array('value' => '01')),
  5054. '1',
  5055. '/option',
  5056. array('option' => array('value' => '02')),
  5057. '2',
  5058. '/option',
  5059. $daysRegex,
  5060. array('option' => array('value' => '10', 'selected' => 'selected')),
  5061. '10',
  5062. '/option',
  5063. $daysRegex,
  5064. '/select',
  5065. );
  5066. $this->assertTags($result, $expected);
  5067. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5068. $result = $this->Form->day('Model.field', array('value' => true));
  5069. $expected = array(
  5070. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5071. array('option' => array('value' => '')),
  5072. '/option',
  5073. array('option' => array('value' => '01')),
  5074. '1',
  5075. '/option',
  5076. array('option' => array('value' => '02')),
  5077. '2',
  5078. '/option',
  5079. $daysRegex,
  5080. array('option' => array('value' => '10', 'selected' => 'selected')),
  5081. '10',
  5082. '/option',
  5083. $daysRegex,
  5084. '/select',
  5085. );
  5086. $this->assertTags($result, $expected);
  5087. }
  5088. /**
  5089. * testMinute method
  5090. *
  5091. * @return void
  5092. */
  5093. public function testMinute() {
  5094. extract($this->dateRegex);
  5095. $result = $this->Form->minute('Model.field');
  5096. $expected = array(
  5097. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5098. array('option' => array('value' => '')),
  5099. '/option',
  5100. array('option' => array('value' => '00')),
  5101. '00',
  5102. '/option',
  5103. array('option' => array('value' => '01')),
  5104. '01',
  5105. '/option',
  5106. array('option' => array('value' => '02')),
  5107. '02',
  5108. '/option',
  5109. $minutesRegex,
  5110. '/select',
  5111. );
  5112. $this->assertTags($result, $expected);
  5113. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5114. $result = $this->Form->minute('Model.field');
  5115. $expected = array(
  5116. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5117. array('option' => array('value' => '')),
  5118. '/option',
  5119. array('option' => array('value' => '00')),
  5120. '00',
  5121. '/option',
  5122. array('option' => array('value' => '01')),
  5123. '01',
  5124. '/option',
  5125. array('option' => array('value' => '02')),
  5126. '02',
  5127. '/option',
  5128. $minutesRegex,
  5129. array('option' => array('value' => '12', 'selected' => 'selected')),
  5130. '12',
  5131. '/option',
  5132. $minutesRegex,
  5133. '/select',
  5134. );
  5135. $this->assertTags($result, $expected);
  5136. $this->Form->request->data['Model']['field'] = '';
  5137. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5138. $expected = array(
  5139. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5140. array('option' => array('value' => '')),
  5141. '/option',
  5142. array('option' => array('value' => '00')),
  5143. '00',
  5144. '/option',
  5145. array('option' => array('value' => '05')),
  5146. '05',
  5147. '/option',
  5148. array('option' => array('value' => '10')),
  5149. '10',
  5150. '/option',
  5151. $minutesRegex,
  5152. '/select',
  5153. );
  5154. $this->assertTags($result, $expected);
  5155. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5156. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5157. $expected = array(
  5158. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5159. array('option' => array('value' => '')),
  5160. '/option',
  5161. array('option' => array('value' => '00')),
  5162. '00',
  5163. '/option',
  5164. array('option' => array('value' => '05')),
  5165. '05',
  5166. '/option',
  5167. array('option' => array('value' => '10', 'selected' => 'selected')),
  5168. '10',
  5169. '/option',
  5170. $minutesRegex,
  5171. '/select',
  5172. );
  5173. $this->assertTags($result, $expected);
  5174. }
  5175. /**
  5176. * testHour method
  5177. *
  5178. * @return void
  5179. */
  5180. public function testHour() {
  5181. extract($this->dateRegex);
  5182. $result = $this->Form->hour('Model.field', false);
  5183. $expected = array(
  5184. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5185. array('option' => array('value' => '')),
  5186. '/option',
  5187. array('option' => array('value' => '01')),
  5188. '1',
  5189. '/option',
  5190. array('option' => array('value' => '02')),
  5191. '2',
  5192. '/option',
  5193. $hoursRegex,
  5194. '/select',
  5195. );
  5196. $this->assertTags($result, $expected);
  5197. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5198. $result = $this->Form->hour('Model.field', false);
  5199. $expected = array(
  5200. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5201. array('option' => array('value' => '')),
  5202. '/option',
  5203. array('option' => array('value' => '01')),
  5204. '1',
  5205. '/option',
  5206. array('option' => array('value' => '02')),
  5207. '2',
  5208. '/option',
  5209. $hoursRegex,
  5210. array('option' => array('value' => '12', 'selected' => 'selected')),
  5211. '12',
  5212. '/option',
  5213. '/select',
  5214. );
  5215. $this->assertTags($result, $expected);
  5216. $this->Form->request->data['Model']['field'] = '';
  5217. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5218. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5219. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  5220. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5221. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5222. $result = $this->Form->hour('Model.field', true);
  5223. $expected = array(
  5224. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5225. array('option' => array('value' => '')),
  5226. '/option',
  5227. array('option' => array('value' => '00', 'selected' => 'selected')),
  5228. '0',
  5229. '/option',
  5230. array('option' => array('value' => '01')),
  5231. '1',
  5232. '/option',
  5233. array('option' => array('value' => '02')),
  5234. '2',
  5235. '/option',
  5236. $hoursRegex,
  5237. '/select',
  5238. );
  5239. $this->assertTags($result, $expected);
  5240. unset($this->Form->request->data['Model']['field']);
  5241. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5242. $thisHour = date('H');
  5243. $optValue = date('G');
  5244. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5245. }
  5246. /**
  5247. * testYear method
  5248. *
  5249. * @return void
  5250. */
  5251. public function testYear() {
  5252. $result = $this->Form->year('Model.field', 2006, 2007);
  5253. $expected = array(
  5254. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5255. array('option' => array('value' => '')),
  5256. '/option',
  5257. array('option' => array('value' => '2007')),
  5258. '2007',
  5259. '/option',
  5260. array('option' => array('value' => '2006')),
  5261. '2006',
  5262. '/option',
  5263. '/select',
  5264. );
  5265. $this->assertTags($result, $expected);
  5266. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5267. $expected = array(
  5268. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5269. array('option' => array('value' => '')),
  5270. '/option',
  5271. array('option' => array('value' => '2006')),
  5272. '2006',
  5273. '/option',
  5274. array('option' => array('value' => '2007')),
  5275. '2007',
  5276. '/option',
  5277. '/select',
  5278. );
  5279. $this->assertTags($result, $expected);
  5280. $this->request->data['Contact']['published'] = '';
  5281. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5282. $expected = array(
  5283. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5284. array('option' => array('value' => '')),
  5285. '/option',
  5286. array('option' => array('value' => '2007')),
  5287. '2007',
  5288. '/option',
  5289. array('option' => array('value' => '2006')),
  5290. '2006',
  5291. '/option',
  5292. '/select',
  5293. );
  5294. $this->assertTags($result, $expected);
  5295. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5296. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5297. $expected = array(
  5298. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5299. array('option' => array('value' => '2007')),
  5300. '2007',
  5301. '/option',
  5302. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5303. '2006',
  5304. '/option',
  5305. '/select',
  5306. );
  5307. $this->assertTags($result, $expected);
  5308. $this->Form->request->data['Contact']['published'] = '';
  5309. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5310. $expected = array(
  5311. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5312. array('option' => array('value' => '')),
  5313. '/option',
  5314. array('option' => array('value' => '2007')),
  5315. '2007',
  5316. '/option',
  5317. array('option' => array('value' => '2006')),
  5318. '2006',
  5319. '/option',
  5320. '/select',
  5321. );
  5322. $this->assertTags($result, $expected);
  5323. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5324. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5325. $expected = array(
  5326. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5327. array('option' => array('value' => '2007')),
  5328. '2007',
  5329. '/option',
  5330. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5331. '2006',
  5332. '/option',
  5333. '/select',
  5334. );
  5335. $this->assertTags($result, $expected);
  5336. $this->Form->request->data['Contact']['published'] = '';
  5337. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5338. $expected = array(
  5339. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5340. array('option' => array('value' => '')),
  5341. '/option',
  5342. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5343. '2007',
  5344. '/option',
  5345. array('option' => array('value' => '2006')),
  5346. '2006',
  5347. '/option',
  5348. '/select',
  5349. );
  5350. $this->assertTags($result, $expected);
  5351. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5352. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5353. $expected = array(
  5354. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5355. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5356. '2007',
  5357. '/option',
  5358. array('option' => array('value' => '2006')),
  5359. '2006',
  5360. '/option',
  5361. '/select',
  5362. );
  5363. $this->assertTags($result, $expected);
  5364. $this->Form->request->data['Contact']['published'] = '';
  5365. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5366. $expected = array(
  5367. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5368. array('option' => array('value' => '2008')),
  5369. '2008',
  5370. '/option',
  5371. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5372. '2007',
  5373. '/option',
  5374. array('option' => array('value' => '2006')),
  5375. '2006',
  5376. '/option',
  5377. '/select',
  5378. );
  5379. $this->assertTags($result, $expected);
  5380. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5381. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5382. $expected = array(
  5383. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5384. array('option' => array('value' => '2008')),
  5385. '2008',
  5386. '/option',
  5387. array('option' => array('value' => '2007')),
  5388. '2007',
  5389. '/option',
  5390. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5391. '2006',
  5392. '/option',
  5393. '/select',
  5394. );
  5395. $this->assertTags($result, $expected);
  5396. $this->Form->request->data = array();
  5397. $this->Form->create('Contact');
  5398. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5399. $expected = array(
  5400. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5401. array('option' => array('value' => '2008')),
  5402. '2008',
  5403. '/option',
  5404. array('option' => array('value' => '2007')),
  5405. '2007',
  5406. '/option',
  5407. array('option' => array('value' => '2006')),
  5408. '2006',
  5409. '/option',
  5410. '/select',
  5411. );
  5412. $this->assertTags($result, $expected);
  5413. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  5414. $this->assertContains('data[Contact][published][year]', $result);
  5415. }
  5416. /**
  5417. * testTextArea method
  5418. *
  5419. * @return void
  5420. */
  5421. public function testTextArea() {
  5422. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  5423. $result = $this->Form->textarea('Model.field');
  5424. $expected = array(
  5425. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5426. 'some test data',
  5427. '/textarea',
  5428. );
  5429. $this->assertTags($result, $expected);
  5430. $result = $this->Form->textarea('Model.tmp');
  5431. $expected = array(
  5432. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  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');
  5438. $expected = array(
  5439. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5440. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5441. '/textarea',
  5442. );
  5443. $this->assertTags($result, $expected);
  5444. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5445. $result = $this->Form->textarea('Model.field', array('escape' => false));
  5446. $expected = array(
  5447. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5448. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5449. '/textarea',
  5450. );
  5451. $this->assertTags($result, $expected);
  5452. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  5453. $result = $this->Form->textarea('Model.0.OtherModel.field');
  5454. $expected = array(
  5455. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  5456. '/textarea'
  5457. );
  5458. $this->assertTags($result, $expected);
  5459. }
  5460. /**
  5461. * testTextAreaWithStupidCharacters method
  5462. *
  5463. * test text area with non-ascii characters
  5464. *
  5465. * @return void
  5466. */
  5467. public function testTextAreaWithStupidCharacters() {
  5468. $this->loadFixtures('Post');
  5469. $result = $this->Form->input('Post.content', array(
  5470. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  5471. ));
  5472. $expected = array(
  5473. 'div' => array('class' => 'input text'),
  5474. 'label' => array('for' => 'PostContent'),
  5475. 'Current Text',
  5476. '/label',
  5477. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  5478. 'GREAT®',
  5479. '/textarea',
  5480. '/div'
  5481. );
  5482. $this->assertTags($result, $expected);
  5483. }
  5484. /**
  5485. * testHiddenField method
  5486. *
  5487. * @return void
  5488. */
  5489. public function testHiddenField() {
  5490. $Contact = ClassRegistry::getObject('Contact');
  5491. $Contact->validationErrors['field'] = 1;
  5492. $this->Form->request->data['Contact']['field'] = 'test';
  5493. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  5494. $this->assertTags($result, array(
  5495. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  5496. );
  5497. }
  5498. /**
  5499. * testFileUploadField method
  5500. *
  5501. * @return void
  5502. */
  5503. public function testFileUploadField() {
  5504. $result = $this->Form->file('Model.upload');
  5505. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5506. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  5507. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  5508. $expected = array(
  5509. 'div' => array('class' => 'input file'),
  5510. 'label' => array('for' => 'ModelUpload'),
  5511. 'Upload',
  5512. '/label',
  5513. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  5514. '/div'
  5515. );
  5516. $this->assertTags($result, $expected);
  5517. }
  5518. /**
  5519. * test File upload input on a model not used in create();
  5520. *
  5521. * @return void
  5522. */
  5523. public function testFileUploadOnOtherModel() {
  5524. $this->Form->create('ValidateUser', array('type' => 'file'));
  5525. $result = $this->Form->file('ValidateProfile.city');
  5526. $expected = array(
  5527. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  5528. );
  5529. $this->assertTags($result, $expected);
  5530. }
  5531. /**
  5532. * testButton method
  5533. *
  5534. * @return void
  5535. */
  5536. public function testButton() {
  5537. $result = $this->Form->button('Hi');
  5538. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5539. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5540. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5541. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5542. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5543. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5544. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5545. $result = $this->Form->button('No type', array('type' => false));
  5546. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5547. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  5548. $this->assertNotRegExp('/\&039/', $result);
  5549. }
  5550. /**
  5551. * Test that button() makes unlocked fields by default.
  5552. *
  5553. * @return void
  5554. */
  5555. public function testButtonUnlockedByDefault() {
  5556. $this->Form->request->params['_Token']['key'] = 'secured';
  5557. $this->Form->button('Save', array('name' => 'save'));
  5558. $this->Form->button('Clear');
  5559. $result = $this->Form->unlockField();
  5560. $this->assertEquals(array('save'), $result);
  5561. }
  5562. /**
  5563. * testPostButton method
  5564. *
  5565. * @return void
  5566. */
  5567. public function testPostButton() {
  5568. $result = $this->Form->postButton('Hi', '/controller/action');
  5569. $this->assertTags($result, array(
  5570. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5571. 'div' => array('style' => 'display:none;'),
  5572. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5573. '/div',
  5574. 'button' => array('type' => 'submit'),
  5575. 'Hi',
  5576. '/button',
  5577. '/form'
  5578. ));
  5579. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5580. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  5581. }
  5582. /**
  5583. * Test that postButton adds _Token fields.
  5584. *
  5585. * @return void
  5586. */
  5587. public function testSecurePostButton() {
  5588. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5589. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5590. $expected = array(
  5591. 'form' => array(
  5592. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5593. ),
  5594. array('div' => array('style' => 'display:none;')),
  5595. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5596. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5597. '/div',
  5598. 'button' => array('type' => 'submit'),
  5599. 'Delete',
  5600. '/button',
  5601. array('div' => array('style' => 'display:none;')),
  5602. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5603. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5604. '/div',
  5605. '/form',
  5606. );
  5607. $this->assertTags($result, $expected);
  5608. }
  5609. /**
  5610. * testPostLink method
  5611. *
  5612. * @return void
  5613. */
  5614. public function testPostLink() {
  5615. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5616. $this->assertTags($result, array(
  5617. 'form' => array(
  5618. 'method' => 'post', 'action' => '/posts/delete/1',
  5619. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5620. ),
  5621. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5622. '/form',
  5623. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5624. 'Delete',
  5625. '/a'
  5626. ));
  5627. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5628. $this->assertTags($result, array(
  5629. 'form' => array(
  5630. 'method' => 'post', 'action' => '/posts/delete/1',
  5631. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5632. ),
  5633. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5634. '/form',
  5635. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5636. 'Delete',
  5637. '/a'
  5638. ));
  5639. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5640. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  5641. }
  5642. /**
  5643. * Test that postLink adds _Token fields.
  5644. *
  5645. * @return void
  5646. */
  5647. public function testSecurePostLink() {
  5648. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5649. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5650. $expected = array(
  5651. 'form' => array(
  5652. 'method' => 'post', 'action' => '/posts/delete/1',
  5653. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5654. ),
  5655. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5656. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5657. 'div' => array('style' => 'display:none;'),
  5658. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5659. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5660. '/div',
  5661. '/form',
  5662. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5663. 'Delete',
  5664. '/a'
  5665. );
  5666. $this->assertTags($result, $expected);
  5667. }
  5668. /**
  5669. * testSubmitButton method
  5670. *
  5671. * @return void
  5672. */
  5673. public function testSubmitButton() {
  5674. $result = $this->Form->submit('');
  5675. $expected = array(
  5676. 'div' => array('class' => 'submit'),
  5677. 'input' => array('type' => 'submit', 'value' => ''),
  5678. '/div'
  5679. );
  5680. $this->assertTags($result, $expected);
  5681. $result = $this->Form->submit('Test Submit');
  5682. $expected = array(
  5683. 'div' => array('class' => 'submit'),
  5684. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5685. '/div'
  5686. );
  5687. $this->assertTags($result, $expected);
  5688. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5689. $expected = array(
  5690. 'span' => array('class' => 'submit'),
  5691. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5692. '/span'
  5693. );
  5694. $this->assertTags($result, $expected);
  5695. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5696. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5697. $this->assertTags($result, $expected);
  5698. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5699. $expected = array(
  5700. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5701. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5702. '/div'
  5703. );
  5704. $this->assertTags($result, $expected);
  5705. $result = $this->Form->submit('Next >');
  5706. $expected = array(
  5707. 'div' => array('class' => 'submit'),
  5708. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5709. '/div'
  5710. );
  5711. $this->assertTags($result, $expected);
  5712. $result = $this->Form->submit('Next >', array('escape' => false));
  5713. $expected = array(
  5714. 'div' => array('class' => 'submit'),
  5715. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5716. '/div'
  5717. );
  5718. $this->assertTags($result, $expected);
  5719. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5720. $expected = array(
  5721. 'div' => array('class' => 'submit'),
  5722. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5723. '/div'
  5724. );
  5725. $this->assertTags($result, $expected);
  5726. $before = '--before--';
  5727. $after = '--after--';
  5728. $result = $this->Form->submit('Test', array('before' => $before));
  5729. $expected = array(
  5730. 'div' => array('class' => 'submit'),
  5731. '--before--',
  5732. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5733. '/div'
  5734. );
  5735. $this->assertTags($result, $expected);
  5736. $result = $this->Form->submit('Test', array('after' => $after));
  5737. $expected = array(
  5738. 'div' => array('class' => 'submit'),
  5739. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5740. '--after--',
  5741. '/div'
  5742. );
  5743. $this->assertTags($result, $expected);
  5744. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5745. $expected = array(
  5746. 'div' => array('class' => 'submit'),
  5747. '--before--',
  5748. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5749. '--after--',
  5750. '/div'
  5751. );
  5752. $this->assertTags($result, $expected);
  5753. }
  5754. /**
  5755. * test image submit types.
  5756. *
  5757. * @return void
  5758. */
  5759. public function testSubmitImage() {
  5760. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5761. $expected = array(
  5762. 'div' => array('class' => 'submit'),
  5763. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5764. '/div'
  5765. );
  5766. $this->assertTags($result, $expected);
  5767. $result = $this->Form->submit('/relative/cake.power.gif');
  5768. $expected = array(
  5769. 'div' => array('class' => 'submit'),
  5770. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5771. '/div'
  5772. );
  5773. $this->assertTags($result, $expected);
  5774. $result = $this->Form->submit('cake.power.gif');
  5775. $expected = array(
  5776. 'div' => array('class' => 'submit'),
  5777. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5778. '/div'
  5779. );
  5780. $this->assertTags($result, $expected);
  5781. $result = $this->Form->submit('Not.an.image');
  5782. $expected = array(
  5783. 'div' => array('class' => 'submit'),
  5784. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5785. '/div'
  5786. );
  5787. $this->assertTags($result, $expected);
  5788. $after = '--after--';
  5789. $before = '--before--';
  5790. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5791. $expected = array(
  5792. 'div' => array('class' => 'submit'),
  5793. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5794. '--after--',
  5795. '/div'
  5796. );
  5797. $this->assertTags($result, $expected);
  5798. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5799. $expected = array(
  5800. 'div' => array('class' => 'submit'),
  5801. '--before--',
  5802. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5803. '/div'
  5804. );
  5805. $this->assertTags($result, $expected);
  5806. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5807. $expected = array(
  5808. 'div' => array('class' => 'submit'),
  5809. '--before--',
  5810. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5811. '--after--',
  5812. '/div'
  5813. );
  5814. $this->assertTags($result, $expected);
  5815. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  5816. $expected = array(
  5817. 'div' => array('class' => 'submit'),
  5818. '--before--',
  5819. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5820. '--after--',
  5821. '/div'
  5822. );
  5823. $this->assertTags($result, $expected);
  5824. }
  5825. /**
  5826. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5827. * be submitted at a time.
  5828. *
  5829. * @return void
  5830. */
  5831. public function testSubmitUnlockedByDefault() {
  5832. $this->Form->request->params['_Token']['key'] = 'secured';
  5833. $this->Form->submit('Go go');
  5834. $this->Form->submit('Save', array('name' => 'save'));
  5835. $result = $this->Form->unlockField();
  5836. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5837. }
  5838. /**
  5839. * Test submit image with timestamps.
  5840. *
  5841. * @return void
  5842. */
  5843. public function testSubmitImageTimestamp() {
  5844. Configure::write('Asset.timestamp', 'force');
  5845. $result = $this->Form->submit('cake.power.gif');
  5846. $expected = array(
  5847. 'div' => array('class' => 'submit'),
  5848. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  5849. '/div'
  5850. );
  5851. $this->assertTags($result, $expected);
  5852. }
  5853. /**
  5854. * test the create() method
  5855. *
  5856. * @return void
  5857. */
  5858. public function testCreate() {
  5859. $result = $this->Form->create('Contact');
  5860. $encoding = strtolower(Configure::read('App.encoding'));
  5861. $expected = array(
  5862. 'form' => array(
  5863. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5864. 'accept-charset' => $encoding
  5865. ),
  5866. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  5867. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5868. '/div'
  5869. );
  5870. $this->assertTags($result, $expected);
  5871. $result = $this->Form->create('Contact', array('type' => 'GET'));
  5872. $expected = array('form' => array(
  5873. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5874. 'accept-charset' => $encoding
  5875. ));
  5876. $this->assertTags($result, $expected);
  5877. $result = $this->Form->create('Contact', array('type' => 'get'));
  5878. $expected = array('form' => array(
  5879. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5880. 'accept-charset' => $encoding
  5881. ));
  5882. $this->assertTags($result, $expected);
  5883. $result = $this->Form->create('Contact', array('type' => 'put'));
  5884. $expected = array(
  5885. 'form' => array(
  5886. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5887. 'accept-charset' => $encoding
  5888. ),
  5889. 'div' => array('style' => 'display:none;'),
  5890. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5891. '/div'
  5892. );
  5893. $this->assertTags($result, $expected);
  5894. $result = $this->Form->create('Contact', array('type' => 'file'));
  5895. $expected = array(
  5896. 'form' => array(
  5897. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5898. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5899. ),
  5900. 'div' => array('style' => 'display:none;'),
  5901. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5902. '/div'
  5903. );
  5904. $this->assertTags($result, $expected);
  5905. $this->Form->request->data['Contact']['id'] = 1;
  5906. $this->Form->request->here = '/contacts/edit/1';
  5907. $this->Form->request['action'] = 'edit';
  5908. $result = $this->Form->create('Contact');
  5909. $expected = array(
  5910. 'form' => array(
  5911. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5912. 'accept-charset' => $encoding
  5913. ),
  5914. 'div' => array('style' => 'display:none;'),
  5915. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5916. '/div'
  5917. );
  5918. $this->assertTags($result, $expected);
  5919. $this->Form->request->data['Contact']['id'] = 1;
  5920. $this->Form->request->here = '/contacts/edit/1';
  5921. $this->Form->request['action'] = 'edit';
  5922. $result = $this->Form->create('Contact', array('type' => 'file'));
  5923. $expected = array(
  5924. 'form' => array(
  5925. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5926. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5927. ),
  5928. 'div' => array('style' => 'display:none;'),
  5929. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5930. '/div'
  5931. );
  5932. $this->assertTags($result, $expected);
  5933. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  5934. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  5935. $expected = array(
  5936. 'form' => array(
  5937. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  5938. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  5939. ),
  5940. 'div' => array('style' => 'display:none;'),
  5941. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5942. '/div'
  5943. );
  5944. $this->assertTags($result, $expected);
  5945. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  5946. $expected = array(
  5947. 'form' => array(
  5948. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  5949. 'accept-charset' => $encoding
  5950. ),
  5951. 'div' => array('style' => 'display:none;'),
  5952. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5953. '/div'
  5954. );
  5955. $this->assertTags($result, $expected);
  5956. $this->Form->request['action'] = 'add';
  5957. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  5958. $expected = array(
  5959. 'form' => array(
  5960. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  5961. 'accept-charset' => $encoding
  5962. ),
  5963. 'div' => array('style' => 'display:none;'),
  5964. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5965. '/div'
  5966. );
  5967. $this->assertTags($result, $expected);
  5968. $result = $this->Form->create('User', array('action' => 'login'));
  5969. $expected = array(
  5970. 'form' => array(
  5971. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  5972. 'accept-charset' => $encoding
  5973. ),
  5974. 'div' => array('style' => 'display:none;'),
  5975. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5976. '/div'
  5977. );
  5978. $this->assertTags($result, $expected);
  5979. $result = $this->Form->create('User', array('url' => '/users/login'));
  5980. $expected = array(
  5981. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  5982. 'div' => array('style' => 'display:none;'),
  5983. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5984. '/div'
  5985. );
  5986. $this->assertTags($result, $expected);
  5987. $this->Form->request['controller'] = 'pages';
  5988. $result = $this->Form->create('User', array('action' => 'signup'));
  5989. $expected = array(
  5990. 'form' => array(
  5991. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  5992. 'accept-charset' => $encoding
  5993. ),
  5994. 'div' => array('style' => 'display:none;'),
  5995. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5996. '/div'
  5997. );
  5998. $this->assertTags($result, $expected);
  5999. $this->Form->request->data = array();
  6000. $this->Form->request['controller'] = 'contacts';
  6001. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6002. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  6003. $expected = array(
  6004. 'form' => array(
  6005. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  6006. 'accept-charset' => 'utf-8'
  6007. ),
  6008. 'div' => array('style' => 'display:none;'),
  6009. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6010. '/div'
  6011. );
  6012. $this->assertTags($result, $expected);
  6013. }
  6014. /**
  6015. * Test the onsubmit option for create()
  6016. *
  6017. * @return void
  6018. */
  6019. public function testCreateOnSubmit() {
  6020. $this->Form->request->data = array();
  6021. $this->Form->request['controller'] = 'contacts';
  6022. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6023. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  6024. $expected = array(
  6025. 'form' => array(
  6026. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  6027. 'accept-charset' => 'utf-8'
  6028. ),
  6029. 'div' => array('style' => 'display:none;'),
  6030. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6031. '/div'
  6032. );
  6033. $this->assertTags($result, $expected);
  6034. $this->Form->request->data = array();
  6035. $this->Form->request['controller'] = 'contacts';
  6036. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6037. $result = $this->Form->create(array(
  6038. 'url' => array('action' => 'index', 'param'),
  6039. 'default' => false,
  6040. 'onsubmit' => 'someFunction();'
  6041. ));
  6042. $expected = array(
  6043. 'form' => array(
  6044. 'id' => 'ContactAddForm', 'method' => 'post',
  6045. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6046. 'action' => '/contacts/index/param',
  6047. 'accept-charset' => 'utf-8'
  6048. ),
  6049. 'div' => array('style' => 'display:none;'),
  6050. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6051. '/div'
  6052. );
  6053. $this->assertTags($result, $expected);
  6054. }
  6055. /**
  6056. * test create() with automatic url generation
  6057. *
  6058. * @return void
  6059. */
  6060. public function testCreateAutoUrl() {
  6061. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6062. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6063. $this->Form->request->base = '/base_url';
  6064. $result = $this->Form->create('Contact');
  6065. $expected = array(
  6066. 'form' => array(
  6067. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6068. 'accept-charset' => 'utf-8'
  6069. ),
  6070. 'div' => array('style' => 'display:none;'),
  6071. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6072. '/div'
  6073. );
  6074. $this->assertTags($result, $expected);
  6075. $this->Form->request['action'] = 'delete';
  6076. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6077. $this->Form->request->base = '/base_url';
  6078. $result = $this->Form->create('Contact');
  6079. $expected = array(
  6080. 'form' => array(
  6081. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6082. 'accept-charset' => 'utf-8'
  6083. ),
  6084. 'div' => array('style' => 'display:none;'),
  6085. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6086. '/div'
  6087. );
  6088. $this->assertTags($result, $expected);
  6089. }
  6090. /**
  6091. * test create() with a custom route
  6092. *
  6093. * @return void
  6094. */
  6095. public function testCreateCustomRoute() {
  6096. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6097. $encoding = strtolower(Configure::read('App.encoding'));
  6098. $result = $this->Form->create('User', array('action' => 'login'));
  6099. $expected = array(
  6100. 'form' => array(
  6101. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6102. 'accept-charset' => $encoding
  6103. ),
  6104. 'div' => array('style' => 'display:none;'),
  6105. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6106. '/div'
  6107. );
  6108. $this->assertTags($result, $expected);
  6109. }
  6110. /**
  6111. * test that inputDefaults are stored and used.
  6112. *
  6113. * @return void
  6114. */
  6115. public function testCreateWithInputDefaults() {
  6116. $this->Form->create('User', array(
  6117. 'inputDefaults' => array(
  6118. 'div' => false,
  6119. 'label' => false,
  6120. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6121. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6122. )
  6123. ));
  6124. $result = $this->Form->input('username');
  6125. $expected = array(
  6126. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6127. );
  6128. $this->assertTags($result, $expected);
  6129. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6130. $expected = array(
  6131. 'div' => array('class' => 'input text'),
  6132. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6133. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6134. '/div'
  6135. );
  6136. $this->assertTags($result, $expected);
  6137. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  6138. $expected = array(
  6139. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6140. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  6141. );
  6142. $this->assertTags($result, $expected);
  6143. $this->Form->create('User', array(
  6144. 'inputDefaults' => array(
  6145. 'div' => false,
  6146. 'label' => array('class' => 'nice', 'for' => 'changed'),
  6147. )
  6148. ));
  6149. $result = $this->Form->input('username', array('div' => true));
  6150. $expected = array(
  6151. 'div' => array('class' => 'input text'),
  6152. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  6153. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6154. '/div'
  6155. );
  6156. $this->assertTags($result, $expected);
  6157. }
  6158. /**
  6159. * test automatic accept-charset overriding
  6160. *
  6161. * @return void
  6162. */
  6163. public function testCreateWithAcceptCharset() {
  6164. $result = $this->Form->create('UserForm', array(
  6165. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  6166. )
  6167. );
  6168. $expected = array(
  6169. 'form' => array(
  6170. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6171. 'accept-charset' => 'iso-8859-1'
  6172. ),
  6173. 'div' => array('style' => 'display:none;'),
  6174. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6175. '/div'
  6176. );
  6177. $this->assertTags($result, $expected);
  6178. }
  6179. /**
  6180. * Test base form url when url param is passed with multiple parameters (&)
  6181. *
  6182. */
  6183. public function testCreateQuerystringrequest() {
  6184. $encoding = strtolower(Configure::read('App.encoding'));
  6185. $result = $this->Form->create('Contact', array(
  6186. 'type' => 'post',
  6187. 'escape' => false,
  6188. 'url' => array(
  6189. 'controller' => 'controller',
  6190. 'action' => 'action',
  6191. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6192. )
  6193. ));
  6194. $expected = array(
  6195. 'form' => array(
  6196. 'id' => 'ContactAddForm',
  6197. 'method' => 'post',
  6198. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6199. 'accept-charset' => $encoding
  6200. ),
  6201. 'div' => array('style' => 'display:none;'),
  6202. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6203. '/div'
  6204. );
  6205. $this->assertTags($result, $expected);
  6206. $result = $this->Form->create('Contact', array(
  6207. 'type' => 'post',
  6208. 'url' => array(
  6209. 'controller' => 'controller',
  6210. 'action' => 'action',
  6211. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6212. )
  6213. ));
  6214. $expected = array(
  6215. 'form' => array(
  6216. 'id' => 'ContactAddForm',
  6217. 'method' => 'post',
  6218. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6219. 'accept-charset' => $encoding
  6220. ),
  6221. 'div' => array('style' => 'display:none;'),
  6222. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6223. '/div'
  6224. );
  6225. $this->assertTags($result, $expected);
  6226. }
  6227. /**
  6228. * test that create() doesn't cause errors by multiple id's being in the primary key
  6229. * as could happen with multiple select or checkboxes.
  6230. *
  6231. * @return void
  6232. */
  6233. public function testCreateWithMultipleIdInData() {
  6234. $encoding = strtolower(Configure::read('App.encoding'));
  6235. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6236. $result = $this->Form->create('Contact');
  6237. $expected = array(
  6238. 'form' => array(
  6239. 'id' => 'ContactAddForm',
  6240. 'method' => 'post',
  6241. 'action' => '/contacts/add',
  6242. 'accept-charset' => $encoding
  6243. ),
  6244. 'div' => array('style' => 'display:none;'),
  6245. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6246. '/div'
  6247. );
  6248. $this->assertTags($result, $expected);
  6249. }
  6250. /**
  6251. * test that create() doesn't add in extra passed params.
  6252. *
  6253. * @return void
  6254. */
  6255. public function testCreatePassedArgs() {
  6256. $encoding = strtolower(Configure::read('App.encoding'));
  6257. $this->Form->request->data['Contact']['id'] = 1;
  6258. $result = $this->Form->create('Contact', array(
  6259. 'type' => 'post',
  6260. 'escape' => false,
  6261. 'url' => array(
  6262. 'action' => 'edit',
  6263. 'myparam'
  6264. )
  6265. ));
  6266. $expected = array(
  6267. 'form' => array(
  6268. 'id' => 'ContactAddForm',
  6269. 'method' => 'post',
  6270. 'action' => '/contacts/edit/myparam',
  6271. 'accept-charset' => $encoding
  6272. ),
  6273. 'div' => array('style' => 'display:none;'),
  6274. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6275. '/div'
  6276. );
  6277. $this->assertTags($result, $expected);
  6278. }
  6279. /**
  6280. * test creating a get form, and get form inputs.
  6281. *
  6282. * @return void
  6283. */
  6284. public function testGetFormCreate() {
  6285. $encoding = strtolower(Configure::read('App.encoding'));
  6286. $result = $this->Form->create('Contact', array('type' => 'get'));
  6287. $this->assertTags($result, array('form' => array(
  6288. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6289. 'accept-charset' => $encoding
  6290. )));
  6291. $result = $this->Form->text('Contact.name');
  6292. $this->assertTags($result, array('input' => array(
  6293. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6294. )));
  6295. $result = $this->Form->password('password');
  6296. $this->assertTags($result, array('input' => array(
  6297. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6298. )));
  6299. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6300. $result = $this->Form->text('user_form');
  6301. $this->assertTags($result, array('input' => array(
  6302. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6303. )));
  6304. }
  6305. /**
  6306. * test get form, and inputs when the model param is false
  6307. *
  6308. * @return void
  6309. */
  6310. public function testGetFormWithFalseModel() {
  6311. $encoding = strtolower(Configure::read('App.encoding'));
  6312. $this->Form->request['controller'] = 'contact_test';
  6313. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6314. $expected = array('form' => array(
  6315. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6316. 'accept-charset' => $encoding
  6317. ));
  6318. $this->assertTags($result, $expected);
  6319. $result = $this->Form->text('reason');
  6320. $expected = array(
  6321. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6322. );
  6323. $this->assertTags($result, $expected);
  6324. }
  6325. /**
  6326. * test that datetime() works with GET style forms.
  6327. *
  6328. * @return void
  6329. */
  6330. public function testDateTimeWithGetForms() {
  6331. extract($this->dateRegex);
  6332. $this->Form->create('Contact', array('type' => 'get'));
  6333. $result = $this->Form->datetime('created');
  6334. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6335. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6336. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6337. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6338. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6339. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6340. }
  6341. /**
  6342. * testEditFormWithData method
  6343. *
  6344. * test auto populating form elements from submitted data.
  6345. *
  6346. * @return void
  6347. */
  6348. public function testEditFormWithData() {
  6349. $this->Form->request->data = array('Person' => array(
  6350. 'id' => 1,
  6351. 'first_name' => 'Nate',
  6352. 'last_name' => 'Abele',
  6353. 'email' => 'nate@example.com'
  6354. ));
  6355. $this->Form->request->addParams(array(
  6356. 'models' => array('Person'),
  6357. 'controller' => 'people',
  6358. 'action' => 'add'
  6359. ));
  6360. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6361. $this->Form->create();
  6362. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6363. $expected = array(
  6364. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6365. 'select' => array(
  6366. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6367. ),
  6368. array('option' => array('value' => 1)), 'Nate', '/option',
  6369. array('option' => array('value' => 2)), 'Garrett', '/option',
  6370. array('option' => array('value' => 3)), 'Larry', '/option',
  6371. '/select'
  6372. );
  6373. $this->assertTags($result, $expected);
  6374. }
  6375. /**
  6376. * Test that required fields are created for various types of validation.
  6377. *
  6378. * @return void
  6379. */
  6380. public function testFormInputRequiredDetection() {
  6381. $this->Form->create('Contact');
  6382. $result = $this->Form->input('Contact.non_existing');
  6383. $expected = array(
  6384. 'div' => array('class' => 'input text'),
  6385. 'label' => array('for' => 'ContactNonExisting'),
  6386. 'Non Existing',
  6387. '/label',
  6388. 'input' => array(
  6389. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  6390. 'id' => 'ContactNonExisting'
  6391. ),
  6392. '/div'
  6393. );
  6394. $this->assertTags($result, $expected);
  6395. $result = $this->Form->input('Contact.imrequired');
  6396. $expected = array(
  6397. 'div' => array('class' => 'input text required'),
  6398. 'label' => array('for' => 'ContactImrequired'),
  6399. 'Imrequired',
  6400. '/label',
  6401. 'input' => array(
  6402. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  6403. 'id' => 'ContactImrequired'
  6404. ),
  6405. '/div'
  6406. );
  6407. $this->assertTags($result, $expected);
  6408. $result = $this->Form->input('Contact.imalsorequired');
  6409. $expected = array(
  6410. 'div' => array('class' => 'input text required'),
  6411. 'label' => array('for' => 'ContactImalsorequired'),
  6412. 'Imalsorequired',
  6413. '/label',
  6414. 'input' => array(
  6415. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  6416. 'id' => 'ContactImalsorequired'
  6417. ),
  6418. '/div'
  6419. );
  6420. $this->assertTags($result, $expected);
  6421. $result = $this->Form->input('Contact.imrequiredtoo');
  6422. $expected = array(
  6423. 'div' => array('class' => 'input text required'),
  6424. 'label' => array('for' => 'ContactImrequiredtoo'),
  6425. 'Imrequiredtoo',
  6426. '/label',
  6427. 'input' => array(
  6428. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  6429. 'id' => 'ContactImrequiredtoo'
  6430. ),
  6431. '/div'
  6432. );
  6433. $this->assertTags($result, $expected);
  6434. $result = $this->Form->input('Contact.required_one');
  6435. $expected = array(
  6436. 'div' => array('class' => 'input text required'),
  6437. 'label' => array('for' => 'ContactRequiredOne'),
  6438. 'Required One',
  6439. '/label',
  6440. 'input' => array(
  6441. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  6442. 'id' => 'ContactRequiredOne'
  6443. ),
  6444. '/div'
  6445. );
  6446. $this->assertTags($result, $expected);
  6447. $result = $this->Form->input('Contact.string_required');
  6448. $expected = array(
  6449. 'div' => array('class' => 'input text required'),
  6450. 'label' => array('for' => 'ContactStringRequired'),
  6451. 'String Required',
  6452. '/label',
  6453. 'input' => array(
  6454. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  6455. 'id' => 'ContactStringRequired'
  6456. ),
  6457. '/div'
  6458. );
  6459. $this->assertTags($result, $expected);
  6460. $result = $this->Form->input('Contact.imnotrequired');
  6461. $expected = array(
  6462. 'div' => array('class' => 'input text'),
  6463. 'label' => array('for' => 'ContactImnotrequired'),
  6464. 'Imnotrequired',
  6465. '/label',
  6466. 'input' => array(
  6467. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  6468. 'id' => 'ContactImnotrequired'
  6469. ),
  6470. '/div'
  6471. );
  6472. $this->assertTags($result, $expected);
  6473. $result = $this->Form->input('Contact.imalsonotrequired');
  6474. $expected = array(
  6475. 'div' => array('class' => 'input text'),
  6476. 'label' => array('for' => 'ContactImalsonotrequired'),
  6477. 'Imalsonotrequired',
  6478. '/label',
  6479. 'input' => array(
  6480. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  6481. 'id' => 'ContactImalsonotrequired'
  6482. ),
  6483. '/div'
  6484. );
  6485. $this->assertTags($result, $expected);
  6486. $result = $this->Form->input('Contact.imnotrequiredeither');
  6487. $expected = array(
  6488. 'div' => array('class' => 'input text'),
  6489. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6490. 'Imnotrequiredeither',
  6491. '/label',
  6492. 'input' => array(
  6493. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  6494. 'id' => 'ContactImnotrequiredeither'
  6495. ),
  6496. '/div'
  6497. );
  6498. $this->assertTags($result, $expected);
  6499. }
  6500. /**
  6501. * testFormMagicInput method
  6502. *
  6503. * @return void
  6504. */
  6505. public function testFormMagicInput() {
  6506. $encoding = strtolower(Configure::read('App.encoding'));
  6507. $result = $this->Form->create('Contact');
  6508. $expected = array(
  6509. 'form' => array(
  6510. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6511. 'accept-charset' => $encoding
  6512. ),
  6513. 'div' => array('style' => 'display:none;'),
  6514. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6515. '/div'
  6516. );
  6517. $this->assertTags($result, $expected);
  6518. $result = $this->Form->input('name');
  6519. $expected = array(
  6520. 'div' => array('class' => 'input text'),
  6521. 'label' => array('for' => 'ContactName'),
  6522. 'Name',
  6523. '/label',
  6524. 'input' => array(
  6525. 'type' => 'text', 'name' => 'data[Contact][name]',
  6526. 'id' => 'ContactName', 'maxlength' => '255'
  6527. ),
  6528. '/div'
  6529. );
  6530. $this->assertTags($result, $expected);
  6531. $result = $this->Form->input('non_existing_field_in_contact_model');
  6532. $expected = array(
  6533. 'div' => array('class' => 'input text'),
  6534. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  6535. 'Non Existing Field In Contact Model',
  6536. '/label',
  6537. 'input' => array(
  6538. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  6539. 'id' => 'ContactNonExistingFieldInContactModel'
  6540. ),
  6541. '/div'
  6542. );
  6543. $this->assertTags($result, $expected);
  6544. $result = $this->Form->input('Address.street');
  6545. $expected = array(
  6546. 'div' => array('class' => 'input text'),
  6547. 'label' => array('for' => 'AddressStreet'),
  6548. 'Street',
  6549. '/label',
  6550. 'input' => array(
  6551. 'type' => 'text', 'name' => 'data[Address][street]',
  6552. 'id' => 'AddressStreet'
  6553. ),
  6554. '/div'
  6555. );
  6556. $this->assertTags($result, $expected);
  6557. $result = $this->Form->input('Address.non_existing_field_in_model');
  6558. $expected = array(
  6559. 'div' => array('class' => 'input text'),
  6560. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  6561. 'Non Existing Field In Model',
  6562. '/label',
  6563. 'input' => array(
  6564. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  6565. 'id' => 'AddressNonExistingFieldInModel'
  6566. ),
  6567. '/div'
  6568. );
  6569. $this->assertTags($result, $expected);
  6570. $result = $this->Form->input('name', array('div' => false));
  6571. $expected = array(
  6572. 'label' => array('for' => 'ContactName'),
  6573. 'Name',
  6574. '/label',
  6575. 'input' => array(
  6576. 'type' => 'text', 'name' => 'data[Contact][name]',
  6577. 'id' => 'ContactName', 'maxlength' => '255'
  6578. )
  6579. );
  6580. $this->assertTags($result, $expected);
  6581. extract($this->dateRegex);
  6582. $now = strtotime('now');
  6583. $result = $this->Form->input('Contact.published', array('div' => false));
  6584. $expected = array(
  6585. 'label' => array('for' => 'ContactPublishedMonth'),
  6586. 'Published',
  6587. '/label',
  6588. array('select' => array(
  6589. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  6590. )),
  6591. $monthsRegex,
  6592. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6593. date('F', $now),
  6594. '/option',
  6595. '*/select',
  6596. '-',
  6597. array('select' => array(
  6598. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  6599. )),
  6600. $daysRegex,
  6601. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6602. date('j', $now),
  6603. '/option',
  6604. '*/select',
  6605. '-',
  6606. array('select' => array(
  6607. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  6608. )),
  6609. $yearsRegex,
  6610. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6611. date('Y', $now),
  6612. '*/select'
  6613. );
  6614. $this->assertTags($result, $expected);
  6615. $result = $this->Form->input('Contact.updated', array('div' => false));
  6616. $expected = array(
  6617. 'label' => array('for' => 'ContactUpdatedMonth'),
  6618. 'Updated',
  6619. '/label',
  6620. array('select' => array(
  6621. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  6622. )),
  6623. $monthsRegex,
  6624. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6625. date('F', $now),
  6626. '/option',
  6627. '*/select',
  6628. '-',
  6629. array('select' => array(
  6630. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  6631. )),
  6632. $daysRegex,
  6633. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6634. date('j', $now),
  6635. '/option',
  6636. '*/select',
  6637. '-',
  6638. array('select' => array(
  6639. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  6640. )),
  6641. $yearsRegex,
  6642. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6643. date('Y', $now),
  6644. '*/select'
  6645. );
  6646. $this->assertTags($result, $expected);
  6647. $result = $this->Form->input('UserForm.stuff');
  6648. $expected = array(
  6649. 'div' => array('class' => 'input text'),
  6650. 'label' => array('for' => 'UserFormStuff'),
  6651. 'Stuff',
  6652. '/label',
  6653. 'input' => array(
  6654. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  6655. 'id' => 'UserFormStuff', 'maxlength' => 10
  6656. ),
  6657. '/div'
  6658. );
  6659. $this->assertTags($result, $expected);
  6660. }
  6661. /**
  6662. * testForMagicInputNonExistingNorValidated method
  6663. *
  6664. * @return void
  6665. */
  6666. public function testForMagicInputNonExistingNorValidated() {
  6667. $encoding = strtolower(Configure::read('App.encoding'));
  6668. $result = $this->Form->create('Contact');
  6669. $expected = array(
  6670. 'form' => array(
  6671. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6672. 'accept-charset' => $encoding
  6673. ),
  6674. 'div' => array('style' => 'display:none;'),
  6675. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6676. '/div'
  6677. );
  6678. $this->assertTags($result, $expected);
  6679. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6680. $expected = array(
  6681. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6682. 'Non Existing Nor Validated',
  6683. '/label',
  6684. 'input' => array(
  6685. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6686. 'id' => 'ContactNonExistingNorValidated'
  6687. )
  6688. );
  6689. $this->assertTags($result, $expected);
  6690. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6691. 'div' => false, 'value' => 'my value'
  6692. ));
  6693. $expected = array(
  6694. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6695. 'Non Existing Nor Validated',
  6696. '/label',
  6697. 'input' => array(
  6698. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6699. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6700. )
  6701. );
  6702. $this->assertTags($result, $expected);
  6703. $this->Form->request->data = array(
  6704. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6705. ));
  6706. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6707. $expected = array(
  6708. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6709. 'Non Existing Nor Validated',
  6710. '/label',
  6711. 'input' => array(
  6712. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6713. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6714. )
  6715. );
  6716. $this->assertTags($result, $expected);
  6717. }
  6718. /**
  6719. * testFormMagicInputLabel method
  6720. *
  6721. * @return void
  6722. */
  6723. public function testFormMagicInputLabel() {
  6724. $encoding = strtolower(Configure::read('App.encoding'));
  6725. $result = $this->Form->create('Contact');
  6726. $expected = array(
  6727. 'form' => array(
  6728. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6729. 'accept-charset' => $encoding
  6730. ),
  6731. 'div' => array('style' => 'display:none;'),
  6732. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6733. '/div'
  6734. );
  6735. $this->assertTags($result, $expected);
  6736. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  6737. $this->assertTags($result, array('input' => array(
  6738. 'name' => 'data[Contact][name]', 'type' => 'text',
  6739. 'id' => 'ContactName', 'maxlength' => '255')
  6740. ));
  6741. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  6742. $expected = array(
  6743. 'label' => array('for' => 'ContactName'),
  6744. 'My label',
  6745. '/label',
  6746. 'input' => array(
  6747. 'type' => 'text', 'name' => 'data[Contact][name]',
  6748. 'id' => 'ContactName', 'maxlength' => '255'
  6749. )
  6750. );
  6751. $this->assertTags($result, $expected);
  6752. $result = $this->Form->input('Contact.name', array(
  6753. 'div' => false, 'label' => array('class' => 'mandatory')
  6754. ));
  6755. $expected = array(
  6756. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6757. 'Name',
  6758. '/label',
  6759. 'input' => array(
  6760. 'type' => 'text', 'name' => 'data[Contact][name]',
  6761. 'id' => 'ContactName', 'maxlength' => '255'
  6762. )
  6763. );
  6764. $this->assertTags($result, $expected);
  6765. $result = $this->Form->input('Contact.name', array(
  6766. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  6767. ));
  6768. $expected = array(
  6769. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6770. 'My label',
  6771. '/label',
  6772. 'input' => array(
  6773. 'type' => 'text', 'name' => 'data[Contact][name]',
  6774. 'id' => 'ContactName', 'maxlength' => '255'
  6775. )
  6776. );
  6777. $this->assertTags($result, $expected);
  6778. $result = $this->Form->input('Contact.name', array(
  6779. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  6780. ));
  6781. $expected = array(
  6782. 'label' => array('for' => 'my_id'),
  6783. 'Name',
  6784. '/label',
  6785. 'input' => array(
  6786. 'type' => 'text', 'name' => 'data[Contact][name]',
  6787. 'id' => 'my_id', 'maxlength' => '255'
  6788. )
  6789. );
  6790. $this->assertTags($result, $expected);
  6791. $result = $this->Form->input('1.id');
  6792. $this->assertTags($result, array('input' => array(
  6793. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6794. 'id' => 'Contact1Id'
  6795. )));
  6796. $result = $this->Form->input("1.name");
  6797. $expected = array(
  6798. 'div' => array('class' => 'input text'),
  6799. 'label' => array('for' => 'Contact1Name'),
  6800. 'Name',
  6801. '/label',
  6802. 'input' => array(
  6803. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  6804. 'id' => 'Contact1Name', 'maxlength' => '255'
  6805. ),
  6806. '/div'
  6807. );
  6808. $this->assertTags($result, $expected);
  6809. $result = $this->Form->input('Contact.1.id');
  6810. $this->assertTags($result, array(
  6811. 'input' => array(
  6812. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6813. 'id' => 'Contact1Id'
  6814. )
  6815. ));
  6816. $result = $this->Form->input("Model.1.name");
  6817. $expected = array(
  6818. 'div' => array('class' => 'input text'),
  6819. 'label' => array('for' => 'Model1Name'),
  6820. 'Name',
  6821. '/label',
  6822. 'input' => array(
  6823. 'type' => 'text', 'name' => 'data[Model][1][name]',
  6824. 'id' => 'Model1Name'
  6825. ),
  6826. '/div'
  6827. );
  6828. $this->assertTags($result, $expected);
  6829. }
  6830. /**
  6831. * testFormEnd method
  6832. *
  6833. * @return void
  6834. */
  6835. public function testFormEnd() {
  6836. $this->assertEquals('</form>', $this->Form->end());
  6837. $result = $this->Form->end('');
  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(array('label' => ''));
  6846. $expected = array(
  6847. 'div' => array('class' => 'submit'),
  6848. 'input' => array('type' => 'submit', 'value' => ''),
  6849. '/div',
  6850. '/form'
  6851. );
  6852. $this->assertTags($result, $expected);
  6853. $result = $this->Form->end('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'));
  6862. $expected = array(
  6863. 'div' => array('class' => 'submit'),
  6864. 'input' => array('type' => 'submit', 'value' => 'save'),
  6865. '/div',
  6866. '/form'
  6867. );
  6868. $this->assertTags($result, $expected);
  6869. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  6870. $expected = array(
  6871. 'div' => array('class' => 'submit'),
  6872. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6873. '/div',
  6874. '/form'
  6875. );
  6876. $this->assertTags($result, $expected);
  6877. $result = $this->Form->end(array('name' => 'Whatever'));
  6878. $expected = array(
  6879. 'div' => array('class' => 'submit'),
  6880. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  6881. '/div',
  6882. '/form'
  6883. );
  6884. $this->assertTags($result, $expected);
  6885. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  6886. $expected = array(
  6887. 'div' => array('class' => 'good'),
  6888. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6889. '/div',
  6890. '/form'
  6891. );
  6892. $this->assertTags($result, $expected);
  6893. $result = $this->Form->end(array(
  6894. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  6895. ));
  6896. $expected = array(
  6897. 'div' => array('class' => 'good'),
  6898. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6899. '/div',
  6900. '/form'
  6901. );
  6902. $this->assertTags($result, $expected);
  6903. }
  6904. /**
  6905. * testMultipleFormWithIdFields method
  6906. *
  6907. * @return void
  6908. */
  6909. public function testMultipleFormWithIdFields() {
  6910. $this->Form->create('UserForm');
  6911. $result = $this->Form->input('id');
  6912. $this->assertTags($result, array('input' => array(
  6913. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  6914. )));
  6915. $result = $this->Form->input('ValidateItem.id');
  6916. $this->assertTags($result, array('input' => array(
  6917. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  6918. 'id' => 'ValidateItemId'
  6919. )));
  6920. $result = $this->Form->input('ValidateUser.id');
  6921. $this->assertTags($result, array('input' => array(
  6922. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  6923. 'id' => 'ValidateUserId'
  6924. )));
  6925. }
  6926. /**
  6927. * testDbLessModel method
  6928. *
  6929. * @return void
  6930. */
  6931. public function testDbLessModel() {
  6932. $this->Form->create('TestMail');
  6933. $result = $this->Form->input('name');
  6934. $expected = array(
  6935. 'div' => array('class' => 'input text'),
  6936. 'label' => array('for' => 'TestMailName'),
  6937. 'Name',
  6938. '/label',
  6939. 'input' => array(
  6940. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6941. 'id' => 'TestMailName'
  6942. ),
  6943. '/div'
  6944. );
  6945. $this->assertTags($result, $expected);
  6946. ClassRegistry::init('TestMail');
  6947. $this->Form->create('TestMail');
  6948. $result = $this->Form->input('name');
  6949. $expected = array(
  6950. 'div' => array('class' => 'input text'),
  6951. 'label' => array('for' => 'TestMailName'),
  6952. 'Name',
  6953. '/label',
  6954. 'input' => array(
  6955. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6956. 'id' => 'TestMailName'
  6957. ),
  6958. '/div'
  6959. );
  6960. $this->assertTags($result, $expected);
  6961. }
  6962. /**
  6963. * testBrokenness method
  6964. *
  6965. * @return void
  6966. */
  6967. public function testBrokenness() {
  6968. /*
  6969. * #4 This test has two parents and four children. By default (as of r7117) both
  6970. * parents are show but the first parent is missing a child. This is the inconsistency
  6971. * in the default behaviour - one parent has all children, the other does not - dependent
  6972. * on the data values.
  6973. */
  6974. $result = $this->Form->select('Model.field', array(
  6975. 'Fred' => array(
  6976. 'freds_son_1' => 'Fred',
  6977. 'freds_son_2' => 'Freddie'
  6978. ),
  6979. 'Bert' => array(
  6980. 'berts_son_1' => 'Albert',
  6981. 'berts_son_2' => 'Bertie')
  6982. ),
  6983. array('showParents' => true, 'empty' => false)
  6984. );
  6985. $expected = array(
  6986. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6987. array('optgroup' => array('label' => 'Fred')),
  6988. array('option' => array('value' => 'freds_son_1')),
  6989. 'Fred',
  6990. '/option',
  6991. array('option' => array('value' => 'freds_son_2')),
  6992. 'Freddie',
  6993. '/option',
  6994. '/optgroup',
  6995. array('optgroup' => array('label' => 'Bert')),
  6996. array('option' => array('value' => 'berts_son_1')),
  6997. 'Albert',
  6998. '/option',
  6999. array('option' => array('value' => 'berts_son_2')),
  7000. 'Bertie',
  7001. '/option',
  7002. '/optgroup',
  7003. '/select'
  7004. );
  7005. $this->assertTags($result, $expected);
  7006. /*
  7007. * #2 This is structurally identical to the test above (#1) - only the parent name has
  7008. * changed, so we should expect the same select list data, just with a different name
  7009. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  7010. * This is where data corruption can occur, because when a select value is missing from
  7011. * a list a form will substitute the first value in the list - without the user knowing.
  7012. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  7013. * affect the availability of 3 => 'Three' as a valid option.
  7014. */
  7015. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  7016. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  7017. ));
  7018. $result = $this->Form->select(
  7019. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  7020. );
  7021. $expected = array(
  7022. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7023. array('option' => array('value' => 1)),
  7024. 'One',
  7025. '/option',
  7026. array('option' => array('value' => 2)),
  7027. 'Two',
  7028. '/option',
  7029. array('optgroup' => array('label' => 'Three')),
  7030. array('option' => array('value' => 3)),
  7031. 'Three',
  7032. '/option',
  7033. array('option' => array('value' => 4)),
  7034. 'Four',
  7035. '/option',
  7036. array('option' => array('value' => 5)),
  7037. 'Five',
  7038. '/option',
  7039. '/optgroup',
  7040. '/select'
  7041. );
  7042. $this->assertTags($result, $expected);
  7043. }
  7044. /**
  7045. * Test the generation of fields for a multi record form.
  7046. *
  7047. * @return void
  7048. */
  7049. public function testMultiRecordForm() {
  7050. $this->Form->create('ValidateProfile');
  7051. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  7052. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  7053. $expected = array(
  7054. 'div' => array('class' => 'input textarea'),
  7055. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  7056. 'Name',
  7057. '/label',
  7058. 'textarea' => array(
  7059. 'id' => 'ValidateProfile1ValidateItem2Name',
  7060. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  7061. 'cols' => 30,
  7062. 'rows' => 6
  7063. ),
  7064. 'Value',
  7065. '/textarea',
  7066. '/div'
  7067. );
  7068. $this->assertTags($result, $expected);
  7069. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
  7070. $expected = array(
  7071. 'div' => array('class' => 'input date'),
  7072. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7073. 'Created',
  7074. '/label',
  7075. array('select' => array(
  7076. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  7077. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7078. )
  7079. ),
  7080. array('option' => array('value' => '')), '/option',
  7081. $this->dateRegex['monthsRegex'],
  7082. '/select', '-',
  7083. array('select' => array(
  7084. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  7085. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7086. )
  7087. ),
  7088. array('option' => array('value' => '')), '/option',
  7089. $this->dateRegex['daysRegex'],
  7090. '/select', '-',
  7091. array('select' => array(
  7092. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  7093. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7094. )
  7095. ),
  7096. array('option' => array('value' => '')), '/option',
  7097. $this->dateRegex['yearsRegex'],
  7098. '/select',
  7099. '/div'
  7100. );
  7101. $this->assertTags($result, $expected);
  7102. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7103. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7104. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7105. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7106. $expected = array(
  7107. 'div' => array('class' => 'input select error'),
  7108. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7109. 'Profile',
  7110. '/label',
  7111. 'select' => array(
  7112. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7113. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7114. 'class' => 'form-error'
  7115. ),
  7116. '/select',
  7117. array('div' => array('class' => 'error-message')),
  7118. 'Error',
  7119. '/div',
  7120. '/div'
  7121. );
  7122. $this->assertTags($result, $expected);
  7123. }
  7124. /**
  7125. * test the correct display of multi-record form validation errors.
  7126. *
  7127. * @return void
  7128. */
  7129. public function testMultiRecordFormValidationErrors() {
  7130. $this->Form->create('ValidateProfile');
  7131. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7132. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7133. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7134. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7135. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7136. $result = $this->Form->error('ValidateProfile.2.city');
  7137. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7138. $result = $this->Form->error('2.city');
  7139. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7140. }
  7141. /**
  7142. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7143. *
  7144. * @return void
  7145. */
  7146. public function testInputTemplate() {
  7147. $result = $this->Form->input('Contact.email', array(
  7148. 'type' => 'text', 'format' => array('input')
  7149. ));
  7150. $expected = array(
  7151. 'div' => array('class' => 'input text'),
  7152. 'input' => array(
  7153. 'type' => 'text', 'name' => 'data[Contact][email]',
  7154. 'id' => 'ContactEmail'
  7155. ),
  7156. '/div'
  7157. );
  7158. $this->assertTags($result, $expected);
  7159. $result = $this->Form->input('Contact.email', array(
  7160. 'type' => 'text', 'format' => array('input', 'label'),
  7161. 'label' => '<em>Email (required)</em>'
  7162. ));
  7163. $expected = array(
  7164. 'div' => array('class' => 'input text'),
  7165. array('input' => array(
  7166. 'type' => 'text', 'name' => 'data[Contact][email]',
  7167. 'id' => 'ContactEmail'
  7168. )),
  7169. 'label' => array('for' => 'ContactEmail'),
  7170. 'em' => array(),
  7171. 'Email (required)',
  7172. '/em',
  7173. '/label',
  7174. '/div'
  7175. );
  7176. $this->assertTags($result, $expected);
  7177. $result = $this->Form->input('Contact.email', array(
  7178. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7179. 'between' => '<div>Something in the middle</div>',
  7180. 'after' => '<span>Some text at the end</span>'
  7181. ));
  7182. $expected = array(
  7183. 'div' => array('class' => 'input text'),
  7184. array('input' => array(
  7185. 'type' => 'text', 'name' => 'data[Contact][email]',
  7186. 'id' => 'ContactEmail'
  7187. )),
  7188. array('div' => array()),
  7189. 'Something in the middle',
  7190. '/div',
  7191. 'label' => array('for' => 'ContactEmail'),
  7192. 'Email',
  7193. '/label',
  7194. 'span' => array(),
  7195. 'Some text at the end',
  7196. '/span',
  7197. '/div'
  7198. );
  7199. $this->assertTags($result, $expected);
  7200. $result = $this->Form->input('Contact.method', array(
  7201. 'type' => 'radio',
  7202. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7203. 'between' => 'I am between',
  7204. ));
  7205. $expected = array(
  7206. 'div' => array('class' => 'input radio'),
  7207. 'fieldset' => array(),
  7208. 'legend' => array(),
  7209. 'Method',
  7210. '/legend',
  7211. 'I am between',
  7212. 'input' => array(
  7213. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  7214. 'value' => '', 'id' => 'ContactMethod_'
  7215. ),
  7216. array('input' => array(
  7217. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7218. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7219. )),
  7220. array('label' => array('for' => 'ContactMethodEmail')),
  7221. 'Email',
  7222. '/label',
  7223. array('input' => array(
  7224. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7225. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7226. )),
  7227. array('label' => array('for' => 'ContactMethodPigeon')),
  7228. 'Pigeon',
  7229. '/label',
  7230. '/fieldset',
  7231. '/div',
  7232. );
  7233. $this->assertTags($result, $expected);
  7234. }
  7235. /**
  7236. * test that some html5 inputs + FormHelper::__call() work
  7237. *
  7238. * @return void
  7239. */
  7240. public function testHtml5Inputs() {
  7241. $result = $this->Form->email('User.email');
  7242. $expected = array(
  7243. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  7244. );
  7245. $this->assertTags($result, $expected);
  7246. $result = $this->Form->search('User.query');
  7247. $expected = array(
  7248. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  7249. );
  7250. $this->assertTags($result, $expected);
  7251. $result = $this->Form->search('User.query', array('value' => 'test'));
  7252. $expected = array(
  7253. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7254. );
  7255. $this->assertTags($result, $expected);
  7256. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7257. $expected = array(
  7258. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7259. );
  7260. $this->assertTags($result, $expected);
  7261. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  7262. $expected = array(
  7263. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7264. );
  7265. $this->assertTags($result, $expected);
  7266. }
  7267. /**
  7268. *
  7269. * @expectedException CakeException
  7270. * @return void
  7271. */
  7272. public function testHtml5InputException() {
  7273. $this->Form->email();
  7274. }
  7275. /**
  7276. * Tests that a model can be loaded from the model names passed in the request object
  7277. *
  7278. * @return void
  7279. */
  7280. public function testIntrospectModelFromRequest() {
  7281. $this->loadFixtures('Post');
  7282. App::build(array(
  7283. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  7284. ));
  7285. CakePlugin::load('TestPlugin');
  7286. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  7287. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  7288. $this->Form->create('TestPluginPost');
  7289. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  7290. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  7291. CakePlugin::unload();
  7292. App::build();
  7293. }
  7294. /**
  7295. * Tests that it is possible to set the validation errors directly in the helper for a field
  7296. *
  7297. * @return void
  7298. */
  7299. public function testCustomValidationErrors() {
  7300. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7301. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7302. $this->assertEquals('Badness!', $result);
  7303. }
  7304. /**
  7305. * Tests that the 'on' key validates as expected on create
  7306. *
  7307. * @return void
  7308. */
  7309. public function testRequiredOnCreate() {
  7310. $this->Form->create('Contact');
  7311. $result = $this->Form->input('Contact.imrequiredonupdate');
  7312. $expected = array(
  7313. 'div' => array('class' => 'input text'),
  7314. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7315. 'Imrequiredonupdate',
  7316. '/label',
  7317. 'input' => array(
  7318. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7319. 'id' => 'ContactImrequiredonupdate'
  7320. ),
  7321. '/div'
  7322. );
  7323. $this->assertTags($result, $expected);
  7324. $result = $this->Form->input('Contact.imrequiredoncreate');
  7325. $expected = array(
  7326. 'div' => array('class' => 'input text required'),
  7327. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7328. 'Imrequiredoncreate',
  7329. '/label',
  7330. 'input' => array(
  7331. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7332. 'id' => 'ContactImrequiredoncreate'
  7333. ),
  7334. '/div'
  7335. );
  7336. $this->assertTags($result, $expected);
  7337. $result = $this->Form->input('Contact.imrequiredonboth');
  7338. $expected = array(
  7339. 'div' => array('class' => 'input text required'),
  7340. 'label' => array('for' => 'ContactImrequiredonboth'),
  7341. 'Imrequiredonboth',
  7342. '/label',
  7343. 'input' => array(
  7344. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7345. 'id' => 'ContactImrequiredonboth'
  7346. ),
  7347. '/div'
  7348. );
  7349. $this->assertTags($result, $expected);
  7350. $result = $this->Form->input('Contact.imrequired');
  7351. $expected = array(
  7352. 'div' => array('class' => 'input text required'),
  7353. 'label' => array('for' => 'ContactImrequired'),
  7354. 'Imrequired',
  7355. '/label',
  7356. 'input' => array(
  7357. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7358. 'id' => 'ContactImrequired'
  7359. ),
  7360. '/div'
  7361. );
  7362. $this->assertTags($result, $expected);
  7363. }
  7364. /**
  7365. * Tests that the 'on' key validates as expected on update
  7366. *
  7367. * @return void
  7368. */
  7369. public function testRequiredOnUpdate() {
  7370. $this->Form->request->data['Contact']['id'] = 1;
  7371. $this->Form->create('Contact');
  7372. $result = $this->Form->input('Contact.imrequiredonupdate');
  7373. $expected = array(
  7374. 'div' => array('class' => 'input text required'),
  7375. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7376. 'Imrequiredonupdate',
  7377. '/label',
  7378. 'input' => array(
  7379. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7380. 'id' => 'ContactImrequiredonupdate'
  7381. ),
  7382. '/div'
  7383. );
  7384. $this->assertTags($result, $expected);
  7385. $result = $this->Form->input('Contact.imrequiredoncreate');
  7386. $expected = array(
  7387. 'div' => array('class' => 'input text'),
  7388. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7389. 'Imrequiredoncreate',
  7390. '/label',
  7391. 'input' => array(
  7392. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7393. 'id' => 'ContactImrequiredoncreate'
  7394. ),
  7395. '/div'
  7396. );
  7397. $this->assertTags($result, $expected);
  7398. $result = $this->Form->input('Contact.imrequiredonboth');
  7399. $expected = array(
  7400. 'div' => array('class' => 'input text required'),
  7401. 'label' => array('for' => 'ContactImrequiredonboth'),
  7402. 'Imrequiredonboth',
  7403. '/label',
  7404. 'input' => array(
  7405. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7406. 'id' => 'ContactImrequiredonboth'
  7407. ),
  7408. '/div'
  7409. );
  7410. $this->assertTags($result, $expected);
  7411. $result = $this->Form->input('Contact.imrequired');
  7412. $expected = array(
  7413. 'div' => array('class' => 'input text required'),
  7414. 'label' => array('for' => 'ContactImrequired'),
  7415. 'Imrequired',
  7416. '/label',
  7417. 'input' => array(
  7418. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7419. 'id' => 'ContactImrequired'
  7420. ),
  7421. '/div'
  7422. );
  7423. $this->assertTags($result, $expected);
  7424. }
  7425. /**
  7426. * Test inputDefaults setter and getter
  7427. *
  7428. * @return void
  7429. */
  7430. public function testInputDefaults() {
  7431. $this->Form->create('Contact');
  7432. $this->Form->inputDefaults(array(
  7433. 'label' => false,
  7434. 'div' => array(
  7435. 'style' => 'color: #000;'
  7436. )
  7437. ));
  7438. $result = $this->Form->input('Contact.field1');
  7439. $expected = array(
  7440. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  7441. 'input' => array(
  7442. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7443. 'id' => 'ContactField1'
  7444. ),
  7445. '/div'
  7446. );
  7447. $this->assertTags($result, $expected);
  7448. $this->Form->inputDefaults(array(
  7449. 'div' => false,
  7450. 'label' => 'Label',
  7451. ));
  7452. $result = $this->Form->input('Contact.field1');
  7453. $expected = array(
  7454. 'label' => array('for' => 'ContactField1'),
  7455. 'Label',
  7456. '/label',
  7457. 'input' => array(
  7458. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7459. 'id' => 'ContactField1'
  7460. ),
  7461. );
  7462. $this->assertTags($result, $expected);
  7463. $this->Form->inputDefaults(array(
  7464. 'label' => false,
  7465. ), true);
  7466. $result = $this->Form->input('Contact.field1');
  7467. $expected = array(
  7468. 'input' => array(
  7469. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7470. 'id' => 'ContactField1'
  7471. ),
  7472. );
  7473. $this->assertTags($result, $expected);
  7474. $result = $this->Form->inputDefaults();
  7475. $expected = array(
  7476. 'div' => false,
  7477. 'label' => false,
  7478. );
  7479. $this->assertEqual($result, $expected);
  7480. }
  7481. }