FormHelperTest.php 263 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc.
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc.
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.View.Helper
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('ClassRegistry', 'Utility');
  20. App::uses('Controller', 'Controller');
  21. App::uses('View', 'View');
  22. App::uses('Model', 'Model');
  23. App::uses('Security', 'Utility');
  24. App::uses('CakeRequest', 'Network');
  25. App::uses('HtmlHelper', 'View/Helper');
  26. App::uses('FormHelper', 'View/Helper');
  27. App::uses('Router', 'Routing');
  28. /**
  29. * ContactTestController class
  30. *
  31. * @package cake
  32. * @package Cake.Test.Case.View.Helper
  33. */
  34. class ContactTestController extends Controller {
  35. /**
  36. * name property
  37. *
  38. * @var string 'ContactTest'
  39. */
  40. public $name = 'ContactTest';
  41. /**
  42. * uses property
  43. *
  44. * @var mixed null
  45. */
  46. public $uses = null;
  47. }
  48. /**
  49. * Contact class
  50. *
  51. * @package cake
  52. * @package Cake.Test.Case.View.Helper
  53. */
  54. class Contact extends CakeTestModel {
  55. /**
  56. * primaryKey property
  57. *
  58. * @var string 'id'
  59. */
  60. public $primaryKey = 'id';
  61. /**
  62. * useTable property
  63. *
  64. * @var bool false
  65. */
  66. public $useTable = false;
  67. /**
  68. * name property
  69. *
  70. * @var string 'Contact'
  71. */
  72. public $name = 'Contact';
  73. /**
  74. * Default schema
  75. *
  76. * @var array
  77. */
  78. protected $_schema = array(
  79. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  80. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  81. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  82. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  83. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  84. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  85. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  86. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  87. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  88. );
  89. /**
  90. * validate property
  91. *
  92. * @var array
  93. */
  94. public $validate = array(
  95. 'non_existing' => array(),
  96. 'idontexist' => array(),
  97. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  98. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  99. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  100. 'imrequiredonboth' => array(
  101. 'required' => array('rule' => 'alphaNumeric'),
  102. ),
  103. 'string_required' => 'notEmpty',
  104. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  105. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  106. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  107. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  108. 'imalsonotrequired' => array(
  109. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  110. 'between' => array('rule' => array('between', 5, 30)),
  111. ),
  112. 'imalsonotrequired2' => array(
  113. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  114. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  115. ),
  116. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  117. 'iamrequiredalways' => array(
  118. 'email' => array('rule' => 'email'),
  119. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  120. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  121. ),
  122. );
  123. /**
  124. * schema method
  125. *
  126. * @return void
  127. */
  128. public function setSchema($schema) {
  129. $this->_schema = $schema;
  130. }
  131. /**
  132. * hasAndBelongsToMany property
  133. *
  134. * @var array
  135. */
  136. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  137. /**
  138. * hasAndBelongsToMany property
  139. *
  140. * @var array
  141. */
  142. public $belongsTo = array('User' => array('className' => 'UserForm'));
  143. }
  144. /**
  145. * ContactTagsContact class
  146. *
  147. * @package cake
  148. * @package Cake.Test.Case.View.Helper
  149. */
  150. class ContactTagsContact extends CakeTestModel {
  151. /**
  152. * useTable property
  153. *
  154. * @var bool false
  155. */
  156. public $useTable = false;
  157. /**
  158. * name property
  159. *
  160. * @var string 'Contact'
  161. */
  162. public $name = 'ContactTagsContact';
  163. /**
  164. * Default schema
  165. *
  166. * @var array
  167. */
  168. protected $_schema = array(
  169. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  170. 'contact_tag_id' => array(
  171. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  172. )
  173. );
  174. /**
  175. * schema method
  176. *
  177. * @return void
  178. */
  179. public function setSchema($schema) {
  180. $this->_schema = $schema;
  181. }
  182. }
  183. /**
  184. * ContactNonStandardPk class
  185. *
  186. * @package cake
  187. * @package Cake.Test.Case.View.Helper
  188. */
  189. class ContactNonStandardPk extends Contact {
  190. /**
  191. * primaryKey property
  192. *
  193. * @var string 'pk'
  194. */
  195. public $primaryKey = 'pk';
  196. /**
  197. * name property
  198. *
  199. * @var string 'ContactNonStandardPk'
  200. */
  201. public $name = 'ContactNonStandardPk';
  202. /**
  203. * schema method
  204. *
  205. * @return void
  206. */
  207. public function schema($field = false) {
  208. $this->_schema = parent::schema();
  209. $this->_schema['pk'] = $this->_schema['id'];
  210. unset($this->_schema['id']);
  211. return $this->_schema;
  212. }
  213. }
  214. /**
  215. * ContactTag class
  216. *
  217. * @package cake
  218. * @package Cake.Test.Case.View.Helper
  219. */
  220. class ContactTag extends Model {
  221. /**
  222. * useTable property
  223. *
  224. * @var bool false
  225. */
  226. public $useTable = false;
  227. /**
  228. * schema definition
  229. *
  230. * @var array
  231. */
  232. protected $_schema = array(
  233. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  234. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  235. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  236. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  237. );
  238. }
  239. /**
  240. * UserForm class
  241. *
  242. * @package cake
  243. * @package Cake.Test.Case.View.Helper
  244. */
  245. class UserForm extends CakeTestModel {
  246. /**
  247. * useTable property
  248. *
  249. * @var bool false
  250. */
  251. public $useTable = false;
  252. /**
  253. * primaryKey property
  254. *
  255. * @var string 'id'
  256. */
  257. public $primaryKey = 'id';
  258. /**
  259. * name property
  260. *
  261. * @var string 'UserForm'
  262. */
  263. public $name = 'UserForm';
  264. /**
  265. * hasMany property
  266. *
  267. * @var array
  268. */
  269. public $hasMany = array(
  270. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  271. ));
  272. /**
  273. * schema definition
  274. *
  275. * @var array
  276. */
  277. protected $_schema = array(
  278. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  279. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  280. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  281. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  282. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  283. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  284. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  285. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  286. );
  287. }
  288. /**
  289. * OpenidUrl class
  290. *
  291. * @package cake
  292. * @package Cake.Test.Case.View.Helper
  293. */
  294. class OpenidUrl extends CakeTestModel {
  295. /**
  296. * useTable property
  297. *
  298. * @var bool false
  299. */
  300. public $useTable = false;
  301. /**
  302. * primaryKey property
  303. *
  304. * @var string 'id'
  305. */
  306. public $primaryKey = 'id';
  307. /**
  308. * name property
  309. *
  310. * @var string 'OpenidUrl'
  311. */
  312. public $name = 'OpenidUrl';
  313. /**
  314. * belongsTo property
  315. *
  316. * @var array
  317. */
  318. public $belongsTo = array('UserForm' => array(
  319. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  320. ));
  321. /**
  322. * validate property
  323. *
  324. * @var array
  325. */
  326. public $validate = array('openid_not_registered' => array());
  327. /**
  328. * schema method
  329. *
  330. * @var array
  331. */
  332. protected $_schema = array(
  333. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  334. 'user_form_id' => array(
  335. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  336. ),
  337. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  338. );
  339. /**
  340. * beforeValidate method
  341. *
  342. * @return void
  343. */
  344. public function beforeValidate($options = array()) {
  345. $this->invalidate('openid_not_registered');
  346. return true;
  347. }
  348. }
  349. /**
  350. * ValidateUser class
  351. *
  352. * @package cake
  353. * @package Cake.Test.Case.View.Helper
  354. */
  355. class ValidateUser extends CakeTestModel {
  356. /**
  357. * primaryKey property
  358. *
  359. * @var string 'id'
  360. */
  361. public $primaryKey = 'id';
  362. /**
  363. * useTable property
  364. *
  365. * @var bool false
  366. */
  367. public $useTable = false;
  368. /**
  369. * name property
  370. *
  371. * @var string 'ValidateUser'
  372. */
  373. public $name = 'ValidateUser';
  374. /**
  375. * hasOne property
  376. *
  377. * @var array
  378. */
  379. public $hasOne = array('ValidateProfile' => array(
  380. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  381. ));
  382. /**
  383. * schema method
  384. *
  385. * @var array
  386. */
  387. protected $_schema = array(
  388. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  389. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  390. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  391. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  392. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  393. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  394. );
  395. /**
  396. * beforeValidate method
  397. *
  398. * @return void
  399. */
  400. public function beforeValidate($options = array()) {
  401. $this->invalidate('email');
  402. return false;
  403. }
  404. }
  405. /**
  406. * ValidateProfile class
  407. *
  408. * @package cake
  409. * @package Cake.Test.Case.View.Helper
  410. */
  411. class ValidateProfile extends CakeTestModel {
  412. /**
  413. * primaryKey property
  414. *
  415. * @var string 'id'
  416. */
  417. public $primaryKey = 'id';
  418. /**
  419. * useTable property
  420. *
  421. * @var bool false
  422. */
  423. public $useTable = false;
  424. /**
  425. * schema property
  426. *
  427. * @var array
  428. */
  429. protected $_schema = array(
  430. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  431. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  432. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  433. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  434. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  435. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  436. );
  437. /**
  438. * name property
  439. *
  440. * @var string 'ValidateProfile'
  441. */
  442. public $name = 'ValidateProfile';
  443. /**
  444. * hasOne property
  445. *
  446. * @var array
  447. */
  448. public $hasOne = array('ValidateItem' => array(
  449. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  450. ));
  451. /**
  452. * belongsTo property
  453. *
  454. * @var array
  455. */
  456. public $belongsTo = array('ValidateUser' => array(
  457. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  458. ));
  459. /**
  460. * beforeValidate method
  461. *
  462. * @return void
  463. */
  464. public function beforeValidate($options = array()) {
  465. $this->invalidate('full_name');
  466. $this->invalidate('city');
  467. return false;
  468. }
  469. }
  470. /**
  471. * ValidateItem class
  472. *
  473. * @package cake
  474. * @package Cake.Test.Case.View.Helper
  475. */
  476. class ValidateItem extends CakeTestModel {
  477. /**
  478. * primaryKey property
  479. *
  480. * @var string 'id'
  481. */
  482. public $primaryKey = 'id';
  483. /**
  484. * useTable property
  485. *
  486. * @var bool false
  487. */
  488. public $useTable = false;
  489. /**
  490. * name property
  491. *
  492. * @var string 'ValidateItem'
  493. */
  494. public $name = 'ValidateItem';
  495. /**
  496. * schema property
  497. *
  498. * @var array
  499. */
  500. protected $_schema = array(
  501. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  502. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  503. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  504. 'description' => array(
  505. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  506. ),
  507. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  508. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  509. );
  510. /**
  511. * belongsTo property
  512. *
  513. * @var array
  514. */
  515. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  516. /**
  517. * beforeValidate method
  518. *
  519. * @return void
  520. */
  521. public function beforeValidate($options = array()) {
  522. $this->invalidate('description');
  523. return false;
  524. }
  525. }
  526. /**
  527. * TestMail class
  528. *
  529. * @package cake
  530. * @package Cake.Test.Case.View.Helper
  531. */
  532. class TestMail extends CakeTestModel {
  533. /**
  534. * primaryKey property
  535. *
  536. * @var string 'id'
  537. */
  538. public $primaryKey = 'id';
  539. /**
  540. * useTable property
  541. *
  542. * @var bool false
  543. */
  544. public $useTable = false;
  545. /**
  546. * name property
  547. *
  548. * @var string 'TestMail'
  549. */
  550. public $name = 'TestMail';
  551. }
  552. /**
  553. * FormHelperTest class
  554. *
  555. * @package cake
  556. * @subpackage Cake.Test.Case.View.Helper
  557. * @property FormHelper $Form
  558. */
  559. class FormHelperTest extends CakeTestCase {
  560. /**
  561. * Fixtures to be used
  562. *
  563. * @var array
  564. */
  565. public $fixtures = array('core.post');
  566. /**
  567. * Do not load the fixtures by default
  568. *
  569. * @var boolean
  570. */
  571. public $autoFixtures = false;
  572. /**
  573. * setUp method
  574. *
  575. * @return void
  576. */
  577. public function setUp() {
  578. parent::setUp();
  579. Configure::write('App.base', '');
  580. $this->Controller = new ContactTestController();
  581. $this->View = new View($this->Controller);
  582. $this->Form = new FormHelper($this->View);
  583. $this->Form->Html = new HtmlHelper($this->View);
  584. $this->Form->request = new CakeRequest('contacts/add', false);
  585. $this->Form->request->here = '/contacts/add';
  586. $this->Form->request['action'] = 'add';
  587. $this->Form->request->webroot = '';
  588. $this->Form->request->base = '';
  589. ClassRegistry::addObject('Contact', new Contact());
  590. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  591. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  592. ClassRegistry::addObject('User', new UserForm());
  593. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  594. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  595. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  596. $this->oldSalt = Configure::read('Security.salt');
  597. $this->dateRegex = array(
  598. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  599. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  600. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  601. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  602. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  603. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  604. );
  605. Configure::write('Security.salt', 'foo!');
  606. }
  607. /**
  608. * tearDown method
  609. *
  610. * @return void
  611. */
  612. public function tearDown() {
  613. parent::tearDown();
  614. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  615. Configure::write('Security.salt', $this->oldSalt);
  616. }
  617. /**
  618. * testFormCreateWithSecurity method
  619. *
  620. * Test form->create() with security key.
  621. *
  622. * @return void
  623. */
  624. public function testCreateWithSecurity() {
  625. $this->Form->request['_Token'] = array('key' => 'testKey');
  626. $encoding = strtolower(Configure::read('App.encoding'));
  627. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  628. $expected = array(
  629. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  630. 'div' => array('style' => 'display:none;'),
  631. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  632. array('input' => array(
  633. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  634. )),
  635. '/div'
  636. );
  637. $this->assertTags($result, $expected);
  638. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  639. $expected['form']['id'] = 'MyForm';
  640. $this->assertTags($result, $expected);
  641. }
  642. /**
  643. * test that create() clears the fields property so it starts fresh
  644. *
  645. * @return void
  646. */
  647. public function testCreateClearingFields() {
  648. $this->Form->fields = array('model_id');
  649. $this->Form->create('Contact');
  650. $this->assertEquals(array(), $this->Form->fields);
  651. }
  652. /**
  653. * Tests form hash generation with model-less data
  654. *
  655. * @return void
  656. */
  657. public function testValidateHashNoModel() {
  658. $this->Form->request['_Token'] = array('key' => 'foo');
  659. $result = $this->Form->secure(array('anything'));
  660. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  661. }
  662. /**
  663. * Tests that models with identical field names get resolved properly
  664. *
  665. * @return void
  666. */
  667. public function testDuplicateFieldNameResolution() {
  668. $result = $this->Form->create('ValidateUser');
  669. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  670. $result = $this->Form->input('ValidateItem.name');
  671. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  672. $result = $this->Form->input('ValidateUser.name');
  673. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  674. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  675. $this->assertRegExp('/type="text"/', $result);
  676. $result = $this->Form->input('ValidateItem.name');
  677. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  678. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  679. $this->assertRegExp('/<textarea/', $result);
  680. $result = $this->Form->input('name');
  681. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  682. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  683. $this->assertRegExp('/type="text"/', $result);
  684. }
  685. /**
  686. * Tests that hidden fields generated for checkboxes don't get locked
  687. *
  688. * @return void
  689. */
  690. public function testNoCheckboxLocking() {
  691. $this->Form->request['_Token'] = array('key' => 'foo');
  692. $this->assertSame(array(), $this->Form->fields);
  693. $this->Form->checkbox('check', array('value' => '1'));
  694. $this->assertSame($this->Form->fields, array('check'));
  695. }
  696. /**
  697. * testFormSecurityFields method
  698. *
  699. * Test generation of secure form hash generation.
  700. *
  701. * @return void
  702. */
  703. public function testFormSecurityFields() {
  704. $key = 'testKey';
  705. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  706. $this->Form->request['_Token'] = array('key' => $key);
  707. $result = $this->Form->secure($fields);
  708. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  709. $hash .= ':' . 'Model.valid';
  710. $hash = urlencode($hash);
  711. $expected = array(
  712. 'div' => array('style' => 'display:none;'),
  713. array('input' => array(
  714. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  715. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  716. )),
  717. array('input' => array(
  718. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  719. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  720. )),
  721. '/div'
  722. );
  723. $this->assertTags($result, $expected);
  724. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  725. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  726. $result = $this->Form->secure($fields);
  727. $expected = array(
  728. 'div' => array('class' => 'hidden'),
  729. array('input' => array(
  730. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  731. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  732. )),
  733. array('input' => array(
  734. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  735. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  736. )),
  737. '/div'
  738. );
  739. $this->assertTags($result, $expected);
  740. }
  741. /**
  742. * Tests correct generation of number fields for double and float fields
  743. *
  744. * @return void
  745. */
  746. public function testTextFieldGenerationForFloats() {
  747. $model = ClassRegistry::getObject('Contact');
  748. $model->setSchema(array('foo' => array(
  749. 'type' => 'float',
  750. 'null' => false,
  751. 'default' => null,
  752. 'length' => null
  753. )));
  754. $this->Form->create('Contact');
  755. $result = $this->Form->input('foo');
  756. $expected = array(
  757. 'div' => array('class' => 'input number'),
  758. 'label' => array('for' => 'ContactFoo'),
  759. 'Foo',
  760. '/label',
  761. array('input' => array(
  762. 'type' => 'number',
  763. 'name' => 'data[Contact][foo]',
  764. 'id' => 'ContactFoo',
  765. 'step' => 'any'
  766. )),
  767. '/div'
  768. );
  769. $this->assertTags($result, $expected);
  770. $result = $this->Form->input('foo', array('step' => 0.5));
  771. $expected = array(
  772. 'div' => array('class' => 'input number'),
  773. 'label' => array('for' => 'ContactFoo'),
  774. 'Foo',
  775. '/label',
  776. array('input' => array(
  777. 'type' => 'number',
  778. 'name' => 'data[Contact][foo]',
  779. 'id' => 'ContactFoo',
  780. 'step' => '0.5'
  781. )),
  782. '/div'
  783. );
  784. $this->assertTags($result, $expected);
  785. }
  786. /**
  787. * Tests correct generation of number fields for integer fields
  788. *
  789. * @access public
  790. * @return void
  791. */
  792. public function testTextFieldTypeNumberGenerationForIntegers() {
  793. $model = ClassRegistry::getObject('Contact');
  794. $model->setSchema(array('foo' => array(
  795. 'type' => 'integer',
  796. 'null' => false,
  797. 'default' => null,
  798. 'length' => null
  799. )));
  800. $this->Form->create('Contact');
  801. $result = $this->Form->input('foo');
  802. $expected = array(
  803. 'div' => array('class' => 'input number'),
  804. 'label' => array('for' => 'ContactFoo'),
  805. 'Foo',
  806. '/label',
  807. array('input' => array(
  808. 'type' => 'number', 'name' => 'data[Contact][foo]',
  809. 'id' => 'ContactFoo'
  810. )),
  811. '/div'
  812. );
  813. $this->assertTags($result, $expected);
  814. }
  815. /**
  816. * testFormSecurityMultipleFields method
  817. *
  818. * Test secure() with multiple row form. Ensure hash is correct.
  819. *
  820. * @return void
  821. */
  822. public function testFormSecurityMultipleFields() {
  823. $key = 'testKey';
  824. $fields = array(
  825. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  826. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  827. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  828. );
  829. $this->Form->request['_Token'] = array('key' => $key);
  830. $result = $this->Form->secure($fields);
  831. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  832. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  833. $expected = array(
  834. 'div' => array('style' => 'display:none;'),
  835. array('input' => array(
  836. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  837. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  838. )),
  839. array('input' => array(
  840. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  841. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  842. )),
  843. '/div'
  844. );
  845. $this->assertTags($result, $expected);
  846. }
  847. /**
  848. * testFormSecurityMultipleSubmitButtons
  849. *
  850. * test form submit generation and ensure that _Token is only created on end()
  851. *
  852. * @return void
  853. */
  854. public function testFormSecurityMultipleSubmitButtons() {
  855. $key = 'testKey';
  856. $this->Form->request['_Token'] = array('key' => $key);
  857. $this->Form->create('Addresses');
  858. $this->Form->input('Address.title');
  859. $this->Form->input('Address.first_name');
  860. $result = $this->Form->submit('Save', array('name' => 'save'));
  861. $expected = array(
  862. 'div' => array('class' => 'submit'),
  863. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  864. '/div',
  865. );
  866. $this->assertTags($result, $expected);
  867. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  868. $expected = array(
  869. 'div' => array('class' => 'submit'),
  870. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  871. '/div',
  872. );
  873. $this->assertTags($result, $expected);
  874. $result = $this->Form->end(null);
  875. $expected = array(
  876. 'div' => array('style' => 'display:none;'),
  877. array('input' => array(
  878. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  879. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  880. )),
  881. array('input' => array(
  882. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  883. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  884. )),
  885. '/div'
  886. );
  887. $this->assertTags($result, $expected);
  888. }
  889. /**
  890. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  891. *
  892. * @return void
  893. */
  894. public function testSecurityButtonNestedNamed() {
  895. $key = 'testKey';
  896. $this->Form->request['_Token'] = array('key' => $key);
  897. $this->Form->create('Addresses');
  898. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  899. $result = $this->Form->unlockField();
  900. $this->assertEquals(array('Address.button'), $result);
  901. }
  902. /**
  903. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  904. *
  905. * @return void
  906. */
  907. public function testSecuritySubmitNestedNamed() {
  908. $key = 'testKey';
  909. $this->Form->request['_Token'] = array('key' => $key);
  910. $this->Form->create('Addresses');
  911. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  912. $result = $this->Form->unlockField();
  913. $this->assertEquals(array('Address.button'), $result);
  914. }
  915. /**
  916. * Test that the correct fields are unlocked for image submits with no names.
  917. *
  918. * @return void
  919. */
  920. public function testSecuritySubmitImageNoName() {
  921. $key = 'testKey';
  922. $this->Form->request['_Token'] = array('key' => $key);
  923. $this->Form->create('User');
  924. $result = $this->Form->submit('save.png');
  925. $expected = array(
  926. 'div' => array('class' => 'submit'),
  927. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  928. '/div'
  929. );
  930. $this->assertTags($result, $expected);
  931. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  932. }
  933. /**
  934. * Test that the correct fields are unlocked for image submits with names.
  935. *
  936. * @return void
  937. */
  938. public function testSecuritySubmitImageName() {
  939. $key = 'testKey';
  940. $this->Form->request['_Token'] = array('key' => $key);
  941. $this->Form->create('User');
  942. $result = $this->Form->submit('save.png', array('name' => 'test'));
  943. $expected = array(
  944. 'div' => array('class' => 'submit'),
  945. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  946. '/div'
  947. );
  948. $this->assertTags($result, $expected);
  949. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  950. }
  951. /**
  952. * testFormSecurityMultipleInputFields method
  953. *
  954. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  955. *
  956. * @return void
  957. */
  958. public function testFormSecurityMultipleInputFields() {
  959. $key = 'testKey';
  960. $this->Form->request['_Token'] = array('key' => $key);
  961. $this->Form->create('Addresses');
  962. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  963. $this->Form->input('Addresses.0.title');
  964. $this->Form->input('Addresses.0.first_name');
  965. $this->Form->input('Addresses.0.last_name');
  966. $this->Form->input('Addresses.0.address');
  967. $this->Form->input('Addresses.0.city');
  968. $this->Form->input('Addresses.0.phone');
  969. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  970. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  971. $this->Form->input('Addresses.1.title');
  972. $this->Form->input('Addresses.1.first_name');
  973. $this->Form->input('Addresses.1.last_name');
  974. $this->Form->input('Addresses.1.address');
  975. $this->Form->input('Addresses.1.city');
  976. $this->Form->input('Addresses.1.phone');
  977. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  978. $result = $this->Form->secure($this->Form->fields);
  979. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  980. $expected = array(
  981. 'div' => array('style' => 'display:none;'),
  982. array('input' => array(
  983. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  984. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  985. )),
  986. array('input' => array(
  987. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  988. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  989. )),
  990. '/div'
  991. );
  992. $this->assertTags($result, $expected);
  993. }
  994. /**
  995. * Test form security with Model.field.0 style inputs
  996. *
  997. * @return void
  998. */
  999. public function testFormSecurityArrayFields() {
  1000. $key = 'testKey';
  1001. $this->Form->request->params['_Token']['key'] = $key;
  1002. $this->Form->create('Address');
  1003. $this->Form->input('Address.primary.1');
  1004. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1005. $this->Form->input('Address.secondary.1.0');
  1006. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1007. }
  1008. /**
  1009. * testFormSecurityMultipleInputDisabledFields method
  1010. *
  1011. * test secure form generation with multiple records and disabled fields.
  1012. *
  1013. * @return void
  1014. */
  1015. public function testFormSecurityMultipleInputDisabledFields() {
  1016. $key = 'testKey';
  1017. $this->Form->request->params['_Token'] = array(
  1018. 'key' => $key,
  1019. 'unlockedFields' => array('first_name', 'address')
  1020. );
  1021. $this->Form->create();
  1022. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1023. $this->Form->input('Addresses.0.title');
  1024. $this->Form->input('Addresses.0.first_name');
  1025. $this->Form->input('Addresses.0.last_name');
  1026. $this->Form->input('Addresses.0.address');
  1027. $this->Form->input('Addresses.0.city');
  1028. $this->Form->input('Addresses.0.phone');
  1029. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1030. $this->Form->input('Addresses.1.title');
  1031. $this->Form->input('Addresses.1.first_name');
  1032. $this->Form->input('Addresses.1.last_name');
  1033. $this->Form->input('Addresses.1.address');
  1034. $this->Form->input('Addresses.1.city');
  1035. $this->Form->input('Addresses.1.phone');
  1036. $result = $this->Form->secure($this->Form->fields);
  1037. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  1038. $expected = array(
  1039. 'div' => array('style' => 'display:none;'),
  1040. array('input' => array(
  1041. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1042. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1043. )),
  1044. array('input' => array(
  1045. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1046. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1047. )),
  1048. '/div'
  1049. );
  1050. $this->assertTags($result, $expected);
  1051. }
  1052. /**
  1053. * testFormSecurityInputDisabledFields method
  1054. *
  1055. * Test single record form with disabled fields.
  1056. *
  1057. * @return void
  1058. */
  1059. public function testFormSecurityInputUnlockedFields() {
  1060. $key = 'testKey';
  1061. $this->Form->request['_Token'] = array(
  1062. 'key' => $key,
  1063. 'unlockedFields' => array('first_name', 'address')
  1064. );
  1065. $this->Form->create();
  1066. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1067. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1068. $this->Form->input('Addresses.title');
  1069. $this->Form->input('Addresses.first_name');
  1070. $this->Form->input('Addresses.last_name');
  1071. $this->Form->input('Addresses.address');
  1072. $this->Form->input('Addresses.city');
  1073. $this->Form->input('Addresses.phone');
  1074. $result = $this->Form->fields;
  1075. $expected = array(
  1076. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1077. 'Addresses.city', 'Addresses.phone'
  1078. );
  1079. $this->assertEquals($expected, $result);
  1080. $result = $this->Form->secure($expected);
  1081. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1082. $expected = array(
  1083. 'div' => array('style' => 'display:none;'),
  1084. array('input' => array(
  1085. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1086. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1087. )),
  1088. array('input' => array(
  1089. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1090. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1091. )),
  1092. '/div'
  1093. );
  1094. $this->assertTags($result, $expected);
  1095. }
  1096. /**
  1097. * test securing inputs with custom name attributes.
  1098. *
  1099. * @return void
  1100. */
  1101. public function testFormSecureWithCustomNameAttribute() {
  1102. $this->Form->request->params['_Token']['key'] = 'testKey';
  1103. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1104. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1105. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1106. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1107. }
  1108. /**
  1109. * testFormSecuredInput method
  1110. *
  1111. * Test generation of entire secure form, assertions made on input() output.
  1112. *
  1113. * @return void
  1114. */
  1115. public function testFormSecuredInput() {
  1116. $this->Form->request['_Token'] = array('key' => 'testKey');
  1117. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1118. $encoding = strtolower(Configure::read('App.encoding'));
  1119. $expected = array(
  1120. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1121. 'div' => array('style' => 'display:none;'),
  1122. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1123. array('input' => array(
  1124. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1125. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1126. )),
  1127. '/div'
  1128. );
  1129. $this->assertTags($result, $expected);
  1130. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1131. $expected = array(
  1132. 'div' => array('class' => 'input text'),
  1133. 'label' => array('for' => 'UserFormPublished'),
  1134. 'Published',
  1135. '/label',
  1136. array('input' => array(
  1137. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1138. 'id' => 'UserFormPublished'
  1139. )),
  1140. '/div'
  1141. );
  1142. $this->assertTags($result, $expected);
  1143. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1144. $expected = array(
  1145. 'div' => array('class' => 'input text'),
  1146. 'label' => array('for' => 'UserFormOther'),
  1147. 'Other',
  1148. '/label',
  1149. array('input' => array(
  1150. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1151. 'id' => 'UserFormOther'
  1152. )),
  1153. '/div'
  1154. );
  1155. $this->assertTags($result, $expected);
  1156. $result = $this->Form->hidden('UserForm.stuff');
  1157. $expected = array('input' => array(
  1158. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1159. 'id' => 'UserFormStuff'
  1160. ));
  1161. $this->assertTags($result, $expected);
  1162. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1163. $expected = array('input' => array(
  1164. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1165. 'value' => '0', 'id' => 'UserFormHidden'
  1166. ));
  1167. $this->assertTags($result, $expected);
  1168. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1169. $expected = array(
  1170. 'div' => array('class' => 'input checkbox'),
  1171. array('input' => array(
  1172. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1173. 'value' => '0', 'id' => 'UserFormSomething_'
  1174. )),
  1175. array('input' => array(
  1176. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1177. 'value' => '1', 'id' => 'UserFormSomething'
  1178. )),
  1179. 'label' => array('for' => 'UserFormSomething'),
  1180. 'Something',
  1181. '/label',
  1182. '/div'
  1183. );
  1184. $this->assertTags($result, $expected);
  1185. $result = $this->Form->fields;
  1186. $expected = array(
  1187. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1188. 'UserForm.hidden' => '0', 'UserForm.something'
  1189. );
  1190. $this->assertEquals($expected, $result);
  1191. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1192. $result = $this->Form->secure($this->Form->fields);
  1193. $expected = array(
  1194. 'div' => array('style' => 'display:none;'),
  1195. array('input' => array(
  1196. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1197. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1198. )),
  1199. array('input' => array(
  1200. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1201. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1202. )),
  1203. '/div'
  1204. );
  1205. $this->assertTags($result, $expected);
  1206. }
  1207. /**
  1208. * Tests that the correct keys are added to the field hash index
  1209. *
  1210. * @return void
  1211. */
  1212. public function testFormSecuredFileInput() {
  1213. $this->Form->request['_Token'] = array('key' => 'testKey');
  1214. $this->assertEquals(array(), $this->Form->fields);
  1215. $result = $this->Form->file('Attachment.file');
  1216. $expected = array(
  1217. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1218. 'Attachment.file.error', 'Attachment.file.size'
  1219. );
  1220. $this->assertEquals($expected, $this->Form->fields);
  1221. }
  1222. /**
  1223. * test that multiple selects keys are added to field hash
  1224. *
  1225. * @return void
  1226. */
  1227. public function testFormSecuredMultipleSelect() {
  1228. $this->Form->request['_Token'] = array('key' => 'testKey');
  1229. $this->assertEquals(array(), $this->Form->fields);
  1230. $options = array('1' => 'one', '2' => 'two');
  1231. $this->Form->select('Model.select', $options);
  1232. $expected = array('Model.select');
  1233. $this->assertEquals($expected, $this->Form->fields);
  1234. $this->Form->fields = array();
  1235. $this->Form->select('Model.select', $options, array('multiple' => true));
  1236. $this->assertEquals($expected, $this->Form->fields);
  1237. }
  1238. /**
  1239. * testFormSecuredRadio method
  1240. *
  1241. * @return void
  1242. */
  1243. public function testFormSecuredRadio() {
  1244. $this->Form->request['_Token'] = array('key' => 'testKey');
  1245. $this->assertEquals(array(), $this->Form->fields);
  1246. $options = array('1' => 'option1', '2' => 'option2');
  1247. $this->Form->radio('Test.test', $options);
  1248. $expected = array('Test.test');
  1249. $this->assertEquals($expected, $this->Form->fields);
  1250. }
  1251. /**
  1252. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1253. * hashing.
  1254. *
  1255. * @return void
  1256. */
  1257. public function testFormSecuredAndDisabled() {
  1258. $this->Form->request['_Token'] = array('key' => 'testKey');
  1259. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1260. $this->Form->text('Model.text', array('disabled' => true));
  1261. $this->Form->password('Model.text', array('disabled' => true));
  1262. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1263. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1264. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1265. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1266. $this->Form->month('Model.month', array('disabled' => true));
  1267. $this->Form->day('Model.day', array('disabled' => true));
  1268. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1269. $this->Form->minute('Model.minute', array('disabled' => true));
  1270. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1271. $expected = array(
  1272. 'Model.radio' => ''
  1273. );
  1274. $this->assertEquals($expected, $this->Form->fields);
  1275. }
  1276. /**
  1277. * testDisableSecurityUsingForm method
  1278. *
  1279. * @return void
  1280. */
  1281. public function testDisableSecurityUsingForm() {
  1282. $this->Form->request['_Token'] = array(
  1283. 'key' => 'testKey',
  1284. 'disabledFields' => array()
  1285. );
  1286. $this->Form->create();
  1287. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1288. $this->Form->input('Addresses.title');
  1289. $this->Form->input('Addresses.first_name', array('secure' => false));
  1290. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1291. $this->Form->input('Addresses.zip', array(
  1292. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1293. ));
  1294. $result = $this->Form->fields;
  1295. $expected = array(
  1296. 'Addresses.id' => '123456', 'Addresses.title',
  1297. );
  1298. $this->assertEquals($expected, $result);
  1299. }
  1300. /**
  1301. * test disableField
  1302. *
  1303. * @return void
  1304. */
  1305. public function testUnlockFieldAddsToList() {
  1306. $this->Form->request['_Token'] = array(
  1307. 'key' => 'testKey',
  1308. 'unlockedFields' => array()
  1309. );
  1310. $this->Form->create('Contact');
  1311. $this->Form->unlockField('Contact.name');
  1312. $this->Form->text('Contact.name');
  1313. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1314. $this->assertEquals(array(), $this->Form->fields);
  1315. }
  1316. /**
  1317. * test unlockField removing from fields array.
  1318. *
  1319. * @return void
  1320. */
  1321. public function testUnlockFieldRemovingFromFields() {
  1322. $this->Form->request['_Token'] = array(
  1323. 'key' => 'testKey',
  1324. 'unlockedFields' => array()
  1325. );
  1326. $this->Form->create('Contact');
  1327. $this->Form->hidden('Contact.id', array('value' => 1));
  1328. $this->Form->text('Contact.name');
  1329. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1330. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1331. $this->Form->unlockField('Contact.name');
  1332. $this->Form->unlockField('Contact.id');
  1333. $this->assertEquals(array(), $this->Form->fields);
  1334. }
  1335. /**
  1336. * testTagIsInvalid method
  1337. *
  1338. * @return void
  1339. */
  1340. public function testTagIsInvalid() {
  1341. $Contact = ClassRegistry::getObject('Contact');
  1342. $Contact->validationErrors[0]['email'] = array('Please provide an email');
  1343. $this->Form->setEntity('Contact.0.email');
  1344. $result = $this->Form->tagIsInvalid();
  1345. $expected = array('Please provide an email');
  1346. $this->assertEquals($expected, $result);
  1347. $this->Form->setEntity('Contact.1.email');
  1348. $result = $this->Form->tagIsInvalid();
  1349. $expected = false;
  1350. $this->assertSame($expected, $result);
  1351. $this->Form->setEntity('Contact.0.name');
  1352. $result = $this->Form->tagIsInvalid();
  1353. $expected = false;
  1354. $this->assertSame($expected, $result);
  1355. }
  1356. /**
  1357. * testPasswordValidation method
  1358. *
  1359. * test validation errors on password input.
  1360. *
  1361. * @return void
  1362. */
  1363. public function testPasswordValidation() {
  1364. $Contact = ClassRegistry::getObject('Contact');
  1365. $Contact->validationErrors['password'] = array('Please provide a password');
  1366. $result = $this->Form->input('Contact.password');
  1367. $expected = array(
  1368. 'div' => array('class' => 'input password error'),
  1369. 'label' => array('for' => 'ContactPassword'),
  1370. 'Password',
  1371. '/label',
  1372. 'input' => array(
  1373. 'type' => 'password', 'name' => 'data[Contact][password]',
  1374. 'id' => 'ContactPassword', 'class' => 'form-error'
  1375. ),
  1376. array('div' => array('class' => 'error-message')),
  1377. 'Please provide a password',
  1378. '/div',
  1379. '/div'
  1380. );
  1381. $this->assertTags($result, $expected);
  1382. }
  1383. /**
  1384. * testEmptyErrorValidation method
  1385. *
  1386. * test validation error div when validation message is an empty string
  1387. *
  1388. * @access public
  1389. * @return void
  1390. */
  1391. public function testEmptyErrorValidation() {
  1392. $this->Form->validationErrors['Contact']['password'] = '';
  1393. $result = $this->Form->input('Contact.password');
  1394. $expected = array(
  1395. 'div' => array('class' => 'input password error'),
  1396. 'label' => array('for' => 'ContactPassword'),
  1397. 'Password',
  1398. '/label',
  1399. 'input' => array(
  1400. 'type' => 'password', 'name' => 'data[Contact][password]',
  1401. 'id' => 'ContactPassword', 'class' => 'form-error'
  1402. ),
  1403. array('div' => array('class' => 'error-message')),
  1404. array(),
  1405. '/div',
  1406. '/div'
  1407. );
  1408. $this->assertTags($result, $expected);
  1409. }
  1410. /**
  1411. * testEmptyInputErrorValidation method
  1412. *
  1413. * test validation error div when validation message is overridden by an empty string when calling input()
  1414. *
  1415. * @access public
  1416. * @return void
  1417. */
  1418. public function testEmptyInputErrorValidation() {
  1419. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1420. $result = $this->Form->input('Contact.password', array('error' => ''));
  1421. $expected = array(
  1422. 'div' => array('class' => 'input password error'),
  1423. 'label' => array('for' => 'ContactPassword'),
  1424. 'Password',
  1425. '/label',
  1426. 'input' => array(
  1427. 'type' => 'password', 'name' => 'data[Contact][password]',
  1428. 'id' => 'ContactPassword', 'class' => 'form-error'
  1429. ),
  1430. array('div' => array('class' => 'error-message')),
  1431. array(),
  1432. '/div',
  1433. '/div'
  1434. );
  1435. $this->assertTags($result, $expected);
  1436. }
  1437. /**
  1438. * testFormValidationAssociated method
  1439. *
  1440. * test display of form errors in conjunction with model::validates.
  1441. *
  1442. * @return void
  1443. */
  1444. public function testFormValidationAssociated() {
  1445. $this->UserForm = ClassRegistry::getObject('UserForm');
  1446. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1447. $data = array(
  1448. 'UserForm' => array('name' => 'user'),
  1449. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1450. );
  1451. $result = $this->UserForm->OpenidUrl->create($data);
  1452. $this->assertFalse(empty($result));
  1453. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1454. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1455. $encoding = strtolower(Configure::read('App.encoding'));
  1456. $expected = array(
  1457. 'form' => array(
  1458. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1459. 'accept-charset' => $encoding
  1460. ),
  1461. 'div' => array('style' => 'display:none;'),
  1462. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1463. '/div'
  1464. );
  1465. $this->assertTags($result, $expected);
  1466. $result = $this->Form->error(
  1467. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1468. );
  1469. $this->assertEquals('Error, not registered', $result);
  1470. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1471. }
  1472. /**
  1473. * testFormValidationAssociatedFirstLevel method
  1474. *
  1475. * test form error display with associated model.
  1476. *
  1477. * @return void
  1478. */
  1479. public function testFormValidationAssociatedFirstLevel() {
  1480. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1481. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1482. $data = array(
  1483. 'ValidateUser' => array('name' => 'mariano'),
  1484. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1485. );
  1486. $result = $this->ValidateUser->create($data);
  1487. $this->assertFalse(empty($result));
  1488. $this->assertFalse($this->ValidateUser->validates());
  1489. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1490. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1491. $encoding = strtolower(Configure::read('App.encoding'));
  1492. $expected = array(
  1493. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1494. 'div' => array('style' => 'display:none;'),
  1495. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1496. '/div'
  1497. );
  1498. $this->assertTags($result, $expected);
  1499. $result = $this->Form->error(
  1500. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1501. );
  1502. $this->assertEquals('Invalid email', $result);
  1503. $result = $this->Form->error(
  1504. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1505. );
  1506. $this->assertEquals('Invalid name', $result);
  1507. $result = $this->Form->error(
  1508. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1509. );
  1510. $this->assertEquals('Invalid city', $result);
  1511. unset($this->ValidateUser->ValidateProfile);
  1512. unset($this->ValidateUser);
  1513. }
  1514. /**
  1515. * testFormValidationAssociatedSecondLevel method
  1516. *
  1517. * test form error display with associated model.
  1518. *
  1519. * @return void
  1520. */
  1521. public function testFormValidationAssociatedSecondLevel() {
  1522. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1523. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1524. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1525. $data = array(
  1526. 'ValidateUser' => array('name' => 'mariano'),
  1527. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1528. 'ValidateItem' => array('name' => 'Item')
  1529. );
  1530. $result = $this->ValidateUser->create($data);
  1531. $this->assertFalse(empty($result));
  1532. $this->assertFalse($this->ValidateUser->validates());
  1533. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1534. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1535. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1536. $encoding = strtolower(Configure::read('App.encoding'));
  1537. $expected = array(
  1538. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1539. 'div' => array('style' => 'display:none;'),
  1540. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1541. '/div'
  1542. );
  1543. $this->assertTags($result, $expected);
  1544. $result = $this->Form->error(
  1545. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1546. );
  1547. $this->assertEquals('Invalid email', $result);
  1548. $result = $this->Form->error(
  1549. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1550. );
  1551. $this->assertEquals('Invalid name', $result);
  1552. $result = $this->Form->error(
  1553. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1554. );
  1555. $result = $this->Form->error(
  1556. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1557. );
  1558. $this->assertEquals('Invalid description', $result);
  1559. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1560. unset($this->ValidateUser->ValidateProfile);
  1561. unset($this->ValidateUser);
  1562. }
  1563. /**
  1564. * testFormValidationMultiRecord method
  1565. *
  1566. * test form error display with multiple records.
  1567. *
  1568. * @return void
  1569. */
  1570. public function testFormValidationMultiRecord() {
  1571. $Contact = ClassRegistry::getObject('Contact');
  1572. $Contact->validationErrors[2] = array(
  1573. 'name' => array('This field cannot be left blank')
  1574. );
  1575. $result = $this->Form->input('Contact.2.name');
  1576. $expected = array(
  1577. 'div' => array('class' => 'input text error'),
  1578. 'label' => array('for' => 'Contact2Name'),
  1579. 'Name',
  1580. '/label',
  1581. 'input' => array(
  1582. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1583. 'class' => 'form-error', 'maxlength' => 255
  1584. ),
  1585. array('div' => array('class' => 'error-message')),
  1586. 'This field cannot be left blank',
  1587. '/div',
  1588. '/div'
  1589. );
  1590. $this->assertTags($result, $expected);
  1591. }
  1592. /**
  1593. * testMultipleInputValidation method
  1594. *
  1595. * test multiple record form validation error display.
  1596. *
  1597. * @return void
  1598. */
  1599. public function testMultipleInputValidation() {
  1600. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1601. $Address->validationErrors[0] = array(
  1602. 'title' => array('This field cannot be empty'),
  1603. 'first_name' => array('This field cannot be empty')
  1604. );
  1605. $Address->validationErrors[1] = array(
  1606. 'last_name' => array('You must have a last name')
  1607. );
  1608. $this->Form->create();
  1609. $result = $this->Form->input('Address.0.title');
  1610. $expected = array(
  1611. 'div' => array('class'),
  1612. 'label' => array('for'),
  1613. 'preg:/[^<]+/',
  1614. '/label',
  1615. 'input' => array(
  1616. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1617. ),
  1618. array('div' => array('class' => 'error-message')),
  1619. 'This field cannot be empty',
  1620. '/div',
  1621. '/div'
  1622. );
  1623. $this->assertTags($result, $expected);
  1624. $result = $this->Form->input('Address.0.first_name');
  1625. $expected = array(
  1626. 'div' => array('class'),
  1627. 'label' => array('for'),
  1628. 'preg:/[^<]+/',
  1629. '/label',
  1630. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1631. array('div' => array('class' => 'error-message')),
  1632. 'This field cannot be empty',
  1633. '/div',
  1634. '/div'
  1635. );
  1636. $this->assertTags($result, $expected);
  1637. $result = $this->Form->input('Address.0.last_name');
  1638. $expected = array(
  1639. 'div' => array('class'),
  1640. 'label' => array('for'),
  1641. 'preg:/[^<]+/',
  1642. '/label',
  1643. 'input' => array('type' => 'text', 'name', 'id'),
  1644. '/div'
  1645. );
  1646. $this->assertTags($result, $expected);
  1647. $result = $this->Form->input('Address.1.last_name');
  1648. $expected = array(
  1649. 'div' => array('class'),
  1650. 'label' => array('for'),
  1651. 'preg:/[^<]+/',
  1652. '/label',
  1653. 'input' => array(
  1654. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1655. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1656. ),
  1657. array('div' => array('class' => 'error-message')),
  1658. 'You must have a last name',
  1659. '/div',
  1660. '/div'
  1661. );
  1662. $this->assertTags($result, $expected);
  1663. }
  1664. /**
  1665. * testInput method
  1666. *
  1667. * Test various incarnations of input().
  1668. *
  1669. * @return void
  1670. */
  1671. public function testInput() {
  1672. $result = $this->Form->input('ValidateUser.balance');
  1673. $expected = array(
  1674. 'div' => array('class'),
  1675. 'label' => array('for'),
  1676. 'Balance',
  1677. '/label',
  1678. 'input' => array('name', 'type' => 'number', 'maxlength' => 8, 'id'),
  1679. '/div',
  1680. );
  1681. $this->assertTags($result, $expected);
  1682. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1683. $expected = array(
  1684. 'div' => array('class' => 'input text'),
  1685. 'label' => array('for' => 'custom'),
  1686. 'Email',
  1687. '/label',
  1688. array('input' => array(
  1689. 'type' => 'text', 'name' => 'data[Contact][email]',
  1690. 'id' => 'custom', 'maxlength' => 255
  1691. )),
  1692. '/div'
  1693. );
  1694. $this->assertTags($result, $expected);
  1695. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1696. $expected = array(
  1697. '<div',
  1698. 'label' => array('for' => 'ContactEmail'),
  1699. 'Email',
  1700. '/label',
  1701. array('input' => array(
  1702. 'type' => 'text', 'name' => 'data[Contact][email]',
  1703. 'id' => 'ContactEmail', 'maxlength' => 255
  1704. )),
  1705. '/div'
  1706. );
  1707. $this->assertTags($result, $expected);
  1708. $result = $this->Form->hidden('Contact.idontexist');
  1709. $expected = array('input' => array(
  1710. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1711. 'id' => 'ContactIdontexist'
  1712. ));
  1713. $this->assertTags($result, $expected);
  1714. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1715. $expected = array(
  1716. 'div' => array('class' => 'input text'),
  1717. 'label' => array('for' => 'ContactEmail'),
  1718. 'Email',
  1719. '/label',
  1720. array('input' => array(
  1721. 'type' => 'text', 'name' => 'data[Contact][email]',
  1722. 'id' => 'ContactEmail'
  1723. )),
  1724. '/div'
  1725. );
  1726. $this->assertTags($result, $expected);
  1727. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1728. $expected = array(
  1729. 'div' => array('class' => 'input text'),
  1730. 'label' => array('for' => 'Contact5Email'),
  1731. 'Email',
  1732. '/label',
  1733. array('input' => array(
  1734. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1735. 'id' => 'Contact5Email'
  1736. )),
  1737. '/div'
  1738. );
  1739. $this->assertTags($result, $expected);
  1740. $result = $this->Form->input('Contact.password');
  1741. $expected = array(
  1742. 'div' => array('class' => 'input password'),
  1743. 'label' => array('for' => 'ContactPassword'),
  1744. 'Password',
  1745. '/label',
  1746. array('input' => array(
  1747. 'type' => 'password', 'name' => 'data[Contact][password]',
  1748. 'id' => 'ContactPassword'
  1749. )),
  1750. '/div'
  1751. );
  1752. $this->assertTags($result, $expected);
  1753. $result = $this->Form->input('Contact.email', array(
  1754. 'type' => 'file', 'class' => 'textbox'
  1755. ));
  1756. $expected = array(
  1757. 'div' => array('class' => 'input file'),
  1758. 'label' => array('for' => 'ContactEmail'),
  1759. 'Email',
  1760. '/label',
  1761. array('input' => array(
  1762. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1763. 'id' => 'ContactEmail'
  1764. )),
  1765. '/div'
  1766. );
  1767. $this->assertTags($result, $expected);
  1768. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1769. $result = $this->Form->input('Contact.phone');
  1770. $expected = array(
  1771. 'div' => array('class' => 'input text'),
  1772. 'label' => array('for' => 'ContactPhone'),
  1773. 'Phone',
  1774. '/label',
  1775. array('input' => array(
  1776. 'type' => 'text', 'name' => 'data[Contact][phone]',
  1777. 'value' => 'Hello &amp; World &gt; weird chars',
  1778. 'id' => 'ContactPhone', 'maxlength' => 255
  1779. )),
  1780. '/div'
  1781. );
  1782. $this->assertTags($result, $expected);
  1783. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1784. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1785. $expected = array(
  1786. 'div' => array('class' => 'input text'),
  1787. 'label' => array('for' => 'myId'),
  1788. 'Field',
  1789. '/label',
  1790. 'input' => array(
  1791. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1792. 'value' => 'My value', 'id' => 'myId'
  1793. ),
  1794. '/div'
  1795. );
  1796. $this->assertTags($result, $expected);
  1797. unset($this->Form->request->data);
  1798. $Contact = ClassRegistry::getObject('Contact');
  1799. $Contact->validationErrors['field'] = array('Badness!');
  1800. $result = $this->Form->input('Contact.field');
  1801. $expected = array(
  1802. 'div' => array('class' => 'input text error'),
  1803. 'label' => array('for' => 'ContactField'),
  1804. 'Field',
  1805. '/label',
  1806. 'input' => array(
  1807. 'type' => 'text', 'name' => 'data[Contact][field]',
  1808. 'id' => 'ContactField', 'class' => 'form-error'
  1809. ),
  1810. array('div' => array('class' => 'error-message')),
  1811. 'Badness!',
  1812. '/div',
  1813. '/div'
  1814. );
  1815. $this->assertTags($result, $expected);
  1816. $result = $this->Form->input('Contact.field', array(
  1817. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1818. ));
  1819. $expected = array(
  1820. 'label' => array('for' => 'ContactField'),
  1821. 'Field',
  1822. '/label',
  1823. 'input' => array(
  1824. 'type' => 'text', 'name' => 'data[Contact][field]',
  1825. 'id' => 'ContactField', 'class' => 'form-error'
  1826. ),
  1827. array('span' => array('class' => 'error-message')),
  1828. 'Badness!',
  1829. '/span'
  1830. );
  1831. $this->assertTags($result, $expected);
  1832. $result = $this->Form->input('Contact.field', array(
  1833. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1834. ));
  1835. $expected = array(
  1836. 'div' => array('class' => 'input text error'),
  1837. 'label' => array('for' => 'ContactField'),
  1838. 'Field',
  1839. '/label',
  1840. 'input' => array(
  1841. 'type' => 'text', 'name' => 'data[Contact][field]',
  1842. 'id' => 'ContactField', 'class' => 'form-error'
  1843. ),
  1844. array('div' => array('class' => 'error')),
  1845. 'Badness!',
  1846. '/div'
  1847. );
  1848. $this->assertTags($result, $expected);
  1849. $result = $this->Form->input('Contact.field', array(
  1850. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1851. ));
  1852. $expected = array(
  1853. 'span' => array('class' => 'input text error'),
  1854. 'label' => array('for' => 'ContactField'),
  1855. 'Field',
  1856. '/label',
  1857. 'input' => array(
  1858. 'type' => 'text', 'name' => 'data[Contact][field]',
  1859. 'id' => 'ContactField', 'class' => 'form-error'
  1860. ),
  1861. 'Badness!',
  1862. '/span'
  1863. );
  1864. $this->assertTags($result, $expected);
  1865. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1866. $expected = array(
  1867. 'div' => array('class' => 'input text error'),
  1868. 'label' => array('for' => 'ContactField'),
  1869. 'Field',
  1870. '/label',
  1871. 'input' => array(
  1872. 'type' => 'text', 'name' => 'data[Contact][field]',
  1873. 'id' => 'ContactField', 'class' => 'form-error'
  1874. ),
  1875. 'A message to you, Rudy',
  1876. array('div' => array('class' => 'error-message')),
  1877. 'Badness!',
  1878. '/div',
  1879. '/div'
  1880. );
  1881. $this->assertTags($result, $expected);
  1882. $this->Form->setEntity(null);
  1883. $this->Form->setEntity('Contact.field');
  1884. $result = $this->Form->input('Contact.field', array(
  1885. 'after' => 'A message to you, Rudy', 'error' => false
  1886. ));
  1887. $expected = array(
  1888. 'div' => array('class' => 'input text'),
  1889. 'label' => array('for' => 'ContactField'),
  1890. 'Field',
  1891. '/label',
  1892. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1893. 'A message to you, Rudy',
  1894. '/div'
  1895. );
  1896. $this->assertTags($result, $expected);
  1897. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1898. $expected = array(
  1899. 'div' => array('class' => 'input text'),
  1900. 'label' => array('for' => 'ObjectField'),
  1901. 'Field',
  1902. '/label',
  1903. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1904. 'A message to you, Rudy',
  1905. '/div'
  1906. );
  1907. $this->assertTags($result, $expected);
  1908. $Contact->validationErrors['field'] = array('minLength');
  1909. $result = $this->Form->input('Contact.field', array(
  1910. 'error' => array(
  1911. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1912. 'maxLength' => 'login too large'
  1913. )
  1914. ));
  1915. $expected = array(
  1916. 'div' => array('class' => 'input text error'),
  1917. 'label' => array('for' => 'ContactField'),
  1918. 'Field',
  1919. '/label',
  1920. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1921. array('div' => array('class' => 'error-message')),
  1922. 'Le login doit contenir au moins 2 caractères',
  1923. '/div',
  1924. '/div'
  1925. );
  1926. $this->assertTags($result, $expected);
  1927. $Contact->validationErrors['field'] = array('maxLength');
  1928. $result = $this->Form->input('Contact.field', array(
  1929. 'error' => array(
  1930. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1931. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1932. 'maxLength' => 'login too large',
  1933. )
  1934. ));
  1935. $expected = array(
  1936. 'div' => array('class' => 'input text error'),
  1937. 'label' => array('for' => 'ContactField'),
  1938. 'Field',
  1939. '/label',
  1940. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1941. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1942. 'login too large',
  1943. '/span',
  1944. '/div'
  1945. );
  1946. $this->assertTags($result, $expected);
  1947. }
  1948. /**
  1949. * Test that inputs with 0 can be created.
  1950. *
  1951. * @return void
  1952. */
  1953. public function testInputZero() {
  1954. $this->Form->create('User');
  1955. $result = $this->Form->input('0');
  1956. $expected = array(
  1957. 'div' => array('class' => 'input text'),
  1958. 'label' => array('for' => 'User0'), '/label',
  1959. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  1960. '/div'
  1961. );
  1962. $this->assertTags($result, $expected);
  1963. }
  1964. /**
  1965. * test input() with checkbox creation
  1966. *
  1967. * @return void
  1968. */
  1969. public function testInputCheckbox() {
  1970. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1971. $expected = array(
  1972. 'div' => array('class' => 'input checkbox'),
  1973. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1974. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1975. '/div'
  1976. );
  1977. $this->assertTags($result, $expected);
  1978. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1979. $expected = array(
  1980. 'div' => array('class' => 'input checkbox'),
  1981. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1982. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1983. '/div'
  1984. );
  1985. $this->assertTags($result, $expected);
  1986. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1987. $expected = array(
  1988. 'div' => array('class' => 'input checkbox'),
  1989. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1990. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1991. '/div'
  1992. );
  1993. $this->assertTags($result, $expected);
  1994. }
  1995. /**
  1996. * test form->input() with time types.
  1997. *
  1998. */
  1999. public function testInputTime() {
  2000. extract($this->dateRegex);
  2001. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2002. $result = explode(':', $result);
  2003. $this->assertRegExp('/option value="23"/', $result[0]);
  2004. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2005. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2006. $result = explode(':', $result);
  2007. $this->assertRegExp('/option value="23"/', $result[0]);
  2008. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2009. $result = $this->Form->input('Model.field', array(
  2010. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2011. ));
  2012. $result = explode(':', $result);
  2013. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2014. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2015. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2016. $result = $this->Form->input('Model.field', array(
  2017. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2018. ));
  2019. $result = explode(':', $result);
  2020. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2021. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2022. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2023. $result = $this->Form->input('prueba', array(
  2024. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  2025. 'maxYear' => date('Y') + 1 ,'interval' => 15
  2026. ));
  2027. $result = explode(':', $result);
  2028. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2029. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2030. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2031. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2032. $result = $this->Form->input('Random.start_time', array(
  2033. 'type' => 'time',
  2034. 'selected' => '18:15'
  2035. ));
  2036. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2037. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2038. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2039. }
  2040. /**
  2041. * Test interval + selected near the hour roll over.
  2042. *
  2043. * @return void
  2044. */
  2045. public function testTimeSelectedWithInterval() {
  2046. $result = $this->Form->input('Model.start_time', array(
  2047. 'type' => 'time',
  2048. 'interval' => 15,
  2049. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2050. ));
  2051. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2052. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2053. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2054. $result = $this->Form->input('Model.start_time', array(
  2055. 'type' => 'time',
  2056. 'interval' => 15,
  2057. 'selected' => '2012-10-23 15:57:00'
  2058. ));
  2059. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2060. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2061. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2062. $result = $this->Form->input('Model.start_time', array(
  2063. 'timeFormat' => 24,
  2064. 'type' => 'time',
  2065. 'interval' => 15,
  2066. 'selected' => '15:57'
  2067. ));
  2068. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2069. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2070. $result = $this->Form->input('Model.start_time', array(
  2071. 'timeFormat' => 24,
  2072. 'type' => 'time',
  2073. 'interval' => 15,
  2074. 'selected' => '23:57'
  2075. ));
  2076. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2077. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2078. $result = $this->Form->input('Model.created', array(
  2079. 'timeFormat' => 24,
  2080. 'type' => 'datetime',
  2081. 'interval' => 15,
  2082. 'selected' => '2012-09-30 23:56'
  2083. ));
  2084. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2085. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2086. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2087. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2088. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2089. }
  2090. /**
  2091. * test form->input() with datetime, date and time types
  2092. *
  2093. * @return void
  2094. */
  2095. public function testInputDatetime() {
  2096. extract($this->dateRegex);
  2097. $result = $this->Form->input('prueba', array(
  2098. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2099. 'maxYear' => date('Y') + 1, 'interval' => 15
  2100. ));
  2101. $result = explode(':', $result);
  2102. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2103. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2104. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2105. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2106. //related to ticket #5013
  2107. $result = $this->Form->input('Contact.date', array(
  2108. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2109. ));
  2110. $this->assertRegExp('/class="customClass"/', $result);
  2111. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2112. $result = $this->Form->input('Contact.date', array(
  2113. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2114. ));
  2115. $this->assertRegExp('/id="customIdDay"/', $result);
  2116. $this->assertRegExp('/id="customIdMonth"/', $result);
  2117. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2118. $result = $this->Form->input('Model.field', array(
  2119. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2120. ));
  2121. $this->assertRegExp('/id="customIDDay"/', $result);
  2122. $this->assertRegExp('/id="customIDHour"/', $result);
  2123. $result = explode('</select><select', $result);
  2124. $result = explode(':', $result[1]);
  2125. $this->assertRegExp('/option value="23"/', $result[0]);
  2126. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2127. $result = $this->Form->input('Model.field', array(
  2128. 'type' => 'datetime', 'timeFormat' => 12
  2129. ));
  2130. $result = explode('</select><select', $result);
  2131. $result = explode(':', $result[1]);
  2132. $this->assertRegExp('/option value="12"/', $result[0]);
  2133. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2134. $this->Form->request->data = array('Contact' => array('created' => null));
  2135. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2136. $expected = array(
  2137. 'div' => array('class' => 'input date'),
  2138. 'label' => array('for' => 'ContactCreatedMonth'),
  2139. 'Created',
  2140. '/label',
  2141. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2142. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2143. $monthsRegex,
  2144. '/select', '-',
  2145. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2146. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2147. $daysRegex,
  2148. '/select', '-',
  2149. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2150. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2151. $yearsRegex,
  2152. '/select',
  2153. '/div'
  2154. );
  2155. $this->assertTags($result, $expected);
  2156. $this->Form->request->data = array('Contact' => array('created' => null));
  2157. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2158. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2159. $this->Form->request->data = array('Contact' => array('created' => null));
  2160. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2161. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2162. $result = $this->Form->input('Contact.created', array(
  2163. 'type' => 'date',
  2164. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2165. 'timeFormat' => 'NONE'
  2166. ));
  2167. $this->assertRegExp('/for\="created-month"/', $result);
  2168. }
  2169. /**
  2170. * Test generating checkboxes in a loop.
  2171. *
  2172. * @return void
  2173. */
  2174. public function testInputCheckboxesInLoop() {
  2175. for ($i = 1; $i < 5; $i++) {
  2176. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2177. $expected = array(
  2178. 'div' => array('class' => 'input checkbox'),
  2179. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2180. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2181. 'label' => array('for' => "Contact{$i}Email"),
  2182. 'Email',
  2183. '/label',
  2184. '/div'
  2185. );
  2186. $this->assertTags($result, $expected);
  2187. }
  2188. }
  2189. /**
  2190. * Test generating checkboxes with disabled elements.
  2191. *
  2192. * @return void
  2193. */
  2194. public function testInputCheckboxWithDisabledElements() {
  2195. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2196. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2197. $expected = array(
  2198. array('div' => array('class' => 'input select')),
  2199. array('label' => array('for' => "ContactMultiple")),
  2200. 'Multiple',
  2201. '/label',
  2202. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2203. array('div' => array('class' => 'checkbox')),
  2204. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2205. array('label' => array('for' => "ContactMultiple1")),
  2206. 'One',
  2207. '/label',
  2208. '/div',
  2209. array('div' => array('class' => 'checkbox')),
  2210. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2211. array('label' => array('for' => "ContactMultiple2")),
  2212. 'Two',
  2213. '/label',
  2214. '/div',
  2215. array('div' => array('class' => 'checkbox')),
  2216. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2217. array('label' => array('for' => "ContactMultiple3")),
  2218. 'Three',
  2219. '/label',
  2220. '/div',
  2221. '/div'
  2222. );
  2223. $this->assertTags($result, $expected);
  2224. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2225. $this->assertTags($result, $expected);
  2226. $disabled = array('2', 3);
  2227. $expected = array(
  2228. array('div' => array('class' => 'input select')),
  2229. array('label' => array('for' => "ContactMultiple")),
  2230. 'Multiple',
  2231. '/label',
  2232. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2233. array('div' => array('class' => 'checkbox')),
  2234. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2235. array('label' => array('for' => "ContactMultiple1")),
  2236. 'One',
  2237. '/label',
  2238. '/div',
  2239. array('div' => array('class' => 'checkbox')),
  2240. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2241. array('label' => array('for' => "ContactMultiple2")),
  2242. 'Two',
  2243. '/label',
  2244. '/div',
  2245. array('div' => array('class' => 'checkbox')),
  2246. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2247. array('label' => array('for' => "ContactMultiple3")),
  2248. 'Three',
  2249. '/label',
  2250. '/div',
  2251. '/div'
  2252. );
  2253. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2254. $this->assertTags($result, $expected);
  2255. }
  2256. /**
  2257. * test input name with leading integer, ensure attributes are generated correctly.
  2258. *
  2259. * @return void
  2260. */
  2261. public function testInputWithLeadingInteger() {
  2262. $result = $this->Form->text('0.Node.title');
  2263. $expected = array(
  2264. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2265. );
  2266. $this->assertTags($result, $expected);
  2267. }
  2268. /**
  2269. * test form->input() with select type inputs.
  2270. *
  2271. * @return void
  2272. */
  2273. public function testInputSelectType() {
  2274. $result = $this->Form->input('email', array(
  2275. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2276. );
  2277. $expected = array(
  2278. 'div' => array('class' => 'input select'),
  2279. 'label' => array('for' => 'email'),
  2280. 'Email',
  2281. '/label',
  2282. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2283. array('option' => array('value' => '')),
  2284. '/option',
  2285. array('option' => array('value' => 'è')),
  2286. 'Firést',
  2287. '/option',
  2288. array('option' => array('value' => 'é')),
  2289. 'Secoènd',
  2290. '/option',
  2291. '/select',
  2292. '/div'
  2293. );
  2294. $this->assertTags($result, $expected);
  2295. $result = $this->Form->input('email', array(
  2296. 'options' => array('First', 'Second'), 'empty' => true)
  2297. );
  2298. $expected = array(
  2299. 'div' => array('class' => 'input select'),
  2300. 'label' => array('for' => 'email'),
  2301. 'Email',
  2302. '/label',
  2303. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2304. array('option' => array('value' => '')),
  2305. '/option',
  2306. array('option' => array('value' => '0')),
  2307. 'First',
  2308. '/option',
  2309. array('option' => array('value' => '1')),
  2310. 'Second',
  2311. '/option',
  2312. '/select',
  2313. '/div'
  2314. );
  2315. $this->assertTags($result, $expected);
  2316. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2317. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2318. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2319. $expected = array(
  2320. 'div' => array('class' => 'input select'),
  2321. 'label' => array('for' => 'ModelUserId'),
  2322. 'User',
  2323. '/label',
  2324. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2325. array('option' => array('value' => '')),
  2326. '/option',
  2327. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2328. 'good',
  2329. '/option',
  2330. array('option' => array('value' => 'other')),
  2331. 'bad',
  2332. '/option',
  2333. '/select',
  2334. '/div'
  2335. );
  2336. $this->assertTags($result, $expected);
  2337. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2338. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2339. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2340. $expected = array(
  2341. 'div' => array('class' => 'input select'),
  2342. 'label' => array('for' => 'ThingUserId'),
  2343. 'User',
  2344. '/label',
  2345. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2346. array('option' => array('value' => '')),
  2347. 'Some Empty',
  2348. '/option',
  2349. array('option' => array('value' => 'value')),
  2350. 'good',
  2351. '/option',
  2352. array('option' => array('value' => 'other')),
  2353. 'bad',
  2354. '/option',
  2355. '/select',
  2356. '/div'
  2357. );
  2358. $this->assertTags($result, $expected);
  2359. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2360. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2361. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2362. $expected = array(
  2363. 'div' => array('class' => 'input select'),
  2364. 'label' => array('for' => 'ThingUserId'),
  2365. 'User',
  2366. '/label',
  2367. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2368. array('option' => array('value' => '')),
  2369. 'Some Empty',
  2370. '/option',
  2371. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2372. 'good',
  2373. '/option',
  2374. array('option' => array('value' => 'other')),
  2375. 'bad',
  2376. '/option',
  2377. '/select',
  2378. '/div'
  2379. );
  2380. $this->assertTags($result, $expected);
  2381. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2382. $this->Form->request->data = array('User' => array('User' => array('value')));
  2383. $result = $this->Form->input('User.User', array('empty' => true));
  2384. $expected = array(
  2385. 'div' => array('class' => 'input select'),
  2386. 'label' => array('for' => 'UserUser'),
  2387. 'User',
  2388. '/label',
  2389. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2390. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2391. array('option' => array('value' => '')),
  2392. '/option',
  2393. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2394. 'good',
  2395. '/option',
  2396. array('option' => array('value' => 'other')),
  2397. 'bad',
  2398. '/option',
  2399. '/select',
  2400. '/div'
  2401. );
  2402. $this->assertTags($result, $expected);
  2403. $this->Form->data = array();
  2404. $result = $this->Form->input('Publisher.id', array(
  2405. 'label' => 'Publisher',
  2406. 'type' => 'select',
  2407. 'multiple' => 'checkbox',
  2408. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2409. ));
  2410. $expected = array(
  2411. array('div' => array('class' => 'input select')),
  2412. array('label' => array('for' => 'PublisherId')),
  2413. 'Publisher',
  2414. '/label',
  2415. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2416. array('div' => array('class' => 'checkbox')),
  2417. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2418. array('label' => array('for' => 'PublisherIdValue1')),
  2419. 'Label 1',
  2420. '/label',
  2421. '/div',
  2422. array('div' => array('class' => 'checkbox')),
  2423. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2424. array('label' => array('for' => 'PublisherIdValue2')),
  2425. 'Label 2',
  2426. '/label',
  2427. '/div',
  2428. '/div'
  2429. );
  2430. $this->assertTags($result, $expected);
  2431. }
  2432. /**
  2433. * test that input() and a non standard primary key makes a hidden input by default.
  2434. *
  2435. * @return void
  2436. */
  2437. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2438. $this->Form->create('User');
  2439. $this->Form->fieldset = array(
  2440. 'User' => array(
  2441. 'fields' => array(
  2442. 'model_id' => array('type' => 'integer')
  2443. ),
  2444. 'validates' => array(),
  2445. 'key' => 'model_id'
  2446. )
  2447. );
  2448. $result = $this->Form->input('model_id');
  2449. $expected = array(
  2450. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2451. );
  2452. $this->assertTags($result, $expected);
  2453. }
  2454. /**
  2455. * test that overriding the magic select type widget is possible
  2456. *
  2457. * @return void
  2458. */
  2459. public function testInputOverridingMagicSelectType() {
  2460. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2461. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2462. $expected = array(
  2463. 'div' => array('class' => 'input text'),
  2464. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2465. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2466. '/div'
  2467. );
  2468. $this->assertTags($result, $expected);
  2469. //Check that magic types still work for plural/singular vars
  2470. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2471. $result = $this->Form->input('Model.type');
  2472. $expected = array(
  2473. 'div' => array('class' => 'input select'),
  2474. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2475. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2476. array('option' => array('value' => 'value')), 'good', '/option',
  2477. array('option' => array('value' => 'other')), 'bad', '/option',
  2478. '/select',
  2479. '/div'
  2480. );
  2481. $this->assertTags($result, $expected);
  2482. }
  2483. /**
  2484. * Test that magic input() selects are created for type=number
  2485. *
  2486. * @return void
  2487. */
  2488. public function testInputMagicSelectForTypeNumber() {
  2489. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2490. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2491. $result = $this->Form->input('ValidateUser.balance');
  2492. $expected = array(
  2493. 'div' => array('class' => 'input select'),
  2494. 'label' => array('for' => 'ValidateUserBalance'),
  2495. 'Balance',
  2496. '/label',
  2497. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2498. array('option' => array('value' => '0')),
  2499. 'nothing',
  2500. '/option',
  2501. array('option' => array('value' => '1', 'selected' => 'selected')),
  2502. 'some',
  2503. '/option',
  2504. array('option' => array('value' => '100')),
  2505. 'a lot',
  2506. '/option',
  2507. '/select',
  2508. '/div'
  2509. );
  2510. $this->assertTags($result, $expected);
  2511. }
  2512. /**
  2513. * Test that magic input() selects can easily be converted into radio types without error.
  2514. *
  2515. * @return void
  2516. */
  2517. public function testInputMagicSelectChangeToRadio() {
  2518. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2519. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2520. $this->assertRegExp('/input type="radio"/', $result);
  2521. }
  2522. /**
  2523. * fields with the same name as the model should work.
  2524. *
  2525. * @return void
  2526. */
  2527. public function testInputWithMatchingFieldAndModelName() {
  2528. $this->Form->create('User');
  2529. $this->Form->fieldset = array(
  2530. 'User' => array(
  2531. 'fields' => array(
  2532. 'User' => array('type' => 'text')
  2533. ),
  2534. 'validates' => array(),
  2535. 'key' => 'id'
  2536. )
  2537. );
  2538. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2539. $result = $this->Form->input('User', array('type' => 'text'));
  2540. $expected = array(
  2541. 'div' => array('class' => 'input text'),
  2542. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2543. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2544. '/div'
  2545. );
  2546. $this->assertTags($result, $expected);
  2547. }
  2548. /**
  2549. * testFormInputs method
  2550. *
  2551. * test correct results from form::inputs().
  2552. *
  2553. * @return void
  2554. */
  2555. public function testFormInputs() {
  2556. $this->Form->create('Contact');
  2557. $result = $this->Form->inputs('The Legend');
  2558. $expected = array(
  2559. '<fieldset',
  2560. '<legend',
  2561. 'The Legend',
  2562. '/legend',
  2563. '*/fieldset',
  2564. );
  2565. $this->assertTags($result, $expected);
  2566. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2567. $expected = array(
  2568. 'fieldset' => array('class' => 'classy-stuff'),
  2569. '<legend',
  2570. 'Field of Dreams',
  2571. '/legend',
  2572. '*/fieldset'
  2573. );
  2574. $this->assertTags($result, $expected);
  2575. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2576. $this->assertTags($result, $expected);
  2577. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2578. $this->assertTags($result, $expected);
  2579. $this->Form->create('Contact');
  2580. $this->Form->request['prefix'] = 'admin';
  2581. $this->Form->request['action'] = 'admin_edit';
  2582. $result = $this->Form->inputs();
  2583. $expected = array(
  2584. '<fieldset',
  2585. '<legend',
  2586. 'Edit Contact',
  2587. '/legend',
  2588. '*/fieldset',
  2589. );
  2590. $this->assertTags($result, $expected);
  2591. $this->Form->create('Contact');
  2592. $result = $this->Form->inputs(false);
  2593. $expected = array(
  2594. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2595. array('div' => array('class' => 'input text')),
  2596. '*/div',
  2597. array('div' => array('class' => 'input text')),
  2598. '*/div',
  2599. array('div' => array('class' => 'input text')),
  2600. '*/div',
  2601. array('div' => array('class' => 'input password')),
  2602. '*/div',
  2603. array('div' => array('class' => 'input date')),
  2604. '*/div',
  2605. array('div' => array('class' => 'input date')),
  2606. '*/div',
  2607. array('div' => array('class' => 'input datetime')),
  2608. '*/div',
  2609. array('div' => array('class' => 'input number')),
  2610. '*/div',
  2611. array('div' => array('class' => 'input select')),
  2612. '*/div',
  2613. );
  2614. $this->assertTags($result, $expected);
  2615. $this->Form->create('Contact');
  2616. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2617. $expected = array(
  2618. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2619. array('div' => array('class' => 'input text')),
  2620. '*/div',
  2621. array('div' => array('class' => 'input text')),
  2622. '*/div',
  2623. array('div' => array('class' => 'input text')),
  2624. '*/div',
  2625. array('div' => array('class' => 'input password')),
  2626. '*/div',
  2627. array('div' => array('class' => 'input date')),
  2628. '*/div',
  2629. array('div' => array('class' => 'input date')),
  2630. '*/div',
  2631. array('div' => array('class' => 'input datetime')),
  2632. '*/div',
  2633. array('div' => array('class' => 'input number')),
  2634. '*/div',
  2635. array('div' => array('class' => 'input select')),
  2636. '*/div',
  2637. );
  2638. $this->assertTags($result, $expected);
  2639. $this->Form->create('Contact');
  2640. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2641. $this->assertTags($result, $expected);
  2642. $this->Form->create('Contact');
  2643. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2644. $expected = array(
  2645. 'fieldset' => array(),
  2646. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2647. array('div' => array('class' => 'input text')),
  2648. '*/div',
  2649. array('div' => array('class' => 'input text')),
  2650. '*/div',
  2651. array('div' => array('class' => 'input text')),
  2652. '*/div',
  2653. array('div' => array('class' => 'input password')),
  2654. '*/div',
  2655. array('div' => array('class' => 'input date')),
  2656. '*/div',
  2657. array('div' => array('class' => 'input date')),
  2658. '*/div',
  2659. array('div' => array('class' => 'input datetime')),
  2660. '*/div',
  2661. array('div' => array('class' => 'input number')),
  2662. '*/div',
  2663. array('div' => array('class' => 'input select')),
  2664. '*/div',
  2665. '/fieldset'
  2666. );
  2667. $this->assertTags($result, $expected);
  2668. $this->Form->create('Contact');
  2669. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2670. $expected = array(
  2671. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2672. array('div' => array('class' => 'input text')),
  2673. '*/div',
  2674. array('div' => array('class' => 'input text')),
  2675. '*/div',
  2676. array('div' => array('class' => 'input text')),
  2677. '*/div',
  2678. array('div' => array('class' => 'input password')),
  2679. '*/div',
  2680. array('div' => array('class' => 'input date')),
  2681. '*/div',
  2682. array('div' => array('class' => 'input date')),
  2683. '*/div',
  2684. array('div' => array('class' => 'input datetime')),
  2685. '*/div',
  2686. array('div' => array('class' => 'input number')),
  2687. '*/div',
  2688. array('div' => array('class' => 'input select')),
  2689. '*/div',
  2690. );
  2691. $this->assertTags($result, $expected);
  2692. $this->Form->create('Contact');
  2693. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2694. $this->assertTags($result, $expected);
  2695. $this->Form->create('Contact');
  2696. $result = $this->Form->inputs('Hello');
  2697. $expected = array(
  2698. 'fieldset' => array(),
  2699. 'legend' => array(),
  2700. 'Hello',
  2701. '/legend',
  2702. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2703. array('div' => array('class' => 'input text')),
  2704. '*/div',
  2705. array('div' => array('class' => 'input text')),
  2706. '*/div',
  2707. array('div' => array('class' => 'input text')),
  2708. '*/div',
  2709. array('div' => array('class' => 'input password')),
  2710. '*/div',
  2711. array('div' => array('class' => 'input date')),
  2712. '*/div',
  2713. array('div' => array('class' => 'input date')),
  2714. '*/div',
  2715. array('div' => array('class' => 'input datetime')),
  2716. '*/div',
  2717. array('div' => array('class' => 'input number')),
  2718. '*/div',
  2719. array('div' => array('class' => 'input select')),
  2720. '*/div',
  2721. '/fieldset'
  2722. );
  2723. $this->assertTags($result, $expected);
  2724. $this->Form->create('Contact');
  2725. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2726. $expected = array(
  2727. 'fieldset' => array(),
  2728. 'legend' => array(),
  2729. 'Hello',
  2730. '/legend',
  2731. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2732. array('div' => array('class' => 'input text')),
  2733. '*/div',
  2734. array('div' => array('class' => 'input text')),
  2735. '*/div',
  2736. array('div' => array('class' => 'input text')),
  2737. '*/div',
  2738. array('div' => array('class' => 'input password')),
  2739. '*/div',
  2740. array('div' => array('class' => 'input date')),
  2741. '*/div',
  2742. array('div' => array('class' => 'input date')),
  2743. '*/div',
  2744. array('div' => array('class' => 'input datetime')),
  2745. '*/div',
  2746. array('div' => array('class' => 'input number')),
  2747. '*/div',
  2748. array('div' => array('class' => 'input select')),
  2749. '*/div',
  2750. '/fieldset'
  2751. );
  2752. $this->assertTags($result, $expected);
  2753. $this->Form->create('Contact');
  2754. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2755. $this->assertTags($result, $expected);
  2756. }
  2757. /**
  2758. * testSelectAsCheckbox method
  2759. *
  2760. * test multi-select widget with checkbox formatting.
  2761. *
  2762. * @return void
  2763. */
  2764. public function testSelectAsCheckbox() {
  2765. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2766. $expected = array(
  2767. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2768. array('div' => array('class' => 'checkbox')),
  2769. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2770. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2771. 'first',
  2772. '/label',
  2773. '/div',
  2774. array('div' => array('class' => 'checkbox')),
  2775. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2776. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2777. 'second',
  2778. '/label',
  2779. '/div',
  2780. array('div' => array('class' => 'checkbox')),
  2781. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2782. array('label' => array('for' => 'ModelMultiField2')),
  2783. 'third',
  2784. '/label',
  2785. '/div',
  2786. );
  2787. $this->assertTags($result, $expected);
  2788. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2789. $expected = array(
  2790. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2791. array('div' => array('class' => 'checkbox')),
  2792. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2793. array('label' => array('for' => 'ModelMultiField12')),
  2794. 'half',
  2795. '/label',
  2796. '/div',
  2797. );
  2798. $this->assertTags($result, $expected);
  2799. }
  2800. /**
  2801. * testLabel method
  2802. *
  2803. * test label generation.
  2804. *
  2805. * @return void
  2806. */
  2807. public function testLabel() {
  2808. $this->Form->text('Person.name');
  2809. $result = $this->Form->label();
  2810. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2811. $this->Form->text('Person.name');
  2812. $result = $this->Form->label();
  2813. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2814. $result = $this->Form->label('Person.first_name');
  2815. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2816. $result = $this->Form->label('Person.first_name', 'Your first name');
  2817. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2818. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2819. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2820. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2821. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2822. $result = $this->Form->label('Person.first_name', '');
  2823. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2824. $result = $this->Form->label('Person.2.name', '');
  2825. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2826. }
  2827. /**
  2828. * testTextbox method
  2829. *
  2830. * test textbox element generation
  2831. *
  2832. * @return void
  2833. */
  2834. public function testTextbox() {
  2835. $result = $this->Form->text('Model.field');
  2836. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2837. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2838. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2839. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2840. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2841. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2842. $result = $this->Form->text('Model.text');
  2843. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2844. $Contact = ClassRegistry::getObject('Contact');
  2845. $Contact->validationErrors['text'] = array(true);
  2846. $this->Form->request->data['Contact']['text'] = 'test';
  2847. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2848. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2849. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2850. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2851. $expected = array(
  2852. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2853. );
  2854. $this->assertTags($result, $expected);
  2855. }
  2856. /**
  2857. * testDefaultValue method
  2858. *
  2859. * Test default value setting
  2860. *
  2861. * @return void
  2862. */
  2863. public function testDefaultValue() {
  2864. $this->Form->request->data['Model']['field'] = 'test';
  2865. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2866. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  2867. unset($this->Form->request->data['Model']['field']);
  2868. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2869. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  2870. }
  2871. /**
  2872. * testCheckboxDefaultValue method
  2873. *
  2874. * Test default value setting on checkbox() method
  2875. *
  2876. * @return void
  2877. */
  2878. public function testCheckboxDefaultValue() {
  2879. $this->Form->request->data['Model']['field'] = false;
  2880. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2881. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2882. unset($this->Form->request->data['Model']['field']);
  2883. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2884. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2885. $this->Form->request->data['Model']['field'] = true;
  2886. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2887. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2888. unset($this->Form->request->data['Model']['field']);
  2889. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2890. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2891. }
  2892. /**
  2893. * testError method
  2894. *
  2895. * Test field error generation
  2896. *
  2897. * @return void
  2898. */
  2899. public function testError() {
  2900. $Contact = ClassRegistry::getObject('Contact');
  2901. $Contact->validationErrors['field'] = array(1);
  2902. $result = $this->Form->error('Contact.field');
  2903. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  2904. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2905. $this->assertEquals('Error in field Field', $result);
  2906. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2907. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2908. $this->assertEquals('This field contains invalid input', $result);
  2909. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2910. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  2911. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  2912. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  2913. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  2914. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  2915. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2916. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  2917. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2918. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  2919. $this->assertEquals('<strong>Badness!</strong>', $result);
  2920. $Contact->validationErrors['field'] = array("email");
  2921. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  2922. $expected = array(
  2923. 'div' => array('class' => 'field-error'),
  2924. 'No good!',
  2925. '/div'
  2926. );
  2927. $this->assertTags($result, $expected);
  2928. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  2929. $result = $this->Form->error('Contact.field', array(
  2930. 'notEmpty' => 'Cannot be empty',
  2931. 'email' => 'No good!'
  2932. ));
  2933. $expected = array(
  2934. 'div' => array('class' => 'error-message'),
  2935. 'ul' => array(),
  2936. '<li', 'Cannot be empty', '/li',
  2937. '<li', 'No good!', '/li',
  2938. '<li', 'Something else', '/li',
  2939. '/ul',
  2940. '/div'
  2941. );
  2942. $this->assertTags($result, $expected);
  2943. /** Testing error messages list options **/
  2944. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  2945. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  2946. $expected = array(
  2947. 'div' => array('class' => 'error-message'),
  2948. 'ol' => array(),
  2949. '<li', 'notEmpty', '/li',
  2950. '<li', 'email', '/li',
  2951. '/ol',
  2952. '/div'
  2953. );
  2954. $this->assertTags($result, $expected);
  2955. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  2956. $expected = array(
  2957. 'div' => array('class' => 'error-message'),
  2958. 'ol' => array(),
  2959. '<li', 'notEmpty', '/li',
  2960. '<li', 'email', '/li',
  2961. '/ol',
  2962. '/div'
  2963. );
  2964. $this->assertTags($result, $expected);
  2965. $result = $this->Form->error('Contact.field', null, array(
  2966. 'listOptions' => array(
  2967. 'class' => 'ul-class',
  2968. 'itemOptions' => array(
  2969. 'class' => 'li-class'
  2970. )
  2971. )
  2972. ));
  2973. $expected = array(
  2974. 'div' => array('class' => 'error-message'),
  2975. 'ul' => array('class' => 'ul-class'),
  2976. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  2977. array('li' => array('class' => 'li-class')), 'email', '/li',
  2978. '/ul',
  2979. '/div'
  2980. );
  2981. $this->assertTags($result, $expected);
  2982. }
  2983. /**
  2984. * test error options when using form->input();
  2985. *
  2986. * @return void
  2987. */
  2988. public function testInputErrorEscape() {
  2989. $this->Form->create('ValidateProfile');
  2990. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  2991. $ValidateProfile->validationErrors['city'] = array('required<br>');
  2992. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
  2993. $this->assertRegExp('/required&lt;br&gt;/', $result);
  2994. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => false))));
  2995. $this->assertRegExp('/required<br>/', $result);
  2996. }
  2997. /**
  2998. * testPassword method
  2999. *
  3000. * Test password element generation
  3001. *
  3002. * @return void
  3003. */
  3004. public function testPassword() {
  3005. $Contact = ClassRegistry::getObject('Contact');
  3006. $result = $this->Form->password('Contact.field');
  3007. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  3008. $Contact->validationErrors['passwd'] = 1;
  3009. $this->Form->request->data['Contact']['passwd'] = 'test';
  3010. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3011. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3012. }
  3013. /**
  3014. * testRadio method
  3015. *
  3016. * Test radio element set generation
  3017. *
  3018. * @return void
  3019. */
  3020. public function testRadio() {
  3021. $result = $this->Form->radio('Model.field', array('option A'));
  3022. $expected = array(
  3023. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3024. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3025. 'label' => array('for' => 'ModelField0'),
  3026. 'option A',
  3027. '/label'
  3028. );
  3029. $this->assertTags($result, $expected);
  3030. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3031. $expected = array(
  3032. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3033. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  3034. 'label' => array('for' => 'ModelField12'),
  3035. 'half',
  3036. '/label'
  3037. );
  3038. $this->assertTags($result, $expected);
  3039. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3040. $expected = array(
  3041. 'fieldset' => array(),
  3042. 'legend' => array(),
  3043. 'Field',
  3044. '/legend',
  3045. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3046. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3047. array('label' => array('for' => 'ModelField0')),
  3048. 'option A',
  3049. '/label',
  3050. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3051. array('label' => array('for' => 'ModelField1')),
  3052. 'option B',
  3053. '/label',
  3054. '/fieldset'
  3055. );
  3056. $this->assertTags($result, $expected);
  3057. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3058. $expected = array(
  3059. 'fieldset' => array(),
  3060. 'legend' => array(),
  3061. 'Field',
  3062. '/legend',
  3063. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3064. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3065. array('label' => array('for' => 'ModelField0')),
  3066. 'option A',
  3067. '/label',
  3068. 'br' => array(),
  3069. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3070. array('label' => array('for' => 'ModelField1')),
  3071. 'option B',
  3072. '/label',
  3073. '/fieldset'
  3074. );
  3075. $this->assertTags($result, $expected);
  3076. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3077. $expected = array(
  3078. 'div' => array('class' => 'input radio'),
  3079. 'fieldset' => array(),
  3080. 'legend' => array(),
  3081. 'Legend title',
  3082. '/legend',
  3083. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3084. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3085. array('label' => array('for' => 'NewsletterSubscribe0')),
  3086. 'Unsubscribe',
  3087. '/label',
  3088. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3089. array('label' => array('for' => 'NewsletterSubscribe1')),
  3090. 'Subscribe',
  3091. '/label',
  3092. '/fieldset',
  3093. '/div'
  3094. );
  3095. $this->assertTags($result, $expected);
  3096. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3097. $expected = array(
  3098. 'div' => array('class' => 'input radio'),
  3099. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3100. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3101. array('label' => array('for' => 'NewsletterSubscribe0')),
  3102. 'Unsubscribe',
  3103. '/label',
  3104. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3105. array('label' => array('for' => 'NewsletterSubscribe1')),
  3106. 'Subscribe',
  3107. '/label',
  3108. '/div'
  3109. );
  3110. $this->assertTags($result, $expected);
  3111. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3112. $expected = array(
  3113. 'div' => array('class' => 'input radio'),
  3114. 'fieldset' => array(),
  3115. 'legend' => array(),
  3116. 'Legend title',
  3117. '/legend',
  3118. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3119. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3120. 'Unsubscribe',
  3121. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3122. 'Subscribe',
  3123. '/fieldset',
  3124. '/div'
  3125. );
  3126. $this->assertTags($result, $expected);
  3127. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3128. $expected = array(
  3129. 'div' => array('class' => 'input radio'),
  3130. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3131. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3132. 'Unsubscribe',
  3133. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3134. 'Subscribe',
  3135. '/div'
  3136. );
  3137. $this->assertTags($result, $expected);
  3138. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'value' => '1', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3139. $expected = array(
  3140. 'div' => array('class' => 'input radio'),
  3141. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3142. 'Unsubscribe',
  3143. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1', 'checked' => 'checked')),
  3144. 'Subscribe',
  3145. '/div'
  3146. );
  3147. $this->assertTags($result, $expected);
  3148. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3149. $expected = array(
  3150. 'fieldset' => array(),
  3151. 'legend' => array(),
  3152. 'Gender',
  3153. '/legend',
  3154. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3155. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3156. array('label' => array('for' => 'EmployeeGenderMale')),
  3157. 'Male',
  3158. '/label',
  3159. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3160. array('label' => array('for' => 'EmployeeGenderFemale')),
  3161. 'Female',
  3162. '/label',
  3163. '/fieldset',
  3164. );
  3165. $this->assertTags($result, $expected);
  3166. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3167. $expected = array(
  3168. 'fieldset' => array(),
  3169. 'legend' => array(),
  3170. 'Gender',
  3171. '/legend',
  3172. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3173. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3174. array('label' => array('for' => 'OfficerGenderMale')),
  3175. 'Male',
  3176. '/label',
  3177. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3178. array('label' => array('for' => 'OfficerGenderFemale')),
  3179. 'Female',
  3180. '/label',
  3181. '/fieldset',
  3182. );
  3183. $this->assertTags($result, $expected);
  3184. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3185. $expected = array(
  3186. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3187. array('input' => array('type' => 'radio', 'name' => 'data[Contact][1][imrequired]', 'value' => '0', 'id' => 'Contact1Imrequired0')),
  3188. 'label' => array('for' => 'Contact1Imrequired0'),
  3189. 'option A',
  3190. '/label'
  3191. );
  3192. $this->assertTags($result, $expected);
  3193. $result = $this->Form->radio('Model.1.field', array('option A'));
  3194. $expected = array(
  3195. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3196. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3197. 'label' => array('for' => 'Model1Field0'),
  3198. 'option A',
  3199. '/label'
  3200. );
  3201. $this->assertTags($result, $expected);
  3202. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3203. $expected = array(
  3204. 'fieldset' => array(),
  3205. 'legend' => array(),
  3206. 'Field',
  3207. '/legend',
  3208. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3209. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3210. array('label' => array('for' => 'ModelField0')),
  3211. 'option A',
  3212. '/label',
  3213. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3214. array('label' => array('for' => 'ModelField1')),
  3215. 'option B',
  3216. '/label',
  3217. '/fieldset'
  3218. );
  3219. $this->assertTags($result, $expected);
  3220. $result = $this->Form->radio(
  3221. 'Model.field',
  3222. array('option A', 'option B'),
  3223. array('between' => 'I am between')
  3224. );
  3225. $expected = array(
  3226. 'fieldset' => array(),
  3227. 'legend' => array(),
  3228. 'Field',
  3229. '/legend',
  3230. 'I am between',
  3231. 'input' => array(
  3232. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3233. 'value' => '', 'id' => 'ModelField_'
  3234. ),
  3235. array('input' => array(
  3236. 'type' => 'radio', 'name' => 'data[Model][field]',
  3237. 'value' => '0', 'id' => 'ModelField0'
  3238. )),
  3239. array('label' => array('for' => 'ModelField0')),
  3240. 'option A',
  3241. '/label',
  3242. array('input' => array(
  3243. 'type' => 'radio', 'name' => 'data[Model][field]',
  3244. 'value' => '1', 'id' => 'ModelField1'
  3245. )),
  3246. array('label' => array('for' => 'ModelField1')),
  3247. 'option B',
  3248. '/label',
  3249. '/fieldset'
  3250. );
  3251. $this->assertTags($result, $expected);
  3252. }
  3253. /**
  3254. * Test that radios with a 0 value are selected under the correct conditions.
  3255. *
  3256. * @return void
  3257. */
  3258. public function testRadioOptionWithZeroValue() {
  3259. $expected = array(
  3260. 'fieldset' => array(),
  3261. 'legend' => array(),
  3262. 'Field',
  3263. '/legend',
  3264. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3265. array('label' => array('for' => 'ModelField1')),
  3266. 'Yes',
  3267. '/label',
  3268. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3269. array('label' => array('for' => 'ModelField0')),
  3270. 'No',
  3271. '/label',
  3272. '/fieldset'
  3273. );
  3274. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3275. $this->assertTags($result, $expected);
  3276. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3277. $this->assertTags($result, $expected);
  3278. $expected = array(
  3279. 'fieldset' => array(),
  3280. 'legend' => array(),
  3281. 'Field',
  3282. '/legend',
  3283. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3284. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3285. array('label' => array('for' => 'ModelField1')),
  3286. 'Yes',
  3287. '/label',
  3288. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3289. array('label' => array('for' => 'ModelField0')),
  3290. 'No',
  3291. '/label',
  3292. '/fieldset'
  3293. );
  3294. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3295. $this->assertTags($result, $expected);
  3296. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3297. $this->assertTags($result, $expected);
  3298. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3299. $this->assertTags($result, $expected);
  3300. }
  3301. /**
  3302. * test disabled radio options
  3303. *
  3304. * @return void
  3305. */
  3306. public function testRadioDisabled() {
  3307. $result = $this->Form->radio(
  3308. 'Model.field',
  3309. array('option A', 'option B'),
  3310. array('disabled' => array('option A'), 'value' => '0')
  3311. );
  3312. $expected = array(
  3313. 'fieldset' => array(),
  3314. 'legend' => array(),
  3315. 'Field',
  3316. '/legend',
  3317. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3318. array('label' => array('for' => 'ModelField0')),
  3319. 'option A',
  3320. '/label',
  3321. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3322. array('label' => array('for' => 'ModelField1')),
  3323. 'option B',
  3324. '/label',
  3325. '/fieldset'
  3326. );
  3327. $this->assertTags($result, $expected);
  3328. $result = $this->Form->radio(
  3329. 'Model.field',
  3330. array('option A', 'option B'),
  3331. array('disabled' => true, 'value' => '0')
  3332. );
  3333. $expected = array(
  3334. 'fieldset' => array(),
  3335. 'legend' => array(),
  3336. 'Field',
  3337. '/legend',
  3338. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3339. array('label' => array('for' => 'ModelField0')),
  3340. 'option A',
  3341. '/label',
  3342. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3343. array('label' => array('for' => 'ModelField1')),
  3344. 'option B',
  3345. '/label',
  3346. '/fieldset'
  3347. );
  3348. $this->assertTags($result, $expected);
  3349. $result = $this->Form->radio(
  3350. 'Model.field',
  3351. array('option A', 'option B'),
  3352. array('disabled' => 'disabled', 'value' => '0')
  3353. );
  3354. $expected = array(
  3355. 'fieldset' => array(),
  3356. 'legend' => array(),
  3357. 'Field',
  3358. '/legend',
  3359. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3360. array('label' => array('for' => 'ModelField0')),
  3361. 'option A',
  3362. '/label',
  3363. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3364. array('label' => array('for' => 'ModelField1')),
  3365. 'option B',
  3366. '/label',
  3367. '/fieldset'
  3368. );
  3369. $this->assertTags($result, $expected);
  3370. }
  3371. /**
  3372. * test disabling the hidden input for radio buttons
  3373. *
  3374. * @return void
  3375. */
  3376. public function testRadioHiddenInputDisabling() {
  3377. $result = $this->Form->input('Model.1.field', array(
  3378. 'type' => 'radio',
  3379. 'options' => array('option A'),
  3380. 'hiddenField' => false
  3381. )
  3382. );
  3383. $expected = array(
  3384. 'div' => array('class' => 'input radio'),
  3385. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3386. 'label' => array('for' => 'Model1Field0'),
  3387. 'option A',
  3388. '/label',
  3389. '/div'
  3390. );
  3391. $this->assertTags($result, $expected);
  3392. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3393. $expected = array(
  3394. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3395. 'label' => array('for' => 'Model1Field0'),
  3396. 'option A',
  3397. '/label'
  3398. );
  3399. $this->assertTags($result, $expected);
  3400. }
  3401. /**
  3402. * test adding an empty option for radio buttons
  3403. *
  3404. * @return void
  3405. */
  3406. public function testRadioAddEmptyOption() {
  3407. $result = $this->Form->input('Model.1.field', array(
  3408. 'type' => 'radio',
  3409. 'options' => array('option A'),
  3410. 'empty' => true,
  3411. 'hiddenField' => false
  3412. ));
  3413. $expected = array(
  3414. 'div' => array('class' => 'input radio'),
  3415. 'fieldset' => array(),
  3416. 'legend' => array(),
  3417. 'Field',
  3418. '/legend',
  3419. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3420. array('label' => array('for' => 'Model1Field')),
  3421. __('empty'),
  3422. '/label',
  3423. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3424. array('label' => array('for' => 'Model1Field0')),
  3425. 'option A',
  3426. '/label',
  3427. '/fieldset',
  3428. '/div'
  3429. );
  3430. $this->assertTags($result, $expected);
  3431. $result = $this->Form->input('Model.1.field', array(
  3432. 'type' => 'radio',
  3433. 'options' => array('option A'),
  3434. 'empty' => 'CustomEmptyLabel',
  3435. 'hiddenField' => false
  3436. ));
  3437. $expected = array(
  3438. 'div' => array('class' => 'input radio'),
  3439. 'fieldset' => array(),
  3440. 'legend' => array(),
  3441. 'Field',
  3442. '/legend',
  3443. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3444. array('label' => array('for' => 'Model1Field')),
  3445. 'CustomEmptyLabel',
  3446. '/label',
  3447. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3448. array('label' => array('for' => 'Model1Field0')),
  3449. 'option A',
  3450. '/label',
  3451. '/fieldset',
  3452. '/div'
  3453. );
  3454. $this->assertTags($result, $expected);
  3455. $result = $this->Form->input('Model.1.field', array(
  3456. 'type' => 'radio',
  3457. 'options' => array('option A'),
  3458. 'empty' => false,
  3459. 'hiddenField' => false
  3460. ));
  3461. $this->assertTextNotContains('"Model1Field"', $result);
  3462. }
  3463. /**
  3464. * testSelect method
  3465. *
  3466. * Test select element generation.
  3467. *
  3468. * @return void
  3469. */
  3470. public function testSelect() {
  3471. $result = $this->Form->select('Model.field', array());
  3472. $expected = array(
  3473. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3474. array('option' => array('value' => '')),
  3475. '/option',
  3476. '/select'
  3477. );
  3478. $this->assertTags($result, $expected);
  3479. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3480. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3481. $expected = array(
  3482. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3483. array('option' => array('value' => '')),
  3484. '/option',
  3485. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3486. 'good',
  3487. '/option',
  3488. array('option' => array('value' => 'other')),
  3489. 'bad',
  3490. '/option',
  3491. '/select'
  3492. );
  3493. $this->assertTags($result, $expected);
  3494. $this->Form->request->data = array();
  3495. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3496. $expected = array(
  3497. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3498. array('option' => array('value' => '')),
  3499. '/option',
  3500. array('option' => array('value' => 'value')),
  3501. 'good',
  3502. '/option',
  3503. array('option' => array('value' => 'other')),
  3504. 'bad',
  3505. '/option',
  3506. '/select'
  3507. );
  3508. $this->assertTags($result, $expected);
  3509. $result = $this->Form->select(
  3510. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3511. array('empty' => false)
  3512. );
  3513. $expected = array(
  3514. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3515. array('option' => array('value' => 'first')),
  3516. 'first &quot;html&quot; &lt;chars&gt;',
  3517. '/option',
  3518. array('option' => array('value' => 'second')),
  3519. 'value',
  3520. '/option',
  3521. '/select'
  3522. );
  3523. $this->assertTags($result, $expected);
  3524. $result = $this->Form->select(
  3525. 'Model.field',
  3526. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3527. array('escape' => false, 'empty' => false)
  3528. );
  3529. $expected = array(
  3530. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3531. array('option' => array('value' => 'first')),
  3532. 'first "html" <chars>',
  3533. '/option',
  3534. array('option' => array('value' => 'second')),
  3535. 'value',
  3536. '/option',
  3537. '/select'
  3538. );
  3539. $this->assertTags($result, $expected);
  3540. $options = array(
  3541. array('value' => 'first', 'name' => 'First'),
  3542. array('value' => 'first', 'name' => 'Another First'),
  3543. );
  3544. $result = $this->Form->select(
  3545. 'Model.field',
  3546. $options,
  3547. array('escape' => false, 'empty' => false)
  3548. );
  3549. $expected = array(
  3550. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3551. array('option' => array('value' => 'first')),
  3552. 'First',
  3553. '/option',
  3554. array('option' => array('value' => 'first')),
  3555. 'Another First',
  3556. '/option',
  3557. '/select'
  3558. );
  3559. $this->assertTags($result, $expected);
  3560. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3561. $result = $this->Form->select(
  3562. 'Model.contact_id',
  3563. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3564. array('escape' => false, 'empty' => 'pick something')
  3565. );
  3566. $expected = array(
  3567. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3568. array('option' => array('value' => '')), 'pick something', '/option',
  3569. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3570. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3571. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3572. '/select'
  3573. );
  3574. $this->assertTags($result, $expected);
  3575. $this->Form->request->data['Model']['field'] = 0;
  3576. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3577. $expected = array(
  3578. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3579. array('option' => array('value' => '')), '/option',
  3580. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3581. array('option' => array('value' => '1')), 'Yes', '/option',
  3582. '/select'
  3583. );
  3584. $this->assertTags($result, $expected);
  3585. }
  3586. /**
  3587. * test that select() with optiongroups listens to the escape param.
  3588. *
  3589. * @return void
  3590. */
  3591. public function testSelectOptionGroupEscaping() {
  3592. $options = array(
  3593. '>< Key' => array(
  3594. 1 => 'One',
  3595. 2 => 'Two'
  3596. )
  3597. );
  3598. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3599. $expected = array(
  3600. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3601. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3602. array('option' => array('value' => '1')), 'One', '/option',
  3603. array('option' => array('value' => '2')), 'Two', '/option',
  3604. '/optgroup',
  3605. '/select'
  3606. );
  3607. $this->assertTags($result, $expected);
  3608. $options = array(
  3609. '>< Key' => array(
  3610. 1 => 'One',
  3611. 2 => 'Two'
  3612. )
  3613. );
  3614. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3615. $expected = array(
  3616. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3617. 'optgroup' => array('label' => '>< Key'),
  3618. array('option' => array('value' => '1')), 'One', '/option',
  3619. array('option' => array('value' => '2')), 'Two', '/option',
  3620. '/optgroup',
  3621. '/select'
  3622. );
  3623. $this->assertTags($result, $expected);
  3624. }
  3625. /**
  3626. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3627. *
  3628. * @return void
  3629. */
  3630. public function testSelectWithNullAttributes() {
  3631. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3632. $expected = array(
  3633. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3634. array('option' => array('value' => '0')),
  3635. 'first',
  3636. '/option',
  3637. array('option' => array('value' => '1')),
  3638. 'second',
  3639. '/option',
  3640. '/select'
  3641. );
  3642. $this->assertTags($result, $expected);
  3643. }
  3644. /**
  3645. * testNestedSelect method
  3646. *
  3647. * test select element generation with optgroups
  3648. *
  3649. * @return void
  3650. */
  3651. public function testNestedSelect() {
  3652. $result = $this->Form->select(
  3653. 'Model.field',
  3654. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3655. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3656. )), array('empty' => false)
  3657. );
  3658. $expected = array(
  3659. 'select' => array('name' => 'data[Model][field]',
  3660. 'id' => 'ModelField'),
  3661. array('option' => array('value' => 1)),
  3662. 'One',
  3663. '/option',
  3664. array('option' => array('value' => 2)),
  3665. 'Two',
  3666. '/option',
  3667. array('optgroup' => array('label' => 'Three')),
  3668. array('option' => array('value' => 4)),
  3669. 'Four',
  3670. '/option',
  3671. array('option' => array('value' => 5)),
  3672. 'Five',
  3673. '/option',
  3674. '/optgroup',
  3675. '/select'
  3676. );
  3677. $this->assertTags($result, $expected);
  3678. $result = $this->Form->select(
  3679. 'Model.field',
  3680. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3681. array('showParents' => true, 'empty' => false)
  3682. );
  3683. $expected = array(
  3684. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3685. array('option' => array('value' => 1)),
  3686. 'One',
  3687. '/option',
  3688. array('option' => array('value' => 2)),
  3689. 'Two',
  3690. '/option',
  3691. array('optgroup' => array('label' => 'Three')),
  3692. array('option' => array('value' => 3)),
  3693. 'Three',
  3694. '/option',
  3695. array('option' => array('value' => 4)),
  3696. 'Four',
  3697. '/option',
  3698. '/optgroup',
  3699. '/select'
  3700. );
  3701. $this->assertTags($result, $expected);
  3702. }
  3703. /**
  3704. * testSelectMultiple method
  3705. *
  3706. * test generation of multiple select elements
  3707. *
  3708. * @return void
  3709. */
  3710. public function testSelectMultiple() {
  3711. $options = array('first', 'second', 'third');
  3712. $result = $this->Form->select(
  3713. 'Model.multi_field', $options, array('multiple' => true)
  3714. );
  3715. $expected = array(
  3716. 'input' => array(
  3717. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3718. ),
  3719. 'select' => array(
  3720. 'name' => 'data[Model][multi_field][]',
  3721. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3722. ),
  3723. array('option' => array('value' => '0')),
  3724. 'first',
  3725. '/option',
  3726. array('option' => array('value' => '1')),
  3727. 'second',
  3728. '/option',
  3729. array('option' => array('value' => '2')),
  3730. 'third',
  3731. '/option',
  3732. '/select'
  3733. );
  3734. $this->assertTags($result, $expected);
  3735. $result = $this->Form->select(
  3736. 'Model.multi_field', $options, array('multiple' => 'multiple')
  3737. );
  3738. $expected = array(
  3739. 'input' => array(
  3740. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3741. ),
  3742. 'select' => array(
  3743. 'name' => 'data[Model][multi_field][]',
  3744. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3745. ),
  3746. array('option' => array('value' => '0')),
  3747. 'first',
  3748. '/option',
  3749. array('option' => array('value' => '1')),
  3750. 'second',
  3751. '/option',
  3752. array('option' => array('value' => '2')),
  3753. 'third',
  3754. '/option',
  3755. '/select'
  3756. );
  3757. $this->assertTags($result, $expected);
  3758. $result = $this->Form->select(
  3759. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  3760. );
  3761. $expected = array(
  3762. 'input' => array(
  3763. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3764. ),
  3765. 'select' => array(
  3766. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  3767. 'multiple' => 'multiple'
  3768. ),
  3769. array('option' => array('value' => '0', 'selected' => 'selected')),
  3770. 'first',
  3771. '/option',
  3772. array('option' => array('value' => '1', 'selected' => 'selected')),
  3773. 'second',
  3774. '/option',
  3775. array('option' => array('value' => '2')),
  3776. 'third',
  3777. '/option',
  3778. '/select'
  3779. );
  3780. $this->assertTags($result, $expected);
  3781. $result = $this->Form->select(
  3782. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  3783. );
  3784. $expected = array(
  3785. 'select' => array(
  3786. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  3787. ),
  3788. array('option' => array('value' => '0', 'selected' => 'selected')),
  3789. 'first',
  3790. '/option',
  3791. array('option' => array('value' => '1', 'selected' => 'selected')),
  3792. 'second',
  3793. '/option',
  3794. array('option' => array('value' => '2')),
  3795. 'third',
  3796. '/option',
  3797. '/select'
  3798. );
  3799. $this->assertTags($result, $expected);
  3800. }
  3801. /**
  3802. * test generation of habtm select boxes.
  3803. *
  3804. * @return void
  3805. */
  3806. public function testHabtmSelectBox() {
  3807. $this->View->viewVars['contactTags'] = array(
  3808. 1 => 'blue',
  3809. 2 => 'red',
  3810. 3 => 'green'
  3811. );
  3812. $this->Form->request->data = array(
  3813. 'Contact' => array(),
  3814. 'ContactTag' => array(
  3815. array(
  3816. 'id' => 1,
  3817. 'name' => 'blue'
  3818. ),
  3819. array(
  3820. 'id' => 3,
  3821. 'name' => 'green'
  3822. )
  3823. )
  3824. );
  3825. $this->Form->create('Contact');
  3826. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3827. $expected = array(
  3828. 'input' => array(
  3829. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3830. ),
  3831. 'select' => array(
  3832. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  3833. 'multiple' => 'multiple'
  3834. ),
  3835. array('option' => array('value' => '1', 'selected' => 'selected')),
  3836. 'blue',
  3837. '/option',
  3838. array('option' => array('value' => '2')),
  3839. 'red',
  3840. '/option',
  3841. array('option' => array('value' => '3', 'selected' => 'selected')),
  3842. 'green',
  3843. '/option',
  3844. '/select'
  3845. );
  3846. $this->assertTags($result, $expected);
  3847. }
  3848. /**
  3849. * test generation of multi select elements in checkbox format
  3850. *
  3851. * @return void
  3852. */
  3853. public function testSelectMultipleCheckboxes() {
  3854. $result = $this->Form->select(
  3855. 'Model.multi_field',
  3856. array('first', 'second', 'third'),
  3857. array('multiple' => 'checkbox')
  3858. );
  3859. $expected = array(
  3860. 'input' => array(
  3861. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3862. ),
  3863. array('div' => array('class' => 'checkbox')),
  3864. array('input' => array(
  3865. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3866. 'value' => '0', 'id' => 'ModelMultiField0'
  3867. )),
  3868. array('label' => array('for' => 'ModelMultiField0')),
  3869. 'first',
  3870. '/label',
  3871. '/div',
  3872. array('div' => array('class' => 'checkbox')),
  3873. array('input' => array(
  3874. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3875. 'value' => '1', 'id' => 'ModelMultiField1'
  3876. )),
  3877. array('label' => array('for' => 'ModelMultiField1')),
  3878. 'second',
  3879. '/label',
  3880. '/div',
  3881. array('div' => array('class' => 'checkbox')),
  3882. array('input' => array(
  3883. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3884. 'value' => '2', 'id' => 'ModelMultiField2'
  3885. )),
  3886. array('label' => array('for' => 'ModelMultiField2')),
  3887. 'third',
  3888. '/label',
  3889. '/div'
  3890. );
  3891. $this->assertTags($result, $expected);
  3892. $result = $this->Form->select(
  3893. 'Model.multi_field',
  3894. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3895. array('multiple' => 'checkbox')
  3896. );
  3897. $expected = array(
  3898. 'input' => array(
  3899. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3900. ),
  3901. array('div' => array('class' => 'checkbox')),
  3902. array('input' => array(
  3903. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3904. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  3905. )),
  3906. array('label' => array('for' => 'ModelMultiFieldA')),
  3907. 'first',
  3908. '/label',
  3909. '/div',
  3910. array('div' => array('class' => 'checkbox')),
  3911. array('input' => array(
  3912. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3913. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  3914. )),
  3915. array('label' => array('for' => 'ModelMultiFieldB')),
  3916. 'second',
  3917. '/label',
  3918. '/div',
  3919. array('div' => array('class' => 'checkbox')),
  3920. array('input' => array(
  3921. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3922. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  3923. )),
  3924. array('label' => array('for' => 'ModelMultiFieldC')),
  3925. 'third',
  3926. '/label',
  3927. '/div'
  3928. );
  3929. $this->assertTags($result, $expected);
  3930. $result = $this->Form->select(
  3931. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  3932. );
  3933. $expected = array(
  3934. 'input' => array(
  3935. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3936. ),
  3937. array('div' => array('class' => 'checkbox')),
  3938. array('input' => array(
  3939. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3940. 'value' => '1', 'id' => 'ModelMultiField1'
  3941. )),
  3942. array('label' => array('for' => 'ModelMultiField1')),
  3943. 'first',
  3944. '/label',
  3945. '/div'
  3946. );
  3947. $this->assertTags($result, $expected);
  3948. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3949. $result = $this->Form->select(
  3950. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3951. );
  3952. $expected = array(
  3953. 'input' => array(
  3954. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3955. ),
  3956. array('div' => array('class' => 'checkbox')),
  3957. array('input' => array(
  3958. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3959. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  3960. )),
  3961. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  3962. 'first',
  3963. '/label',
  3964. '/div',
  3965. array('div' => array('class' => 'checkbox')),
  3966. array('input' => array(
  3967. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3968. 'value' => 'Array', 'id' => 'ModelTagsArray'
  3969. )),
  3970. array('label' => array('for' => 'ModelTagsArray')),
  3971. 'Array',
  3972. '/label',
  3973. '/div'
  3974. );
  3975. $this->assertTags($result, $expected);
  3976. }
  3977. /**
  3978. * test multiple checkboxes with div styles.
  3979. *
  3980. * @return void
  3981. */
  3982. public function testSelectMultipleCheckboxDiv() {
  3983. $result = $this->Form->select(
  3984. 'Model.tags',
  3985. array('first', 'second'),
  3986. array('multiple' => 'checkbox', 'class' => 'my-class')
  3987. );
  3988. $expected = array(
  3989. 'input' => array(
  3990. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3991. ),
  3992. array('div' => array('class' => 'my-class')),
  3993. array('input' => array(
  3994. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3995. 'value' => '0', 'id' => 'ModelTags0'
  3996. )),
  3997. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  3998. '/div',
  3999. array('div' => array('class' => 'my-class')),
  4000. array('input' => array(
  4001. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4002. 'value' => '1', 'id' => 'ModelTags1'
  4003. )),
  4004. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4005. '/div'
  4006. );
  4007. $this->assertTags($result, $expected);
  4008. $result = $this->Form->input('Model.tags', array(
  4009. 'options' => array('first', 'second'),
  4010. 'multiple' => 'checkbox',
  4011. 'class' => 'my-class',
  4012. 'div' => false,
  4013. 'label' => false
  4014. ));
  4015. $this->assertTags($result, $expected);
  4016. $Contact = ClassRegistry::getObject('Contact');
  4017. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4018. $result = $this->Form->input('Contact.tags', array(
  4019. 'options' => array('one'),
  4020. 'multiple' => 'checkbox',
  4021. 'label' => false,
  4022. 'div' => false
  4023. ));
  4024. $expected = array(
  4025. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4026. array('div' => array('class' => 'checkbox form-error')),
  4027. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4028. array('label' => array('for' => 'ContactTags0')),
  4029. 'one',
  4030. '/label',
  4031. '/div'
  4032. );
  4033. $this->assertTags($result, $expected);
  4034. $result = $this->Form->input('Contact.tags', array(
  4035. 'options' => array('one'),
  4036. 'multiple' => 'checkbox',
  4037. 'class' => 'mycheckbox',
  4038. 'label' => false,
  4039. 'div' => false
  4040. ));
  4041. $expected = array(
  4042. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4043. array('div' => array('class' => 'mycheckbox form-error')),
  4044. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4045. array('label' => array('for' => 'ContactTags0')),
  4046. 'one',
  4047. '/label',
  4048. '/div'
  4049. );
  4050. $this->assertTags($result, $expected);
  4051. }
  4052. /**
  4053. * Checks the security hash array generated for multiple-input checkbox elements
  4054. *
  4055. * @return void
  4056. */
  4057. public function testSelectMultipleCheckboxSecurity() {
  4058. $this->Form->request['_Token'] = array('key' => 'testKey');
  4059. $this->assertEquals(array(), $this->Form->fields);
  4060. $result = $this->Form->select(
  4061. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4062. array('multiple' => 'checkbox')
  4063. );
  4064. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4065. $result = $this->Form->secure($this->Form->fields);
  4066. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4067. $this->assertRegExp('/"' . $key . '"/', $result);
  4068. }
  4069. /**
  4070. * Multiple select elements should always be secured as they always participate
  4071. * in the POST data.
  4072. *
  4073. * @return void
  4074. */
  4075. public function testSelectMultipleSecureWithNoOptions() {
  4076. $this->Form->request['_Token'] = array('key' => 'testkey');
  4077. $this->assertEquals(array(), $this->Form->fields);
  4078. $result = $this->Form->select(
  4079. 'Model.select',
  4080. array(),
  4081. array('multiple' => true)
  4082. );
  4083. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4084. }
  4085. /**
  4086. * When a select box has no options it should not be added to the fields list
  4087. * as it always fail post validation.
  4088. *
  4089. * @return void
  4090. */
  4091. public function testSelectNoSecureWithNoOptions() {
  4092. $this->Form->request['_Token'] = array('key' => 'testkey');
  4093. $this->assertEquals(array(), $this->Form->fields);
  4094. $this->Form->select(
  4095. 'Model.select',
  4096. array()
  4097. );
  4098. $this->assertEquals(array(), $this->Form->fields);
  4099. $this->Form->select(
  4100. 'Model.select',
  4101. array(),
  4102. array('empty' => true)
  4103. );
  4104. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4105. }
  4106. /**
  4107. * testInputMultipleCheckboxes method
  4108. *
  4109. * test input() resulting in multi select elements being generated.
  4110. *
  4111. * @return void
  4112. */
  4113. public function testInputMultipleCheckboxes() {
  4114. $result = $this->Form->input('Model.multi_field', array(
  4115. 'options' => array('first', 'second', 'third'),
  4116. 'multiple' => 'checkbox'
  4117. ));
  4118. $expected = array(
  4119. array('div' => array('class' => 'input select')),
  4120. array('label' => array('for' => 'ModelMultiField')),
  4121. 'Multi Field',
  4122. '/label',
  4123. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  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. array('div' => array('class' => 'checkbox')),
  4137. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4138. array('label' => array('for' => 'ModelMultiField2')),
  4139. 'third',
  4140. '/label',
  4141. '/div',
  4142. '/div'
  4143. );
  4144. $this->assertTags($result, $expected);
  4145. $result = $this->Form->input('Model.multi_field', array(
  4146. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4147. 'multiple' => 'checkbox'
  4148. ));
  4149. $expected = array(
  4150. array('div' => array('class' => 'input select')),
  4151. array('label' => array('for' => 'ModelMultiField')),
  4152. 'Multi Field',
  4153. '/label',
  4154. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4155. array('div' => array('class' => 'checkbox')),
  4156. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4157. array('label' => array('for' => 'ModelMultiFieldA')),
  4158. 'first',
  4159. '/label',
  4160. '/div',
  4161. array('div' => array('class' => 'checkbox')),
  4162. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4163. array('label' => array('for' => 'ModelMultiFieldB')),
  4164. 'second',
  4165. '/label',
  4166. '/div',
  4167. array('div' => array('class' => 'checkbox')),
  4168. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4169. array('label' => array('for' => 'ModelMultiFieldC')),
  4170. 'third',
  4171. '/label',
  4172. '/div',
  4173. '/div'
  4174. );
  4175. $this->assertTags($result, $expected);
  4176. $result = $this->Form->input('Model.multi_field', array(
  4177. 'options' => array('1' => 'first'),
  4178. 'multiple' => 'checkbox',
  4179. 'label' => false,
  4180. 'div' => false
  4181. ));
  4182. $expected = array(
  4183. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4184. array('div' => array('class' => 'checkbox')),
  4185. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4186. array('label' => array('for' => 'ModelMultiField1')),
  4187. 'first',
  4188. '/label',
  4189. '/div'
  4190. );
  4191. $this->assertTags($result, $expected);
  4192. $result = $this->Form->input('Model.multi_field', array(
  4193. 'options' => array('2' => 'second'),
  4194. 'multiple' => 'checkbox',
  4195. 'label' => false,
  4196. 'div' => false
  4197. ));
  4198. $expected = array(
  4199. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4200. array('div' => array('class' => 'checkbox')),
  4201. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4202. array('label' => array('for' => 'ModelMultiField2')),
  4203. 'second',
  4204. '/label',
  4205. '/div'
  4206. );
  4207. $this->assertTags($result, $expected);
  4208. }
  4209. /**
  4210. * testSelectHiddenFieldOmission method
  4211. *
  4212. * test that select() with 'hiddenField' => false omits the hidden field
  4213. *
  4214. * @return void
  4215. */
  4216. public function testSelectHiddenFieldOmission() {
  4217. $result = $this->Form->select('Model.multi_field',
  4218. array('first', 'second'),
  4219. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4220. );
  4221. $expected = array(
  4222. array('div' => array('class' => 'checkbox')),
  4223. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4224. array('label' => array('for' => 'ModelMultiField0')),
  4225. 'first',
  4226. '/label',
  4227. '/div',
  4228. array('div' => array('class' => 'checkbox')),
  4229. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4230. array('label' => array('for' => 'ModelMultiField1')),
  4231. 'second',
  4232. '/label',
  4233. '/div'
  4234. );
  4235. $this->assertTags($result, $expected);
  4236. $result = $this->Form->input('Model.multi_field', array(
  4237. 'options' => array('first', 'second'),
  4238. 'multiple' => 'checkbox',
  4239. 'hiddenField' => false
  4240. ));
  4241. $expected = array(
  4242. array('div' => array('class' => 'input select')),
  4243. array('label' => array('for' => 'ModelMultiField')),
  4244. 'Multi Field',
  4245. '/label',
  4246. array('div' => array('class' => 'checkbox')),
  4247. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4248. array('label' => array('for' => 'ModelMultiField0')),
  4249. 'first',
  4250. '/label',
  4251. '/div',
  4252. array('div' => array('class' => 'checkbox')),
  4253. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4254. array('label' => array('for' => 'ModelMultiField1')),
  4255. 'second',
  4256. '/label',
  4257. '/div',
  4258. '/div'
  4259. );
  4260. $this->assertTags($result, $expected);
  4261. }
  4262. /**
  4263. * test that select() with multiple = checkbox works with overriding name attribute.
  4264. *
  4265. * @return void
  4266. */
  4267. public function testSelectCheckboxMultipleOverrideName() {
  4268. $result = $this->Form->input('category', array(
  4269. 'type' => 'select',
  4270. 'multiple' => 'checkbox',
  4271. 'name' => 'data[fish]',
  4272. 'options' => array('1', '2'),
  4273. 'div' => false,
  4274. 'label' => false,
  4275. ));
  4276. $expected = array(
  4277. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4278. array('div' => array('class' => 'checkbox')),
  4279. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4280. array('label' => array('for' => 'Category0')), '1', '/label',
  4281. '/div',
  4282. array('div' => array('class' => 'checkbox')),
  4283. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4284. array('label' => array('for' => 'Category1')), '2', '/label',
  4285. '/div'
  4286. );
  4287. $this->assertTags($result, $expected);
  4288. }
  4289. /**
  4290. * Test that 'id' overrides all the checkbox id's as well.
  4291. *
  4292. * @return void
  4293. */
  4294. public function testSelectCheckboxMultipleId() {
  4295. $result = $this->Form->select(
  4296. 'Model.multi_field',
  4297. array('first', 'second', 'third'),
  4298. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4299. );
  4300. $expected = array(
  4301. 'input' => array(
  4302. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4303. ),
  4304. array('div' => array('class' => 'checkbox')),
  4305. array('input' => array(
  4306. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4307. 'value' => '0', 'id' => 'CustomId0'
  4308. )),
  4309. array('label' => array('for' => 'CustomId0')),
  4310. 'first',
  4311. '/label',
  4312. '/div',
  4313. array('div' => array('class' => 'checkbox')),
  4314. array('input' => array(
  4315. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4316. 'value' => '1', 'id' => 'CustomId1'
  4317. )),
  4318. array('label' => array('for' => 'CustomId1')),
  4319. 'second',
  4320. '/label',
  4321. '/div',
  4322. array('div' => array('class' => 'checkbox')),
  4323. array('input' => array(
  4324. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4325. 'value' => '2', 'id' => 'CustomId2'
  4326. )),
  4327. array('label' => array('for' => 'CustomId2')),
  4328. 'third',
  4329. '/label',
  4330. '/div'
  4331. );
  4332. $this->assertTags($result, $expected);
  4333. }
  4334. /**
  4335. * testCheckbox method
  4336. *
  4337. * Test generation of checkboxes
  4338. *
  4339. * @return void
  4340. */
  4341. public function testCheckbox() {
  4342. $result = $this->Form->checkbox('Model.field');
  4343. $expected = array(
  4344. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4345. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4346. );
  4347. $this->assertTags($result, $expected);
  4348. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4349. $expected = array(
  4350. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4351. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4352. );
  4353. $this->assertTags($result, $expected);
  4354. $Contact = ClassRegistry::getObject('Contact');
  4355. $Contact->validationErrors['field'] = 1;
  4356. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4357. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4358. $expected = array(
  4359. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4360. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4361. );
  4362. $this->assertTags($result, $expected);
  4363. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4364. $expected = array(
  4365. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4366. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4367. );
  4368. $this->assertTags($result, $expected);
  4369. $this->Form->request->data['Contact']['field'] = '';
  4370. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4371. $expected = array(
  4372. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4373. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4374. );
  4375. $this->assertTags($result, $expected);
  4376. $Contact->validationErrors = array();
  4377. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4378. $expected = array(
  4379. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4380. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4381. );
  4382. $this->assertTags($result, $expected);
  4383. $this->Form->request->data['Contact']['published'] = 1;
  4384. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4385. $expected = array(
  4386. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4387. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4388. );
  4389. $this->assertTags($result, $expected);
  4390. $this->Form->request->data['Contact']['published'] = 0;
  4391. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4392. $expected = array(
  4393. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4394. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4395. );
  4396. $this->assertTags($result, $expected);
  4397. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4398. $expected = array(
  4399. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4400. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4401. );
  4402. $this->assertTags($result, $expected);
  4403. $result = $this->Form->checkbox('CustomField.1.value');
  4404. $expected = array(
  4405. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4406. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4407. );
  4408. $this->assertTags($result, $expected);
  4409. }
  4410. /**
  4411. * test checkbox() with a custom name attribute
  4412. *
  4413. * @return void
  4414. */
  4415. public function testCheckboxCustomNameAttribute() {
  4416. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4417. $expected = array(
  4418. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4419. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4420. );
  4421. $this->assertTags($result, $expected);
  4422. }
  4423. /**
  4424. * test the checked option for checkboxes.
  4425. *
  4426. * @return void
  4427. */
  4428. public function testCheckboxCheckedOption() {
  4429. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4430. $expected = array(
  4431. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4432. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4433. );
  4434. $this->assertTags($result, $expected);
  4435. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4436. $expected = array(
  4437. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4438. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4439. );
  4440. $this->assertTags($result, $expected);
  4441. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4442. $expected = array(
  4443. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4444. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4445. );
  4446. $this->assertTags($result, $expected);
  4447. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4448. $expected = array(
  4449. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4450. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4451. );
  4452. $this->assertTags($result, $expected);
  4453. $this->Form->request->data['Model']['field'] = 1;
  4454. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4455. $expected = array(
  4456. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4457. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4458. );
  4459. $this->assertTags($result, $expected);
  4460. }
  4461. /**
  4462. * Test that disabled attribute works on both the checkbox and hidden input.
  4463. *
  4464. * @return void
  4465. */
  4466. public function testCheckboxDisabling() {
  4467. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4468. $expected = array(
  4469. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4470. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4471. );
  4472. $this->assertTags($result, $expected);
  4473. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4474. $expected = array(
  4475. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4476. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4477. );
  4478. $this->assertTags($result, $expected);
  4479. }
  4480. /**
  4481. * Test that the hidden input for checkboxes can be omitted or set to a
  4482. * specific value.
  4483. *
  4484. * @return void
  4485. */
  4486. public function testCheckboxHiddenField() {
  4487. $result = $this->Form->input('UserForm.something', array(
  4488. 'type' => 'checkbox',
  4489. 'hiddenField' => false
  4490. ));
  4491. $expected = array(
  4492. 'div' => array('class' => 'input checkbox'),
  4493. array('input' => array(
  4494. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4495. 'value' => '1', 'id' => 'UserFormSomething'
  4496. )),
  4497. 'label' => array('for' => 'UserFormSomething'),
  4498. 'Something',
  4499. '/label',
  4500. '/div'
  4501. );
  4502. $this->assertTags($result, $expected);
  4503. $result = $this->Form->input('UserForm.something', array(
  4504. 'type' => 'checkbox',
  4505. 'value' => 'Y',
  4506. 'hiddenField' => 'N',
  4507. ));
  4508. $expected = array(
  4509. 'div' => array('class' => 'input checkbox'),
  4510. array('input' => array(
  4511. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  4512. 'value' => 'N', 'id' => 'UserFormSomething_'
  4513. )),
  4514. array('input' => array(
  4515. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4516. 'value' => 'Y', 'id' => 'UserFormSomething'
  4517. )),
  4518. 'label' => array('for' => 'UserFormSomething'),
  4519. 'Something',
  4520. '/label',
  4521. '/div'
  4522. );
  4523. $this->assertTags($result, $expected);
  4524. }
  4525. /**
  4526. * testDateTime method
  4527. *
  4528. * Test generation of date/time select elements
  4529. *
  4530. * @return void
  4531. */
  4532. public function testDateTime() {
  4533. extract($this->dateRegex);
  4534. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4535. $now = strtotime('now');
  4536. $expected = array(
  4537. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4538. $daysRegex,
  4539. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4540. date('j', $now),
  4541. '/option',
  4542. '*/select',
  4543. '-',
  4544. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4545. $monthsRegex,
  4546. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4547. date('F', $now),
  4548. '/option',
  4549. '*/select',
  4550. '-',
  4551. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4552. $yearsRegex,
  4553. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4554. date('Y', $now),
  4555. '/option',
  4556. '*/select',
  4557. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4558. $hoursRegex,
  4559. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4560. date('g', $now),
  4561. '/option',
  4562. '*/select',
  4563. ':',
  4564. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4565. $minutesRegex,
  4566. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4567. date('i', $now),
  4568. '/option',
  4569. '*/select',
  4570. ' ',
  4571. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4572. $meridianRegex,
  4573. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4574. date('a', $now),
  4575. '/option',
  4576. '*/select'
  4577. );
  4578. $this->assertTags($result, $expected);
  4579. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4580. $expected = array(
  4581. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4582. $daysRegex,
  4583. array('option' => array('value' => '')),
  4584. '/option',
  4585. '*/select',
  4586. '-',
  4587. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4588. $monthsRegex,
  4589. array('option' => array('value' => '')),
  4590. '/option',
  4591. '*/select',
  4592. '-',
  4593. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4594. $yearsRegex,
  4595. array('option' => array('value' => '')),
  4596. '/option',
  4597. '*/select',
  4598. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4599. $hoursRegex,
  4600. array('option' => array('value' => '')),
  4601. '/option',
  4602. '*/select',
  4603. ':',
  4604. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4605. $minutesRegex,
  4606. array('option' => array('value' => '')),
  4607. '/option',
  4608. '*/select',
  4609. ' ',
  4610. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4611. $meridianRegex,
  4612. array('option' => array('value' => '')),
  4613. '/option',
  4614. '*/select'
  4615. );
  4616. $this->assertTags($result, $expected);
  4617. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4618. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  4619. $this->assertTags($result, $expected);
  4620. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4621. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  4622. $this->assertTags($result, $expected);
  4623. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4624. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  4625. $expected = array(
  4626. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4627. $daysRegex,
  4628. array('option' => array('value' => '')),
  4629. '/option',
  4630. '*/select',
  4631. '-',
  4632. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4633. $monthsRegex,
  4634. array('option' => array('value' => '')),
  4635. '/option',
  4636. '*/select',
  4637. '-',
  4638. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4639. $yearsRegex,
  4640. array('option' => array('value' => '')),
  4641. '/option',
  4642. '*/select',
  4643. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4644. $hoursRegex,
  4645. array('option' => array('value' => '')),
  4646. '/option',
  4647. '*/select',
  4648. ':',
  4649. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4650. $minutesRegex,
  4651. array('option' => array('value' => '')),
  4652. '/option',
  4653. array('option' => array('value' => '00')),
  4654. '00',
  4655. '/option',
  4656. array('option' => array('value' => '05')),
  4657. '05',
  4658. '/option',
  4659. array('option' => array('value' => '10')),
  4660. '10',
  4661. '/option',
  4662. '*/select',
  4663. ' ',
  4664. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4665. $meridianRegex,
  4666. array('option' => array('value' => '')),
  4667. '/option',
  4668. '*/select'
  4669. );
  4670. $this->assertTags($result, $expected);
  4671. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4672. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4673. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4674. $this->Form->request->data['Contact']['data'] = null;
  4675. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4676. $expected = array(
  4677. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4678. $daysRegex,
  4679. array('option' => array('value' => '')),
  4680. '/option',
  4681. '*/select',
  4682. '-',
  4683. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4684. $monthsRegex,
  4685. array('option' => array('value' => '')),
  4686. '/option',
  4687. '*/select',
  4688. '-',
  4689. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4690. $yearsRegex,
  4691. array('option' => array('value' => '')),
  4692. '/option',
  4693. '*/select',
  4694. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4695. $hoursRegex,
  4696. array('option' => array('value' => '')),
  4697. '/option',
  4698. '*/select',
  4699. ':',
  4700. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4701. $minutesRegex,
  4702. array('option' => array('value' => '')),
  4703. '/option',
  4704. '*/select',
  4705. ' ',
  4706. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4707. $meridianRegex,
  4708. array('option' => array('value' => '')),
  4709. '/option',
  4710. '*/select'
  4711. );
  4712. $this->assertTags($result, $expected);
  4713. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4714. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  4715. $now = strtotime($this->Form->data['Model']['field']);
  4716. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  4717. $expected = array(
  4718. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4719. $daysRegex,
  4720. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4721. date('j', $now),
  4722. '/option',
  4723. '*/select',
  4724. '-',
  4725. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4726. $monthsRegex,
  4727. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4728. date('F', $now),
  4729. '/option',
  4730. '*/select',
  4731. '-',
  4732. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  4733. $yearsRegex,
  4734. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4735. date('Y', $now),
  4736. '/option',
  4737. '*/select',
  4738. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4739. $hoursRegex,
  4740. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4741. date('g', $now),
  4742. '/option',
  4743. '*/select',
  4744. ':',
  4745. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4746. $minutesRegex,
  4747. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4748. date('i', $now),
  4749. '/option',
  4750. '*/select',
  4751. ' ',
  4752. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  4753. $meridianRegex,
  4754. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4755. date('a', $now),
  4756. '/option',
  4757. '*/select'
  4758. );
  4759. $this->assertTags($result, $expected);
  4760. $selected = strtotime('2008-10-26 12:33:00');
  4761. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  4762. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  4763. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  4764. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  4765. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  4766. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  4767. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  4768. $this->Form->create('Contact');
  4769. $result = $this->Form->input('published');
  4770. $now = strtotime('now');
  4771. $expected = array(
  4772. 'div' => array('class' => 'input date'),
  4773. 'label' => array('for' => 'ContactPublishedMonth'),
  4774. 'Published',
  4775. '/label',
  4776. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4777. $monthsRegex,
  4778. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4779. date('F', $now),
  4780. '/option',
  4781. '*/select',
  4782. '-',
  4783. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4784. $daysRegex,
  4785. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4786. date('j', $now),
  4787. '/option',
  4788. '*/select',
  4789. '-',
  4790. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4791. $yearsRegex,
  4792. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4793. date('Y', $now),
  4794. '/option',
  4795. '*/select',
  4796. '/div'
  4797. );
  4798. $this->assertTags($result, $expected);
  4799. $result = $this->Form->input('published2', array('type' => 'date'));
  4800. $now = strtotime('now');
  4801. $expected = array(
  4802. 'div' => array('class' => 'input date'),
  4803. 'label' => array('for' => 'ContactPublished2Month'),
  4804. 'Published2',
  4805. '/label',
  4806. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  4807. $monthsRegex,
  4808. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4809. date('F', $now),
  4810. '/option',
  4811. '*/select',
  4812. '-',
  4813. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  4814. $daysRegex,
  4815. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4816. date('j', $now),
  4817. '/option',
  4818. '*/select',
  4819. '-',
  4820. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  4821. $yearsRegex,
  4822. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4823. date('Y', $now),
  4824. '/option',
  4825. '*/select',
  4826. '/div'
  4827. );
  4828. $this->assertTags($result, $expected);
  4829. $this->Form->create('Contact');
  4830. $result = $this->Form->input('published', array('monthNames' => false));
  4831. $now = strtotime('now');
  4832. $expected = array(
  4833. 'div' => array('class' => 'input date'),
  4834. 'label' => array('for' => 'ContactPublishedMonth'),
  4835. 'Published',
  4836. '/label',
  4837. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4838. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4839. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4840. date('m', $now),
  4841. '/option',
  4842. '*/select',
  4843. '-',
  4844. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4845. $daysRegex,
  4846. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4847. date('j', $now),
  4848. '/option',
  4849. '*/select',
  4850. '-',
  4851. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4852. $yearsRegex,
  4853. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4854. date('Y', $now),
  4855. '/option',
  4856. '*/select',
  4857. '/div'
  4858. );
  4859. $this->assertTags($result, $expected);
  4860. $result = $this->Form->input('published', array('type' => 'time'));
  4861. $now = strtotime('now');
  4862. $expected = array(
  4863. 'div' => array('class' => 'input time'),
  4864. 'label' => array('for' => 'ContactPublishedHour'),
  4865. 'Published',
  4866. '/label',
  4867. array('select' => array('name' => 'data[Contact][published][hour]', 'id' => 'ContactPublishedHour')),
  4868. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4869. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4870. date('g', $now),
  4871. '/option',
  4872. '*/select',
  4873. ':',
  4874. );
  4875. $this->assertTags($result, $expected);
  4876. $result = $this->Form->input('published', array(
  4877. 'timeFormat' => 24,
  4878. 'interval' => 5,
  4879. 'selected' => strtotime('2009-09-03 13:37:00'),
  4880. 'type' => 'datetime'
  4881. ));
  4882. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4883. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  4884. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  4885. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  4886. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  4887. $this->assertNoErrors();
  4888. $this->Form->request->data['Contact'] = array(
  4889. 'date' => array(
  4890. 'day' => '',
  4891. 'month' => '',
  4892. 'year' => '',
  4893. 'hour' => '',
  4894. 'min' => '',
  4895. 'meridian' => ''
  4896. )
  4897. );
  4898. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4899. }
  4900. /**
  4901. * test that datetime() and default values work.
  4902. *
  4903. * @return void
  4904. */
  4905. public function testDatetimeWithDefault() {
  4906. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  4907. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4908. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4909. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4910. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  4911. 'default' => '2009-06-01 11:15:30'
  4912. ));
  4913. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4914. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4915. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4916. }
  4917. /**
  4918. * test that bogus non-date time data doesn't cause errors.
  4919. *
  4920. * @return void
  4921. */
  4922. public function testDateTimeWithBogusData() {
  4923. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  4924. $this->assertNotRegExp('/selected="selected">\d/', $result);
  4925. }
  4926. /**
  4927. * testDateTimeEmptyAsArray
  4928. *
  4929. * @return void
  4930. */
  4931. public function testDateTimeEmptyAsArray() {
  4932. $result = $this->Form->dateTime('Contact.date',
  4933. 'DMY',
  4934. '12',
  4935. array(
  4936. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  4937. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  4938. )
  4939. )
  4940. );
  4941. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4942. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4943. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4944. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4945. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4946. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4947. $result = $this->Form->dateTime('Contact.date',
  4948. 'DMY',
  4949. '12',
  4950. array(
  4951. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4952. )
  4953. );
  4954. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4955. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4956. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4957. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  4958. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  4959. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  4960. }
  4961. /**
  4962. * testFormDateTimeMulti method
  4963. *
  4964. * test multiple datetime element generation
  4965. *
  4966. * @return void
  4967. */
  4968. public function testFormDateTimeMulti() {
  4969. extract($this->dateRegex);
  4970. $result = $this->Form->dateTime('Contact.1.updated');
  4971. $expected = array(
  4972. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  4973. $daysRegex,
  4974. array('option' => array('value' => '')),
  4975. '/option',
  4976. '*/select',
  4977. '-',
  4978. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  4979. $monthsRegex,
  4980. array('option' => array('value' => '')),
  4981. '/option',
  4982. '*/select',
  4983. '-',
  4984. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  4985. $yearsRegex,
  4986. array('option' => array('value' => '')),
  4987. '/option',
  4988. '*/select',
  4989. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  4990. $hoursRegex,
  4991. array('option' => array('value' => '')),
  4992. '/option',
  4993. '*/select',
  4994. ':',
  4995. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  4996. $minutesRegex,
  4997. array('option' => array('value' => '')),
  4998. '/option',
  4999. '*/select',
  5000. ' ',
  5001. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5002. $meridianRegex,
  5003. array('option' => array('value' => '')),
  5004. '/option',
  5005. '*/select'
  5006. );
  5007. $this->assertTags($result, $expected);
  5008. $result = $this->Form->dateTime('Contact.2.updated');
  5009. $expected = array(
  5010. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5011. $daysRegex,
  5012. array('option' => array('value' => '')),
  5013. '/option',
  5014. '*/select',
  5015. '-',
  5016. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5017. $monthsRegex,
  5018. array('option' => array('value' => '')),
  5019. '/option',
  5020. '*/select',
  5021. '-',
  5022. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5023. $yearsRegex,
  5024. array('option' => array('value' => '')),
  5025. '/option',
  5026. '*/select',
  5027. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5028. $hoursRegex,
  5029. array('option' => array('value' => '')),
  5030. '/option',
  5031. '*/select',
  5032. ':',
  5033. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5034. $minutesRegex,
  5035. array('option' => array('value' => '')),
  5036. '/option',
  5037. '*/select',
  5038. ' ',
  5039. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5040. $meridianRegex,
  5041. array('option' => array('value' => '')),
  5042. '/option',
  5043. '*/select'
  5044. );
  5045. $this->assertTags($result, $expected);
  5046. }
  5047. /**
  5048. * When changing the date format, the label should always focus the first select box when
  5049. * clicked.
  5050. *
  5051. * @return void
  5052. */
  5053. public function testDateTimeLabelIdMatchesFirstInput() {
  5054. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5055. $this->assertContains('label for="ModelDateMonth"', $result);
  5056. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5057. $this->assertContains('label for="ModelDateDay"', $result);
  5058. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5059. $this->assertContains('label for="ModelDateYear"', $result);
  5060. }
  5061. /**
  5062. * testMonth method
  5063. *
  5064. * @return void
  5065. */
  5066. public function testMonth() {
  5067. $result = $this->Form->month('Model.field');
  5068. $expected = array(
  5069. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5070. array('option' => array('value' => '')),
  5071. '/option',
  5072. array('option' => array('value' => '01')),
  5073. date('F', strtotime('2008-01-01 00:00:00')),
  5074. '/option',
  5075. array('option' => array('value' => '02')),
  5076. date('F', strtotime('2008-02-01 00:00:00')),
  5077. '/option',
  5078. '*/select',
  5079. );
  5080. $this->assertTags($result, $expected);
  5081. $result = $this->Form->month('Model.field', array('empty' => true));
  5082. $expected = array(
  5083. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5084. array('option' => array('value' => '')),
  5085. '/option',
  5086. array('option' => array('value' => '01')),
  5087. date('F', strtotime('2008-01-01 00:00:00')),
  5088. '/option',
  5089. array('option' => array('value' => '02')),
  5090. date('F', strtotime('2008-02-01 00:00:00')),
  5091. '/option',
  5092. '*/select',
  5093. );
  5094. $this->assertTags($result, $expected);
  5095. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5096. $expected = array(
  5097. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5098. array('option' => array('value' => '')),
  5099. '/option',
  5100. array('option' => array('value' => '01')),
  5101. '01',
  5102. '/option',
  5103. array('option' => array('value' => '02')),
  5104. '02',
  5105. '/option',
  5106. '*/select',
  5107. );
  5108. $this->assertTags($result, $expected);
  5109. $monthNames = array(
  5110. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5111. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5112. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5113. $expected = array(
  5114. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5115. array('option' => array('value' => '')),
  5116. '/option',
  5117. array('option' => array('value' => '01')),
  5118. 'Jan',
  5119. '/option',
  5120. array('option' => array('value' => '02')),
  5121. 'Feb',
  5122. '/option',
  5123. '*/select',
  5124. );
  5125. $this->assertTags($result, $expected);
  5126. }
  5127. /**
  5128. * testDay method
  5129. *
  5130. * @return void
  5131. */
  5132. public function testDay() {
  5133. extract($this->dateRegex);
  5134. $result = $this->Form->day('Model.field', array('value' => false));
  5135. $expected = array(
  5136. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5137. array('option' => array('value' => '')),
  5138. '/option',
  5139. array('option' => array('value' => '01')),
  5140. '1',
  5141. '/option',
  5142. array('option' => array('value' => '02')),
  5143. '2',
  5144. '/option',
  5145. $daysRegex,
  5146. '/select',
  5147. );
  5148. $this->assertTags($result, $expected);
  5149. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5150. $result = $this->Form->day('Model.field');
  5151. $expected = array(
  5152. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5153. array('option' => array('value' => '')),
  5154. '/option',
  5155. array('option' => array('value' => '01')),
  5156. '1',
  5157. '/option',
  5158. array('option' => array('value' => '02')),
  5159. '2',
  5160. '/option',
  5161. $daysRegex,
  5162. array('option' => array('value' => '10', 'selected' => 'selected')),
  5163. '10',
  5164. '/option',
  5165. $daysRegex,
  5166. '/select',
  5167. );
  5168. $this->assertTags($result, $expected);
  5169. $this->Form->request->data['Model']['field'] = '';
  5170. $result = $this->Form->day('Model.field', array('value' => '10'));
  5171. $expected = array(
  5172. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5173. array('option' => array('value' => '')),
  5174. '/option',
  5175. array('option' => array('value' => '01')),
  5176. '1',
  5177. '/option',
  5178. array('option' => array('value' => '02')),
  5179. '2',
  5180. '/option',
  5181. $daysRegex,
  5182. array('option' => array('value' => '10', 'selected' => 'selected')),
  5183. '10',
  5184. '/option',
  5185. $daysRegex,
  5186. '/select',
  5187. );
  5188. $this->assertTags($result, $expected);
  5189. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5190. $result = $this->Form->day('Model.field', array('value' => true));
  5191. $expected = array(
  5192. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5193. array('option' => array('value' => '')),
  5194. '/option',
  5195. array('option' => array('value' => '01')),
  5196. '1',
  5197. '/option',
  5198. array('option' => array('value' => '02')),
  5199. '2',
  5200. '/option',
  5201. $daysRegex,
  5202. array('option' => array('value' => '10', 'selected' => 'selected')),
  5203. '10',
  5204. '/option',
  5205. $daysRegex,
  5206. '/select',
  5207. );
  5208. $this->assertTags($result, $expected);
  5209. }
  5210. /**
  5211. * testMinute method
  5212. *
  5213. * @return void
  5214. */
  5215. public function testMinute() {
  5216. extract($this->dateRegex);
  5217. $result = $this->Form->minute('Model.field');
  5218. $expected = array(
  5219. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5220. array('option' => array('value' => '')),
  5221. '/option',
  5222. array('option' => array('value' => '00')),
  5223. '00',
  5224. '/option',
  5225. array('option' => array('value' => '01')),
  5226. '01',
  5227. '/option',
  5228. array('option' => array('value' => '02')),
  5229. '02',
  5230. '/option',
  5231. $minutesRegex,
  5232. '/select',
  5233. );
  5234. $this->assertTags($result, $expected);
  5235. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5236. $result = $this->Form->minute('Model.field');
  5237. $expected = array(
  5238. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5239. array('option' => array('value' => '')),
  5240. '/option',
  5241. array('option' => array('value' => '00')),
  5242. '00',
  5243. '/option',
  5244. array('option' => array('value' => '01')),
  5245. '01',
  5246. '/option',
  5247. array('option' => array('value' => '02')),
  5248. '02',
  5249. '/option',
  5250. $minutesRegex,
  5251. array('option' => array('value' => '12', 'selected' => 'selected')),
  5252. '12',
  5253. '/option',
  5254. $minutesRegex,
  5255. '/select',
  5256. );
  5257. $this->assertTags($result, $expected);
  5258. $this->Form->request->data['Model']['field'] = '';
  5259. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5260. $expected = array(
  5261. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5262. array('option' => array('value' => '')),
  5263. '/option',
  5264. array('option' => array('value' => '00')),
  5265. '00',
  5266. '/option',
  5267. array('option' => array('value' => '05')),
  5268. '05',
  5269. '/option',
  5270. array('option' => array('value' => '10')),
  5271. '10',
  5272. '/option',
  5273. $minutesRegex,
  5274. '/select',
  5275. );
  5276. $this->assertTags($result, $expected);
  5277. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5278. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5279. $expected = array(
  5280. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5281. array('option' => array('value' => '')),
  5282. '/option',
  5283. array('option' => array('value' => '00')),
  5284. '00',
  5285. '/option',
  5286. array('option' => array('value' => '05')),
  5287. '05',
  5288. '/option',
  5289. array('option' => array('value' => '10', 'selected' => 'selected')),
  5290. '10',
  5291. '/option',
  5292. $minutesRegex,
  5293. '/select',
  5294. );
  5295. $this->assertTags($result, $expected);
  5296. }
  5297. /**
  5298. * testHour method
  5299. *
  5300. * @return void
  5301. */
  5302. public function testHour() {
  5303. extract($this->dateRegex);
  5304. $result = $this->Form->hour('Model.field', false);
  5305. $expected = array(
  5306. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5307. array('option' => array('value' => '')),
  5308. '/option',
  5309. array('option' => array('value' => '01')),
  5310. '1',
  5311. '/option',
  5312. array('option' => array('value' => '02')),
  5313. '2',
  5314. '/option',
  5315. $hoursRegex,
  5316. '/select',
  5317. );
  5318. $this->assertTags($result, $expected);
  5319. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5320. $result = $this->Form->hour('Model.field', false);
  5321. $expected = array(
  5322. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5323. array('option' => array('value' => '')),
  5324. '/option',
  5325. array('option' => array('value' => '01')),
  5326. '1',
  5327. '/option',
  5328. array('option' => array('value' => '02')),
  5329. '2',
  5330. '/option',
  5331. $hoursRegex,
  5332. array('option' => array('value' => '12', 'selected' => 'selected')),
  5333. '12',
  5334. '/option',
  5335. '/select',
  5336. );
  5337. $this->assertTags($result, $expected);
  5338. $this->Form->request->data['Model']['field'] = '';
  5339. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5340. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5341. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  5342. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5343. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5344. $result = $this->Form->hour('Model.field', true);
  5345. $expected = array(
  5346. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5347. array('option' => array('value' => '')),
  5348. '/option',
  5349. array('option' => array('value' => '00', 'selected' => 'selected')),
  5350. '0',
  5351. '/option',
  5352. array('option' => array('value' => '01')),
  5353. '1',
  5354. '/option',
  5355. array('option' => array('value' => '02')),
  5356. '2',
  5357. '/option',
  5358. $hoursRegex,
  5359. '/select',
  5360. );
  5361. $this->assertTags($result, $expected);
  5362. unset($this->Form->request->data['Model']['field']);
  5363. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5364. $thisHour = date('H');
  5365. $optValue = date('G');
  5366. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5367. }
  5368. /**
  5369. * testYear method
  5370. *
  5371. * @return void
  5372. */
  5373. public function testYear() {
  5374. $result = $this->Form->year('Model.field', 2006, 2007);
  5375. $expected = array(
  5376. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5377. array('option' => array('value' => '')),
  5378. '/option',
  5379. array('option' => array('value' => '2007')),
  5380. '2007',
  5381. '/option',
  5382. array('option' => array('value' => '2006')),
  5383. '2006',
  5384. '/option',
  5385. '/select',
  5386. );
  5387. $this->assertTags($result, $expected);
  5388. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5389. $expected = array(
  5390. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5391. array('option' => array('value' => '')),
  5392. '/option',
  5393. array('option' => array('value' => '2006')),
  5394. '2006',
  5395. '/option',
  5396. array('option' => array('value' => '2007')),
  5397. '2007',
  5398. '/option',
  5399. '/select',
  5400. );
  5401. $this->assertTags($result, $expected);
  5402. $this->request->data['Contact']['published'] = '';
  5403. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5404. $expected = array(
  5405. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5406. array('option' => array('value' => '')),
  5407. '/option',
  5408. array('option' => array('value' => '2007')),
  5409. '2007',
  5410. '/option',
  5411. array('option' => array('value' => '2006')),
  5412. '2006',
  5413. '/option',
  5414. '/select',
  5415. );
  5416. $this->assertTags($result, $expected);
  5417. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5418. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5419. $expected = array(
  5420. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5421. array('option' => array('value' => '2007')),
  5422. '2007',
  5423. '/option',
  5424. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5425. '2006',
  5426. '/option',
  5427. '/select',
  5428. );
  5429. $this->assertTags($result, $expected);
  5430. $this->Form->request->data['Contact']['published'] = '';
  5431. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5432. $expected = array(
  5433. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5434. array('option' => array('value' => '')),
  5435. '/option',
  5436. array('option' => array('value' => '2007')),
  5437. '2007',
  5438. '/option',
  5439. array('option' => array('value' => '2006')),
  5440. '2006',
  5441. '/option',
  5442. '/select',
  5443. );
  5444. $this->assertTags($result, $expected);
  5445. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5446. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5447. $expected = array(
  5448. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5449. array('option' => array('value' => '2007')),
  5450. '2007',
  5451. '/option',
  5452. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5453. '2006',
  5454. '/option',
  5455. '/select',
  5456. );
  5457. $this->assertTags($result, $expected);
  5458. $this->Form->request->data['Contact']['published'] = '';
  5459. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5460. $expected = array(
  5461. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5462. array('option' => array('value' => '')),
  5463. '/option',
  5464. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5465. '2007',
  5466. '/option',
  5467. array('option' => array('value' => '2006')),
  5468. '2006',
  5469. '/option',
  5470. '/select',
  5471. );
  5472. $this->assertTags($result, $expected);
  5473. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5474. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5475. $expected = array(
  5476. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5477. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5478. '2007',
  5479. '/option',
  5480. array('option' => array('value' => '2006')),
  5481. '2006',
  5482. '/option',
  5483. '/select',
  5484. );
  5485. $this->assertTags($result, $expected);
  5486. $this->Form->request->data['Contact']['published'] = '';
  5487. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5488. $expected = array(
  5489. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5490. array('option' => array('value' => '2008')),
  5491. '2008',
  5492. '/option',
  5493. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5494. '2007',
  5495. '/option',
  5496. array('option' => array('value' => '2006')),
  5497. '2006',
  5498. '/option',
  5499. '/select',
  5500. );
  5501. $this->assertTags($result, $expected);
  5502. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5503. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5504. $expected = array(
  5505. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5506. array('option' => array('value' => '2008')),
  5507. '2008',
  5508. '/option',
  5509. array('option' => array('value' => '2007')),
  5510. '2007',
  5511. '/option',
  5512. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5513. '2006',
  5514. '/option',
  5515. '/select',
  5516. );
  5517. $this->assertTags($result, $expected);
  5518. $this->Form->request->data = array();
  5519. $this->Form->create('Contact');
  5520. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5521. $expected = array(
  5522. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5523. array('option' => array('value' => '2008')),
  5524. '2008',
  5525. '/option',
  5526. array('option' => array('value' => '2007')),
  5527. '2007',
  5528. '/option',
  5529. array('option' => array('value' => '2006')),
  5530. '2006',
  5531. '/option',
  5532. '/select',
  5533. );
  5534. $this->assertTags($result, $expected);
  5535. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  5536. $this->assertContains('data[Contact][published][year]', $result);
  5537. }
  5538. /**
  5539. * testTextArea method
  5540. *
  5541. * @return void
  5542. */
  5543. public function testTextArea() {
  5544. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  5545. $result = $this->Form->textarea('Model.field');
  5546. $expected = array(
  5547. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5548. 'some test data',
  5549. '/textarea',
  5550. );
  5551. $this->assertTags($result, $expected);
  5552. $result = $this->Form->textarea('Model.tmp');
  5553. $expected = array(
  5554. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  5555. '/textarea',
  5556. );
  5557. $this->assertTags($result, $expected);
  5558. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5559. $result = $this->Form->textarea('Model.field');
  5560. $expected = array(
  5561. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5562. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5563. '/textarea',
  5564. );
  5565. $this->assertTags($result, $expected);
  5566. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5567. $result = $this->Form->textarea('Model.field', array('escape' => false));
  5568. $expected = array(
  5569. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5570. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5571. '/textarea',
  5572. );
  5573. $this->assertTags($result, $expected);
  5574. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  5575. $result = $this->Form->textarea('Model.0.OtherModel.field');
  5576. $expected = array(
  5577. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  5578. '/textarea'
  5579. );
  5580. $this->assertTags($result, $expected);
  5581. }
  5582. /**
  5583. * testTextAreaWithStupidCharacters method
  5584. *
  5585. * test text area with non-ascii characters
  5586. *
  5587. * @return void
  5588. */
  5589. public function testTextAreaWithStupidCharacters() {
  5590. $this->loadFixtures('Post');
  5591. $result = $this->Form->input('Post.content', array(
  5592. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  5593. ));
  5594. $expected = array(
  5595. 'div' => array('class' => 'input text'),
  5596. 'label' => array('for' => 'PostContent'),
  5597. 'Current Text',
  5598. '/label',
  5599. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  5600. 'GREAT®',
  5601. '/textarea',
  5602. '/div'
  5603. );
  5604. $this->assertTags($result, $expected);
  5605. }
  5606. /**
  5607. * testHiddenField method
  5608. *
  5609. * @return void
  5610. */
  5611. public function testHiddenField() {
  5612. $Contact = ClassRegistry::getObject('Contact');
  5613. $Contact->validationErrors['field'] = 1;
  5614. $this->Form->request->data['Contact']['field'] = 'test';
  5615. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  5616. $this->assertTags($result, array(
  5617. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  5618. );
  5619. }
  5620. /**
  5621. * testFileUploadField method
  5622. *
  5623. * @return void
  5624. */
  5625. public function testFileUploadField() {
  5626. $result = $this->Form->file('Model.upload');
  5627. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5628. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  5629. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  5630. $expected = array(
  5631. 'div' => array('class' => 'input file'),
  5632. 'label' => array('for' => 'ModelUpload'),
  5633. 'Upload',
  5634. '/label',
  5635. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  5636. '/div'
  5637. );
  5638. $this->assertTags($result, $expected);
  5639. }
  5640. /**
  5641. * test File upload input on a model not used in create();
  5642. *
  5643. * @return void
  5644. */
  5645. public function testFileUploadOnOtherModel() {
  5646. $this->Form->create('ValidateUser', array('type' => 'file'));
  5647. $result = $this->Form->file('ValidateProfile.city');
  5648. $expected = array(
  5649. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  5650. );
  5651. $this->assertTags($result, $expected);
  5652. }
  5653. /**
  5654. * testButton method
  5655. *
  5656. * @return void
  5657. */
  5658. public function testButton() {
  5659. $result = $this->Form->button('Hi');
  5660. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5661. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5662. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5663. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5664. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5665. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5666. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5667. $result = $this->Form->button('No type', array('type' => false));
  5668. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5669. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  5670. $this->assertNotRegExp('/\&039/', $result);
  5671. }
  5672. /**
  5673. * Test that button() makes unlocked fields by default.
  5674. *
  5675. * @return void
  5676. */
  5677. public function testButtonUnlockedByDefault() {
  5678. $this->Form->request->params['_Token']['key'] = 'secured';
  5679. $this->Form->button('Save', array('name' => 'save'));
  5680. $this->Form->button('Clear');
  5681. $result = $this->Form->unlockField();
  5682. $this->assertEquals(array('save'), $result);
  5683. }
  5684. /**
  5685. * testPostButton method
  5686. *
  5687. * @return void
  5688. */
  5689. public function testPostButton() {
  5690. $result = $this->Form->postButton('Hi', '/controller/action');
  5691. $this->assertTags($result, array(
  5692. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5693. 'div' => array('style' => 'display:none;'),
  5694. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5695. '/div',
  5696. 'button' => array('type' => 'submit'),
  5697. 'Hi',
  5698. '/button',
  5699. '/form'
  5700. ));
  5701. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5702. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  5703. }
  5704. /**
  5705. * Test that postButton adds _Token fields.
  5706. *
  5707. * @return void
  5708. */
  5709. public function testSecurePostButton() {
  5710. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5711. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5712. $expected = array(
  5713. 'form' => array(
  5714. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5715. ),
  5716. array('div' => array('style' => 'display:none;')),
  5717. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5718. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5719. '/div',
  5720. 'button' => array('type' => 'submit'),
  5721. 'Delete',
  5722. '/button',
  5723. array('div' => array('style' => 'display:none;')),
  5724. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5725. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5726. '/div',
  5727. '/form',
  5728. );
  5729. $this->assertTags($result, $expected);
  5730. }
  5731. /**
  5732. * testPostLink method
  5733. *
  5734. * @return void
  5735. */
  5736. public function testPostLink() {
  5737. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5738. $this->assertTags($result, array(
  5739. 'form' => array(
  5740. 'method' => 'post', 'action' => '/posts/delete/1',
  5741. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5742. ),
  5743. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5744. '/form',
  5745. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5746. 'Delete',
  5747. '/a'
  5748. ));
  5749. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  5750. $this->assertTags($result, array(
  5751. 'form' => array(
  5752. 'method' => 'post', 'action' => '/posts/delete/1',
  5753. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5754. ),
  5755. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  5756. '/form',
  5757. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5758. 'Delete',
  5759. '/a'
  5760. ));
  5761. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5762. $this->assertTags($result, array(
  5763. 'form' => array(
  5764. 'method' => 'post', 'action' => '/posts/delete/1',
  5765. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5766. ),
  5767. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5768. '/form',
  5769. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5770. 'Delete',
  5771. '/a'
  5772. ));
  5773. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5774. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  5775. }
  5776. /**
  5777. * Test that postLink adds _Token fields.
  5778. *
  5779. * @return void
  5780. */
  5781. public function testSecurePostLink() {
  5782. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5783. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5784. $expected = array(
  5785. 'form' => array(
  5786. 'method' => 'post', 'action' => '/posts/delete/1',
  5787. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5788. ),
  5789. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5790. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5791. 'div' => array('style' => 'display:none;'),
  5792. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5793. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5794. '/div',
  5795. '/form',
  5796. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5797. 'Delete',
  5798. '/a'
  5799. );
  5800. $this->assertTags($result, $expected);
  5801. }
  5802. /**
  5803. * testSubmitButton method
  5804. *
  5805. * @return void
  5806. */
  5807. public function testSubmitButton() {
  5808. $result = $this->Form->submit('');
  5809. $expected = array(
  5810. 'div' => array('class' => 'submit'),
  5811. 'input' => array('type' => 'submit', 'value' => ''),
  5812. '/div'
  5813. );
  5814. $this->assertTags($result, $expected);
  5815. $result = $this->Form->submit('Test Submit');
  5816. $expected = array(
  5817. 'div' => array('class' => 'submit'),
  5818. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5819. '/div'
  5820. );
  5821. $this->assertTags($result, $expected);
  5822. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5823. $expected = array(
  5824. 'span' => array('class' => 'submit'),
  5825. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5826. '/span'
  5827. );
  5828. $this->assertTags($result, $expected);
  5829. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5830. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5831. $this->assertTags($result, $expected);
  5832. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5833. $expected = array(
  5834. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5835. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5836. '/div'
  5837. );
  5838. $this->assertTags($result, $expected);
  5839. $result = $this->Form->submit('Next >');
  5840. $expected = array(
  5841. 'div' => array('class' => 'submit'),
  5842. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5843. '/div'
  5844. );
  5845. $this->assertTags($result, $expected);
  5846. $result = $this->Form->submit('Next >', array('escape' => false));
  5847. $expected = array(
  5848. 'div' => array('class' => 'submit'),
  5849. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5850. '/div'
  5851. );
  5852. $this->assertTags($result, $expected);
  5853. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5854. $expected = array(
  5855. 'div' => array('class' => 'submit'),
  5856. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5857. '/div'
  5858. );
  5859. $this->assertTags($result, $expected);
  5860. $before = '--before--';
  5861. $after = '--after--';
  5862. $result = $this->Form->submit('Test', array('before' => $before));
  5863. $expected = array(
  5864. 'div' => array('class' => 'submit'),
  5865. '--before--',
  5866. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5867. '/div'
  5868. );
  5869. $this->assertTags($result, $expected);
  5870. $result = $this->Form->submit('Test', array('after' => $after));
  5871. $expected = array(
  5872. 'div' => array('class' => 'submit'),
  5873. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5874. '--after--',
  5875. '/div'
  5876. );
  5877. $this->assertTags($result, $expected);
  5878. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5879. $expected = array(
  5880. 'div' => array('class' => 'submit'),
  5881. '--before--',
  5882. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5883. '--after--',
  5884. '/div'
  5885. );
  5886. $this->assertTags($result, $expected);
  5887. }
  5888. /**
  5889. * test image submit types.
  5890. *
  5891. * @return void
  5892. */
  5893. public function testSubmitImage() {
  5894. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5895. $expected = array(
  5896. 'div' => array('class' => 'submit'),
  5897. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5898. '/div'
  5899. );
  5900. $this->assertTags($result, $expected);
  5901. $result = $this->Form->submit('/relative/cake.power.gif');
  5902. $expected = array(
  5903. 'div' => array('class' => 'submit'),
  5904. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5905. '/div'
  5906. );
  5907. $this->assertTags($result, $expected);
  5908. $result = $this->Form->submit('cake.power.gif');
  5909. $expected = array(
  5910. 'div' => array('class' => 'submit'),
  5911. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5912. '/div'
  5913. );
  5914. $this->assertTags($result, $expected);
  5915. $result = $this->Form->submit('Not.an.image');
  5916. $expected = array(
  5917. 'div' => array('class' => 'submit'),
  5918. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5919. '/div'
  5920. );
  5921. $this->assertTags($result, $expected);
  5922. $after = '--after--';
  5923. $before = '--before--';
  5924. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5925. $expected = array(
  5926. 'div' => array('class' => 'submit'),
  5927. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5928. '--after--',
  5929. '/div'
  5930. );
  5931. $this->assertTags($result, $expected);
  5932. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5933. $expected = array(
  5934. 'div' => array('class' => 'submit'),
  5935. '--before--',
  5936. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5937. '/div'
  5938. );
  5939. $this->assertTags($result, $expected);
  5940. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5941. $expected = array(
  5942. 'div' => array('class' => 'submit'),
  5943. '--before--',
  5944. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5945. '--after--',
  5946. '/div'
  5947. );
  5948. $this->assertTags($result, $expected);
  5949. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  5950. $expected = array(
  5951. 'div' => array('class' => 'submit'),
  5952. '--before--',
  5953. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5954. '--after--',
  5955. '/div'
  5956. );
  5957. $this->assertTags($result, $expected);
  5958. }
  5959. /**
  5960. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5961. * be submitted at a time.
  5962. *
  5963. * @return void
  5964. */
  5965. public function testSubmitUnlockedByDefault() {
  5966. $this->Form->request->params['_Token']['key'] = 'secured';
  5967. $this->Form->submit('Go go');
  5968. $this->Form->submit('Save', array('name' => 'save'));
  5969. $result = $this->Form->unlockField();
  5970. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5971. }
  5972. /**
  5973. * Test submit image with timestamps.
  5974. *
  5975. * @return void
  5976. */
  5977. public function testSubmitImageTimestamp() {
  5978. Configure::write('Asset.timestamp', 'force');
  5979. $result = $this->Form->submit('cake.power.gif');
  5980. $expected = array(
  5981. 'div' => array('class' => 'submit'),
  5982. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  5983. '/div'
  5984. );
  5985. $this->assertTags($result, $expected);
  5986. }
  5987. /**
  5988. * test the create() method
  5989. *
  5990. * @return void
  5991. */
  5992. public function testCreate() {
  5993. $result = $this->Form->create('Contact');
  5994. $encoding = strtolower(Configure::read('App.encoding'));
  5995. $expected = array(
  5996. 'form' => array(
  5997. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5998. 'accept-charset' => $encoding
  5999. ),
  6000. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  6001. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6002. '/div'
  6003. );
  6004. $this->assertTags($result, $expected);
  6005. $result = $this->Form->create('Contact', array('type' => 'GET'));
  6006. $expected = array('form' => array(
  6007. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6008. 'accept-charset' => $encoding
  6009. ));
  6010. $this->assertTags($result, $expected);
  6011. $result = $this->Form->create('Contact', array('type' => 'get'));
  6012. $expected = array('form' => array(
  6013. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6014. 'accept-charset' => $encoding
  6015. ));
  6016. $this->assertTags($result, $expected);
  6017. $result = $this->Form->create('Contact', array('type' => 'put'));
  6018. $expected = array(
  6019. 'form' => array(
  6020. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6021. 'accept-charset' => $encoding
  6022. ),
  6023. 'div' => array('style' => 'display:none;'),
  6024. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6025. '/div'
  6026. );
  6027. $this->assertTags($result, $expected);
  6028. $result = $this->Form->create('Contact', array('type' => 'file'));
  6029. $expected = array(
  6030. 'form' => array(
  6031. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6032. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6033. ),
  6034. 'div' => array('style' => 'display:none;'),
  6035. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6036. '/div'
  6037. );
  6038. $this->assertTags($result, $expected);
  6039. $this->Form->request->data['Contact']['id'] = 1;
  6040. $this->Form->request->here = '/contacts/edit/1';
  6041. $this->Form->request['action'] = 'edit';
  6042. $result = $this->Form->create('Contact');
  6043. $expected = array(
  6044. 'form' => array(
  6045. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6046. 'accept-charset' => $encoding
  6047. ),
  6048. 'div' => array('style' => 'display:none;'),
  6049. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6050. '/div'
  6051. );
  6052. $this->assertTags($result, $expected);
  6053. $this->Form->request->data['Contact']['id'] = 1;
  6054. $this->Form->request->here = '/contacts/edit/1';
  6055. $this->Form->request['action'] = 'edit';
  6056. $result = $this->Form->create('Contact', array('type' => 'file'));
  6057. $expected = array(
  6058. 'form' => array(
  6059. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6060. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6061. ),
  6062. 'div' => array('style' => 'display:none;'),
  6063. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6064. '/div'
  6065. );
  6066. $this->assertTags($result, $expected);
  6067. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  6068. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  6069. $expected = array(
  6070. 'form' => array(
  6071. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  6072. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  6073. ),
  6074. 'div' => array('style' => 'display:none;'),
  6075. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6076. '/div'
  6077. );
  6078. $this->assertTags($result, $expected);
  6079. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  6080. $expected = array(
  6081. 'form' => array(
  6082. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  6083. 'accept-charset' => $encoding
  6084. ),
  6085. 'div' => array('style' => 'display:none;'),
  6086. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6087. '/div'
  6088. );
  6089. $this->assertTags($result, $expected);
  6090. $this->Form->request['action'] = 'add';
  6091. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  6092. $expected = array(
  6093. 'form' => array(
  6094. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  6095. 'accept-charset' => $encoding
  6096. ),
  6097. 'div' => array('style' => 'display:none;'),
  6098. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6099. '/div'
  6100. );
  6101. $this->assertTags($result, $expected);
  6102. $result = $this->Form->create('User', array('action' => 'login'));
  6103. $expected = array(
  6104. 'form' => array(
  6105. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  6106. 'accept-charset' => $encoding
  6107. ),
  6108. 'div' => array('style' => 'display:none;'),
  6109. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6110. '/div'
  6111. );
  6112. $this->assertTags($result, $expected);
  6113. $result = $this->Form->create('User', array('url' => '/users/login'));
  6114. $expected = array(
  6115. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  6116. 'div' => array('style' => 'display:none;'),
  6117. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6118. '/div'
  6119. );
  6120. $this->assertTags($result, $expected);
  6121. $this->Form->request['controller'] = 'pages';
  6122. $result = $this->Form->create('User', array('action' => 'signup'));
  6123. $expected = array(
  6124. 'form' => array(
  6125. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  6126. 'accept-charset' => $encoding
  6127. ),
  6128. 'div' => array('style' => 'display:none;'),
  6129. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6130. '/div'
  6131. );
  6132. $this->assertTags($result, $expected);
  6133. $this->Form->request->data = array();
  6134. $this->Form->request['controller'] = 'contacts';
  6135. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6136. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  6137. $expected = array(
  6138. 'form' => array(
  6139. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  6140. 'accept-charset' => 'utf-8'
  6141. ),
  6142. 'div' => array('style' => 'display:none;'),
  6143. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6144. '/div'
  6145. );
  6146. $this->assertTags($result, $expected);
  6147. }
  6148. /**
  6149. * Test the onsubmit option for create()
  6150. *
  6151. * @return void
  6152. */
  6153. public function testCreateOnSubmit() {
  6154. $this->Form->request->data = array();
  6155. $this->Form->request['controller'] = 'contacts';
  6156. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6157. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  6158. $expected = array(
  6159. 'form' => array(
  6160. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  6161. 'accept-charset' => 'utf-8'
  6162. ),
  6163. 'div' => array('style' => 'display:none;'),
  6164. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6165. '/div'
  6166. );
  6167. $this->assertTags($result, $expected);
  6168. $this->Form->request->data = array();
  6169. $this->Form->request['controller'] = 'contacts';
  6170. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6171. $result = $this->Form->create(array(
  6172. 'url' => array('action' => 'index', 'param'),
  6173. 'default' => false,
  6174. 'onsubmit' => 'someFunction();'
  6175. ));
  6176. $expected = array(
  6177. 'form' => array(
  6178. 'id' => 'ContactAddForm', 'method' => 'post',
  6179. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6180. 'action' => '/contacts/index/param',
  6181. 'accept-charset' => 'utf-8'
  6182. ),
  6183. 'div' => array('style' => 'display:none;'),
  6184. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6185. '/div'
  6186. );
  6187. $this->assertTags($result, $expected);
  6188. }
  6189. /**
  6190. * test create() with automatic url generation
  6191. *
  6192. * @return void
  6193. */
  6194. public function testCreateAutoUrl() {
  6195. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6196. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6197. $this->Form->request->base = '/base_url';
  6198. $result = $this->Form->create('Contact');
  6199. $expected = array(
  6200. 'form' => array(
  6201. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6202. 'accept-charset' => 'utf-8'
  6203. ),
  6204. 'div' => array('style' => 'display:none;'),
  6205. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6206. '/div'
  6207. );
  6208. $this->assertTags($result, $expected);
  6209. $this->Form->request['action'] = 'delete';
  6210. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6211. $this->Form->request->base = '/base_url';
  6212. $result = $this->Form->create('Contact');
  6213. $expected = array(
  6214. 'form' => array(
  6215. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6216. 'accept-charset' => 'utf-8'
  6217. ),
  6218. 'div' => array('style' => 'display:none;'),
  6219. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6220. '/div'
  6221. );
  6222. $this->assertTags($result, $expected);
  6223. }
  6224. /**
  6225. * test create() with a custom route
  6226. *
  6227. * @return void
  6228. */
  6229. public function testCreateCustomRoute() {
  6230. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6231. $encoding = strtolower(Configure::read('App.encoding'));
  6232. $result = $this->Form->create('User', array('action' => 'login'));
  6233. $expected = array(
  6234. 'form' => array(
  6235. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6236. 'accept-charset' => $encoding
  6237. ),
  6238. 'div' => array('style' => 'display:none;'),
  6239. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6240. '/div'
  6241. );
  6242. $this->assertTags($result, $expected);
  6243. }
  6244. /**
  6245. * test that inputDefaults are stored and used.
  6246. *
  6247. * @return void
  6248. */
  6249. public function testCreateWithInputDefaults() {
  6250. $this->Form->create('User', array(
  6251. 'inputDefaults' => array(
  6252. 'div' => false,
  6253. 'label' => false,
  6254. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6255. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6256. )
  6257. ));
  6258. $result = $this->Form->input('username');
  6259. $expected = array(
  6260. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6261. );
  6262. $this->assertTags($result, $expected);
  6263. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6264. $expected = array(
  6265. 'div' => array('class' => 'input text'),
  6266. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6267. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6268. '/div'
  6269. );
  6270. $this->assertTags($result, $expected);
  6271. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  6272. $expected = array(
  6273. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6274. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  6275. );
  6276. $this->assertTags($result, $expected);
  6277. $this->Form->create('User', array(
  6278. 'inputDefaults' => array(
  6279. 'div' => false,
  6280. 'label' => array('class' => 'nice', 'for' => 'changed'),
  6281. )
  6282. ));
  6283. $result = $this->Form->input('username', array('div' => true));
  6284. $expected = array(
  6285. 'div' => array('class' => 'input text'),
  6286. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  6287. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6288. '/div'
  6289. );
  6290. $this->assertTags($result, $expected);
  6291. }
  6292. /**
  6293. * test automatic accept-charset overriding
  6294. *
  6295. * @return void
  6296. */
  6297. public function testCreateWithAcceptCharset() {
  6298. $result = $this->Form->create('UserForm', array(
  6299. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  6300. )
  6301. );
  6302. $expected = array(
  6303. 'form' => array(
  6304. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6305. 'accept-charset' => 'iso-8859-1'
  6306. ),
  6307. 'div' => array('style' => 'display:none;'),
  6308. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6309. '/div'
  6310. );
  6311. $this->assertTags($result, $expected);
  6312. }
  6313. /**
  6314. * Test base form url when url param is passed with multiple parameters (&)
  6315. *
  6316. */
  6317. public function testCreateQuerystringrequest() {
  6318. $encoding = strtolower(Configure::read('App.encoding'));
  6319. $result = $this->Form->create('Contact', array(
  6320. 'type' => 'post',
  6321. 'escape' => false,
  6322. 'url' => array(
  6323. 'controller' => 'controller',
  6324. 'action' => 'action',
  6325. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6326. )
  6327. ));
  6328. $expected = array(
  6329. 'form' => array(
  6330. 'id' => 'ContactAddForm',
  6331. 'method' => 'post',
  6332. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6333. 'accept-charset' => $encoding
  6334. ),
  6335. 'div' => array('style' => 'display:none;'),
  6336. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6337. '/div'
  6338. );
  6339. $this->assertTags($result, $expected);
  6340. $result = $this->Form->create('Contact', array(
  6341. 'type' => 'post',
  6342. 'url' => array(
  6343. 'controller' => 'controller',
  6344. 'action' => 'action',
  6345. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6346. )
  6347. ));
  6348. $expected = array(
  6349. 'form' => array(
  6350. 'id' => 'ContactAddForm',
  6351. 'method' => 'post',
  6352. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6353. 'accept-charset' => $encoding
  6354. ),
  6355. 'div' => array('style' => 'display:none;'),
  6356. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6357. '/div'
  6358. );
  6359. $this->assertTags($result, $expected);
  6360. }
  6361. /**
  6362. * test that create() doesn't cause errors by multiple id's being in the primary key
  6363. * as could happen with multiple select or checkboxes.
  6364. *
  6365. * @return void
  6366. */
  6367. public function testCreateWithMultipleIdInData() {
  6368. $encoding = strtolower(Configure::read('App.encoding'));
  6369. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6370. $result = $this->Form->create('Contact');
  6371. $expected = array(
  6372. 'form' => array(
  6373. 'id' => 'ContactAddForm',
  6374. 'method' => 'post',
  6375. 'action' => '/contacts/add',
  6376. 'accept-charset' => $encoding
  6377. ),
  6378. 'div' => array('style' => 'display:none;'),
  6379. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6380. '/div'
  6381. );
  6382. $this->assertTags($result, $expected);
  6383. }
  6384. /**
  6385. * test that create() doesn't add in extra passed params.
  6386. *
  6387. * @return void
  6388. */
  6389. public function testCreatePassedArgs() {
  6390. $encoding = strtolower(Configure::read('App.encoding'));
  6391. $this->Form->request->data['Contact']['id'] = 1;
  6392. $result = $this->Form->create('Contact', array(
  6393. 'type' => 'post',
  6394. 'escape' => false,
  6395. 'url' => array(
  6396. 'action' => 'edit',
  6397. 'myparam'
  6398. )
  6399. ));
  6400. $expected = array(
  6401. 'form' => array(
  6402. 'id' => 'ContactAddForm',
  6403. 'method' => 'post',
  6404. 'action' => '/contacts/edit/myparam',
  6405. 'accept-charset' => $encoding
  6406. ),
  6407. 'div' => array('style' => 'display:none;'),
  6408. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6409. '/div'
  6410. );
  6411. $this->assertTags($result, $expected);
  6412. }
  6413. /**
  6414. * test creating a get form, and get form inputs.
  6415. *
  6416. * @return void
  6417. */
  6418. public function testGetFormCreate() {
  6419. $encoding = strtolower(Configure::read('App.encoding'));
  6420. $result = $this->Form->create('Contact', array('type' => 'get'));
  6421. $this->assertTags($result, array('form' => array(
  6422. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6423. 'accept-charset' => $encoding
  6424. )));
  6425. $result = $this->Form->text('Contact.name');
  6426. $this->assertTags($result, array('input' => array(
  6427. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6428. )));
  6429. $result = $this->Form->password('password');
  6430. $this->assertTags($result, array('input' => array(
  6431. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6432. )));
  6433. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6434. $result = $this->Form->text('user_form');
  6435. $this->assertTags($result, array('input' => array(
  6436. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6437. )));
  6438. }
  6439. /**
  6440. * test get form, and inputs when the model param is false
  6441. *
  6442. * @return void
  6443. */
  6444. public function testGetFormWithFalseModel() {
  6445. $encoding = strtolower(Configure::read('App.encoding'));
  6446. $this->Form->request['controller'] = 'contact_test';
  6447. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6448. $expected = array('form' => array(
  6449. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6450. 'accept-charset' => $encoding
  6451. ));
  6452. $this->assertTags($result, $expected);
  6453. $result = $this->Form->text('reason');
  6454. $expected = array(
  6455. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6456. );
  6457. $this->assertTags($result, $expected);
  6458. }
  6459. /**
  6460. * test that datetime() works with GET style forms.
  6461. *
  6462. * @return void
  6463. */
  6464. public function testDateTimeWithGetForms() {
  6465. extract($this->dateRegex);
  6466. $this->Form->create('Contact', array('type' => 'get'));
  6467. $result = $this->Form->datetime('created');
  6468. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6469. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6470. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6471. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6472. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6473. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6474. }
  6475. /**
  6476. * testEditFormWithData method
  6477. *
  6478. * test auto populating form elements from submitted data.
  6479. *
  6480. * @return void
  6481. */
  6482. public function testEditFormWithData() {
  6483. $this->Form->request->data = array('Person' => array(
  6484. 'id' => 1,
  6485. 'first_name' => 'Nate',
  6486. 'last_name' => 'Abele',
  6487. 'email' => 'nate@example.com'
  6488. ));
  6489. $this->Form->request->addParams(array(
  6490. 'models' => array('Person'),
  6491. 'controller' => 'people',
  6492. 'action' => 'add'
  6493. ));
  6494. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6495. $this->Form->create();
  6496. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6497. $expected = array(
  6498. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6499. 'select' => array(
  6500. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6501. ),
  6502. array('option' => array('value' => 1)), 'Nate', '/option',
  6503. array('option' => array('value' => 2)), 'Garrett', '/option',
  6504. array('option' => array('value' => 3)), 'Larry', '/option',
  6505. '/select'
  6506. );
  6507. $this->assertTags($result, $expected);
  6508. }
  6509. /**
  6510. * Test that required fields are created for various types of validation.
  6511. *
  6512. * @return void
  6513. */
  6514. public function testFormInputRequiredDetection() {
  6515. $this->Form->create('Contact');
  6516. $result = $this->Form->input('Contact.non_existing');
  6517. $expected = array(
  6518. 'div' => array('class' => 'input text'),
  6519. 'label' => array('for' => 'ContactNonExisting'),
  6520. 'Non Existing',
  6521. '/label',
  6522. 'input' => array(
  6523. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  6524. 'id' => 'ContactNonExisting'
  6525. ),
  6526. '/div'
  6527. );
  6528. $this->assertTags($result, $expected);
  6529. $result = $this->Form->input('Contact.imrequired');
  6530. $expected = array(
  6531. 'div' => array('class' => 'input text required'),
  6532. 'label' => array('for' => 'ContactImrequired'),
  6533. 'Imrequired',
  6534. '/label',
  6535. 'input' => array(
  6536. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  6537. 'id' => 'ContactImrequired'
  6538. ),
  6539. '/div'
  6540. );
  6541. $this->assertTags($result, $expected);
  6542. $result = $this->Form->input('Contact.imalsorequired');
  6543. $expected = array(
  6544. 'div' => array('class' => 'input text required'),
  6545. 'label' => array('for' => 'ContactImalsorequired'),
  6546. 'Imalsorequired',
  6547. '/label',
  6548. 'input' => array(
  6549. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  6550. 'id' => 'ContactImalsorequired'
  6551. ),
  6552. '/div'
  6553. );
  6554. $this->assertTags($result, $expected);
  6555. $result = $this->Form->input('Contact.imrequiredtoo');
  6556. $expected = array(
  6557. 'div' => array('class' => 'input text required'),
  6558. 'label' => array('for' => 'ContactImrequiredtoo'),
  6559. 'Imrequiredtoo',
  6560. '/label',
  6561. 'input' => array(
  6562. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  6563. 'id' => 'ContactImrequiredtoo'
  6564. ),
  6565. '/div'
  6566. );
  6567. $this->assertTags($result, $expected);
  6568. $result = $this->Form->input('Contact.required_one');
  6569. $expected = array(
  6570. 'div' => array('class' => 'input text required'),
  6571. 'label' => array('for' => 'ContactRequiredOne'),
  6572. 'Required One',
  6573. '/label',
  6574. 'input' => array(
  6575. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  6576. 'id' => 'ContactRequiredOne'
  6577. ),
  6578. '/div'
  6579. );
  6580. $this->assertTags($result, $expected);
  6581. $result = $this->Form->input('Contact.string_required');
  6582. $expected = array(
  6583. 'div' => array('class' => 'input text required'),
  6584. 'label' => array('for' => 'ContactStringRequired'),
  6585. 'String Required',
  6586. '/label',
  6587. 'input' => array(
  6588. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  6589. 'id' => 'ContactStringRequired'
  6590. ),
  6591. '/div'
  6592. );
  6593. $this->assertTags($result, $expected);
  6594. $result = $this->Form->input('Contact.imnotrequired');
  6595. $expected = array(
  6596. 'div' => array('class' => 'input text'),
  6597. 'label' => array('for' => 'ContactImnotrequired'),
  6598. 'Imnotrequired',
  6599. '/label',
  6600. 'input' => array(
  6601. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  6602. 'id' => 'ContactImnotrequired'
  6603. ),
  6604. '/div'
  6605. );
  6606. $this->assertTags($result, $expected);
  6607. $result = $this->Form->input('Contact.imalsonotrequired');
  6608. $expected = array(
  6609. 'div' => array('class' => 'input text'),
  6610. 'label' => array('for' => 'ContactImalsonotrequired'),
  6611. 'Imalsonotrequired',
  6612. '/label',
  6613. 'input' => array(
  6614. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  6615. 'id' => 'ContactImalsonotrequired'
  6616. ),
  6617. '/div'
  6618. );
  6619. $this->assertTags($result, $expected);
  6620. $result = $this->Form->input('Contact.imalsonotrequired2');
  6621. $expected = array(
  6622. 'div' => array('class' => 'input text'),
  6623. 'label' => array('for' => 'ContactImalsonotrequired2'),
  6624. 'Imalsonotrequired2',
  6625. '/label',
  6626. 'input' => array(
  6627. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  6628. 'id' => 'ContactImalsonotrequired2'
  6629. ),
  6630. '/div'
  6631. );
  6632. $this->assertTags($result, $expected);
  6633. $result = $this->Form->input('Contact.imnotrequiredeither');
  6634. $expected = array(
  6635. 'div' => array('class' => 'input text'),
  6636. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6637. 'Imnotrequiredeither',
  6638. '/label',
  6639. 'input' => array(
  6640. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  6641. 'id' => 'ContactImnotrequiredeither'
  6642. ),
  6643. '/div'
  6644. );
  6645. $this->assertTags($result, $expected);
  6646. $result = $this->Form->input('Contact.iamrequiredalways');
  6647. $expected = array(
  6648. 'div' => array('class' => 'input text required'),
  6649. 'label' => array('for' => 'ContactIamrequiredalways'),
  6650. 'Iamrequiredalways',
  6651. '/label',
  6652. 'input' => array(
  6653. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  6654. 'id' => 'ContactIamrequiredalways'
  6655. ),
  6656. '/div'
  6657. );
  6658. $this->assertTags($result, $expected);
  6659. }
  6660. /**
  6661. * testFormMagicInput method
  6662. *
  6663. * @return void
  6664. */
  6665. public function testFormMagicInput() {
  6666. $encoding = strtolower(Configure::read('App.encoding'));
  6667. $result = $this->Form->create('Contact');
  6668. $expected = array(
  6669. 'form' => array(
  6670. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6671. 'accept-charset' => $encoding
  6672. ),
  6673. 'div' => array('style' => 'display:none;'),
  6674. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6675. '/div'
  6676. );
  6677. $this->assertTags($result, $expected);
  6678. $result = $this->Form->input('name');
  6679. $expected = array(
  6680. 'div' => array('class' => 'input text'),
  6681. 'label' => array('for' => 'ContactName'),
  6682. 'Name',
  6683. '/label',
  6684. 'input' => array(
  6685. 'type' => 'text', 'name' => 'data[Contact][name]',
  6686. 'id' => 'ContactName', 'maxlength' => '255'
  6687. ),
  6688. '/div'
  6689. );
  6690. $this->assertTags($result, $expected);
  6691. $result = $this->Form->input('non_existing_field_in_contact_model');
  6692. $expected = array(
  6693. 'div' => array('class' => 'input text'),
  6694. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  6695. 'Non Existing Field In Contact Model',
  6696. '/label',
  6697. 'input' => array(
  6698. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  6699. 'id' => 'ContactNonExistingFieldInContactModel'
  6700. ),
  6701. '/div'
  6702. );
  6703. $this->assertTags($result, $expected);
  6704. $result = $this->Form->input('Address.street');
  6705. $expected = array(
  6706. 'div' => array('class' => 'input text'),
  6707. 'label' => array('for' => 'AddressStreet'),
  6708. 'Street',
  6709. '/label',
  6710. 'input' => array(
  6711. 'type' => 'text', 'name' => 'data[Address][street]',
  6712. 'id' => 'AddressStreet'
  6713. ),
  6714. '/div'
  6715. );
  6716. $this->assertTags($result, $expected);
  6717. $result = $this->Form->input('Address.non_existing_field_in_model');
  6718. $expected = array(
  6719. 'div' => array('class' => 'input text'),
  6720. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  6721. 'Non Existing Field In Model',
  6722. '/label',
  6723. 'input' => array(
  6724. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  6725. 'id' => 'AddressNonExistingFieldInModel'
  6726. ),
  6727. '/div'
  6728. );
  6729. $this->assertTags($result, $expected);
  6730. $result = $this->Form->input('name', array('div' => false));
  6731. $expected = array(
  6732. 'label' => array('for' => 'ContactName'),
  6733. 'Name',
  6734. '/label',
  6735. 'input' => array(
  6736. 'type' => 'text', 'name' => 'data[Contact][name]',
  6737. 'id' => 'ContactName', 'maxlength' => '255'
  6738. )
  6739. );
  6740. $this->assertTags($result, $expected);
  6741. extract($this->dateRegex);
  6742. $now = strtotime('now');
  6743. $result = $this->Form->input('Contact.published', array('div' => false));
  6744. $expected = array(
  6745. 'label' => array('for' => 'ContactPublishedMonth'),
  6746. 'Published',
  6747. '/label',
  6748. array('select' => array(
  6749. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  6750. )),
  6751. $monthsRegex,
  6752. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6753. date('F', $now),
  6754. '/option',
  6755. '*/select',
  6756. '-',
  6757. array('select' => array(
  6758. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  6759. )),
  6760. $daysRegex,
  6761. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6762. date('j', $now),
  6763. '/option',
  6764. '*/select',
  6765. '-',
  6766. array('select' => array(
  6767. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  6768. )),
  6769. $yearsRegex,
  6770. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6771. date('Y', $now),
  6772. '*/select'
  6773. );
  6774. $this->assertTags($result, $expected);
  6775. $result = $this->Form->input('Contact.updated', array('div' => false));
  6776. $expected = array(
  6777. 'label' => array('for' => 'ContactUpdatedMonth'),
  6778. 'Updated',
  6779. '/label',
  6780. array('select' => array(
  6781. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  6782. )),
  6783. $monthsRegex,
  6784. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6785. date('F', $now),
  6786. '/option',
  6787. '*/select',
  6788. '-',
  6789. array('select' => array(
  6790. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  6791. )),
  6792. $daysRegex,
  6793. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6794. date('j', $now),
  6795. '/option',
  6796. '*/select',
  6797. '-',
  6798. array('select' => array(
  6799. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  6800. )),
  6801. $yearsRegex,
  6802. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6803. date('Y', $now),
  6804. '*/select'
  6805. );
  6806. $this->assertTags($result, $expected);
  6807. $result = $this->Form->input('UserForm.stuff');
  6808. $expected = array(
  6809. 'div' => array('class' => 'input text'),
  6810. 'label' => array('for' => 'UserFormStuff'),
  6811. 'Stuff',
  6812. '/label',
  6813. 'input' => array(
  6814. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  6815. 'id' => 'UserFormStuff', 'maxlength' => 10
  6816. ),
  6817. '/div'
  6818. );
  6819. $this->assertTags($result, $expected);
  6820. }
  6821. /**
  6822. * testForMagicInputNonExistingNorValidated method
  6823. *
  6824. * @return void
  6825. */
  6826. public function testForMagicInputNonExistingNorValidated() {
  6827. $encoding = strtolower(Configure::read('App.encoding'));
  6828. $result = $this->Form->create('Contact');
  6829. $expected = array(
  6830. 'form' => array(
  6831. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6832. 'accept-charset' => $encoding
  6833. ),
  6834. 'div' => array('style' => 'display:none;'),
  6835. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6836. '/div'
  6837. );
  6838. $this->assertTags($result, $expected);
  6839. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6840. $expected = array(
  6841. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6842. 'Non Existing Nor Validated',
  6843. '/label',
  6844. 'input' => array(
  6845. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6846. 'id' => 'ContactNonExistingNorValidated'
  6847. )
  6848. );
  6849. $this->assertTags($result, $expected);
  6850. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6851. 'div' => false, 'value' => 'my value'
  6852. ));
  6853. $expected = array(
  6854. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6855. 'Non Existing Nor Validated',
  6856. '/label',
  6857. 'input' => array(
  6858. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6859. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6860. )
  6861. );
  6862. $this->assertTags($result, $expected);
  6863. $this->Form->request->data = array(
  6864. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6865. ));
  6866. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6867. $expected = array(
  6868. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6869. 'Non Existing Nor Validated',
  6870. '/label',
  6871. 'input' => array(
  6872. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6873. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6874. )
  6875. );
  6876. $this->assertTags($result, $expected);
  6877. }
  6878. /**
  6879. * testFormMagicInputLabel method
  6880. *
  6881. * @return void
  6882. */
  6883. public function testFormMagicInputLabel() {
  6884. $encoding = strtolower(Configure::read('App.encoding'));
  6885. $result = $this->Form->create('Contact');
  6886. $expected = array(
  6887. 'form' => array(
  6888. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6889. 'accept-charset' => $encoding
  6890. ),
  6891. 'div' => array('style' => 'display:none;'),
  6892. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6893. '/div'
  6894. );
  6895. $this->assertTags($result, $expected);
  6896. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  6897. $this->assertTags($result, array('input' => array(
  6898. 'name' => 'data[Contact][name]', 'type' => 'text',
  6899. 'id' => 'ContactName', 'maxlength' => '255')
  6900. ));
  6901. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  6902. $expected = array(
  6903. 'label' => array('for' => 'ContactName'),
  6904. 'My label',
  6905. '/label',
  6906. 'input' => array(
  6907. 'type' => 'text', 'name' => 'data[Contact][name]',
  6908. 'id' => 'ContactName', 'maxlength' => '255'
  6909. )
  6910. );
  6911. $this->assertTags($result, $expected);
  6912. $result = $this->Form->input('Contact.name', array(
  6913. 'div' => false, 'label' => array('class' => 'mandatory')
  6914. ));
  6915. $expected = array(
  6916. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6917. 'Name',
  6918. '/label',
  6919. 'input' => array(
  6920. 'type' => 'text', 'name' => 'data[Contact][name]',
  6921. 'id' => 'ContactName', 'maxlength' => '255'
  6922. )
  6923. );
  6924. $this->assertTags($result, $expected);
  6925. $result = $this->Form->input('Contact.name', array(
  6926. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  6927. ));
  6928. $expected = array(
  6929. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6930. 'My label',
  6931. '/label',
  6932. 'input' => array(
  6933. 'type' => 'text', 'name' => 'data[Contact][name]',
  6934. 'id' => 'ContactName', 'maxlength' => '255'
  6935. )
  6936. );
  6937. $this->assertTags($result, $expected);
  6938. $result = $this->Form->input('Contact.name', array(
  6939. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  6940. ));
  6941. $expected = array(
  6942. 'label' => array('for' => 'my_id'),
  6943. 'Name',
  6944. '/label',
  6945. 'input' => array(
  6946. 'type' => 'text', 'name' => 'data[Contact][name]',
  6947. 'id' => 'my_id', 'maxlength' => '255'
  6948. )
  6949. );
  6950. $this->assertTags($result, $expected);
  6951. $result = $this->Form->input('1.id');
  6952. $this->assertTags($result, array('input' => array(
  6953. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6954. 'id' => 'Contact1Id'
  6955. )));
  6956. $result = $this->Form->input("1.name");
  6957. $expected = array(
  6958. 'div' => array('class' => 'input text'),
  6959. 'label' => array('for' => 'Contact1Name'),
  6960. 'Name',
  6961. '/label',
  6962. 'input' => array(
  6963. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  6964. 'id' => 'Contact1Name', 'maxlength' => '255'
  6965. ),
  6966. '/div'
  6967. );
  6968. $this->assertTags($result, $expected);
  6969. $result = $this->Form->input('Contact.1.id');
  6970. $this->assertTags($result, array(
  6971. 'input' => array(
  6972. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6973. 'id' => 'Contact1Id'
  6974. )
  6975. ));
  6976. $result = $this->Form->input("Model.1.name");
  6977. $expected = array(
  6978. 'div' => array('class' => 'input text'),
  6979. 'label' => array('for' => 'Model1Name'),
  6980. 'Name',
  6981. '/label',
  6982. 'input' => array(
  6983. 'type' => 'text', 'name' => 'data[Model][1][name]',
  6984. 'id' => 'Model1Name'
  6985. ),
  6986. '/div'
  6987. );
  6988. $this->assertTags($result, $expected);
  6989. }
  6990. /**
  6991. * testFormEnd method
  6992. *
  6993. * @return void
  6994. */
  6995. public function testFormEnd() {
  6996. $this->assertEquals('</form>', $this->Form->end());
  6997. $result = $this->Form->end('');
  6998. $expected = array(
  6999. 'div' => array('class' => 'submit'),
  7000. 'input' => array('type' => 'submit', 'value' => ''),
  7001. '/div',
  7002. '/form'
  7003. );
  7004. $this->assertTags($result, $expected);
  7005. $result = $this->Form->end(array('label' => ''));
  7006. $expected = array(
  7007. 'div' => array('class' => 'submit'),
  7008. 'input' => array('type' => 'submit', 'value' => ''),
  7009. '/div',
  7010. '/form'
  7011. );
  7012. $this->assertTags($result, $expected);
  7013. $result = $this->Form->end('save');
  7014. $expected = array(
  7015. 'div' => array('class' => 'submit'),
  7016. 'input' => array('type' => 'submit', 'value' => 'save'),
  7017. '/div',
  7018. '/form'
  7019. );
  7020. $this->assertTags($result, $expected);
  7021. $result = $this->Form->end(array('label' => 'save'));
  7022. $expected = array(
  7023. 'div' => array('class' => 'submit'),
  7024. 'input' => array('type' => 'submit', 'value' => 'save'),
  7025. '/div',
  7026. '/form'
  7027. );
  7028. $this->assertTags($result, $expected);
  7029. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  7030. $expected = array(
  7031. 'div' => array('class' => 'submit'),
  7032. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7033. '/div',
  7034. '/form'
  7035. );
  7036. $this->assertTags($result, $expected);
  7037. $result = $this->Form->end(array('name' => 'Whatever'));
  7038. $expected = array(
  7039. 'div' => array('class' => 'submit'),
  7040. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  7041. '/div',
  7042. '/form'
  7043. );
  7044. $this->assertTags($result, $expected);
  7045. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  7046. $expected = array(
  7047. 'div' => array('class' => 'good'),
  7048. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7049. '/div',
  7050. '/form'
  7051. );
  7052. $this->assertTags($result, $expected);
  7053. $result = $this->Form->end(array(
  7054. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  7055. ));
  7056. $expected = array(
  7057. 'div' => array('class' => 'good'),
  7058. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7059. '/div',
  7060. '/form'
  7061. );
  7062. $this->assertTags($result, $expected);
  7063. }
  7064. /**
  7065. * testMultipleFormWithIdFields method
  7066. *
  7067. * @return void
  7068. */
  7069. public function testMultipleFormWithIdFields() {
  7070. $this->Form->create('UserForm');
  7071. $result = $this->Form->input('id');
  7072. $this->assertTags($result, array('input' => array(
  7073. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  7074. )));
  7075. $result = $this->Form->input('ValidateItem.id');
  7076. $this->assertTags($result, array('input' => array(
  7077. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  7078. 'id' => 'ValidateItemId'
  7079. )));
  7080. $result = $this->Form->input('ValidateUser.id');
  7081. $this->assertTags($result, array('input' => array(
  7082. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  7083. 'id' => 'ValidateUserId'
  7084. )));
  7085. }
  7086. /**
  7087. * testDbLessModel method
  7088. *
  7089. * @return void
  7090. */
  7091. public function testDbLessModel() {
  7092. $this->Form->create('TestMail');
  7093. $result = $this->Form->input('name');
  7094. $expected = array(
  7095. 'div' => array('class' => 'input text'),
  7096. 'label' => array('for' => 'TestMailName'),
  7097. 'Name',
  7098. '/label',
  7099. 'input' => array(
  7100. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7101. 'id' => 'TestMailName'
  7102. ),
  7103. '/div'
  7104. );
  7105. $this->assertTags($result, $expected);
  7106. ClassRegistry::init('TestMail');
  7107. $this->Form->create('TestMail');
  7108. $result = $this->Form->input('name');
  7109. $expected = array(
  7110. 'div' => array('class' => 'input text'),
  7111. 'label' => array('for' => 'TestMailName'),
  7112. 'Name',
  7113. '/label',
  7114. 'input' => array(
  7115. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7116. 'id' => 'TestMailName'
  7117. ),
  7118. '/div'
  7119. );
  7120. $this->assertTags($result, $expected);
  7121. }
  7122. /**
  7123. * testBrokenness method
  7124. *
  7125. * @return void
  7126. */
  7127. public function testBrokenness() {
  7128. /*
  7129. * #4 This test has two parents and four children. By default (as of r7117) both
  7130. * parents are show but the first parent is missing a child. This is the inconsistency
  7131. * in the default behaviour - one parent has all children, the other does not - dependent
  7132. * on the data values.
  7133. */
  7134. $result = $this->Form->select('Model.field', array(
  7135. 'Fred' => array(
  7136. 'freds_son_1' => 'Fred',
  7137. 'freds_son_2' => 'Freddie'
  7138. ),
  7139. 'Bert' => array(
  7140. 'berts_son_1' => 'Albert',
  7141. 'berts_son_2' => 'Bertie')
  7142. ),
  7143. array('showParents' => true, 'empty' => false)
  7144. );
  7145. $expected = array(
  7146. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7147. array('optgroup' => array('label' => 'Fred')),
  7148. array('option' => array('value' => 'freds_son_1')),
  7149. 'Fred',
  7150. '/option',
  7151. array('option' => array('value' => 'freds_son_2')),
  7152. 'Freddie',
  7153. '/option',
  7154. '/optgroup',
  7155. array('optgroup' => array('label' => 'Bert')),
  7156. array('option' => array('value' => 'berts_son_1')),
  7157. 'Albert',
  7158. '/option',
  7159. array('option' => array('value' => 'berts_son_2')),
  7160. 'Bertie',
  7161. '/option',
  7162. '/optgroup',
  7163. '/select'
  7164. );
  7165. $this->assertTags($result, $expected);
  7166. /*
  7167. * #2 This is structurally identical to the test above (#1) - only the parent name has
  7168. * changed, so we should expect the same select list data, just with a different name
  7169. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  7170. * This is where data corruption can occur, because when a select value is missing from
  7171. * a list a form will substitute the first value in the list - without the user knowing.
  7172. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  7173. * affect the availability of 3 => 'Three' as a valid option.
  7174. */
  7175. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  7176. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  7177. ));
  7178. $result = $this->Form->select(
  7179. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  7180. );
  7181. $expected = array(
  7182. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7183. array('option' => array('value' => 1)),
  7184. 'One',
  7185. '/option',
  7186. array('option' => array('value' => 2)),
  7187. 'Two',
  7188. '/option',
  7189. array('optgroup' => array('label' => 'Three')),
  7190. array('option' => array('value' => 3)),
  7191. 'Three',
  7192. '/option',
  7193. array('option' => array('value' => 4)),
  7194. 'Four',
  7195. '/option',
  7196. array('option' => array('value' => 5)),
  7197. 'Five',
  7198. '/option',
  7199. '/optgroup',
  7200. '/select'
  7201. );
  7202. $this->assertTags($result, $expected);
  7203. }
  7204. /**
  7205. * Test the generation of fields for a multi record form.
  7206. *
  7207. * @return void
  7208. */
  7209. public function testMultiRecordForm() {
  7210. $this->Form->create('ValidateProfile');
  7211. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  7212. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  7213. $expected = array(
  7214. 'div' => array('class' => 'input textarea'),
  7215. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  7216. 'Name',
  7217. '/label',
  7218. 'textarea' => array(
  7219. 'id' => 'ValidateProfile1ValidateItem2Name',
  7220. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  7221. 'cols' => 30,
  7222. 'rows' => 6
  7223. ),
  7224. 'Value',
  7225. '/textarea',
  7226. '/div'
  7227. );
  7228. $this->assertTags($result, $expected);
  7229. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
  7230. $expected = array(
  7231. 'div' => array('class' => 'input date'),
  7232. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7233. 'Created',
  7234. '/label',
  7235. array('select' => array(
  7236. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  7237. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7238. )
  7239. ),
  7240. array('option' => array('value' => '')), '/option',
  7241. $this->dateRegex['monthsRegex'],
  7242. '/select', '-',
  7243. array('select' => array(
  7244. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  7245. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7246. )
  7247. ),
  7248. array('option' => array('value' => '')), '/option',
  7249. $this->dateRegex['daysRegex'],
  7250. '/select', '-',
  7251. array('select' => array(
  7252. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  7253. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7254. )
  7255. ),
  7256. array('option' => array('value' => '')), '/option',
  7257. $this->dateRegex['yearsRegex'],
  7258. '/select',
  7259. '/div'
  7260. );
  7261. $this->assertTags($result, $expected);
  7262. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7263. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7264. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7265. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7266. $expected = array(
  7267. 'div' => array('class' => 'input select error'),
  7268. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7269. 'Profile',
  7270. '/label',
  7271. 'select' => array(
  7272. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7273. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7274. 'class' => 'form-error'
  7275. ),
  7276. '/select',
  7277. array('div' => array('class' => 'error-message')),
  7278. 'Error',
  7279. '/div',
  7280. '/div'
  7281. );
  7282. $this->assertTags($result, $expected);
  7283. }
  7284. /**
  7285. * test the correct display of multi-record form validation errors.
  7286. *
  7287. * @return void
  7288. */
  7289. public function testMultiRecordFormValidationErrors() {
  7290. $this->Form->create('ValidateProfile');
  7291. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7292. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7293. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7294. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7295. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7296. $result = $this->Form->error('ValidateProfile.2.city');
  7297. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7298. $result = $this->Form->error('2.city');
  7299. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7300. }
  7301. /**
  7302. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7303. *
  7304. * @return void
  7305. */
  7306. public function testInputTemplate() {
  7307. $result = $this->Form->input('Contact.email', array(
  7308. 'type' => 'text', 'format' => array('input')
  7309. ));
  7310. $expected = array(
  7311. 'div' => array('class' => 'input text'),
  7312. 'input' => array(
  7313. 'type' => 'text', 'name' => 'data[Contact][email]',
  7314. 'id' => 'ContactEmail'
  7315. ),
  7316. '/div'
  7317. );
  7318. $this->assertTags($result, $expected);
  7319. $result = $this->Form->input('Contact.email', array(
  7320. 'type' => 'text', 'format' => array('input', 'label'),
  7321. 'label' => '<em>Email (required)</em>'
  7322. ));
  7323. $expected = array(
  7324. 'div' => array('class' => 'input text'),
  7325. array('input' => array(
  7326. 'type' => 'text', 'name' => 'data[Contact][email]',
  7327. 'id' => 'ContactEmail'
  7328. )),
  7329. 'label' => array('for' => 'ContactEmail'),
  7330. 'em' => array(),
  7331. 'Email (required)',
  7332. '/em',
  7333. '/label',
  7334. '/div'
  7335. );
  7336. $this->assertTags($result, $expected);
  7337. $result = $this->Form->input('Contact.email', array(
  7338. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7339. 'between' => '<div>Something in the middle</div>',
  7340. 'after' => '<span>Some text at the end</span>'
  7341. ));
  7342. $expected = array(
  7343. 'div' => array('class' => 'input text'),
  7344. array('input' => array(
  7345. 'type' => 'text', 'name' => 'data[Contact][email]',
  7346. 'id' => 'ContactEmail'
  7347. )),
  7348. array('div' => array()),
  7349. 'Something in the middle',
  7350. '/div',
  7351. 'label' => array('for' => 'ContactEmail'),
  7352. 'Email',
  7353. '/label',
  7354. 'span' => array(),
  7355. 'Some text at the end',
  7356. '/span',
  7357. '/div'
  7358. );
  7359. $this->assertTags($result, $expected);
  7360. $result = $this->Form->input('Contact.method', array(
  7361. 'type' => 'radio',
  7362. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7363. 'between' => 'I am between',
  7364. ));
  7365. $expected = array(
  7366. 'div' => array('class' => 'input radio'),
  7367. 'fieldset' => array(),
  7368. 'legend' => array(),
  7369. 'Method',
  7370. '/legend',
  7371. 'I am between',
  7372. 'input' => array(
  7373. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  7374. 'value' => '', 'id' => 'ContactMethod_'
  7375. ),
  7376. array('input' => array(
  7377. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7378. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7379. )),
  7380. array('label' => array('for' => 'ContactMethodEmail')),
  7381. 'Email',
  7382. '/label',
  7383. array('input' => array(
  7384. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7385. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7386. )),
  7387. array('label' => array('for' => 'ContactMethodPigeon')),
  7388. 'Pigeon',
  7389. '/label',
  7390. '/fieldset',
  7391. '/div',
  7392. );
  7393. $this->assertTags($result, $expected);
  7394. }
  7395. /**
  7396. * test that some html5 inputs + FormHelper::__call() work
  7397. *
  7398. * @return void
  7399. */
  7400. public function testHtml5Inputs() {
  7401. $result = $this->Form->email('User.email');
  7402. $expected = array(
  7403. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  7404. );
  7405. $this->assertTags($result, $expected);
  7406. $result = $this->Form->search('User.query');
  7407. $expected = array(
  7408. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  7409. );
  7410. $this->assertTags($result, $expected);
  7411. $result = $this->Form->search('User.query', array('value' => 'test'));
  7412. $expected = array(
  7413. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7414. );
  7415. $this->assertTags($result, $expected);
  7416. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7417. $expected = array(
  7418. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7419. );
  7420. $this->assertTags($result, $expected);
  7421. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  7422. $expected = array(
  7423. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7424. );
  7425. $this->assertTags($result, $expected);
  7426. }
  7427. /**
  7428. *
  7429. * @expectedException CakeException
  7430. * @return void
  7431. */
  7432. public function testHtml5InputException() {
  7433. $this->Form->email();
  7434. }
  7435. /**
  7436. * Tests that a model can be loaded from the model names passed in the request object
  7437. *
  7438. * @return void
  7439. */
  7440. public function testIntrospectModelFromRequest() {
  7441. $this->loadFixtures('Post');
  7442. App::build(array(
  7443. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  7444. ));
  7445. CakePlugin::load('TestPlugin');
  7446. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  7447. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  7448. $this->Form->create('TestPluginPost');
  7449. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  7450. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  7451. CakePlugin::unload();
  7452. App::build();
  7453. }
  7454. /**
  7455. * Tests that it is possible to set the validation errors directly in the helper for a field
  7456. *
  7457. * @return void
  7458. */
  7459. public function testCustomValidationErrors() {
  7460. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7461. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7462. $this->assertEquals('Badness!', $result);
  7463. }
  7464. /**
  7465. * Tests that the 'on' key validates as expected on create
  7466. *
  7467. * @return void
  7468. */
  7469. public function testRequiredOnCreate() {
  7470. $this->Form->create('Contact');
  7471. $result = $this->Form->input('Contact.imrequiredonupdate');
  7472. $expected = array(
  7473. 'div' => array('class' => 'input text'),
  7474. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7475. 'Imrequiredonupdate',
  7476. '/label',
  7477. 'input' => array(
  7478. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7479. 'id' => 'ContactImrequiredonupdate'
  7480. ),
  7481. '/div'
  7482. );
  7483. $this->assertTags($result, $expected);
  7484. $result = $this->Form->input('Contact.imrequiredoncreate');
  7485. $expected = array(
  7486. 'div' => array('class' => 'input text required'),
  7487. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7488. 'Imrequiredoncreate',
  7489. '/label',
  7490. 'input' => array(
  7491. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7492. 'id' => 'ContactImrequiredoncreate'
  7493. ),
  7494. '/div'
  7495. );
  7496. $this->assertTags($result, $expected);
  7497. $result = $this->Form->input('Contact.imrequiredonboth');
  7498. $expected = array(
  7499. 'div' => array('class' => 'input text required'),
  7500. 'label' => array('for' => 'ContactImrequiredonboth'),
  7501. 'Imrequiredonboth',
  7502. '/label',
  7503. 'input' => array(
  7504. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7505. 'id' => 'ContactImrequiredonboth'
  7506. ),
  7507. '/div'
  7508. );
  7509. $this->assertTags($result, $expected);
  7510. $result = $this->Form->input('Contact.imrequired');
  7511. $expected = array(
  7512. 'div' => array('class' => 'input text required'),
  7513. 'label' => array('for' => 'ContactImrequired'),
  7514. 'Imrequired',
  7515. '/label',
  7516. 'input' => array(
  7517. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7518. 'id' => 'ContactImrequired'
  7519. ),
  7520. '/div'
  7521. );
  7522. $this->assertTags($result, $expected);
  7523. }
  7524. /**
  7525. * Tests that the 'on' key validates as expected on update
  7526. *
  7527. * @return void
  7528. */
  7529. public function testRequiredOnUpdate() {
  7530. $this->Form->request->data['Contact']['id'] = 1;
  7531. $this->Form->create('Contact');
  7532. $result = $this->Form->input('Contact.imrequiredonupdate');
  7533. $expected = array(
  7534. 'div' => array('class' => 'input text required'),
  7535. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7536. 'Imrequiredonupdate',
  7537. '/label',
  7538. 'input' => array(
  7539. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7540. 'id' => 'ContactImrequiredonupdate'
  7541. ),
  7542. '/div'
  7543. );
  7544. $this->assertTags($result, $expected);
  7545. $result = $this->Form->input('Contact.imrequiredoncreate');
  7546. $expected = array(
  7547. 'div' => array('class' => 'input text'),
  7548. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7549. 'Imrequiredoncreate',
  7550. '/label',
  7551. 'input' => array(
  7552. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7553. 'id' => 'ContactImrequiredoncreate'
  7554. ),
  7555. '/div'
  7556. );
  7557. $this->assertTags($result, $expected);
  7558. $result = $this->Form->input('Contact.imrequiredonboth');
  7559. $expected = array(
  7560. 'div' => array('class' => 'input text required'),
  7561. 'label' => array('for' => 'ContactImrequiredonboth'),
  7562. 'Imrequiredonboth',
  7563. '/label',
  7564. 'input' => array(
  7565. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7566. 'id' => 'ContactImrequiredonboth'
  7567. ),
  7568. '/div'
  7569. );
  7570. $this->assertTags($result, $expected);
  7571. $result = $this->Form->input('Contact.imrequired');
  7572. $expected = array(
  7573. 'div' => array('class' => 'input text required'),
  7574. 'label' => array('for' => 'ContactImrequired'),
  7575. 'Imrequired',
  7576. '/label',
  7577. 'input' => array(
  7578. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7579. 'id' => 'ContactImrequired'
  7580. ),
  7581. '/div'
  7582. );
  7583. $this->assertTags($result, $expected);
  7584. }
  7585. /**
  7586. * Test inputDefaults setter and getter
  7587. *
  7588. * @return void
  7589. */
  7590. public function testInputDefaults() {
  7591. $this->Form->create('Contact');
  7592. $this->Form->inputDefaults(array(
  7593. 'label' => false,
  7594. 'div' => array(
  7595. 'style' => 'color: #000;'
  7596. )
  7597. ));
  7598. $result = $this->Form->input('Contact.field1');
  7599. $expected = array(
  7600. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  7601. 'input' => array(
  7602. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7603. 'id' => 'ContactField1'
  7604. ),
  7605. '/div'
  7606. );
  7607. $this->assertTags($result, $expected);
  7608. $this->Form->inputDefaults(array(
  7609. 'div' => false,
  7610. 'label' => 'Label',
  7611. ));
  7612. $result = $this->Form->input('Contact.field1');
  7613. $expected = array(
  7614. 'label' => array('for' => 'ContactField1'),
  7615. 'Label',
  7616. '/label',
  7617. 'input' => array(
  7618. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7619. 'id' => 'ContactField1'
  7620. ),
  7621. );
  7622. $this->assertTags($result, $expected);
  7623. $this->Form->inputDefaults(array(
  7624. 'label' => false,
  7625. ), true);
  7626. $result = $this->Form->input('Contact.field1');
  7627. $expected = array(
  7628. 'input' => array(
  7629. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7630. 'id' => 'ContactField1'
  7631. ),
  7632. );
  7633. $this->assertTags($result, $expected);
  7634. $result = $this->Form->inputDefaults();
  7635. $expected = array(
  7636. 'div' => false,
  7637. 'label' => false,
  7638. );
  7639. $this->assertEqual($result, $expected);
  7640. }
  7641. }