FormHelperTest.php 289 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074
  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 (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  16. * @package Cake.Test.Case.View.Helper
  17. * @since CakePHP(tm) v 1.2.0.4206
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. App::uses('ClassRegistry', 'Utility');
  21. App::uses('Controller', 'Controller');
  22. App::uses('View', 'View');
  23. App::uses('Model', 'Model');
  24. App::uses('Security', 'Utility');
  25. App::uses('CakeRequest', 'Network');
  26. App::uses('HtmlHelper', 'View/Helper');
  27. App::uses('FormHelper', 'View/Helper');
  28. App::uses('Router', 'Routing');
  29. /**
  30. * ContactTestController class
  31. *
  32. * @package Cake.Test.Case.View.Helper
  33. */
  34. class ContactTestController extends Controller {
  35. /**
  36. * uses property
  37. *
  38. * @var mixed null
  39. */
  40. public $uses = null;
  41. }
  42. /**
  43. * Contact class
  44. *
  45. * @package Cake.Test.Case.View.Helper
  46. */
  47. class Contact extends CakeTestModel {
  48. /**
  49. * useTable property
  50. *
  51. * @var bool false
  52. */
  53. public $useTable = false;
  54. /**
  55. * Default schema
  56. *
  57. * @var array
  58. */
  59. protected $_schema = array(
  60. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  61. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  62. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  63. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  64. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  65. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  66. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  67. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  68. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  69. );
  70. /**
  71. * validate property
  72. *
  73. * @var array
  74. */
  75. public $validate = array(
  76. 'non_existing' => array(),
  77. 'idontexist' => array(),
  78. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  79. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  80. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  81. 'imrequiredonboth' => array(
  82. 'required' => array('rule' => 'alphaNumeric'),
  83. ),
  84. 'string_required' => 'notEmpty',
  85. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  86. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  87. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  88. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  89. 'imalsonotrequired' => array(
  90. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  91. 'between' => array('rule' => array('between', 5, 30)),
  92. ),
  93. 'imalsonotrequired2' => array(
  94. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  95. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  96. ),
  97. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  98. 'iamrequiredalways' => array(
  99. 'email' => array('rule' => 'email'),
  100. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  101. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  102. ),
  103. 'boolean_field' => array('rule' => 'boolean')
  104. );
  105. /**
  106. * schema method
  107. *
  108. * @return void
  109. */
  110. public function setSchema($schema) {
  111. $this->_schema = $schema;
  112. }
  113. /**
  114. * hasAndBelongsToMany property
  115. *
  116. * @var array
  117. */
  118. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  119. /**
  120. * hasAndBelongsToMany property
  121. *
  122. * @var array
  123. */
  124. public $belongsTo = array('User' => array('className' => 'UserForm'));
  125. }
  126. /**
  127. * ContactTagsContact class
  128. *
  129. * @package Cake.Test.Case.View.Helper
  130. */
  131. class ContactTagsContact extends CakeTestModel {
  132. /**
  133. * useTable property
  134. *
  135. * @var bool false
  136. */
  137. public $useTable = false;
  138. /**
  139. * Default schema
  140. *
  141. * @var array
  142. */
  143. protected $_schema = array(
  144. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  145. 'contact_tag_id' => array(
  146. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  147. )
  148. );
  149. /**
  150. * schema method
  151. *
  152. * @return void
  153. */
  154. public function setSchema($schema) {
  155. $this->_schema = $schema;
  156. }
  157. }
  158. /**
  159. * ContactNonStandardPk class
  160. *
  161. * @package Cake.Test.Case.View.Helper
  162. */
  163. class ContactNonStandardPk extends Contact {
  164. /**
  165. * primaryKey property
  166. *
  167. * @var string
  168. */
  169. public $primaryKey = 'pk';
  170. /**
  171. * schema method
  172. *
  173. * @return void
  174. */
  175. public function schema($field = false) {
  176. $this->_schema = parent::schema();
  177. $this->_schema['pk'] = $this->_schema['id'];
  178. unset($this->_schema['id']);
  179. return $this->_schema;
  180. }
  181. }
  182. /**
  183. * ContactTag class
  184. *
  185. * @package Cake.Test.Case.View.Helper
  186. */
  187. class ContactTag extends Model {
  188. /**
  189. * useTable property
  190. *
  191. * @var bool false
  192. */
  193. public $useTable = false;
  194. /**
  195. * schema definition
  196. *
  197. * @var array
  198. */
  199. protected $_schema = array(
  200. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  201. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  202. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  203. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  204. );
  205. }
  206. /**
  207. * UserForm class
  208. *
  209. * @package Cake.Test.Case.View.Helper
  210. */
  211. class UserForm extends CakeTestModel {
  212. /**
  213. * useTable property
  214. *
  215. * @var bool false
  216. */
  217. public $useTable = false;
  218. /**
  219. * hasMany property
  220. *
  221. * @var array
  222. */
  223. public $hasMany = array(
  224. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  225. ));
  226. /**
  227. * schema definition
  228. *
  229. * @var array
  230. */
  231. protected $_schema = array(
  232. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  233. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  234. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  235. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  236. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  237. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  238. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  239. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  240. );
  241. }
  242. /**
  243. * OpenidUrl class
  244. *
  245. * @package Cake.Test.Case.View.Helper
  246. */
  247. class OpenidUrl extends CakeTestModel {
  248. /**
  249. * useTable property
  250. *
  251. * @var bool false
  252. */
  253. public $useTable = false;
  254. /**
  255. * belongsTo property
  256. *
  257. * @var array
  258. */
  259. public $belongsTo = array('UserForm' => array(
  260. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  261. ));
  262. /**
  263. * validate property
  264. *
  265. * @var array
  266. */
  267. public $validate = array('openid_not_registered' => array());
  268. /**
  269. * schema method
  270. *
  271. * @var array
  272. */
  273. protected $_schema = array(
  274. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  275. 'user_form_id' => array(
  276. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  277. ),
  278. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  279. );
  280. /**
  281. * beforeValidate method
  282. *
  283. * @return void
  284. */
  285. public function beforeValidate($options = array()) {
  286. $this->invalidate('openid_not_registered');
  287. return true;
  288. }
  289. }
  290. /**
  291. * ValidateUser class
  292. *
  293. * @package Cake.Test.Case.View.Helper
  294. */
  295. class ValidateUser extends CakeTestModel {
  296. /**
  297. * useTable property
  298. *
  299. * @var bool false
  300. */
  301. public $useTable = false;
  302. /**
  303. * hasOne property
  304. *
  305. * @var array
  306. */
  307. public $hasOne = array('ValidateProfile' => array(
  308. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  309. ));
  310. /**
  311. * schema method
  312. *
  313. * @var array
  314. */
  315. protected $_schema = array(
  316. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  317. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  318. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  319. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  320. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  321. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  322. );
  323. /**
  324. * beforeValidate method
  325. *
  326. * @return void
  327. */
  328. public function beforeValidate($options = array()) {
  329. $this->invalidate('email');
  330. return false;
  331. }
  332. }
  333. /**
  334. * ValidateProfile class
  335. *
  336. * @package Cake.Test.Case.View.Helper
  337. */
  338. class ValidateProfile extends CakeTestModel {
  339. /**
  340. * useTable property
  341. *
  342. * @var bool false
  343. */
  344. public $useTable = false;
  345. /**
  346. * schema property
  347. *
  348. * @var array
  349. */
  350. protected $_schema = array(
  351. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  352. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  353. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  354. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  355. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  356. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  357. );
  358. /**
  359. * hasOne property
  360. *
  361. * @var array
  362. */
  363. public $hasOne = array('ValidateItem' => array(
  364. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  365. ));
  366. /**
  367. * belongsTo property
  368. *
  369. * @var array
  370. */
  371. public $belongsTo = array('ValidateUser' => array(
  372. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  373. ));
  374. /**
  375. * beforeValidate method
  376. *
  377. * @return void
  378. */
  379. public function beforeValidate($options = array()) {
  380. $this->invalidate('full_name');
  381. $this->invalidate('city');
  382. return false;
  383. }
  384. }
  385. /**
  386. * ValidateItem class
  387. *
  388. * @package Cake.Test.Case.View.Helper
  389. */
  390. class ValidateItem extends CakeTestModel {
  391. /**
  392. * useTable property
  393. *
  394. * @var bool false
  395. */
  396. public $useTable = false;
  397. /**
  398. * schema property
  399. *
  400. * @var array
  401. */
  402. protected $_schema = array(
  403. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  404. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  405. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  406. 'description' => array(
  407. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  408. ),
  409. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  410. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  411. );
  412. /**
  413. * belongsTo property
  414. *
  415. * @var array
  416. */
  417. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  418. /**
  419. * beforeValidate method
  420. *
  421. * @return void
  422. */
  423. public function beforeValidate($options = array()) {
  424. $this->invalidate('description');
  425. return false;
  426. }
  427. }
  428. /**
  429. * TestMail class
  430. *
  431. * @package Cake.Test.Case.View.Helper
  432. */
  433. class TestMail extends CakeTestModel {
  434. /**
  435. * useTable property
  436. *
  437. * @var bool false
  438. */
  439. public $useTable = false;
  440. }
  441. /**
  442. * FormHelperTest class
  443. *
  444. * @package Cake.Test.Case.View.Helper
  445. * @property FormHelper $Form
  446. */
  447. class FormHelperTest extends CakeTestCase {
  448. /**
  449. * Fixtures to be used
  450. *
  451. * @var array
  452. */
  453. public $fixtures = array('core.post');
  454. /**
  455. * Do not load the fixtures by default
  456. *
  457. * @var boolean
  458. */
  459. public $autoFixtures = false;
  460. /**
  461. * setUp method
  462. *
  463. * @return void
  464. */
  465. public function setUp() {
  466. parent::setUp();
  467. Configure::write('App.base', '');
  468. Configure::delete('Asset');
  469. $this->Controller = new ContactTestController();
  470. $this->View = new View($this->Controller);
  471. $this->Form = new FormHelper($this->View);
  472. $this->Form->Html = new HtmlHelper($this->View);
  473. $this->Form->request = new CakeRequest('contacts/add', false);
  474. $this->Form->request->here = '/contacts/add';
  475. $this->Form->request['action'] = 'add';
  476. $this->Form->request->webroot = '';
  477. $this->Form->request->base = '';
  478. ClassRegistry::addObject('Contact', new Contact());
  479. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  480. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  481. ClassRegistry::addObject('User', new UserForm());
  482. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  483. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  484. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  485. $this->oldSalt = Configure::read('Security.salt');
  486. $this->dateRegex = array(
  487. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  488. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  489. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  490. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  491. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  492. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  493. );
  494. Configure::write('Security.salt', 'foo!');
  495. }
  496. /**
  497. * tearDown method
  498. *
  499. * @return void
  500. */
  501. public function tearDown() {
  502. parent::tearDown();
  503. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  504. Configure::write('Security.salt', $this->oldSalt);
  505. }
  506. /**
  507. * testFormCreateWithSecurity method
  508. *
  509. * Test form->create() with security key.
  510. *
  511. * @return void
  512. */
  513. public function testCreateWithSecurity() {
  514. $this->Form->request['_Token'] = array('key' => 'testKey');
  515. $encoding = strtolower(Configure::read('App.encoding'));
  516. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  517. $expected = array(
  518. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  519. 'div' => array('style' => 'display:none;'),
  520. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  521. array('input' => array(
  522. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  523. )),
  524. '/div'
  525. );
  526. $this->assertTags($result, $expected);
  527. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  528. $expected['form']['id'] = 'MyForm';
  529. $this->assertTags($result, $expected);
  530. }
  531. /**
  532. * testFormCreateGetNoSecurity method
  533. *
  534. * Test form->create() with no security key as its a get form
  535. *
  536. * @return void
  537. */
  538. public function testCreateEndGetNoSecurity() {
  539. $this->Form->request['_Token'] = array('key' => 'testKey');
  540. $encoding = strtolower(Configure::read('App.encoding'));
  541. $result = $this->Form->create('Contact', array('type' => 'get', 'url' => '/contacts/add'));
  542. $this->assertNotContains('Token', $result);
  543. $result = $this->Form->end('Save');
  544. $this->assertNotContains('Token', $result);
  545. }
  546. /**
  547. * test that create() clears the fields property so it starts fresh
  548. *
  549. * @return void
  550. */
  551. public function testCreateClearingFields() {
  552. $this->Form->fields = array('model_id');
  553. $this->Form->create('Contact');
  554. $this->assertEquals(array(), $this->Form->fields);
  555. }
  556. /**
  557. * Tests form hash generation with model-less data
  558. *
  559. * @return void
  560. */
  561. public function testValidateHashNoModel() {
  562. $this->Form->request['_Token'] = array('key' => 'foo');
  563. $result = $this->Form->secure(array('anything'));
  564. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  565. }
  566. /**
  567. * Tests that models with identical field names get resolved properly
  568. *
  569. * @return void
  570. */
  571. public function testDuplicateFieldNameResolution() {
  572. $result = $this->Form->create('ValidateUser');
  573. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  574. $result = $this->Form->input('ValidateItem.name');
  575. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  576. $result = $this->Form->input('ValidateUser.name');
  577. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  578. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  579. $this->assertRegExp('/type="text"/', $result);
  580. $result = $this->Form->input('ValidateItem.name');
  581. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  582. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  583. $this->assertRegExp('/<textarea/', $result);
  584. $result = $this->Form->input('name');
  585. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  586. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  587. $this->assertRegExp('/type="text"/', $result);
  588. }
  589. /**
  590. * Tests that hidden fields generated for checkboxes don't get locked
  591. *
  592. * @return void
  593. */
  594. public function testNoCheckboxLocking() {
  595. $this->Form->request['_Token'] = array('key' => 'foo');
  596. $this->assertSame(array(), $this->Form->fields);
  597. $this->Form->checkbox('check', array('value' => '1'));
  598. $this->assertSame($this->Form->fields, array('check'));
  599. }
  600. /**
  601. * testFormSecurityFields method
  602. *
  603. * Test generation of secure form hash generation.
  604. *
  605. * @return void
  606. */
  607. public function testFormSecurityFields() {
  608. $key = 'testKey';
  609. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  610. $this->Form->request['_Token'] = array('key' => $key);
  611. $result = $this->Form->secure($fields);
  612. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  613. $hash .= ':' . 'Model.valid';
  614. $hash = urlencode($hash);
  615. $expected = array(
  616. 'div' => array('style' => 'display:none;'),
  617. array('input' => array(
  618. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  619. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  620. )),
  621. array('input' => array(
  622. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  623. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  624. )),
  625. '/div'
  626. );
  627. $this->assertTags($result, $expected);
  628. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  629. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  630. $result = $this->Form->secure($fields);
  631. $expected = array(
  632. 'div' => array('class' => 'hidden'),
  633. array('input' => array(
  634. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  635. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  636. )),
  637. array('input' => array(
  638. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  639. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  640. )),
  641. '/div'
  642. );
  643. $this->assertTags($result, $expected);
  644. }
  645. /**
  646. * Tests correct generation of number fields for double and float fields
  647. *
  648. * @return void
  649. */
  650. public function testTextFieldGenerationForFloats() {
  651. $model = ClassRegistry::getObject('Contact');
  652. $model->setSchema(array('foo' => array(
  653. 'type' => 'float',
  654. 'null' => false,
  655. 'default' => null,
  656. 'length' => 10
  657. )));
  658. $this->Form->create('Contact');
  659. $result = $this->Form->input('foo');
  660. $expected = array(
  661. 'div' => array('class' => 'input number'),
  662. 'label' => array('for' => 'ContactFoo'),
  663. 'Foo',
  664. '/label',
  665. array('input' => array(
  666. 'type' => 'number',
  667. 'name' => 'data[Contact][foo]',
  668. 'id' => 'ContactFoo',
  669. 'step' => 'any'
  670. )),
  671. '/div'
  672. );
  673. $this->assertTags($result, $expected);
  674. $result = $this->Form->input('foo', array('step' => 0.5));
  675. $expected = array(
  676. 'div' => array('class' => 'input number'),
  677. 'label' => array('for' => 'ContactFoo'),
  678. 'Foo',
  679. '/label',
  680. array('input' => array(
  681. 'type' => 'number',
  682. 'name' => 'data[Contact][foo]',
  683. 'id' => 'ContactFoo',
  684. 'step' => '0.5'
  685. )),
  686. '/div'
  687. );
  688. $this->assertTags($result, $expected);
  689. }
  690. /**
  691. * Tests correct generation of number fields for integer fields
  692. *
  693. * @access public
  694. * @return void
  695. */
  696. public function testTextFieldTypeNumberGenerationForIntegers() {
  697. $model = ClassRegistry::getObject('Contact');
  698. $model->setSchema(array('foo' => array(
  699. 'type' => 'integer',
  700. 'null' => false,
  701. 'default' => null,
  702. 'length' => null
  703. )));
  704. $this->Form->create('Contact');
  705. $result = $this->Form->input('foo');
  706. $expected = array(
  707. 'div' => array('class' => 'input number'),
  708. 'label' => array('for' => 'ContactFoo'),
  709. 'Foo',
  710. '/label',
  711. array('input' => array(
  712. 'type' => 'number', 'name' => 'data[Contact][foo]',
  713. 'id' => 'ContactFoo'
  714. )),
  715. '/div'
  716. );
  717. $this->assertTags($result, $expected);
  718. }
  719. /**
  720. * testFormSecurityMultipleFields method
  721. *
  722. * Test secure() with multiple row form. Ensure hash is correct.
  723. *
  724. * @return void
  725. */
  726. public function testFormSecurityMultipleFields() {
  727. $key = 'testKey';
  728. $fields = array(
  729. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  730. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  731. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  732. );
  733. $this->Form->request['_Token'] = array('key' => $key);
  734. $result = $this->Form->secure($fields);
  735. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  736. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  737. $expected = array(
  738. 'div' => array('style' => 'display:none;'),
  739. array('input' => array(
  740. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  741. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  742. )),
  743. array('input' => array(
  744. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  745. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  746. )),
  747. '/div'
  748. );
  749. $this->assertTags($result, $expected);
  750. }
  751. /**
  752. * testFormSecurityMultipleSubmitButtons
  753. *
  754. * test form submit generation and ensure that _Token is only created on end()
  755. *
  756. * @return void
  757. */
  758. public function testFormSecurityMultipleSubmitButtons() {
  759. $key = 'testKey';
  760. $this->Form->request['_Token'] = array('key' => $key);
  761. $this->Form->create('Addresses');
  762. $this->Form->input('Address.title');
  763. $this->Form->input('Address.first_name');
  764. $result = $this->Form->submit('Save', array('name' => 'save'));
  765. $expected = array(
  766. 'div' => array('class' => 'submit'),
  767. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  768. '/div',
  769. );
  770. $this->assertTags($result, $expected);
  771. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  772. $expected = array(
  773. 'div' => array('class' => 'submit'),
  774. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  775. '/div',
  776. );
  777. $this->assertTags($result, $expected);
  778. $result = $this->Form->end(null);
  779. $expected = array(
  780. 'div' => array('style' => 'display:none;'),
  781. array('input' => array(
  782. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  783. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  784. )),
  785. array('input' => array(
  786. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  787. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  788. )),
  789. '/div'
  790. );
  791. $this->assertTags($result, $expected);
  792. }
  793. /**
  794. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  795. *
  796. * @return void
  797. */
  798. public function testSecurityButtonNestedNamed() {
  799. $key = 'testKey';
  800. $this->Form->request['_Token'] = array('key' => $key);
  801. $this->Form->create('Addresses');
  802. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  803. $result = $this->Form->unlockField();
  804. $this->assertEquals(array('Address.button'), $result);
  805. }
  806. /**
  807. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  808. *
  809. * @return void
  810. */
  811. public function testSecuritySubmitNestedNamed() {
  812. $key = 'testKey';
  813. $this->Form->request['_Token'] = array('key' => $key);
  814. $this->Form->create('Addresses');
  815. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  816. $result = $this->Form->unlockField();
  817. $this->assertEquals(array('Address.button'), $result);
  818. }
  819. /**
  820. * Test that the correct fields are unlocked for image submits with no names.
  821. *
  822. * @return void
  823. */
  824. public function testSecuritySubmitImageNoName() {
  825. $key = 'testKey';
  826. $this->Form->request['_Token'] = array('key' => $key);
  827. $this->Form->create('User');
  828. $result = $this->Form->submit('save.png');
  829. $expected = array(
  830. 'div' => array('class' => 'submit'),
  831. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  832. '/div'
  833. );
  834. $this->assertTags($result, $expected);
  835. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  836. }
  837. /**
  838. * Test that the correct fields are unlocked for image submits with names.
  839. *
  840. * @return void
  841. */
  842. public function testSecuritySubmitImageName() {
  843. $key = 'testKey';
  844. $this->Form->request['_Token'] = array('key' => $key);
  845. $this->Form->create('User');
  846. $result = $this->Form->submit('save.png', array('name' => 'test'));
  847. $expected = array(
  848. 'div' => array('class' => 'submit'),
  849. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  850. '/div'
  851. );
  852. $this->assertTags($result, $expected);
  853. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  854. }
  855. /**
  856. * testFormSecurityMultipleInputFields method
  857. *
  858. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  859. *
  860. * @return void
  861. */
  862. public function testFormSecurityMultipleInputFields() {
  863. $key = 'testKey';
  864. $this->Form->request['_Token'] = array('key' => $key);
  865. $this->Form->create('Addresses');
  866. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  867. $this->Form->input('Addresses.0.title');
  868. $this->Form->input('Addresses.0.first_name');
  869. $this->Form->input('Addresses.0.last_name');
  870. $this->Form->input('Addresses.0.address');
  871. $this->Form->input('Addresses.0.city');
  872. $this->Form->input('Addresses.0.phone');
  873. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  874. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  875. $this->Form->input('Addresses.1.title');
  876. $this->Form->input('Addresses.1.first_name');
  877. $this->Form->input('Addresses.1.last_name');
  878. $this->Form->input('Addresses.1.address');
  879. $this->Form->input('Addresses.1.city');
  880. $this->Form->input('Addresses.1.phone');
  881. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  882. $result = $this->Form->secure($this->Form->fields);
  883. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  884. $expected = array(
  885. 'div' => array('style' => 'display:none;'),
  886. array('input' => array(
  887. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  888. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  889. )),
  890. array('input' => array(
  891. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  892. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  893. )),
  894. '/div'
  895. );
  896. $this->assertTags($result, $expected);
  897. }
  898. /**
  899. * Test form security with Model.field.0 style inputs
  900. *
  901. * @return void
  902. */
  903. public function testFormSecurityArrayFields() {
  904. $key = 'testKey';
  905. $this->Form->request->params['_Token']['key'] = $key;
  906. $this->Form->create('Address');
  907. $this->Form->input('Address.primary.1');
  908. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  909. $this->Form->input('Address.secondary.1.0');
  910. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  911. }
  912. /**
  913. * testFormSecurityMultipleInputDisabledFields method
  914. *
  915. * test secure form generation with multiple records and disabled fields.
  916. *
  917. * @return void
  918. */
  919. public function testFormSecurityMultipleInputDisabledFields() {
  920. $key = 'testKey';
  921. $this->Form->request->params['_Token'] = array(
  922. 'key' => $key,
  923. 'unlockedFields' => array('first_name', 'address')
  924. );
  925. $this->Form->create();
  926. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  927. $this->Form->input('Addresses.0.title');
  928. $this->Form->input('Addresses.0.first_name');
  929. $this->Form->input('Addresses.0.last_name');
  930. $this->Form->input('Addresses.0.address');
  931. $this->Form->input('Addresses.0.city');
  932. $this->Form->input('Addresses.0.phone');
  933. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  934. $this->Form->input('Addresses.1.title');
  935. $this->Form->input('Addresses.1.first_name');
  936. $this->Form->input('Addresses.1.last_name');
  937. $this->Form->input('Addresses.1.address');
  938. $this->Form->input('Addresses.1.city');
  939. $this->Form->input('Addresses.1.phone');
  940. $result = $this->Form->secure($this->Form->fields);
  941. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  942. $expected = array(
  943. 'div' => array('style' => 'display:none;'),
  944. array('input' => array(
  945. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  946. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  947. )),
  948. array('input' => array(
  949. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  950. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  951. )),
  952. '/div'
  953. );
  954. $this->assertTags($result, $expected);
  955. }
  956. /**
  957. * testFormSecurityInputDisabledFields method
  958. *
  959. * Test single record form with disabled fields.
  960. *
  961. * @return void
  962. */
  963. public function testFormSecurityInputUnlockedFields() {
  964. $key = 'testKey';
  965. $this->Form->request['_Token'] = array(
  966. 'key' => $key,
  967. 'unlockedFields' => array('first_name', 'address')
  968. );
  969. $this->Form->create();
  970. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  971. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  972. $this->Form->input('Addresses.title');
  973. $this->Form->input('Addresses.first_name');
  974. $this->Form->input('Addresses.last_name');
  975. $this->Form->input('Addresses.address');
  976. $this->Form->input('Addresses.city');
  977. $this->Form->input('Addresses.phone');
  978. $result = $this->Form->fields;
  979. $expected = array(
  980. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  981. 'Addresses.city', 'Addresses.phone'
  982. );
  983. $this->assertEquals($expected, $result);
  984. $result = $this->Form->secure($expected);
  985. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  986. $expected = array(
  987. 'div' => array('style' => 'display:none;'),
  988. array('input' => array(
  989. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  990. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  991. )),
  992. array('input' => array(
  993. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  994. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  995. )),
  996. '/div'
  997. );
  998. $this->assertTags($result, $expected);
  999. }
  1000. /**
  1001. * test securing inputs with custom name attributes.
  1002. *
  1003. * @return void
  1004. */
  1005. public function testFormSecureWithCustomNameAttribute() {
  1006. $this->Form->request->params['_Token']['key'] = 'testKey';
  1007. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1008. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1009. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1010. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1011. }
  1012. /**
  1013. * testFormSecuredInput method
  1014. *
  1015. * Test generation of entire secure form, assertions made on input() output.
  1016. *
  1017. * @return void
  1018. */
  1019. public function testFormSecuredInput() {
  1020. $this->Form->request['_Token'] = array('key' => 'testKey');
  1021. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1022. $encoding = strtolower(Configure::read('App.encoding'));
  1023. $expected = array(
  1024. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1025. 'div' => array('style' => 'display:none;'),
  1026. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1027. array('input' => array(
  1028. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1029. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1030. )),
  1031. '/div'
  1032. );
  1033. $this->assertTags($result, $expected);
  1034. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1035. $expected = array(
  1036. 'div' => array('class' => 'input text'),
  1037. 'label' => array('for' => 'UserFormPublished'),
  1038. 'Published',
  1039. '/label',
  1040. array('input' => array(
  1041. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1042. 'id' => 'UserFormPublished'
  1043. )),
  1044. '/div'
  1045. );
  1046. $this->assertTags($result, $expected);
  1047. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1048. $expected = array(
  1049. 'div' => array('class' => 'input text'),
  1050. 'label' => array('for' => 'UserFormOther'),
  1051. 'Other',
  1052. '/label',
  1053. array('input' => array(
  1054. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1055. 'id' => 'UserFormOther'
  1056. )),
  1057. '/div'
  1058. );
  1059. $this->assertTags($result, $expected);
  1060. $result = $this->Form->hidden('UserForm.stuff');
  1061. $expected = array(
  1062. 'input' => array(
  1063. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1064. 'id' => 'UserFormStuff'
  1065. ));
  1066. $this->assertTags($result, $expected);
  1067. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1068. $expected = array('input' => array(
  1069. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1070. 'value' => '0', 'id' => 'UserFormHidden'
  1071. ));
  1072. $this->assertTags($result, $expected);
  1073. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1074. $expected = array(
  1075. 'div' => array('class' => 'input checkbox'),
  1076. array('input' => array(
  1077. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1078. 'value' => '0', 'id' => 'UserFormSomething_'
  1079. )),
  1080. array('input' => array(
  1081. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1082. 'value' => '1', 'id' => 'UserFormSomething'
  1083. )),
  1084. 'label' => array('for' => 'UserFormSomething'),
  1085. 'Something',
  1086. '/label',
  1087. '/div'
  1088. );
  1089. $this->assertTags($result, $expected);
  1090. $result = $this->Form->fields;
  1091. $expected = array(
  1092. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1093. 'UserForm.hidden' => '0', 'UserForm.something'
  1094. );
  1095. $this->assertEquals($expected, $result);
  1096. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1097. $result = $this->Form->secure($this->Form->fields);
  1098. $expected = array(
  1099. 'div' => array('style' => 'display:none;'),
  1100. array('input' => array(
  1101. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1102. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1103. )),
  1104. array('input' => array(
  1105. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1106. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1107. )),
  1108. '/div'
  1109. );
  1110. $this->assertTags($result, $expected);
  1111. }
  1112. /**
  1113. * Test secured inputs with custom names.
  1114. *
  1115. * @return void
  1116. */
  1117. public function testSecuredInputCustomName() {
  1118. $this->Form->request['_Token'] = array('key' => 'testKey');
  1119. $this->assertEquals(array(), $this->Form->fields);
  1120. $this->Form->input('text_input', array(
  1121. 'name' => 'data[Option][General.default_role]',
  1122. ));
  1123. $expected = array('Option.General.default_role');
  1124. $this->assertEquals($expected, $this->Form->fields);
  1125. $this->Form->input('select_box', array(
  1126. 'name' => 'data[Option][General.select_role]',
  1127. 'type' => 'select',
  1128. 'options' => array(1, 2),
  1129. ));
  1130. $expected = array('Option.General.default_role', 'Option.General.select_role');
  1131. $this->assertEquals($expected, $this->Form->fields);
  1132. }
  1133. /**
  1134. * Tests that the correct keys are added to the field hash index
  1135. *
  1136. * @return void
  1137. */
  1138. public function testFormSecuredFileInput() {
  1139. $this->Form->request['_Token'] = array('key' => 'testKey');
  1140. $this->assertEquals(array(), $this->Form->fields);
  1141. $this->Form->file('Attachment.file');
  1142. $expected = array(
  1143. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1144. 'Attachment.file.error', 'Attachment.file.size'
  1145. );
  1146. $this->assertEquals($expected, $this->Form->fields);
  1147. }
  1148. /**
  1149. * test that multiple selects keys are added to field hash
  1150. *
  1151. * @return void
  1152. */
  1153. public function testFormSecuredMultipleSelect() {
  1154. $this->Form->request['_Token'] = array('key' => 'testKey');
  1155. $this->assertEquals(array(), $this->Form->fields);
  1156. $options = array('1' => 'one', '2' => 'two');
  1157. $this->Form->select('Model.select', $options);
  1158. $expected = array('Model.select');
  1159. $this->assertEquals($expected, $this->Form->fields);
  1160. $this->Form->fields = array();
  1161. $this->Form->select('Model.select', $options, array('multiple' => true));
  1162. $this->assertEquals($expected, $this->Form->fields);
  1163. }
  1164. /**
  1165. * testFormSecuredRadio method
  1166. *
  1167. * @return void
  1168. */
  1169. public function testFormSecuredRadio() {
  1170. $this->Form->request['_Token'] = array('key' => 'testKey');
  1171. $this->assertEquals(array(), $this->Form->fields);
  1172. $options = array('1' => 'option1', '2' => 'option2');
  1173. $this->Form->radio('Test.test', $options);
  1174. $expected = array('Test.test');
  1175. $this->assertEquals($expected, $this->Form->fields);
  1176. }
  1177. /**
  1178. * Test that when disabled is in a list based attribute array it works.
  1179. *
  1180. * @return void
  1181. */
  1182. public function testFormSecuredAndDisabledNotAssoc() {
  1183. $this->Form->request['_Token'] = array('key' => 'testKey');
  1184. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1185. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1186. $this->Form->text('Model.text', array('disabled'));
  1187. $this->Form->textarea('Model.textarea', array('disabled'));
  1188. $this->Form->password('Model.password', array('disabled'));
  1189. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1190. $expected = array(
  1191. 'Model.radio' => ''
  1192. );
  1193. $this->assertEquals($expected, $this->Form->fields);
  1194. }
  1195. /**
  1196. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1197. * hashing.
  1198. *
  1199. * @return void
  1200. */
  1201. public function testFormSecuredAndDisabled() {
  1202. $this->Form->request['_Token'] = array('key' => 'testKey');
  1203. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1204. $this->Form->text('Model.text', array('disabled' => true));
  1205. $this->Form->password('Model.text', array('disabled' => true));
  1206. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1207. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1208. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1209. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1210. $this->Form->month('Model.month', array('disabled' => true));
  1211. $this->Form->day('Model.day', array('disabled' => true));
  1212. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1213. $this->Form->minute('Model.minute', array('disabled' => true));
  1214. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1215. $expected = array(
  1216. 'Model.radio' => ''
  1217. );
  1218. $this->assertEquals($expected, $this->Form->fields);
  1219. }
  1220. /**
  1221. * testDisableSecurityUsingForm method
  1222. *
  1223. * @return void
  1224. */
  1225. public function testDisableSecurityUsingForm() {
  1226. $this->Form->request['_Token'] = array(
  1227. 'key' => 'testKey',
  1228. 'disabledFields' => array()
  1229. );
  1230. $this->Form->create();
  1231. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1232. $this->Form->input('Addresses.title');
  1233. $this->Form->input('Addresses.first_name', array('secure' => false));
  1234. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1235. $this->Form->input('Addresses.zip', array(
  1236. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1237. ));
  1238. $result = $this->Form->fields;
  1239. $expected = array(
  1240. 'Addresses.id' => '123456', 'Addresses.title',
  1241. );
  1242. $this->assertEquals($expected, $result);
  1243. }
  1244. /**
  1245. * test disableField
  1246. *
  1247. * @return void
  1248. */
  1249. public function testUnlockFieldAddsToList() {
  1250. $this->Form->request['_Token'] = array(
  1251. 'key' => 'testKey',
  1252. 'unlockedFields' => array()
  1253. );
  1254. $this->Form->create('Contact');
  1255. $this->Form->unlockField('Contact.name');
  1256. $this->Form->text('Contact.name');
  1257. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1258. $this->assertEquals(array(), $this->Form->fields);
  1259. }
  1260. /**
  1261. * test unlockField removing from fields array.
  1262. *
  1263. * @return void
  1264. */
  1265. public function testUnlockFieldRemovingFromFields() {
  1266. $this->Form->request['_Token'] = array(
  1267. 'key' => 'testKey',
  1268. 'unlockedFields' => array()
  1269. );
  1270. $this->Form->create('Contact');
  1271. $this->Form->hidden('Contact.id', array('value' => 1));
  1272. $this->Form->text('Contact.name');
  1273. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1274. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1275. $this->Form->unlockField('Contact.name');
  1276. $this->Form->unlockField('Contact.id');
  1277. $this->assertEquals(array(), $this->Form->fields);
  1278. }
  1279. /**
  1280. * testTagIsInvalid method
  1281. *
  1282. * @return void
  1283. */
  1284. public function testTagIsInvalid() {
  1285. $Contact = ClassRegistry::getObject('Contact');
  1286. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1287. $this->Form->setEntity('Contact.0.email');
  1288. $result = $this->Form->tagIsInvalid();
  1289. $this->assertEquals($expected, $result);
  1290. $this->Form->setEntity('Contact.1.email');
  1291. $result = $this->Form->tagIsInvalid();
  1292. $this->assertFalse($result);
  1293. $this->Form->setEntity('Contact.0.name');
  1294. $result = $this->Form->tagIsInvalid();
  1295. $this->assertFalse($result);
  1296. }
  1297. /**
  1298. * Test tagIsInvalid with validation errors from a saveMany
  1299. *
  1300. * @return void
  1301. */
  1302. public function testTagIsInvalidSaveMany() {
  1303. $Contact = ClassRegistry::getObject('Contact');
  1304. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1305. $this->Form->create('Contact');
  1306. $this->Form->setEntity('0.email');
  1307. $result = $this->Form->tagIsInvalid();
  1308. $this->assertEquals($expected, $result);
  1309. $this->Form->setEntity('0.Contact.email');
  1310. $result = $this->Form->tagIsInvalid();
  1311. $this->assertEquals($expected, $result);
  1312. }
  1313. /**
  1314. * Test validation errors.
  1315. *
  1316. * @return void
  1317. */
  1318. public function testPasswordValidation() {
  1319. $Contact = ClassRegistry::getObject('Contact');
  1320. $Contact->validationErrors['password'] = array('Please provide a password');
  1321. $result = $this->Form->input('Contact.password');
  1322. $expected = array(
  1323. 'div' => array('class' => 'input password error'),
  1324. 'label' => array('for' => 'ContactPassword'),
  1325. 'Password',
  1326. '/label',
  1327. 'input' => array(
  1328. 'type' => 'password', 'name' => 'data[Contact][password]',
  1329. 'id' => 'ContactPassword', 'class' => 'form-error'
  1330. ),
  1331. array('div' => array('class' => 'error-message')),
  1332. 'Please provide a password',
  1333. '/div',
  1334. '/div'
  1335. );
  1336. $this->assertTags($result, $expected);
  1337. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1338. $expected = array(
  1339. 'div' => array('class' => 'input password error'),
  1340. 'label' => array('for' => 'ContactPassword'),
  1341. 'Password',
  1342. '/label',
  1343. 'input' => array(
  1344. 'type' => 'password', 'name' => 'data[Contact][password]',
  1345. 'id' => 'ContactPassword', 'class' => 'form-error'
  1346. ),
  1347. '/div'
  1348. );
  1349. $this->assertTags($result, $expected);
  1350. }
  1351. /**
  1352. * Test validation errors, when validation message is an empty string.
  1353. *
  1354. * @access public
  1355. * @return void
  1356. */
  1357. public function testEmptyErrorValidation() {
  1358. $this->Form->validationErrors['Contact']['password'] = '';
  1359. $result = $this->Form->input('Contact.password');
  1360. $expected = array(
  1361. 'div' => array('class' => 'input password error'),
  1362. 'label' => array('for' => 'ContactPassword'),
  1363. 'Password',
  1364. '/label',
  1365. 'input' => array(
  1366. 'type' => 'password', 'name' => 'data[Contact][password]',
  1367. 'id' => 'ContactPassword', 'class' => 'form-error'
  1368. ),
  1369. array('div' => array('class' => 'error-message')),
  1370. array(),
  1371. '/div',
  1372. '/div'
  1373. );
  1374. $this->assertTags($result, $expected);
  1375. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1376. $expected = array(
  1377. 'div' => array('class' => 'input password error'),
  1378. 'label' => array('for' => 'ContactPassword'),
  1379. 'Password',
  1380. '/label',
  1381. 'input' => array(
  1382. 'type' => 'password', 'name' => 'data[Contact][password]',
  1383. 'id' => 'ContactPassword', 'class' => 'form-error'
  1384. ),
  1385. '/div'
  1386. );
  1387. $this->assertTags($result, $expected);
  1388. }
  1389. /**
  1390. * Test validation errors, when calling input() overriding validation message by an empty string.
  1391. *
  1392. * @access public
  1393. * @return void
  1394. */
  1395. public function testEmptyInputErrorValidation() {
  1396. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1397. $result = $this->Form->input('Contact.password', array('error' => ''));
  1398. $expected = array(
  1399. 'div' => array('class' => 'input password error'),
  1400. 'label' => array('for' => 'ContactPassword'),
  1401. 'Password',
  1402. '/label',
  1403. 'input' => array(
  1404. 'type' => 'password', 'name' => 'data[Contact][password]',
  1405. 'id' => 'ContactPassword', 'class' => 'form-error'
  1406. ),
  1407. array('div' => array('class' => 'error-message')),
  1408. array(),
  1409. '/div',
  1410. '/div'
  1411. );
  1412. $this->assertTags($result, $expected);
  1413. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1414. $expected = array(
  1415. 'div' => array('class' => 'input password error'),
  1416. 'label' => array('for' => 'ContactPassword'),
  1417. 'Password',
  1418. '/label',
  1419. 'input' => array(
  1420. 'type' => 'password', 'name' => 'data[Contact][password]',
  1421. 'id' => 'ContactPassword', 'class' => 'form-error'
  1422. ),
  1423. '/div'
  1424. );
  1425. $this->assertTags($result, $expected);
  1426. }
  1427. /**
  1428. * testFormValidationAssociated method
  1429. *
  1430. * test display of form errors in conjunction with model::validates.
  1431. *
  1432. * @return void
  1433. */
  1434. public function testFormValidationAssociated() {
  1435. $this->UserForm = ClassRegistry::getObject('UserForm');
  1436. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1437. $data = array(
  1438. 'UserForm' => array('name' => 'user'),
  1439. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1440. );
  1441. $result = $this->UserForm->OpenidUrl->create($data);
  1442. $this->assertFalse(empty($result));
  1443. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1444. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1445. $encoding = strtolower(Configure::read('App.encoding'));
  1446. $expected = array(
  1447. 'form' => array(
  1448. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1449. 'accept-charset' => $encoding
  1450. ),
  1451. 'div' => array('style' => 'display:none;'),
  1452. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1453. '/div'
  1454. );
  1455. $this->assertTags($result, $expected);
  1456. $result = $this->Form->error(
  1457. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1458. );
  1459. $this->assertEquals('Error, not registered', $result);
  1460. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1461. }
  1462. /**
  1463. * testFormValidationAssociatedFirstLevel method
  1464. *
  1465. * test form error display with associated model.
  1466. *
  1467. * @return void
  1468. */
  1469. public function testFormValidationAssociatedFirstLevel() {
  1470. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1471. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1472. $data = array(
  1473. 'ValidateUser' => array('name' => 'mariano'),
  1474. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1475. );
  1476. $result = $this->ValidateUser->create($data);
  1477. $this->assertFalse(empty($result));
  1478. $this->assertFalse($this->ValidateUser->validates());
  1479. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1480. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1481. $encoding = strtolower(Configure::read('App.encoding'));
  1482. $expected = array(
  1483. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1484. 'div' => array('style' => 'display:none;'),
  1485. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1486. '/div'
  1487. );
  1488. $this->assertTags($result, $expected);
  1489. $result = $this->Form->error(
  1490. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1491. );
  1492. $this->assertEquals('Invalid email', $result);
  1493. $result = $this->Form->error(
  1494. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1495. );
  1496. $this->assertEquals('Invalid name', $result);
  1497. $result = $this->Form->error(
  1498. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1499. );
  1500. $this->assertEquals('Invalid city', $result);
  1501. unset($this->ValidateUser->ValidateProfile);
  1502. unset($this->ValidateUser);
  1503. }
  1504. /**
  1505. * testFormValidationAssociatedSecondLevel method
  1506. *
  1507. * test form error display with associated model.
  1508. *
  1509. * @return void
  1510. */
  1511. public function testFormValidationAssociatedSecondLevel() {
  1512. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1513. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1514. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1515. $data = array(
  1516. 'ValidateUser' => array('name' => 'mariano'),
  1517. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1518. 'ValidateItem' => array('name' => 'Item')
  1519. );
  1520. $result = $this->ValidateUser->create($data);
  1521. $this->assertFalse(empty($result));
  1522. $this->assertFalse($this->ValidateUser->validates());
  1523. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1524. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1525. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1526. $encoding = strtolower(Configure::read('App.encoding'));
  1527. $expected = array(
  1528. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1529. 'div' => array('style' => 'display:none;'),
  1530. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1531. '/div'
  1532. );
  1533. $this->assertTags($result, $expected);
  1534. $result = $this->Form->error(
  1535. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1536. );
  1537. $this->assertEquals('Invalid email', $result);
  1538. $result = $this->Form->error(
  1539. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1540. );
  1541. $this->assertEquals('Invalid name', $result);
  1542. $result = $this->Form->error(
  1543. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1544. );
  1545. $result = $this->Form->error(
  1546. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1547. );
  1548. $this->assertEquals('Invalid description', $result);
  1549. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1550. unset($this->ValidateUser->ValidateProfile);
  1551. unset($this->ValidateUser);
  1552. }
  1553. /**
  1554. * testFormValidationMultiRecord method
  1555. *
  1556. * test form error display with multiple records.
  1557. *
  1558. * @return void
  1559. */
  1560. public function testFormValidationMultiRecord() {
  1561. $Contact = ClassRegistry::getObject('Contact');
  1562. $Contact->validationErrors[2] = array(
  1563. 'name' => array('This field cannot be left blank')
  1564. );
  1565. $result = $this->Form->input('Contact.2.name');
  1566. $expected = array(
  1567. 'div' => array('class' => 'input text error'),
  1568. 'label' => array('for' => 'Contact2Name'),
  1569. 'Name',
  1570. '/label',
  1571. 'input' => array(
  1572. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1573. 'class' => 'form-error', 'maxlength' => 255
  1574. ),
  1575. array('div' => array('class' => 'error-message')),
  1576. 'This field cannot be left blank',
  1577. '/div',
  1578. '/div'
  1579. );
  1580. $this->assertTags($result, $expected);
  1581. }
  1582. /**
  1583. * testMultipleInputValidation method
  1584. *
  1585. * test multiple record form validation error display.
  1586. *
  1587. * @return void
  1588. */
  1589. public function testMultipleInputValidation() {
  1590. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1591. $Address->validationErrors[0] = array(
  1592. 'title' => array('This field cannot be empty'),
  1593. 'first_name' => array('This field cannot be empty')
  1594. );
  1595. $Address->validationErrors[1] = array(
  1596. 'last_name' => array('You must have a last name')
  1597. );
  1598. $this->Form->create();
  1599. $result = $this->Form->input('Address.0.title');
  1600. $expected = array(
  1601. 'div' => array('class'),
  1602. 'label' => array('for'),
  1603. 'preg:/[^<]+/',
  1604. '/label',
  1605. 'input' => array(
  1606. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1607. ),
  1608. array('div' => array('class' => 'error-message')),
  1609. 'This field cannot be empty',
  1610. '/div',
  1611. '/div'
  1612. );
  1613. $this->assertTags($result, $expected);
  1614. $result = $this->Form->input('Address.0.first_name');
  1615. $expected = array(
  1616. 'div' => array('class'),
  1617. 'label' => array('for'),
  1618. 'preg:/[^<]+/',
  1619. '/label',
  1620. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1621. array('div' => array('class' => 'error-message')),
  1622. 'This field cannot be empty',
  1623. '/div',
  1624. '/div'
  1625. );
  1626. $this->assertTags($result, $expected);
  1627. $result = $this->Form->input('Address.0.last_name');
  1628. $expected = array(
  1629. 'div' => array('class'),
  1630. 'label' => array('for'),
  1631. 'preg:/[^<]+/',
  1632. '/label',
  1633. 'input' => array('type' => 'text', 'name', 'id'),
  1634. '/div'
  1635. );
  1636. $this->assertTags($result, $expected);
  1637. $result = $this->Form->input('Address.1.last_name');
  1638. $expected = array(
  1639. 'div' => array('class'),
  1640. 'label' => array('for'),
  1641. 'preg:/[^<]+/',
  1642. '/label',
  1643. 'input' => array(
  1644. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1645. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1646. ),
  1647. array('div' => array('class' => 'error-message')),
  1648. 'You must have a last name',
  1649. '/div',
  1650. '/div'
  1651. );
  1652. $this->assertTags($result, $expected);
  1653. }
  1654. /**
  1655. * testInput method
  1656. *
  1657. * Test various incarnations of input().
  1658. *
  1659. * @return void
  1660. */
  1661. public function testInput() {
  1662. $result = $this->Form->input('ValidateUser.balance');
  1663. $expected = array(
  1664. 'div' => array('class'),
  1665. 'label' => array('for'),
  1666. 'Balance',
  1667. '/label',
  1668. 'input' => array('name', 'type' => 'number', 'id'),
  1669. '/div',
  1670. );
  1671. $this->assertTags($result, $expected);
  1672. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1673. $expected = array(
  1674. 'div' => array('class' => 'input email'),
  1675. 'label' => array('for' => 'custom'),
  1676. 'Email',
  1677. '/label',
  1678. array('input' => array(
  1679. 'type' => 'email', 'name' => 'data[Contact][email]',
  1680. 'id' => 'custom', 'maxlength' => 255
  1681. )),
  1682. '/div'
  1683. );
  1684. $this->assertTags($result, $expected);
  1685. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1686. $expected = array(
  1687. '<div',
  1688. 'label' => array('for' => 'ContactEmail'),
  1689. 'Email',
  1690. '/label',
  1691. array('input' => array(
  1692. 'type' => 'email', 'name' => 'data[Contact][email]',
  1693. 'id' => 'ContactEmail', 'maxlength' => 255
  1694. )),
  1695. '/div'
  1696. );
  1697. $this->assertTags($result, $expected);
  1698. $result = $this->Form->hidden('Contact.idontexist');
  1699. $expected = array('input' => array(
  1700. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1701. 'id' => 'ContactIdontexist'
  1702. ));
  1703. $this->assertTags($result, $expected);
  1704. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1705. $expected = array(
  1706. 'div' => array('class' => 'input text'),
  1707. 'label' => array('for' => 'ContactEmail'),
  1708. 'Email',
  1709. '/label',
  1710. array('input' => array(
  1711. 'type' => 'text', 'name' => 'data[Contact][email]',
  1712. 'id' => 'ContactEmail'
  1713. )),
  1714. '/div'
  1715. );
  1716. $this->assertTags($result, $expected);
  1717. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1718. $expected = array(
  1719. 'div' => array('class' => 'input text'),
  1720. 'label' => array('for' => 'Contact5Email'),
  1721. 'Email',
  1722. '/label',
  1723. array('input' => array(
  1724. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1725. 'id' => 'Contact5Email'
  1726. )),
  1727. '/div'
  1728. );
  1729. $this->assertTags($result, $expected);
  1730. $result = $this->Form->input('Contact.password');
  1731. $expected = array(
  1732. 'div' => array('class' => 'input password'),
  1733. 'label' => array('for' => 'ContactPassword'),
  1734. 'Password',
  1735. '/label',
  1736. array('input' => array(
  1737. 'type' => 'password', 'name' => 'data[Contact][password]',
  1738. 'id' => 'ContactPassword'
  1739. )),
  1740. '/div'
  1741. );
  1742. $this->assertTags($result, $expected);
  1743. $result = $this->Form->input('Contact.email', array(
  1744. 'type' => 'file', 'class' => 'textbox'
  1745. ));
  1746. $expected = array(
  1747. 'div' => array('class' => 'input file'),
  1748. 'label' => array('for' => 'ContactEmail'),
  1749. 'Email',
  1750. '/label',
  1751. array('input' => array(
  1752. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1753. 'id' => 'ContactEmail'
  1754. )),
  1755. '/div'
  1756. );
  1757. $this->assertTags($result, $expected);
  1758. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1759. $result = $this->Form->input('Contact.phone');
  1760. $expected = array(
  1761. 'div' => array('class' => 'input tel'),
  1762. 'label' => array('for' => 'ContactPhone'),
  1763. 'Phone',
  1764. '/label',
  1765. array('input' => array(
  1766. 'type' => 'tel', 'name' => 'data[Contact][phone]',
  1767. 'value' => 'Hello &amp; World &gt; weird chars',
  1768. 'id' => 'ContactPhone', 'maxlength' => 255
  1769. )),
  1770. '/div'
  1771. );
  1772. $this->assertTags($result, $expected);
  1773. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1774. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1775. $expected = array(
  1776. 'div' => array('class' => 'input text'),
  1777. 'label' => array('for' => 'myId'),
  1778. 'Field',
  1779. '/label',
  1780. 'input' => array(
  1781. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1782. 'value' => 'My value', 'id' => 'myId'
  1783. ),
  1784. '/div'
  1785. );
  1786. $this->assertTags($result, $expected);
  1787. unset($this->Form->request->data);
  1788. $Contact = ClassRegistry::getObject('Contact');
  1789. $Contact->validationErrors['field'] = array('Badness!');
  1790. $result = $this->Form->input('Contact.field');
  1791. $expected = array(
  1792. 'div' => array('class' => 'input text error'),
  1793. 'label' => array('for' => 'ContactField'),
  1794. 'Field',
  1795. '/label',
  1796. 'input' => array(
  1797. 'type' => 'text', 'name' => 'data[Contact][field]',
  1798. 'id' => 'ContactField', 'class' => 'form-error'
  1799. ),
  1800. array('div' => array('class' => 'error-message')),
  1801. 'Badness!',
  1802. '/div',
  1803. '/div'
  1804. );
  1805. $this->assertTags($result, $expected);
  1806. $result = $this->Form->input('Contact.field', array(
  1807. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1808. ));
  1809. $expected = array(
  1810. 'label' => array('for' => 'ContactField'),
  1811. 'Field',
  1812. '/label',
  1813. 'input' => array(
  1814. 'type' => 'text', 'name' => 'data[Contact][field]',
  1815. 'id' => 'ContactField', 'class' => 'form-error'
  1816. ),
  1817. array('span' => array('class' => 'error-message')),
  1818. 'Badness!',
  1819. '/span'
  1820. );
  1821. $this->assertTags($result, $expected);
  1822. $result = $this->Form->input('Contact.field', array(
  1823. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1824. ));
  1825. $expected = array(
  1826. 'div' => array('class' => 'input text error'),
  1827. 'label' => array('for' => 'ContactField'),
  1828. 'Field',
  1829. '/label',
  1830. 'input' => array(
  1831. 'type' => 'text', 'name' => 'data[Contact][field]',
  1832. 'id' => 'ContactField', 'class' => 'form-error'
  1833. ),
  1834. array('div' => array('class' => 'error')),
  1835. 'Badness!',
  1836. '/div'
  1837. );
  1838. $this->assertTags($result, $expected);
  1839. $result = $this->Form->input('Contact.field', array(
  1840. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1841. ));
  1842. $expected = array(
  1843. 'span' => array('class' => 'input text error'),
  1844. 'label' => array('for' => 'ContactField'),
  1845. 'Field',
  1846. '/label',
  1847. 'input' => array(
  1848. 'type' => 'text', 'name' => 'data[Contact][field]',
  1849. 'id' => 'ContactField', 'class' => 'form-error'
  1850. ),
  1851. 'Badness!',
  1852. '/span'
  1853. );
  1854. $this->assertTags($result, $expected);
  1855. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1856. $expected = array(
  1857. 'div' => array('class' => 'input text error'),
  1858. 'label' => array('for' => 'ContactField'),
  1859. 'Field',
  1860. '/label',
  1861. 'input' => array(
  1862. 'type' => 'text', 'name' => 'data[Contact][field]',
  1863. 'id' => 'ContactField', 'class' => 'form-error'
  1864. ),
  1865. 'A message to you, Rudy',
  1866. array('div' => array('class' => 'error-message')),
  1867. 'Badness!',
  1868. '/div',
  1869. '/div'
  1870. );
  1871. $this->assertTags($result, $expected);
  1872. $this->Form->setEntity(null);
  1873. $this->Form->setEntity('Contact.field');
  1874. $result = $this->Form->input('Contact.field', array(
  1875. 'after' => 'A message to you, Rudy', 'error' => false
  1876. ));
  1877. $expected = array(
  1878. 'div' => array('class' => 'input text'),
  1879. 'label' => array('for' => 'ContactField'),
  1880. 'Field',
  1881. '/label',
  1882. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1883. 'A message to you, Rudy',
  1884. '/div'
  1885. );
  1886. $this->assertTags($result, $expected);
  1887. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1888. $expected = array(
  1889. 'div' => array('class' => 'input text'),
  1890. 'label' => array('for' => 'ObjectField'),
  1891. 'Field',
  1892. '/label',
  1893. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1894. 'A message to you, Rudy',
  1895. '/div'
  1896. );
  1897. $this->assertTags($result, $expected);
  1898. $Contact->validationErrors['field'] = array('minLength');
  1899. $result = $this->Form->input('Contact.field', array(
  1900. 'error' => array(
  1901. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1902. 'maxLength' => 'login too large'
  1903. )
  1904. ));
  1905. $expected = array(
  1906. 'div' => array('class' => 'input text error'),
  1907. 'label' => array('for' => 'ContactField'),
  1908. 'Field',
  1909. '/label',
  1910. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1911. array('div' => array('class' => 'error-message')),
  1912. 'Le login doit contenir au moins 2 caractères',
  1913. '/div',
  1914. '/div'
  1915. );
  1916. $this->assertTags($result, $expected);
  1917. $Contact->validationErrors['field'] = array('maxLength');
  1918. $result = $this->Form->input('Contact.field', array(
  1919. 'error' => array(
  1920. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1921. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1922. 'maxLength' => 'login too large',
  1923. )
  1924. ));
  1925. $expected = array(
  1926. 'div' => array('class' => 'input text error'),
  1927. 'label' => array('for' => 'ContactField'),
  1928. 'Field',
  1929. '/label',
  1930. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1931. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1932. 'login too large',
  1933. '/span',
  1934. '/div'
  1935. );
  1936. $this->assertTags($result, $expected);
  1937. }
  1938. /**
  1939. * Test that inputs with 0 can be created.
  1940. *
  1941. * @return void
  1942. */
  1943. public function testInputZero() {
  1944. $this->Form->create('User');
  1945. $result = $this->Form->input('0');
  1946. $expected = array(
  1947. 'div' => array('class' => 'input text'),
  1948. 'label' => array('for' => 'User0'), '/label',
  1949. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  1950. '/div'
  1951. );
  1952. $this->assertTags($result, $expected);
  1953. }
  1954. /**
  1955. * test input() with checkbox creation
  1956. *
  1957. * @return void
  1958. */
  1959. public function testInputCheckbox() {
  1960. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1961. $expected = array(
  1962. 'div' => array('class' => 'input checkbox'),
  1963. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1964. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1965. '/div'
  1966. );
  1967. $this->assertTags($result, $expected);
  1968. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1969. $expected = array(
  1970. 'div' => array('class' => 'input checkbox'),
  1971. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1972. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1973. '/div'
  1974. );
  1975. $this->assertTags($result, $expected);
  1976. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1977. $expected = array(
  1978. 'div' => array('class' => 'input checkbox'),
  1979. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1980. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1981. '/div'
  1982. );
  1983. $this->assertTags($result, $expected);
  1984. $result = $this->Form->input('User.disabled', array(
  1985. 'label' => 'Disabled',
  1986. 'type' => 'checkbox',
  1987. 'data-foo' => 'disabled'
  1988. ));
  1989. $expected = array(
  1990. 'div' => array('class' => 'input checkbox'),
  1991. 'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
  1992. array('input' => array(
  1993. 'type' => 'checkbox',
  1994. 'name' => 'data[User][disabled]',
  1995. 'value' => '1',
  1996. 'id' => 'UserDisabled',
  1997. 'data-foo' => 'disabled'
  1998. )),
  1999. 'label' => array('for' => 'UserDisabled'),
  2000. 'Disabled',
  2001. '/label',
  2002. '/div'
  2003. );
  2004. $this->assertTags($result, $expected);
  2005. }
  2006. /**
  2007. * test form->input() with time types.
  2008. *
  2009. */
  2010. public function testInputTime() {
  2011. extract($this->dateRegex);
  2012. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2013. $result = explode(':', $result);
  2014. $this->assertRegExp('/option value="23"/', $result[0]);
  2015. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2016. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2017. $result = explode(':', $result);
  2018. $this->assertRegExp('/option value="23"/', $result[0]);
  2019. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2020. $result = $this->Form->input('Model.field', array(
  2021. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2022. ));
  2023. $result = explode(':', $result);
  2024. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2025. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2026. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2027. $result = $this->Form->input('Model.field', array(
  2028. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2029. ));
  2030. $result = explode(':', $result);
  2031. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2032. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2033. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2034. $result = $this->Form->input('prueba', array(
  2035. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  2036. 'maxYear' => date('Y') + 1 ,'interval' => 15
  2037. ));
  2038. $result = explode(':', $result);
  2039. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2040. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2041. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2042. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2043. $result = $this->Form->input('Random.start_time', array(
  2044. 'type' => 'time',
  2045. 'selected' => '18:15'
  2046. ));
  2047. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2048. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2049. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2050. $result = $this->Form->input('published', array('type' => 'time'));
  2051. $now = strtotime('now');
  2052. $this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
  2053. $now = strtotime('2013-03-09 00:42:21');
  2054. $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
  2055. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2056. $this->assertContains('<option value="42" selected="selected">42</option>', $result);
  2057. }
  2058. /**
  2059. * Test interval + selected near the hour roll over.
  2060. *
  2061. * @return void
  2062. */
  2063. public function testTimeSelectedWithInterval() {
  2064. $result = $this->Form->input('Model.start_time', array(
  2065. 'type' => 'time',
  2066. 'interval' => 15,
  2067. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2068. ));
  2069. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2070. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2071. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2072. $result = $this->Form->input('Model.start_time', array(
  2073. 'type' => 'time',
  2074. 'interval' => 15,
  2075. 'selected' => '2012-10-23 15:57:00'
  2076. ));
  2077. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2078. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2079. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2080. $result = $this->Form->input('Model.start_time', array(
  2081. 'timeFormat' => 24,
  2082. 'type' => 'time',
  2083. 'interval' => 15,
  2084. 'selected' => '15:57'
  2085. ));
  2086. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2087. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2088. $result = $this->Form->input('Model.start_time', array(
  2089. 'timeFormat' => 24,
  2090. 'type' => 'time',
  2091. 'interval' => 15,
  2092. 'selected' => '23:57'
  2093. ));
  2094. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2095. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2096. $result = $this->Form->input('Model.created', array(
  2097. 'timeFormat' => 24,
  2098. 'type' => 'datetime',
  2099. 'interval' => 15,
  2100. 'selected' => '2012-09-30 23:56'
  2101. ));
  2102. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2103. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2104. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2105. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2106. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2107. }
  2108. /**
  2109. * Test interval & timeFormat = 12
  2110. *
  2111. * @return void
  2112. */
  2113. public function testInputTimeWithIntervalAnd12HourFormat() {
  2114. $result = $this->Form->input('Model.start_time', array(
  2115. 'type' => 'time',
  2116. 'timeFormat' => 12,
  2117. 'interval' => 5,
  2118. 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
  2119. ));
  2120. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2121. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2122. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2123. $result = $this->Form->input('Model.start_time', array(
  2124. 'type' => 'time',
  2125. 'timeFormat' => '12',
  2126. 'interval' => 5,
  2127. 'selected' => '2013-04-19 16:30:00'
  2128. ));
  2129. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2130. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2131. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2132. $result = $this->Form->input('Model.start_time', array(
  2133. 'type' => 'time',
  2134. 'timeFormat' => '12',
  2135. 'interval' => 10,
  2136. 'selected' => '2013-05-19 00:33:00'
  2137. ));
  2138. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2139. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2140. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2141. $result = $this->Form->input('Model.start_time', array(
  2142. 'type' => 'time',
  2143. 'timeFormat' => '12',
  2144. 'interval' => 10,
  2145. 'selected' => '2013-05-19 13:33:00'
  2146. ));
  2147. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2148. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2149. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2150. $result = $this->Form->input('Model.start_time', array(
  2151. 'type' => 'time',
  2152. 'timeFormat' => '12',
  2153. 'interval' => 10,
  2154. 'selected' => '2013-05-19 01:33:00'
  2155. ));
  2156. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2157. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2158. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2159. }
  2160. /**
  2161. * test form->input() with datetime, date and time types
  2162. *
  2163. * @return void
  2164. */
  2165. public function testInputDatetime() {
  2166. extract($this->dateRegex);
  2167. $result = $this->Form->input('prueba', array(
  2168. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2169. 'maxYear' => date('Y') + 1, 'interval' => 15
  2170. ));
  2171. $result = explode(':', $result);
  2172. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2173. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2174. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2175. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2176. //related to ticket #5013
  2177. $result = $this->Form->input('Contact.date', array(
  2178. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2179. ));
  2180. $this->assertRegExp('/class="customClass"/', $result);
  2181. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2182. $result = $this->Form->input('Contact.date', array(
  2183. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2184. ));
  2185. $this->assertRegExp('/id="customIdDay"/', $result);
  2186. $this->assertRegExp('/id="customIdMonth"/', $result);
  2187. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2188. $result = $this->Form->input('Model.field', array(
  2189. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2190. ));
  2191. $this->assertRegExp('/id="customIDDay"/', $result);
  2192. $this->assertRegExp('/id="customIDHour"/', $result);
  2193. $result = explode('</select><select', $result);
  2194. $result = explode(':', $result[1]);
  2195. $this->assertRegExp('/option value="23"/', $result[0]);
  2196. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2197. $result = $this->Form->input('Model.field', array(
  2198. 'type' => 'datetime', 'timeFormat' => 12
  2199. ));
  2200. $result = explode('</select><select', $result);
  2201. $result = explode(':', $result[1]);
  2202. $this->assertRegExp('/option value="12"/', $result[0]);
  2203. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2204. $this->Form->request->data = array('Contact' => array('created' => null));
  2205. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2206. $expected = array(
  2207. 'div' => array('class' => 'input date'),
  2208. 'label' => array('for' => 'ContactCreatedMonth'),
  2209. 'Created',
  2210. '/label',
  2211. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2212. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2213. $monthsRegex,
  2214. '/select', '-',
  2215. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2216. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2217. $daysRegex,
  2218. '/select', '-',
  2219. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2220. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2221. $yearsRegex,
  2222. '/select',
  2223. '/div'
  2224. );
  2225. $this->assertTags($result, $expected);
  2226. $this->Form->request->data = array('Contact' => array('created' => null));
  2227. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2228. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2229. $this->Form->request->data = array('Contact' => array('created' => null));
  2230. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2231. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2232. $result = $this->Form->input('Contact.created', array(
  2233. 'type' => 'date',
  2234. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2235. 'timeFormat' => 'NONE'
  2236. ));
  2237. $this->assertRegExp('/for\="created-month"/', $result);
  2238. }
  2239. /**
  2240. * Test generating checkboxes in a loop.
  2241. *
  2242. * @return void
  2243. */
  2244. public function testInputCheckboxesInLoop() {
  2245. for ($i = 1; $i < 5; $i++) {
  2246. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2247. $expected = array(
  2248. 'div' => array('class' => 'input checkbox'),
  2249. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2250. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2251. 'label' => array('for' => "Contact{$i}Email"),
  2252. 'Email',
  2253. '/label',
  2254. '/div'
  2255. );
  2256. $this->assertTags($result, $expected);
  2257. }
  2258. }
  2259. /**
  2260. * Test generating checkboxes with disabled elements.
  2261. *
  2262. * @return void
  2263. */
  2264. public function testInputCheckboxWithDisabledElements() {
  2265. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2266. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2267. $expected = array(
  2268. array('div' => array('class' => 'input select')),
  2269. array('label' => array('for' => "ContactMultiple")),
  2270. 'Multiple',
  2271. '/label',
  2272. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2273. array('div' => array('class' => 'checkbox')),
  2274. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2275. array('label' => array('for' => "ContactMultiple1")),
  2276. 'One',
  2277. '/label',
  2278. '/div',
  2279. array('div' => array('class' => 'checkbox')),
  2280. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2281. array('label' => array('for' => "ContactMultiple2")),
  2282. 'Two',
  2283. '/label',
  2284. '/div',
  2285. array('div' => array('class' => 'checkbox')),
  2286. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2287. array('label' => array('for' => "ContactMultiple3")),
  2288. 'Three',
  2289. '/label',
  2290. '/div',
  2291. '/div'
  2292. );
  2293. $this->assertTags($result, $expected);
  2294. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2295. $this->assertTags($result, $expected);
  2296. $disabled = array('2', 3);
  2297. $expected = array(
  2298. array('div' => array('class' => 'input select')),
  2299. array('label' => array('for' => "ContactMultiple")),
  2300. 'Multiple',
  2301. '/label',
  2302. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2303. array('div' => array('class' => 'checkbox')),
  2304. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2305. array('label' => array('for' => "ContactMultiple1")),
  2306. 'One',
  2307. '/label',
  2308. '/div',
  2309. array('div' => array('class' => 'checkbox')),
  2310. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2311. array('label' => array('for' => "ContactMultiple2")),
  2312. 'Two',
  2313. '/label',
  2314. '/div',
  2315. array('div' => array('class' => 'checkbox')),
  2316. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2317. array('label' => array('for' => "ContactMultiple3")),
  2318. 'Three',
  2319. '/label',
  2320. '/div',
  2321. '/div'
  2322. );
  2323. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2324. $this->assertTags($result, $expected);
  2325. // make sure 50 does only disable 50, and not 50f5c0cf
  2326. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2327. $disabled = array(50);
  2328. $expected = array(
  2329. array('div' => array('class' => 'input select')),
  2330. array('label' => array('for' => "ContactMultiple")),
  2331. 'Multiple',
  2332. '/label',
  2333. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2334. array('div' => array('class' => 'checkbox')),
  2335. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2336. array('label' => array('for' => "ContactMultiple50")),
  2337. 'Fifty',
  2338. '/label',
  2339. '/div',
  2340. array('div' => array('class' => 'checkbox')),
  2341. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2342. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2343. 'Stringy',
  2344. '/label',
  2345. '/div',
  2346. '/div'
  2347. );
  2348. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2349. $this->assertTags($result, $expected);
  2350. }
  2351. /**
  2352. * test input name with leading integer, ensure attributes are generated correctly.
  2353. *
  2354. * @return void
  2355. */
  2356. public function testInputWithLeadingInteger() {
  2357. $result = $this->Form->text('0.Node.title');
  2358. $expected = array(
  2359. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2360. );
  2361. $this->assertTags($result, $expected);
  2362. }
  2363. /**
  2364. * test form->input() with select type inputs.
  2365. *
  2366. * @return void
  2367. */
  2368. public function testInputSelectType() {
  2369. $result = $this->Form->input('email', array(
  2370. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2371. );
  2372. $expected = array(
  2373. 'div' => array('class' => 'input select'),
  2374. 'label' => array('for' => 'email'),
  2375. 'Email',
  2376. '/label',
  2377. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2378. array('option' => array('value' => '')),
  2379. '/option',
  2380. array('option' => array('value' => 'è')),
  2381. 'Firést',
  2382. '/option',
  2383. array('option' => array('value' => 'é')),
  2384. 'Secoènd',
  2385. '/option',
  2386. '/select',
  2387. '/div'
  2388. );
  2389. $this->assertTags($result, $expected);
  2390. $result = $this->Form->input('email', array(
  2391. 'options' => array('First', 'Second'), 'empty' => true)
  2392. );
  2393. $expected = array(
  2394. 'div' => array('class' => 'input select'),
  2395. 'label' => array('for' => 'email'),
  2396. 'Email',
  2397. '/label',
  2398. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2399. array('option' => array('value' => '')),
  2400. '/option',
  2401. array('option' => array('value' => '0')),
  2402. 'First',
  2403. '/option',
  2404. array('option' => array('value' => '1')),
  2405. 'Second',
  2406. '/option',
  2407. '/select',
  2408. '/div'
  2409. );
  2410. $this->assertTags($result, $expected);
  2411. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2412. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2413. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2414. $expected = array(
  2415. 'div' => array('class' => 'input select'),
  2416. 'label' => array('for' => 'ModelUserId'),
  2417. 'User',
  2418. '/label',
  2419. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2420. array('option' => array('value' => '')),
  2421. '/option',
  2422. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2423. 'good',
  2424. '/option',
  2425. array('option' => array('value' => 'other')),
  2426. 'bad',
  2427. '/option',
  2428. '/select',
  2429. '/div'
  2430. );
  2431. $this->assertTags($result, $expected);
  2432. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2433. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2434. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2435. $expected = array(
  2436. 'div' => array('class' => 'input select'),
  2437. 'label' => array('for' => 'ThingUserId'),
  2438. 'User',
  2439. '/label',
  2440. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2441. array('option' => array('value' => '')),
  2442. 'Some Empty',
  2443. '/option',
  2444. array('option' => array('value' => 'value')),
  2445. 'good',
  2446. '/option',
  2447. array('option' => array('value' => 'other')),
  2448. 'bad',
  2449. '/option',
  2450. '/select',
  2451. '/div'
  2452. );
  2453. $this->assertTags($result, $expected);
  2454. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2455. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2456. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2457. $expected = array(
  2458. 'div' => array('class' => 'input select'),
  2459. 'label' => array('for' => 'ThingUserId'),
  2460. 'User',
  2461. '/label',
  2462. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2463. array('option' => array('value' => '')),
  2464. 'Some Empty',
  2465. '/option',
  2466. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2467. 'good',
  2468. '/option',
  2469. array('option' => array('value' => 'other')),
  2470. 'bad',
  2471. '/option',
  2472. '/select',
  2473. '/div'
  2474. );
  2475. $this->assertTags($result, $expected);
  2476. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2477. $this->Form->request->data = array('User' => array('User' => array('value')));
  2478. $result = $this->Form->input('User.User', array('empty' => true));
  2479. $expected = array(
  2480. 'div' => array('class' => 'input select'),
  2481. 'label' => array('for' => 'UserUser'),
  2482. 'User',
  2483. '/label',
  2484. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2485. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2486. array('option' => array('value' => '')),
  2487. '/option',
  2488. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2489. 'good',
  2490. '/option',
  2491. array('option' => array('value' => 'other')),
  2492. 'bad',
  2493. '/option',
  2494. '/select',
  2495. '/div'
  2496. );
  2497. $this->assertTags($result, $expected);
  2498. $this->Form->data = array();
  2499. $result = $this->Form->input('Publisher.id', array(
  2500. 'label' => 'Publisher',
  2501. 'type' => 'select',
  2502. 'multiple' => 'checkbox',
  2503. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2504. ));
  2505. $expected = array(
  2506. array('div' => array('class' => 'input select')),
  2507. array('label' => array('for' => 'PublisherId')),
  2508. 'Publisher',
  2509. '/label',
  2510. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2511. array('div' => array('class' => 'checkbox')),
  2512. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2513. array('label' => array('for' => 'PublisherIdValue1')),
  2514. 'Label 1',
  2515. '/label',
  2516. '/div',
  2517. array('div' => array('class' => 'checkbox')),
  2518. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2519. array('label' => array('for' => 'PublisherIdValue2')),
  2520. 'Label 2',
  2521. '/label',
  2522. '/div',
  2523. '/div'
  2524. );
  2525. $this->assertTags($result, $expected);
  2526. }
  2527. /**
  2528. * test that input() and a non standard primary key makes a hidden input by default.
  2529. *
  2530. * @return void
  2531. */
  2532. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2533. $this->Form->create('User');
  2534. $this->Form->fieldset = array(
  2535. 'User' => array(
  2536. 'fields' => array(
  2537. 'model_id' => array('type' => 'integer')
  2538. ),
  2539. 'validates' => array(),
  2540. 'key' => 'model_id'
  2541. )
  2542. );
  2543. $result = $this->Form->input('model_id');
  2544. $expected = array(
  2545. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2546. );
  2547. $this->assertTags($result, $expected);
  2548. }
  2549. /**
  2550. * test that overriding the magic select type widget is possible
  2551. *
  2552. * @return void
  2553. */
  2554. public function testInputOverridingMagicSelectType() {
  2555. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2556. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2557. $expected = array(
  2558. 'div' => array('class' => 'input text'),
  2559. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2560. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2561. '/div'
  2562. );
  2563. $this->assertTags($result, $expected);
  2564. //Check that magic types still work for plural/singular vars
  2565. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2566. $result = $this->Form->input('Model.type');
  2567. $expected = array(
  2568. 'div' => array('class' => 'input select'),
  2569. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2570. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2571. array('option' => array('value' => 'value')), 'good', '/option',
  2572. array('option' => array('value' => 'other')), 'bad', '/option',
  2573. '/select',
  2574. '/div'
  2575. );
  2576. $this->assertTags($result, $expected);
  2577. }
  2578. /**
  2579. * Test that magic input() selects are created for type=number
  2580. *
  2581. * @return void
  2582. */
  2583. public function testInputMagicSelectForTypeNumber() {
  2584. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2585. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2586. $result = $this->Form->input('ValidateUser.balance');
  2587. $expected = array(
  2588. 'div' => array('class' => 'input select'),
  2589. 'label' => array('for' => 'ValidateUserBalance'),
  2590. 'Balance',
  2591. '/label',
  2592. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2593. array('option' => array('value' => '0')),
  2594. 'nothing',
  2595. '/option',
  2596. array('option' => array('value' => '1', 'selected' => 'selected')),
  2597. 'some',
  2598. '/option',
  2599. array('option' => array('value' => '100')),
  2600. 'a lot',
  2601. '/option',
  2602. '/select',
  2603. '/div'
  2604. );
  2605. $this->assertTags($result, $expected);
  2606. }
  2607. /**
  2608. * Test that magic input() selects can easily be converted into radio types without error.
  2609. *
  2610. * @return void
  2611. */
  2612. public function testInputMagicSelectChangeToRadio() {
  2613. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2614. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2615. $this->assertRegExp('/input type="radio"/', $result);
  2616. }
  2617. /**
  2618. * fields with the same name as the model should work.
  2619. *
  2620. * @return void
  2621. */
  2622. public function testInputWithMatchingFieldAndModelName() {
  2623. $this->Form->create('User');
  2624. $this->Form->fieldset = array(
  2625. 'User' => array(
  2626. 'fields' => array(
  2627. 'User' => array('type' => 'text')
  2628. ),
  2629. 'validates' => array(),
  2630. 'key' => 'id'
  2631. )
  2632. );
  2633. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2634. $result = $this->Form->input('User', array('type' => 'text'));
  2635. $expected = array(
  2636. 'div' => array('class' => 'input text'),
  2637. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2638. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2639. '/div'
  2640. );
  2641. $this->assertTags($result, $expected);
  2642. }
  2643. /**
  2644. * testFormInputs method
  2645. *
  2646. * test correct results from form::inputs().
  2647. *
  2648. * @return void
  2649. */
  2650. public function testFormInputs() {
  2651. $this->Form->create('Contact');
  2652. $result = $this->Form->inputs('The Legend');
  2653. $expected = array(
  2654. '<fieldset',
  2655. '<legend',
  2656. 'The Legend',
  2657. '/legend',
  2658. '*/fieldset',
  2659. );
  2660. $this->assertTags($result, $expected);
  2661. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2662. $expected = array(
  2663. 'fieldset' => array('class' => 'classy-stuff'),
  2664. '<legend',
  2665. 'Field of Dreams',
  2666. '/legend',
  2667. '*/fieldset'
  2668. );
  2669. $this->assertTags($result, $expected);
  2670. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2671. $this->assertTags($result, $expected);
  2672. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2673. $this->assertTags($result, $expected);
  2674. $this->Form->create('Contact');
  2675. $this->Form->request['prefix'] = 'admin';
  2676. $this->Form->request['action'] = 'admin_edit';
  2677. $result = $this->Form->inputs();
  2678. $expected = array(
  2679. '<fieldset',
  2680. '<legend',
  2681. 'Edit Contact',
  2682. '/legend',
  2683. '*/fieldset',
  2684. );
  2685. $this->assertTags($result, $expected);
  2686. $this->Form->create('Contact');
  2687. $result = $this->Form->inputs(false);
  2688. $expected = array(
  2689. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2690. array('div' => array('class' => 'input text')),
  2691. '*/div',
  2692. array('div' => array('class' => 'input email')),
  2693. '*/div',
  2694. array('div' => array('class' => 'input tel')),
  2695. '*/div',
  2696. array('div' => array('class' => 'input password')),
  2697. '*/div',
  2698. array('div' => array('class' => 'input date')),
  2699. '*/div',
  2700. array('div' => array('class' => 'input date')),
  2701. '*/div',
  2702. array('div' => array('class' => 'input datetime')),
  2703. '*/div',
  2704. array('div' => array('class' => 'input number')),
  2705. '*/div',
  2706. array('div' => array('class' => 'input select')),
  2707. '*/div',
  2708. );
  2709. $this->assertTags($result, $expected);
  2710. $this->Form->create('Contact');
  2711. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2712. $expected = array(
  2713. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2714. array('div' => array('class' => 'input text')),
  2715. '*/div',
  2716. array('div' => array('class' => 'input email')),
  2717. '*/div',
  2718. array('div' => array('class' => 'input tel')),
  2719. '*/div',
  2720. array('div' => array('class' => 'input password')),
  2721. '*/div',
  2722. array('div' => array('class' => 'input date')),
  2723. '*/div',
  2724. array('div' => array('class' => 'input date')),
  2725. '*/div',
  2726. array('div' => array('class' => 'input datetime')),
  2727. '*/div',
  2728. array('div' => array('class' => 'input number')),
  2729. '*/div',
  2730. array('div' => array('class' => 'input select')),
  2731. '*/div',
  2732. );
  2733. $this->assertTags($result, $expected);
  2734. $this->Form->create('Contact');
  2735. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2736. $this->assertTags($result, $expected);
  2737. $this->Form->create('Contact');
  2738. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2739. $expected = array(
  2740. 'fieldset' => array(),
  2741. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2742. array('div' => array('class' => 'input text')),
  2743. '*/div',
  2744. array('div' => array('class' => 'input email')),
  2745. '*/div',
  2746. array('div' => array('class' => 'input tel')),
  2747. '*/div',
  2748. array('div' => array('class' => 'input password')),
  2749. '*/div',
  2750. array('div' => array('class' => 'input date')),
  2751. '*/div',
  2752. array('div' => array('class' => 'input date')),
  2753. '*/div',
  2754. array('div' => array('class' => 'input datetime')),
  2755. '*/div',
  2756. array('div' => array('class' => 'input number')),
  2757. '*/div',
  2758. array('div' => array('class' => 'input select')),
  2759. '*/div',
  2760. '/fieldset'
  2761. );
  2762. $this->assertTags($result, $expected);
  2763. $this->Form->create('Contact');
  2764. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2765. $expected = array(
  2766. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2767. array('div' => array('class' => 'input text')),
  2768. '*/div',
  2769. array('div' => array('class' => 'input email')),
  2770. '*/div',
  2771. array('div' => array('class' => 'input tel')),
  2772. '*/div',
  2773. array('div' => array('class' => 'input password')),
  2774. '*/div',
  2775. array('div' => array('class' => 'input date')),
  2776. '*/div',
  2777. array('div' => array('class' => 'input date')),
  2778. '*/div',
  2779. array('div' => array('class' => 'input datetime')),
  2780. '*/div',
  2781. array('div' => array('class' => 'input number')),
  2782. '*/div',
  2783. array('div' => array('class' => 'input select')),
  2784. '*/div',
  2785. );
  2786. $this->assertTags($result, $expected);
  2787. $this->Form->create('Contact');
  2788. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2789. $this->assertTags($result, $expected);
  2790. $this->Form->create('Contact');
  2791. $result = $this->Form->inputs('Hello');
  2792. $expected = array(
  2793. 'fieldset' => array(),
  2794. 'legend' => array(),
  2795. 'Hello',
  2796. '/legend',
  2797. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2798. array('div' => array('class' => 'input text')),
  2799. '*/div',
  2800. array('div' => array('class' => 'input email')),
  2801. '*/div',
  2802. array('div' => array('class' => 'input tel')),
  2803. '*/div',
  2804. array('div' => array('class' => 'input password')),
  2805. '*/div',
  2806. array('div' => array('class' => 'input date')),
  2807. '*/div',
  2808. array('div' => array('class' => 'input date')),
  2809. '*/div',
  2810. array('div' => array('class' => 'input datetime')),
  2811. '*/div',
  2812. array('div' => array('class' => 'input number')),
  2813. '*/div',
  2814. array('div' => array('class' => 'input select')),
  2815. '*/div',
  2816. '/fieldset'
  2817. );
  2818. $this->assertTags($result, $expected);
  2819. $this->Form->create('Contact');
  2820. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2821. $expected = array(
  2822. 'fieldset' => array(),
  2823. 'legend' => array(),
  2824. 'Hello',
  2825. '/legend',
  2826. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2827. array('div' => array('class' => 'input text')),
  2828. '*/div',
  2829. array('div' => array('class' => 'input email')),
  2830. '*/div',
  2831. array('div' => array('class' => 'input tel')),
  2832. '*/div',
  2833. array('div' => array('class' => 'input password')),
  2834. '*/div',
  2835. array('div' => array('class' => 'input date')),
  2836. '*/div',
  2837. array('div' => array('class' => 'input date')),
  2838. '*/div',
  2839. array('div' => array('class' => 'input datetime')),
  2840. '*/div',
  2841. array('div' => array('class' => 'input number')),
  2842. '*/div',
  2843. array('div' => array('class' => 'input select')),
  2844. '*/div',
  2845. '/fieldset'
  2846. );
  2847. $this->assertTags($result, $expected);
  2848. $this->Form->create('Contact');
  2849. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2850. $this->assertTags($result, $expected);
  2851. $this->Form->end();
  2852. $this->Form->create(false);
  2853. $expected = array(
  2854. 'fieldset' => array(),
  2855. array('div' => array('class' => 'input text')),
  2856. 'label' => array('for' => 'foo'),
  2857. 'Foo',
  2858. '/label',
  2859. 'input' => array('type' => 'text', 'name' => 'data[foo]', 'id' => 'foo'),
  2860. '*/div',
  2861. '/fieldset'
  2862. );
  2863. $result = $this->Form->inputs(
  2864. array('foo' => array('type' => 'text')),
  2865. array(),
  2866. array('legend' => false)
  2867. );
  2868. $this->assertTags($result, $expected);
  2869. }
  2870. /**
  2871. * Tests inputs() works with plugin models
  2872. *
  2873. * @return void
  2874. */
  2875. public function testInputsPluginModel() {
  2876. $this->loadFixtures('Post');
  2877. App::build(array(
  2878. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  2879. ));
  2880. CakePlugin::load('TestPlugin');
  2881. $this->Form->request['models'] = array(
  2882. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  2883. );
  2884. $this->Form->create('TestPlugin.TestPluginPost');
  2885. $result = $this->Form->inputs();
  2886. $this->assertContains('data[TestPluginPost][id]', $result);
  2887. $this->assertContains('data[TestPluginPost][author_id]', $result);
  2888. $this->assertContains('data[TestPluginPost][title]', $result);
  2889. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  2890. $this->assertFalse(ClassRegistry::isKeySet('TestPlugin'));
  2891. $this->assertEquals('TestPluginPost', $this->Form->model());
  2892. }
  2893. /**
  2894. * testSelectAsCheckbox method
  2895. *
  2896. * test multi-select widget with checkbox formatting.
  2897. *
  2898. * @return void
  2899. */
  2900. public function testSelectAsCheckbox() {
  2901. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2902. $expected = array(
  2903. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2904. array('div' => array('class' => 'checkbox')),
  2905. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2906. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2907. 'first',
  2908. '/label',
  2909. '/div',
  2910. array('div' => array('class' => 'checkbox')),
  2911. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2912. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2913. 'second',
  2914. '/label',
  2915. '/div',
  2916. array('div' => array('class' => 'checkbox')),
  2917. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2918. array('label' => array('for' => 'ModelMultiField2')),
  2919. 'third',
  2920. '/label',
  2921. '/div',
  2922. );
  2923. $this->assertTags($result, $expected);
  2924. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2925. $expected = array(
  2926. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2927. array('div' => array('class' => 'checkbox')),
  2928. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2929. array('label' => array('for' => 'ModelMultiField12')),
  2930. 'half',
  2931. '/label',
  2932. '/div',
  2933. );
  2934. $this->assertTags($result, $expected);
  2935. }
  2936. /**
  2937. * testLabel method
  2938. *
  2939. * test label generation.
  2940. *
  2941. * @return void
  2942. */
  2943. public function testLabel() {
  2944. $this->Form->text('Person.name');
  2945. $result = $this->Form->label();
  2946. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2947. $this->Form->text('Person.name');
  2948. $result = $this->Form->label();
  2949. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2950. $result = $this->Form->label('Person.first_name');
  2951. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2952. $result = $this->Form->label('Person.first_name', 'Your first name');
  2953. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2954. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2955. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2956. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2957. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2958. $result = $this->Form->label('Person.first_name', '');
  2959. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2960. $result = $this->Form->label('Person.2.name', '');
  2961. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2962. }
  2963. /**
  2964. * testTextbox method
  2965. *
  2966. * test textbox element generation
  2967. *
  2968. * @return void
  2969. */
  2970. public function testTextbox() {
  2971. $result = $this->Form->text('Model.field');
  2972. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2973. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2974. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2975. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2976. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2977. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2978. $result = $this->Form->text('Model.text');
  2979. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2980. $Contact = ClassRegistry::getObject('Contact');
  2981. $Contact->validationErrors['text'] = array(true);
  2982. $this->Form->request->data['Contact']['text'] = 'test';
  2983. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2984. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2985. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2986. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2987. $expected = array(
  2988. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2989. );
  2990. $this->assertTags($result, $expected);
  2991. }
  2992. /**
  2993. * testDefaultValue method
  2994. *
  2995. * Test default value setting
  2996. *
  2997. * @return void
  2998. */
  2999. public function testDefaultValue() {
  3000. $this->Form->request->data['Model']['field'] = 'test';
  3001. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3002. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  3003. unset($this->Form->request->data['Model']['field']);
  3004. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3005. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  3006. }
  3007. /**
  3008. * testCheckboxDefaultValue method
  3009. *
  3010. * Test default value setting on checkbox() method
  3011. *
  3012. * @return void
  3013. */
  3014. public function testCheckboxDefaultValue() {
  3015. $this->Form->request->data['Model']['field'] = false;
  3016. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3017. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3018. unset($this->Form->request->data['Model']['field']);
  3019. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3020. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3021. $this->Form->request->data['Model']['field'] = true;
  3022. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3023. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3024. unset($this->Form->request->data['Model']['field']);
  3025. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3026. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3027. }
  3028. /**
  3029. * testError method
  3030. *
  3031. * Test field error generation
  3032. *
  3033. * @return void
  3034. */
  3035. public function testError() {
  3036. $Contact = ClassRegistry::getObject('Contact');
  3037. $Contact->validationErrors['field'] = array(1);
  3038. $result = $this->Form->error('Contact.field');
  3039. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  3040. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3041. $this->assertEquals('Error in field Field', $result);
  3042. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3043. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3044. $this->assertEquals('This field contains invalid input', $result);
  3045. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3046. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  3047. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  3048. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  3049. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  3050. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  3051. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3052. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  3053. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3054. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  3055. $this->assertEquals('<strong>Badness!</strong>', $result);
  3056. $Contact->validationErrors['field'] = array("email");
  3057. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  3058. $expected = array(
  3059. 'div' => array('class' => 'field-error'),
  3060. 'No good!',
  3061. '/div'
  3062. );
  3063. $this->assertTags($result, $expected);
  3064. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  3065. $result = $this->Form->error('Contact.field', array(
  3066. 'notEmpty' => 'Cannot be empty',
  3067. 'email' => 'No good!'
  3068. ));
  3069. $expected = array(
  3070. 'div' => array('class' => 'error-message'),
  3071. 'ul' => array(),
  3072. '<li', 'Cannot be empty', '/li',
  3073. '<li', 'No good!', '/li',
  3074. '<li', 'Something else', '/li',
  3075. '/ul',
  3076. '/div'
  3077. );
  3078. $this->assertTags($result, $expected);
  3079. // Testing error messages list options
  3080. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  3081. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  3082. $expected = array(
  3083. 'div' => array('class' => 'error-message'),
  3084. 'ol' => array(),
  3085. '<li', 'notEmpty', '/li',
  3086. '<li', 'email', '/li',
  3087. '/ol',
  3088. '/div'
  3089. );
  3090. $this->assertTags($result, $expected);
  3091. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  3092. $expected = array(
  3093. 'div' => array('class' => 'error-message'),
  3094. 'ol' => array(),
  3095. '<li', 'notEmpty', '/li',
  3096. '<li', 'email', '/li',
  3097. '/ol',
  3098. '/div'
  3099. );
  3100. $this->assertTags($result, $expected);
  3101. $result = $this->Form->error('Contact.field', null, array(
  3102. 'listOptions' => array(
  3103. 'class' => 'ul-class',
  3104. 'itemOptions' => array(
  3105. 'class' => 'li-class'
  3106. )
  3107. )
  3108. ));
  3109. $expected = array(
  3110. 'div' => array('class' => 'error-message'),
  3111. 'ul' => array('class' => 'ul-class'),
  3112. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  3113. array('li' => array('class' => 'li-class')), 'email', '/li',
  3114. '/ul',
  3115. '/div'
  3116. );
  3117. $this->assertTags($result, $expected);
  3118. }
  3119. /**
  3120. * test error options when using form->input();
  3121. *
  3122. * @return void
  3123. */
  3124. public function testInputErrorEscape() {
  3125. $this->Form->create('ValidateProfile');
  3126. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  3127. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3128. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
  3129. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3130. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
  3131. $this->assertRegExp('/required<br>/', $result);
  3132. }
  3133. /**
  3134. * testPassword method
  3135. *
  3136. * Test password element generation
  3137. *
  3138. * @return void
  3139. */
  3140. public function testPassword() {
  3141. $Contact = ClassRegistry::getObject('Contact');
  3142. $result = $this->Form->password('Contact.field');
  3143. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  3144. $Contact->validationErrors['passwd'] = 1;
  3145. $this->Form->request->data['Contact']['passwd'] = 'test';
  3146. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3147. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3148. }
  3149. /**
  3150. * testRadio method
  3151. *
  3152. * Test radio element set generation
  3153. *
  3154. * @return void
  3155. */
  3156. public function testRadio() {
  3157. $result = $this->Form->radio('Model.field', array('option A'));
  3158. $expected = array(
  3159. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3160. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3161. 'label' => array('for' => 'ModelField0'),
  3162. 'option A',
  3163. '/label'
  3164. );
  3165. $this->assertTags($result, $expected);
  3166. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3167. $expected = array(
  3168. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3169. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  3170. 'label' => array('for' => 'ModelField12'),
  3171. 'half',
  3172. '/label'
  3173. );
  3174. $this->assertTags($result, $expected);
  3175. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3176. $expected = array(
  3177. 'fieldset' => array(),
  3178. 'legend' => array(),
  3179. 'Field',
  3180. '/legend',
  3181. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3182. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3183. array('label' => array('for' => 'ModelField0')),
  3184. 'option A',
  3185. '/label',
  3186. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3187. array('label' => array('for' => 'ModelField1')),
  3188. 'option B',
  3189. '/label',
  3190. '/fieldset'
  3191. );
  3192. $this->assertTags($result, $expected);
  3193. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3194. $expected = array(
  3195. 'fieldset' => array(),
  3196. 'legend' => array(),
  3197. 'Field',
  3198. '/legend',
  3199. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3200. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3201. array('label' => array('for' => 'ModelField0')),
  3202. 'option A',
  3203. '/label',
  3204. 'br' => array(),
  3205. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3206. array('label' => array('for' => 'ModelField1')),
  3207. 'option B',
  3208. '/label',
  3209. '/fieldset'
  3210. );
  3211. $this->assertTags($result, $expected);
  3212. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3213. $expected = array(
  3214. 'fieldset' => array(),
  3215. 'legend' => array(),
  3216. 'Gender',
  3217. '/legend',
  3218. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3219. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3220. array('label' => array('for' => 'EmployeeGenderMale')),
  3221. 'Male',
  3222. '/label',
  3223. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3224. array('label' => array('for' => 'EmployeeGenderFemale')),
  3225. 'Female',
  3226. '/label',
  3227. '/fieldset',
  3228. );
  3229. $this->assertTags($result, $expected);
  3230. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3231. $expected = array(
  3232. 'fieldset' => array(),
  3233. 'legend' => array(),
  3234. 'Gender',
  3235. '/legend',
  3236. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3237. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3238. array('label' => array('for' => 'OfficerGenderMale')),
  3239. 'Male',
  3240. '/label',
  3241. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3242. array('label' => array('for' => 'OfficerGenderFemale')),
  3243. 'Female',
  3244. '/label',
  3245. '/fieldset',
  3246. );
  3247. $this->assertTags($result, $expected);
  3248. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3249. $expected = array(
  3250. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3251. array('input' => array(
  3252. 'type' => 'radio',
  3253. 'name' => 'data[Contact][1][imrequired]',
  3254. 'value' => '0',
  3255. 'id' => 'Contact1Imrequired0',
  3256. 'required' => 'required'
  3257. )),
  3258. 'label' => array('for' => 'Contact1Imrequired0'),
  3259. 'option A',
  3260. '/label'
  3261. );
  3262. $this->assertTags($result, $expected);
  3263. $result = $this->Form->radio('Model.1.field', array('option A'));
  3264. $expected = array(
  3265. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3266. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3267. 'label' => array('for' => 'Model1Field0'),
  3268. 'option A',
  3269. '/label'
  3270. );
  3271. $this->assertTags($result, $expected);
  3272. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3273. $expected = array(
  3274. 'fieldset' => array(),
  3275. 'legend' => array(),
  3276. 'Field',
  3277. '/legend',
  3278. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3279. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3280. array('label' => array('for' => 'ModelField0')),
  3281. 'option A',
  3282. '/label',
  3283. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3284. array('label' => array('for' => 'ModelField1')),
  3285. 'option B',
  3286. '/label',
  3287. '/fieldset'
  3288. );
  3289. $this->assertTags($result, $expected);
  3290. }
  3291. /**
  3292. * Test radio inputs with between as string or array. Also ensure
  3293. * that an array with less between elements works.
  3294. *
  3295. * @return void
  3296. */
  3297. public function testRadioBetween() {
  3298. $result = $this->Form->radio(
  3299. 'Model.field',
  3300. array('option A', 'option B'),
  3301. array('between' => 'I am between')
  3302. );
  3303. $expected = array(
  3304. 'fieldset' => array(),
  3305. 'legend' => array(),
  3306. 'Field',
  3307. '/legend',
  3308. 'I am between',
  3309. 'input' => array(
  3310. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3311. 'value' => '', 'id' => 'ModelField_'
  3312. ),
  3313. array('input' => array(
  3314. 'type' => 'radio', 'name' => 'data[Model][field]',
  3315. 'value' => '0', 'id' => 'ModelField0'
  3316. )),
  3317. array('label' => array('for' => 'ModelField0')),
  3318. 'option A',
  3319. '/label',
  3320. array('input' => array(
  3321. 'type' => 'radio', 'name' => 'data[Model][field]',
  3322. 'value' => '1', 'id' => 'ModelField1'
  3323. )),
  3324. array('label' => array('for' => 'ModelField1')),
  3325. 'option B',
  3326. '/label',
  3327. '/fieldset'
  3328. );
  3329. $this->assertTags($result, $expected);
  3330. $result = $this->Form->radio(
  3331. 'Model.field',
  3332. array('option A', 'option B', 'option C'),
  3333. array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
  3334. );
  3335. $expected = array(
  3336. 'fieldset' => array(),
  3337. 'legend' => array(),
  3338. 'Field',
  3339. '/legend',
  3340. 'input' => array(
  3341. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3342. 'value' => '', 'id' => 'ModelField_'
  3343. ),
  3344. array('input' => array(
  3345. 'type' => 'radio', 'name' => 'data[Model][field]',
  3346. 'value' => '0', 'id' => 'ModelField0'
  3347. )),
  3348. array('label' => array('for' => 'ModelField0')),
  3349. 'option A',
  3350. '/label',
  3351. 'between A',
  3352. '--separator--',
  3353. array('input' => array(
  3354. 'type' => 'radio', 'name' => 'data[Model][field]',
  3355. 'value' => '1', 'id' => 'ModelField1'
  3356. )),
  3357. array('label' => array('for' => 'ModelField1')),
  3358. 'option B',
  3359. '/label',
  3360. 'between B',
  3361. '--separator--',
  3362. array('input' => array(
  3363. 'type' => 'radio', 'name' => 'data[Model][field]',
  3364. 'value' => '2', 'id' => 'ModelField2'
  3365. )),
  3366. array('label' => array('for' => 'ModelField2')),
  3367. 'option C',
  3368. '/label',
  3369. 'between C',
  3370. '/fieldset'
  3371. );
  3372. $this->assertTags($result, $expected);
  3373. $result = $this->Form->input('Model.field', array(
  3374. 'options' => array('1' => 'first', '2' => 'second'),
  3375. 'type' => 'radio',
  3376. 'before' => '--before--',
  3377. 'after' => '--after--',
  3378. 'separator' => '--separator--',
  3379. 'between' => array('--between first--', '--between second--')
  3380. ));
  3381. $expected = array(
  3382. 'div' => array('class' => 'input radio'),
  3383. '--before--',
  3384. 'fieldset' => array(),
  3385. 'legend' => array(),
  3386. 'Field',
  3387. '/legend',
  3388. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3389. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3390. array('label' => array('for' => 'ModelField1')),
  3391. 'first',
  3392. '/label',
  3393. '--between first--',
  3394. '--separator--',
  3395. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3396. array('label' => array('for' => 'ModelField2')),
  3397. 'second',
  3398. '/label',
  3399. '--between second--',
  3400. '/fieldset',
  3401. '--after--',
  3402. '/div'
  3403. );
  3404. $this->assertTags($result, $expected);
  3405. $result = $this->Form->input('Model.field', array(
  3406. 'options' => array('1' => 'first', '2' => 'second'),
  3407. 'type' => 'radio',
  3408. 'before' => '--before--',
  3409. 'after' => '--after--',
  3410. 'separator' => '--separator--',
  3411. 'between' => array('--between first--')
  3412. ));
  3413. $expected = array(
  3414. 'div' => array('class' => 'input radio'),
  3415. '--before--',
  3416. 'fieldset' => array(),
  3417. 'legend' => array(),
  3418. 'Field',
  3419. '/legend',
  3420. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3421. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3422. array('label' => array('for' => 'ModelField1')),
  3423. 'first',
  3424. '/label',
  3425. '--between first--',
  3426. '--separator--',
  3427. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3428. array('label' => array('for' => 'ModelField2')),
  3429. 'second',
  3430. '/label',
  3431. '/fieldset',
  3432. '--after--',
  3433. '/div'
  3434. );
  3435. $this->assertTags($result, $expected);
  3436. }
  3437. /**
  3438. * Test that radios with a 0 value are selected under the correct conditions.
  3439. * Also ensure that values that are booleanish are handled correctly.
  3440. *
  3441. * @return void
  3442. */
  3443. public function testRadioOptionWithBooleanishValues() {
  3444. $expected = array(
  3445. 'fieldset' => array(),
  3446. 'legend' => array(),
  3447. 'Field',
  3448. '/legend',
  3449. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3450. array('label' => array('for' => 'ModelField1')),
  3451. 'Yes',
  3452. '/label',
  3453. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3454. array('label' => array('for' => 'ModelField0')),
  3455. 'No',
  3456. '/label',
  3457. '/fieldset'
  3458. );
  3459. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3460. $this->assertTags($result, $expected);
  3461. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3462. $this->assertTags($result, $expected);
  3463. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3464. $this->assertTags($result, $expected);
  3465. $expected = array(
  3466. 'fieldset' => array(),
  3467. 'legend' => array(),
  3468. 'Field',
  3469. '/legend',
  3470. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3471. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3472. array('label' => array('for' => 'ModelField1')),
  3473. 'Yes',
  3474. '/label',
  3475. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3476. array('label' => array('for' => 'ModelField0')),
  3477. 'No',
  3478. '/label',
  3479. '/fieldset'
  3480. );
  3481. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3482. $this->assertTags($result, $expected);
  3483. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3484. $this->assertTags($result, $expected);
  3485. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3486. $this->assertTags($result, $expected);
  3487. $expected = array(
  3488. 'fieldset' => array(),
  3489. 'legend' => array(),
  3490. 'Field',
  3491. '/legend',
  3492. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3493. array('label' => array('for' => 'ModelField1')),
  3494. 'Yes',
  3495. '/label',
  3496. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3497. array('label' => array('for' => 'ModelField0')),
  3498. 'No',
  3499. '/label',
  3500. '/fieldset'
  3501. );
  3502. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3503. $this->assertTags($result, $expected);
  3504. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3505. $this->assertTags($result, $expected);
  3506. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3507. $this->assertTags($result, $expected);
  3508. }
  3509. /**
  3510. * test disabled radio options
  3511. *
  3512. * @return void
  3513. */
  3514. public function testRadioDisabled() {
  3515. $result = $this->Form->radio(
  3516. 'Model.field',
  3517. array('option A', 'option B'),
  3518. array('disabled' => array(0), 'value' => '0')
  3519. );
  3520. $expected = array(
  3521. 'fieldset' => array(),
  3522. 'legend' => array(),
  3523. 'Field',
  3524. '/legend',
  3525. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3526. array('label' => array('for' => 'ModelField0')),
  3527. 'option A',
  3528. '/label',
  3529. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3530. array('label' => array('for' => 'ModelField1')),
  3531. 'option B',
  3532. '/label',
  3533. '/fieldset'
  3534. );
  3535. $this->assertTags($result, $expected);
  3536. $result = $this->Form->radio(
  3537. 'Model.field',
  3538. array('option A', 'option B'),
  3539. array('disabled' => true, 'value' => '0')
  3540. );
  3541. $expected = array(
  3542. 'fieldset' => array(),
  3543. 'legend' => array(),
  3544. 'Field',
  3545. '/legend',
  3546. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3547. array('label' => array('for' => 'ModelField0')),
  3548. 'option A',
  3549. '/label',
  3550. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3551. array('label' => array('for' => 'ModelField1')),
  3552. 'option B',
  3553. '/label',
  3554. '/fieldset'
  3555. );
  3556. $this->assertTags($result, $expected);
  3557. $result = $this->Form->radio(
  3558. 'Model.field',
  3559. array('option A', 'option B'),
  3560. array('disabled' => 'disabled', 'value' => '0')
  3561. );
  3562. $expected = array(
  3563. 'fieldset' => array(),
  3564. 'legend' => array(),
  3565. 'Field',
  3566. '/legend',
  3567. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3568. array('label' => array('for' => 'ModelField0')),
  3569. 'option A',
  3570. '/label',
  3571. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3572. array('label' => array('for' => 'ModelField1')),
  3573. 'option B',
  3574. '/label',
  3575. '/fieldset'
  3576. );
  3577. $this->assertTags($result, $expected);
  3578. $result = $this->Form->input('Model.field', array(
  3579. 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
  3580. 'type' => 'radio',
  3581. 'disabled' => array(2, '3x'),
  3582. ));
  3583. $expected = array(
  3584. 'div' => array('class' => 'input radio'),
  3585. 'fieldset' => array(),
  3586. 'legend' => array(),
  3587. 'Field',
  3588. '/legend',
  3589. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3590. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3591. array('label' => array('for' => 'ModelField1')),
  3592. 'first',
  3593. '/label',
  3594. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
  3595. array('label' => array('for' => 'ModelField2')),
  3596. 'second',
  3597. '/label',
  3598. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2x', 'id' => 'ModelField2x')),
  3599. array('label' => array('for' => 'ModelField2x')),
  3600. '2x',
  3601. '/label',
  3602. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '3', 'id' => 'ModelField3')),
  3603. array('label' => array('for' => 'ModelField3')),
  3604. 'third',
  3605. '/label',
  3606. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
  3607. array('label' => array('for' => 'ModelField3x')),
  3608. '3x',
  3609. '/label',
  3610. '/fieldset',
  3611. '/div'
  3612. );
  3613. $this->assertTags($result, $expected);
  3614. }
  3615. /**
  3616. * test disabling the hidden input for radio buttons
  3617. *
  3618. * @return void
  3619. */
  3620. public function testRadioHiddenInputDisabling() {
  3621. $result = $this->Form->input('Model.1.field', array(
  3622. 'type' => 'radio',
  3623. 'options' => array('option A'),
  3624. 'hiddenField' => false
  3625. )
  3626. );
  3627. $expected = array(
  3628. 'div' => array('class' => 'input radio'),
  3629. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3630. 'label' => array('for' => 'Model1Field0'),
  3631. 'option A',
  3632. '/label',
  3633. '/div'
  3634. );
  3635. $this->assertTags($result, $expected);
  3636. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3637. $expected = array(
  3638. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3639. 'label' => array('for' => 'Model1Field0'),
  3640. 'option A',
  3641. '/label'
  3642. );
  3643. $this->assertTags($result, $expected);
  3644. }
  3645. /**
  3646. * test adding an empty option for radio buttons
  3647. *
  3648. * @return void
  3649. */
  3650. public function testRadioAddEmptyOption() {
  3651. $result = $this->Form->input('Model.1.field', array(
  3652. 'type' => 'radio',
  3653. 'options' => array('option A'),
  3654. 'empty' => true,
  3655. 'hiddenField' => false
  3656. ));
  3657. $expected = array(
  3658. 'div' => array('class' => 'input radio'),
  3659. 'fieldset' => array(),
  3660. 'legend' => array(),
  3661. 'Field',
  3662. '/legend',
  3663. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3664. array('label' => array('for' => 'Model1Field')),
  3665. __('empty'),
  3666. '/label',
  3667. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3668. array('label' => array('for' => 'Model1Field0')),
  3669. 'option A',
  3670. '/label',
  3671. '/fieldset',
  3672. '/div'
  3673. );
  3674. $this->assertTags($result, $expected);
  3675. $result = $this->Form->input('Model.1.field', array(
  3676. 'type' => 'radio',
  3677. 'options' => array('option A'),
  3678. 'empty' => 'CustomEmptyLabel',
  3679. 'hiddenField' => false
  3680. ));
  3681. $expected = array(
  3682. 'div' => array('class' => 'input radio'),
  3683. 'fieldset' => array(),
  3684. 'legend' => array(),
  3685. 'Field',
  3686. '/legend',
  3687. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3688. array('label' => array('for' => 'Model1Field')),
  3689. 'CustomEmptyLabel',
  3690. '/label',
  3691. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3692. array('label' => array('for' => 'Model1Field0')),
  3693. 'option A',
  3694. '/label',
  3695. '/fieldset',
  3696. '/div'
  3697. );
  3698. $this->assertTags($result, $expected);
  3699. $result = $this->Form->input('Model.1.field', array(
  3700. 'type' => 'radio',
  3701. 'options' => array('option A'),
  3702. 'empty' => false,
  3703. 'hiddenField' => false
  3704. ));
  3705. $this->assertTextNotContains('"Model1Field"', $result);
  3706. }
  3707. /**
  3708. * testSelect method
  3709. *
  3710. * Test select element generation.
  3711. *
  3712. * @return void
  3713. */
  3714. public function testSelect() {
  3715. $result = $this->Form->select('Model.field', array());
  3716. $expected = array(
  3717. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3718. array('option' => array('value' => '')),
  3719. '/option',
  3720. '/select'
  3721. );
  3722. $this->assertTags($result, $expected);
  3723. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3724. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3725. $expected = array(
  3726. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3727. array('option' => array('value' => '')),
  3728. '/option',
  3729. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3730. 'good',
  3731. '/option',
  3732. array('option' => array('value' => 'other')),
  3733. 'bad',
  3734. '/option',
  3735. '/select'
  3736. );
  3737. $this->assertTags($result, $expected);
  3738. $this->Form->request->data = array();
  3739. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3740. $expected = array(
  3741. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3742. array('option' => array('value' => '')),
  3743. '/option',
  3744. array('option' => array('value' => 'value')),
  3745. 'good',
  3746. '/option',
  3747. array('option' => array('value' => 'other')),
  3748. 'bad',
  3749. '/option',
  3750. '/select'
  3751. );
  3752. $this->assertTags($result, $expected);
  3753. $result = $this->Form->select(
  3754. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3755. array('empty' => false)
  3756. );
  3757. $expected = array(
  3758. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3759. array('option' => array('value' => 'first')),
  3760. 'first &quot;html&quot; &lt;chars&gt;',
  3761. '/option',
  3762. array('option' => array('value' => 'second')),
  3763. 'value',
  3764. '/option',
  3765. '/select'
  3766. );
  3767. $this->assertTags($result, $expected);
  3768. $result = $this->Form->select(
  3769. 'Model.field',
  3770. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3771. array('escape' => false, 'empty' => false)
  3772. );
  3773. $expected = array(
  3774. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3775. array('option' => array('value' => 'first')),
  3776. 'first "html" <chars>',
  3777. '/option',
  3778. array('option' => array('value' => 'second')),
  3779. 'value',
  3780. '/option',
  3781. '/select'
  3782. );
  3783. $this->assertTags($result, $expected);
  3784. $options = array(
  3785. array('value' => 'first', 'name' => 'First'),
  3786. array('value' => 'first', 'name' => 'Another First'),
  3787. );
  3788. $result = $this->Form->select(
  3789. 'Model.field',
  3790. $options,
  3791. array('escape' => false, 'empty' => false)
  3792. );
  3793. $expected = array(
  3794. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3795. array('option' => array('value' => 'first')),
  3796. 'First',
  3797. '/option',
  3798. array('option' => array('value' => 'first')),
  3799. 'Another First',
  3800. '/option',
  3801. '/select'
  3802. );
  3803. $this->assertTags($result, $expected);
  3804. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3805. $result = $this->Form->select(
  3806. 'Model.contact_id',
  3807. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3808. array('escape' => false, 'empty' => 'pick something')
  3809. );
  3810. $expected = array(
  3811. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3812. array('option' => array('value' => '')), 'pick something', '/option',
  3813. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3814. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3815. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3816. '/select'
  3817. );
  3818. $this->assertTags($result, $expected);
  3819. $this->Form->request->data['Model']['field'] = 0;
  3820. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3821. $expected = array(
  3822. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3823. array('option' => array('value' => '')), '/option',
  3824. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3825. array('option' => array('value' => '1')), 'Yes', '/option',
  3826. '/select'
  3827. );
  3828. $this->assertTags($result, $expected);
  3829. $this->Form->request->data['Model']['field'] = 50;
  3830. $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
  3831. $expected = array(
  3832. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3833. array('option' => array('value' => '')), '/option',
  3834. array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
  3835. array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
  3836. '/select'
  3837. );
  3838. $this->assertTags($result, $expected);
  3839. $result = $this->Form->select('Contact.required_one', array('option A'));
  3840. $expected = array(
  3841. 'select' => array(
  3842. 'name' => 'data[Contact][required_one]',
  3843. 'id' => 'ContactRequiredOne',
  3844. 'required' => 'required'
  3845. ),
  3846. array('option' => array('value' => '')), '/option',
  3847. array('option' => array('value' => '0')), 'option A', '/option',
  3848. '/select'
  3849. );
  3850. $this->assertTags($result, $expected);
  3851. $result = $this->Form->select('Contact.required_one', array('option A'), array('disabled' => true));
  3852. $expected = array(
  3853. 'select' => array(
  3854. 'name' => 'data[Contact][required_one]',
  3855. 'id' => 'ContactRequiredOne',
  3856. 'disabled' => 'disabled'
  3857. ),
  3858. array('option' => array('value' => '')), '/option',
  3859. array('option' => array('value' => '0')), 'option A', '/option',
  3860. '/select'
  3861. );
  3862. $this->assertTags($result, $expected);
  3863. }
  3864. /**
  3865. * test that select() with optiongroups listens to the escape param.
  3866. *
  3867. * @return void
  3868. */
  3869. public function testSelectOptionGroupEscaping() {
  3870. $options = array(
  3871. '>< Key' => array(
  3872. 1 => 'One',
  3873. 2 => 'Two'
  3874. )
  3875. );
  3876. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3877. $expected = array(
  3878. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3879. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3880. array('option' => array('value' => '1')), 'One', '/option',
  3881. array('option' => array('value' => '2')), 'Two', '/option',
  3882. '/optgroup',
  3883. '/select'
  3884. );
  3885. $this->assertTags($result, $expected);
  3886. $options = array(
  3887. '>< Key' => array(
  3888. 1 => 'One',
  3889. 2 => 'Two'
  3890. )
  3891. );
  3892. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3893. $expected = array(
  3894. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3895. 'optgroup' => array('label' => '>< Key'),
  3896. array('option' => array('value' => '1')), 'One', '/option',
  3897. array('option' => array('value' => '2')), 'Two', '/option',
  3898. '/optgroup',
  3899. '/select'
  3900. );
  3901. $this->assertTags($result, $expected);
  3902. }
  3903. /**
  3904. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3905. *
  3906. * @return void
  3907. */
  3908. public function testSelectWithNullAttributes() {
  3909. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3910. $expected = array(
  3911. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3912. array('option' => array('value' => '0')),
  3913. 'first',
  3914. '/option',
  3915. array('option' => array('value' => '1')),
  3916. 'second',
  3917. '/option',
  3918. '/select'
  3919. );
  3920. $this->assertTags($result, $expected);
  3921. }
  3922. /**
  3923. * testNestedSelect method
  3924. *
  3925. * test select element generation with optgroups
  3926. *
  3927. * @return void
  3928. */
  3929. public function testNestedSelect() {
  3930. $result = $this->Form->select(
  3931. 'Model.field',
  3932. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3933. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3934. )), array('empty' => false)
  3935. );
  3936. $expected = array(
  3937. 'select' => array('name' => 'data[Model][field]',
  3938. 'id' => 'ModelField'),
  3939. array('option' => array('value' => 1)),
  3940. 'One',
  3941. '/option',
  3942. array('option' => array('value' => 2)),
  3943. 'Two',
  3944. '/option',
  3945. array('optgroup' => array('label' => 'Three')),
  3946. array('option' => array('value' => 4)),
  3947. 'Four',
  3948. '/option',
  3949. array('option' => array('value' => 5)),
  3950. 'Five',
  3951. '/option',
  3952. '/optgroup',
  3953. '/select'
  3954. );
  3955. $this->assertTags($result, $expected);
  3956. $result = $this->Form->select(
  3957. 'Model.field',
  3958. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3959. array('showParents' => true, 'empty' => false)
  3960. );
  3961. $expected = array(
  3962. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3963. array('option' => array('value' => 1)),
  3964. 'One',
  3965. '/option',
  3966. array('option' => array('value' => 2)),
  3967. 'Two',
  3968. '/option',
  3969. array('optgroup' => array('label' => 'Three')),
  3970. array('option' => array('value' => 3)),
  3971. 'Three',
  3972. '/option',
  3973. array('option' => array('value' => 4)),
  3974. 'Four',
  3975. '/option',
  3976. '/optgroup',
  3977. '/select'
  3978. );
  3979. $this->assertTags($result, $expected);
  3980. }
  3981. /**
  3982. * testSelectMultiple method
  3983. *
  3984. * test generation of multiple select elements
  3985. *
  3986. * @return void
  3987. */
  3988. public function testSelectMultiple() {
  3989. $options = array('first', 'second', 'third');
  3990. $result = $this->Form->select(
  3991. 'Model.multi_field', $options, array('multiple' => true)
  3992. );
  3993. $expected = array(
  3994. 'input' => array(
  3995. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3996. ),
  3997. 'select' => array(
  3998. 'name' => 'data[Model][multi_field][]',
  3999. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4000. ),
  4001. array('option' => array('value' => '0')),
  4002. 'first',
  4003. '/option',
  4004. array('option' => array('value' => '1')),
  4005. 'second',
  4006. '/option',
  4007. array('option' => array('value' => '2')),
  4008. 'third',
  4009. '/option',
  4010. '/select'
  4011. );
  4012. $this->assertTags($result, $expected);
  4013. $result = $this->Form->select(
  4014. 'Model.multi_field', $options, array('multiple' => 'multiple')
  4015. );
  4016. $expected = array(
  4017. 'input' => array(
  4018. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4019. ),
  4020. 'select' => array(
  4021. 'name' => 'data[Model][multi_field][]',
  4022. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4023. ),
  4024. array('option' => array('value' => '0')),
  4025. 'first',
  4026. '/option',
  4027. array('option' => array('value' => '1')),
  4028. 'second',
  4029. '/option',
  4030. array('option' => array('value' => '2')),
  4031. 'third',
  4032. '/option',
  4033. '/select'
  4034. );
  4035. $this->assertTags($result, $expected);
  4036. $result = $this->Form->select(
  4037. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  4038. );
  4039. $expected = array(
  4040. 'input' => array(
  4041. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4042. ),
  4043. 'select' => array(
  4044. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  4045. 'multiple' => 'multiple'
  4046. ),
  4047. array('option' => array('value' => '0', 'selected' => 'selected')),
  4048. 'first',
  4049. '/option',
  4050. array('option' => array('value' => '1', 'selected' => 'selected')),
  4051. 'second',
  4052. '/option',
  4053. array('option' => array('value' => '2')),
  4054. 'third',
  4055. '/option',
  4056. '/select'
  4057. );
  4058. $this->assertTags($result, $expected);
  4059. $result = $this->Form->select(
  4060. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  4061. );
  4062. $expected = array(
  4063. 'select' => array(
  4064. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  4065. ),
  4066. array('option' => array('value' => '0', 'selected' => 'selected')),
  4067. 'first',
  4068. '/option',
  4069. array('option' => array('value' => '1', 'selected' => 'selected')),
  4070. 'second',
  4071. '/option',
  4072. array('option' => array('value' => '2')),
  4073. 'third',
  4074. '/option',
  4075. '/select'
  4076. );
  4077. $this->assertTags($result, $expected);
  4078. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4079. $selected = array('2', '3x');
  4080. $result = $this->Form->select(
  4081. 'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
  4082. );
  4083. $expected = array(
  4084. 'input' => array(
  4085. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4086. ),
  4087. 'select' => array(
  4088. 'name' => 'data[Model][multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4089. ),
  4090. array('option' => array('value' => '1')),
  4091. 'One',
  4092. '/option',
  4093. array('option' => array('value' => '2', 'selected' => 'selected')),
  4094. 'Two',
  4095. '/option',
  4096. array('option' => array('value' => '3')),
  4097. 'Three',
  4098. '/option',
  4099. array('option' => array('value' => '3x', 'selected' => 'selected')),
  4100. 'Stringy',
  4101. '/option',
  4102. '/select'
  4103. );
  4104. $this->assertTags($result, $expected);
  4105. $result = $this->Form->select('Contact.required_one', array(
  4106. '1' => 'option A',
  4107. '2' => 'option B'
  4108. ), array('multiple' => true));
  4109. $expected = array(
  4110. 'input' => array(
  4111. 'type' => 'hidden', 'name' => 'data[Contact][required_one]', 'value' => '', 'id' => 'ContactRequiredOne_'
  4112. ),
  4113. 'select' => array(
  4114. 'name' => 'data[Contact][required_one][]',
  4115. 'id' => 'ContactRequiredOne',
  4116. 'required' => 'required',
  4117. 'multiple' => 'multiple'
  4118. ),
  4119. array('option' => array('value' => '1')), 'option A', '/option',
  4120. array('option' => array('value' => '2')), 'option B', '/option',
  4121. '/select'
  4122. );
  4123. $this->assertTags($result, $expected);
  4124. }
  4125. /**
  4126. * Test generating multiple select with disabled elements.
  4127. *
  4128. * @return void
  4129. */
  4130. public function testSelectMultipleWithDisabledElements() {
  4131. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4132. $disabled = array(2, 3);
  4133. $result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
  4134. $expected = array(
  4135. 'input' => array(
  4136. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4137. ),
  4138. 'select' => array(
  4139. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4140. ),
  4141. array('option' => array('value' => '1')),
  4142. 'One',
  4143. '/option',
  4144. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4145. 'Two',
  4146. '/option',
  4147. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4148. 'Three',
  4149. '/option',
  4150. array('option' => array('value' => '3x')),
  4151. 'Stringy',
  4152. '/option',
  4153. '/select'
  4154. );
  4155. $this->assertTags($result, $expected);
  4156. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4157. $disabled = array('2', '3x');
  4158. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4159. $expected = array(
  4160. array('div' => array('class' => 'input select')),
  4161. array('label' => array('for' => 'ContactMultiple')),
  4162. 'Multiple',
  4163. '/label',
  4164. 'input' => array(
  4165. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4166. ),
  4167. 'select' => array(
  4168. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4169. ),
  4170. array('option' => array('value' => '1')),
  4171. 'One',
  4172. '/option',
  4173. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4174. 'Two',
  4175. '/option',
  4176. array('option' => array('value' => '3')),
  4177. 'Three',
  4178. '/option',
  4179. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4180. 'Stringy',
  4181. '/option',
  4182. '/select',
  4183. '/div'
  4184. );
  4185. $this->assertTags($result, $expected);
  4186. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4187. $disabled = true;
  4188. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4189. $expected = array(
  4190. array('div' => array('class' => 'input select')),
  4191. array('label' => array('for' => 'ContactMultiple')),
  4192. 'Multiple',
  4193. '/label',
  4194. 'input' => array(
  4195. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4196. ),
  4197. 'select' => array(
  4198. 'name' => 'data[Contact][multiple][]', 'disabled' => 'disabled', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4199. ),
  4200. array('option' => array('value' => '1')),
  4201. 'One',
  4202. '/option',
  4203. array('option' => array('value' => '2')),
  4204. 'Two',
  4205. '/option',
  4206. array('option' => array('value' => '3')),
  4207. 'Three',
  4208. '/option',
  4209. array('option' => array('value' => '3x')),
  4210. 'Stringy',
  4211. '/option',
  4212. '/select',
  4213. '/div'
  4214. );
  4215. $this->assertTags($result, $expected);
  4216. }
  4217. /**
  4218. * Test generating select with disabled elements.
  4219. *
  4220. * @return void
  4221. */
  4222. public function testSelectWithDisabledElements() {
  4223. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4224. $disabled = array(2, 3);
  4225. $result = $this->Form->select('Model.field', $options, array('disabled' => $disabled));
  4226. $expected = array(
  4227. 'select' => array(
  4228. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4229. ),
  4230. array('option' => array('value' => '')),
  4231. '/option',
  4232. array('option' => array('value' => '1')),
  4233. 'One',
  4234. '/option',
  4235. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4236. 'Two',
  4237. '/option',
  4238. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4239. 'Three',
  4240. '/option',
  4241. array('option' => array('value' => '3x')),
  4242. 'Stringy',
  4243. '/option',
  4244. '/select'
  4245. );
  4246. $this->assertTags($result, $expected);
  4247. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4248. $disabled = array('2', '3x');
  4249. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4250. $expected = array(
  4251. array('div' => array('class' => 'input select')),
  4252. array('label' => array('for' => 'ModelField')),
  4253. 'Field',
  4254. '/label',
  4255. 'select' => array(
  4256. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4257. ),
  4258. array('option' => array('value' => '1')),
  4259. 'One',
  4260. '/option',
  4261. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4262. 'Two',
  4263. '/option',
  4264. array('option' => array('value' => '3')),
  4265. 'Three',
  4266. '/option',
  4267. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4268. 'Stringy',
  4269. '/option',
  4270. '/select',
  4271. '/div'
  4272. );
  4273. $this->assertTags($result, $expected);
  4274. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4275. $disabled = true;
  4276. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4277. $expected = array(
  4278. array('div' => array('class' => 'input select')),
  4279. array('label' => array('for' => 'ModelField')),
  4280. 'Field',
  4281. '/label',
  4282. 'select' => array(
  4283. 'name' => 'data[Model][field]', 'id' => 'ModelField', 'disabled' => 'disabled'
  4284. ),
  4285. array('option' => array('value' => '1')),
  4286. 'One',
  4287. '/option',
  4288. array('option' => array('value' => '2')),
  4289. 'Two',
  4290. '/option',
  4291. array('option' => array('value' => '3')),
  4292. 'Three',
  4293. '/option',
  4294. array('option' => array('value' => '3x')),
  4295. 'Stringy',
  4296. '/option',
  4297. '/select',
  4298. '/div'
  4299. );
  4300. $this->assertTags($result, $expected);
  4301. }
  4302. /**
  4303. * test generation of habtm select boxes.
  4304. *
  4305. * @return void
  4306. */
  4307. public function testHabtmSelectBox() {
  4308. $this->View->viewVars['contactTags'] = array(
  4309. 1 => 'blue',
  4310. 2 => 'red',
  4311. 3 => 'green'
  4312. );
  4313. $this->Form->request->data = array(
  4314. 'Contact' => array(),
  4315. 'ContactTag' => array(
  4316. array(
  4317. 'id' => '1',
  4318. 'name' => 'blue'
  4319. ),
  4320. array(
  4321. 'id' => 3,
  4322. 'name' => 'green'
  4323. )
  4324. )
  4325. );
  4326. $this->Form->create('Contact');
  4327. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4328. $expected = array(
  4329. 'input' => array(
  4330. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4331. ),
  4332. 'select' => array(
  4333. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4334. 'multiple' => 'multiple'
  4335. ),
  4336. array('option' => array('value' => '1', 'selected' => 'selected')),
  4337. 'blue',
  4338. '/option',
  4339. array('option' => array('value' => '2')),
  4340. 'red',
  4341. '/option',
  4342. array('option' => array('value' => '3', 'selected' => 'selected')),
  4343. 'green',
  4344. '/option',
  4345. '/select'
  4346. );
  4347. $this->assertTags($result, $expected);
  4348. // make sure only 50 is selected, and not 50f5c0cf
  4349. $this->View->viewVars['contactTags'] = array(
  4350. '1' => 'blue',
  4351. '50f5c0cf' => 'red',
  4352. '50' => 'green'
  4353. );
  4354. $this->Form->request->data = array(
  4355. 'Contact' => array(),
  4356. 'ContactTag' => array(
  4357. array(
  4358. 'id' => 1,
  4359. 'name' => 'blue'
  4360. ),
  4361. array(
  4362. 'id' => 50,
  4363. 'name' => 'green'
  4364. )
  4365. )
  4366. );
  4367. $this->Form->create('Contact');
  4368. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4369. $expected = array(
  4370. 'input' => array(
  4371. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4372. ),
  4373. 'select' => array(
  4374. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4375. 'multiple' => 'multiple'
  4376. ),
  4377. array('option' => array('value' => '1', 'selected' => 'selected')),
  4378. 'blue',
  4379. '/option',
  4380. array('option' => array('value' => '50f5c0cf')),
  4381. 'red',
  4382. '/option',
  4383. array('option' => array('value' => '50', 'selected' => 'selected')),
  4384. 'green',
  4385. '/option',
  4386. '/select'
  4387. );
  4388. $this->assertTags($result, $expected);
  4389. }
  4390. /**
  4391. * test generation of multi select elements in checkbox format
  4392. *
  4393. * @return void
  4394. */
  4395. public function testSelectMultipleCheckboxes() {
  4396. $result = $this->Form->select(
  4397. 'Model.multi_field',
  4398. array('first', 'second', 'third'),
  4399. array('multiple' => 'checkbox')
  4400. );
  4401. $expected = array(
  4402. 'input' => array(
  4403. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4404. ),
  4405. array('div' => array('class' => 'checkbox')),
  4406. array('input' => array(
  4407. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4408. 'value' => '0', 'id' => 'ModelMultiField0'
  4409. )),
  4410. array('label' => array('for' => 'ModelMultiField0')),
  4411. 'first',
  4412. '/label',
  4413. '/div',
  4414. array('div' => array('class' => 'checkbox')),
  4415. array('input' => array(
  4416. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4417. 'value' => '1', 'id' => 'ModelMultiField1'
  4418. )),
  4419. array('label' => array('for' => 'ModelMultiField1')),
  4420. 'second',
  4421. '/label',
  4422. '/div',
  4423. array('div' => array('class' => 'checkbox')),
  4424. array('input' => array(
  4425. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4426. 'value' => '2', 'id' => 'ModelMultiField2'
  4427. )),
  4428. array('label' => array('for' => 'ModelMultiField2')),
  4429. 'third',
  4430. '/label',
  4431. '/div'
  4432. );
  4433. $this->assertTags($result, $expected);
  4434. $result = $this->Form->select(
  4435. 'Model.multi_field',
  4436. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4437. array('multiple' => 'checkbox')
  4438. );
  4439. $expected = array(
  4440. 'input' => array(
  4441. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4442. ),
  4443. array('div' => array('class' => 'checkbox')),
  4444. array('input' => array(
  4445. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4446. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  4447. )),
  4448. array('label' => array('for' => 'ModelMultiFieldA')),
  4449. 'first',
  4450. '/label',
  4451. '/div',
  4452. array('div' => array('class' => 'checkbox')),
  4453. array('input' => array(
  4454. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4455. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  4456. )),
  4457. array('label' => array('for' => 'ModelMultiFieldB')),
  4458. 'second',
  4459. '/label',
  4460. '/div',
  4461. array('div' => array('class' => 'checkbox')),
  4462. array('input' => array(
  4463. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4464. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  4465. )),
  4466. array('label' => array('for' => 'ModelMultiFieldC')),
  4467. 'third',
  4468. '/label',
  4469. '/div'
  4470. );
  4471. $this->assertTags($result, $expected);
  4472. $result = $this->Form->select(
  4473. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  4474. );
  4475. $expected = array(
  4476. 'input' => array(
  4477. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4478. ),
  4479. array('div' => array('class' => 'checkbox')),
  4480. array('input' => array(
  4481. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4482. 'value' => '1', 'id' => 'ModelMultiField1'
  4483. )),
  4484. array('label' => array('for' => 'ModelMultiField1')),
  4485. 'first',
  4486. '/label',
  4487. '/div'
  4488. );
  4489. $this->assertTags($result, $expected);
  4490. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4491. $result = $this->Form->select(
  4492. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4493. );
  4494. $expected = array(
  4495. 'input' => array(
  4496. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4497. ),
  4498. array('div' => array('class' => 'checkbox')),
  4499. array('input' => array(
  4500. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4501. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  4502. )),
  4503. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  4504. 'first',
  4505. '/label',
  4506. '/div',
  4507. array('div' => array('class' => 'checkbox')),
  4508. array('input' => array(
  4509. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4510. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4511. )),
  4512. array('label' => array('for' => 'ModelTagsArray')),
  4513. 'Array',
  4514. '/label',
  4515. '/div'
  4516. );
  4517. $this->assertTags($result, $expected);
  4518. }
  4519. /**
  4520. * test multiple checkboxes with div styles.
  4521. *
  4522. * @return void
  4523. */
  4524. public function testSelectMultipleCheckboxDiv() {
  4525. $result = $this->Form->select(
  4526. 'Model.tags',
  4527. array('first', 'second'),
  4528. array('multiple' => 'checkbox', 'class' => 'my-class')
  4529. );
  4530. $expected = array(
  4531. 'input' => array(
  4532. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4533. ),
  4534. array('div' => array('class' => 'my-class')),
  4535. array('input' => array(
  4536. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4537. 'value' => '0', 'id' => 'ModelTags0'
  4538. )),
  4539. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4540. '/div',
  4541. array('div' => array('class' => 'my-class')),
  4542. array('input' => array(
  4543. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4544. 'value' => '1', 'id' => 'ModelTags1'
  4545. )),
  4546. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4547. '/div'
  4548. );
  4549. $this->assertTags($result, $expected);
  4550. $result = $this->Form->input('Model.tags', array(
  4551. 'options' => array('first', 'second'),
  4552. 'multiple' => 'checkbox',
  4553. 'class' => 'my-class',
  4554. 'div' => false,
  4555. 'label' => false
  4556. ));
  4557. $this->assertTags($result, $expected);
  4558. $Contact = ClassRegistry::getObject('Contact');
  4559. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4560. $result = $this->Form->input('Contact.tags', array(
  4561. 'options' => array('one'),
  4562. 'multiple' => 'checkbox',
  4563. 'label' => false,
  4564. 'div' => false
  4565. ));
  4566. $expected = array(
  4567. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4568. array('div' => array('class' => 'checkbox form-error')),
  4569. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4570. array('label' => array('for' => 'ContactTags0')),
  4571. 'one',
  4572. '/label',
  4573. '/div'
  4574. );
  4575. $this->assertTags($result, $expected);
  4576. $result = $this->Form->input('Contact.tags', array(
  4577. 'options' => array('one'),
  4578. 'multiple' => 'checkbox',
  4579. 'class' => 'mycheckbox',
  4580. 'label' => false,
  4581. 'div' => false
  4582. ));
  4583. $expected = array(
  4584. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4585. array('div' => array('class' => 'mycheckbox form-error')),
  4586. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4587. array('label' => array('for' => 'ContactTags0')),
  4588. 'one',
  4589. '/label',
  4590. '/div'
  4591. );
  4592. $this->assertTags($result, $expected);
  4593. }
  4594. /**
  4595. * Checks the security hash array generated for multiple-input checkbox elements
  4596. *
  4597. * @return void
  4598. */
  4599. public function testSelectMultipleCheckboxSecurity() {
  4600. $this->Form->request['_Token'] = array('key' => 'testKey');
  4601. $this->assertEquals(array(), $this->Form->fields);
  4602. $result = $this->Form->select(
  4603. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4604. array('multiple' => 'checkbox')
  4605. );
  4606. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4607. $result = $this->Form->secure($this->Form->fields);
  4608. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4609. $this->assertRegExp('/"' . $key . '"/', $result);
  4610. }
  4611. /**
  4612. * Multiple select elements should always be secured as they always participate
  4613. * in the POST data.
  4614. *
  4615. * @return void
  4616. */
  4617. public function testSelectMultipleSecureWithNoOptions() {
  4618. $this->Form->request['_Token'] = array('key' => 'testkey');
  4619. $this->assertEquals(array(), $this->Form->fields);
  4620. $this->Form->select(
  4621. 'Model.select',
  4622. array(),
  4623. array('multiple' => true)
  4624. );
  4625. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4626. }
  4627. /**
  4628. * When a select box has no options it should not be added to the fields list
  4629. * as it always fail post validation.
  4630. *
  4631. * @return void
  4632. */
  4633. public function testSelectNoSecureWithNoOptions() {
  4634. $this->Form->request['_Token'] = array('key' => 'testkey');
  4635. $this->assertEquals(array(), $this->Form->fields);
  4636. $this->Form->select(
  4637. 'Model.select',
  4638. array()
  4639. );
  4640. $this->assertEquals(array(), $this->Form->fields);
  4641. $this->Form->select(
  4642. 'Model.select',
  4643. array(),
  4644. array('empty' => true)
  4645. );
  4646. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4647. }
  4648. /**
  4649. * testInputMultipleCheckboxes method
  4650. *
  4651. * test input() resulting in multi select elements being generated.
  4652. *
  4653. * @return void
  4654. */
  4655. public function testInputMultipleCheckboxes() {
  4656. $result = $this->Form->input('Model.multi_field', array(
  4657. 'options' => array('first', 'second', 'third'),
  4658. 'multiple' => 'checkbox'
  4659. ));
  4660. $expected = array(
  4661. array('div' => array('class' => 'input select')),
  4662. array('label' => array('for' => 'ModelMultiField')),
  4663. 'Multi Field',
  4664. '/label',
  4665. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4666. array('div' => array('class' => 'checkbox')),
  4667. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4668. array('label' => array('for' => 'ModelMultiField0')),
  4669. 'first',
  4670. '/label',
  4671. '/div',
  4672. array('div' => array('class' => 'checkbox')),
  4673. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4674. array('label' => array('for' => 'ModelMultiField1')),
  4675. 'second',
  4676. '/label',
  4677. '/div',
  4678. array('div' => array('class' => 'checkbox')),
  4679. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4680. array('label' => array('for' => 'ModelMultiField2')),
  4681. 'third',
  4682. '/label',
  4683. '/div',
  4684. '/div'
  4685. );
  4686. $this->assertTags($result, $expected);
  4687. $result = $this->Form->input('Model.multi_field', array(
  4688. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4689. 'multiple' => 'checkbox'
  4690. ));
  4691. $expected = array(
  4692. array('div' => array('class' => 'input select')),
  4693. array('label' => array('for' => 'ModelMultiField')),
  4694. 'Multi Field',
  4695. '/label',
  4696. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4697. array('div' => array('class' => 'checkbox')),
  4698. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4699. array('label' => array('for' => 'ModelMultiFieldA')),
  4700. 'first',
  4701. '/label',
  4702. '/div',
  4703. array('div' => array('class' => 'checkbox')),
  4704. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4705. array('label' => array('for' => 'ModelMultiFieldB')),
  4706. 'second',
  4707. '/label',
  4708. '/div',
  4709. array('div' => array('class' => 'checkbox')),
  4710. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4711. array('label' => array('for' => 'ModelMultiFieldC')),
  4712. 'third',
  4713. '/label',
  4714. '/div',
  4715. '/div'
  4716. );
  4717. $this->assertTags($result, $expected);
  4718. $result = $this->Form->input('Model.multi_field', array(
  4719. 'options' => array('1' => 'first'),
  4720. 'multiple' => 'checkbox',
  4721. 'label' => false,
  4722. 'div' => false
  4723. ));
  4724. $expected = array(
  4725. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4726. array('div' => array('class' => 'checkbox')),
  4727. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4728. array('label' => array('for' => 'ModelMultiField1')),
  4729. 'first',
  4730. '/label',
  4731. '/div'
  4732. );
  4733. $this->assertTags($result, $expected);
  4734. $result = $this->Form->input('Model.multi_field', array(
  4735. 'options' => array('2' => 'second'),
  4736. 'multiple' => 'checkbox',
  4737. 'label' => false,
  4738. 'div' => false
  4739. ));
  4740. $expected = array(
  4741. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4742. array('div' => array('class' => 'checkbox')),
  4743. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4744. array('label' => array('for' => 'ModelMultiField2')),
  4745. 'second',
  4746. '/label',
  4747. '/div'
  4748. );
  4749. $this->assertTags($result, $expected);
  4750. }
  4751. /**
  4752. * testSelectHiddenFieldOmission method
  4753. *
  4754. * test that select() with 'hiddenField' => false omits the hidden field
  4755. *
  4756. * @return void
  4757. */
  4758. public function testSelectHiddenFieldOmission() {
  4759. $result = $this->Form->select('Model.multi_field',
  4760. array('first', 'second'),
  4761. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4762. );
  4763. $expected = array(
  4764. array('div' => array('class' => 'checkbox')),
  4765. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4766. array('label' => array('for' => 'ModelMultiField0')),
  4767. 'first',
  4768. '/label',
  4769. '/div',
  4770. array('div' => array('class' => 'checkbox')),
  4771. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4772. array('label' => array('for' => 'ModelMultiField1')),
  4773. 'second',
  4774. '/label',
  4775. '/div'
  4776. );
  4777. $this->assertTags($result, $expected);
  4778. $result = $this->Form->input('Model.multi_field', array(
  4779. 'options' => array('first', 'second'),
  4780. 'multiple' => 'checkbox',
  4781. 'hiddenField' => false
  4782. ));
  4783. $expected = array(
  4784. array('div' => array('class' => 'input select')),
  4785. array('label' => array('for' => 'ModelMultiField')),
  4786. 'Multi Field',
  4787. '/label',
  4788. array('div' => array('class' => 'checkbox')),
  4789. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4790. array('label' => array('for' => 'ModelMultiField0')),
  4791. 'first',
  4792. '/label',
  4793. '/div',
  4794. array('div' => array('class' => 'checkbox')),
  4795. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4796. array('label' => array('for' => 'ModelMultiField1')),
  4797. 'second',
  4798. '/label',
  4799. '/div',
  4800. '/div'
  4801. );
  4802. $this->assertTags($result, $expected);
  4803. }
  4804. /**
  4805. * test that select() with multiple = checkbox works with overriding name attribute.
  4806. *
  4807. * @return void
  4808. */
  4809. public function testSelectCheckboxMultipleOverrideName() {
  4810. $result = $this->Form->input('category', array(
  4811. 'type' => 'select',
  4812. 'multiple' => 'checkbox',
  4813. 'name' => 'data[fish]',
  4814. 'options' => array('1', '2'),
  4815. 'div' => false,
  4816. 'label' => false,
  4817. ));
  4818. $expected = array(
  4819. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4820. array('div' => array('class' => 'checkbox')),
  4821. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4822. array('label' => array('for' => 'Category0')), '1', '/label',
  4823. '/div',
  4824. array('div' => array('class' => 'checkbox')),
  4825. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4826. array('label' => array('for' => 'Category1')), '2', '/label',
  4827. '/div'
  4828. );
  4829. $this->assertTags($result, $expected);
  4830. }
  4831. /**
  4832. * Test that 'id' overrides all the checkbox id's as well.
  4833. *
  4834. * @return void
  4835. */
  4836. public function testSelectCheckboxMultipleId() {
  4837. $result = $this->Form->select(
  4838. 'Model.multi_field',
  4839. array('first', 'second', 'third'),
  4840. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4841. );
  4842. $expected = array(
  4843. 'input' => array(
  4844. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4845. ),
  4846. array('div' => array('class' => 'checkbox')),
  4847. array('input' => array(
  4848. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4849. 'value' => '0', 'id' => 'CustomId0'
  4850. )),
  4851. array('label' => array('for' => 'CustomId0')),
  4852. 'first',
  4853. '/label',
  4854. '/div',
  4855. array('div' => array('class' => 'checkbox')),
  4856. array('input' => array(
  4857. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4858. 'value' => '1', 'id' => 'CustomId1'
  4859. )),
  4860. array('label' => array('for' => 'CustomId1')),
  4861. 'second',
  4862. '/label',
  4863. '/div',
  4864. array('div' => array('class' => 'checkbox')),
  4865. array('input' => array(
  4866. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4867. 'value' => '2', 'id' => 'CustomId2'
  4868. )),
  4869. array('label' => array('for' => 'CustomId2')),
  4870. 'third',
  4871. '/label',
  4872. '/div'
  4873. );
  4874. $this->assertTags($result, $expected);
  4875. }
  4876. /**
  4877. * testCheckbox method
  4878. *
  4879. * Test generation of checkboxes
  4880. *
  4881. * @return void
  4882. */
  4883. public function testCheckbox() {
  4884. $result = $this->Form->checkbox('Model.field');
  4885. $expected = array(
  4886. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4887. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4888. );
  4889. $this->assertTags($result, $expected);
  4890. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4891. $expected = array(
  4892. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4893. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4894. );
  4895. $this->assertTags($result, $expected);
  4896. $Contact = ClassRegistry::getObject('Contact');
  4897. $Contact->validationErrors['field'] = 1;
  4898. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4899. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4900. $expected = array(
  4901. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4902. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4903. );
  4904. $this->assertTags($result, $expected);
  4905. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4906. $expected = array(
  4907. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4908. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4909. );
  4910. $this->assertTags($result, $expected);
  4911. $this->Form->request->data['Contact']['field'] = '';
  4912. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4913. $expected = array(
  4914. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4915. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4916. );
  4917. $this->assertTags($result, $expected);
  4918. $Contact->validationErrors = array();
  4919. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4920. $expected = array(
  4921. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4922. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4923. );
  4924. $this->assertTags($result, $expected);
  4925. $this->Form->request->data['Contact']['published'] = 1;
  4926. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4927. $expected = array(
  4928. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4929. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4930. );
  4931. $this->assertTags($result, $expected);
  4932. $this->Form->request->data['Contact']['published'] = 0;
  4933. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4934. $expected = array(
  4935. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4936. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4937. );
  4938. $this->assertTags($result, $expected);
  4939. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4940. $expected = array(
  4941. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4942. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4943. );
  4944. $this->assertTags($result, $expected);
  4945. $result = $this->Form->checkbox('CustomField.1.value');
  4946. $expected = array(
  4947. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4948. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4949. );
  4950. $this->assertTags($result, $expected);
  4951. }
  4952. /**
  4953. * test checkbox() with a custom name attribute
  4954. *
  4955. * @return void
  4956. */
  4957. public function testCheckboxCustomNameAttribute() {
  4958. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4959. $expected = array(
  4960. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4961. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4962. );
  4963. $this->assertTags($result, $expected);
  4964. }
  4965. /**
  4966. * test the checked option for checkboxes.
  4967. *
  4968. * @return void
  4969. */
  4970. public function testCheckboxCheckedOption() {
  4971. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4972. $expected = array(
  4973. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4974. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4975. );
  4976. $this->assertTags($result, $expected);
  4977. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4978. $expected = array(
  4979. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4980. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4981. );
  4982. $this->assertTags($result, $expected);
  4983. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4984. $expected = array(
  4985. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4986. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4987. );
  4988. $this->assertTags($result, $expected);
  4989. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4990. $expected = array(
  4991. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4992. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4993. );
  4994. $this->assertTags($result, $expected);
  4995. $this->Form->request->data['Model']['field'] = 1;
  4996. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4997. $expected = array(
  4998. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4999. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5000. );
  5001. $this->assertTags($result, $expected);
  5002. }
  5003. /**
  5004. * Test that disabled attribute works on both the checkbox and hidden input.
  5005. *
  5006. * @return void
  5007. */
  5008. public function testCheckboxDisabling() {
  5009. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  5010. $expected = array(
  5011. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  5012. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  5013. );
  5014. $this->assertTags($result, $expected);
  5015. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  5016. $expected = array(
  5017. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  5018. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  5019. );
  5020. $this->assertTags($result, $expected);
  5021. }
  5022. /**
  5023. * Test that the hidden input for checkboxes can be omitted or set to a
  5024. * specific value.
  5025. *
  5026. * @return void
  5027. */
  5028. public function testCheckboxHiddenField() {
  5029. $result = $this->Form->input('UserForm.something', array(
  5030. 'type' => 'checkbox',
  5031. 'hiddenField' => false
  5032. ));
  5033. $expected = array(
  5034. 'div' => array('class' => 'input checkbox'),
  5035. array('input' => array(
  5036. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5037. 'value' => '1', 'id' => 'UserFormSomething'
  5038. )),
  5039. 'label' => array('for' => 'UserFormSomething'),
  5040. 'Something',
  5041. '/label',
  5042. '/div'
  5043. );
  5044. $this->assertTags($result, $expected);
  5045. $result = $this->Form->input('UserForm.something', array(
  5046. 'type' => 'checkbox',
  5047. 'value' => 'Y',
  5048. 'hiddenField' => 'N',
  5049. ));
  5050. $expected = array(
  5051. 'div' => array('class' => 'input checkbox'),
  5052. array('input' => array(
  5053. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  5054. 'value' => 'N', 'id' => 'UserFormSomething_'
  5055. )),
  5056. array('input' => array(
  5057. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5058. 'value' => 'Y', 'id' => 'UserFormSomething'
  5059. )),
  5060. 'label' => array('for' => 'UserFormSomething'),
  5061. 'Something',
  5062. '/label',
  5063. '/div'
  5064. );
  5065. $this->assertTags($result, $expected);
  5066. }
  5067. /**
  5068. * testDateTime method
  5069. *
  5070. * Test generation of date/time select elements
  5071. *
  5072. * @return void
  5073. */
  5074. public function testDateTime() {
  5075. extract($this->dateRegex);
  5076. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5077. $now = strtotime('now');
  5078. $expected = array(
  5079. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5080. $daysRegex,
  5081. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5082. date('j', $now),
  5083. '/option',
  5084. '*/select',
  5085. '-',
  5086. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5087. $monthsRegex,
  5088. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5089. date('F', $now),
  5090. '/option',
  5091. '*/select',
  5092. '-',
  5093. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5094. $yearsRegex,
  5095. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5096. date('Y', $now),
  5097. '/option',
  5098. '*/select',
  5099. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5100. $hoursRegex,
  5101. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5102. date('g', $now),
  5103. '/option',
  5104. '*/select',
  5105. ':',
  5106. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5107. $minutesRegex,
  5108. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5109. date('i', $now),
  5110. '/option',
  5111. '*/select',
  5112. ' ',
  5113. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5114. $meridianRegex,
  5115. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5116. date('a', $now),
  5117. '/option',
  5118. '*/select'
  5119. );
  5120. $this->assertTags($result, $expected);
  5121. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5122. $expected = array(
  5123. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5124. $daysRegex,
  5125. array('option' => array('value' => '')),
  5126. '/option',
  5127. '*/select',
  5128. '-',
  5129. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5130. $monthsRegex,
  5131. array('option' => array('value' => '')),
  5132. '/option',
  5133. '*/select',
  5134. '-',
  5135. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5136. $yearsRegex,
  5137. array('option' => array('value' => '')),
  5138. '/option',
  5139. '*/select',
  5140. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5141. $hoursRegex,
  5142. array('option' => array('value' => '')),
  5143. '/option',
  5144. '*/select',
  5145. ':',
  5146. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5147. $minutesRegex,
  5148. array('option' => array('value' => '')),
  5149. '/option',
  5150. '*/select',
  5151. ' ',
  5152. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5153. $meridianRegex,
  5154. array('option' => array('value' => '')),
  5155. '/option',
  5156. '*/select'
  5157. );
  5158. $this->assertTags($result, $expected);
  5159. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5160. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  5161. $this->assertTags($result, $expected);
  5162. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5163. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  5164. $this->assertTags($result, $expected);
  5165. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5166. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  5167. $expected = array(
  5168. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5169. $daysRegex,
  5170. array('option' => array('value' => '')),
  5171. '/option',
  5172. '*/select',
  5173. '-',
  5174. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5175. $monthsRegex,
  5176. array('option' => array('value' => '')),
  5177. '/option',
  5178. '*/select',
  5179. '-',
  5180. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5181. $yearsRegex,
  5182. array('option' => array('value' => '')),
  5183. '/option',
  5184. '*/select',
  5185. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5186. $hoursRegex,
  5187. array('option' => array('value' => '')),
  5188. '/option',
  5189. '*/select',
  5190. ':',
  5191. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5192. $minutesRegex,
  5193. array('option' => array('value' => '')),
  5194. '/option',
  5195. array('option' => array('value' => '00')),
  5196. '00',
  5197. '/option',
  5198. array('option' => array('value' => '05')),
  5199. '05',
  5200. '/option',
  5201. array('option' => array('value' => '10')),
  5202. '10',
  5203. '/option',
  5204. '*/select',
  5205. ' ',
  5206. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5207. $meridianRegex,
  5208. array('option' => array('value' => '')),
  5209. '/option',
  5210. '*/select'
  5211. );
  5212. $this->assertTags($result, $expected);
  5213. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5214. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5215. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5216. $this->Form->request->data['Contact']['data'] = null;
  5217. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5218. $expected = array(
  5219. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5220. $daysRegex,
  5221. array('option' => array('value' => '')),
  5222. '/option',
  5223. '*/select',
  5224. '-',
  5225. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5226. $monthsRegex,
  5227. array('option' => array('value' => '')),
  5228. '/option',
  5229. '*/select',
  5230. '-',
  5231. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5232. $yearsRegex,
  5233. array('option' => array('value' => '')),
  5234. '/option',
  5235. '*/select',
  5236. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5237. $hoursRegex,
  5238. array('option' => array('value' => '')),
  5239. '/option',
  5240. '*/select',
  5241. ':',
  5242. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5243. $minutesRegex,
  5244. array('option' => array('value' => '')),
  5245. '/option',
  5246. '*/select',
  5247. ' ',
  5248. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5249. $meridianRegex,
  5250. array('option' => array('value' => '')),
  5251. '/option',
  5252. '*/select'
  5253. );
  5254. $this->assertTags($result, $expected);
  5255. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5256. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  5257. $now = strtotime($this->Form->data['Model']['field']);
  5258. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  5259. $expected = array(
  5260. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5261. $daysRegex,
  5262. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5263. date('j', $now),
  5264. '/option',
  5265. '*/select',
  5266. '-',
  5267. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5268. $monthsRegex,
  5269. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5270. date('F', $now),
  5271. '/option',
  5272. '*/select',
  5273. '-',
  5274. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5275. $yearsRegex,
  5276. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5277. date('Y', $now),
  5278. '/option',
  5279. '*/select',
  5280. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5281. $hoursRegex,
  5282. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5283. date('g', $now),
  5284. '/option',
  5285. '*/select',
  5286. ':',
  5287. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5288. $minutesRegex,
  5289. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5290. date('i', $now),
  5291. '/option',
  5292. '*/select',
  5293. ' ',
  5294. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  5295. $meridianRegex,
  5296. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5297. date('a', $now),
  5298. '/option',
  5299. '*/select'
  5300. );
  5301. $this->assertTags($result, $expected);
  5302. $selected = strtotime('2008-10-26 12:33:00');
  5303. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  5304. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  5305. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  5306. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  5307. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  5308. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  5309. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  5310. $this->Form->create('Contact');
  5311. $result = $this->Form->input('published');
  5312. $now = strtotime('now');
  5313. $expected = array(
  5314. 'div' => array('class' => 'input date'),
  5315. 'label' => array('for' => 'ContactPublishedMonth'),
  5316. 'Published',
  5317. '/label',
  5318. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5319. $monthsRegex,
  5320. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5321. date('F', $now),
  5322. '/option',
  5323. '*/select',
  5324. '-',
  5325. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5326. $daysRegex,
  5327. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5328. date('j', $now),
  5329. '/option',
  5330. '*/select',
  5331. '-',
  5332. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5333. $yearsRegex,
  5334. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5335. date('Y', $now),
  5336. '/option',
  5337. '*/select',
  5338. '/div'
  5339. );
  5340. $this->assertTags($result, $expected);
  5341. $result = $this->Form->input('published2', array('type' => 'date'));
  5342. $now = strtotime('now');
  5343. $expected = array(
  5344. 'div' => array('class' => 'input date'),
  5345. 'label' => array('for' => 'ContactPublished2Month'),
  5346. 'Published2',
  5347. '/label',
  5348. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  5349. $monthsRegex,
  5350. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5351. date('F', $now),
  5352. '/option',
  5353. '*/select',
  5354. '-',
  5355. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  5356. $daysRegex,
  5357. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5358. date('j', $now),
  5359. '/option',
  5360. '*/select',
  5361. '-',
  5362. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  5363. $yearsRegex,
  5364. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5365. date('Y', $now),
  5366. '/option',
  5367. '*/select',
  5368. '/div'
  5369. );
  5370. $this->assertTags($result, $expected);
  5371. $this->Form->create('Contact');
  5372. $result = $this->Form->input('published', array('monthNames' => false));
  5373. $now = strtotime('now');
  5374. $expected = array(
  5375. 'div' => array('class' => 'input date'),
  5376. 'label' => array('for' => 'ContactPublishedMonth'),
  5377. 'Published',
  5378. '/label',
  5379. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5380. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5381. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5382. date('m', $now),
  5383. '/option',
  5384. '*/select',
  5385. '-',
  5386. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5387. $daysRegex,
  5388. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5389. date('j', $now),
  5390. '/option',
  5391. '*/select',
  5392. '-',
  5393. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5394. $yearsRegex,
  5395. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5396. date('Y', $now),
  5397. '/option',
  5398. '*/select',
  5399. '/div'
  5400. );
  5401. $this->assertTags($result, $expected);
  5402. $result = $this->Form->input('published', array(
  5403. 'timeFormat' => 24,
  5404. 'interval' => 5,
  5405. 'selected' => strtotime('2009-09-03 13:37:00'),
  5406. 'type' => 'datetime'
  5407. ));
  5408. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5409. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  5410. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  5411. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  5412. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  5413. }
  5414. /**
  5415. * Test that empty values don't trigger errors.
  5416. *
  5417. * @return void
  5418. */
  5419. public function testDateTimeNoErrorsOnEmptyData() {
  5420. $this->Form->request->data['Contact'] = array(
  5421. 'date' => array(
  5422. 'day' => '',
  5423. 'month' => '',
  5424. 'year' => '',
  5425. 'hour' => '',
  5426. 'min' => '',
  5427. 'meridian' => ''
  5428. )
  5429. );
  5430. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5431. $this->assertNotEmpty($result);
  5432. }
  5433. /**
  5434. * test that datetime() and default values work.
  5435. *
  5436. * @return void
  5437. */
  5438. public function testDatetimeWithDefault() {
  5439. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  5440. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5441. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5442. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5443. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  5444. 'default' => '2009-06-01 11:15:30'
  5445. ));
  5446. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5447. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5448. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5449. }
  5450. /**
  5451. * test that bogus non-date time data doesn't cause errors.
  5452. *
  5453. * @return void
  5454. */
  5455. public function testDateTimeWithBogusData() {
  5456. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  5457. $this->assertNotRegExp('/selected="selected">\d/', $result);
  5458. }
  5459. /**
  5460. * testDateTimeEmptyAsArray
  5461. *
  5462. * @return void
  5463. */
  5464. public function testDateTimeEmptyAsArray() {
  5465. $result = $this->Form->dateTime('Contact.date',
  5466. 'DMY',
  5467. '12',
  5468. array(
  5469. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  5470. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  5471. )
  5472. )
  5473. );
  5474. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5475. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5476. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5477. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5478. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5479. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5480. $result = $this->Form->dateTime('Contact.date',
  5481. 'DMY',
  5482. '12',
  5483. array(
  5484. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  5485. )
  5486. );
  5487. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5488. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5489. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5490. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  5491. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  5492. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  5493. }
  5494. /**
  5495. * testFormDateTimeMulti method
  5496. *
  5497. * test multiple datetime element generation
  5498. *
  5499. * @return void
  5500. */
  5501. public function testFormDateTimeMulti() {
  5502. extract($this->dateRegex);
  5503. $result = $this->Form->dateTime('Contact.1.updated');
  5504. $expected = array(
  5505. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  5506. $daysRegex,
  5507. array('option' => array('value' => '')),
  5508. '/option',
  5509. '*/select',
  5510. '-',
  5511. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  5512. $monthsRegex,
  5513. array('option' => array('value' => '')),
  5514. '/option',
  5515. '*/select',
  5516. '-',
  5517. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  5518. $yearsRegex,
  5519. array('option' => array('value' => '')),
  5520. '/option',
  5521. '*/select',
  5522. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  5523. $hoursRegex,
  5524. array('option' => array('value' => '')),
  5525. '/option',
  5526. '*/select',
  5527. ':',
  5528. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5529. $minutesRegex,
  5530. array('option' => array('value' => '')),
  5531. '/option',
  5532. '*/select',
  5533. ' ',
  5534. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5535. $meridianRegex,
  5536. array('option' => array('value' => '')),
  5537. '/option',
  5538. '*/select'
  5539. );
  5540. $this->assertTags($result, $expected);
  5541. $result = $this->Form->dateTime('Contact.2.updated');
  5542. $expected = array(
  5543. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5544. $daysRegex,
  5545. array('option' => array('value' => '')),
  5546. '/option',
  5547. '*/select',
  5548. '-',
  5549. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5550. $monthsRegex,
  5551. array('option' => array('value' => '')),
  5552. '/option',
  5553. '*/select',
  5554. '-',
  5555. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5556. $yearsRegex,
  5557. array('option' => array('value' => '')),
  5558. '/option',
  5559. '*/select',
  5560. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5561. $hoursRegex,
  5562. array('option' => array('value' => '')),
  5563. '/option',
  5564. '*/select',
  5565. ':',
  5566. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5567. $minutesRegex,
  5568. array('option' => array('value' => '')),
  5569. '/option',
  5570. '*/select',
  5571. ' ',
  5572. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5573. $meridianRegex,
  5574. array('option' => array('value' => '')),
  5575. '/option',
  5576. '*/select'
  5577. );
  5578. $this->assertTags($result, $expected);
  5579. }
  5580. /**
  5581. * When changing the date format, the label should always focus the first select box when
  5582. * clicked.
  5583. *
  5584. * @return void
  5585. */
  5586. public function testDateTimeLabelIdMatchesFirstInput() {
  5587. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5588. $this->assertContains('label for="ModelDateMonth"', $result);
  5589. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5590. $this->assertContains('label for="ModelDateDay"', $result);
  5591. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5592. $this->assertContains('label for="ModelDateYear"', $result);
  5593. }
  5594. /**
  5595. * testMonth method
  5596. *
  5597. * @return void
  5598. */
  5599. public function testMonth() {
  5600. $result = $this->Form->month('Model.field');
  5601. $expected = array(
  5602. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5603. array('option' => array('value' => '')),
  5604. '/option',
  5605. array('option' => array('value' => '01')),
  5606. date('F', strtotime('2008-01-01 00:00:00')),
  5607. '/option',
  5608. array('option' => array('value' => '02')),
  5609. date('F', strtotime('2008-02-01 00:00:00')),
  5610. '/option',
  5611. '*/select',
  5612. );
  5613. $this->assertTags($result, $expected);
  5614. $result = $this->Form->month('Model.field', array('empty' => true));
  5615. $expected = array(
  5616. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5617. array('option' => array('value' => '')),
  5618. '/option',
  5619. array('option' => array('value' => '01')),
  5620. date('F', strtotime('2008-01-01 00:00:00')),
  5621. '/option',
  5622. array('option' => array('value' => '02')),
  5623. date('F', strtotime('2008-02-01 00:00:00')),
  5624. '/option',
  5625. '*/select',
  5626. );
  5627. $this->assertTags($result, $expected);
  5628. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5629. $expected = array(
  5630. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5631. array('option' => array('value' => '')),
  5632. '/option',
  5633. array('option' => array('value' => '01')),
  5634. '01',
  5635. '/option',
  5636. array('option' => array('value' => '02')),
  5637. '02',
  5638. '/option',
  5639. '*/select',
  5640. );
  5641. $this->assertTags($result, $expected);
  5642. $monthNames = array(
  5643. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5644. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5645. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5646. $expected = array(
  5647. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5648. array('option' => array('value' => '')),
  5649. '/option',
  5650. array('option' => array('value' => '01')),
  5651. 'Jan',
  5652. '/option',
  5653. array('option' => array('value' => '02')),
  5654. 'Feb',
  5655. '/option',
  5656. '*/select',
  5657. );
  5658. $this->assertTags($result, $expected);
  5659. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5660. $result = $this->Form->month('Project.release');
  5661. $expected = array(
  5662. array('select' => array('name' => 'data[Project][release][month]', 'id' => 'ProjectReleaseMonth')),
  5663. array('option' => array('value' => '')),
  5664. '/option',
  5665. array('option' => array('value' => '01')),
  5666. 'January',
  5667. '/option',
  5668. array('option' => array('value' => '02', 'selected' => 'selected')),
  5669. 'February',
  5670. '/option',
  5671. '*/select',
  5672. );
  5673. $this->assertTags($result, $expected);
  5674. }
  5675. /**
  5676. * testDay method
  5677. *
  5678. * @return void
  5679. */
  5680. public function testDay() {
  5681. extract($this->dateRegex);
  5682. $result = $this->Form->day('Model.field', array('value' => false));
  5683. $expected = array(
  5684. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5685. array('option' => array('value' => '')),
  5686. '/option',
  5687. array('option' => array('value' => '01')),
  5688. '1',
  5689. '/option',
  5690. array('option' => array('value' => '02')),
  5691. '2',
  5692. '/option',
  5693. $daysRegex,
  5694. '/select',
  5695. );
  5696. $this->assertTags($result, $expected);
  5697. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5698. $result = $this->Form->day('Model.field');
  5699. $expected = array(
  5700. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5701. array('option' => array('value' => '')),
  5702. '/option',
  5703. array('option' => array('value' => '01')),
  5704. '1',
  5705. '/option',
  5706. array('option' => array('value' => '02')),
  5707. '2',
  5708. '/option',
  5709. $daysRegex,
  5710. array('option' => array('value' => '10', 'selected' => 'selected')),
  5711. '10',
  5712. '/option',
  5713. $daysRegex,
  5714. '/select',
  5715. );
  5716. $this->assertTags($result, $expected);
  5717. $this->Form->request->data['Model']['field'] = '';
  5718. $result = $this->Form->day('Model.field', array('value' => '10'));
  5719. $expected = array(
  5720. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5721. array('option' => array('value' => '')),
  5722. '/option',
  5723. array('option' => array('value' => '01')),
  5724. '1',
  5725. '/option',
  5726. array('option' => array('value' => '02')),
  5727. '2',
  5728. '/option',
  5729. $daysRegex,
  5730. array('option' => array('value' => '10', 'selected' => 'selected')),
  5731. '10',
  5732. '/option',
  5733. $daysRegex,
  5734. '/select',
  5735. );
  5736. $this->assertTags($result, $expected);
  5737. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5738. $result = $this->Form->day('Model.field', array('value' => true));
  5739. $expected = array(
  5740. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5741. array('option' => array('value' => '')),
  5742. '/option',
  5743. array('option' => array('value' => '01')),
  5744. '1',
  5745. '/option',
  5746. array('option' => array('value' => '02')),
  5747. '2',
  5748. '/option',
  5749. $daysRegex,
  5750. array('option' => array('value' => '10', 'selected' => 'selected')),
  5751. '10',
  5752. '/option',
  5753. $daysRegex,
  5754. '/select',
  5755. );
  5756. $this->assertTags($result, $expected);
  5757. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5758. $result = $this->Form->day('Project.release');
  5759. $expected = array(
  5760. array('select' => array('name' => 'data[Project][release][day]', 'id' => 'ProjectReleaseDay')),
  5761. array('option' => array('value' => '')),
  5762. '/option',
  5763. array('option' => array('value' => '01')),
  5764. '1',
  5765. '/option',
  5766. array('option' => array('value' => '02')),
  5767. '2',
  5768. '/option',
  5769. $daysRegex,
  5770. array('option' => array('value' => '10', 'selected' => 'selected')),
  5771. '10',
  5772. '/option',
  5773. $daysRegex,
  5774. '/select',
  5775. );
  5776. $this->assertTags($result, $expected);
  5777. }
  5778. /**
  5779. * testMinute method
  5780. *
  5781. * @return void
  5782. */
  5783. public function testMinute() {
  5784. extract($this->dateRegex);
  5785. $result = $this->Form->minute('Model.field');
  5786. $expected = array(
  5787. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5788. array('option' => array('value' => '')),
  5789. '/option',
  5790. array('option' => array('value' => '00')),
  5791. '00',
  5792. '/option',
  5793. array('option' => array('value' => '01')),
  5794. '01',
  5795. '/option',
  5796. array('option' => array('value' => '02')),
  5797. '02',
  5798. '/option',
  5799. $minutesRegex,
  5800. '/select',
  5801. );
  5802. $this->assertTags($result, $expected);
  5803. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5804. $result = $this->Form->minute('Model.field');
  5805. $expected = array(
  5806. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5807. array('option' => array('value' => '')),
  5808. '/option',
  5809. array('option' => array('value' => '00')),
  5810. '00',
  5811. '/option',
  5812. array('option' => array('value' => '01')),
  5813. '01',
  5814. '/option',
  5815. array('option' => array('value' => '02')),
  5816. '02',
  5817. '/option',
  5818. $minutesRegex,
  5819. array('option' => array('value' => '12', 'selected' => 'selected')),
  5820. '12',
  5821. '/option',
  5822. $minutesRegex,
  5823. '/select',
  5824. );
  5825. $this->assertTags($result, $expected);
  5826. $this->Form->request->data['Model']['field'] = '';
  5827. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5828. $expected = array(
  5829. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5830. array('option' => array('value' => '')),
  5831. '/option',
  5832. array('option' => array('value' => '00')),
  5833. '00',
  5834. '/option',
  5835. array('option' => array('value' => '05')),
  5836. '05',
  5837. '/option',
  5838. array('option' => array('value' => '10')),
  5839. '10',
  5840. '/option',
  5841. $minutesRegex,
  5842. '/select',
  5843. );
  5844. $this->assertTags($result, $expected);
  5845. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5846. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5847. $expected = array(
  5848. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5849. array('option' => array('value' => '')),
  5850. '/option',
  5851. array('option' => array('value' => '00')),
  5852. '00',
  5853. '/option',
  5854. array('option' => array('value' => '05')),
  5855. '05',
  5856. '/option',
  5857. array('option' => array('value' => '10', 'selected' => 'selected')),
  5858. '10',
  5859. '/option',
  5860. $minutesRegex,
  5861. '/select',
  5862. );
  5863. $this->assertTags($result, $expected);
  5864. }
  5865. /**
  5866. * testHour method
  5867. *
  5868. * @return void
  5869. */
  5870. public function testHour() {
  5871. extract($this->dateRegex);
  5872. $result = $this->Form->hour('Model.field', false);
  5873. $expected = array(
  5874. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5875. array('option' => array('value' => '')),
  5876. '/option',
  5877. array('option' => array('value' => '01')),
  5878. '1',
  5879. '/option',
  5880. array('option' => array('value' => '02')),
  5881. '2',
  5882. '/option',
  5883. $hoursRegex,
  5884. '/select',
  5885. );
  5886. $this->assertTags($result, $expected);
  5887. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5888. $result = $this->Form->hour('Model.field', false);
  5889. $expected = array(
  5890. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5891. array('option' => array('value' => '')),
  5892. '/option',
  5893. array('option' => array('value' => '01')),
  5894. '1',
  5895. '/option',
  5896. array('option' => array('value' => '02')),
  5897. '2',
  5898. '/option',
  5899. $hoursRegex,
  5900. array('option' => array('value' => '12', 'selected' => 'selected')),
  5901. '12',
  5902. '/option',
  5903. '/select',
  5904. );
  5905. $this->assertTags($result, $expected);
  5906. $this->Form->request->data['Model']['field'] = '';
  5907. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5908. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5909. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  5910. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5911. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5912. $result = $this->Form->hour('Model.field', true);
  5913. $expected = array(
  5914. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5915. array('option' => array('value' => '')),
  5916. '/option',
  5917. array('option' => array('value' => '00', 'selected' => 'selected')),
  5918. '0',
  5919. '/option',
  5920. array('option' => array('value' => '01')),
  5921. '1',
  5922. '/option',
  5923. array('option' => array('value' => '02')),
  5924. '2',
  5925. '/option',
  5926. $hoursRegex,
  5927. '/select',
  5928. );
  5929. $this->assertTags($result, $expected);
  5930. unset($this->Form->request->data['Model']['field']);
  5931. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5932. $thisHour = date('H');
  5933. $optValue = date('G');
  5934. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5935. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  5936. $result = $this->Form->hour('Model.field', true);
  5937. $expected = array(
  5938. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5939. array('option' => array('value' => '')),
  5940. '/option',
  5941. array('option' => array('value' => '00')),
  5942. '0',
  5943. '/option',
  5944. array('option' => array('value' => '01', 'selected' => 'selected')),
  5945. '1',
  5946. '/option',
  5947. array('option' => array('value' => '02')),
  5948. '2',
  5949. '/option',
  5950. $hoursRegex,
  5951. '/select',
  5952. );
  5953. $this->assertTags($result, $expected);
  5954. }
  5955. /**
  5956. * testYear method
  5957. *
  5958. * @return void
  5959. */
  5960. public function testYear() {
  5961. $result = $this->Form->year('Model.field', 2006, 2007);
  5962. $expected = array(
  5963. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5964. array('option' => array('value' => '')),
  5965. '/option',
  5966. array('option' => array('value' => '2007')),
  5967. '2007',
  5968. '/option',
  5969. array('option' => array('value' => '2006')),
  5970. '2006',
  5971. '/option',
  5972. '/select',
  5973. );
  5974. $this->assertTags($result, $expected);
  5975. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5976. $expected = array(
  5977. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5978. array('option' => array('value' => '')),
  5979. '/option',
  5980. array('option' => array('value' => '2006')),
  5981. '2006',
  5982. '/option',
  5983. array('option' => array('value' => '2007')),
  5984. '2007',
  5985. '/option',
  5986. '/select',
  5987. );
  5988. $this->assertTags($result, $expected);
  5989. $this->request->data['Contact']['published'] = '';
  5990. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5991. $expected = array(
  5992. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5993. array('option' => array('value' => '')),
  5994. '/option',
  5995. array('option' => array('value' => '2007')),
  5996. '2007',
  5997. '/option',
  5998. array('option' => array('value' => '2006')),
  5999. '2006',
  6000. '/option',
  6001. '/select',
  6002. );
  6003. $this->assertTags($result, $expected);
  6004. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6005. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  6006. $expected = array(
  6007. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6008. array('option' => array('value' => '2007')),
  6009. '2007',
  6010. '/option',
  6011. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6012. '2006',
  6013. '/option',
  6014. '/select',
  6015. );
  6016. $this->assertTags($result, $expected);
  6017. $this->Form->request->data['Contact']['published'] = '';
  6018. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  6019. $expected = array(
  6020. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6021. array('option' => array('value' => '')),
  6022. '/option',
  6023. array('option' => array('value' => '2007')),
  6024. '2007',
  6025. '/option',
  6026. array('option' => array('value' => '2006')),
  6027. '2006',
  6028. '/option',
  6029. '/select',
  6030. );
  6031. $this->assertTags($result, $expected);
  6032. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6033. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  6034. $expected = array(
  6035. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6036. array('option' => array('value' => '2007')),
  6037. '2007',
  6038. '/option',
  6039. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6040. '2006',
  6041. '/option',
  6042. '/select',
  6043. );
  6044. $this->assertTags($result, $expected);
  6045. $this->Form->request->data['Contact']['published'] = '';
  6046. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  6047. $expected = array(
  6048. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6049. array('option' => array('value' => '')),
  6050. '/option',
  6051. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6052. '2007',
  6053. '/option',
  6054. array('option' => array('value' => '2006')),
  6055. '2006',
  6056. '/option',
  6057. '/select',
  6058. );
  6059. $this->assertTags($result, $expected);
  6060. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6061. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  6062. $expected = array(
  6063. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6064. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6065. '2007',
  6066. '/option',
  6067. array('option' => array('value' => '2006')),
  6068. '2006',
  6069. '/option',
  6070. '/select',
  6071. );
  6072. $this->assertTags($result, $expected);
  6073. $this->Form->request->data['Contact']['published'] = '';
  6074. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  6075. $expected = array(
  6076. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6077. array('option' => array('value' => '2008')),
  6078. '2008',
  6079. '/option',
  6080. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6081. '2007',
  6082. '/option',
  6083. array('option' => array('value' => '2006')),
  6084. '2006',
  6085. '/option',
  6086. '/select',
  6087. );
  6088. $this->assertTags($result, $expected);
  6089. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6090. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  6091. $expected = array(
  6092. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6093. array('option' => array('value' => '2008')),
  6094. '2008',
  6095. '/option',
  6096. array('option' => array('value' => '2007')),
  6097. '2007',
  6098. '/option',
  6099. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6100. '2006',
  6101. '/option',
  6102. '/select',
  6103. );
  6104. $this->assertTags($result, $expected);
  6105. $this->Form->request->data = array();
  6106. $this->Form->create('Contact');
  6107. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  6108. $expected = array(
  6109. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6110. array('option' => array('value' => '2008')),
  6111. '2008',
  6112. '/option',
  6113. array('option' => array('value' => '2007')),
  6114. '2007',
  6115. '/option',
  6116. array('option' => array('value' => '2006')),
  6117. '2006',
  6118. '/option',
  6119. '/select',
  6120. );
  6121. $this->assertTags($result, $expected);
  6122. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  6123. $this->assertContains('data[Contact][published][year]', $result);
  6124. }
  6125. /**
  6126. * testYearAutoExpandRange method
  6127. *
  6128. * @return void
  6129. */
  6130. public function testYearAutoExpandRange() {
  6131. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6132. $result = $this->Form->year('User.birthday');
  6133. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6134. $result = $matches[1];
  6135. $expected = range(date('Y') + 20, 1930);
  6136. $this->assertEquals($result, $expected);
  6137. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6138. $result = $this->Form->year('Project.release');
  6139. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6140. $result = $matches[1];
  6141. $expected = range(2050, date('Y') - 20);
  6142. $this->assertEquals($result, $expected);
  6143. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6144. $result = $this->Form->year('Project.release', 1890, 1900);
  6145. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6146. $result = $matches[1];
  6147. $expected = range(1900, 1881);
  6148. $this->assertEquals($result, $expected);
  6149. }
  6150. /**
  6151. * testTextArea method
  6152. *
  6153. * @return void
  6154. */
  6155. public function testTextArea() {
  6156. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  6157. $result = $this->Form->textarea('Model.field');
  6158. $expected = array(
  6159. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6160. 'some test data',
  6161. '/textarea',
  6162. );
  6163. $this->assertTags($result, $expected);
  6164. $result = $this->Form->textarea('Model.tmp');
  6165. $expected = array(
  6166. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  6167. '/textarea',
  6168. );
  6169. $this->assertTags($result, $expected);
  6170. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6171. $result = $this->Form->textarea('Model.field');
  6172. $expected = array(
  6173. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6174. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6175. '/textarea',
  6176. );
  6177. $this->assertTags($result, $expected);
  6178. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6179. $result = $this->Form->textarea('Model.field', array('escape' => false));
  6180. $expected = array(
  6181. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6182. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6183. '/textarea',
  6184. );
  6185. $this->assertTags($result, $expected);
  6186. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  6187. $result = $this->Form->textarea('Model.0.OtherModel.field');
  6188. $expected = array(
  6189. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  6190. '/textarea'
  6191. );
  6192. $this->assertTags($result, $expected);
  6193. }
  6194. /**
  6195. * testTextAreaWithStupidCharacters method
  6196. *
  6197. * test text area with non-ascii characters
  6198. *
  6199. * @return void
  6200. */
  6201. public function testTextAreaWithStupidCharacters() {
  6202. $this->loadFixtures('Post');
  6203. $result = $this->Form->input('Post.content', array(
  6204. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  6205. ));
  6206. $expected = array(
  6207. 'div' => array('class' => 'input textarea'),
  6208. 'label' => array('for' => 'PostContent'),
  6209. 'Current Text',
  6210. '/label',
  6211. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  6212. 'GREAT®',
  6213. '/textarea',
  6214. '/div'
  6215. );
  6216. $this->assertTags($result, $expected);
  6217. }
  6218. /**
  6219. * testHiddenField method
  6220. *
  6221. * @return void
  6222. */
  6223. public function testHiddenField() {
  6224. $Contact = ClassRegistry::getObject('Contact');
  6225. $Contact->validationErrors['field'] = 1;
  6226. $this->Form->request->data['Contact']['field'] = 'test';
  6227. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  6228. $this->assertTags($result, array(
  6229. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  6230. );
  6231. }
  6232. /**
  6233. * testFileUploadField method
  6234. *
  6235. * @return void
  6236. */
  6237. public function testFileUploadField() {
  6238. $result = $this->Form->file('Model.upload');
  6239. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6240. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  6241. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  6242. $expected = array(
  6243. 'div' => array('class' => 'input file'),
  6244. 'label' => array('for' => 'ModelUpload'),
  6245. 'Upload',
  6246. '/label',
  6247. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  6248. '/div'
  6249. );
  6250. $this->assertTags($result, $expected);
  6251. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6252. $result = $this->Form->file('Model.upload');
  6253. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6254. }
  6255. /**
  6256. * test File upload input on a model not used in create();
  6257. *
  6258. * @return void
  6259. */
  6260. public function testFileUploadOnOtherModel() {
  6261. $this->Form->create('ValidateUser', array('type' => 'file'));
  6262. $result = $this->Form->file('ValidateProfile.city');
  6263. $expected = array(
  6264. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  6265. );
  6266. $this->assertTags($result, $expected);
  6267. }
  6268. /**
  6269. * testButton method
  6270. *
  6271. * @return void
  6272. */
  6273. public function testButton() {
  6274. $result = $this->Form->button('Hi');
  6275. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  6276. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  6277. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  6278. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  6279. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  6280. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  6281. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  6282. $result = $this->Form->button('No type', array('type' => false));
  6283. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  6284. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  6285. $this->assertNotRegExp('/\&039/', $result);
  6286. }
  6287. /**
  6288. * Test that button() makes unlocked fields by default.
  6289. *
  6290. * @return void
  6291. */
  6292. public function testButtonUnlockedByDefault() {
  6293. $this->Form->request->params['_Token']['key'] = 'secured';
  6294. $this->Form->button('Save', array('name' => 'save'));
  6295. $this->Form->button('Clear');
  6296. $result = $this->Form->unlockField();
  6297. $this->assertEquals(array('save'), $result);
  6298. }
  6299. /**
  6300. * testPostButton method
  6301. *
  6302. * @return void
  6303. */
  6304. public function testPostButton() {
  6305. $result = $this->Form->postButton('Hi', '/controller/action');
  6306. $this->assertTags($result, array(
  6307. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  6308. 'div' => array('style' => 'display:none;'),
  6309. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6310. '/div',
  6311. 'button' => array('type' => 'submit'),
  6312. 'Hi',
  6313. '/button',
  6314. '/form'
  6315. ));
  6316. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  6317. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  6318. }
  6319. /**
  6320. * Test that postButton adds _Token fields.
  6321. *
  6322. * @return void
  6323. */
  6324. public function testSecurePostButton() {
  6325. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6326. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6327. $expected = array(
  6328. 'form' => array(
  6329. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6330. ),
  6331. array('div' => array('style' => 'display:none;')),
  6332. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6333. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6334. '/div',
  6335. 'button' => array('type' => 'submit'),
  6336. 'Delete',
  6337. '/button',
  6338. array('div' => array('style' => 'display:none;')),
  6339. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6340. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6341. '/div',
  6342. '/form',
  6343. );
  6344. $this->assertTags($result, $expected);
  6345. }
  6346. /**
  6347. * testPostLink method
  6348. *
  6349. * @return void
  6350. */
  6351. public function testPostLink() {
  6352. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6353. $this->assertTags($result, array(
  6354. 'form' => array(
  6355. 'method' => 'post', 'action' => '/posts/delete/1',
  6356. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6357. ),
  6358. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6359. '/form',
  6360. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6361. 'Delete',
  6362. '/a'
  6363. ));
  6364. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  6365. $this->assertTags($result, array(
  6366. 'form' => array(
  6367. 'method' => 'post', 'action' => '/posts/delete/1',
  6368. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6369. ),
  6370. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  6371. '/form',
  6372. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6373. 'Delete',
  6374. '/a'
  6375. ));
  6376. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  6377. $this->assertTags($result, array(
  6378. 'form' => array(
  6379. 'method' => 'post', 'action' => '/posts/delete/1',
  6380. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6381. ),
  6382. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6383. '/form',
  6384. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6385. 'Delete',
  6386. '/a'
  6387. ));
  6388. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  6389. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  6390. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  6391. $this->assertTags($result, array(
  6392. 'form' => array(
  6393. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6394. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6395. ),
  6396. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6397. '/form',
  6398. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6399. 'Delete',
  6400. '/a'
  6401. ));
  6402. }
  6403. /**
  6404. * Test that postLink adds _Token fields.
  6405. *
  6406. * @return void
  6407. */
  6408. public function testSecurePostLink() {
  6409. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6410. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6411. $expected = array(
  6412. 'form' => array(
  6413. 'method' => 'post', 'action' => '/posts/delete/1',
  6414. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6415. ),
  6416. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6417. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6418. 'div' => array('style' => 'display:none;'),
  6419. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6420. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6421. '/div',
  6422. '/form',
  6423. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6424. 'Delete',
  6425. '/a'
  6426. );
  6427. $this->assertTags($result, $expected);
  6428. }
  6429. /**
  6430. * testSubmitButton method
  6431. *
  6432. * @return void
  6433. */
  6434. public function testSubmitButton() {
  6435. $result = $this->Form->submit('');
  6436. $expected = array(
  6437. 'div' => array('class' => 'submit'),
  6438. 'input' => array('type' => 'submit', 'value' => ''),
  6439. '/div'
  6440. );
  6441. $this->assertTags($result, $expected);
  6442. $result = $this->Form->submit('Test Submit');
  6443. $expected = array(
  6444. 'div' => array('class' => 'submit'),
  6445. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6446. '/div'
  6447. );
  6448. $this->assertTags($result, $expected);
  6449. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  6450. $expected = array(
  6451. 'span' => array('class' => 'submit'),
  6452. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6453. '/span'
  6454. );
  6455. $this->assertTags($result, $expected);
  6456. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  6457. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  6458. $this->assertTags($result, $expected);
  6459. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  6460. $expected = array(
  6461. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  6462. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6463. '/div'
  6464. );
  6465. $this->assertTags($result, $expected);
  6466. $result = $this->Form->submit('Next >');
  6467. $expected = array(
  6468. 'div' => array('class' => 'submit'),
  6469. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  6470. '/div'
  6471. );
  6472. $this->assertTags($result, $expected);
  6473. $result = $this->Form->submit('Next >', array('escape' => false));
  6474. $expected = array(
  6475. 'div' => array('class' => 'submit'),
  6476. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  6477. '/div'
  6478. );
  6479. $this->assertTags($result, $expected);
  6480. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  6481. $expected = array(
  6482. 'div' => array('class' => 'submit'),
  6483. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  6484. '/div'
  6485. );
  6486. $this->assertTags($result, $expected);
  6487. $before = '--before--';
  6488. $after = '--after--';
  6489. $result = $this->Form->submit('Test', array('before' => $before));
  6490. $expected = array(
  6491. 'div' => array('class' => 'submit'),
  6492. '--before--',
  6493. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6494. '/div'
  6495. );
  6496. $this->assertTags($result, $expected);
  6497. $result = $this->Form->submit('Test', array('after' => $after));
  6498. $expected = array(
  6499. 'div' => array('class' => 'submit'),
  6500. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6501. '--after--',
  6502. '/div'
  6503. );
  6504. $this->assertTags($result, $expected);
  6505. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  6506. $expected = array(
  6507. 'div' => array('class' => 'submit'),
  6508. '--before--',
  6509. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6510. '--after--',
  6511. '/div'
  6512. );
  6513. $this->assertTags($result, $expected);
  6514. }
  6515. /**
  6516. * test image submit types.
  6517. *
  6518. * @return void
  6519. */
  6520. public function testSubmitImage() {
  6521. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6522. $expected = array(
  6523. 'div' => array('class' => 'submit'),
  6524. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  6525. '/div'
  6526. );
  6527. $this->assertTags($result, $expected);
  6528. $result = $this->Form->submit('/relative/cake.power.gif');
  6529. $expected = array(
  6530. 'div' => array('class' => 'submit'),
  6531. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  6532. '/div'
  6533. );
  6534. $this->assertTags($result, $expected);
  6535. $result = $this->Form->submit('cake.power.gif');
  6536. $expected = array(
  6537. 'div' => array('class' => 'submit'),
  6538. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6539. '/div'
  6540. );
  6541. $this->assertTags($result, $expected);
  6542. $result = $this->Form->submit('Not.an.image');
  6543. $expected = array(
  6544. 'div' => array('class' => 'submit'),
  6545. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6546. '/div'
  6547. );
  6548. $this->assertTags($result, $expected);
  6549. $after = '--after--';
  6550. $before = '--before--';
  6551. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  6552. $expected = array(
  6553. 'div' => array('class' => 'submit'),
  6554. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6555. '--after--',
  6556. '/div'
  6557. );
  6558. $this->assertTags($result, $expected);
  6559. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  6560. $expected = array(
  6561. 'div' => array('class' => 'submit'),
  6562. '--before--',
  6563. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6564. '/div'
  6565. );
  6566. $this->assertTags($result, $expected);
  6567. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  6568. $expected = array(
  6569. 'div' => array('class' => 'submit'),
  6570. '--before--',
  6571. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6572. '--after--',
  6573. '/div'
  6574. );
  6575. $this->assertTags($result, $expected);
  6576. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  6577. $expected = array(
  6578. 'div' => array('class' => 'submit'),
  6579. '--before--',
  6580. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6581. '--after--',
  6582. '/div'
  6583. );
  6584. $this->assertTags($result, $expected);
  6585. }
  6586. /**
  6587. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6588. * be submitted at a time.
  6589. *
  6590. * @return void
  6591. */
  6592. public function testSubmitUnlockedByDefault() {
  6593. $this->Form->request->params['_Token']['key'] = 'secured';
  6594. $this->Form->submit('Go go');
  6595. $this->Form->submit('Save', array('name' => 'save'));
  6596. $result = $this->Form->unlockField();
  6597. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  6598. }
  6599. /**
  6600. * Test submit image with timestamps.
  6601. *
  6602. * @return void
  6603. */
  6604. public function testSubmitImageTimestamp() {
  6605. Configure::write('Asset.timestamp', 'force');
  6606. $result = $this->Form->submit('cake.power.gif');
  6607. $expected = array(
  6608. 'div' => array('class' => 'submit'),
  6609. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  6610. '/div'
  6611. );
  6612. $this->assertTags($result, $expected);
  6613. }
  6614. /**
  6615. * test the create() method
  6616. *
  6617. * @return void
  6618. */
  6619. public function testCreate() {
  6620. $result = $this->Form->create('Contact');
  6621. $encoding = strtolower(Configure::read('App.encoding'));
  6622. $expected = array(
  6623. 'form' => array(
  6624. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6625. 'accept-charset' => $encoding
  6626. ),
  6627. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  6628. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6629. '/div'
  6630. );
  6631. $this->assertTags($result, $expected);
  6632. $result = $this->Form->create('Contact', array('type' => 'GET'));
  6633. $expected = array('form' => array(
  6634. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6635. 'accept-charset' => $encoding
  6636. ));
  6637. $this->assertTags($result, $expected);
  6638. $result = $this->Form->create('Contact', array('type' => 'get'));
  6639. $expected = array('form' => array(
  6640. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6641. 'accept-charset' => $encoding
  6642. ));
  6643. $this->assertTags($result, $expected);
  6644. $result = $this->Form->create('Contact', array('type' => 'put'));
  6645. $expected = array(
  6646. 'form' => array(
  6647. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6648. 'accept-charset' => $encoding
  6649. ),
  6650. 'div' => array('style' => 'display:none;'),
  6651. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6652. '/div'
  6653. );
  6654. $this->assertTags($result, $expected);
  6655. $result = $this->Form->create('Contact', array('type' => 'file'));
  6656. $expected = array(
  6657. 'form' => array(
  6658. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6659. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6660. ),
  6661. 'div' => array('style' => 'display:none;'),
  6662. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6663. '/div'
  6664. );
  6665. $this->assertTags($result, $expected);
  6666. $this->Form->request->data['Contact']['id'] = 1;
  6667. $this->Form->request->here = '/contacts/edit/1';
  6668. $this->Form->request['action'] = 'edit';
  6669. $result = $this->Form->create('Contact');
  6670. $expected = array(
  6671. 'form' => array(
  6672. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6673. 'accept-charset' => $encoding
  6674. ),
  6675. 'div' => array('style' => 'display:none;'),
  6676. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6677. '/div'
  6678. );
  6679. $this->assertTags($result, $expected);
  6680. $this->Form->request->data['Contact']['id'] = 1;
  6681. $this->Form->request->here = '/contacts/edit/1';
  6682. $this->Form->request['action'] = 'edit';
  6683. $result = $this->Form->create('Contact', array('type' => 'file'));
  6684. $expected = array(
  6685. 'form' => array(
  6686. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6687. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6688. ),
  6689. 'div' => array('style' => 'display:none;'),
  6690. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6691. '/div'
  6692. );
  6693. $this->assertTags($result, $expected);
  6694. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  6695. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  6696. $expected = array(
  6697. 'form' => array(
  6698. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  6699. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  6700. ),
  6701. 'div' => array('style' => 'display:none;'),
  6702. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6703. '/div'
  6704. );
  6705. $this->assertTags($result, $expected);
  6706. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  6707. $expected = array(
  6708. 'form' => array(
  6709. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  6710. 'accept-charset' => $encoding
  6711. ),
  6712. 'div' => array('style' => 'display:none;'),
  6713. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6714. '/div'
  6715. );
  6716. $this->assertTags($result, $expected);
  6717. $this->Form->request['action'] = 'add';
  6718. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  6719. $expected = array(
  6720. 'form' => array(
  6721. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  6722. 'accept-charset' => $encoding
  6723. ),
  6724. 'div' => array('style' => 'display:none;'),
  6725. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6726. '/div'
  6727. );
  6728. $this->assertTags($result, $expected);
  6729. $result = $this->Form->create('User', array('action' => 'login'));
  6730. $expected = array(
  6731. 'form' => array(
  6732. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  6733. 'accept-charset' => $encoding
  6734. ),
  6735. 'div' => array('style' => 'display:none;'),
  6736. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6737. '/div'
  6738. );
  6739. $this->assertTags($result, $expected);
  6740. $result = $this->Form->create('User', array('url' => '/users/login'));
  6741. $expected = array(
  6742. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  6743. 'div' => array('style' => 'display:none;'),
  6744. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6745. '/div'
  6746. );
  6747. $this->assertTags($result, $expected);
  6748. $this->Form->request['controller'] = 'pages';
  6749. $result = $this->Form->create('User', array('action' => 'signup'));
  6750. $expected = array(
  6751. 'form' => array(
  6752. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  6753. 'accept-charset' => $encoding
  6754. ),
  6755. 'div' => array('style' => 'display:none;'),
  6756. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6757. '/div'
  6758. );
  6759. $this->assertTags($result, $expected);
  6760. $this->Form->request->data = array();
  6761. $this->Form->request['controller'] = 'contacts';
  6762. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6763. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  6764. $expected = array(
  6765. 'form' => array(
  6766. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  6767. 'accept-charset' => 'utf-8'
  6768. ),
  6769. 'div' => array('style' => 'display:none;'),
  6770. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6771. '/div'
  6772. );
  6773. $this->assertTags($result, $expected);
  6774. }
  6775. /**
  6776. * Test the onsubmit option for create()
  6777. *
  6778. * @return void
  6779. */
  6780. public function testCreateOnSubmit() {
  6781. $this->Form->request->data = array();
  6782. $this->Form->request['controller'] = 'contacts';
  6783. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6784. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  6785. $expected = array(
  6786. 'form' => array(
  6787. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  6788. 'accept-charset' => 'utf-8'
  6789. ),
  6790. 'div' => array('style' => 'display:none;'),
  6791. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6792. '/div'
  6793. );
  6794. $this->assertTags($result, $expected);
  6795. $this->Form->request->data = array();
  6796. $this->Form->request['controller'] = 'contacts';
  6797. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6798. $result = $this->Form->create(array(
  6799. 'url' => array('action' => 'index', 'param'),
  6800. 'default' => false,
  6801. 'onsubmit' => 'someFunction();'
  6802. ));
  6803. $expected = array(
  6804. 'form' => array(
  6805. 'id' => 'ContactAddForm', 'method' => 'post',
  6806. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6807. 'action' => '/contacts/index/param',
  6808. 'accept-charset' => 'utf-8'
  6809. ),
  6810. 'div' => array('style' => 'display:none;'),
  6811. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6812. '/div'
  6813. );
  6814. $this->assertTags($result, $expected);
  6815. }
  6816. /**
  6817. * test create() with automatic url generation
  6818. *
  6819. * @return void
  6820. */
  6821. public function testCreateAutoUrl() {
  6822. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6823. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6824. $this->Form->request->base = '/base_url';
  6825. $result = $this->Form->create('Contact');
  6826. $expected = array(
  6827. 'form' => array(
  6828. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6829. 'accept-charset' => 'utf-8'
  6830. ),
  6831. 'div' => array('style' => 'display:none;'),
  6832. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6833. '/div'
  6834. );
  6835. $this->assertTags($result, $expected);
  6836. $this->Form->request['action'] = 'delete';
  6837. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6838. $this->Form->request->base = '/base_url';
  6839. $result = $this->Form->create('Contact');
  6840. $expected = array(
  6841. 'form' => array(
  6842. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6843. 'accept-charset' => 'utf-8'
  6844. ),
  6845. 'div' => array('style' => 'display:none;'),
  6846. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6847. '/div'
  6848. );
  6849. $this->assertTags($result, $expected);
  6850. }
  6851. /**
  6852. * test create() with a custom route
  6853. *
  6854. * @return void
  6855. */
  6856. public function testCreateCustomRoute() {
  6857. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6858. $encoding = strtolower(Configure::read('App.encoding'));
  6859. $result = $this->Form->create('User', array('action' => 'login'));
  6860. $expected = array(
  6861. 'form' => array(
  6862. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6863. 'accept-charset' => $encoding
  6864. ),
  6865. 'div' => array('style' => 'display:none;'),
  6866. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6867. '/div'
  6868. );
  6869. $this->assertTags($result, $expected);
  6870. }
  6871. /**
  6872. * test that inputDefaults are stored and used.
  6873. *
  6874. * @return void
  6875. */
  6876. public function testCreateWithInputDefaults() {
  6877. $this->Form->create('User', array(
  6878. 'inputDefaults' => array(
  6879. 'div' => false,
  6880. 'label' => false,
  6881. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6882. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6883. )
  6884. ));
  6885. $result = $this->Form->input('username');
  6886. $expected = array(
  6887. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6888. );
  6889. $this->assertTags($result, $expected);
  6890. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6891. $expected = array(
  6892. 'div' => array('class' => 'input text'),
  6893. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6894. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6895. '/div'
  6896. );
  6897. $this->assertTags($result, $expected);
  6898. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  6899. $expected = array(
  6900. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6901. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  6902. );
  6903. $this->assertTags($result, $expected);
  6904. $this->Form->create('User', array(
  6905. 'inputDefaults' => array(
  6906. 'div' => false,
  6907. 'label' => array('class' => 'nice', 'for' => 'changed'),
  6908. )
  6909. ));
  6910. $result = $this->Form->input('username', array('div' => true));
  6911. $expected = array(
  6912. 'div' => array('class' => 'input text'),
  6913. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  6914. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6915. '/div'
  6916. );
  6917. $this->assertTags($result, $expected);
  6918. }
  6919. /**
  6920. * test automatic accept-charset overriding
  6921. *
  6922. * @return void
  6923. */
  6924. public function testCreateWithAcceptCharset() {
  6925. $result = $this->Form->create('UserForm', array(
  6926. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  6927. )
  6928. );
  6929. $expected = array(
  6930. 'form' => array(
  6931. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6932. 'accept-charset' => 'iso-8859-1'
  6933. ),
  6934. 'div' => array('style' => 'display:none;'),
  6935. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6936. '/div'
  6937. );
  6938. $this->assertTags($result, $expected);
  6939. }
  6940. /**
  6941. * Test base form url when url param is passed with multiple parameters (&)
  6942. *
  6943. */
  6944. public function testCreateQuerystringrequest() {
  6945. $encoding = strtolower(Configure::read('App.encoding'));
  6946. $result = $this->Form->create('Contact', array(
  6947. 'type' => 'post',
  6948. 'escape' => false,
  6949. 'url' => array(
  6950. 'controller' => 'controller',
  6951. 'action' => 'action',
  6952. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6953. )
  6954. ));
  6955. $expected = array(
  6956. 'form' => array(
  6957. 'id' => 'ContactAddForm',
  6958. 'method' => 'post',
  6959. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6960. 'accept-charset' => $encoding
  6961. ),
  6962. 'div' => array('style' => 'display:none;'),
  6963. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6964. '/div'
  6965. );
  6966. $this->assertTags($result, $expected);
  6967. $result = $this->Form->create('Contact', array(
  6968. 'type' => 'post',
  6969. 'url' => array(
  6970. 'controller' => 'controller',
  6971. 'action' => 'action',
  6972. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6973. )
  6974. ));
  6975. $expected = array(
  6976. 'form' => array(
  6977. 'id' => 'ContactAddForm',
  6978. 'method' => 'post',
  6979. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6980. 'accept-charset' => $encoding
  6981. ),
  6982. 'div' => array('style' => 'display:none;'),
  6983. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6984. '/div'
  6985. );
  6986. $this->assertTags($result, $expected);
  6987. }
  6988. /**
  6989. * test that create() doesn't cause errors by multiple id's being in the primary key
  6990. * as could happen with multiple select or checkboxes.
  6991. *
  6992. * @return void
  6993. */
  6994. public function testCreateWithMultipleIdInData() {
  6995. $encoding = strtolower(Configure::read('App.encoding'));
  6996. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6997. $result = $this->Form->create('Contact');
  6998. $expected = array(
  6999. 'form' => array(
  7000. 'id' => 'ContactAddForm',
  7001. 'method' => 'post',
  7002. 'action' => '/contacts/add',
  7003. 'accept-charset' => $encoding
  7004. ),
  7005. 'div' => array('style' => 'display:none;'),
  7006. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7007. '/div'
  7008. );
  7009. $this->assertTags($result, $expected);
  7010. }
  7011. /**
  7012. * test that create() doesn't add in extra passed params.
  7013. *
  7014. * @return void
  7015. */
  7016. public function testCreatePassedArgs() {
  7017. $encoding = strtolower(Configure::read('App.encoding'));
  7018. $this->Form->request->data['Contact']['id'] = 1;
  7019. $result = $this->Form->create('Contact', array(
  7020. 'type' => 'post',
  7021. 'escape' => false,
  7022. 'url' => array(
  7023. 'action' => 'edit',
  7024. 'myparam'
  7025. )
  7026. ));
  7027. $expected = array(
  7028. 'form' => array(
  7029. 'id' => 'ContactAddForm',
  7030. 'method' => 'post',
  7031. 'action' => '/contacts/edit/myparam',
  7032. 'accept-charset' => $encoding
  7033. ),
  7034. 'div' => array('style' => 'display:none;'),
  7035. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7036. '/div'
  7037. );
  7038. $this->assertTags($result, $expected);
  7039. }
  7040. /**
  7041. * test creating a get form, and get form inputs.
  7042. *
  7043. * @return void
  7044. */
  7045. public function testGetFormCreate() {
  7046. $encoding = strtolower(Configure::read('App.encoding'));
  7047. $result = $this->Form->create('Contact', array('type' => 'get'));
  7048. $this->assertTags($result, array('form' => array(
  7049. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7050. 'accept-charset' => $encoding
  7051. )));
  7052. $result = $this->Form->text('Contact.name');
  7053. $this->assertTags($result, array('input' => array(
  7054. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  7055. )));
  7056. $result = $this->Form->password('password');
  7057. $this->assertTags($result, array('input' => array(
  7058. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  7059. )));
  7060. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  7061. $result = $this->Form->text('user_form');
  7062. $this->assertTags($result, array('input' => array(
  7063. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  7064. )));
  7065. }
  7066. /**
  7067. * test get form, and inputs when the model param is false
  7068. *
  7069. * @return void
  7070. */
  7071. public function testGetFormWithFalseModel() {
  7072. $encoding = strtolower(Configure::read('App.encoding'));
  7073. $this->Form->request['controller'] = 'contact_test';
  7074. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  7075. $expected = array('form' => array(
  7076. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  7077. 'accept-charset' => $encoding
  7078. ));
  7079. $this->assertTags($result, $expected);
  7080. $result = $this->Form->text('reason');
  7081. $expected = array(
  7082. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  7083. );
  7084. $this->assertTags($result, $expected);
  7085. }
  7086. /**
  7087. * test that datetime() works with GET style forms.
  7088. *
  7089. * @return void
  7090. */
  7091. public function testDateTimeWithGetForms() {
  7092. extract($this->dateRegex);
  7093. $this->Form->create('Contact', array('type' => 'get'));
  7094. $result = $this->Form->datetime('created');
  7095. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  7096. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  7097. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  7098. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  7099. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  7100. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  7101. }
  7102. /**
  7103. * testEditFormWithData method
  7104. *
  7105. * test auto populating form elements from submitted data.
  7106. *
  7107. * @return void
  7108. */
  7109. public function testEditFormWithData() {
  7110. $this->Form->request->data = array('Person' => array(
  7111. 'id' => 1,
  7112. 'first_name' => 'Nate',
  7113. 'last_name' => 'Abele',
  7114. 'email' => 'nate@example.com'
  7115. ));
  7116. $this->Form->request->addParams(array(
  7117. 'models' => array('Person'),
  7118. 'controller' => 'people',
  7119. 'action' => 'add'
  7120. ));
  7121. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  7122. $this->Form->create();
  7123. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  7124. $expected = array(
  7125. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  7126. 'select' => array(
  7127. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  7128. ),
  7129. array('option' => array('value' => 1)), 'Nate', '/option',
  7130. array('option' => array('value' => 2)), 'Garrett', '/option',
  7131. array('option' => array('value' => 3)), 'Larry', '/option',
  7132. '/select'
  7133. );
  7134. $this->assertTags($result, $expected);
  7135. }
  7136. /**
  7137. * Test that required fields are created for various types of validation.
  7138. *
  7139. * @return void
  7140. */
  7141. public function testFormInputRequiredDetection() {
  7142. $this->Form->create('Contact');
  7143. $result = $this->Form->input('Contact.non_existing');
  7144. $expected = array(
  7145. 'div' => array('class' => 'input text'),
  7146. 'label' => array('for' => 'ContactNonExisting'),
  7147. 'Non Existing',
  7148. '/label',
  7149. 'input' => array(
  7150. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  7151. 'id' => 'ContactNonExisting'
  7152. ),
  7153. '/div'
  7154. );
  7155. $this->assertTags($result, $expected);
  7156. $result = $this->Form->input('Contact.imrequired');
  7157. $expected = array(
  7158. 'div' => array('class' => 'input text required'),
  7159. 'label' => array('for' => 'ContactImrequired'),
  7160. 'Imrequired',
  7161. '/label',
  7162. 'input' => array(
  7163. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7164. 'id' => 'ContactImrequired',
  7165. 'required' => 'required'
  7166. ),
  7167. '/div'
  7168. );
  7169. $this->assertTags($result, $expected);
  7170. $result = $this->Form->input('Contact.imalsorequired');
  7171. $expected = array(
  7172. 'div' => array('class' => 'input text required'),
  7173. 'label' => array('for' => 'ContactImalsorequired'),
  7174. 'Imalsorequired',
  7175. '/label',
  7176. 'input' => array(
  7177. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  7178. 'id' => 'ContactImalsorequired',
  7179. 'required' => 'required'
  7180. ),
  7181. '/div'
  7182. );
  7183. $this->assertTags($result, $expected);
  7184. $result = $this->Form->input('Contact.imrequiredtoo');
  7185. $expected = array(
  7186. 'div' => array('class' => 'input text required'),
  7187. 'label' => array('for' => 'ContactImrequiredtoo'),
  7188. 'Imrequiredtoo',
  7189. '/label',
  7190. 'input' => array(
  7191. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  7192. 'id' => 'ContactImrequiredtoo',
  7193. 'required' => 'required'
  7194. ),
  7195. '/div'
  7196. );
  7197. $this->assertTags($result, $expected);
  7198. $result = $this->Form->input('Contact.required_one');
  7199. $expected = array(
  7200. 'div' => array('class' => 'input text required'),
  7201. 'label' => array('for' => 'ContactRequiredOne'),
  7202. 'Required One',
  7203. '/label',
  7204. 'input' => array(
  7205. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  7206. 'id' => 'ContactRequiredOne',
  7207. 'required' => 'required'
  7208. ),
  7209. '/div'
  7210. );
  7211. $this->assertTags($result, $expected);
  7212. $result = $this->Form->input('Contact.string_required');
  7213. $expected = array(
  7214. 'div' => array('class' => 'input text required'),
  7215. 'label' => array('for' => 'ContactStringRequired'),
  7216. 'String Required',
  7217. '/label',
  7218. 'input' => array(
  7219. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  7220. 'id' => 'ContactStringRequired',
  7221. 'required' => 'required'
  7222. ),
  7223. '/div'
  7224. );
  7225. $this->assertTags($result, $expected);
  7226. $result = $this->Form->input('Contact.imnotrequired');
  7227. $expected = array(
  7228. 'div' => array('class' => 'input text'),
  7229. 'label' => array('for' => 'ContactImnotrequired'),
  7230. 'Imnotrequired',
  7231. '/label',
  7232. 'input' => array(
  7233. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  7234. 'id' => 'ContactImnotrequired'
  7235. ),
  7236. '/div'
  7237. );
  7238. $this->assertTags($result, $expected);
  7239. $result = $this->Form->input('Contact.imalsonotrequired');
  7240. $expected = array(
  7241. 'div' => array('class' => 'input text'),
  7242. 'label' => array('for' => 'ContactImalsonotrequired'),
  7243. 'Imalsonotrequired',
  7244. '/label',
  7245. 'input' => array(
  7246. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  7247. 'id' => 'ContactImalsonotrequired'
  7248. ),
  7249. '/div'
  7250. );
  7251. $this->assertTags($result, $expected);
  7252. $result = $this->Form->input('Contact.imalsonotrequired2');
  7253. $expected = array(
  7254. 'div' => array('class' => 'input text'),
  7255. 'label' => array('for' => 'ContactImalsonotrequired2'),
  7256. 'Imalsonotrequired2',
  7257. '/label',
  7258. 'input' => array(
  7259. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  7260. 'id' => 'ContactImalsonotrequired2'
  7261. ),
  7262. '/div'
  7263. );
  7264. $this->assertTags($result, $expected);
  7265. $result = $this->Form->input('Contact.imnotrequiredeither');
  7266. $expected = array(
  7267. 'div' => array('class' => 'input text'),
  7268. 'label' => array('for' => 'ContactImnotrequiredeither'),
  7269. 'Imnotrequiredeither',
  7270. '/label',
  7271. 'input' => array(
  7272. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  7273. 'id' => 'ContactImnotrequiredeither'
  7274. ),
  7275. '/div'
  7276. );
  7277. $this->assertTags($result, $expected);
  7278. $result = $this->Form->input('Contact.iamrequiredalways');
  7279. $expected = array(
  7280. 'div' => array('class' => 'input text required'),
  7281. 'label' => array('for' => 'ContactIamrequiredalways'),
  7282. 'Iamrequiredalways',
  7283. '/label',
  7284. 'input' => array(
  7285. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  7286. 'id' => 'ContactIamrequiredalways',
  7287. 'required' => 'required'
  7288. ),
  7289. '/div'
  7290. );
  7291. $this->assertTags($result, $expected);
  7292. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  7293. $expected = array(
  7294. 'div' => array('class' => 'input checkbox required'),
  7295. array('input' => array(
  7296. 'type' => 'hidden',
  7297. 'name' => 'data[Contact][boolean_field]',
  7298. 'id' => 'ContactBooleanField_',
  7299. 'value' => '0'
  7300. )),
  7301. array('input' => array(
  7302. 'type' => 'checkbox',
  7303. 'name' => 'data[Contact][boolean_field]',
  7304. 'value' => '1',
  7305. 'id' => 'ContactBooleanField'
  7306. )),
  7307. 'label' => array('for' => 'ContactBooleanField'),
  7308. 'Boolean Field',
  7309. '/label',
  7310. '/div'
  7311. );
  7312. $this->assertTags($result, $expected);
  7313. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  7314. $expected = array(
  7315. 'div' => array('class' => 'input checkbox required'),
  7316. array('input' => array(
  7317. 'type' => 'hidden',
  7318. 'name' => 'data[Contact][boolean_field]',
  7319. 'id' => 'ContactBooleanField_',
  7320. 'value' => '0'
  7321. )),
  7322. array('input' => array(
  7323. 'type' => 'checkbox',
  7324. 'name' => 'data[Contact][boolean_field]',
  7325. 'value' => '1',
  7326. 'id' => 'ContactBooleanField',
  7327. 'required' => 'required'
  7328. )),
  7329. 'label' => array('for' => 'ContactBooleanField'),
  7330. 'Boolean Field',
  7331. '/label',
  7332. '/div'
  7333. );
  7334. $this->assertTags($result, $expected);
  7335. }
  7336. /**
  7337. * testFormMagicInput method
  7338. *
  7339. * @return void
  7340. */
  7341. public function testFormMagicInput() {
  7342. $encoding = strtolower(Configure::read('App.encoding'));
  7343. $result = $this->Form->create('Contact');
  7344. $expected = array(
  7345. 'form' => array(
  7346. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7347. 'accept-charset' => $encoding
  7348. ),
  7349. 'div' => array('style' => 'display:none;'),
  7350. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7351. '/div'
  7352. );
  7353. $this->assertTags($result, $expected);
  7354. $result = $this->Form->input('name');
  7355. $expected = array(
  7356. 'div' => array('class' => 'input text'),
  7357. 'label' => array('for' => 'ContactName'),
  7358. 'Name',
  7359. '/label',
  7360. 'input' => array(
  7361. 'type' => 'text', 'name' => 'data[Contact][name]',
  7362. 'id' => 'ContactName', 'maxlength' => '255'
  7363. ),
  7364. '/div'
  7365. );
  7366. $this->assertTags($result, $expected);
  7367. $result = $this->Form->input('non_existing_field_in_contact_model');
  7368. $expected = array(
  7369. 'div' => array('class' => 'input text'),
  7370. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  7371. 'Non Existing Field In Contact Model',
  7372. '/label',
  7373. 'input' => array(
  7374. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  7375. 'id' => 'ContactNonExistingFieldInContactModel'
  7376. ),
  7377. '/div'
  7378. );
  7379. $this->assertTags($result, $expected);
  7380. $result = $this->Form->input('Address.street');
  7381. $expected = array(
  7382. 'div' => array('class' => 'input text'),
  7383. 'label' => array('for' => 'AddressStreet'),
  7384. 'Street',
  7385. '/label',
  7386. 'input' => array(
  7387. 'type' => 'text', 'name' => 'data[Address][street]',
  7388. 'id' => 'AddressStreet'
  7389. ),
  7390. '/div'
  7391. );
  7392. $this->assertTags($result, $expected);
  7393. $result = $this->Form->input('Address.non_existing_field_in_model');
  7394. $expected = array(
  7395. 'div' => array('class' => 'input text'),
  7396. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  7397. 'Non Existing Field In Model',
  7398. '/label',
  7399. 'input' => array(
  7400. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  7401. 'id' => 'AddressNonExistingFieldInModel'
  7402. ),
  7403. '/div'
  7404. );
  7405. $this->assertTags($result, $expected);
  7406. $result = $this->Form->input('name', array('div' => false));
  7407. $expected = array(
  7408. 'label' => array('for' => 'ContactName'),
  7409. 'Name',
  7410. '/label',
  7411. 'input' => array(
  7412. 'type' => 'text', 'name' => 'data[Contact][name]',
  7413. 'id' => 'ContactName', 'maxlength' => '255'
  7414. )
  7415. );
  7416. $this->assertTags($result, $expected);
  7417. extract($this->dateRegex);
  7418. $now = strtotime('now');
  7419. $result = $this->Form->input('Contact.published', array('div' => false));
  7420. $expected = array(
  7421. 'label' => array('for' => 'ContactPublishedMonth'),
  7422. 'Published',
  7423. '/label',
  7424. array('select' => array(
  7425. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  7426. )),
  7427. $monthsRegex,
  7428. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7429. date('F', $now),
  7430. '/option',
  7431. '*/select',
  7432. '-',
  7433. array('select' => array(
  7434. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  7435. )),
  7436. $daysRegex,
  7437. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7438. date('j', $now),
  7439. '/option',
  7440. '*/select',
  7441. '-',
  7442. array('select' => array(
  7443. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  7444. )),
  7445. $yearsRegex,
  7446. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7447. date('Y', $now),
  7448. '*/select'
  7449. );
  7450. $this->assertTags($result, $expected);
  7451. $result = $this->Form->input('Contact.updated', array('div' => false));
  7452. $expected = array(
  7453. 'label' => array('for' => 'ContactUpdatedMonth'),
  7454. 'Updated',
  7455. '/label',
  7456. array('select' => array(
  7457. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  7458. )),
  7459. $monthsRegex,
  7460. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7461. date('F', $now),
  7462. '/option',
  7463. '*/select',
  7464. '-',
  7465. array('select' => array(
  7466. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  7467. )),
  7468. $daysRegex,
  7469. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7470. date('j', $now),
  7471. '/option',
  7472. '*/select',
  7473. '-',
  7474. array('select' => array(
  7475. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  7476. )),
  7477. $yearsRegex,
  7478. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7479. date('Y', $now),
  7480. '*/select'
  7481. );
  7482. $this->assertTags($result, $expected);
  7483. $result = $this->Form->input('UserForm.stuff');
  7484. $expected = array(
  7485. 'div' => array('class' => 'input text'),
  7486. 'label' => array('for' => 'UserFormStuff'),
  7487. 'Stuff',
  7488. '/label',
  7489. 'input' => array(
  7490. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  7491. 'id' => 'UserFormStuff', 'maxlength' => 10
  7492. ),
  7493. '/div'
  7494. );
  7495. $this->assertTags($result, $expected);
  7496. }
  7497. /**
  7498. * testForMagicInputNonExistingNorValidated method
  7499. *
  7500. * @return void
  7501. */
  7502. public function testForMagicInputNonExistingNorValidated() {
  7503. $encoding = strtolower(Configure::read('App.encoding'));
  7504. $result = $this->Form->create('Contact');
  7505. $expected = array(
  7506. 'form' => array(
  7507. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7508. 'accept-charset' => $encoding
  7509. ),
  7510. 'div' => array('style' => 'display:none;'),
  7511. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7512. '/div'
  7513. );
  7514. $this->assertTags($result, $expected);
  7515. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7516. $expected = array(
  7517. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7518. 'Non Existing Nor Validated',
  7519. '/label',
  7520. 'input' => array(
  7521. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7522. 'id' => 'ContactNonExistingNorValidated'
  7523. )
  7524. );
  7525. $this->assertTags($result, $expected);
  7526. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  7527. 'div' => false, 'value' => 'my value'
  7528. ));
  7529. $expected = array(
  7530. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7531. 'Non Existing Nor Validated',
  7532. '/label',
  7533. 'input' => array(
  7534. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7535. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  7536. )
  7537. );
  7538. $this->assertTags($result, $expected);
  7539. $this->Form->request->data = array(
  7540. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  7541. ));
  7542. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7543. $expected = array(
  7544. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7545. 'Non Existing Nor Validated',
  7546. '/label',
  7547. 'input' => array(
  7548. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7549. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  7550. )
  7551. );
  7552. $this->assertTags($result, $expected);
  7553. }
  7554. /**
  7555. * testFormMagicInputLabel method
  7556. *
  7557. * @return void
  7558. */
  7559. public function testFormMagicInputLabel() {
  7560. $encoding = strtolower(Configure::read('App.encoding'));
  7561. $result = $this->Form->create('Contact');
  7562. $expected = array(
  7563. 'form' => array(
  7564. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7565. 'accept-charset' => $encoding
  7566. ),
  7567. 'div' => array('style' => 'display:none;'),
  7568. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7569. '/div'
  7570. );
  7571. $this->assertTags($result, $expected);
  7572. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  7573. $this->assertTags($result, array('input' => array(
  7574. 'name' => 'data[Contact][name]', 'type' => 'text',
  7575. 'id' => 'ContactName', 'maxlength' => '255')
  7576. ));
  7577. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  7578. $expected = array(
  7579. 'label' => array('for' => 'ContactName'),
  7580. 'My label',
  7581. '/label',
  7582. 'input' => array(
  7583. 'type' => 'text', 'name' => 'data[Contact][name]',
  7584. 'id' => 'ContactName', 'maxlength' => '255'
  7585. )
  7586. );
  7587. $this->assertTags($result, $expected);
  7588. $result = $this->Form->input('Contact.name', array(
  7589. 'div' => false, 'label' => array('class' => 'mandatory')
  7590. ));
  7591. $expected = array(
  7592. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7593. 'Name',
  7594. '/label',
  7595. 'input' => array(
  7596. 'type' => 'text', 'name' => 'data[Contact][name]',
  7597. 'id' => 'ContactName', 'maxlength' => '255'
  7598. )
  7599. );
  7600. $this->assertTags($result, $expected);
  7601. $result = $this->Form->input('Contact.name', array(
  7602. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  7603. ));
  7604. $expected = array(
  7605. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7606. 'My label',
  7607. '/label',
  7608. 'input' => array(
  7609. 'type' => 'text', 'name' => 'data[Contact][name]',
  7610. 'id' => 'ContactName', 'maxlength' => '255'
  7611. )
  7612. );
  7613. $this->assertTags($result, $expected);
  7614. $result = $this->Form->input('Contact.name', array(
  7615. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  7616. ));
  7617. $expected = array(
  7618. 'label' => array('for' => 'my_id'),
  7619. 'Name',
  7620. '/label',
  7621. 'input' => array(
  7622. 'type' => 'text', 'name' => 'data[Contact][name]',
  7623. 'id' => 'my_id', 'maxlength' => '255'
  7624. )
  7625. );
  7626. $this->assertTags($result, $expected);
  7627. $result = $this->Form->input('1.id');
  7628. $this->assertTags($result, array('input' => array(
  7629. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7630. 'id' => 'Contact1Id'
  7631. )));
  7632. $result = $this->Form->input("1.name");
  7633. $expected = array(
  7634. 'div' => array('class' => 'input text'),
  7635. 'label' => array('for' => 'Contact1Name'),
  7636. 'Name',
  7637. '/label',
  7638. 'input' => array(
  7639. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  7640. 'id' => 'Contact1Name', 'maxlength' => '255'
  7641. ),
  7642. '/div'
  7643. );
  7644. $this->assertTags($result, $expected);
  7645. $result = $this->Form->input('Contact.1.id');
  7646. $this->assertTags($result, array(
  7647. 'input' => array(
  7648. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7649. 'id' => 'Contact1Id'
  7650. )
  7651. ));
  7652. $result = $this->Form->input("Model.1.name");
  7653. $expected = array(
  7654. 'div' => array('class' => 'input text'),
  7655. 'label' => array('for' => 'Model1Name'),
  7656. 'Name',
  7657. '/label',
  7658. 'input' => array(
  7659. 'type' => 'text', 'name' => 'data[Model][1][name]',
  7660. 'id' => 'Model1Name'
  7661. ),
  7662. '/div'
  7663. );
  7664. $this->assertTags($result, $expected);
  7665. }
  7666. /**
  7667. * testFormEnd method
  7668. *
  7669. * @return void
  7670. */
  7671. public function testFormEnd() {
  7672. $this->assertEquals('</form>', $this->Form->end());
  7673. $result = $this->Form->end('');
  7674. $expected = array(
  7675. 'div' => array('class' => 'submit'),
  7676. 'input' => array('type' => 'submit', 'value' => ''),
  7677. '/div',
  7678. '/form'
  7679. );
  7680. $this->assertTags($result, $expected);
  7681. $result = $this->Form->end(array('label' => ''));
  7682. $expected = array(
  7683. 'div' => array('class' => 'submit'),
  7684. 'input' => array('type' => 'submit', 'value' => ''),
  7685. '/div',
  7686. '/form'
  7687. );
  7688. $this->assertTags($result, $expected);
  7689. $result = $this->Form->end('save');
  7690. $expected = array(
  7691. 'div' => array('class' => 'submit'),
  7692. 'input' => array('type' => 'submit', 'value' => 'save'),
  7693. '/div',
  7694. '/form'
  7695. );
  7696. $this->assertTags($result, $expected);
  7697. $result = $this->Form->end(array('label' => 'save'));
  7698. $expected = array(
  7699. 'div' => array('class' => 'submit'),
  7700. 'input' => array('type' => 'submit', 'value' => 'save'),
  7701. '/div',
  7702. '/form'
  7703. );
  7704. $this->assertTags($result, $expected);
  7705. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  7706. $expected = array(
  7707. 'div' => array('class' => 'submit'),
  7708. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7709. '/div',
  7710. '/form'
  7711. );
  7712. $this->assertTags($result, $expected);
  7713. $result = $this->Form->end(array('name' => 'Whatever'));
  7714. $expected = array(
  7715. 'div' => array('class' => 'submit'),
  7716. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  7717. '/div',
  7718. '/form'
  7719. );
  7720. $this->assertTags($result, $expected);
  7721. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  7722. $expected = array(
  7723. 'div' => array('class' => 'good'),
  7724. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7725. '/div',
  7726. '/form'
  7727. );
  7728. $this->assertTags($result, $expected);
  7729. $result = $this->Form->end(array(
  7730. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  7731. ));
  7732. $expected = array(
  7733. 'div' => array('class' => 'good'),
  7734. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7735. '/div',
  7736. '/form'
  7737. );
  7738. $this->assertTags($result, $expected);
  7739. }
  7740. /**
  7741. * testMultipleFormWithIdFields method
  7742. *
  7743. * @return void
  7744. */
  7745. public function testMultipleFormWithIdFields() {
  7746. $this->Form->create('UserForm');
  7747. $result = $this->Form->input('id');
  7748. $this->assertTags($result, array('input' => array(
  7749. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  7750. )));
  7751. $result = $this->Form->input('ValidateItem.id');
  7752. $this->assertTags($result, array('input' => array(
  7753. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  7754. 'id' => 'ValidateItemId'
  7755. )));
  7756. $result = $this->Form->input('ValidateUser.id');
  7757. $this->assertTags($result, array('input' => array(
  7758. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  7759. 'id' => 'ValidateUserId'
  7760. )));
  7761. }
  7762. /**
  7763. * testDbLessModel method
  7764. *
  7765. * @return void
  7766. */
  7767. public function testDbLessModel() {
  7768. $this->Form->create('TestMail');
  7769. $result = $this->Form->input('name');
  7770. $expected = array(
  7771. 'div' => array('class' => 'input text'),
  7772. 'label' => array('for' => 'TestMailName'),
  7773. 'Name',
  7774. '/label',
  7775. 'input' => array(
  7776. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7777. 'id' => 'TestMailName'
  7778. ),
  7779. '/div'
  7780. );
  7781. $this->assertTags($result, $expected);
  7782. ClassRegistry::init('TestMail');
  7783. $this->Form->create('TestMail');
  7784. $result = $this->Form->input('name');
  7785. $expected = array(
  7786. 'div' => array('class' => 'input text'),
  7787. 'label' => array('for' => 'TestMailName'),
  7788. 'Name',
  7789. '/label',
  7790. 'input' => array(
  7791. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7792. 'id' => 'TestMailName'
  7793. ),
  7794. '/div'
  7795. );
  7796. $this->assertTags($result, $expected);
  7797. }
  7798. /**
  7799. * testBrokenness method
  7800. *
  7801. * @return void
  7802. */
  7803. public function testBrokenness() {
  7804. /*
  7805. * #4 This test has two parents and four children. By default (as of r7117) both
  7806. * parents are show but the first parent is missing a child. This is the inconsistency
  7807. * in the default behaviour - one parent has all children, the other does not - dependent
  7808. * on the data values.
  7809. */
  7810. $result = $this->Form->select('Model.field', array(
  7811. 'Fred' => array(
  7812. 'freds_son_1' => 'Fred',
  7813. 'freds_son_2' => 'Freddie'
  7814. ),
  7815. 'Bert' => array(
  7816. 'berts_son_1' => 'Albert',
  7817. 'berts_son_2' => 'Bertie')
  7818. ),
  7819. array('showParents' => true, 'empty' => false)
  7820. );
  7821. $expected = array(
  7822. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7823. array('optgroup' => array('label' => 'Fred')),
  7824. array('option' => array('value' => 'freds_son_1')),
  7825. 'Fred',
  7826. '/option',
  7827. array('option' => array('value' => 'freds_son_2')),
  7828. 'Freddie',
  7829. '/option',
  7830. '/optgroup',
  7831. array('optgroup' => array('label' => 'Bert')),
  7832. array('option' => array('value' => 'berts_son_1')),
  7833. 'Albert',
  7834. '/option',
  7835. array('option' => array('value' => 'berts_son_2')),
  7836. 'Bertie',
  7837. '/option',
  7838. '/optgroup',
  7839. '/select'
  7840. );
  7841. $this->assertTags($result, $expected);
  7842. /*
  7843. * #2 This is structurally identical to the test above (#1) - only the parent name has
  7844. * changed, so we should expect the same select list data, just with a different name
  7845. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  7846. * This is where data corruption can occur, because when a select value is missing from
  7847. * a list a form will substitute the first value in the list - without the user knowing.
  7848. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  7849. * affect the availability of 3 => 'Three' as a valid option.
  7850. */
  7851. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  7852. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  7853. ));
  7854. $result = $this->Form->select(
  7855. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  7856. );
  7857. $expected = array(
  7858. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7859. array('option' => array('value' => 1)),
  7860. 'One',
  7861. '/option',
  7862. array('option' => array('value' => 2)),
  7863. 'Two',
  7864. '/option',
  7865. array('optgroup' => array('label' => 'Three')),
  7866. array('option' => array('value' => 3)),
  7867. 'Three',
  7868. '/option',
  7869. array('option' => array('value' => 4)),
  7870. 'Four',
  7871. '/option',
  7872. array('option' => array('value' => 5)),
  7873. 'Five',
  7874. '/option',
  7875. '/optgroup',
  7876. '/select'
  7877. );
  7878. $this->assertTags($result, $expected);
  7879. }
  7880. /**
  7881. * Test the generation of fields for a multi record form.
  7882. *
  7883. * @return void
  7884. */
  7885. public function testMultiRecordForm() {
  7886. $this->Form->create('ValidateProfile');
  7887. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  7888. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  7889. $expected = array(
  7890. 'div' => array('class' => 'input textarea'),
  7891. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  7892. 'Name',
  7893. '/label',
  7894. 'textarea' => array(
  7895. 'id' => 'ValidateProfile1ValidateItem2Name',
  7896. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  7897. 'cols' => 30,
  7898. 'rows' => 6
  7899. ),
  7900. 'Value',
  7901. '/textarea',
  7902. '/div'
  7903. );
  7904. $this->assertTags($result, $expected);
  7905. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  7906. $expected = array(
  7907. 'div' => array('class' => 'input date'),
  7908. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7909. 'Created',
  7910. '/label',
  7911. array('select' => array(
  7912. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  7913. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7914. )
  7915. ),
  7916. array('option' => array('value' => '')), '/option',
  7917. $this->dateRegex['monthsRegex'],
  7918. '/select', '-',
  7919. array('select' => array(
  7920. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  7921. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7922. )
  7923. ),
  7924. array('option' => array('value' => '')), '/option',
  7925. $this->dateRegex['daysRegex'],
  7926. '/select', '-',
  7927. array('select' => array(
  7928. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  7929. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7930. )
  7931. ),
  7932. array('option' => array('value' => '')), '/option',
  7933. $this->dateRegex['yearsRegex'],
  7934. '/select',
  7935. '/div'
  7936. );
  7937. $this->assertTags($result, $expected);
  7938. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7939. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7940. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7941. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7942. $expected = array(
  7943. 'div' => array('class' => 'input select error'),
  7944. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7945. 'Profile',
  7946. '/label',
  7947. 'select' => array(
  7948. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7949. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7950. 'class' => 'form-error'
  7951. ),
  7952. '/select',
  7953. array('div' => array('class' => 'error-message')),
  7954. 'Error',
  7955. '/div',
  7956. '/div'
  7957. );
  7958. $this->assertTags($result, $expected);
  7959. }
  7960. /**
  7961. * test the correct display of multi-record form validation errors.
  7962. *
  7963. * @return void
  7964. */
  7965. public function testMultiRecordFormValidationErrors() {
  7966. $this->Form->create('ValidateProfile');
  7967. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7968. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7969. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7970. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7971. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7972. $result = $this->Form->error('ValidateProfile.2.city');
  7973. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7974. $result = $this->Form->error('2.city');
  7975. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7976. }
  7977. /**
  7978. * test the correct display of multi-record form validation errors.
  7979. *
  7980. * @return void
  7981. */
  7982. public function testSaveManyRecordFormValidationErrors() {
  7983. $this->Form->create('ValidateUser');
  7984. $ValidateUser = ClassRegistry::getObject('ValidateUser');
  7985. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  7986. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  7987. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7988. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  7989. $result = $this->Form->error('ValidateUser.0.city');
  7990. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7991. }
  7992. /**
  7993. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7994. *
  7995. * @return void
  7996. */
  7997. public function testInputTemplate() {
  7998. $result = $this->Form->input('Contact.email', array(
  7999. 'type' => 'text', 'format' => array('input')
  8000. ));
  8001. $expected = array(
  8002. 'div' => array('class' => 'input text'),
  8003. 'input' => array(
  8004. 'type' => 'text', 'name' => 'data[Contact][email]',
  8005. 'id' => 'ContactEmail'
  8006. ),
  8007. '/div'
  8008. );
  8009. $this->assertTags($result, $expected);
  8010. $result = $this->Form->input('Contact.email', array(
  8011. 'type' => 'text', 'format' => array('input', 'label'),
  8012. 'label' => '<em>Email (required)</em>'
  8013. ));
  8014. $expected = array(
  8015. 'div' => array('class' => 'input text'),
  8016. array('input' => array(
  8017. 'type' => 'text', 'name' => 'data[Contact][email]',
  8018. 'id' => 'ContactEmail'
  8019. )),
  8020. 'label' => array('for' => 'ContactEmail'),
  8021. 'em' => array(),
  8022. 'Email (required)',
  8023. '/em',
  8024. '/label',
  8025. '/div'
  8026. );
  8027. $this->assertTags($result, $expected);
  8028. $result = $this->Form->input('Contact.email', array(
  8029. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  8030. 'between' => '<div>Something in the middle</div>',
  8031. 'after' => '<span>Some text at the end</span>'
  8032. ));
  8033. $expected = array(
  8034. 'div' => array('class' => 'input text'),
  8035. array('input' => array(
  8036. 'type' => 'text', 'name' => 'data[Contact][email]',
  8037. 'id' => 'ContactEmail'
  8038. )),
  8039. array('div' => array()),
  8040. 'Something in the middle',
  8041. '/div',
  8042. 'label' => array('for' => 'ContactEmail'),
  8043. 'Email',
  8044. '/label',
  8045. 'span' => array(),
  8046. 'Some text at the end',
  8047. '/span',
  8048. '/div'
  8049. );
  8050. $this->assertTags($result, $expected);
  8051. $result = $this->Form->input('Contact.method', array(
  8052. 'type' => 'radio',
  8053. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  8054. 'between' => 'I am between',
  8055. ));
  8056. $expected = array(
  8057. 'div' => array('class' => 'input radio'),
  8058. 'fieldset' => array(),
  8059. 'legend' => array(),
  8060. 'Method',
  8061. '/legend',
  8062. 'I am between',
  8063. 'input' => array(
  8064. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  8065. 'value' => '', 'id' => 'ContactMethod_'
  8066. ),
  8067. array('input' => array(
  8068. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8069. 'value' => 'email', 'id' => 'ContactMethodEmail'
  8070. )),
  8071. array('label' => array('for' => 'ContactMethodEmail')),
  8072. 'Email',
  8073. '/label',
  8074. array('input' => array(
  8075. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8076. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  8077. )),
  8078. array('label' => array('for' => 'ContactMethodPigeon')),
  8079. 'Pigeon',
  8080. '/label',
  8081. '/fieldset',
  8082. '/div',
  8083. );
  8084. $this->assertTags($result, $expected);
  8085. }
  8086. /**
  8087. * test that some html5 inputs + FormHelper::__call() work
  8088. *
  8089. * @return void
  8090. */
  8091. public function testHtml5Inputs() {
  8092. $result = $this->Form->email('User.email');
  8093. $expected = array(
  8094. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  8095. );
  8096. $this->assertTags($result, $expected);
  8097. $result = $this->Form->search('User.query');
  8098. $expected = array(
  8099. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  8100. );
  8101. $this->assertTags($result, $expected);
  8102. $result = $this->Form->search('User.query', array('value' => 'test'));
  8103. $expected = array(
  8104. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8105. );
  8106. $this->assertTags($result, $expected);
  8107. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  8108. $expected = array(
  8109. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8110. );
  8111. $this->assertTags($result, $expected);
  8112. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  8113. $expected = array(
  8114. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  8115. );
  8116. $this->assertTags($result, $expected);
  8117. }
  8118. /**
  8119. *
  8120. * @expectedException CakeException
  8121. * @return void
  8122. */
  8123. public function testHtml5InputException() {
  8124. $this->Form->email();
  8125. }
  8126. /**
  8127. * Tests that a model can be loaded from the model names passed in the request object
  8128. *
  8129. * @return void
  8130. */
  8131. public function testIntrospectModelFromRequest() {
  8132. $this->loadFixtures('Post');
  8133. App::build(array(
  8134. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  8135. ));
  8136. CakePlugin::load('TestPlugin');
  8137. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  8138. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  8139. $this->Form->create('TestPluginPost');
  8140. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  8141. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  8142. CakePlugin::unload();
  8143. App::build();
  8144. }
  8145. /**
  8146. * Tests that it is possible to set the validation errors directly in the helper for a field
  8147. *
  8148. * @return void
  8149. */
  8150. public function testCustomValidationErrors() {
  8151. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  8152. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  8153. $this->assertEquals('Badness!', $result);
  8154. }
  8155. /**
  8156. * Tests that the 'on' key validates as expected on create
  8157. *
  8158. * @return void
  8159. */
  8160. public function testRequiredOnCreate() {
  8161. $this->Form->create('Contact');
  8162. $result = $this->Form->input('Contact.imrequiredonupdate');
  8163. $expected = array(
  8164. 'div' => array('class' => 'input text'),
  8165. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8166. 'Imrequiredonupdate',
  8167. '/label',
  8168. 'input' => array(
  8169. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8170. 'id' => 'ContactImrequiredonupdate'
  8171. ),
  8172. '/div'
  8173. );
  8174. $this->assertTags($result, $expected);
  8175. $result = $this->Form->input('Contact.imrequiredoncreate');
  8176. $expected = array(
  8177. 'div' => array('class' => 'input text required'),
  8178. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8179. 'Imrequiredoncreate',
  8180. '/label',
  8181. 'input' => array(
  8182. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8183. 'id' => 'ContactImrequiredoncreate',
  8184. 'required' => 'required'
  8185. ),
  8186. '/div'
  8187. );
  8188. $this->assertTags($result, $expected);
  8189. $result = $this->Form->input('Contact.imrequiredonboth');
  8190. $expected = array(
  8191. 'div' => array('class' => 'input text required'),
  8192. 'label' => array('for' => 'ContactImrequiredonboth'),
  8193. 'Imrequiredonboth',
  8194. '/label',
  8195. 'input' => array(
  8196. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8197. 'id' => 'ContactImrequiredonboth',
  8198. 'required' => 'required'
  8199. ),
  8200. '/div'
  8201. );
  8202. $this->assertTags($result, $expected);
  8203. $this->Form->inputDefaults(array('required' => false));
  8204. $result = $this->Form->input('Contact.imrequired');
  8205. $expected = array(
  8206. 'div' => array('class' => 'input text'),
  8207. 'label' => array('for' => 'ContactImrequired'),
  8208. 'Imrequired',
  8209. '/label',
  8210. 'input' => array(
  8211. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8212. 'id' => 'ContactImrequired'
  8213. ),
  8214. '/div'
  8215. );
  8216. $this->assertTags($result, $expected);
  8217. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  8218. $this->assertTags($result, $expected);
  8219. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  8220. $expected = array(
  8221. 'div' => array('class' => 'input text required'),
  8222. 'label' => array('for' => 'ContactImrequired'),
  8223. 'Imrequired',
  8224. '/label',
  8225. 'input' => array(
  8226. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8227. 'id' => 'ContactImrequired'
  8228. ),
  8229. '/div'
  8230. );
  8231. $this->assertTags($result, $expected);
  8232. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  8233. $this->assertTags($result, $expected);
  8234. }
  8235. /**
  8236. * Tests that the 'on' key validates as expected on update
  8237. *
  8238. * @return void
  8239. */
  8240. public function testRequiredOnUpdate() {
  8241. $this->Form->request->data['Contact']['id'] = 1;
  8242. $this->Form->create('Contact');
  8243. $result = $this->Form->input('Contact.imrequiredonupdate');
  8244. $expected = array(
  8245. 'div' => array('class' => 'input text required'),
  8246. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8247. 'Imrequiredonupdate',
  8248. '/label',
  8249. 'input' => array(
  8250. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8251. 'id' => 'ContactImrequiredonupdate',
  8252. 'required' => 'required'
  8253. ),
  8254. '/div'
  8255. );
  8256. $this->assertTags($result, $expected);
  8257. $result = $this->Form->input('Contact.imrequiredoncreate');
  8258. $expected = array(
  8259. 'div' => array('class' => 'input text'),
  8260. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8261. 'Imrequiredoncreate',
  8262. '/label',
  8263. 'input' => array(
  8264. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8265. 'id' => 'ContactImrequiredoncreate'
  8266. ),
  8267. '/div'
  8268. );
  8269. $this->assertTags($result, $expected);
  8270. $result = $this->Form->input('Contact.imrequiredonboth');
  8271. $expected = array(
  8272. 'div' => array('class' => 'input text required'),
  8273. 'label' => array('for' => 'ContactImrequiredonboth'),
  8274. 'Imrequiredonboth',
  8275. '/label',
  8276. 'input' => array(
  8277. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8278. 'id' => 'ContactImrequiredonboth',
  8279. 'required' => 'required'
  8280. ),
  8281. '/div'
  8282. );
  8283. $this->assertTags($result, $expected);
  8284. $result = $this->Form->input('Contact.imrequired');
  8285. $expected = array(
  8286. 'div' => array('class' => 'input text required'),
  8287. 'label' => array('for' => 'ContactImrequired'),
  8288. 'Imrequired',
  8289. '/label',
  8290. 'input' => array(
  8291. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8292. 'id' => 'ContactImrequired',
  8293. 'required' => 'required'
  8294. ),
  8295. '/div'
  8296. );
  8297. $this->assertTags($result, $expected);
  8298. }
  8299. /**
  8300. * Test inputDefaults setter and getter
  8301. *
  8302. * @return void
  8303. */
  8304. public function testInputDefaults() {
  8305. $this->Form->create('Contact');
  8306. $this->Form->inputDefaults(array(
  8307. 'label' => false,
  8308. 'div' => array(
  8309. 'style' => 'color: #000;'
  8310. )
  8311. ));
  8312. $result = $this->Form->input('Contact.field1');
  8313. $expected = array(
  8314. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  8315. 'input' => array(
  8316. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8317. 'id' => 'ContactField1'
  8318. ),
  8319. '/div'
  8320. );
  8321. $this->assertTags($result, $expected);
  8322. $this->Form->inputDefaults(array(
  8323. 'div' => false,
  8324. 'label' => 'Label',
  8325. ));
  8326. $result = $this->Form->input('Contact.field1');
  8327. $expected = array(
  8328. 'label' => array('for' => 'ContactField1'),
  8329. 'Label',
  8330. '/label',
  8331. 'input' => array(
  8332. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8333. 'id' => 'ContactField1'
  8334. ),
  8335. );
  8336. $this->assertTags($result, $expected);
  8337. $this->Form->inputDefaults(array(
  8338. 'label' => false,
  8339. ), true);
  8340. $result = $this->Form->input('Contact.field1');
  8341. $expected = array(
  8342. 'input' => array(
  8343. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8344. 'id' => 'ContactField1'
  8345. ),
  8346. );
  8347. $this->assertTags($result, $expected);
  8348. $result = $this->Form->inputDefaults();
  8349. $expected = array(
  8350. 'div' => false,
  8351. 'label' => false,
  8352. );
  8353. $this->assertEqual($result, $expected);
  8354. }
  8355. }