FormHelperTest.php 284 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930
  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 MIT License (http://www.opensource.org/licenses/mit-license.php)
  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
  33. * @package Cake.Test.Case.View.Helper
  34. */
  35. class ContactTestController extends Controller {
  36. /**
  37. * uses property
  38. *
  39. * @var mixed null
  40. */
  41. public $uses = null;
  42. }
  43. /**
  44. * Contact class
  45. *
  46. * @package cake
  47. * @package Cake.Test.Case.View.Helper
  48. */
  49. class Contact extends CakeTestModel {
  50. /**
  51. * useTable property
  52. *
  53. * @var bool false
  54. */
  55. public $useTable = false;
  56. /**
  57. * Default schema
  58. *
  59. * @var array
  60. */
  61. protected $_schema = array(
  62. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  63. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  64. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  65. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  66. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  67. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  68. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  69. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  70. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  71. );
  72. /**
  73. * validate property
  74. *
  75. * @var array
  76. */
  77. public $validate = array(
  78. 'non_existing' => array(),
  79. 'idontexist' => array(),
  80. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  81. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  82. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  83. 'imrequiredonboth' => array(
  84. 'required' => array('rule' => 'alphaNumeric'),
  85. ),
  86. 'string_required' => 'notEmpty',
  87. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  88. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  89. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  90. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  91. 'imalsonotrequired' => array(
  92. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  93. 'between' => array('rule' => array('between', 5, 30)),
  94. ),
  95. 'imalsonotrequired2' => array(
  96. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  97. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  98. ),
  99. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  100. 'iamrequiredalways' => array(
  101. 'email' => array('rule' => 'email'),
  102. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  103. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  104. ),
  105. 'boolean_field' => array('rule' => 'boolean')
  106. );
  107. /**
  108. * schema method
  109. *
  110. * @return void
  111. */
  112. public function setSchema($schema) {
  113. $this->_schema = $schema;
  114. }
  115. /**
  116. * hasAndBelongsToMany property
  117. *
  118. * @var array
  119. */
  120. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  121. /**
  122. * hasAndBelongsToMany property
  123. *
  124. * @var array
  125. */
  126. public $belongsTo = array('User' => array('className' => 'UserForm'));
  127. }
  128. /**
  129. * ContactTagsContact class
  130. *
  131. * @package cake
  132. * @package Cake.Test.Case.View.Helper
  133. */
  134. class ContactTagsContact extends CakeTestModel {
  135. /**
  136. * useTable property
  137. *
  138. * @var bool false
  139. */
  140. public $useTable = false;
  141. /**
  142. * Default schema
  143. *
  144. * @var array
  145. */
  146. protected $_schema = array(
  147. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  148. 'contact_tag_id' => array(
  149. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  150. )
  151. );
  152. /**
  153. * schema method
  154. *
  155. * @return void
  156. */
  157. public function setSchema($schema) {
  158. $this->_schema = $schema;
  159. }
  160. }
  161. /**
  162. * ContactNonStandardPk class
  163. *
  164. * @package cake
  165. * @package Cake.Test.Case.View.Helper
  166. */
  167. class ContactNonStandardPk extends Contact {
  168. /**
  169. * primaryKey property
  170. *
  171. * @var string 'pk'
  172. */
  173. public $primaryKey = 'pk';
  174. /**
  175. * schema method
  176. *
  177. * @return void
  178. */
  179. public function schema($field = false) {
  180. $this->_schema = parent::schema();
  181. $this->_schema['pk'] = $this->_schema['id'];
  182. unset($this->_schema['id']);
  183. return $this->_schema;
  184. }
  185. }
  186. /**
  187. * ContactTag class
  188. *
  189. * @package cake
  190. * @package Cake.Test.Case.View.Helper
  191. */
  192. class ContactTag extends Model {
  193. /**
  194. * useTable property
  195. *
  196. * @var bool false
  197. */
  198. public $useTable = false;
  199. /**
  200. * schema definition
  201. *
  202. * @var array
  203. */
  204. protected $_schema = array(
  205. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  206. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  207. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  208. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  209. );
  210. }
  211. /**
  212. * UserForm class
  213. *
  214. * @package cake
  215. * @package Cake.Test.Case.View.Helper
  216. */
  217. class UserForm extends CakeTestModel {
  218. /**
  219. * useTable property
  220. *
  221. * @var bool false
  222. */
  223. public $useTable = false;
  224. /**
  225. * hasMany property
  226. *
  227. * @var array
  228. */
  229. public $hasMany = array(
  230. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  231. ));
  232. /**
  233. * schema definition
  234. *
  235. * @var array
  236. */
  237. protected $_schema = array(
  238. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  239. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  240. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  241. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  242. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  243. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  244. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  245. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  246. );
  247. }
  248. /**
  249. * OpenidUrl class
  250. *
  251. * @package cake
  252. * @package Cake.Test.Case.View.Helper
  253. */
  254. class OpenidUrl extends CakeTestModel {
  255. /**
  256. * useTable property
  257. *
  258. * @var bool false
  259. */
  260. public $useTable = false;
  261. /**
  262. * belongsTo property
  263. *
  264. * @var array
  265. */
  266. public $belongsTo = array('UserForm' => array(
  267. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  268. ));
  269. /**
  270. * validate property
  271. *
  272. * @var array
  273. */
  274. public $validate = array('openid_not_registered' => array());
  275. /**
  276. * schema method
  277. *
  278. * @var array
  279. */
  280. protected $_schema = array(
  281. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  282. 'user_form_id' => array(
  283. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  284. ),
  285. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  286. );
  287. /**
  288. * beforeValidate method
  289. *
  290. * @return void
  291. */
  292. public function beforeValidate($options = array()) {
  293. $this->invalidate('openid_not_registered');
  294. return true;
  295. }
  296. }
  297. /**
  298. * ValidateUser class
  299. *
  300. * @package cake
  301. * @package Cake.Test.Case.View.Helper
  302. */
  303. class ValidateUser extends CakeTestModel {
  304. /**
  305. * useTable property
  306. *
  307. * @var bool false
  308. */
  309. public $useTable = false;
  310. /**
  311. * hasOne property
  312. *
  313. * @var array
  314. */
  315. public $hasOne = array('ValidateProfile' => array(
  316. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  317. ));
  318. /**
  319. * schema method
  320. *
  321. * @var array
  322. */
  323. protected $_schema = array(
  324. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  325. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  326. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  327. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  328. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  329. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  330. );
  331. /**
  332. * beforeValidate method
  333. *
  334. * @return void
  335. */
  336. public function beforeValidate($options = array()) {
  337. $this->invalidate('email');
  338. return false;
  339. }
  340. }
  341. /**
  342. * ValidateProfile class
  343. *
  344. * @package cake
  345. * @package Cake.Test.Case.View.Helper
  346. */
  347. class ValidateProfile extends CakeTestModel {
  348. /**
  349. * useTable property
  350. *
  351. * @var bool false
  352. */
  353. public $useTable = false;
  354. /**
  355. * schema property
  356. *
  357. * @var array
  358. */
  359. protected $_schema = array(
  360. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  361. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  362. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  363. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  364. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  365. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  366. );
  367. /**
  368. * hasOne property
  369. *
  370. * @var array
  371. */
  372. public $hasOne = array('ValidateItem' => array(
  373. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  374. ));
  375. /**
  376. * belongsTo property
  377. *
  378. * @var array
  379. */
  380. public $belongsTo = array('ValidateUser' => array(
  381. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  382. ));
  383. /**
  384. * beforeValidate method
  385. *
  386. * @return void
  387. */
  388. public function beforeValidate($options = array()) {
  389. $this->invalidate('full_name');
  390. $this->invalidate('city');
  391. return false;
  392. }
  393. }
  394. /**
  395. * ValidateItem class
  396. *
  397. * @package cake
  398. * @package Cake.Test.Case.View.Helper
  399. */
  400. class ValidateItem extends CakeTestModel {
  401. /**
  402. * useTable property
  403. *
  404. * @var bool false
  405. */
  406. public $useTable = false;
  407. /**
  408. * schema property
  409. *
  410. * @var array
  411. */
  412. protected $_schema = array(
  413. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  414. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  415. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  416. 'description' => array(
  417. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  418. ),
  419. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  420. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  421. );
  422. /**
  423. * belongsTo property
  424. *
  425. * @var array
  426. */
  427. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  428. /**
  429. * beforeValidate method
  430. *
  431. * @return void
  432. */
  433. public function beforeValidate($options = array()) {
  434. $this->invalidate('description');
  435. return false;
  436. }
  437. }
  438. /**
  439. * TestMail class
  440. *
  441. * @package cake
  442. * @package Cake.Test.Case.View.Helper
  443. */
  444. class TestMail extends CakeTestModel {
  445. /**
  446. * useTable property
  447. *
  448. * @var bool false
  449. */
  450. public $useTable = false;
  451. }
  452. /**
  453. * FormHelperTest class
  454. *
  455. * @package cake
  456. * @subpackage Cake.Test.Case.View.Helper
  457. * @property FormHelper $Form
  458. */
  459. class FormHelperTest extends CakeTestCase {
  460. /**
  461. * Fixtures to be used
  462. *
  463. * @var array
  464. */
  465. public $fixtures = array('core.post');
  466. /**
  467. * Do not load the fixtures by default
  468. *
  469. * @var boolean
  470. */
  471. public $autoFixtures = false;
  472. /**
  473. * setUp method
  474. *
  475. * @return void
  476. */
  477. public function setUp() {
  478. parent::setUp();
  479. Configure::write('App.base', '');
  480. $this->Controller = new ContactTestController();
  481. $this->View = new View($this->Controller);
  482. $this->Form = new FormHelper($this->View);
  483. $this->Form->Html = new HtmlHelper($this->View);
  484. $this->Form->request = new CakeRequest('contacts/add', false);
  485. $this->Form->request->here = '/contacts/add';
  486. $this->Form->request['action'] = 'add';
  487. $this->Form->request->webroot = '';
  488. $this->Form->request->base = '';
  489. ClassRegistry::addObject('Contact', new Contact());
  490. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  491. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  492. ClassRegistry::addObject('User', new UserForm());
  493. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  494. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  495. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  496. $this->oldSalt = Configure::read('Security.salt');
  497. $this->dateRegex = array(
  498. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  499. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  500. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  501. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  502. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  503. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  504. );
  505. Configure::write('Security.salt', 'foo!');
  506. }
  507. /**
  508. * tearDown method
  509. *
  510. * @return void
  511. */
  512. public function tearDown() {
  513. parent::tearDown();
  514. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  515. Configure::write('Security.salt', $this->oldSalt);
  516. }
  517. /**
  518. * testFormCreateWithSecurity method
  519. *
  520. * Test form->create() with security key.
  521. *
  522. * @return void
  523. */
  524. public function testCreateWithSecurity() {
  525. $this->Form->request['_Token'] = array('key' => 'testKey');
  526. $encoding = strtolower(Configure::read('App.encoding'));
  527. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  528. $expected = array(
  529. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  530. 'div' => array('style' => 'display:none;'),
  531. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  532. array('input' => array(
  533. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  534. )),
  535. '/div'
  536. );
  537. $this->assertTags($result, $expected);
  538. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  539. $expected['form']['id'] = 'MyForm';
  540. $this->assertTags($result, $expected);
  541. }
  542. /**
  543. * testFormCreateGetNoSecurity method
  544. *
  545. * Test form->create() with no security key as its a get form
  546. *
  547. * @return void
  548. */
  549. public function testCreateEndGetNoSecurity() {
  550. $this->Form->request['_Token'] = array('key' => 'testKey');
  551. $encoding = strtolower(Configure::read('App.encoding'));
  552. $result = $this->Form->create('Contact', array('type' => 'get', 'url' => '/contacts/add'));
  553. $this->assertNotContains('Token', $result);
  554. $result = $this->Form->end('Save');
  555. $this->assertNotContains('Token', $result);
  556. }
  557. /**
  558. * test that create() clears the fields property so it starts fresh
  559. *
  560. * @return void
  561. */
  562. public function testCreateClearingFields() {
  563. $this->Form->fields = array('model_id');
  564. $this->Form->create('Contact');
  565. $this->assertEquals(array(), $this->Form->fields);
  566. }
  567. /**
  568. * Tests form hash generation with model-less data
  569. *
  570. * @return void
  571. */
  572. public function testValidateHashNoModel() {
  573. $this->Form->request['_Token'] = array('key' => 'foo');
  574. $result = $this->Form->secure(array('anything'));
  575. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  576. }
  577. /**
  578. * Tests that models with identical field names get resolved properly
  579. *
  580. * @return void
  581. */
  582. public function testDuplicateFieldNameResolution() {
  583. $result = $this->Form->create('ValidateUser');
  584. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  585. $result = $this->Form->input('ValidateItem.name');
  586. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  587. $result = $this->Form->input('ValidateUser.name');
  588. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  589. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  590. $this->assertRegExp('/type="text"/', $result);
  591. $result = $this->Form->input('ValidateItem.name');
  592. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  593. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  594. $this->assertRegExp('/<textarea/', $result);
  595. $result = $this->Form->input('name');
  596. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  597. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  598. $this->assertRegExp('/type="text"/', $result);
  599. }
  600. /**
  601. * Tests that hidden fields generated for checkboxes don't get locked
  602. *
  603. * @return void
  604. */
  605. public function testNoCheckboxLocking() {
  606. $this->Form->request['_Token'] = array('key' => 'foo');
  607. $this->assertSame(array(), $this->Form->fields);
  608. $this->Form->checkbox('check', array('value' => '1'));
  609. $this->assertSame($this->Form->fields, array('check'));
  610. }
  611. /**
  612. * testFormSecurityFields method
  613. *
  614. * Test generation of secure form hash generation.
  615. *
  616. * @return void
  617. */
  618. public function testFormSecurityFields() {
  619. $key = 'testKey';
  620. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  621. $this->Form->request['_Token'] = array('key' => $key);
  622. $result = $this->Form->secure($fields);
  623. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  624. $hash .= ':' . 'Model.valid';
  625. $hash = urlencode($hash);
  626. $expected = array(
  627. 'div' => array('style' => 'display:none;'),
  628. array('input' => array(
  629. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  630. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  631. )),
  632. array('input' => array(
  633. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  634. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  635. )),
  636. '/div'
  637. );
  638. $this->assertTags($result, $expected);
  639. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  640. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  641. $result = $this->Form->secure($fields);
  642. $expected = array(
  643. 'div' => array('class' => 'hidden'),
  644. array('input' => array(
  645. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  646. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  647. )),
  648. array('input' => array(
  649. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  650. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  651. )),
  652. '/div'
  653. );
  654. $this->assertTags($result, $expected);
  655. }
  656. /**
  657. * Tests correct generation of number fields for double and float fields
  658. *
  659. * @return void
  660. */
  661. public function testTextFieldGenerationForFloats() {
  662. $model = ClassRegistry::getObject('Contact');
  663. $model->setSchema(array('foo' => array(
  664. 'type' => 'float',
  665. 'null' => false,
  666. 'default' => null,
  667. 'length' => 10
  668. )));
  669. $this->Form->create('Contact');
  670. $result = $this->Form->input('foo');
  671. $expected = array(
  672. 'div' => array('class' => 'input number'),
  673. 'label' => array('for' => 'ContactFoo'),
  674. 'Foo',
  675. '/label',
  676. array('input' => array(
  677. 'type' => 'number',
  678. 'name' => 'data[Contact][foo]',
  679. 'id' => 'ContactFoo',
  680. 'step' => 'any'
  681. )),
  682. '/div'
  683. );
  684. $this->assertTags($result, $expected);
  685. $result = $this->Form->input('foo', array('step' => 0.5));
  686. $expected = array(
  687. 'div' => array('class' => 'input number'),
  688. 'label' => array('for' => 'ContactFoo'),
  689. 'Foo',
  690. '/label',
  691. array('input' => array(
  692. 'type' => 'number',
  693. 'name' => 'data[Contact][foo]',
  694. 'id' => 'ContactFoo',
  695. 'step' => '0.5'
  696. )),
  697. '/div'
  698. );
  699. $this->assertTags($result, $expected);
  700. }
  701. /**
  702. * Tests correct generation of number fields for integer fields
  703. *
  704. * @access public
  705. * @return void
  706. */
  707. public function testTextFieldTypeNumberGenerationForIntegers() {
  708. $model = ClassRegistry::getObject('Contact');
  709. $model->setSchema(array('foo' => array(
  710. 'type' => 'integer',
  711. 'null' => false,
  712. 'default' => null,
  713. 'length' => null
  714. )));
  715. $this->Form->create('Contact');
  716. $result = $this->Form->input('foo');
  717. $expected = array(
  718. 'div' => array('class' => 'input number'),
  719. 'label' => array('for' => 'ContactFoo'),
  720. 'Foo',
  721. '/label',
  722. array('input' => array(
  723. 'type' => 'number', 'name' => 'data[Contact][foo]',
  724. 'id' => 'ContactFoo'
  725. )),
  726. '/div'
  727. );
  728. $this->assertTags($result, $expected);
  729. }
  730. /**
  731. * testFormSecurityMultipleFields method
  732. *
  733. * Test secure() with multiple row form. Ensure hash is correct.
  734. *
  735. * @return void
  736. */
  737. public function testFormSecurityMultipleFields() {
  738. $key = 'testKey';
  739. $fields = array(
  740. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  741. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  742. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  743. );
  744. $this->Form->request['_Token'] = array('key' => $key);
  745. $result = $this->Form->secure($fields);
  746. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  747. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  748. $expected = array(
  749. 'div' => array('style' => 'display:none;'),
  750. array('input' => array(
  751. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  752. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  753. )),
  754. array('input' => array(
  755. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  756. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  757. )),
  758. '/div'
  759. );
  760. $this->assertTags($result, $expected);
  761. }
  762. /**
  763. * testFormSecurityMultipleSubmitButtons
  764. *
  765. * test form submit generation and ensure that _Token is only created on end()
  766. *
  767. * @return void
  768. */
  769. public function testFormSecurityMultipleSubmitButtons() {
  770. $key = 'testKey';
  771. $this->Form->request['_Token'] = array('key' => $key);
  772. $this->Form->create('Addresses');
  773. $this->Form->input('Address.title');
  774. $this->Form->input('Address.first_name');
  775. $result = $this->Form->submit('Save', array('name' => 'save'));
  776. $expected = array(
  777. 'div' => array('class' => 'submit'),
  778. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  779. '/div',
  780. );
  781. $this->assertTags($result, $expected);
  782. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  783. $expected = array(
  784. 'div' => array('class' => 'submit'),
  785. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  786. '/div',
  787. );
  788. $this->assertTags($result, $expected);
  789. $result = $this->Form->end(null);
  790. $expected = array(
  791. 'div' => array('style' => 'display:none;'),
  792. array('input' => array(
  793. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  794. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  795. )),
  796. array('input' => array(
  797. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  798. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  799. )),
  800. '/div'
  801. );
  802. $this->assertTags($result, $expected);
  803. }
  804. /**
  805. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  806. *
  807. * @return void
  808. */
  809. public function testSecurityButtonNestedNamed() {
  810. $key = 'testKey';
  811. $this->Form->request['_Token'] = array('key' => $key);
  812. $this->Form->create('Addresses');
  813. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  814. $result = $this->Form->unlockField();
  815. $this->assertEquals(array('Address.button'), $result);
  816. }
  817. /**
  818. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  819. *
  820. * @return void
  821. */
  822. public function testSecuritySubmitNestedNamed() {
  823. $key = 'testKey';
  824. $this->Form->request['_Token'] = array('key' => $key);
  825. $this->Form->create('Addresses');
  826. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  827. $result = $this->Form->unlockField();
  828. $this->assertEquals(array('Address.button'), $result);
  829. }
  830. /**
  831. * Test that the correct fields are unlocked for image submits with no names.
  832. *
  833. * @return void
  834. */
  835. public function testSecuritySubmitImageNoName() {
  836. $key = 'testKey';
  837. $this->Form->request['_Token'] = array('key' => $key);
  838. $this->Form->create('User');
  839. $result = $this->Form->submit('save.png');
  840. $expected = array(
  841. 'div' => array('class' => 'submit'),
  842. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  843. '/div'
  844. );
  845. $this->assertTags($result, $expected);
  846. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  847. }
  848. /**
  849. * Test that the correct fields are unlocked for image submits with names.
  850. *
  851. * @return void
  852. */
  853. public function testSecuritySubmitImageName() {
  854. $key = 'testKey';
  855. $this->Form->request['_Token'] = array('key' => $key);
  856. $this->Form->create('User');
  857. $result = $this->Form->submit('save.png', array('name' => 'test'));
  858. $expected = array(
  859. 'div' => array('class' => 'submit'),
  860. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  861. '/div'
  862. );
  863. $this->assertTags($result, $expected);
  864. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  865. }
  866. /**
  867. * testFormSecurityMultipleInputFields method
  868. *
  869. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  870. *
  871. * @return void
  872. */
  873. public function testFormSecurityMultipleInputFields() {
  874. $key = 'testKey';
  875. $this->Form->request['_Token'] = array('key' => $key);
  876. $this->Form->create('Addresses');
  877. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  878. $this->Form->input('Addresses.0.title');
  879. $this->Form->input('Addresses.0.first_name');
  880. $this->Form->input('Addresses.0.last_name');
  881. $this->Form->input('Addresses.0.address');
  882. $this->Form->input('Addresses.0.city');
  883. $this->Form->input('Addresses.0.phone');
  884. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  885. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  886. $this->Form->input('Addresses.1.title');
  887. $this->Form->input('Addresses.1.first_name');
  888. $this->Form->input('Addresses.1.last_name');
  889. $this->Form->input('Addresses.1.address');
  890. $this->Form->input('Addresses.1.city');
  891. $this->Form->input('Addresses.1.phone');
  892. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  893. $result = $this->Form->secure($this->Form->fields);
  894. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  895. $expected = array(
  896. 'div' => array('style' => 'display:none;'),
  897. array('input' => array(
  898. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  899. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  900. )),
  901. array('input' => array(
  902. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  903. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  904. )),
  905. '/div'
  906. );
  907. $this->assertTags($result, $expected);
  908. }
  909. /**
  910. * Test form security with Model.field.0 style inputs
  911. *
  912. * @return void
  913. */
  914. public function testFormSecurityArrayFields() {
  915. $key = 'testKey';
  916. $this->Form->request->params['_Token']['key'] = $key;
  917. $this->Form->create('Address');
  918. $this->Form->input('Address.primary.1');
  919. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  920. $this->Form->input('Address.secondary.1.0');
  921. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  922. }
  923. /**
  924. * testFormSecurityMultipleInputDisabledFields method
  925. *
  926. * test secure form generation with multiple records and disabled fields.
  927. *
  928. * @return void
  929. */
  930. public function testFormSecurityMultipleInputDisabledFields() {
  931. $key = 'testKey';
  932. $this->Form->request->params['_Token'] = array(
  933. 'key' => $key,
  934. 'unlockedFields' => array('first_name', 'address')
  935. );
  936. $this->Form->create();
  937. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  938. $this->Form->input('Addresses.0.title');
  939. $this->Form->input('Addresses.0.first_name');
  940. $this->Form->input('Addresses.0.last_name');
  941. $this->Form->input('Addresses.0.address');
  942. $this->Form->input('Addresses.0.city');
  943. $this->Form->input('Addresses.0.phone');
  944. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  945. $this->Form->input('Addresses.1.title');
  946. $this->Form->input('Addresses.1.first_name');
  947. $this->Form->input('Addresses.1.last_name');
  948. $this->Form->input('Addresses.1.address');
  949. $this->Form->input('Addresses.1.city');
  950. $this->Form->input('Addresses.1.phone');
  951. $result = $this->Form->secure($this->Form->fields);
  952. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  953. $expected = array(
  954. 'div' => array('style' => 'display:none;'),
  955. array('input' => array(
  956. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  957. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  958. )),
  959. array('input' => array(
  960. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  961. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  962. )),
  963. '/div'
  964. );
  965. $this->assertTags($result, $expected);
  966. }
  967. /**
  968. * testFormSecurityInputDisabledFields method
  969. *
  970. * Test single record form with disabled fields.
  971. *
  972. * @return void
  973. */
  974. public function testFormSecurityInputUnlockedFields() {
  975. $key = 'testKey';
  976. $this->Form->request['_Token'] = array(
  977. 'key' => $key,
  978. 'unlockedFields' => array('first_name', 'address')
  979. );
  980. $this->Form->create();
  981. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  982. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  983. $this->Form->input('Addresses.title');
  984. $this->Form->input('Addresses.first_name');
  985. $this->Form->input('Addresses.last_name');
  986. $this->Form->input('Addresses.address');
  987. $this->Form->input('Addresses.city');
  988. $this->Form->input('Addresses.phone');
  989. $result = $this->Form->fields;
  990. $expected = array(
  991. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  992. 'Addresses.city', 'Addresses.phone'
  993. );
  994. $this->assertEquals($expected, $result);
  995. $result = $this->Form->secure($expected);
  996. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  997. $expected = array(
  998. 'div' => array('style' => 'display:none;'),
  999. array('input' => array(
  1000. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1001. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1002. )),
  1003. array('input' => array(
  1004. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1005. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1006. )),
  1007. '/div'
  1008. );
  1009. $this->assertTags($result, $expected);
  1010. }
  1011. /**
  1012. * test securing inputs with custom name attributes.
  1013. *
  1014. * @return void
  1015. */
  1016. public function testFormSecureWithCustomNameAttribute() {
  1017. $this->Form->request->params['_Token']['key'] = 'testKey';
  1018. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1019. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1020. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1021. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1022. }
  1023. /**
  1024. * testFormSecuredInput method
  1025. *
  1026. * Test generation of entire secure form, assertions made on input() output.
  1027. *
  1028. * @return void
  1029. */
  1030. public function testFormSecuredInput() {
  1031. $this->Form->request['_Token'] = array('key' => 'testKey');
  1032. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1033. $encoding = strtolower(Configure::read('App.encoding'));
  1034. $expected = array(
  1035. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1036. 'div' => array('style' => 'display:none;'),
  1037. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1038. array('input' => array(
  1039. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1040. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1041. )),
  1042. '/div'
  1043. );
  1044. $this->assertTags($result, $expected);
  1045. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1046. $expected = array(
  1047. 'div' => array('class' => 'input text'),
  1048. 'label' => array('for' => 'UserFormPublished'),
  1049. 'Published',
  1050. '/label',
  1051. array('input' => array(
  1052. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1053. 'id' => 'UserFormPublished'
  1054. )),
  1055. '/div'
  1056. );
  1057. $this->assertTags($result, $expected);
  1058. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1059. $expected = array(
  1060. 'div' => array('class' => 'input text'),
  1061. 'label' => array('for' => 'UserFormOther'),
  1062. 'Other',
  1063. '/label',
  1064. array('input' => array(
  1065. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1066. 'id' => 'UserFormOther'
  1067. )),
  1068. '/div'
  1069. );
  1070. $this->assertTags($result, $expected);
  1071. $result = $this->Form->hidden('UserForm.stuff');
  1072. $expected = array(
  1073. 'input' => array(
  1074. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1075. 'id' => 'UserFormStuff'
  1076. ));
  1077. $this->assertTags($result, $expected);
  1078. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1079. $expected = array('input' => array(
  1080. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1081. 'value' => '0', 'id' => 'UserFormHidden'
  1082. ));
  1083. $this->assertTags($result, $expected);
  1084. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1085. $expected = array(
  1086. 'div' => array('class' => 'input checkbox'),
  1087. array('input' => array(
  1088. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1089. 'value' => '0', 'id' => 'UserFormSomething_'
  1090. )),
  1091. array('input' => array(
  1092. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1093. 'value' => '1', 'id' => 'UserFormSomething'
  1094. )),
  1095. 'label' => array('for' => 'UserFormSomething'),
  1096. 'Something',
  1097. '/label',
  1098. '/div'
  1099. );
  1100. $this->assertTags($result, $expected);
  1101. $result = $this->Form->fields;
  1102. $expected = array(
  1103. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1104. 'UserForm.hidden' => '0', 'UserForm.something'
  1105. );
  1106. $this->assertEquals($expected, $result);
  1107. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1108. $result = $this->Form->secure($this->Form->fields);
  1109. $expected = array(
  1110. 'div' => array('style' => 'display:none;'),
  1111. array('input' => array(
  1112. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1113. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1114. )),
  1115. array('input' => array(
  1116. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1117. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1118. )),
  1119. '/div'
  1120. );
  1121. $this->assertTags($result, $expected);
  1122. }
  1123. /**
  1124. * Test secured inputs with custom names.
  1125. *
  1126. * @return void
  1127. */
  1128. public function testSecuredInputCustomName() {
  1129. $this->Form->request['_Token'] = array('key' => 'testKey');
  1130. $this->assertEquals(array(), $this->Form->fields);
  1131. $this->Form->input('text_input', array(
  1132. 'name' => 'data[Option][General.default_role]',
  1133. ));
  1134. $expected = array('Option.General.default_role');
  1135. $this->assertEquals($expected, $this->Form->fields);
  1136. $this->Form->input('select_box', array(
  1137. 'name' => 'data[Option][General.select_role]',
  1138. 'type' => 'select',
  1139. 'options' => array(1, 2),
  1140. ));
  1141. $expected = array('Option.General.default_role', 'Option.General.select_role');
  1142. $this->assertEquals($expected, $this->Form->fields);
  1143. }
  1144. /**
  1145. * Tests that the correct keys are added to the field hash index
  1146. *
  1147. * @return void
  1148. */
  1149. public function testFormSecuredFileInput() {
  1150. $this->Form->request['_Token'] = array('key' => 'testKey');
  1151. $this->assertEquals(array(), $this->Form->fields);
  1152. $this->Form->file('Attachment.file');
  1153. $expected = array(
  1154. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1155. 'Attachment.file.error', 'Attachment.file.size'
  1156. );
  1157. $this->assertEquals($expected, $this->Form->fields);
  1158. }
  1159. /**
  1160. * test that multiple selects keys are added to field hash
  1161. *
  1162. * @return void
  1163. */
  1164. public function testFormSecuredMultipleSelect() {
  1165. $this->Form->request['_Token'] = array('key' => 'testKey');
  1166. $this->assertEquals(array(), $this->Form->fields);
  1167. $options = array('1' => 'one', '2' => 'two');
  1168. $this->Form->select('Model.select', $options);
  1169. $expected = array('Model.select');
  1170. $this->assertEquals($expected, $this->Form->fields);
  1171. $this->Form->fields = array();
  1172. $this->Form->select('Model.select', $options, array('multiple' => true));
  1173. $this->assertEquals($expected, $this->Form->fields);
  1174. }
  1175. /**
  1176. * testFormSecuredRadio method
  1177. *
  1178. * @return void
  1179. */
  1180. public function testFormSecuredRadio() {
  1181. $this->Form->request['_Token'] = array('key' => 'testKey');
  1182. $this->assertEquals(array(), $this->Form->fields);
  1183. $options = array('1' => 'option1', '2' => 'option2');
  1184. $this->Form->radio('Test.test', $options);
  1185. $expected = array('Test.test');
  1186. $this->assertEquals($expected, $this->Form->fields);
  1187. }
  1188. /**
  1189. * Test that when disabled is in a list based attribute array it works.
  1190. *
  1191. * @return void
  1192. */
  1193. public function testFormSecuredAndDisabledNotAssoc() {
  1194. $this->Form->request['_Token'] = array('key' => 'testKey');
  1195. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1196. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1197. $this->Form->text('Model.text', array('disabled'));
  1198. $this->Form->textarea('Model.textarea', array('disabled'));
  1199. $this->Form->password('Model.password', array('disabled'));
  1200. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1201. $expected = array(
  1202. 'Model.radio' => ''
  1203. );
  1204. $this->assertEquals($expected, $this->Form->fields);
  1205. }
  1206. /**
  1207. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1208. * hashing.
  1209. *
  1210. * @return void
  1211. */
  1212. public function testFormSecuredAndDisabled() {
  1213. $this->Form->request['_Token'] = array('key' => 'testKey');
  1214. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1215. $this->Form->text('Model.text', array('disabled' => true));
  1216. $this->Form->password('Model.text', array('disabled' => true));
  1217. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1218. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1219. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1220. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1221. $this->Form->month('Model.month', array('disabled' => true));
  1222. $this->Form->day('Model.day', array('disabled' => true));
  1223. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1224. $this->Form->minute('Model.minute', array('disabled' => true));
  1225. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1226. $expected = array(
  1227. 'Model.radio' => ''
  1228. );
  1229. $this->assertEquals($expected, $this->Form->fields);
  1230. }
  1231. /**
  1232. * testDisableSecurityUsingForm method
  1233. *
  1234. * @return void
  1235. */
  1236. public function testDisableSecurityUsingForm() {
  1237. $this->Form->request['_Token'] = array(
  1238. 'key' => 'testKey',
  1239. 'disabledFields' => array()
  1240. );
  1241. $this->Form->create();
  1242. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1243. $this->Form->input('Addresses.title');
  1244. $this->Form->input('Addresses.first_name', array('secure' => false));
  1245. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1246. $this->Form->input('Addresses.zip', array(
  1247. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1248. ));
  1249. $result = $this->Form->fields;
  1250. $expected = array(
  1251. 'Addresses.id' => '123456', 'Addresses.title',
  1252. );
  1253. $this->assertEquals($expected, $result);
  1254. }
  1255. /**
  1256. * test disableField
  1257. *
  1258. * @return void
  1259. */
  1260. public function testUnlockFieldAddsToList() {
  1261. $this->Form->request['_Token'] = array(
  1262. 'key' => 'testKey',
  1263. 'unlockedFields' => array()
  1264. );
  1265. $this->Form->create('Contact');
  1266. $this->Form->unlockField('Contact.name');
  1267. $this->Form->text('Contact.name');
  1268. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1269. $this->assertEquals(array(), $this->Form->fields);
  1270. }
  1271. /**
  1272. * test unlockField removing from fields array.
  1273. *
  1274. * @return void
  1275. */
  1276. public function testUnlockFieldRemovingFromFields() {
  1277. $this->Form->request['_Token'] = array(
  1278. 'key' => 'testKey',
  1279. 'unlockedFields' => array()
  1280. );
  1281. $this->Form->create('Contact');
  1282. $this->Form->hidden('Contact.id', array('value' => 1));
  1283. $this->Form->text('Contact.name');
  1284. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1285. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1286. $this->Form->unlockField('Contact.name');
  1287. $this->Form->unlockField('Contact.id');
  1288. $this->assertEquals(array(), $this->Form->fields);
  1289. }
  1290. /**
  1291. * testTagIsInvalid method
  1292. *
  1293. * @return void
  1294. */
  1295. public function testTagIsInvalid() {
  1296. $Contact = ClassRegistry::getObject('Contact');
  1297. $Contact->validationErrors[0]['email'] = array('Please provide an email');
  1298. $this->Form->setEntity('Contact.0.email');
  1299. $result = $this->Form->tagIsInvalid();
  1300. $expected = array('Please provide an email');
  1301. $this->assertEquals($expected, $result);
  1302. $this->Form->setEntity('Contact.1.email');
  1303. $result = $this->Form->tagIsInvalid();
  1304. $expected = false;
  1305. $this->assertSame($expected, $result);
  1306. $this->Form->setEntity('Contact.0.name');
  1307. $result = $this->Form->tagIsInvalid();
  1308. $expected = false;
  1309. $this->assertSame($expected, $result);
  1310. }
  1311. /**
  1312. * Test validation errors.
  1313. *
  1314. * @return void
  1315. */
  1316. public function testPasswordValidation() {
  1317. $Contact = ClassRegistry::getObject('Contact');
  1318. $Contact->validationErrors['password'] = array('Please provide a password');
  1319. $result = $this->Form->input('Contact.password');
  1320. $expected = array(
  1321. 'div' => array('class' => 'input password error'),
  1322. 'label' => array('for' => 'ContactPassword'),
  1323. 'Password',
  1324. '/label',
  1325. 'input' => array(
  1326. 'type' => 'password', 'name' => 'data[Contact][password]',
  1327. 'id' => 'ContactPassword', 'class' => 'form-error'
  1328. ),
  1329. array('div' => array('class' => 'error-message')),
  1330. 'Please provide a password',
  1331. '/div',
  1332. '/div'
  1333. );
  1334. $this->assertTags($result, $expected);
  1335. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1336. $expected = array(
  1337. 'div' => array('class' => 'input password error'),
  1338. 'label' => array('for' => 'ContactPassword'),
  1339. 'Password',
  1340. '/label',
  1341. 'input' => array(
  1342. 'type' => 'password', 'name' => 'data[Contact][password]',
  1343. 'id' => 'ContactPassword', 'class' => 'form-error'
  1344. ),
  1345. '/div'
  1346. );
  1347. $this->assertTags($result, $expected);
  1348. }
  1349. /**
  1350. * Test validation errors, when validation message is an empty string.
  1351. *
  1352. * @access public
  1353. * @return void
  1354. */
  1355. public function testEmptyErrorValidation() {
  1356. $this->Form->validationErrors['Contact']['password'] = '';
  1357. $result = $this->Form->input('Contact.password');
  1358. $expected = array(
  1359. 'div' => array('class' => 'input password error'),
  1360. 'label' => array('for' => 'ContactPassword'),
  1361. 'Password',
  1362. '/label',
  1363. 'input' => array(
  1364. 'type' => 'password', 'name' => 'data[Contact][password]',
  1365. 'id' => 'ContactPassword', 'class' => 'form-error'
  1366. ),
  1367. array('div' => array('class' => 'error-message')),
  1368. array(),
  1369. '/div',
  1370. '/div'
  1371. );
  1372. $this->assertTags($result, $expected);
  1373. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1374. $expected = array(
  1375. 'div' => array('class' => 'input password error'),
  1376. 'label' => array('for' => 'ContactPassword'),
  1377. 'Password',
  1378. '/label',
  1379. 'input' => array(
  1380. 'type' => 'password', 'name' => 'data[Contact][password]',
  1381. 'id' => 'ContactPassword', 'class' => 'form-error'
  1382. ),
  1383. '/div'
  1384. );
  1385. $this->assertTags($result, $expected);
  1386. }
  1387. /**
  1388. * Test validation errors, when calling input() overriding validation message by an empty string.
  1389. *
  1390. * @access public
  1391. * @return void
  1392. */
  1393. public function testEmptyInputErrorValidation() {
  1394. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1395. $result = $this->Form->input('Contact.password', array('error' => ''));
  1396. $expected = array(
  1397. 'div' => array('class' => 'input password error'),
  1398. 'label' => array('for' => 'ContactPassword'),
  1399. 'Password',
  1400. '/label',
  1401. 'input' => array(
  1402. 'type' => 'password', 'name' => 'data[Contact][password]',
  1403. 'id' => 'ContactPassword', 'class' => 'form-error'
  1404. ),
  1405. array('div' => array('class' => 'error-message')),
  1406. array(),
  1407. '/div',
  1408. '/div'
  1409. );
  1410. $this->assertTags($result, $expected);
  1411. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1412. $expected = array(
  1413. 'div' => array('class' => 'input password error'),
  1414. 'label' => array('for' => 'ContactPassword'),
  1415. 'Password',
  1416. '/label',
  1417. 'input' => array(
  1418. 'type' => 'password', 'name' => 'data[Contact][password]',
  1419. 'id' => 'ContactPassword', 'class' => 'form-error'
  1420. ),
  1421. '/div'
  1422. );
  1423. $this->assertTags($result, $expected);
  1424. }
  1425. /**
  1426. * testFormValidationAssociated method
  1427. *
  1428. * test display of form errors in conjunction with model::validates.
  1429. *
  1430. * @return void
  1431. */
  1432. public function testFormValidationAssociated() {
  1433. $this->UserForm = ClassRegistry::getObject('UserForm');
  1434. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1435. $data = array(
  1436. 'UserForm' => array('name' => 'user'),
  1437. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1438. );
  1439. $result = $this->UserForm->OpenidUrl->create($data);
  1440. $this->assertFalse(empty($result));
  1441. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1442. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1443. $encoding = strtolower(Configure::read('App.encoding'));
  1444. $expected = array(
  1445. 'form' => array(
  1446. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1447. 'accept-charset' => $encoding
  1448. ),
  1449. 'div' => array('style' => 'display:none;'),
  1450. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1451. '/div'
  1452. );
  1453. $this->assertTags($result, $expected);
  1454. $result = $this->Form->error(
  1455. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1456. );
  1457. $this->assertEquals('Error, not registered', $result);
  1458. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1459. }
  1460. /**
  1461. * testFormValidationAssociatedFirstLevel method
  1462. *
  1463. * test form error display with associated model.
  1464. *
  1465. * @return void
  1466. */
  1467. public function testFormValidationAssociatedFirstLevel() {
  1468. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1469. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1470. $data = array(
  1471. 'ValidateUser' => array('name' => 'mariano'),
  1472. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1473. );
  1474. $result = $this->ValidateUser->create($data);
  1475. $this->assertFalse(empty($result));
  1476. $this->assertFalse($this->ValidateUser->validates());
  1477. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1478. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1479. $encoding = strtolower(Configure::read('App.encoding'));
  1480. $expected = array(
  1481. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1482. 'div' => array('style' => 'display:none;'),
  1483. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1484. '/div'
  1485. );
  1486. $this->assertTags($result, $expected);
  1487. $result = $this->Form->error(
  1488. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1489. );
  1490. $this->assertEquals('Invalid email', $result);
  1491. $result = $this->Form->error(
  1492. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1493. );
  1494. $this->assertEquals('Invalid name', $result);
  1495. $result = $this->Form->error(
  1496. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1497. );
  1498. $this->assertEquals('Invalid city', $result);
  1499. unset($this->ValidateUser->ValidateProfile);
  1500. unset($this->ValidateUser);
  1501. }
  1502. /**
  1503. * testFormValidationAssociatedSecondLevel method
  1504. *
  1505. * test form error display with associated model.
  1506. *
  1507. * @return void
  1508. */
  1509. public function testFormValidationAssociatedSecondLevel() {
  1510. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1511. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1512. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1513. $data = array(
  1514. 'ValidateUser' => array('name' => 'mariano'),
  1515. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1516. 'ValidateItem' => array('name' => 'Item')
  1517. );
  1518. $result = $this->ValidateUser->create($data);
  1519. $this->assertFalse(empty($result));
  1520. $this->assertFalse($this->ValidateUser->validates());
  1521. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1522. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1523. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1524. $encoding = strtolower(Configure::read('App.encoding'));
  1525. $expected = array(
  1526. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1527. 'div' => array('style' => 'display:none;'),
  1528. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1529. '/div'
  1530. );
  1531. $this->assertTags($result, $expected);
  1532. $result = $this->Form->error(
  1533. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1534. );
  1535. $this->assertEquals('Invalid email', $result);
  1536. $result = $this->Form->error(
  1537. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1538. );
  1539. $this->assertEquals('Invalid name', $result);
  1540. $result = $this->Form->error(
  1541. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1542. );
  1543. $result = $this->Form->error(
  1544. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1545. );
  1546. $this->assertEquals('Invalid description', $result);
  1547. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1548. unset($this->ValidateUser->ValidateProfile);
  1549. unset($this->ValidateUser);
  1550. }
  1551. /**
  1552. * testFormValidationMultiRecord method
  1553. *
  1554. * test form error display with multiple records.
  1555. *
  1556. * @return void
  1557. */
  1558. public function testFormValidationMultiRecord() {
  1559. $Contact = ClassRegistry::getObject('Contact');
  1560. $Contact->validationErrors[2] = array(
  1561. 'name' => array('This field cannot be left blank')
  1562. );
  1563. $result = $this->Form->input('Contact.2.name');
  1564. $expected = array(
  1565. 'div' => array('class' => 'input text error'),
  1566. 'label' => array('for' => 'Contact2Name'),
  1567. 'Name',
  1568. '/label',
  1569. 'input' => array(
  1570. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1571. 'class' => 'form-error', 'maxlength' => 255
  1572. ),
  1573. array('div' => array('class' => 'error-message')),
  1574. 'This field cannot be left blank',
  1575. '/div',
  1576. '/div'
  1577. );
  1578. $this->assertTags($result, $expected);
  1579. }
  1580. /**
  1581. * testMultipleInputValidation method
  1582. *
  1583. * test multiple record form validation error display.
  1584. *
  1585. * @return void
  1586. */
  1587. public function testMultipleInputValidation() {
  1588. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1589. $Address->validationErrors[0] = array(
  1590. 'title' => array('This field cannot be empty'),
  1591. 'first_name' => array('This field cannot be empty')
  1592. );
  1593. $Address->validationErrors[1] = array(
  1594. 'last_name' => array('You must have a last name')
  1595. );
  1596. $this->Form->create();
  1597. $result = $this->Form->input('Address.0.title');
  1598. $expected = array(
  1599. 'div' => array('class'),
  1600. 'label' => array('for'),
  1601. 'preg:/[^<]+/',
  1602. '/label',
  1603. 'input' => array(
  1604. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1605. ),
  1606. array('div' => array('class' => 'error-message')),
  1607. 'This field cannot be empty',
  1608. '/div',
  1609. '/div'
  1610. );
  1611. $this->assertTags($result, $expected);
  1612. $result = $this->Form->input('Address.0.first_name');
  1613. $expected = array(
  1614. 'div' => array('class'),
  1615. 'label' => array('for'),
  1616. 'preg:/[^<]+/',
  1617. '/label',
  1618. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1619. array('div' => array('class' => 'error-message')),
  1620. 'This field cannot be empty',
  1621. '/div',
  1622. '/div'
  1623. );
  1624. $this->assertTags($result, $expected);
  1625. $result = $this->Form->input('Address.0.last_name');
  1626. $expected = array(
  1627. 'div' => array('class'),
  1628. 'label' => array('for'),
  1629. 'preg:/[^<]+/',
  1630. '/label',
  1631. 'input' => array('type' => 'text', 'name', 'id'),
  1632. '/div'
  1633. );
  1634. $this->assertTags($result, $expected);
  1635. $result = $this->Form->input('Address.1.last_name');
  1636. $expected = array(
  1637. 'div' => array('class'),
  1638. 'label' => array('for'),
  1639. 'preg:/[^<]+/',
  1640. '/label',
  1641. 'input' => array(
  1642. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1643. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1644. ),
  1645. array('div' => array('class' => 'error-message')),
  1646. 'You must have a last name',
  1647. '/div',
  1648. '/div'
  1649. );
  1650. $this->assertTags($result, $expected);
  1651. }
  1652. /**
  1653. * testInput method
  1654. *
  1655. * Test various incarnations of input().
  1656. *
  1657. * @return void
  1658. */
  1659. public function testInput() {
  1660. $result = $this->Form->input('ValidateUser.balance');
  1661. $expected = array(
  1662. 'div' => array('class'),
  1663. 'label' => array('for'),
  1664. 'Balance',
  1665. '/label',
  1666. 'input' => array('name', 'type' => 'number', 'id'),
  1667. '/div',
  1668. );
  1669. $this->assertTags($result, $expected);
  1670. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1671. $expected = array(
  1672. 'div' => array('class' => 'input email'),
  1673. 'label' => array('for' => 'custom'),
  1674. 'Email',
  1675. '/label',
  1676. array('input' => array(
  1677. 'type' => 'email', 'name' => 'data[Contact][email]',
  1678. 'id' => 'custom', 'maxlength' => 255
  1679. )),
  1680. '/div'
  1681. );
  1682. $this->assertTags($result, $expected);
  1683. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1684. $expected = array(
  1685. '<div',
  1686. 'label' => array('for' => 'ContactEmail'),
  1687. 'Email',
  1688. '/label',
  1689. array('input' => array(
  1690. 'type' => 'email', 'name' => 'data[Contact][email]',
  1691. 'id' => 'ContactEmail', 'maxlength' => 255
  1692. )),
  1693. '/div'
  1694. );
  1695. $this->assertTags($result, $expected);
  1696. $result = $this->Form->hidden('Contact.idontexist');
  1697. $expected = array('input' => array(
  1698. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1699. 'id' => 'ContactIdontexist'
  1700. ));
  1701. $this->assertTags($result, $expected);
  1702. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1703. $expected = array(
  1704. 'div' => array('class' => 'input text'),
  1705. 'label' => array('for' => 'ContactEmail'),
  1706. 'Email',
  1707. '/label',
  1708. array('input' => array(
  1709. 'type' => 'text', 'name' => 'data[Contact][email]',
  1710. 'id' => 'ContactEmail'
  1711. )),
  1712. '/div'
  1713. );
  1714. $this->assertTags($result, $expected);
  1715. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1716. $expected = array(
  1717. 'div' => array('class' => 'input text'),
  1718. 'label' => array('for' => 'Contact5Email'),
  1719. 'Email',
  1720. '/label',
  1721. array('input' => array(
  1722. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1723. 'id' => 'Contact5Email'
  1724. )),
  1725. '/div'
  1726. );
  1727. $this->assertTags($result, $expected);
  1728. $result = $this->Form->input('Contact.password');
  1729. $expected = array(
  1730. 'div' => array('class' => 'input password'),
  1731. 'label' => array('for' => 'ContactPassword'),
  1732. 'Password',
  1733. '/label',
  1734. array('input' => array(
  1735. 'type' => 'password', 'name' => 'data[Contact][password]',
  1736. 'id' => 'ContactPassword'
  1737. )),
  1738. '/div'
  1739. );
  1740. $this->assertTags($result, $expected);
  1741. $result = $this->Form->input('Contact.email', array(
  1742. 'type' => 'file', 'class' => 'textbox'
  1743. ));
  1744. $expected = array(
  1745. 'div' => array('class' => 'input file'),
  1746. 'label' => array('for' => 'ContactEmail'),
  1747. 'Email',
  1748. '/label',
  1749. array('input' => array(
  1750. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1751. 'id' => 'ContactEmail'
  1752. )),
  1753. '/div'
  1754. );
  1755. $this->assertTags($result, $expected);
  1756. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1757. $result = $this->Form->input('Contact.phone');
  1758. $expected = array(
  1759. 'div' => array('class' => 'input tel'),
  1760. 'label' => array('for' => 'ContactPhone'),
  1761. 'Phone',
  1762. '/label',
  1763. array('input' => array(
  1764. 'type' => 'tel', 'name' => 'data[Contact][phone]',
  1765. 'value' => 'Hello &amp; World &gt; weird chars',
  1766. 'id' => 'ContactPhone', 'maxlength' => 255
  1767. )),
  1768. '/div'
  1769. );
  1770. $this->assertTags($result, $expected);
  1771. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1772. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1773. $expected = array(
  1774. 'div' => array('class' => 'input text'),
  1775. 'label' => array('for' => 'myId'),
  1776. 'Field',
  1777. '/label',
  1778. 'input' => array(
  1779. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1780. 'value' => 'My value', 'id' => 'myId'
  1781. ),
  1782. '/div'
  1783. );
  1784. $this->assertTags($result, $expected);
  1785. unset($this->Form->request->data);
  1786. $Contact = ClassRegistry::getObject('Contact');
  1787. $Contact->validationErrors['field'] = array('Badness!');
  1788. $result = $this->Form->input('Contact.field');
  1789. $expected = array(
  1790. 'div' => array('class' => 'input text error'),
  1791. 'label' => array('for' => 'ContactField'),
  1792. 'Field',
  1793. '/label',
  1794. 'input' => array(
  1795. 'type' => 'text', 'name' => 'data[Contact][field]',
  1796. 'id' => 'ContactField', 'class' => 'form-error'
  1797. ),
  1798. array('div' => array('class' => 'error-message')),
  1799. 'Badness!',
  1800. '/div',
  1801. '/div'
  1802. );
  1803. $this->assertTags($result, $expected);
  1804. $result = $this->Form->input('Contact.field', array(
  1805. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1806. ));
  1807. $expected = array(
  1808. 'label' => array('for' => 'ContactField'),
  1809. 'Field',
  1810. '/label',
  1811. 'input' => array(
  1812. 'type' => 'text', 'name' => 'data[Contact][field]',
  1813. 'id' => 'ContactField', 'class' => 'form-error'
  1814. ),
  1815. array('span' => array('class' => 'error-message')),
  1816. 'Badness!',
  1817. '/span'
  1818. );
  1819. $this->assertTags($result, $expected);
  1820. $result = $this->Form->input('Contact.field', array(
  1821. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1822. ));
  1823. $expected = array(
  1824. 'div' => array('class' => 'input text error'),
  1825. 'label' => array('for' => 'ContactField'),
  1826. 'Field',
  1827. '/label',
  1828. 'input' => array(
  1829. 'type' => 'text', 'name' => 'data[Contact][field]',
  1830. 'id' => 'ContactField', 'class' => 'form-error'
  1831. ),
  1832. array('div' => array('class' => 'error')),
  1833. 'Badness!',
  1834. '/div'
  1835. );
  1836. $this->assertTags($result, $expected);
  1837. $result = $this->Form->input('Contact.field', array(
  1838. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1839. ));
  1840. $expected = array(
  1841. 'span' => array('class' => 'input text error'),
  1842. 'label' => array('for' => 'ContactField'),
  1843. 'Field',
  1844. '/label',
  1845. 'input' => array(
  1846. 'type' => 'text', 'name' => 'data[Contact][field]',
  1847. 'id' => 'ContactField', 'class' => 'form-error'
  1848. ),
  1849. 'Badness!',
  1850. '/span'
  1851. );
  1852. $this->assertTags($result, $expected);
  1853. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1854. $expected = array(
  1855. 'div' => array('class' => 'input text error'),
  1856. 'label' => array('for' => 'ContactField'),
  1857. 'Field',
  1858. '/label',
  1859. 'input' => array(
  1860. 'type' => 'text', 'name' => 'data[Contact][field]',
  1861. 'id' => 'ContactField', 'class' => 'form-error'
  1862. ),
  1863. 'A message to you, Rudy',
  1864. array('div' => array('class' => 'error-message')),
  1865. 'Badness!',
  1866. '/div',
  1867. '/div'
  1868. );
  1869. $this->assertTags($result, $expected);
  1870. $this->Form->setEntity(null);
  1871. $this->Form->setEntity('Contact.field');
  1872. $result = $this->Form->input('Contact.field', array(
  1873. 'after' => 'A message to you, Rudy', 'error' => false
  1874. ));
  1875. $expected = array(
  1876. 'div' => array('class' => 'input text'),
  1877. 'label' => array('for' => 'ContactField'),
  1878. 'Field',
  1879. '/label',
  1880. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1881. 'A message to you, Rudy',
  1882. '/div'
  1883. );
  1884. $this->assertTags($result, $expected);
  1885. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1886. $expected = array(
  1887. 'div' => array('class' => 'input text'),
  1888. 'label' => array('for' => 'ObjectField'),
  1889. 'Field',
  1890. '/label',
  1891. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1892. 'A message to you, Rudy',
  1893. '/div'
  1894. );
  1895. $this->assertTags($result, $expected);
  1896. $Contact->validationErrors['field'] = array('minLength');
  1897. $result = $this->Form->input('Contact.field', array(
  1898. 'error' => array(
  1899. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1900. 'maxLength' => 'login too large'
  1901. )
  1902. ));
  1903. $expected = array(
  1904. 'div' => array('class' => 'input text error'),
  1905. 'label' => array('for' => 'ContactField'),
  1906. 'Field',
  1907. '/label',
  1908. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1909. array('div' => array('class' => 'error-message')),
  1910. 'Le login doit contenir au moins 2 caractères',
  1911. '/div',
  1912. '/div'
  1913. );
  1914. $this->assertTags($result, $expected);
  1915. $Contact->validationErrors['field'] = array('maxLength');
  1916. $result = $this->Form->input('Contact.field', array(
  1917. 'error' => array(
  1918. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1919. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1920. 'maxLength' => 'login too large',
  1921. )
  1922. ));
  1923. $expected = array(
  1924. 'div' => array('class' => 'input text error'),
  1925. 'label' => array('for' => 'ContactField'),
  1926. 'Field',
  1927. '/label',
  1928. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1929. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1930. 'login too large',
  1931. '/span',
  1932. '/div'
  1933. );
  1934. $this->assertTags($result, $expected);
  1935. }
  1936. /**
  1937. * Test that inputs with 0 can be created.
  1938. *
  1939. * @return void
  1940. */
  1941. public function testInputZero() {
  1942. $this->Form->create('User');
  1943. $result = $this->Form->input('0');
  1944. $expected = array(
  1945. 'div' => array('class' => 'input text'),
  1946. 'label' => array('for' => 'User0'), '/label',
  1947. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  1948. '/div'
  1949. );
  1950. $this->assertTags($result, $expected);
  1951. }
  1952. /**
  1953. * test input() with checkbox creation
  1954. *
  1955. * @return void
  1956. */
  1957. public function testInputCheckbox() {
  1958. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1959. $expected = array(
  1960. 'div' => array('class' => 'input checkbox'),
  1961. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1962. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1963. '/div'
  1964. );
  1965. $this->assertTags($result, $expected);
  1966. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1967. $expected = array(
  1968. 'div' => array('class' => 'input checkbox'),
  1969. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1970. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1971. '/div'
  1972. );
  1973. $this->assertTags($result, $expected);
  1974. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1975. $expected = array(
  1976. 'div' => array('class' => 'input checkbox'),
  1977. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1978. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1979. '/div'
  1980. );
  1981. $this->assertTags($result, $expected);
  1982. $result = $this->Form->input('User.disabled', array(
  1983. 'label' => 'Disabled',
  1984. 'type' => 'checkbox',
  1985. 'data-foo' => 'disabled'
  1986. ));
  1987. $expected = array(
  1988. 'div' => array('class' => 'input checkbox'),
  1989. 'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
  1990. array('input' => array(
  1991. 'type' => 'checkbox',
  1992. 'name' => 'data[User][disabled]',
  1993. 'value' => '1',
  1994. 'id' => 'UserDisabled',
  1995. 'data-foo' => 'disabled'
  1996. )),
  1997. 'label' => array('for' => 'UserDisabled'),
  1998. 'Disabled',
  1999. '/label',
  2000. '/div'
  2001. );
  2002. $this->assertTags($result, $expected);
  2003. }
  2004. /**
  2005. * test form->input() with time types.
  2006. *
  2007. */
  2008. public function testInputTime() {
  2009. extract($this->dateRegex);
  2010. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2011. $result = explode(':', $result);
  2012. $this->assertRegExp('/option value="23"/', $result[0]);
  2013. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2014. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2015. $result = explode(':', $result);
  2016. $this->assertRegExp('/option value="23"/', $result[0]);
  2017. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2018. $result = $this->Form->input('Model.field', array(
  2019. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2020. ));
  2021. $result = explode(':', $result);
  2022. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2023. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2024. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2025. $result = $this->Form->input('Model.field', array(
  2026. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2027. ));
  2028. $result = explode(':', $result);
  2029. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2030. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2031. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2032. $result = $this->Form->input('prueba', array(
  2033. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  2034. 'maxYear' => date('Y') + 1 ,'interval' => 15
  2035. ));
  2036. $result = explode(':', $result);
  2037. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2038. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2039. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2040. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2041. $result = $this->Form->input('Random.start_time', array(
  2042. 'type' => 'time',
  2043. 'selected' => '18:15'
  2044. ));
  2045. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2046. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2047. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2048. $result = $this->Form->input('published', array('type' => 'time'));
  2049. $now = strtotime('now');
  2050. $this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
  2051. $now = strtotime('2013-03-09 00:42:21');
  2052. $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
  2053. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2054. $this->assertContains('<option value="42" selected="selected">42</option>', $result);
  2055. }
  2056. /**
  2057. * Test interval + selected near the hour roll over.
  2058. *
  2059. * @return void
  2060. */
  2061. public function testTimeSelectedWithInterval() {
  2062. $result = $this->Form->input('Model.start_time', array(
  2063. 'type' => 'time',
  2064. 'interval' => 15,
  2065. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2066. ));
  2067. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2068. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2069. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2070. $result = $this->Form->input('Model.start_time', array(
  2071. 'type' => 'time',
  2072. 'interval' => 15,
  2073. 'selected' => '2012-10-23 15:57:00'
  2074. ));
  2075. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2076. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2077. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2078. $result = $this->Form->input('Model.start_time', array(
  2079. 'timeFormat' => 24,
  2080. 'type' => 'time',
  2081. 'interval' => 15,
  2082. 'selected' => '15:57'
  2083. ));
  2084. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2085. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2086. $result = $this->Form->input('Model.start_time', array(
  2087. 'timeFormat' => 24,
  2088. 'type' => 'time',
  2089. 'interval' => 15,
  2090. 'selected' => '23:57'
  2091. ));
  2092. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2093. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2094. $result = $this->Form->input('Model.created', array(
  2095. 'timeFormat' => 24,
  2096. 'type' => 'datetime',
  2097. 'interval' => 15,
  2098. 'selected' => '2012-09-30 23:56'
  2099. ));
  2100. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2101. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2102. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2103. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2104. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2105. }
  2106. /**
  2107. * Test interval & timeFormat = 12
  2108. *
  2109. * @return void
  2110. */
  2111. public function testInputTimeWithIntervalAnd12HourFormat() {
  2112. $result = $this->Form->input('Model.start_time', array(
  2113. 'type' => 'time',
  2114. 'timeFormat' => 12,
  2115. 'interval' => 5,
  2116. 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
  2117. ));
  2118. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2119. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2120. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2121. $result = $this->Form->input('Model.start_time', array(
  2122. 'type' => 'time',
  2123. 'timeFormat' => '12',
  2124. 'interval' => 5,
  2125. 'selected' => '2013-04-19 16:30:00'
  2126. ));
  2127. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2128. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2129. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2130. $result = $this->Form->input('Model.start_time', array(
  2131. 'type' => 'time',
  2132. 'timeFormat' => '12',
  2133. 'interval' => 10,
  2134. 'selected' => '2013-05-19 00:33:00'
  2135. ));
  2136. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2137. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2138. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2139. $result = $this->Form->input('Model.start_time', array(
  2140. 'type' => 'time',
  2141. 'timeFormat' => '12',
  2142. 'interval' => 10,
  2143. 'selected' => '2013-05-19 13:33:00'
  2144. ));
  2145. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2146. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2147. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2148. $result = $this->Form->input('Model.start_time', array(
  2149. 'type' => 'time',
  2150. 'timeFormat' => '12',
  2151. 'interval' => 10,
  2152. 'selected' => '2013-05-19 01:33:00'
  2153. ));
  2154. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2155. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2156. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2157. }
  2158. /**
  2159. * test form->input() with datetime, date and time types
  2160. *
  2161. * @return void
  2162. */
  2163. public function testInputDatetime() {
  2164. extract($this->dateRegex);
  2165. $result = $this->Form->input('prueba', array(
  2166. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2167. 'maxYear' => date('Y') + 1, 'interval' => 15
  2168. ));
  2169. $result = explode(':', $result);
  2170. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2171. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2172. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2173. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2174. //related to ticket #5013
  2175. $result = $this->Form->input('Contact.date', array(
  2176. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2177. ));
  2178. $this->assertRegExp('/class="customClass"/', $result);
  2179. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2180. $result = $this->Form->input('Contact.date', array(
  2181. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2182. ));
  2183. $this->assertRegExp('/id="customIdDay"/', $result);
  2184. $this->assertRegExp('/id="customIdMonth"/', $result);
  2185. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2186. $result = $this->Form->input('Model.field', array(
  2187. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2188. ));
  2189. $this->assertRegExp('/id="customIDDay"/', $result);
  2190. $this->assertRegExp('/id="customIDHour"/', $result);
  2191. $result = explode('</select><select', $result);
  2192. $result = explode(':', $result[1]);
  2193. $this->assertRegExp('/option value="23"/', $result[0]);
  2194. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2195. $result = $this->Form->input('Model.field', array(
  2196. 'type' => 'datetime', 'timeFormat' => 12
  2197. ));
  2198. $result = explode('</select><select', $result);
  2199. $result = explode(':', $result[1]);
  2200. $this->assertRegExp('/option value="12"/', $result[0]);
  2201. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2202. $this->Form->request->data = array('Contact' => array('created' => null));
  2203. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2204. $expected = array(
  2205. 'div' => array('class' => 'input date'),
  2206. 'label' => array('for' => 'ContactCreatedMonth'),
  2207. 'Created',
  2208. '/label',
  2209. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2210. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2211. $monthsRegex,
  2212. '/select', '-',
  2213. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2214. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2215. $daysRegex,
  2216. '/select', '-',
  2217. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2218. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2219. $yearsRegex,
  2220. '/select',
  2221. '/div'
  2222. );
  2223. $this->assertTags($result, $expected);
  2224. $this->Form->request->data = array('Contact' => array('created' => null));
  2225. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2226. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2227. $this->Form->request->data = array('Contact' => array('created' => null));
  2228. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2229. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2230. $result = $this->Form->input('Contact.created', array(
  2231. 'type' => 'date',
  2232. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2233. 'timeFormat' => 'NONE'
  2234. ));
  2235. $this->assertRegExp('/for\="created-month"/', $result);
  2236. }
  2237. /**
  2238. * Test generating checkboxes in a loop.
  2239. *
  2240. * @return void
  2241. */
  2242. public function testInputCheckboxesInLoop() {
  2243. for ($i = 1; $i < 5; $i++) {
  2244. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2245. $expected = array(
  2246. 'div' => array('class' => 'input checkbox'),
  2247. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2248. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2249. 'label' => array('for' => "Contact{$i}Email"),
  2250. 'Email',
  2251. '/label',
  2252. '/div'
  2253. );
  2254. $this->assertTags($result, $expected);
  2255. }
  2256. }
  2257. /**
  2258. * Test generating checkboxes with disabled elements.
  2259. *
  2260. * @return void
  2261. */
  2262. public function testInputCheckboxWithDisabledElements() {
  2263. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2264. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2265. $expected = array(
  2266. array('div' => array('class' => 'input select')),
  2267. array('label' => array('for' => "ContactMultiple")),
  2268. 'Multiple',
  2269. '/label',
  2270. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2271. array('div' => array('class' => 'checkbox')),
  2272. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2273. array('label' => array('for' => "ContactMultiple1")),
  2274. 'One',
  2275. '/label',
  2276. '/div',
  2277. array('div' => array('class' => 'checkbox')),
  2278. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2279. array('label' => array('for' => "ContactMultiple2")),
  2280. 'Two',
  2281. '/label',
  2282. '/div',
  2283. array('div' => array('class' => 'checkbox')),
  2284. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2285. array('label' => array('for' => "ContactMultiple3")),
  2286. 'Three',
  2287. '/label',
  2288. '/div',
  2289. '/div'
  2290. );
  2291. $this->assertTags($result, $expected);
  2292. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2293. $this->assertTags($result, $expected);
  2294. $disabled = array('2', 3);
  2295. $expected = array(
  2296. array('div' => array('class' => 'input select')),
  2297. array('label' => array('for' => "ContactMultiple")),
  2298. 'Multiple',
  2299. '/label',
  2300. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2301. array('div' => array('class' => 'checkbox')),
  2302. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2303. array('label' => array('for' => "ContactMultiple1")),
  2304. 'One',
  2305. '/label',
  2306. '/div',
  2307. array('div' => array('class' => 'checkbox')),
  2308. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2309. array('label' => array('for' => "ContactMultiple2")),
  2310. 'Two',
  2311. '/label',
  2312. '/div',
  2313. array('div' => array('class' => 'checkbox')),
  2314. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2315. array('label' => array('for' => "ContactMultiple3")),
  2316. 'Three',
  2317. '/label',
  2318. '/div',
  2319. '/div'
  2320. );
  2321. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2322. $this->assertTags($result, $expected);
  2323. // make sure 50 does only disable 50, and not 50f5c0cf
  2324. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2325. $disabled = array(50);
  2326. $expected = array(
  2327. array('div' => array('class' => 'input select')),
  2328. array('label' => array('for' => "ContactMultiple")),
  2329. 'Multiple',
  2330. '/label',
  2331. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2332. array('div' => array('class' => 'checkbox')),
  2333. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2334. array('label' => array('for' => "ContactMultiple50")),
  2335. 'Fifty',
  2336. '/label',
  2337. '/div',
  2338. array('div' => array('class' => 'checkbox')),
  2339. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2340. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2341. 'Stringy',
  2342. '/label',
  2343. '/div',
  2344. '/div'
  2345. );
  2346. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2347. $this->assertTags($result, $expected);
  2348. }
  2349. /**
  2350. * test input name with leading integer, ensure attributes are generated correctly.
  2351. *
  2352. * @return void
  2353. */
  2354. public function testInputWithLeadingInteger() {
  2355. $result = $this->Form->text('0.Node.title');
  2356. $expected = array(
  2357. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2358. );
  2359. $this->assertTags($result, $expected);
  2360. }
  2361. /**
  2362. * test form->input() with select type inputs.
  2363. *
  2364. * @return void
  2365. */
  2366. public function testInputSelectType() {
  2367. $result = $this->Form->input('email', array(
  2368. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2369. );
  2370. $expected = array(
  2371. 'div' => array('class' => 'input select'),
  2372. 'label' => array('for' => 'email'),
  2373. 'Email',
  2374. '/label',
  2375. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2376. array('option' => array('value' => '')),
  2377. '/option',
  2378. array('option' => array('value' => 'è')),
  2379. 'Firést',
  2380. '/option',
  2381. array('option' => array('value' => 'é')),
  2382. 'Secoènd',
  2383. '/option',
  2384. '/select',
  2385. '/div'
  2386. );
  2387. $this->assertTags($result, $expected);
  2388. $result = $this->Form->input('email', array(
  2389. 'options' => array('First', 'Second'), 'empty' => true)
  2390. );
  2391. $expected = array(
  2392. 'div' => array('class' => 'input select'),
  2393. 'label' => array('for' => 'email'),
  2394. 'Email',
  2395. '/label',
  2396. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2397. array('option' => array('value' => '')),
  2398. '/option',
  2399. array('option' => array('value' => '0')),
  2400. 'First',
  2401. '/option',
  2402. array('option' => array('value' => '1')),
  2403. 'Second',
  2404. '/option',
  2405. '/select',
  2406. '/div'
  2407. );
  2408. $this->assertTags($result, $expected);
  2409. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2410. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2411. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2412. $expected = array(
  2413. 'div' => array('class' => 'input select'),
  2414. 'label' => array('for' => 'ModelUserId'),
  2415. 'User',
  2416. '/label',
  2417. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2418. array('option' => array('value' => '')),
  2419. '/option',
  2420. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2421. 'good',
  2422. '/option',
  2423. array('option' => array('value' => 'other')),
  2424. 'bad',
  2425. '/option',
  2426. '/select',
  2427. '/div'
  2428. );
  2429. $this->assertTags($result, $expected);
  2430. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2431. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2432. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2433. $expected = array(
  2434. 'div' => array('class' => 'input select'),
  2435. 'label' => array('for' => 'ThingUserId'),
  2436. 'User',
  2437. '/label',
  2438. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2439. array('option' => array('value' => '')),
  2440. 'Some Empty',
  2441. '/option',
  2442. array('option' => array('value' => 'value')),
  2443. 'good',
  2444. '/option',
  2445. array('option' => array('value' => 'other')),
  2446. 'bad',
  2447. '/option',
  2448. '/select',
  2449. '/div'
  2450. );
  2451. $this->assertTags($result, $expected);
  2452. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2453. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2454. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2455. $expected = array(
  2456. 'div' => array('class' => 'input select'),
  2457. 'label' => array('for' => 'ThingUserId'),
  2458. 'User',
  2459. '/label',
  2460. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2461. array('option' => array('value' => '')),
  2462. 'Some Empty',
  2463. '/option',
  2464. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2465. 'good',
  2466. '/option',
  2467. array('option' => array('value' => 'other')),
  2468. 'bad',
  2469. '/option',
  2470. '/select',
  2471. '/div'
  2472. );
  2473. $this->assertTags($result, $expected);
  2474. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2475. $this->Form->request->data = array('User' => array('User' => array('value')));
  2476. $result = $this->Form->input('User.User', array('empty' => true));
  2477. $expected = array(
  2478. 'div' => array('class' => 'input select'),
  2479. 'label' => array('for' => 'UserUser'),
  2480. 'User',
  2481. '/label',
  2482. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2483. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2484. array('option' => array('value' => '')),
  2485. '/option',
  2486. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2487. 'good',
  2488. '/option',
  2489. array('option' => array('value' => 'other')),
  2490. 'bad',
  2491. '/option',
  2492. '/select',
  2493. '/div'
  2494. );
  2495. $this->assertTags($result, $expected);
  2496. $this->Form->data = array();
  2497. $result = $this->Form->input('Publisher.id', array(
  2498. 'label' => 'Publisher',
  2499. 'type' => 'select',
  2500. 'multiple' => 'checkbox',
  2501. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2502. ));
  2503. $expected = array(
  2504. array('div' => array('class' => 'input select')),
  2505. array('label' => array('for' => 'PublisherId')),
  2506. 'Publisher',
  2507. '/label',
  2508. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2509. array('div' => array('class' => 'checkbox')),
  2510. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2511. array('label' => array('for' => 'PublisherIdValue1')),
  2512. 'Label 1',
  2513. '/label',
  2514. '/div',
  2515. array('div' => array('class' => 'checkbox')),
  2516. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2517. array('label' => array('for' => 'PublisherIdValue2')),
  2518. 'Label 2',
  2519. '/label',
  2520. '/div',
  2521. '/div'
  2522. );
  2523. $this->assertTags($result, $expected);
  2524. }
  2525. /**
  2526. * test that input() and a non standard primary key makes a hidden input by default.
  2527. *
  2528. * @return void
  2529. */
  2530. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2531. $this->Form->create('User');
  2532. $this->Form->fieldset = array(
  2533. 'User' => array(
  2534. 'fields' => array(
  2535. 'model_id' => array('type' => 'integer')
  2536. ),
  2537. 'validates' => array(),
  2538. 'key' => 'model_id'
  2539. )
  2540. );
  2541. $result = $this->Form->input('model_id');
  2542. $expected = array(
  2543. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2544. );
  2545. $this->assertTags($result, $expected);
  2546. }
  2547. /**
  2548. * test that overriding the magic select type widget is possible
  2549. *
  2550. * @return void
  2551. */
  2552. public function testInputOverridingMagicSelectType() {
  2553. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2554. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2555. $expected = array(
  2556. 'div' => array('class' => 'input text'),
  2557. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2558. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2559. '/div'
  2560. );
  2561. $this->assertTags($result, $expected);
  2562. //Check that magic types still work for plural/singular vars
  2563. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2564. $result = $this->Form->input('Model.type');
  2565. $expected = array(
  2566. 'div' => array('class' => 'input select'),
  2567. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2568. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2569. array('option' => array('value' => 'value')), 'good', '/option',
  2570. array('option' => array('value' => 'other')), 'bad', '/option',
  2571. '/select',
  2572. '/div'
  2573. );
  2574. $this->assertTags($result, $expected);
  2575. }
  2576. /**
  2577. * Test that magic input() selects are created for type=number
  2578. *
  2579. * @return void
  2580. */
  2581. public function testInputMagicSelectForTypeNumber() {
  2582. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2583. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2584. $result = $this->Form->input('ValidateUser.balance');
  2585. $expected = array(
  2586. 'div' => array('class' => 'input select'),
  2587. 'label' => array('for' => 'ValidateUserBalance'),
  2588. 'Balance',
  2589. '/label',
  2590. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2591. array('option' => array('value' => '0')),
  2592. 'nothing',
  2593. '/option',
  2594. array('option' => array('value' => '1', 'selected' => 'selected')),
  2595. 'some',
  2596. '/option',
  2597. array('option' => array('value' => '100')),
  2598. 'a lot',
  2599. '/option',
  2600. '/select',
  2601. '/div'
  2602. );
  2603. $this->assertTags($result, $expected);
  2604. }
  2605. /**
  2606. * Test that magic input() selects can easily be converted into radio types without error.
  2607. *
  2608. * @return void
  2609. */
  2610. public function testInputMagicSelectChangeToRadio() {
  2611. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2612. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2613. $this->assertRegExp('/input type="radio"/', $result);
  2614. }
  2615. /**
  2616. * fields with the same name as the model should work.
  2617. *
  2618. * @return void
  2619. */
  2620. public function testInputWithMatchingFieldAndModelName() {
  2621. $this->Form->create('User');
  2622. $this->Form->fieldset = array(
  2623. 'User' => array(
  2624. 'fields' => array(
  2625. 'User' => array('type' => 'text')
  2626. ),
  2627. 'validates' => array(),
  2628. 'key' => 'id'
  2629. )
  2630. );
  2631. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2632. $result = $this->Form->input('User', array('type' => 'text'));
  2633. $expected = array(
  2634. 'div' => array('class' => 'input text'),
  2635. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2636. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2637. '/div'
  2638. );
  2639. $this->assertTags($result, $expected);
  2640. }
  2641. /**
  2642. * testFormInputs method
  2643. *
  2644. * test correct results from form::inputs().
  2645. *
  2646. * @return void
  2647. */
  2648. public function testFormInputs() {
  2649. $this->Form->create('Contact');
  2650. $result = $this->Form->inputs('The Legend');
  2651. $expected = array(
  2652. '<fieldset',
  2653. '<legend',
  2654. 'The Legend',
  2655. '/legend',
  2656. '*/fieldset',
  2657. );
  2658. $this->assertTags($result, $expected);
  2659. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2660. $expected = array(
  2661. 'fieldset' => array('class' => 'classy-stuff'),
  2662. '<legend',
  2663. 'Field of Dreams',
  2664. '/legend',
  2665. '*/fieldset'
  2666. );
  2667. $this->assertTags($result, $expected);
  2668. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2669. $this->assertTags($result, $expected);
  2670. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2671. $this->assertTags($result, $expected);
  2672. $this->Form->create('Contact');
  2673. $this->Form->request['prefix'] = 'admin';
  2674. $this->Form->request['action'] = 'admin_edit';
  2675. $result = $this->Form->inputs();
  2676. $expected = array(
  2677. '<fieldset',
  2678. '<legend',
  2679. 'Edit Contact',
  2680. '/legend',
  2681. '*/fieldset',
  2682. );
  2683. $this->assertTags($result, $expected);
  2684. $this->Form->create('Contact');
  2685. $result = $this->Form->inputs(false);
  2686. $expected = array(
  2687. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2688. array('div' => array('class' => 'input text')),
  2689. '*/div',
  2690. array('div' => array('class' => 'input email')),
  2691. '*/div',
  2692. array('div' => array('class' => 'input tel')),
  2693. '*/div',
  2694. array('div' => array('class' => 'input password')),
  2695. '*/div',
  2696. array('div' => array('class' => 'input date')),
  2697. '*/div',
  2698. array('div' => array('class' => 'input date')),
  2699. '*/div',
  2700. array('div' => array('class' => 'input datetime')),
  2701. '*/div',
  2702. array('div' => array('class' => 'input number')),
  2703. '*/div',
  2704. array('div' => array('class' => 'input select')),
  2705. '*/div',
  2706. );
  2707. $this->assertTags($result, $expected);
  2708. $this->Form->create('Contact');
  2709. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2710. $expected = array(
  2711. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2712. array('div' => array('class' => 'input text')),
  2713. '*/div',
  2714. array('div' => array('class' => 'input email')),
  2715. '*/div',
  2716. array('div' => array('class' => 'input tel')),
  2717. '*/div',
  2718. array('div' => array('class' => 'input password')),
  2719. '*/div',
  2720. array('div' => array('class' => 'input date')),
  2721. '*/div',
  2722. array('div' => array('class' => 'input date')),
  2723. '*/div',
  2724. array('div' => array('class' => 'input datetime')),
  2725. '*/div',
  2726. array('div' => array('class' => 'input number')),
  2727. '*/div',
  2728. array('div' => array('class' => 'input select')),
  2729. '*/div',
  2730. );
  2731. $this->assertTags($result, $expected);
  2732. $this->Form->create('Contact');
  2733. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2734. $this->assertTags($result, $expected);
  2735. $this->Form->create('Contact');
  2736. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2737. $expected = array(
  2738. 'fieldset' => array(),
  2739. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2740. array('div' => array('class' => 'input text')),
  2741. '*/div',
  2742. array('div' => array('class' => 'input email')),
  2743. '*/div',
  2744. array('div' => array('class' => 'input tel')),
  2745. '*/div',
  2746. array('div' => array('class' => 'input password')),
  2747. '*/div',
  2748. array('div' => array('class' => 'input date')),
  2749. '*/div',
  2750. array('div' => array('class' => 'input date')),
  2751. '*/div',
  2752. array('div' => array('class' => 'input datetime')),
  2753. '*/div',
  2754. array('div' => array('class' => 'input number')),
  2755. '*/div',
  2756. array('div' => array('class' => 'input select')),
  2757. '*/div',
  2758. '/fieldset'
  2759. );
  2760. $this->assertTags($result, $expected);
  2761. $this->Form->create('Contact');
  2762. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2763. $expected = array(
  2764. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2765. array('div' => array('class' => 'input text')),
  2766. '*/div',
  2767. array('div' => array('class' => 'input email')),
  2768. '*/div',
  2769. array('div' => array('class' => 'input tel')),
  2770. '*/div',
  2771. array('div' => array('class' => 'input password')),
  2772. '*/div',
  2773. array('div' => array('class' => 'input date')),
  2774. '*/div',
  2775. array('div' => array('class' => 'input date')),
  2776. '*/div',
  2777. array('div' => array('class' => 'input datetime')),
  2778. '*/div',
  2779. array('div' => array('class' => 'input number')),
  2780. '*/div',
  2781. array('div' => array('class' => 'input select')),
  2782. '*/div',
  2783. );
  2784. $this->assertTags($result, $expected);
  2785. $this->Form->create('Contact');
  2786. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2787. $this->assertTags($result, $expected);
  2788. $this->Form->create('Contact');
  2789. $result = $this->Form->inputs('Hello');
  2790. $expected = array(
  2791. 'fieldset' => array(),
  2792. 'legend' => array(),
  2793. 'Hello',
  2794. '/legend',
  2795. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2796. array('div' => array('class' => 'input text')),
  2797. '*/div',
  2798. array('div' => array('class' => 'input email')),
  2799. '*/div',
  2800. array('div' => array('class' => 'input tel')),
  2801. '*/div',
  2802. array('div' => array('class' => 'input password')),
  2803. '*/div',
  2804. array('div' => array('class' => 'input date')),
  2805. '*/div',
  2806. array('div' => array('class' => 'input date')),
  2807. '*/div',
  2808. array('div' => array('class' => 'input datetime')),
  2809. '*/div',
  2810. array('div' => array('class' => 'input number')),
  2811. '*/div',
  2812. array('div' => array('class' => 'input select')),
  2813. '*/div',
  2814. '/fieldset'
  2815. );
  2816. $this->assertTags($result, $expected);
  2817. $this->Form->create('Contact');
  2818. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2819. $expected = array(
  2820. 'fieldset' => array(),
  2821. 'legend' => array(),
  2822. 'Hello',
  2823. '/legend',
  2824. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2825. array('div' => array('class' => 'input text')),
  2826. '*/div',
  2827. array('div' => array('class' => 'input email')),
  2828. '*/div',
  2829. array('div' => array('class' => 'input tel')),
  2830. '*/div',
  2831. array('div' => array('class' => 'input password')),
  2832. '*/div',
  2833. array('div' => array('class' => 'input date')),
  2834. '*/div',
  2835. array('div' => array('class' => 'input date')),
  2836. '*/div',
  2837. array('div' => array('class' => 'input datetime')),
  2838. '*/div',
  2839. array('div' => array('class' => 'input number')),
  2840. '*/div',
  2841. array('div' => array('class' => 'input select')),
  2842. '*/div',
  2843. '/fieldset'
  2844. );
  2845. $this->assertTags($result, $expected);
  2846. $this->Form->create('Contact');
  2847. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2848. $this->assertTags($result, $expected);
  2849. $this->Form->end();
  2850. $this->Form->create(false);
  2851. $expected = array(
  2852. 'fieldset' => array(),
  2853. array('div' => array('class' => 'input text')),
  2854. 'label' => array('for' => 'foo'),
  2855. 'Foo',
  2856. '/label',
  2857. 'input' => array('type' => 'text', 'name' => 'data[foo]', 'id' => 'foo'),
  2858. '*/div',
  2859. '/fieldset'
  2860. );
  2861. $result = $this->Form->inputs(
  2862. array('foo' => array('type' => 'text')),
  2863. array(),
  2864. array('legend' => false)
  2865. );
  2866. $this->assertTags($result, $expected);
  2867. }
  2868. /**
  2869. * Tests inputs() works with plugin models
  2870. *
  2871. * @return void
  2872. */
  2873. public function testInputsPluginModel() {
  2874. $this->loadFixtures('Post');
  2875. App::build(array(
  2876. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  2877. ));
  2878. CakePlugin::load('TestPlugin');
  2879. $this->Form->request['models'] = array(
  2880. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  2881. );
  2882. $this->Form->create('TestPlugin.TestPluginPost');
  2883. $result = $this->Form->inputs();
  2884. $this->assertContains('data[TestPluginPost][id]', $result);
  2885. $this->assertContains('data[TestPluginPost][author_id]', $result);
  2886. $this->assertContains('data[TestPluginPost][title]', $result);
  2887. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  2888. $this->assertFalse(ClassRegistry::isKeySet('TestPlugin'));
  2889. $this->assertEquals('TestPluginPost', $this->Form->model());
  2890. }
  2891. /**
  2892. * testSelectAsCheckbox method
  2893. *
  2894. * test multi-select widget with checkbox formatting.
  2895. *
  2896. * @return void
  2897. */
  2898. public function testSelectAsCheckbox() {
  2899. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2900. $expected = array(
  2901. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2902. array('div' => array('class' => 'checkbox')),
  2903. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2904. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2905. 'first',
  2906. '/label',
  2907. '/div',
  2908. array('div' => array('class' => 'checkbox')),
  2909. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2910. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2911. 'second',
  2912. '/label',
  2913. '/div',
  2914. array('div' => array('class' => 'checkbox')),
  2915. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2916. array('label' => array('for' => 'ModelMultiField2')),
  2917. 'third',
  2918. '/label',
  2919. '/div',
  2920. );
  2921. $this->assertTags($result, $expected);
  2922. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2923. $expected = array(
  2924. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2925. array('div' => array('class' => 'checkbox')),
  2926. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2927. array('label' => array('for' => 'ModelMultiField12')),
  2928. 'half',
  2929. '/label',
  2930. '/div',
  2931. );
  2932. $this->assertTags($result, $expected);
  2933. }
  2934. /**
  2935. * testLabel method
  2936. *
  2937. * test label generation.
  2938. *
  2939. * @return void
  2940. */
  2941. public function testLabel() {
  2942. $this->Form->text('Person.name');
  2943. $result = $this->Form->label();
  2944. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2945. $this->Form->text('Person.name');
  2946. $result = $this->Form->label();
  2947. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2948. $result = $this->Form->label('Person.first_name');
  2949. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2950. $result = $this->Form->label('Person.first_name', 'Your first name');
  2951. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2952. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2953. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2954. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2955. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2956. $result = $this->Form->label('Person.first_name', '');
  2957. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2958. $result = $this->Form->label('Person.2.name', '');
  2959. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2960. }
  2961. /**
  2962. * testTextbox method
  2963. *
  2964. * test textbox element generation
  2965. *
  2966. * @return void
  2967. */
  2968. public function testTextbox() {
  2969. $result = $this->Form->text('Model.field');
  2970. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2971. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2972. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2973. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2974. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2975. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2976. $result = $this->Form->text('Model.text');
  2977. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2978. $Contact = ClassRegistry::getObject('Contact');
  2979. $Contact->validationErrors['text'] = array(true);
  2980. $this->Form->request->data['Contact']['text'] = 'test';
  2981. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2982. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2983. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2984. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2985. $expected = array(
  2986. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2987. );
  2988. $this->assertTags($result, $expected);
  2989. }
  2990. /**
  2991. * testDefaultValue method
  2992. *
  2993. * Test default value setting
  2994. *
  2995. * @return void
  2996. */
  2997. public function testDefaultValue() {
  2998. $this->Form->request->data['Model']['field'] = 'test';
  2999. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3000. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  3001. unset($this->Form->request->data['Model']['field']);
  3002. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3003. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  3004. }
  3005. /**
  3006. * testCheckboxDefaultValue method
  3007. *
  3008. * Test default value setting on checkbox() method
  3009. *
  3010. * @return void
  3011. */
  3012. public function testCheckboxDefaultValue() {
  3013. $this->Form->request->data['Model']['field'] = false;
  3014. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3015. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3016. unset($this->Form->request->data['Model']['field']);
  3017. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3018. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3019. $this->Form->request->data['Model']['field'] = true;
  3020. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3021. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3022. unset($this->Form->request->data['Model']['field']);
  3023. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3024. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3025. }
  3026. /**
  3027. * testError method
  3028. *
  3029. * Test field error generation
  3030. *
  3031. * @return void
  3032. */
  3033. public function testError() {
  3034. $Contact = ClassRegistry::getObject('Contact');
  3035. $Contact->validationErrors['field'] = array(1);
  3036. $result = $this->Form->error('Contact.field');
  3037. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  3038. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3039. $this->assertEquals('Error in field Field', $result);
  3040. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3041. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3042. $this->assertEquals('This field contains invalid input', $result);
  3043. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3044. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  3045. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  3046. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  3047. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  3048. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  3049. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3050. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  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' => false));
  3053. $this->assertEquals('<strong>Badness!</strong>', $result);
  3054. $Contact->validationErrors['field'] = array("email");
  3055. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  3056. $expected = array(
  3057. 'div' => array('class' => 'field-error'),
  3058. 'No good!',
  3059. '/div'
  3060. );
  3061. $this->assertTags($result, $expected);
  3062. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  3063. $result = $this->Form->error('Contact.field', array(
  3064. 'notEmpty' => 'Cannot be empty',
  3065. 'email' => 'No good!'
  3066. ));
  3067. $expected = array(
  3068. 'div' => array('class' => 'error-message'),
  3069. 'ul' => array(),
  3070. '<li', 'Cannot be empty', '/li',
  3071. '<li', 'No good!', '/li',
  3072. '<li', 'Something else', '/li',
  3073. '/ul',
  3074. '/div'
  3075. );
  3076. $this->assertTags($result, $expected);
  3077. // Testing error messages list options
  3078. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  3079. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  3080. $expected = array(
  3081. 'div' => array('class' => 'error-message'),
  3082. 'ol' => array(),
  3083. '<li', 'notEmpty', '/li',
  3084. '<li', 'email', '/li',
  3085. '/ol',
  3086. '/div'
  3087. );
  3088. $this->assertTags($result, $expected);
  3089. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  3090. $expected = array(
  3091. 'div' => array('class' => 'error-message'),
  3092. 'ol' => array(),
  3093. '<li', 'notEmpty', '/li',
  3094. '<li', 'email', '/li',
  3095. '/ol',
  3096. '/div'
  3097. );
  3098. $this->assertTags($result, $expected);
  3099. $result = $this->Form->error('Contact.field', null, array(
  3100. 'listOptions' => array(
  3101. 'class' => 'ul-class',
  3102. 'itemOptions' => array(
  3103. 'class' => 'li-class'
  3104. )
  3105. )
  3106. ));
  3107. $expected = array(
  3108. 'div' => array('class' => 'error-message'),
  3109. 'ul' => array('class' => 'ul-class'),
  3110. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  3111. array('li' => array('class' => 'li-class')), 'email', '/li',
  3112. '/ul',
  3113. '/div'
  3114. );
  3115. $this->assertTags($result, $expected);
  3116. }
  3117. /**
  3118. * test error options when using form->input();
  3119. *
  3120. * @return void
  3121. */
  3122. public function testInputErrorEscape() {
  3123. $this->Form->create('ValidateProfile');
  3124. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  3125. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3126. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
  3127. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3128. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => false))));
  3129. $this->assertRegExp('/required<br>/', $result);
  3130. }
  3131. /**
  3132. * testPassword method
  3133. *
  3134. * Test password element generation
  3135. *
  3136. * @return void
  3137. */
  3138. public function testPassword() {
  3139. $Contact = ClassRegistry::getObject('Contact');
  3140. $result = $this->Form->password('Contact.field');
  3141. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  3142. $Contact->validationErrors['passwd'] = 1;
  3143. $this->Form->request->data['Contact']['passwd'] = 'test';
  3144. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3145. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3146. }
  3147. /**
  3148. * testRadio method
  3149. *
  3150. * Test radio element set generation
  3151. *
  3152. * @return void
  3153. */
  3154. public function testRadio() {
  3155. $result = $this->Form->radio('Model.field', array('option A'));
  3156. $expected = array(
  3157. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3158. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3159. 'label' => array('for' => 'ModelField0'),
  3160. 'option A',
  3161. '/label'
  3162. );
  3163. $this->assertTags($result, $expected);
  3164. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3165. $expected = array(
  3166. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3167. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  3168. 'label' => array('for' => 'ModelField12'),
  3169. 'half',
  3170. '/label'
  3171. );
  3172. $this->assertTags($result, $expected);
  3173. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3174. $expected = array(
  3175. 'fieldset' => array(),
  3176. 'legend' => array(),
  3177. 'Field',
  3178. '/legend',
  3179. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3180. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3181. array('label' => array('for' => 'ModelField0')),
  3182. 'option A',
  3183. '/label',
  3184. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3185. array('label' => array('for' => 'ModelField1')),
  3186. 'option B',
  3187. '/label',
  3188. '/fieldset'
  3189. );
  3190. $this->assertTags($result, $expected);
  3191. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3192. $expected = array(
  3193. 'fieldset' => array(),
  3194. 'legend' => array(),
  3195. 'Field',
  3196. '/legend',
  3197. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3198. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3199. array('label' => array('for' => 'ModelField0')),
  3200. 'option A',
  3201. '/label',
  3202. 'br' => array(),
  3203. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3204. array('label' => array('for' => 'ModelField1')),
  3205. 'option B',
  3206. '/label',
  3207. '/fieldset'
  3208. );
  3209. $this->assertTags($result, $expected);
  3210. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3211. $expected = array(
  3212. 'fieldset' => array(),
  3213. 'legend' => array(),
  3214. 'Gender',
  3215. '/legend',
  3216. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3217. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3218. array('label' => array('for' => 'EmployeeGenderMale')),
  3219. 'Male',
  3220. '/label',
  3221. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3222. array('label' => array('for' => 'EmployeeGenderFemale')),
  3223. 'Female',
  3224. '/label',
  3225. '/fieldset',
  3226. );
  3227. $this->assertTags($result, $expected);
  3228. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3229. $expected = array(
  3230. 'fieldset' => array(),
  3231. 'legend' => array(),
  3232. 'Gender',
  3233. '/legend',
  3234. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3235. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3236. array('label' => array('for' => 'OfficerGenderMale')),
  3237. 'Male',
  3238. '/label',
  3239. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3240. array('label' => array('for' => 'OfficerGenderFemale')),
  3241. 'Female',
  3242. '/label',
  3243. '/fieldset',
  3244. );
  3245. $this->assertTags($result, $expected);
  3246. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3247. $expected = array(
  3248. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3249. array('input' => array(
  3250. 'type' => 'radio',
  3251. 'name' => 'data[Contact][1][imrequired]',
  3252. 'value' => '0',
  3253. 'id' => 'Contact1Imrequired0',
  3254. 'required' => 'required'
  3255. )),
  3256. 'label' => array('for' => 'Contact1Imrequired0'),
  3257. 'option A',
  3258. '/label'
  3259. );
  3260. $this->assertTags($result, $expected);
  3261. $result = $this->Form->radio('Model.1.field', array('option A'));
  3262. $expected = array(
  3263. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3264. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3265. 'label' => array('for' => 'Model1Field0'),
  3266. 'option A',
  3267. '/label'
  3268. );
  3269. $this->assertTags($result, $expected);
  3270. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3271. $expected = array(
  3272. 'fieldset' => array(),
  3273. 'legend' => array(),
  3274. 'Field',
  3275. '/legend',
  3276. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3277. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3278. array('label' => array('for' => 'ModelField0')),
  3279. 'option A',
  3280. '/label',
  3281. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3282. array('label' => array('for' => 'ModelField1')),
  3283. 'option B',
  3284. '/label',
  3285. '/fieldset'
  3286. );
  3287. $this->assertTags($result, $expected);
  3288. }
  3289. /**
  3290. * Test radio inputs with between as string or array. Also ensure
  3291. * that an array with less between elements works.
  3292. *
  3293. * @return void
  3294. */
  3295. public function testRadioBetween() {
  3296. $result = $this->Form->radio(
  3297. 'Model.field',
  3298. array('option A', 'option B'),
  3299. array('between' => 'I am between')
  3300. );
  3301. $expected = array(
  3302. 'fieldset' => array(),
  3303. 'legend' => array(),
  3304. 'Field',
  3305. '/legend',
  3306. 'I am between',
  3307. 'input' => array(
  3308. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3309. 'value' => '', 'id' => 'ModelField_'
  3310. ),
  3311. array('input' => array(
  3312. 'type' => 'radio', 'name' => 'data[Model][field]',
  3313. 'value' => '0', 'id' => 'ModelField0'
  3314. )),
  3315. array('label' => array('for' => 'ModelField0')),
  3316. 'option A',
  3317. '/label',
  3318. array('input' => array(
  3319. 'type' => 'radio', 'name' => 'data[Model][field]',
  3320. 'value' => '1', 'id' => 'ModelField1'
  3321. )),
  3322. array('label' => array('for' => 'ModelField1')),
  3323. 'option B',
  3324. '/label',
  3325. '/fieldset'
  3326. );
  3327. $this->assertTags($result, $expected);
  3328. $result = $this->Form->radio(
  3329. 'Model.field',
  3330. array('option A', 'option B', 'option C'),
  3331. array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
  3332. );
  3333. $expected = array(
  3334. 'fieldset' => array(),
  3335. 'legend' => array(),
  3336. 'Field',
  3337. '/legend',
  3338. 'input' => array(
  3339. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3340. 'value' => '', 'id' => 'ModelField_'
  3341. ),
  3342. array('input' => array(
  3343. 'type' => 'radio', 'name' => 'data[Model][field]',
  3344. 'value' => '0', 'id' => 'ModelField0'
  3345. )),
  3346. array('label' => array('for' => 'ModelField0')),
  3347. 'option A',
  3348. '/label',
  3349. 'between A',
  3350. '--separator--',
  3351. array('input' => array(
  3352. 'type' => 'radio', 'name' => 'data[Model][field]',
  3353. 'value' => '1', 'id' => 'ModelField1'
  3354. )),
  3355. array('label' => array('for' => 'ModelField1')),
  3356. 'option B',
  3357. '/label',
  3358. 'between B',
  3359. '--separator--',
  3360. array('input' => array(
  3361. 'type' => 'radio', 'name' => 'data[Model][field]',
  3362. 'value' => '2', 'id' => 'ModelField2'
  3363. )),
  3364. array('label' => array('for' => 'ModelField2')),
  3365. 'option C',
  3366. '/label',
  3367. 'between C',
  3368. '/fieldset'
  3369. );
  3370. $this->assertTags($result, $expected);
  3371. $result = $this->Form->input('Model.field', array(
  3372. 'options' => array('1' => 'first', '2' => 'second'),
  3373. 'type' => 'radio',
  3374. 'before' => '--before--',
  3375. 'after' => '--after--',
  3376. 'separator' => '--separator--',
  3377. 'between' => array('--between first--', '--between second--')
  3378. ));
  3379. $expected = array(
  3380. 'div' => array('class' => 'input radio'),
  3381. '--before--',
  3382. 'fieldset' => array(),
  3383. 'legend' => array(),
  3384. 'Field',
  3385. '/legend',
  3386. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3387. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3388. array('label' => array('for' => 'ModelField1')),
  3389. 'first',
  3390. '/label',
  3391. '--between first--',
  3392. '--separator--',
  3393. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3394. array('label' => array('for' => 'ModelField2')),
  3395. 'second',
  3396. '/label',
  3397. '--between second--',
  3398. '/fieldset',
  3399. '--after--',
  3400. '/div'
  3401. );
  3402. $this->assertTags($result, $expected);
  3403. $result = $this->Form->input('Model.field', array(
  3404. 'options' => array('1' => 'first', '2' => 'second'),
  3405. 'type' => 'radio',
  3406. 'before' => '--before--',
  3407. 'after' => '--after--',
  3408. 'separator' => '--separator--',
  3409. 'between' => array('--between first--')
  3410. ));
  3411. $expected = array(
  3412. 'div' => array('class' => 'input radio'),
  3413. '--before--',
  3414. 'fieldset' => array(),
  3415. 'legend' => array(),
  3416. 'Field',
  3417. '/legend',
  3418. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3419. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3420. array('label' => array('for' => 'ModelField1')),
  3421. 'first',
  3422. '/label',
  3423. '--between first--',
  3424. '--separator--',
  3425. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3426. array('label' => array('for' => 'ModelField2')),
  3427. 'second',
  3428. '/label',
  3429. '/fieldset',
  3430. '--after--',
  3431. '/div'
  3432. );
  3433. $this->assertTags($result, $expected);
  3434. }
  3435. /**
  3436. * Test that radios with a 0 value are selected under the correct conditions.
  3437. * Also ensure that values that are booleanish are handled correctly.
  3438. *
  3439. * @return void
  3440. */
  3441. public function testRadioOptionWithBooleanishValues() {
  3442. $expected = array(
  3443. 'fieldset' => array(),
  3444. 'legend' => array(),
  3445. 'Field',
  3446. '/legend',
  3447. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3448. array('label' => array('for' => 'ModelField1')),
  3449. 'Yes',
  3450. '/label',
  3451. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3452. array('label' => array('for' => 'ModelField0')),
  3453. 'No',
  3454. '/label',
  3455. '/fieldset'
  3456. );
  3457. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3458. $this->assertTags($result, $expected);
  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' => false));
  3462. $this->assertTags($result, $expected);
  3463. $expected = array(
  3464. 'fieldset' => array(),
  3465. 'legend' => array(),
  3466. 'Field',
  3467. '/legend',
  3468. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3469. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3470. array('label' => array('for' => 'ModelField1')),
  3471. 'Yes',
  3472. '/label',
  3473. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3474. array('label' => array('for' => 'ModelField0')),
  3475. 'No',
  3476. '/label',
  3477. '/fieldset'
  3478. );
  3479. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3480. $this->assertTags($result, $expected);
  3481. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3482. $this->assertTags($result, $expected);
  3483. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3484. $this->assertTags($result, $expected);
  3485. $expected = array(
  3486. 'fieldset' => array(),
  3487. 'legend' => array(),
  3488. 'Field',
  3489. '/legend',
  3490. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3491. array('label' => array('for' => 'ModelField1')),
  3492. 'Yes',
  3493. '/label',
  3494. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3495. array('label' => array('for' => 'ModelField0')),
  3496. 'No',
  3497. '/label',
  3498. '/fieldset'
  3499. );
  3500. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3501. $this->assertTags($result, $expected);
  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' => true));
  3505. $this->assertTags($result, $expected);
  3506. }
  3507. /**
  3508. * test disabled radio options
  3509. *
  3510. * @return void
  3511. */
  3512. public function testRadioDisabled() {
  3513. $result = $this->Form->radio(
  3514. 'Model.field',
  3515. array('option A', 'option B'),
  3516. array('disabled' => array(0), 'value' => '0')
  3517. );
  3518. $expected = array(
  3519. 'fieldset' => array(),
  3520. 'legend' => array(),
  3521. 'Field',
  3522. '/legend',
  3523. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3524. array('label' => array('for' => 'ModelField0')),
  3525. 'option A',
  3526. '/label',
  3527. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3528. array('label' => array('for' => 'ModelField1')),
  3529. 'option B',
  3530. '/label',
  3531. '/fieldset'
  3532. );
  3533. $this->assertTags($result, $expected);
  3534. $result = $this->Form->radio(
  3535. 'Model.field',
  3536. array('option A', 'option B'),
  3537. array('disabled' => true, 'value' => '0')
  3538. );
  3539. $expected = array(
  3540. 'fieldset' => array(),
  3541. 'legend' => array(),
  3542. 'Field',
  3543. '/legend',
  3544. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3545. array('label' => array('for' => 'ModelField0')),
  3546. 'option A',
  3547. '/label',
  3548. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3549. array('label' => array('for' => 'ModelField1')),
  3550. 'option B',
  3551. '/label',
  3552. '/fieldset'
  3553. );
  3554. $this->assertTags($result, $expected);
  3555. $result = $this->Form->radio(
  3556. 'Model.field',
  3557. array('option A', 'option B'),
  3558. array('disabled' => 'disabled', 'value' => '0')
  3559. );
  3560. $expected = array(
  3561. 'fieldset' => array(),
  3562. 'legend' => array(),
  3563. 'Field',
  3564. '/legend',
  3565. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3566. array('label' => array('for' => 'ModelField0')),
  3567. 'option A',
  3568. '/label',
  3569. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3570. array('label' => array('for' => 'ModelField1')),
  3571. 'option B',
  3572. '/label',
  3573. '/fieldset'
  3574. );
  3575. $this->assertTags($result, $expected);
  3576. $result = $this->Form->input('Model.field', array(
  3577. 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
  3578. 'type' => 'radio',
  3579. 'disabled' => array(2, '3x'),
  3580. ));
  3581. $expected = array(
  3582. 'div' => array('class' => 'input radio'),
  3583. 'fieldset' => array(),
  3584. 'legend' => array(),
  3585. 'Field',
  3586. '/legend',
  3587. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3588. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3589. array('label' => array('for' => 'ModelField1')),
  3590. 'first',
  3591. '/label',
  3592. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
  3593. array('label' => array('for' => 'ModelField2')),
  3594. 'second',
  3595. '/label',
  3596. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2x', 'id' => 'ModelField2x')),
  3597. array('label' => array('for' => 'ModelField2x')),
  3598. '2x',
  3599. '/label',
  3600. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '3', 'id' => 'ModelField3')),
  3601. array('label' => array('for' => 'ModelField3')),
  3602. 'third',
  3603. '/label',
  3604. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
  3605. array('label' => array('for' => 'ModelField3x')),
  3606. '3x',
  3607. '/label',
  3608. '/fieldset',
  3609. '/div'
  3610. );
  3611. $this->assertTags($result, $expected);
  3612. }
  3613. /**
  3614. * test disabling the hidden input for radio buttons
  3615. *
  3616. * @return void
  3617. */
  3618. public function testRadioHiddenInputDisabling() {
  3619. $result = $this->Form->input('Model.1.field', array(
  3620. 'type' => 'radio',
  3621. 'options' => array('option A'),
  3622. 'hiddenField' => false
  3623. )
  3624. );
  3625. $expected = array(
  3626. 'div' => array('class' => 'input radio'),
  3627. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3628. 'label' => array('for' => 'Model1Field0'),
  3629. 'option A',
  3630. '/label',
  3631. '/div'
  3632. );
  3633. $this->assertTags($result, $expected);
  3634. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3635. $expected = array(
  3636. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3637. 'label' => array('for' => 'Model1Field0'),
  3638. 'option A',
  3639. '/label'
  3640. );
  3641. $this->assertTags($result, $expected);
  3642. }
  3643. /**
  3644. * test adding an empty option for radio buttons
  3645. *
  3646. * @return void
  3647. */
  3648. public function testRadioAddEmptyOption() {
  3649. $result = $this->Form->input('Model.1.field', array(
  3650. 'type' => 'radio',
  3651. 'options' => array('option A'),
  3652. 'empty' => true,
  3653. 'hiddenField' => false
  3654. ));
  3655. $expected = array(
  3656. 'div' => array('class' => 'input radio'),
  3657. 'fieldset' => array(),
  3658. 'legend' => array(),
  3659. 'Field',
  3660. '/legend',
  3661. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3662. array('label' => array('for' => 'Model1Field')),
  3663. __('empty'),
  3664. '/label',
  3665. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3666. array('label' => array('for' => 'Model1Field0')),
  3667. 'option A',
  3668. '/label',
  3669. '/fieldset',
  3670. '/div'
  3671. );
  3672. $this->assertTags($result, $expected);
  3673. $result = $this->Form->input('Model.1.field', array(
  3674. 'type' => 'radio',
  3675. 'options' => array('option A'),
  3676. 'empty' => 'CustomEmptyLabel',
  3677. 'hiddenField' => false
  3678. ));
  3679. $expected = array(
  3680. 'div' => array('class' => 'input radio'),
  3681. 'fieldset' => array(),
  3682. 'legend' => array(),
  3683. 'Field',
  3684. '/legend',
  3685. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3686. array('label' => array('for' => 'Model1Field')),
  3687. 'CustomEmptyLabel',
  3688. '/label',
  3689. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3690. array('label' => array('for' => 'Model1Field0')),
  3691. 'option A',
  3692. '/label',
  3693. '/fieldset',
  3694. '/div'
  3695. );
  3696. $this->assertTags($result, $expected);
  3697. $result = $this->Form->input('Model.1.field', array(
  3698. 'type' => 'radio',
  3699. 'options' => array('option A'),
  3700. 'empty' => false,
  3701. 'hiddenField' => false
  3702. ));
  3703. $this->assertTextNotContains('"Model1Field"', $result);
  3704. }
  3705. /**
  3706. * testSelect method
  3707. *
  3708. * Test select element generation.
  3709. *
  3710. * @return void
  3711. */
  3712. public function testSelect() {
  3713. $result = $this->Form->select('Model.field', array());
  3714. $expected = array(
  3715. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3716. array('option' => array('value' => '')),
  3717. '/option',
  3718. '/select'
  3719. );
  3720. $this->assertTags($result, $expected);
  3721. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3722. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3723. $expected = array(
  3724. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3725. array('option' => array('value' => '')),
  3726. '/option',
  3727. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3728. 'good',
  3729. '/option',
  3730. array('option' => array('value' => 'other')),
  3731. 'bad',
  3732. '/option',
  3733. '/select'
  3734. );
  3735. $this->assertTags($result, $expected);
  3736. $this->Form->request->data = array();
  3737. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3738. $expected = array(
  3739. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3740. array('option' => array('value' => '')),
  3741. '/option',
  3742. array('option' => array('value' => 'value')),
  3743. 'good',
  3744. '/option',
  3745. array('option' => array('value' => 'other')),
  3746. 'bad',
  3747. '/option',
  3748. '/select'
  3749. );
  3750. $this->assertTags($result, $expected);
  3751. $result = $this->Form->select(
  3752. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3753. array('empty' => false)
  3754. );
  3755. $expected = array(
  3756. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3757. array('option' => array('value' => 'first')),
  3758. 'first &quot;html&quot; &lt;chars&gt;',
  3759. '/option',
  3760. array('option' => array('value' => 'second')),
  3761. 'value',
  3762. '/option',
  3763. '/select'
  3764. );
  3765. $this->assertTags($result, $expected);
  3766. $result = $this->Form->select(
  3767. 'Model.field',
  3768. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3769. array('escape' => false, 'empty' => false)
  3770. );
  3771. $expected = array(
  3772. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3773. array('option' => array('value' => 'first')),
  3774. 'first "html" <chars>',
  3775. '/option',
  3776. array('option' => array('value' => 'second')),
  3777. 'value',
  3778. '/option',
  3779. '/select'
  3780. );
  3781. $this->assertTags($result, $expected);
  3782. $options = array(
  3783. array('value' => 'first', 'name' => 'First'),
  3784. array('value' => 'first', 'name' => 'Another First'),
  3785. );
  3786. $result = $this->Form->select(
  3787. 'Model.field',
  3788. $options,
  3789. array('escape' => false, 'empty' => false)
  3790. );
  3791. $expected = array(
  3792. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3793. array('option' => array('value' => 'first')),
  3794. 'First',
  3795. '/option',
  3796. array('option' => array('value' => 'first')),
  3797. 'Another First',
  3798. '/option',
  3799. '/select'
  3800. );
  3801. $this->assertTags($result, $expected);
  3802. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3803. $result = $this->Form->select(
  3804. 'Model.contact_id',
  3805. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3806. array('escape' => false, 'empty' => 'pick something')
  3807. );
  3808. $expected = array(
  3809. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3810. array('option' => array('value' => '')), 'pick something', '/option',
  3811. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3812. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3813. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3814. '/select'
  3815. );
  3816. $this->assertTags($result, $expected);
  3817. $this->Form->request->data['Model']['field'] = 0;
  3818. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3819. $expected = array(
  3820. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3821. array('option' => array('value' => '')), '/option',
  3822. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3823. array('option' => array('value' => '1')), 'Yes', '/option',
  3824. '/select'
  3825. );
  3826. $this->assertTags($result, $expected);
  3827. $this->Form->request->data['Model']['field'] = 50;
  3828. $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
  3829. $expected = array(
  3830. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3831. array('option' => array('value' => '')), '/option',
  3832. array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
  3833. array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
  3834. '/select'
  3835. );
  3836. $this->assertTags($result, $expected);
  3837. $result = $this->Form->select('Contact.required_one', array('option A'));
  3838. $expected = array(
  3839. 'select' => array(
  3840. 'name' => 'data[Contact][required_one]',
  3841. 'id' => 'ContactRequiredOne',
  3842. 'required' => 'required'
  3843. ),
  3844. array('option' => array('value' => '')), '/option',
  3845. array('option' => array('value' => '0')), 'option A', '/option',
  3846. '/select'
  3847. );
  3848. $this->assertTags($result, $expected);
  3849. $result = $this->Form->select('Contact.required_one', array('option A'), array('disabled' => true));
  3850. $expected = array(
  3851. 'select' => array(
  3852. 'name' => 'data[Contact][required_one]',
  3853. 'id' => 'ContactRequiredOne',
  3854. 'disabled' => 'disabled'
  3855. ),
  3856. array('option' => array('value' => '', 'disabled' => 'disabled')), '/option',
  3857. array('option' => array('value' => '0', 'disabled' => 'disabled')), 'option A', '/option',
  3858. '/select'
  3859. );
  3860. $this->assertTags($result, $expected);
  3861. }
  3862. /**
  3863. * test that select() with optiongroups listens to the escape param.
  3864. *
  3865. * @return void
  3866. */
  3867. public function testSelectOptionGroupEscaping() {
  3868. $options = array(
  3869. '>< Key' => array(
  3870. 1 => 'One',
  3871. 2 => 'Two'
  3872. )
  3873. );
  3874. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3875. $expected = array(
  3876. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3877. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3878. array('option' => array('value' => '1')), 'One', '/option',
  3879. array('option' => array('value' => '2')), 'Two', '/option',
  3880. '/optgroup',
  3881. '/select'
  3882. );
  3883. $this->assertTags($result, $expected);
  3884. $options = array(
  3885. '>< Key' => array(
  3886. 1 => 'One',
  3887. 2 => 'Two'
  3888. )
  3889. );
  3890. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3891. $expected = array(
  3892. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3893. 'optgroup' => array('label' => '>< Key'),
  3894. array('option' => array('value' => '1')), 'One', '/option',
  3895. array('option' => array('value' => '2')), 'Two', '/option',
  3896. '/optgroup',
  3897. '/select'
  3898. );
  3899. $this->assertTags($result, $expected);
  3900. }
  3901. /**
  3902. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3903. *
  3904. * @return void
  3905. */
  3906. public function testSelectWithNullAttributes() {
  3907. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3908. $expected = array(
  3909. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3910. array('option' => array('value' => '0')),
  3911. 'first',
  3912. '/option',
  3913. array('option' => array('value' => '1')),
  3914. 'second',
  3915. '/option',
  3916. '/select'
  3917. );
  3918. $this->assertTags($result, $expected);
  3919. }
  3920. /**
  3921. * testNestedSelect method
  3922. *
  3923. * test select element generation with optgroups
  3924. *
  3925. * @return void
  3926. */
  3927. public function testNestedSelect() {
  3928. $result = $this->Form->select(
  3929. 'Model.field',
  3930. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3931. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3932. )), array('empty' => false)
  3933. );
  3934. $expected = array(
  3935. 'select' => array('name' => 'data[Model][field]',
  3936. 'id' => 'ModelField'),
  3937. array('option' => array('value' => 1)),
  3938. 'One',
  3939. '/option',
  3940. array('option' => array('value' => 2)),
  3941. 'Two',
  3942. '/option',
  3943. array('optgroup' => array('label' => 'Three')),
  3944. array('option' => array('value' => 4)),
  3945. 'Four',
  3946. '/option',
  3947. array('option' => array('value' => 5)),
  3948. 'Five',
  3949. '/option',
  3950. '/optgroup',
  3951. '/select'
  3952. );
  3953. $this->assertTags($result, $expected);
  3954. $result = $this->Form->select(
  3955. 'Model.field',
  3956. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3957. array('showParents' => true, 'empty' => false)
  3958. );
  3959. $expected = array(
  3960. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3961. array('option' => array('value' => 1)),
  3962. 'One',
  3963. '/option',
  3964. array('option' => array('value' => 2)),
  3965. 'Two',
  3966. '/option',
  3967. array('optgroup' => array('label' => 'Three')),
  3968. array('option' => array('value' => 3)),
  3969. 'Three',
  3970. '/option',
  3971. array('option' => array('value' => 4)),
  3972. 'Four',
  3973. '/option',
  3974. '/optgroup',
  3975. '/select'
  3976. );
  3977. $this->assertTags($result, $expected);
  3978. }
  3979. /**
  3980. * testSelectMultiple method
  3981. *
  3982. * test generation of multiple select elements
  3983. *
  3984. * @return void
  3985. */
  3986. public function testSelectMultiple() {
  3987. $options = array('first', 'second', 'third');
  3988. $result = $this->Form->select(
  3989. 'Model.multi_field', $options, array('multiple' => true)
  3990. );
  3991. $expected = array(
  3992. 'input' => array(
  3993. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3994. ),
  3995. 'select' => array(
  3996. 'name' => 'data[Model][multi_field][]',
  3997. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3998. ),
  3999. array('option' => array('value' => '0')),
  4000. 'first',
  4001. '/option',
  4002. array('option' => array('value' => '1')),
  4003. 'second',
  4004. '/option',
  4005. array('option' => array('value' => '2')),
  4006. 'third',
  4007. '/option',
  4008. '/select'
  4009. );
  4010. $this->assertTags($result, $expected);
  4011. $result = $this->Form->select(
  4012. 'Model.multi_field', $options, array('multiple' => 'multiple')
  4013. );
  4014. $expected = array(
  4015. 'input' => array(
  4016. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4017. ),
  4018. 'select' => array(
  4019. 'name' => 'data[Model][multi_field][]',
  4020. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4021. ),
  4022. array('option' => array('value' => '0')),
  4023. 'first',
  4024. '/option',
  4025. array('option' => array('value' => '1')),
  4026. 'second',
  4027. '/option',
  4028. array('option' => array('value' => '2')),
  4029. 'third',
  4030. '/option',
  4031. '/select'
  4032. );
  4033. $this->assertTags($result, $expected);
  4034. $result = $this->Form->select(
  4035. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  4036. );
  4037. $expected = array(
  4038. 'input' => array(
  4039. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4040. ),
  4041. 'select' => array(
  4042. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  4043. 'multiple' => 'multiple'
  4044. ),
  4045. array('option' => array('value' => '0', 'selected' => 'selected')),
  4046. 'first',
  4047. '/option',
  4048. array('option' => array('value' => '1', 'selected' => 'selected')),
  4049. 'second',
  4050. '/option',
  4051. array('option' => array('value' => '2')),
  4052. 'third',
  4053. '/option',
  4054. '/select'
  4055. );
  4056. $this->assertTags($result, $expected);
  4057. $result = $this->Form->select(
  4058. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  4059. );
  4060. $expected = array(
  4061. 'select' => array(
  4062. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  4063. ),
  4064. array('option' => array('value' => '0', 'selected' => 'selected')),
  4065. 'first',
  4066. '/option',
  4067. array('option' => array('value' => '1', 'selected' => 'selected')),
  4068. 'second',
  4069. '/option',
  4070. array('option' => array('value' => '2')),
  4071. 'third',
  4072. '/option',
  4073. '/select'
  4074. );
  4075. $this->assertTags($result, $expected);
  4076. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4077. $selected = array('2', '3x');
  4078. $result = $this->Form->select(
  4079. 'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
  4080. );
  4081. $expected = array(
  4082. 'input' => array(
  4083. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4084. ),
  4085. 'select' => array(
  4086. 'name' => 'data[Model][multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4087. ),
  4088. array('option' => array('value' => '1')),
  4089. 'One',
  4090. '/option',
  4091. array('option' => array('value' => '2', 'selected' => 'selected')),
  4092. 'Two',
  4093. '/option',
  4094. array('option' => array('value' => '3')),
  4095. 'Three',
  4096. '/option',
  4097. array('option' => array('value' => '3x', 'selected' => 'selected')),
  4098. 'Stringy',
  4099. '/option',
  4100. '/select'
  4101. );
  4102. $this->assertTags($result, $expected);
  4103. $result = $this->Form->select('Contact.required_one', array(
  4104. '1' => 'option A',
  4105. '2' => 'option B'
  4106. ), array('multiple' => true));
  4107. $expected = array(
  4108. 'input' => array(
  4109. 'type' => 'hidden', 'name' => 'data[Contact][required_one]', 'value' => '', 'id' => 'ContactRequiredOne_'
  4110. ),
  4111. 'select' => array(
  4112. 'name' => 'data[Contact][required_one][]',
  4113. 'id' => 'ContactRequiredOne',
  4114. 'required' => 'required',
  4115. 'multiple' => 'multiple'
  4116. ),
  4117. array('option' => array('value' => '1')), 'option A', '/option',
  4118. array('option' => array('value' => '2')), 'option B', '/option',
  4119. '/select'
  4120. );
  4121. $this->assertTags($result, $expected);
  4122. }
  4123. /**
  4124. * Test generating multiple select with disabled elements.
  4125. *
  4126. * @return void
  4127. */
  4128. public function testSelectMultipleWithDisabledElements() {
  4129. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4130. $disabled = array(2, 3);
  4131. $result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
  4132. $expected = array(
  4133. 'input' => array(
  4134. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4135. ),
  4136. 'select' => array(
  4137. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4138. ),
  4139. array('option' => array('value' => '1')),
  4140. 'One',
  4141. '/option',
  4142. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4143. 'Two',
  4144. '/option',
  4145. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4146. 'Three',
  4147. '/option',
  4148. array('option' => array('value' => '3x')),
  4149. 'Stringy',
  4150. '/option',
  4151. '/select'
  4152. );
  4153. $this->assertTags($result, $expected);
  4154. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4155. $disabled = array('2', '3x');
  4156. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4157. $expected = array(
  4158. array('div' => array('class' => 'input select')),
  4159. array('label' => array('for' => 'ContactMultiple')),
  4160. 'Multiple',
  4161. '/label',
  4162. 'input' => array(
  4163. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4164. ),
  4165. 'select' => array(
  4166. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4167. ),
  4168. array('option' => array('value' => '1')),
  4169. 'One',
  4170. '/option',
  4171. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4172. 'Two',
  4173. '/option',
  4174. array('option' => array('value' => '3')),
  4175. 'Three',
  4176. '/option',
  4177. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4178. 'Stringy',
  4179. '/option',
  4180. '/select',
  4181. '/div'
  4182. );
  4183. $this->assertTags($result, $expected);
  4184. }
  4185. /**
  4186. * test generation of habtm select boxes.
  4187. *
  4188. * @return void
  4189. */
  4190. public function testHabtmSelectBox() {
  4191. $this->View->viewVars['contactTags'] = array(
  4192. 1 => 'blue',
  4193. 2 => 'red',
  4194. 3 => 'green'
  4195. );
  4196. $this->Form->request->data = array(
  4197. 'Contact' => array(),
  4198. 'ContactTag' => array(
  4199. array(
  4200. 'id' => '1',
  4201. 'name' => 'blue'
  4202. ),
  4203. array(
  4204. 'id' => 3,
  4205. 'name' => 'green'
  4206. )
  4207. )
  4208. );
  4209. $this->Form->create('Contact');
  4210. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4211. $expected = array(
  4212. 'input' => array(
  4213. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4214. ),
  4215. 'select' => array(
  4216. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4217. 'multiple' => 'multiple'
  4218. ),
  4219. array('option' => array('value' => '1', 'selected' => 'selected')),
  4220. 'blue',
  4221. '/option',
  4222. array('option' => array('value' => '2')),
  4223. 'red',
  4224. '/option',
  4225. array('option' => array('value' => '3', 'selected' => 'selected')),
  4226. 'green',
  4227. '/option',
  4228. '/select'
  4229. );
  4230. $this->assertTags($result, $expected);
  4231. // make sure only 50 is selected, and not 50f5c0cf
  4232. $this->View->viewVars['contactTags'] = array(
  4233. '1' => 'blue',
  4234. '50f5c0cf' => 'red',
  4235. '50' => 'green'
  4236. );
  4237. $this->Form->request->data = array(
  4238. 'Contact' => array(),
  4239. 'ContactTag' => array(
  4240. array(
  4241. 'id' => 1,
  4242. 'name' => 'blue'
  4243. ),
  4244. array(
  4245. 'id' => 50,
  4246. 'name' => 'green'
  4247. )
  4248. )
  4249. );
  4250. $this->Form->create('Contact');
  4251. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4252. $expected = array(
  4253. 'input' => array(
  4254. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4255. ),
  4256. 'select' => array(
  4257. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4258. 'multiple' => 'multiple'
  4259. ),
  4260. array('option' => array('value' => '1', 'selected' => 'selected')),
  4261. 'blue',
  4262. '/option',
  4263. array('option' => array('value' => '50f5c0cf')),
  4264. 'red',
  4265. '/option',
  4266. array('option' => array('value' => '50', 'selected' => 'selected')),
  4267. 'green',
  4268. '/option',
  4269. '/select'
  4270. );
  4271. $this->assertTags($result, $expected);
  4272. }
  4273. /**
  4274. * test generation of multi select elements in checkbox format
  4275. *
  4276. * @return void
  4277. */
  4278. public function testSelectMultipleCheckboxes() {
  4279. $result = $this->Form->select(
  4280. 'Model.multi_field',
  4281. array('first', 'second', 'third'),
  4282. array('multiple' => 'checkbox')
  4283. );
  4284. $expected = array(
  4285. 'input' => array(
  4286. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4287. ),
  4288. array('div' => array('class' => 'checkbox')),
  4289. array('input' => array(
  4290. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4291. 'value' => '0', 'id' => 'ModelMultiField0'
  4292. )),
  4293. array('label' => array('for' => 'ModelMultiField0')),
  4294. 'first',
  4295. '/label',
  4296. '/div',
  4297. array('div' => array('class' => 'checkbox')),
  4298. array('input' => array(
  4299. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4300. 'value' => '1', 'id' => 'ModelMultiField1'
  4301. )),
  4302. array('label' => array('for' => 'ModelMultiField1')),
  4303. 'second',
  4304. '/label',
  4305. '/div',
  4306. array('div' => array('class' => 'checkbox')),
  4307. array('input' => array(
  4308. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4309. 'value' => '2', 'id' => 'ModelMultiField2'
  4310. )),
  4311. array('label' => array('for' => 'ModelMultiField2')),
  4312. 'third',
  4313. '/label',
  4314. '/div'
  4315. );
  4316. $this->assertTags($result, $expected);
  4317. $result = $this->Form->select(
  4318. 'Model.multi_field',
  4319. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4320. array('multiple' => 'checkbox')
  4321. );
  4322. $expected = array(
  4323. 'input' => array(
  4324. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4325. ),
  4326. array('div' => array('class' => 'checkbox')),
  4327. array('input' => array(
  4328. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4329. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  4330. )),
  4331. array('label' => array('for' => 'ModelMultiFieldA')),
  4332. 'first',
  4333. '/label',
  4334. '/div',
  4335. array('div' => array('class' => 'checkbox')),
  4336. array('input' => array(
  4337. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4338. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  4339. )),
  4340. array('label' => array('for' => 'ModelMultiFieldB')),
  4341. 'second',
  4342. '/label',
  4343. '/div',
  4344. array('div' => array('class' => 'checkbox')),
  4345. array('input' => array(
  4346. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4347. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  4348. )),
  4349. array('label' => array('for' => 'ModelMultiFieldC')),
  4350. 'third',
  4351. '/label',
  4352. '/div'
  4353. );
  4354. $this->assertTags($result, $expected);
  4355. $result = $this->Form->select(
  4356. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  4357. );
  4358. $expected = array(
  4359. 'input' => array(
  4360. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4361. ),
  4362. array('div' => array('class' => 'checkbox')),
  4363. array('input' => array(
  4364. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4365. 'value' => '1', 'id' => 'ModelMultiField1'
  4366. )),
  4367. array('label' => array('for' => 'ModelMultiField1')),
  4368. 'first',
  4369. '/label',
  4370. '/div'
  4371. );
  4372. $this->assertTags($result, $expected);
  4373. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4374. $result = $this->Form->select(
  4375. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4376. );
  4377. $expected = array(
  4378. 'input' => array(
  4379. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4380. ),
  4381. array('div' => array('class' => 'checkbox')),
  4382. array('input' => array(
  4383. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4384. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  4385. )),
  4386. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  4387. 'first',
  4388. '/label',
  4389. '/div',
  4390. array('div' => array('class' => 'checkbox')),
  4391. array('input' => array(
  4392. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4393. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4394. )),
  4395. array('label' => array('for' => 'ModelTagsArray')),
  4396. 'Array',
  4397. '/label',
  4398. '/div'
  4399. );
  4400. $this->assertTags($result, $expected);
  4401. }
  4402. /**
  4403. * test multiple checkboxes with div styles.
  4404. *
  4405. * @return void
  4406. */
  4407. public function testSelectMultipleCheckboxDiv() {
  4408. $result = $this->Form->select(
  4409. 'Model.tags',
  4410. array('first', 'second'),
  4411. array('multiple' => 'checkbox', 'class' => 'my-class')
  4412. );
  4413. $expected = array(
  4414. 'input' => array(
  4415. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4416. ),
  4417. array('div' => array('class' => 'my-class')),
  4418. array('input' => array(
  4419. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4420. 'value' => '0', 'id' => 'ModelTags0'
  4421. )),
  4422. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4423. '/div',
  4424. array('div' => array('class' => 'my-class')),
  4425. array('input' => array(
  4426. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4427. 'value' => '1', 'id' => 'ModelTags1'
  4428. )),
  4429. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4430. '/div'
  4431. );
  4432. $this->assertTags($result, $expected);
  4433. $result = $this->Form->input('Model.tags', array(
  4434. 'options' => array('first', 'second'),
  4435. 'multiple' => 'checkbox',
  4436. 'class' => 'my-class',
  4437. 'div' => false,
  4438. 'label' => false
  4439. ));
  4440. $this->assertTags($result, $expected);
  4441. $Contact = ClassRegistry::getObject('Contact');
  4442. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4443. $result = $this->Form->input('Contact.tags', array(
  4444. 'options' => array('one'),
  4445. 'multiple' => 'checkbox',
  4446. 'label' => false,
  4447. 'div' => false
  4448. ));
  4449. $expected = array(
  4450. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4451. array('div' => array('class' => 'checkbox form-error')),
  4452. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4453. array('label' => array('for' => 'ContactTags0')),
  4454. 'one',
  4455. '/label',
  4456. '/div'
  4457. );
  4458. $this->assertTags($result, $expected);
  4459. $result = $this->Form->input('Contact.tags', array(
  4460. 'options' => array('one'),
  4461. 'multiple' => 'checkbox',
  4462. 'class' => 'mycheckbox',
  4463. 'label' => false,
  4464. 'div' => false
  4465. ));
  4466. $expected = array(
  4467. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4468. array('div' => array('class' => 'mycheckbox form-error')),
  4469. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4470. array('label' => array('for' => 'ContactTags0')),
  4471. 'one',
  4472. '/label',
  4473. '/div'
  4474. );
  4475. $this->assertTags($result, $expected);
  4476. }
  4477. /**
  4478. * Checks the security hash array generated for multiple-input checkbox elements
  4479. *
  4480. * @return void
  4481. */
  4482. public function testSelectMultipleCheckboxSecurity() {
  4483. $this->Form->request['_Token'] = array('key' => 'testKey');
  4484. $this->assertEquals(array(), $this->Form->fields);
  4485. $result = $this->Form->select(
  4486. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4487. array('multiple' => 'checkbox')
  4488. );
  4489. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4490. $result = $this->Form->secure($this->Form->fields);
  4491. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4492. $this->assertRegExp('/"' . $key . '"/', $result);
  4493. }
  4494. /**
  4495. * Multiple select elements should always be secured as they always participate
  4496. * in the POST data.
  4497. *
  4498. * @return void
  4499. */
  4500. public function testSelectMultipleSecureWithNoOptions() {
  4501. $this->Form->request['_Token'] = array('key' => 'testkey');
  4502. $this->assertEquals(array(), $this->Form->fields);
  4503. $this->Form->select(
  4504. 'Model.select',
  4505. array(),
  4506. array('multiple' => true)
  4507. );
  4508. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4509. }
  4510. /**
  4511. * When a select box has no options it should not be added to the fields list
  4512. * as it always fail post validation.
  4513. *
  4514. * @return void
  4515. */
  4516. public function testSelectNoSecureWithNoOptions() {
  4517. $this->Form->request['_Token'] = array('key' => 'testkey');
  4518. $this->assertEquals(array(), $this->Form->fields);
  4519. $this->Form->select(
  4520. 'Model.select',
  4521. array()
  4522. );
  4523. $this->assertEquals(array(), $this->Form->fields);
  4524. $this->Form->select(
  4525. 'Model.select',
  4526. array(),
  4527. array('empty' => true)
  4528. );
  4529. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4530. }
  4531. /**
  4532. * testInputMultipleCheckboxes method
  4533. *
  4534. * test input() resulting in multi select elements being generated.
  4535. *
  4536. * @return void
  4537. */
  4538. public function testInputMultipleCheckboxes() {
  4539. $result = $this->Form->input('Model.multi_field', array(
  4540. 'options' => array('first', 'second', 'third'),
  4541. 'multiple' => 'checkbox'
  4542. ));
  4543. $expected = array(
  4544. array('div' => array('class' => 'input select')),
  4545. array('label' => array('for' => 'ModelMultiField')),
  4546. 'Multi Field',
  4547. '/label',
  4548. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4549. array('div' => array('class' => 'checkbox')),
  4550. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4551. array('label' => array('for' => 'ModelMultiField0')),
  4552. 'first',
  4553. '/label',
  4554. '/div',
  4555. array('div' => array('class' => 'checkbox')),
  4556. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4557. array('label' => array('for' => 'ModelMultiField1')),
  4558. 'second',
  4559. '/label',
  4560. '/div',
  4561. array('div' => array('class' => 'checkbox')),
  4562. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4563. array('label' => array('for' => 'ModelMultiField2')),
  4564. 'third',
  4565. '/label',
  4566. '/div',
  4567. '/div'
  4568. );
  4569. $this->assertTags($result, $expected);
  4570. $result = $this->Form->input('Model.multi_field', array(
  4571. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4572. 'multiple' => 'checkbox'
  4573. ));
  4574. $expected = array(
  4575. array('div' => array('class' => 'input select')),
  4576. array('label' => array('for' => 'ModelMultiField')),
  4577. 'Multi Field',
  4578. '/label',
  4579. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4580. array('div' => array('class' => 'checkbox')),
  4581. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4582. array('label' => array('for' => 'ModelMultiFieldA')),
  4583. 'first',
  4584. '/label',
  4585. '/div',
  4586. array('div' => array('class' => 'checkbox')),
  4587. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4588. array('label' => array('for' => 'ModelMultiFieldB')),
  4589. 'second',
  4590. '/label',
  4591. '/div',
  4592. array('div' => array('class' => 'checkbox')),
  4593. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4594. array('label' => array('for' => 'ModelMultiFieldC')),
  4595. 'third',
  4596. '/label',
  4597. '/div',
  4598. '/div'
  4599. );
  4600. $this->assertTags($result, $expected);
  4601. $result = $this->Form->input('Model.multi_field', array(
  4602. 'options' => array('1' => 'first'),
  4603. 'multiple' => 'checkbox',
  4604. 'label' => false,
  4605. 'div' => false
  4606. ));
  4607. $expected = array(
  4608. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4609. array('div' => array('class' => 'checkbox')),
  4610. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4611. array('label' => array('for' => 'ModelMultiField1')),
  4612. 'first',
  4613. '/label',
  4614. '/div'
  4615. );
  4616. $this->assertTags($result, $expected);
  4617. $result = $this->Form->input('Model.multi_field', array(
  4618. 'options' => array('2' => 'second'),
  4619. 'multiple' => 'checkbox',
  4620. 'label' => false,
  4621. 'div' => false
  4622. ));
  4623. $expected = array(
  4624. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4625. array('div' => array('class' => 'checkbox')),
  4626. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4627. array('label' => array('for' => 'ModelMultiField2')),
  4628. 'second',
  4629. '/label',
  4630. '/div'
  4631. );
  4632. $this->assertTags($result, $expected);
  4633. }
  4634. /**
  4635. * testSelectHiddenFieldOmission method
  4636. *
  4637. * test that select() with 'hiddenField' => false omits the hidden field
  4638. *
  4639. * @return void
  4640. */
  4641. public function testSelectHiddenFieldOmission() {
  4642. $result = $this->Form->select('Model.multi_field',
  4643. array('first', 'second'),
  4644. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4645. );
  4646. $expected = array(
  4647. array('div' => array('class' => 'checkbox')),
  4648. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4649. array('label' => array('for' => 'ModelMultiField0')),
  4650. 'first',
  4651. '/label',
  4652. '/div',
  4653. array('div' => array('class' => 'checkbox')),
  4654. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4655. array('label' => array('for' => 'ModelMultiField1')),
  4656. 'second',
  4657. '/label',
  4658. '/div'
  4659. );
  4660. $this->assertTags($result, $expected);
  4661. $result = $this->Form->input('Model.multi_field', array(
  4662. 'options' => array('first', 'second'),
  4663. 'multiple' => 'checkbox',
  4664. 'hiddenField' => false
  4665. ));
  4666. $expected = array(
  4667. array('div' => array('class' => 'input select')),
  4668. array('label' => array('for' => 'ModelMultiField')),
  4669. 'Multi Field',
  4670. '/label',
  4671. array('div' => array('class' => 'checkbox')),
  4672. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4673. array('label' => array('for' => 'ModelMultiField0')),
  4674. 'first',
  4675. '/label',
  4676. '/div',
  4677. array('div' => array('class' => 'checkbox')),
  4678. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4679. array('label' => array('for' => 'ModelMultiField1')),
  4680. 'second',
  4681. '/label',
  4682. '/div',
  4683. '/div'
  4684. );
  4685. $this->assertTags($result, $expected);
  4686. }
  4687. /**
  4688. * test that select() with multiple = checkbox works with overriding name attribute.
  4689. *
  4690. * @return void
  4691. */
  4692. public function testSelectCheckboxMultipleOverrideName() {
  4693. $result = $this->Form->input('category', array(
  4694. 'type' => 'select',
  4695. 'multiple' => 'checkbox',
  4696. 'name' => 'data[fish]',
  4697. 'options' => array('1', '2'),
  4698. 'div' => false,
  4699. 'label' => false,
  4700. ));
  4701. $expected = array(
  4702. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4703. array('div' => array('class' => 'checkbox')),
  4704. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4705. array('label' => array('for' => 'Category0')), '1', '/label',
  4706. '/div',
  4707. array('div' => array('class' => 'checkbox')),
  4708. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4709. array('label' => array('for' => 'Category1')), '2', '/label',
  4710. '/div'
  4711. );
  4712. $this->assertTags($result, $expected);
  4713. }
  4714. /**
  4715. * Test that 'id' overrides all the checkbox id's as well.
  4716. *
  4717. * @return void
  4718. */
  4719. public function testSelectCheckboxMultipleId() {
  4720. $result = $this->Form->select(
  4721. 'Model.multi_field',
  4722. array('first', 'second', 'third'),
  4723. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4724. );
  4725. $expected = array(
  4726. 'input' => array(
  4727. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4728. ),
  4729. array('div' => array('class' => 'checkbox')),
  4730. array('input' => array(
  4731. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4732. 'value' => '0', 'id' => 'CustomId0'
  4733. )),
  4734. array('label' => array('for' => 'CustomId0')),
  4735. 'first',
  4736. '/label',
  4737. '/div',
  4738. array('div' => array('class' => 'checkbox')),
  4739. array('input' => array(
  4740. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4741. 'value' => '1', 'id' => 'CustomId1'
  4742. )),
  4743. array('label' => array('for' => 'CustomId1')),
  4744. 'second',
  4745. '/label',
  4746. '/div',
  4747. array('div' => array('class' => 'checkbox')),
  4748. array('input' => array(
  4749. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4750. 'value' => '2', 'id' => 'CustomId2'
  4751. )),
  4752. array('label' => array('for' => 'CustomId2')),
  4753. 'third',
  4754. '/label',
  4755. '/div'
  4756. );
  4757. $this->assertTags($result, $expected);
  4758. }
  4759. /**
  4760. * testCheckbox method
  4761. *
  4762. * Test generation of checkboxes
  4763. *
  4764. * @return void
  4765. */
  4766. public function testCheckbox() {
  4767. $result = $this->Form->checkbox('Model.field');
  4768. $expected = array(
  4769. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4770. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4771. );
  4772. $this->assertTags($result, $expected);
  4773. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4774. $expected = array(
  4775. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4776. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4777. );
  4778. $this->assertTags($result, $expected);
  4779. $Contact = ClassRegistry::getObject('Contact');
  4780. $Contact->validationErrors['field'] = 1;
  4781. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4782. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4783. $expected = array(
  4784. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4785. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4786. );
  4787. $this->assertTags($result, $expected);
  4788. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4789. $expected = array(
  4790. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4791. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4792. );
  4793. $this->assertTags($result, $expected);
  4794. $this->Form->request->data['Contact']['field'] = '';
  4795. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4796. $expected = array(
  4797. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4798. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4799. );
  4800. $this->assertTags($result, $expected);
  4801. $Contact->validationErrors = array();
  4802. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4803. $expected = array(
  4804. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4805. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4806. );
  4807. $this->assertTags($result, $expected);
  4808. $this->Form->request->data['Contact']['published'] = 1;
  4809. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4810. $expected = array(
  4811. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4812. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4813. );
  4814. $this->assertTags($result, $expected);
  4815. $this->Form->request->data['Contact']['published'] = 0;
  4816. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4817. $expected = array(
  4818. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4819. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4820. );
  4821. $this->assertTags($result, $expected);
  4822. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4823. $expected = array(
  4824. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4825. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4826. );
  4827. $this->assertTags($result, $expected);
  4828. $result = $this->Form->checkbox('CustomField.1.value');
  4829. $expected = array(
  4830. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4831. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4832. );
  4833. $this->assertTags($result, $expected);
  4834. }
  4835. /**
  4836. * test checkbox() with a custom name attribute
  4837. *
  4838. * @return void
  4839. */
  4840. public function testCheckboxCustomNameAttribute() {
  4841. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4842. $expected = array(
  4843. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4844. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4845. );
  4846. $this->assertTags($result, $expected);
  4847. }
  4848. /**
  4849. * test the checked option for checkboxes.
  4850. *
  4851. * @return void
  4852. */
  4853. public function testCheckboxCheckedOption() {
  4854. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4855. $expected = array(
  4856. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4857. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4858. );
  4859. $this->assertTags($result, $expected);
  4860. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4861. $expected = array(
  4862. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4863. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4864. );
  4865. $this->assertTags($result, $expected);
  4866. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4867. $expected = array(
  4868. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4869. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4870. );
  4871. $this->assertTags($result, $expected);
  4872. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4873. $expected = array(
  4874. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4875. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4876. );
  4877. $this->assertTags($result, $expected);
  4878. $this->Form->request->data['Model']['field'] = 1;
  4879. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4880. $expected = array(
  4881. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4882. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4883. );
  4884. $this->assertTags($result, $expected);
  4885. }
  4886. /**
  4887. * Test that disabled attribute works on both the checkbox and hidden input.
  4888. *
  4889. * @return void
  4890. */
  4891. public function testCheckboxDisabling() {
  4892. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4893. $expected = array(
  4894. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4895. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4896. );
  4897. $this->assertTags($result, $expected);
  4898. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4899. $expected = array(
  4900. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4901. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4902. );
  4903. $this->assertTags($result, $expected);
  4904. }
  4905. /**
  4906. * Test that the hidden input for checkboxes can be omitted or set to a
  4907. * specific value.
  4908. *
  4909. * @return void
  4910. */
  4911. public function testCheckboxHiddenField() {
  4912. $result = $this->Form->input('UserForm.something', array(
  4913. 'type' => 'checkbox',
  4914. 'hiddenField' => false
  4915. ));
  4916. $expected = array(
  4917. 'div' => array('class' => 'input checkbox'),
  4918. array('input' => array(
  4919. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4920. 'value' => '1', 'id' => 'UserFormSomething'
  4921. )),
  4922. 'label' => array('for' => 'UserFormSomething'),
  4923. 'Something',
  4924. '/label',
  4925. '/div'
  4926. );
  4927. $this->assertTags($result, $expected);
  4928. $result = $this->Form->input('UserForm.something', array(
  4929. 'type' => 'checkbox',
  4930. 'value' => 'Y',
  4931. 'hiddenField' => 'N',
  4932. ));
  4933. $expected = array(
  4934. 'div' => array('class' => 'input checkbox'),
  4935. array('input' => array(
  4936. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  4937. 'value' => 'N', 'id' => 'UserFormSomething_'
  4938. )),
  4939. array('input' => array(
  4940. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4941. 'value' => 'Y', 'id' => 'UserFormSomething'
  4942. )),
  4943. 'label' => array('for' => 'UserFormSomething'),
  4944. 'Something',
  4945. '/label',
  4946. '/div'
  4947. );
  4948. $this->assertTags($result, $expected);
  4949. }
  4950. /**
  4951. * testDateTime method
  4952. *
  4953. * Test generation of date/time select elements
  4954. *
  4955. * @return void
  4956. */
  4957. public function testDateTime() {
  4958. extract($this->dateRegex);
  4959. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4960. $now = strtotime('now');
  4961. $expected = array(
  4962. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4963. $daysRegex,
  4964. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4965. date('j', $now),
  4966. '/option',
  4967. '*/select',
  4968. '-',
  4969. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4970. $monthsRegex,
  4971. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4972. date('F', $now),
  4973. '/option',
  4974. '*/select',
  4975. '-',
  4976. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4977. $yearsRegex,
  4978. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4979. date('Y', $now),
  4980. '/option',
  4981. '*/select',
  4982. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4983. $hoursRegex,
  4984. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4985. date('g', $now),
  4986. '/option',
  4987. '*/select',
  4988. ':',
  4989. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4990. $minutesRegex,
  4991. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4992. date('i', $now),
  4993. '/option',
  4994. '*/select',
  4995. ' ',
  4996. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4997. $meridianRegex,
  4998. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4999. date('a', $now),
  5000. '/option',
  5001. '*/select'
  5002. );
  5003. $this->assertTags($result, $expected);
  5004. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5005. $expected = array(
  5006. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5007. $daysRegex,
  5008. array('option' => array('value' => '')),
  5009. '/option',
  5010. '*/select',
  5011. '-',
  5012. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5013. $monthsRegex,
  5014. array('option' => array('value' => '')),
  5015. '/option',
  5016. '*/select',
  5017. '-',
  5018. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5019. $yearsRegex,
  5020. array('option' => array('value' => '')),
  5021. '/option',
  5022. '*/select',
  5023. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5024. $hoursRegex,
  5025. array('option' => array('value' => '')),
  5026. '/option',
  5027. '*/select',
  5028. ':',
  5029. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5030. $minutesRegex,
  5031. array('option' => array('value' => '')),
  5032. '/option',
  5033. '*/select',
  5034. ' ',
  5035. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5036. $meridianRegex,
  5037. array('option' => array('value' => '')),
  5038. '/option',
  5039. '*/select'
  5040. );
  5041. $this->assertTags($result, $expected);
  5042. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5043. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  5044. $this->assertTags($result, $expected);
  5045. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5046. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  5047. $this->assertTags($result, $expected);
  5048. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5049. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  5050. $expected = array(
  5051. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5052. $daysRegex,
  5053. array('option' => array('value' => '')),
  5054. '/option',
  5055. '*/select',
  5056. '-',
  5057. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5058. $monthsRegex,
  5059. array('option' => array('value' => '')),
  5060. '/option',
  5061. '*/select',
  5062. '-',
  5063. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5064. $yearsRegex,
  5065. array('option' => array('value' => '')),
  5066. '/option',
  5067. '*/select',
  5068. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5069. $hoursRegex,
  5070. array('option' => array('value' => '')),
  5071. '/option',
  5072. '*/select',
  5073. ':',
  5074. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5075. $minutesRegex,
  5076. array('option' => array('value' => '')),
  5077. '/option',
  5078. array('option' => array('value' => '00')),
  5079. '00',
  5080. '/option',
  5081. array('option' => array('value' => '05')),
  5082. '05',
  5083. '/option',
  5084. array('option' => array('value' => '10')),
  5085. '10',
  5086. '/option',
  5087. '*/select',
  5088. ' ',
  5089. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5090. $meridianRegex,
  5091. array('option' => array('value' => '')),
  5092. '/option',
  5093. '*/select'
  5094. );
  5095. $this->assertTags($result, $expected);
  5096. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5097. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5098. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5099. $this->Form->request->data['Contact']['data'] = null;
  5100. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5101. $expected = array(
  5102. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5103. $daysRegex,
  5104. array('option' => array('value' => '')),
  5105. '/option',
  5106. '*/select',
  5107. '-',
  5108. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5109. $monthsRegex,
  5110. array('option' => array('value' => '')),
  5111. '/option',
  5112. '*/select',
  5113. '-',
  5114. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5115. $yearsRegex,
  5116. array('option' => array('value' => '')),
  5117. '/option',
  5118. '*/select',
  5119. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5120. $hoursRegex,
  5121. array('option' => array('value' => '')),
  5122. '/option',
  5123. '*/select',
  5124. ':',
  5125. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5126. $minutesRegex,
  5127. array('option' => array('value' => '')),
  5128. '/option',
  5129. '*/select',
  5130. ' ',
  5131. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5132. $meridianRegex,
  5133. array('option' => array('value' => '')),
  5134. '/option',
  5135. '*/select'
  5136. );
  5137. $this->assertTags($result, $expected);
  5138. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5139. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  5140. $now = strtotime($this->Form->data['Model']['field']);
  5141. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  5142. $expected = array(
  5143. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5144. $daysRegex,
  5145. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5146. date('j', $now),
  5147. '/option',
  5148. '*/select',
  5149. '-',
  5150. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5151. $monthsRegex,
  5152. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5153. date('F', $now),
  5154. '/option',
  5155. '*/select',
  5156. '-',
  5157. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5158. $yearsRegex,
  5159. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5160. date('Y', $now),
  5161. '/option',
  5162. '*/select',
  5163. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5164. $hoursRegex,
  5165. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5166. date('g', $now),
  5167. '/option',
  5168. '*/select',
  5169. ':',
  5170. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5171. $minutesRegex,
  5172. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5173. date('i', $now),
  5174. '/option',
  5175. '*/select',
  5176. ' ',
  5177. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  5178. $meridianRegex,
  5179. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5180. date('a', $now),
  5181. '/option',
  5182. '*/select'
  5183. );
  5184. $this->assertTags($result, $expected);
  5185. $selected = strtotime('2008-10-26 12:33:00');
  5186. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  5187. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  5188. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  5189. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  5190. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  5191. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  5192. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  5193. $this->Form->create('Contact');
  5194. $result = $this->Form->input('published');
  5195. $now = strtotime('now');
  5196. $expected = array(
  5197. 'div' => array('class' => 'input date'),
  5198. 'label' => array('for' => 'ContactPublishedMonth'),
  5199. 'Published',
  5200. '/label',
  5201. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5202. $monthsRegex,
  5203. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5204. date('F', $now),
  5205. '/option',
  5206. '*/select',
  5207. '-',
  5208. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5209. $daysRegex,
  5210. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5211. date('j', $now),
  5212. '/option',
  5213. '*/select',
  5214. '-',
  5215. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5216. $yearsRegex,
  5217. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5218. date('Y', $now),
  5219. '/option',
  5220. '*/select',
  5221. '/div'
  5222. );
  5223. $this->assertTags($result, $expected);
  5224. $result = $this->Form->input('published2', array('type' => 'date'));
  5225. $now = strtotime('now');
  5226. $expected = array(
  5227. 'div' => array('class' => 'input date'),
  5228. 'label' => array('for' => 'ContactPublished2Month'),
  5229. 'Published2',
  5230. '/label',
  5231. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  5232. $monthsRegex,
  5233. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5234. date('F', $now),
  5235. '/option',
  5236. '*/select',
  5237. '-',
  5238. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  5239. $daysRegex,
  5240. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5241. date('j', $now),
  5242. '/option',
  5243. '*/select',
  5244. '-',
  5245. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  5246. $yearsRegex,
  5247. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5248. date('Y', $now),
  5249. '/option',
  5250. '*/select',
  5251. '/div'
  5252. );
  5253. $this->assertTags($result, $expected);
  5254. $this->Form->create('Contact');
  5255. $result = $this->Form->input('published', array('monthNames' => false));
  5256. $now = strtotime('now');
  5257. $expected = array(
  5258. 'div' => array('class' => 'input date'),
  5259. 'label' => array('for' => 'ContactPublishedMonth'),
  5260. 'Published',
  5261. '/label',
  5262. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5263. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5264. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5265. date('m', $now),
  5266. '/option',
  5267. '*/select',
  5268. '-',
  5269. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5270. $daysRegex,
  5271. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5272. date('j', $now),
  5273. '/option',
  5274. '*/select',
  5275. '-',
  5276. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5277. $yearsRegex,
  5278. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5279. date('Y', $now),
  5280. '/option',
  5281. '*/select',
  5282. '/div'
  5283. );
  5284. $this->assertTags($result, $expected);
  5285. $result = $this->Form->input('published', array(
  5286. 'timeFormat' => 24,
  5287. 'interval' => 5,
  5288. 'selected' => strtotime('2009-09-03 13:37:00'),
  5289. 'type' => 'datetime'
  5290. ));
  5291. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5292. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  5293. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  5294. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  5295. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  5296. }
  5297. /**
  5298. * Test that empty values don't trigger errors.
  5299. *
  5300. * @return void
  5301. */
  5302. public function testDateTimeNoErrorsOnEmptyData() {
  5303. $this->Form->request->data['Contact'] = array(
  5304. 'date' => array(
  5305. 'day' => '',
  5306. 'month' => '',
  5307. 'year' => '',
  5308. 'hour' => '',
  5309. 'min' => '',
  5310. 'meridian' => ''
  5311. )
  5312. );
  5313. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5314. $this->assertNotEmpty($result);
  5315. }
  5316. /**
  5317. * test that datetime() and default values work.
  5318. *
  5319. * @return void
  5320. */
  5321. public function testDatetimeWithDefault() {
  5322. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  5323. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5324. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5325. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5326. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  5327. 'default' => '2009-06-01 11:15:30'
  5328. ));
  5329. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5330. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5331. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5332. }
  5333. /**
  5334. * test that bogus non-date time data doesn't cause errors.
  5335. *
  5336. * @return void
  5337. */
  5338. public function testDateTimeWithBogusData() {
  5339. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  5340. $this->assertNotRegExp('/selected="selected">\d/', $result);
  5341. }
  5342. /**
  5343. * testDateTimeEmptyAsArray
  5344. *
  5345. * @return void
  5346. */
  5347. public function testDateTimeEmptyAsArray() {
  5348. $result = $this->Form->dateTime('Contact.date',
  5349. 'DMY',
  5350. '12',
  5351. array(
  5352. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  5353. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  5354. )
  5355. )
  5356. );
  5357. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5358. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5359. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5360. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5361. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5362. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5363. $result = $this->Form->dateTime('Contact.date',
  5364. 'DMY',
  5365. '12',
  5366. array(
  5367. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  5368. )
  5369. );
  5370. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5371. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5372. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5373. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  5374. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  5375. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  5376. }
  5377. /**
  5378. * testFormDateTimeMulti method
  5379. *
  5380. * test multiple datetime element generation
  5381. *
  5382. * @return void
  5383. */
  5384. public function testFormDateTimeMulti() {
  5385. extract($this->dateRegex);
  5386. $result = $this->Form->dateTime('Contact.1.updated');
  5387. $expected = array(
  5388. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  5389. $daysRegex,
  5390. array('option' => array('value' => '')),
  5391. '/option',
  5392. '*/select',
  5393. '-',
  5394. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  5395. $monthsRegex,
  5396. array('option' => array('value' => '')),
  5397. '/option',
  5398. '*/select',
  5399. '-',
  5400. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  5401. $yearsRegex,
  5402. array('option' => array('value' => '')),
  5403. '/option',
  5404. '*/select',
  5405. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  5406. $hoursRegex,
  5407. array('option' => array('value' => '')),
  5408. '/option',
  5409. '*/select',
  5410. ':',
  5411. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5412. $minutesRegex,
  5413. array('option' => array('value' => '')),
  5414. '/option',
  5415. '*/select',
  5416. ' ',
  5417. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5418. $meridianRegex,
  5419. array('option' => array('value' => '')),
  5420. '/option',
  5421. '*/select'
  5422. );
  5423. $this->assertTags($result, $expected);
  5424. $result = $this->Form->dateTime('Contact.2.updated');
  5425. $expected = array(
  5426. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5427. $daysRegex,
  5428. array('option' => array('value' => '')),
  5429. '/option',
  5430. '*/select',
  5431. '-',
  5432. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5433. $monthsRegex,
  5434. array('option' => array('value' => '')),
  5435. '/option',
  5436. '*/select',
  5437. '-',
  5438. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5439. $yearsRegex,
  5440. array('option' => array('value' => '')),
  5441. '/option',
  5442. '*/select',
  5443. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5444. $hoursRegex,
  5445. array('option' => array('value' => '')),
  5446. '/option',
  5447. '*/select',
  5448. ':',
  5449. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5450. $minutesRegex,
  5451. array('option' => array('value' => '')),
  5452. '/option',
  5453. '*/select',
  5454. ' ',
  5455. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5456. $meridianRegex,
  5457. array('option' => array('value' => '')),
  5458. '/option',
  5459. '*/select'
  5460. );
  5461. $this->assertTags($result, $expected);
  5462. }
  5463. /**
  5464. * When changing the date format, the label should always focus the first select box when
  5465. * clicked.
  5466. *
  5467. * @return void
  5468. */
  5469. public function testDateTimeLabelIdMatchesFirstInput() {
  5470. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5471. $this->assertContains('label for="ModelDateMonth"', $result);
  5472. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5473. $this->assertContains('label for="ModelDateDay"', $result);
  5474. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5475. $this->assertContains('label for="ModelDateYear"', $result);
  5476. }
  5477. /**
  5478. * testMonth method
  5479. *
  5480. * @return void
  5481. */
  5482. public function testMonth() {
  5483. $result = $this->Form->month('Model.field');
  5484. $expected = array(
  5485. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5486. array('option' => array('value' => '')),
  5487. '/option',
  5488. array('option' => array('value' => '01')),
  5489. date('F', strtotime('2008-01-01 00:00:00')),
  5490. '/option',
  5491. array('option' => array('value' => '02')),
  5492. date('F', strtotime('2008-02-01 00:00:00')),
  5493. '/option',
  5494. '*/select',
  5495. );
  5496. $this->assertTags($result, $expected);
  5497. $result = $this->Form->month('Model.field', array('empty' => true));
  5498. $expected = array(
  5499. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5500. array('option' => array('value' => '')),
  5501. '/option',
  5502. array('option' => array('value' => '01')),
  5503. date('F', strtotime('2008-01-01 00:00:00')),
  5504. '/option',
  5505. array('option' => array('value' => '02')),
  5506. date('F', strtotime('2008-02-01 00:00:00')),
  5507. '/option',
  5508. '*/select',
  5509. );
  5510. $this->assertTags($result, $expected);
  5511. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5512. $expected = array(
  5513. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5514. array('option' => array('value' => '')),
  5515. '/option',
  5516. array('option' => array('value' => '01')),
  5517. '01',
  5518. '/option',
  5519. array('option' => array('value' => '02')),
  5520. '02',
  5521. '/option',
  5522. '*/select',
  5523. );
  5524. $this->assertTags($result, $expected);
  5525. $monthNames = array(
  5526. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5527. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5528. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5529. $expected = array(
  5530. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5531. array('option' => array('value' => '')),
  5532. '/option',
  5533. array('option' => array('value' => '01')),
  5534. 'Jan',
  5535. '/option',
  5536. array('option' => array('value' => '02')),
  5537. 'Feb',
  5538. '/option',
  5539. '*/select',
  5540. );
  5541. $this->assertTags($result, $expected);
  5542. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5543. $result = $this->Form->month('Project.release');
  5544. $expected = array(
  5545. array('select' => array('name' => 'data[Project][release][month]', 'id' => 'ProjectReleaseMonth')),
  5546. array('option' => array('value' => '')),
  5547. '/option',
  5548. array('option' => array('value' => '01')),
  5549. 'January',
  5550. '/option',
  5551. array('option' => array('value' => '02', 'selected' => 'selected')),
  5552. 'February',
  5553. '/option',
  5554. '*/select',
  5555. );
  5556. $this->assertTags($result, $expected);
  5557. }
  5558. /**
  5559. * testDay method
  5560. *
  5561. * @return void
  5562. */
  5563. public function testDay() {
  5564. extract($this->dateRegex);
  5565. $result = $this->Form->day('Model.field', array('value' => false));
  5566. $expected = array(
  5567. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5568. array('option' => array('value' => '')),
  5569. '/option',
  5570. array('option' => array('value' => '01')),
  5571. '1',
  5572. '/option',
  5573. array('option' => array('value' => '02')),
  5574. '2',
  5575. '/option',
  5576. $daysRegex,
  5577. '/select',
  5578. );
  5579. $this->assertTags($result, $expected);
  5580. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5581. $result = $this->Form->day('Model.field');
  5582. $expected = array(
  5583. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5584. array('option' => array('value' => '')),
  5585. '/option',
  5586. array('option' => array('value' => '01')),
  5587. '1',
  5588. '/option',
  5589. array('option' => array('value' => '02')),
  5590. '2',
  5591. '/option',
  5592. $daysRegex,
  5593. array('option' => array('value' => '10', 'selected' => 'selected')),
  5594. '10',
  5595. '/option',
  5596. $daysRegex,
  5597. '/select',
  5598. );
  5599. $this->assertTags($result, $expected);
  5600. $this->Form->request->data['Model']['field'] = '';
  5601. $result = $this->Form->day('Model.field', array('value' => '10'));
  5602. $expected = array(
  5603. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5604. array('option' => array('value' => '')),
  5605. '/option',
  5606. array('option' => array('value' => '01')),
  5607. '1',
  5608. '/option',
  5609. array('option' => array('value' => '02')),
  5610. '2',
  5611. '/option',
  5612. $daysRegex,
  5613. array('option' => array('value' => '10', 'selected' => 'selected')),
  5614. '10',
  5615. '/option',
  5616. $daysRegex,
  5617. '/select',
  5618. );
  5619. $this->assertTags($result, $expected);
  5620. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5621. $result = $this->Form->day('Model.field', array('value' => true));
  5622. $expected = array(
  5623. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5624. array('option' => array('value' => '')),
  5625. '/option',
  5626. array('option' => array('value' => '01')),
  5627. '1',
  5628. '/option',
  5629. array('option' => array('value' => '02')),
  5630. '2',
  5631. '/option',
  5632. $daysRegex,
  5633. array('option' => array('value' => '10', 'selected' => 'selected')),
  5634. '10',
  5635. '/option',
  5636. $daysRegex,
  5637. '/select',
  5638. );
  5639. $this->assertTags($result, $expected);
  5640. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5641. $result = $this->Form->day('Project.release');
  5642. $expected = array(
  5643. array('select' => array('name' => 'data[Project][release][day]', 'id' => 'ProjectReleaseDay')),
  5644. array('option' => array('value' => '')),
  5645. '/option',
  5646. array('option' => array('value' => '01')),
  5647. '1',
  5648. '/option',
  5649. array('option' => array('value' => '02')),
  5650. '2',
  5651. '/option',
  5652. $daysRegex,
  5653. array('option' => array('value' => '10', 'selected' => 'selected')),
  5654. '10',
  5655. '/option',
  5656. $daysRegex,
  5657. '/select',
  5658. );
  5659. $this->assertTags($result, $expected);
  5660. }
  5661. /**
  5662. * testMinute method
  5663. *
  5664. * @return void
  5665. */
  5666. public function testMinute() {
  5667. extract($this->dateRegex);
  5668. $result = $this->Form->minute('Model.field');
  5669. $expected = array(
  5670. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5671. array('option' => array('value' => '')),
  5672. '/option',
  5673. array('option' => array('value' => '00')),
  5674. '00',
  5675. '/option',
  5676. array('option' => array('value' => '01')),
  5677. '01',
  5678. '/option',
  5679. array('option' => array('value' => '02')),
  5680. '02',
  5681. '/option',
  5682. $minutesRegex,
  5683. '/select',
  5684. );
  5685. $this->assertTags($result, $expected);
  5686. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5687. $result = $this->Form->minute('Model.field');
  5688. $expected = array(
  5689. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5690. array('option' => array('value' => '')),
  5691. '/option',
  5692. array('option' => array('value' => '00')),
  5693. '00',
  5694. '/option',
  5695. array('option' => array('value' => '01')),
  5696. '01',
  5697. '/option',
  5698. array('option' => array('value' => '02')),
  5699. '02',
  5700. '/option',
  5701. $minutesRegex,
  5702. array('option' => array('value' => '12', 'selected' => 'selected')),
  5703. '12',
  5704. '/option',
  5705. $minutesRegex,
  5706. '/select',
  5707. );
  5708. $this->assertTags($result, $expected);
  5709. $this->Form->request->data['Model']['field'] = '';
  5710. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5711. $expected = array(
  5712. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5713. array('option' => array('value' => '')),
  5714. '/option',
  5715. array('option' => array('value' => '00')),
  5716. '00',
  5717. '/option',
  5718. array('option' => array('value' => '05')),
  5719. '05',
  5720. '/option',
  5721. array('option' => array('value' => '10')),
  5722. '10',
  5723. '/option',
  5724. $minutesRegex,
  5725. '/select',
  5726. );
  5727. $this->assertTags($result, $expected);
  5728. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5729. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5730. $expected = array(
  5731. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5732. array('option' => array('value' => '')),
  5733. '/option',
  5734. array('option' => array('value' => '00')),
  5735. '00',
  5736. '/option',
  5737. array('option' => array('value' => '05')),
  5738. '05',
  5739. '/option',
  5740. array('option' => array('value' => '10', 'selected' => 'selected')),
  5741. '10',
  5742. '/option',
  5743. $minutesRegex,
  5744. '/select',
  5745. );
  5746. $this->assertTags($result, $expected);
  5747. }
  5748. /**
  5749. * testHour method
  5750. *
  5751. * @return void
  5752. */
  5753. public function testHour() {
  5754. extract($this->dateRegex);
  5755. $result = $this->Form->hour('Model.field', false);
  5756. $expected = array(
  5757. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5758. array('option' => array('value' => '')),
  5759. '/option',
  5760. array('option' => array('value' => '01')),
  5761. '1',
  5762. '/option',
  5763. array('option' => array('value' => '02')),
  5764. '2',
  5765. '/option',
  5766. $hoursRegex,
  5767. '/select',
  5768. );
  5769. $this->assertTags($result, $expected);
  5770. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5771. $result = $this->Form->hour('Model.field', false);
  5772. $expected = array(
  5773. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5774. array('option' => array('value' => '')),
  5775. '/option',
  5776. array('option' => array('value' => '01')),
  5777. '1',
  5778. '/option',
  5779. array('option' => array('value' => '02')),
  5780. '2',
  5781. '/option',
  5782. $hoursRegex,
  5783. array('option' => array('value' => '12', 'selected' => 'selected')),
  5784. '12',
  5785. '/option',
  5786. '/select',
  5787. );
  5788. $this->assertTags($result, $expected);
  5789. $this->Form->request->data['Model']['field'] = '';
  5790. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5791. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5792. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  5793. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5794. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5795. $result = $this->Form->hour('Model.field', true);
  5796. $expected = array(
  5797. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5798. array('option' => array('value' => '')),
  5799. '/option',
  5800. array('option' => array('value' => '00', 'selected' => 'selected')),
  5801. '0',
  5802. '/option',
  5803. array('option' => array('value' => '01')),
  5804. '1',
  5805. '/option',
  5806. array('option' => array('value' => '02')),
  5807. '2',
  5808. '/option',
  5809. $hoursRegex,
  5810. '/select',
  5811. );
  5812. $this->assertTags($result, $expected);
  5813. unset($this->Form->request->data['Model']['field']);
  5814. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5815. $thisHour = date('H');
  5816. $optValue = date('G');
  5817. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5818. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  5819. $result = $this->Form->hour('Model.field', true);
  5820. $expected = array(
  5821. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5822. array('option' => array('value' => '')),
  5823. '/option',
  5824. array('option' => array('value' => '00')),
  5825. '0',
  5826. '/option',
  5827. array('option' => array('value' => '01', 'selected' => 'selected')),
  5828. '1',
  5829. '/option',
  5830. array('option' => array('value' => '02')),
  5831. '2',
  5832. '/option',
  5833. $hoursRegex,
  5834. '/select',
  5835. );
  5836. $this->assertTags($result, $expected);
  5837. }
  5838. /**
  5839. * testYear method
  5840. *
  5841. * @return void
  5842. */
  5843. public function testYear() {
  5844. $result = $this->Form->year('Model.field', 2006, 2007);
  5845. $expected = array(
  5846. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5847. array('option' => array('value' => '')),
  5848. '/option',
  5849. array('option' => array('value' => '2007')),
  5850. '2007',
  5851. '/option',
  5852. array('option' => array('value' => '2006')),
  5853. '2006',
  5854. '/option',
  5855. '/select',
  5856. );
  5857. $this->assertTags($result, $expected);
  5858. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5859. $expected = array(
  5860. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5861. array('option' => array('value' => '')),
  5862. '/option',
  5863. array('option' => array('value' => '2006')),
  5864. '2006',
  5865. '/option',
  5866. array('option' => array('value' => '2007')),
  5867. '2007',
  5868. '/option',
  5869. '/select',
  5870. );
  5871. $this->assertTags($result, $expected);
  5872. $this->request->data['Contact']['published'] = '';
  5873. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5874. $expected = array(
  5875. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5876. array('option' => array('value' => '')),
  5877. '/option',
  5878. array('option' => array('value' => '2007')),
  5879. '2007',
  5880. '/option',
  5881. array('option' => array('value' => '2006')),
  5882. '2006',
  5883. '/option',
  5884. '/select',
  5885. );
  5886. $this->assertTags($result, $expected);
  5887. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5888. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5889. $expected = array(
  5890. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5891. array('option' => array('value' => '2007')),
  5892. '2007',
  5893. '/option',
  5894. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5895. '2006',
  5896. '/option',
  5897. '/select',
  5898. );
  5899. $this->assertTags($result, $expected);
  5900. $this->Form->request->data['Contact']['published'] = '';
  5901. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5902. $expected = array(
  5903. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5904. array('option' => array('value' => '')),
  5905. '/option',
  5906. array('option' => array('value' => '2007')),
  5907. '2007',
  5908. '/option',
  5909. array('option' => array('value' => '2006')),
  5910. '2006',
  5911. '/option',
  5912. '/select',
  5913. );
  5914. $this->assertTags($result, $expected);
  5915. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5916. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5917. $expected = array(
  5918. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5919. array('option' => array('value' => '2007')),
  5920. '2007',
  5921. '/option',
  5922. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5923. '2006',
  5924. '/option',
  5925. '/select',
  5926. );
  5927. $this->assertTags($result, $expected);
  5928. $this->Form->request->data['Contact']['published'] = '';
  5929. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5930. $expected = array(
  5931. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5932. array('option' => array('value' => '')),
  5933. '/option',
  5934. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5935. '2007',
  5936. '/option',
  5937. array('option' => array('value' => '2006')),
  5938. '2006',
  5939. '/option',
  5940. '/select',
  5941. );
  5942. $this->assertTags($result, $expected);
  5943. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5944. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5945. $expected = array(
  5946. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5947. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5948. '2007',
  5949. '/option',
  5950. array('option' => array('value' => '2006')),
  5951. '2006',
  5952. '/option',
  5953. '/select',
  5954. );
  5955. $this->assertTags($result, $expected);
  5956. $this->Form->request->data['Contact']['published'] = '';
  5957. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5958. $expected = array(
  5959. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5960. array('option' => array('value' => '2008')),
  5961. '2008',
  5962. '/option',
  5963. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5964. '2007',
  5965. '/option',
  5966. array('option' => array('value' => '2006')),
  5967. '2006',
  5968. '/option',
  5969. '/select',
  5970. );
  5971. $this->assertTags($result, $expected);
  5972. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5973. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5974. $expected = array(
  5975. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5976. array('option' => array('value' => '2008')),
  5977. '2008',
  5978. '/option',
  5979. array('option' => array('value' => '2007')),
  5980. '2007',
  5981. '/option',
  5982. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5983. '2006',
  5984. '/option',
  5985. '/select',
  5986. );
  5987. $this->assertTags($result, $expected);
  5988. $this->Form->request->data = array();
  5989. $this->Form->create('Contact');
  5990. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5991. $expected = array(
  5992. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5993. array('option' => array('value' => '2008')),
  5994. '2008',
  5995. '/option',
  5996. array('option' => array('value' => '2007')),
  5997. '2007',
  5998. '/option',
  5999. array('option' => array('value' => '2006')),
  6000. '2006',
  6001. '/option',
  6002. '/select',
  6003. );
  6004. $this->assertTags($result, $expected);
  6005. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  6006. $this->assertContains('data[Contact][published][year]', $result);
  6007. }
  6008. /**
  6009. * testYearAutoExpandRange method
  6010. *
  6011. * @return void
  6012. */
  6013. public function testYearAutoExpandRange() {
  6014. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6015. $result = $this->Form->year('User.birthday');
  6016. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6017. $result = $matches[1];
  6018. $expected = range(date('Y') + 20, 1930);
  6019. $this->assertEquals($result, $expected);
  6020. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6021. $result = $this->Form->year('Project.release');
  6022. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6023. $result = $matches[1];
  6024. $expected = range(2050, date('Y') - 20);
  6025. $this->assertEquals($result, $expected);
  6026. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6027. $result = $this->Form->year('Project.release', 1890, 1900);
  6028. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6029. $result = $matches[1];
  6030. $expected = range(1900, 1881);
  6031. $this->assertEquals($result, $expected);
  6032. }
  6033. /**
  6034. * testTextArea method
  6035. *
  6036. * @return void
  6037. */
  6038. public function testTextArea() {
  6039. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  6040. $result = $this->Form->textarea('Model.field');
  6041. $expected = array(
  6042. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6043. 'some test data',
  6044. '/textarea',
  6045. );
  6046. $this->assertTags($result, $expected);
  6047. $result = $this->Form->textarea('Model.tmp');
  6048. $expected = array(
  6049. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  6050. '/textarea',
  6051. );
  6052. $this->assertTags($result, $expected);
  6053. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6054. $result = $this->Form->textarea('Model.field');
  6055. $expected = array(
  6056. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6057. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6058. '/textarea',
  6059. );
  6060. $this->assertTags($result, $expected);
  6061. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6062. $result = $this->Form->textarea('Model.field', array('escape' => false));
  6063. $expected = array(
  6064. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6065. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6066. '/textarea',
  6067. );
  6068. $this->assertTags($result, $expected);
  6069. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  6070. $result = $this->Form->textarea('Model.0.OtherModel.field');
  6071. $expected = array(
  6072. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  6073. '/textarea'
  6074. );
  6075. $this->assertTags($result, $expected);
  6076. }
  6077. /**
  6078. * testTextAreaWithStupidCharacters method
  6079. *
  6080. * test text area with non-ascii characters
  6081. *
  6082. * @return void
  6083. */
  6084. public function testTextAreaWithStupidCharacters() {
  6085. $this->loadFixtures('Post');
  6086. $result = $this->Form->input('Post.content', array(
  6087. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  6088. ));
  6089. $expected = array(
  6090. 'div' => array('class' => 'input textarea'),
  6091. 'label' => array('for' => 'PostContent'),
  6092. 'Current Text',
  6093. '/label',
  6094. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  6095. 'GREAT®',
  6096. '/textarea',
  6097. '/div'
  6098. );
  6099. $this->assertTags($result, $expected);
  6100. }
  6101. /**
  6102. * testHiddenField method
  6103. *
  6104. * @return void
  6105. */
  6106. public function testHiddenField() {
  6107. $Contact = ClassRegistry::getObject('Contact');
  6108. $Contact->validationErrors['field'] = 1;
  6109. $this->Form->request->data['Contact']['field'] = 'test';
  6110. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  6111. $this->assertTags($result, array(
  6112. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  6113. );
  6114. }
  6115. /**
  6116. * testFileUploadField method
  6117. *
  6118. * @return void
  6119. */
  6120. public function testFileUploadField() {
  6121. $result = $this->Form->file('Model.upload');
  6122. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6123. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  6124. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  6125. $expected = array(
  6126. 'div' => array('class' => 'input file'),
  6127. 'label' => array('for' => 'ModelUpload'),
  6128. 'Upload',
  6129. '/label',
  6130. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  6131. '/div'
  6132. );
  6133. $this->assertTags($result, $expected);
  6134. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6135. $result = $this->Form->file('Model.upload');
  6136. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6137. }
  6138. /**
  6139. * test File upload input on a model not used in create();
  6140. *
  6141. * @return void
  6142. */
  6143. public function testFileUploadOnOtherModel() {
  6144. $this->Form->create('ValidateUser', array('type' => 'file'));
  6145. $result = $this->Form->file('ValidateProfile.city');
  6146. $expected = array(
  6147. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  6148. );
  6149. $this->assertTags($result, $expected);
  6150. }
  6151. /**
  6152. * testButton method
  6153. *
  6154. * @return void
  6155. */
  6156. public function testButton() {
  6157. $result = $this->Form->button('Hi');
  6158. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  6159. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  6160. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  6161. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  6162. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  6163. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  6164. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  6165. $result = $this->Form->button('No type', array('type' => false));
  6166. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  6167. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  6168. $this->assertNotRegExp('/\&039/', $result);
  6169. }
  6170. /**
  6171. * Test that button() makes unlocked fields by default.
  6172. *
  6173. * @return void
  6174. */
  6175. public function testButtonUnlockedByDefault() {
  6176. $this->Form->request->params['_Token']['key'] = 'secured';
  6177. $this->Form->button('Save', array('name' => 'save'));
  6178. $this->Form->button('Clear');
  6179. $result = $this->Form->unlockField();
  6180. $this->assertEquals(array('save'), $result);
  6181. }
  6182. /**
  6183. * testPostButton method
  6184. *
  6185. * @return void
  6186. */
  6187. public function testPostButton() {
  6188. $result = $this->Form->postButton('Hi', '/controller/action');
  6189. $this->assertTags($result, array(
  6190. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  6191. 'div' => array('style' => 'display:none;'),
  6192. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6193. '/div',
  6194. 'button' => array('type' => 'submit'),
  6195. 'Hi',
  6196. '/button',
  6197. '/form'
  6198. ));
  6199. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  6200. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  6201. }
  6202. /**
  6203. * Test that postButton adds _Token fields.
  6204. *
  6205. * @return void
  6206. */
  6207. public function testSecurePostButton() {
  6208. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6209. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6210. $expected = array(
  6211. 'form' => array(
  6212. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6213. ),
  6214. array('div' => array('style' => 'display:none;')),
  6215. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6216. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6217. '/div',
  6218. 'button' => array('type' => 'submit'),
  6219. 'Delete',
  6220. '/button',
  6221. array('div' => array('style' => 'display:none;')),
  6222. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6223. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6224. '/div',
  6225. '/form',
  6226. );
  6227. $this->assertTags($result, $expected);
  6228. }
  6229. /**
  6230. * testPostLink method
  6231. *
  6232. * @return void
  6233. */
  6234. public function testPostLink() {
  6235. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6236. $this->assertTags($result, array(
  6237. 'form' => array(
  6238. 'method' => 'post', 'action' => '/posts/delete/1',
  6239. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6240. ),
  6241. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6242. '/form',
  6243. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6244. 'Delete',
  6245. '/a'
  6246. ));
  6247. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  6248. $this->assertTags($result, array(
  6249. 'form' => array(
  6250. 'method' => 'post', 'action' => '/posts/delete/1',
  6251. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6252. ),
  6253. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  6254. '/form',
  6255. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6256. 'Delete',
  6257. '/a'
  6258. ));
  6259. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  6260. $this->assertTags($result, array(
  6261. 'form' => array(
  6262. 'method' => 'post', 'action' => '/posts/delete/1',
  6263. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6264. ),
  6265. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6266. '/form',
  6267. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6268. 'Delete',
  6269. '/a'
  6270. ));
  6271. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  6272. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  6273. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  6274. $this->assertTags($result, array(
  6275. 'form' => array(
  6276. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6277. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6278. ),
  6279. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6280. '/form',
  6281. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6282. 'Delete',
  6283. '/a'
  6284. ));
  6285. }
  6286. /**
  6287. * Test that postLink adds _Token fields.
  6288. *
  6289. * @return void
  6290. */
  6291. public function testSecurePostLink() {
  6292. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6293. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6294. $expected = array(
  6295. 'form' => array(
  6296. 'method' => 'post', 'action' => '/posts/delete/1',
  6297. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6298. ),
  6299. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6300. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6301. 'div' => array('style' => 'display:none;'),
  6302. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6303. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6304. '/div',
  6305. '/form',
  6306. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6307. 'Delete',
  6308. '/a'
  6309. );
  6310. $this->assertTags($result, $expected);
  6311. }
  6312. /**
  6313. * testSubmitButton method
  6314. *
  6315. * @return void
  6316. */
  6317. public function testSubmitButton() {
  6318. $result = $this->Form->submit('');
  6319. $expected = array(
  6320. 'div' => array('class' => 'submit'),
  6321. 'input' => array('type' => 'submit', 'value' => ''),
  6322. '/div'
  6323. );
  6324. $this->assertTags($result, $expected);
  6325. $result = $this->Form->submit('Test Submit');
  6326. $expected = array(
  6327. 'div' => array('class' => 'submit'),
  6328. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6329. '/div'
  6330. );
  6331. $this->assertTags($result, $expected);
  6332. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  6333. $expected = array(
  6334. 'span' => array('class' => 'submit'),
  6335. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6336. '/span'
  6337. );
  6338. $this->assertTags($result, $expected);
  6339. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  6340. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  6341. $this->assertTags($result, $expected);
  6342. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  6343. $expected = array(
  6344. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  6345. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6346. '/div'
  6347. );
  6348. $this->assertTags($result, $expected);
  6349. $result = $this->Form->submit('Next >');
  6350. $expected = array(
  6351. 'div' => array('class' => 'submit'),
  6352. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  6353. '/div'
  6354. );
  6355. $this->assertTags($result, $expected);
  6356. $result = $this->Form->submit('Next >', array('escape' => false));
  6357. $expected = array(
  6358. 'div' => array('class' => 'submit'),
  6359. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  6360. '/div'
  6361. );
  6362. $this->assertTags($result, $expected);
  6363. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  6364. $expected = array(
  6365. 'div' => array('class' => 'submit'),
  6366. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  6367. '/div'
  6368. );
  6369. $this->assertTags($result, $expected);
  6370. $before = '--before--';
  6371. $after = '--after--';
  6372. $result = $this->Form->submit('Test', array('before' => $before));
  6373. $expected = array(
  6374. 'div' => array('class' => 'submit'),
  6375. '--before--',
  6376. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6377. '/div'
  6378. );
  6379. $this->assertTags($result, $expected);
  6380. $result = $this->Form->submit('Test', array('after' => $after));
  6381. $expected = array(
  6382. 'div' => array('class' => 'submit'),
  6383. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6384. '--after--',
  6385. '/div'
  6386. );
  6387. $this->assertTags($result, $expected);
  6388. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  6389. $expected = array(
  6390. 'div' => array('class' => 'submit'),
  6391. '--before--',
  6392. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6393. '--after--',
  6394. '/div'
  6395. );
  6396. $this->assertTags($result, $expected);
  6397. }
  6398. /**
  6399. * test image submit types.
  6400. *
  6401. * @return void
  6402. */
  6403. public function testSubmitImage() {
  6404. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6405. $expected = array(
  6406. 'div' => array('class' => 'submit'),
  6407. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  6408. '/div'
  6409. );
  6410. $this->assertTags($result, $expected);
  6411. $result = $this->Form->submit('/relative/cake.power.gif');
  6412. $expected = array(
  6413. 'div' => array('class' => 'submit'),
  6414. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  6415. '/div'
  6416. );
  6417. $this->assertTags($result, $expected);
  6418. $result = $this->Form->submit('cake.power.gif');
  6419. $expected = array(
  6420. 'div' => array('class' => 'submit'),
  6421. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6422. '/div'
  6423. );
  6424. $this->assertTags($result, $expected);
  6425. $result = $this->Form->submit('Not.an.image');
  6426. $expected = array(
  6427. 'div' => array('class' => 'submit'),
  6428. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6429. '/div'
  6430. );
  6431. $this->assertTags($result, $expected);
  6432. $after = '--after--';
  6433. $before = '--before--';
  6434. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  6435. $expected = array(
  6436. 'div' => array('class' => 'submit'),
  6437. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6438. '--after--',
  6439. '/div'
  6440. );
  6441. $this->assertTags($result, $expected);
  6442. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  6443. $expected = array(
  6444. 'div' => array('class' => 'submit'),
  6445. '--before--',
  6446. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6447. '/div'
  6448. );
  6449. $this->assertTags($result, $expected);
  6450. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  6451. $expected = array(
  6452. 'div' => array('class' => 'submit'),
  6453. '--before--',
  6454. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6455. '--after--',
  6456. '/div'
  6457. );
  6458. $this->assertTags($result, $expected);
  6459. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  6460. $expected = array(
  6461. 'div' => array('class' => 'submit'),
  6462. '--before--',
  6463. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6464. '--after--',
  6465. '/div'
  6466. );
  6467. $this->assertTags($result, $expected);
  6468. }
  6469. /**
  6470. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6471. * be submitted at a time.
  6472. *
  6473. * @return void
  6474. */
  6475. public function testSubmitUnlockedByDefault() {
  6476. $this->Form->request->params['_Token']['key'] = 'secured';
  6477. $this->Form->submit('Go go');
  6478. $this->Form->submit('Save', array('name' => 'save'));
  6479. $result = $this->Form->unlockField();
  6480. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  6481. }
  6482. /**
  6483. * Test submit image with timestamps.
  6484. *
  6485. * @return void
  6486. */
  6487. public function testSubmitImageTimestamp() {
  6488. Configure::write('Asset.timestamp', 'force');
  6489. $result = $this->Form->submit('cake.power.gif');
  6490. $expected = array(
  6491. 'div' => array('class' => 'submit'),
  6492. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  6493. '/div'
  6494. );
  6495. $this->assertTags($result, $expected);
  6496. }
  6497. /**
  6498. * test the create() method
  6499. *
  6500. * @return void
  6501. */
  6502. public function testCreate() {
  6503. $result = $this->Form->create('Contact');
  6504. $encoding = strtolower(Configure::read('App.encoding'));
  6505. $expected = array(
  6506. 'form' => array(
  6507. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6508. 'accept-charset' => $encoding
  6509. ),
  6510. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  6511. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6512. '/div'
  6513. );
  6514. $this->assertTags($result, $expected);
  6515. $result = $this->Form->create('Contact', array('type' => 'GET'));
  6516. $expected = array('form' => array(
  6517. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6518. 'accept-charset' => $encoding
  6519. ));
  6520. $this->assertTags($result, $expected);
  6521. $result = $this->Form->create('Contact', array('type' => 'get'));
  6522. $expected = array('form' => array(
  6523. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6524. 'accept-charset' => $encoding
  6525. ));
  6526. $this->assertTags($result, $expected);
  6527. $result = $this->Form->create('Contact', array('type' => 'put'));
  6528. $expected = array(
  6529. 'form' => array(
  6530. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6531. 'accept-charset' => $encoding
  6532. ),
  6533. 'div' => array('style' => 'display:none;'),
  6534. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6535. '/div'
  6536. );
  6537. $this->assertTags($result, $expected);
  6538. $result = $this->Form->create('Contact', array('type' => 'file'));
  6539. $expected = array(
  6540. 'form' => array(
  6541. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6542. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6543. ),
  6544. 'div' => array('style' => 'display:none;'),
  6545. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6546. '/div'
  6547. );
  6548. $this->assertTags($result, $expected);
  6549. $this->Form->request->data['Contact']['id'] = 1;
  6550. $this->Form->request->here = '/contacts/edit/1';
  6551. $this->Form->request['action'] = 'edit';
  6552. $result = $this->Form->create('Contact');
  6553. $expected = array(
  6554. 'form' => array(
  6555. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6556. 'accept-charset' => $encoding
  6557. ),
  6558. 'div' => array('style' => 'display:none;'),
  6559. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6560. '/div'
  6561. );
  6562. $this->assertTags($result, $expected);
  6563. $this->Form->request->data['Contact']['id'] = 1;
  6564. $this->Form->request->here = '/contacts/edit/1';
  6565. $this->Form->request['action'] = 'edit';
  6566. $result = $this->Form->create('Contact', array('type' => 'file'));
  6567. $expected = array(
  6568. 'form' => array(
  6569. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6570. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6571. ),
  6572. 'div' => array('style' => 'display:none;'),
  6573. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6574. '/div'
  6575. );
  6576. $this->assertTags($result, $expected);
  6577. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  6578. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  6579. $expected = array(
  6580. 'form' => array(
  6581. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  6582. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  6583. ),
  6584. 'div' => array('style' => 'display:none;'),
  6585. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6586. '/div'
  6587. );
  6588. $this->assertTags($result, $expected);
  6589. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  6590. $expected = array(
  6591. 'form' => array(
  6592. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  6593. 'accept-charset' => $encoding
  6594. ),
  6595. 'div' => array('style' => 'display:none;'),
  6596. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6597. '/div'
  6598. );
  6599. $this->assertTags($result, $expected);
  6600. $this->Form->request['action'] = 'add';
  6601. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  6602. $expected = array(
  6603. 'form' => array(
  6604. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  6605. 'accept-charset' => $encoding
  6606. ),
  6607. 'div' => array('style' => 'display:none;'),
  6608. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6609. '/div'
  6610. );
  6611. $this->assertTags($result, $expected);
  6612. $result = $this->Form->create('User', array('action' => 'login'));
  6613. $expected = array(
  6614. 'form' => array(
  6615. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  6616. 'accept-charset' => $encoding
  6617. ),
  6618. 'div' => array('style' => 'display:none;'),
  6619. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6620. '/div'
  6621. );
  6622. $this->assertTags($result, $expected);
  6623. $result = $this->Form->create('User', array('url' => '/users/login'));
  6624. $expected = array(
  6625. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  6626. 'div' => array('style' => 'display:none;'),
  6627. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6628. '/div'
  6629. );
  6630. $this->assertTags($result, $expected);
  6631. $this->Form->request['controller'] = 'pages';
  6632. $result = $this->Form->create('User', array('action' => 'signup'));
  6633. $expected = array(
  6634. 'form' => array(
  6635. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  6636. 'accept-charset' => $encoding
  6637. ),
  6638. 'div' => array('style' => 'display:none;'),
  6639. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6640. '/div'
  6641. );
  6642. $this->assertTags($result, $expected);
  6643. $this->Form->request->data = array();
  6644. $this->Form->request['controller'] = 'contacts';
  6645. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6646. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  6647. $expected = array(
  6648. 'form' => array(
  6649. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  6650. 'accept-charset' => 'utf-8'
  6651. ),
  6652. 'div' => array('style' => 'display:none;'),
  6653. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6654. '/div'
  6655. );
  6656. $this->assertTags($result, $expected);
  6657. }
  6658. /**
  6659. * Test the onsubmit option for create()
  6660. *
  6661. * @return void
  6662. */
  6663. public function testCreateOnSubmit() {
  6664. $this->Form->request->data = array();
  6665. $this->Form->request['controller'] = 'contacts';
  6666. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6667. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  6668. $expected = array(
  6669. 'form' => array(
  6670. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  6671. 'accept-charset' => 'utf-8'
  6672. ),
  6673. 'div' => array('style' => 'display:none;'),
  6674. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6675. '/div'
  6676. );
  6677. $this->assertTags($result, $expected);
  6678. $this->Form->request->data = array();
  6679. $this->Form->request['controller'] = 'contacts';
  6680. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6681. $result = $this->Form->create(array(
  6682. 'url' => array('action' => 'index', 'param'),
  6683. 'default' => false,
  6684. 'onsubmit' => 'someFunction();'
  6685. ));
  6686. $expected = array(
  6687. 'form' => array(
  6688. 'id' => 'ContactAddForm', 'method' => 'post',
  6689. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6690. 'action' => '/contacts/index/param',
  6691. 'accept-charset' => 'utf-8'
  6692. ),
  6693. 'div' => array('style' => 'display:none;'),
  6694. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6695. '/div'
  6696. );
  6697. $this->assertTags($result, $expected);
  6698. }
  6699. /**
  6700. * test create() with automatic url generation
  6701. *
  6702. * @return void
  6703. */
  6704. public function testCreateAutoUrl() {
  6705. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6706. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6707. $this->Form->request->base = '/base_url';
  6708. $result = $this->Form->create('Contact');
  6709. $expected = array(
  6710. 'form' => array(
  6711. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6712. 'accept-charset' => 'utf-8'
  6713. ),
  6714. 'div' => array('style' => 'display:none;'),
  6715. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6716. '/div'
  6717. );
  6718. $this->assertTags($result, $expected);
  6719. $this->Form->request['action'] = 'delete';
  6720. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6721. $this->Form->request->base = '/base_url';
  6722. $result = $this->Form->create('Contact');
  6723. $expected = array(
  6724. 'form' => array(
  6725. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6726. 'accept-charset' => 'utf-8'
  6727. ),
  6728. 'div' => array('style' => 'display:none;'),
  6729. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6730. '/div'
  6731. );
  6732. $this->assertTags($result, $expected);
  6733. }
  6734. /**
  6735. * test create() with a custom route
  6736. *
  6737. * @return void
  6738. */
  6739. public function testCreateCustomRoute() {
  6740. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6741. $encoding = strtolower(Configure::read('App.encoding'));
  6742. $result = $this->Form->create('User', array('action' => 'login'));
  6743. $expected = array(
  6744. 'form' => array(
  6745. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6746. 'accept-charset' => $encoding
  6747. ),
  6748. 'div' => array('style' => 'display:none;'),
  6749. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6750. '/div'
  6751. );
  6752. $this->assertTags($result, $expected);
  6753. }
  6754. /**
  6755. * test that inputDefaults are stored and used.
  6756. *
  6757. * @return void
  6758. */
  6759. public function testCreateWithInputDefaults() {
  6760. $this->Form->create('User', array(
  6761. 'inputDefaults' => array(
  6762. 'div' => false,
  6763. 'label' => false,
  6764. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6765. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6766. )
  6767. ));
  6768. $result = $this->Form->input('username');
  6769. $expected = array(
  6770. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6771. );
  6772. $this->assertTags($result, $expected);
  6773. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6774. $expected = array(
  6775. 'div' => array('class' => 'input text'),
  6776. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6777. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6778. '/div'
  6779. );
  6780. $this->assertTags($result, $expected);
  6781. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  6782. $expected = array(
  6783. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6784. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  6785. );
  6786. $this->assertTags($result, $expected);
  6787. $this->Form->create('User', array(
  6788. 'inputDefaults' => array(
  6789. 'div' => false,
  6790. 'label' => array('class' => 'nice', 'for' => 'changed'),
  6791. )
  6792. ));
  6793. $result = $this->Form->input('username', array('div' => true));
  6794. $expected = array(
  6795. 'div' => array('class' => 'input text'),
  6796. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  6797. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6798. '/div'
  6799. );
  6800. $this->assertTags($result, $expected);
  6801. }
  6802. /**
  6803. * test automatic accept-charset overriding
  6804. *
  6805. * @return void
  6806. */
  6807. public function testCreateWithAcceptCharset() {
  6808. $result = $this->Form->create('UserForm', array(
  6809. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  6810. )
  6811. );
  6812. $expected = array(
  6813. 'form' => array(
  6814. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6815. 'accept-charset' => 'iso-8859-1'
  6816. ),
  6817. 'div' => array('style' => 'display:none;'),
  6818. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6819. '/div'
  6820. );
  6821. $this->assertTags($result, $expected);
  6822. }
  6823. /**
  6824. * Test base form url when url param is passed with multiple parameters (&)
  6825. *
  6826. */
  6827. public function testCreateQuerystringrequest() {
  6828. $encoding = strtolower(Configure::read('App.encoding'));
  6829. $result = $this->Form->create('Contact', array(
  6830. 'type' => 'post',
  6831. 'escape' => false,
  6832. 'url' => array(
  6833. 'controller' => 'controller',
  6834. 'action' => 'action',
  6835. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6836. )
  6837. ));
  6838. $expected = array(
  6839. 'form' => array(
  6840. 'id' => 'ContactAddForm',
  6841. 'method' => 'post',
  6842. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6843. 'accept-charset' => $encoding
  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. $result = $this->Form->create('Contact', array(
  6851. 'type' => 'post',
  6852. 'url' => array(
  6853. 'controller' => 'controller',
  6854. 'action' => 'action',
  6855. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6856. )
  6857. ));
  6858. $expected = array(
  6859. 'form' => array(
  6860. 'id' => 'ContactAddForm',
  6861. 'method' => 'post',
  6862. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  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 create() doesn't cause errors by multiple id's being in the primary key
  6873. * as could happen with multiple select or checkboxes.
  6874. *
  6875. * @return void
  6876. */
  6877. public function testCreateWithMultipleIdInData() {
  6878. $encoding = strtolower(Configure::read('App.encoding'));
  6879. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6880. $result = $this->Form->create('Contact');
  6881. $expected = array(
  6882. 'form' => array(
  6883. 'id' => 'ContactAddForm',
  6884. 'method' => 'post',
  6885. 'action' => '/contacts/add',
  6886. 'accept-charset' => $encoding
  6887. ),
  6888. 'div' => array('style' => 'display:none;'),
  6889. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6890. '/div'
  6891. );
  6892. $this->assertTags($result, $expected);
  6893. }
  6894. /**
  6895. * test that create() doesn't add in extra passed params.
  6896. *
  6897. * @return void
  6898. */
  6899. public function testCreatePassedArgs() {
  6900. $encoding = strtolower(Configure::read('App.encoding'));
  6901. $this->Form->request->data['Contact']['id'] = 1;
  6902. $result = $this->Form->create('Contact', array(
  6903. 'type' => 'post',
  6904. 'escape' => false,
  6905. 'url' => array(
  6906. 'action' => 'edit',
  6907. 'myparam'
  6908. )
  6909. ));
  6910. $expected = array(
  6911. 'form' => array(
  6912. 'id' => 'ContactAddForm',
  6913. 'method' => 'post',
  6914. 'action' => '/contacts/edit/myparam',
  6915. 'accept-charset' => $encoding
  6916. ),
  6917. 'div' => array('style' => 'display:none;'),
  6918. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6919. '/div'
  6920. );
  6921. $this->assertTags($result, $expected);
  6922. }
  6923. /**
  6924. * test creating a get form, and get form inputs.
  6925. *
  6926. * @return void
  6927. */
  6928. public function testGetFormCreate() {
  6929. $encoding = strtolower(Configure::read('App.encoding'));
  6930. $result = $this->Form->create('Contact', array('type' => 'get'));
  6931. $this->assertTags($result, array('form' => array(
  6932. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6933. 'accept-charset' => $encoding
  6934. )));
  6935. $result = $this->Form->text('Contact.name');
  6936. $this->assertTags($result, array('input' => array(
  6937. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6938. )));
  6939. $result = $this->Form->password('password');
  6940. $this->assertTags($result, array('input' => array(
  6941. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6942. )));
  6943. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6944. $result = $this->Form->text('user_form');
  6945. $this->assertTags($result, array('input' => array(
  6946. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6947. )));
  6948. }
  6949. /**
  6950. * test get form, and inputs when the model param is false
  6951. *
  6952. * @return void
  6953. */
  6954. public function testGetFormWithFalseModel() {
  6955. $encoding = strtolower(Configure::read('App.encoding'));
  6956. $this->Form->request['controller'] = 'contact_test';
  6957. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6958. $expected = array('form' => array(
  6959. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6960. 'accept-charset' => $encoding
  6961. ));
  6962. $this->assertTags($result, $expected);
  6963. $result = $this->Form->text('reason');
  6964. $expected = array(
  6965. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6966. );
  6967. $this->assertTags($result, $expected);
  6968. }
  6969. /**
  6970. * test that datetime() works with GET style forms.
  6971. *
  6972. * @return void
  6973. */
  6974. public function testDateTimeWithGetForms() {
  6975. extract($this->dateRegex);
  6976. $this->Form->create('Contact', array('type' => 'get'));
  6977. $result = $this->Form->datetime('created');
  6978. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6979. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6980. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6981. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6982. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6983. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6984. }
  6985. /**
  6986. * testEditFormWithData method
  6987. *
  6988. * test auto populating form elements from submitted data.
  6989. *
  6990. * @return void
  6991. */
  6992. public function testEditFormWithData() {
  6993. $this->Form->request->data = array('Person' => array(
  6994. 'id' => 1,
  6995. 'first_name' => 'Nate',
  6996. 'last_name' => 'Abele',
  6997. 'email' => 'nate@example.com'
  6998. ));
  6999. $this->Form->request->addParams(array(
  7000. 'models' => array('Person'),
  7001. 'controller' => 'people',
  7002. 'action' => 'add'
  7003. ));
  7004. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  7005. $this->Form->create();
  7006. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  7007. $expected = array(
  7008. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  7009. 'select' => array(
  7010. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  7011. ),
  7012. array('option' => array('value' => 1)), 'Nate', '/option',
  7013. array('option' => array('value' => 2)), 'Garrett', '/option',
  7014. array('option' => array('value' => 3)), 'Larry', '/option',
  7015. '/select'
  7016. );
  7017. $this->assertTags($result, $expected);
  7018. }
  7019. /**
  7020. * Test that required fields are created for various types of validation.
  7021. *
  7022. * @return void
  7023. */
  7024. public function testFormInputRequiredDetection() {
  7025. $this->Form->create('Contact');
  7026. $result = $this->Form->input('Contact.non_existing');
  7027. $expected = array(
  7028. 'div' => array('class' => 'input text'),
  7029. 'label' => array('for' => 'ContactNonExisting'),
  7030. 'Non Existing',
  7031. '/label',
  7032. 'input' => array(
  7033. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  7034. 'id' => 'ContactNonExisting'
  7035. ),
  7036. '/div'
  7037. );
  7038. $this->assertTags($result, $expected);
  7039. $result = $this->Form->input('Contact.imrequired');
  7040. $expected = array(
  7041. 'div' => array('class' => 'input text required'),
  7042. 'label' => array('for' => 'ContactImrequired'),
  7043. 'Imrequired',
  7044. '/label',
  7045. 'input' => array(
  7046. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7047. 'id' => 'ContactImrequired',
  7048. 'required' => 'required'
  7049. ),
  7050. '/div'
  7051. );
  7052. $this->assertTags($result, $expected);
  7053. $result = $this->Form->input('Contact.imalsorequired');
  7054. $expected = array(
  7055. 'div' => array('class' => 'input text required'),
  7056. 'label' => array('for' => 'ContactImalsorequired'),
  7057. 'Imalsorequired',
  7058. '/label',
  7059. 'input' => array(
  7060. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  7061. 'id' => 'ContactImalsorequired',
  7062. 'required' => 'required'
  7063. ),
  7064. '/div'
  7065. );
  7066. $this->assertTags($result, $expected);
  7067. $result = $this->Form->input('Contact.imrequiredtoo');
  7068. $expected = array(
  7069. 'div' => array('class' => 'input text required'),
  7070. 'label' => array('for' => 'ContactImrequiredtoo'),
  7071. 'Imrequiredtoo',
  7072. '/label',
  7073. 'input' => array(
  7074. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  7075. 'id' => 'ContactImrequiredtoo',
  7076. 'required' => 'required'
  7077. ),
  7078. '/div'
  7079. );
  7080. $this->assertTags($result, $expected);
  7081. $result = $this->Form->input('Contact.required_one');
  7082. $expected = array(
  7083. 'div' => array('class' => 'input text required'),
  7084. 'label' => array('for' => 'ContactRequiredOne'),
  7085. 'Required One',
  7086. '/label',
  7087. 'input' => array(
  7088. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  7089. 'id' => 'ContactRequiredOne',
  7090. 'required' => 'required'
  7091. ),
  7092. '/div'
  7093. );
  7094. $this->assertTags($result, $expected);
  7095. $result = $this->Form->input('Contact.string_required');
  7096. $expected = array(
  7097. 'div' => array('class' => 'input text required'),
  7098. 'label' => array('for' => 'ContactStringRequired'),
  7099. 'String Required',
  7100. '/label',
  7101. 'input' => array(
  7102. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  7103. 'id' => 'ContactStringRequired',
  7104. 'required' => 'required'
  7105. ),
  7106. '/div'
  7107. );
  7108. $this->assertTags($result, $expected);
  7109. $result = $this->Form->input('Contact.imnotrequired');
  7110. $expected = array(
  7111. 'div' => array('class' => 'input text'),
  7112. 'label' => array('for' => 'ContactImnotrequired'),
  7113. 'Imnotrequired',
  7114. '/label',
  7115. 'input' => array(
  7116. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  7117. 'id' => 'ContactImnotrequired'
  7118. ),
  7119. '/div'
  7120. );
  7121. $this->assertTags($result, $expected);
  7122. $result = $this->Form->input('Contact.imalsonotrequired');
  7123. $expected = array(
  7124. 'div' => array('class' => 'input text'),
  7125. 'label' => array('for' => 'ContactImalsonotrequired'),
  7126. 'Imalsonotrequired',
  7127. '/label',
  7128. 'input' => array(
  7129. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  7130. 'id' => 'ContactImalsonotrequired'
  7131. ),
  7132. '/div'
  7133. );
  7134. $this->assertTags($result, $expected);
  7135. $result = $this->Form->input('Contact.imalsonotrequired2');
  7136. $expected = array(
  7137. 'div' => array('class' => 'input text'),
  7138. 'label' => array('for' => 'ContactImalsonotrequired2'),
  7139. 'Imalsonotrequired2',
  7140. '/label',
  7141. 'input' => array(
  7142. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  7143. 'id' => 'ContactImalsonotrequired2'
  7144. ),
  7145. '/div'
  7146. );
  7147. $this->assertTags($result, $expected);
  7148. $result = $this->Form->input('Contact.imnotrequiredeither');
  7149. $expected = array(
  7150. 'div' => array('class' => 'input text'),
  7151. 'label' => array('for' => 'ContactImnotrequiredeither'),
  7152. 'Imnotrequiredeither',
  7153. '/label',
  7154. 'input' => array(
  7155. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  7156. 'id' => 'ContactImnotrequiredeither'
  7157. ),
  7158. '/div'
  7159. );
  7160. $this->assertTags($result, $expected);
  7161. $result = $this->Form->input('Contact.iamrequiredalways');
  7162. $expected = array(
  7163. 'div' => array('class' => 'input text required'),
  7164. 'label' => array('for' => 'ContactIamrequiredalways'),
  7165. 'Iamrequiredalways',
  7166. '/label',
  7167. 'input' => array(
  7168. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  7169. 'id' => 'ContactIamrequiredalways',
  7170. 'required' => 'required'
  7171. ),
  7172. '/div'
  7173. );
  7174. $this->assertTags($result, $expected);
  7175. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  7176. $expected = array(
  7177. 'div' => array('class' => 'input checkbox required'),
  7178. array('input' => array(
  7179. 'type' => 'hidden',
  7180. 'name' => 'data[Contact][boolean_field]',
  7181. 'id' => 'ContactBooleanField_',
  7182. 'value' => '0'
  7183. )),
  7184. array('input' => array(
  7185. 'type' => 'checkbox',
  7186. 'name' => 'data[Contact][boolean_field]',
  7187. 'value' => '1',
  7188. 'id' => 'ContactBooleanField'
  7189. )),
  7190. 'label' => array('for' => 'ContactBooleanField'),
  7191. 'Boolean Field',
  7192. '/label',
  7193. '/div'
  7194. );
  7195. $this->assertTags($result, $expected);
  7196. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  7197. $expected = array(
  7198. 'div' => array('class' => 'input checkbox required'),
  7199. array('input' => array(
  7200. 'type' => 'hidden',
  7201. 'name' => 'data[Contact][boolean_field]',
  7202. 'id' => 'ContactBooleanField_',
  7203. 'value' => '0'
  7204. )),
  7205. array('input' => array(
  7206. 'type' => 'checkbox',
  7207. 'name' => 'data[Contact][boolean_field]',
  7208. 'value' => '1',
  7209. 'id' => 'ContactBooleanField',
  7210. 'required' => 'required'
  7211. )),
  7212. 'label' => array('for' => 'ContactBooleanField'),
  7213. 'Boolean Field',
  7214. '/label',
  7215. '/div'
  7216. );
  7217. $this->assertTags($result, $expected);
  7218. }
  7219. /**
  7220. * testFormMagicInput method
  7221. *
  7222. * @return void
  7223. */
  7224. public function testFormMagicInput() {
  7225. $encoding = strtolower(Configure::read('App.encoding'));
  7226. $result = $this->Form->create('Contact');
  7227. $expected = array(
  7228. 'form' => array(
  7229. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7230. 'accept-charset' => $encoding
  7231. ),
  7232. 'div' => array('style' => 'display:none;'),
  7233. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7234. '/div'
  7235. );
  7236. $this->assertTags($result, $expected);
  7237. $result = $this->Form->input('name');
  7238. $expected = array(
  7239. 'div' => array('class' => 'input text'),
  7240. 'label' => array('for' => 'ContactName'),
  7241. 'Name',
  7242. '/label',
  7243. 'input' => array(
  7244. 'type' => 'text', 'name' => 'data[Contact][name]',
  7245. 'id' => 'ContactName', 'maxlength' => '255'
  7246. ),
  7247. '/div'
  7248. );
  7249. $this->assertTags($result, $expected);
  7250. $result = $this->Form->input('non_existing_field_in_contact_model');
  7251. $expected = array(
  7252. 'div' => array('class' => 'input text'),
  7253. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  7254. 'Non Existing Field In Contact Model',
  7255. '/label',
  7256. 'input' => array(
  7257. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  7258. 'id' => 'ContactNonExistingFieldInContactModel'
  7259. ),
  7260. '/div'
  7261. );
  7262. $this->assertTags($result, $expected);
  7263. $result = $this->Form->input('Address.street');
  7264. $expected = array(
  7265. 'div' => array('class' => 'input text'),
  7266. 'label' => array('for' => 'AddressStreet'),
  7267. 'Street',
  7268. '/label',
  7269. 'input' => array(
  7270. 'type' => 'text', 'name' => 'data[Address][street]',
  7271. 'id' => 'AddressStreet'
  7272. ),
  7273. '/div'
  7274. );
  7275. $this->assertTags($result, $expected);
  7276. $result = $this->Form->input('Address.non_existing_field_in_model');
  7277. $expected = array(
  7278. 'div' => array('class' => 'input text'),
  7279. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  7280. 'Non Existing Field In Model',
  7281. '/label',
  7282. 'input' => array(
  7283. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  7284. 'id' => 'AddressNonExistingFieldInModel'
  7285. ),
  7286. '/div'
  7287. );
  7288. $this->assertTags($result, $expected);
  7289. $result = $this->Form->input('name', array('div' => false));
  7290. $expected = array(
  7291. 'label' => array('for' => 'ContactName'),
  7292. 'Name',
  7293. '/label',
  7294. 'input' => array(
  7295. 'type' => 'text', 'name' => 'data[Contact][name]',
  7296. 'id' => 'ContactName', 'maxlength' => '255'
  7297. )
  7298. );
  7299. $this->assertTags($result, $expected);
  7300. extract($this->dateRegex);
  7301. $now = strtotime('now');
  7302. $result = $this->Form->input('Contact.published', array('div' => false));
  7303. $expected = array(
  7304. 'label' => array('for' => 'ContactPublishedMonth'),
  7305. 'Published',
  7306. '/label',
  7307. array('select' => array(
  7308. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  7309. )),
  7310. $monthsRegex,
  7311. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7312. date('F', $now),
  7313. '/option',
  7314. '*/select',
  7315. '-',
  7316. array('select' => array(
  7317. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  7318. )),
  7319. $daysRegex,
  7320. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7321. date('j', $now),
  7322. '/option',
  7323. '*/select',
  7324. '-',
  7325. array('select' => array(
  7326. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  7327. )),
  7328. $yearsRegex,
  7329. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7330. date('Y', $now),
  7331. '*/select'
  7332. );
  7333. $this->assertTags($result, $expected);
  7334. $result = $this->Form->input('Contact.updated', array('div' => false));
  7335. $expected = array(
  7336. 'label' => array('for' => 'ContactUpdatedMonth'),
  7337. 'Updated',
  7338. '/label',
  7339. array('select' => array(
  7340. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  7341. )),
  7342. $monthsRegex,
  7343. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7344. date('F', $now),
  7345. '/option',
  7346. '*/select',
  7347. '-',
  7348. array('select' => array(
  7349. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  7350. )),
  7351. $daysRegex,
  7352. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7353. date('j', $now),
  7354. '/option',
  7355. '*/select',
  7356. '-',
  7357. array('select' => array(
  7358. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  7359. )),
  7360. $yearsRegex,
  7361. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7362. date('Y', $now),
  7363. '*/select'
  7364. );
  7365. $this->assertTags($result, $expected);
  7366. $result = $this->Form->input('UserForm.stuff');
  7367. $expected = array(
  7368. 'div' => array('class' => 'input text'),
  7369. 'label' => array('for' => 'UserFormStuff'),
  7370. 'Stuff',
  7371. '/label',
  7372. 'input' => array(
  7373. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  7374. 'id' => 'UserFormStuff', 'maxlength' => 10
  7375. ),
  7376. '/div'
  7377. );
  7378. $this->assertTags($result, $expected);
  7379. }
  7380. /**
  7381. * testForMagicInputNonExistingNorValidated method
  7382. *
  7383. * @return void
  7384. */
  7385. public function testForMagicInputNonExistingNorValidated() {
  7386. $encoding = strtolower(Configure::read('App.encoding'));
  7387. $result = $this->Form->create('Contact');
  7388. $expected = array(
  7389. 'form' => array(
  7390. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7391. 'accept-charset' => $encoding
  7392. ),
  7393. 'div' => array('style' => 'display:none;'),
  7394. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7395. '/div'
  7396. );
  7397. $this->assertTags($result, $expected);
  7398. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7399. $expected = array(
  7400. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7401. 'Non Existing Nor Validated',
  7402. '/label',
  7403. 'input' => array(
  7404. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7405. 'id' => 'ContactNonExistingNorValidated'
  7406. )
  7407. );
  7408. $this->assertTags($result, $expected);
  7409. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  7410. 'div' => false, 'value' => 'my value'
  7411. ));
  7412. $expected = array(
  7413. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7414. 'Non Existing Nor Validated',
  7415. '/label',
  7416. 'input' => array(
  7417. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7418. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  7419. )
  7420. );
  7421. $this->assertTags($result, $expected);
  7422. $this->Form->request->data = array(
  7423. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  7424. ));
  7425. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7426. $expected = array(
  7427. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7428. 'Non Existing Nor Validated',
  7429. '/label',
  7430. 'input' => array(
  7431. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  7432. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  7433. )
  7434. );
  7435. $this->assertTags($result, $expected);
  7436. }
  7437. /**
  7438. * testFormMagicInputLabel method
  7439. *
  7440. * @return void
  7441. */
  7442. public function testFormMagicInputLabel() {
  7443. $encoding = strtolower(Configure::read('App.encoding'));
  7444. $result = $this->Form->create('Contact');
  7445. $expected = array(
  7446. 'form' => array(
  7447. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7448. 'accept-charset' => $encoding
  7449. ),
  7450. 'div' => array('style' => 'display:none;'),
  7451. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7452. '/div'
  7453. );
  7454. $this->assertTags($result, $expected);
  7455. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  7456. $this->assertTags($result, array('input' => array(
  7457. 'name' => 'data[Contact][name]', 'type' => 'text',
  7458. 'id' => 'ContactName', 'maxlength' => '255')
  7459. ));
  7460. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  7461. $expected = array(
  7462. 'label' => array('for' => 'ContactName'),
  7463. 'My label',
  7464. '/label',
  7465. 'input' => array(
  7466. 'type' => 'text', 'name' => 'data[Contact][name]',
  7467. 'id' => 'ContactName', 'maxlength' => '255'
  7468. )
  7469. );
  7470. $this->assertTags($result, $expected);
  7471. $result = $this->Form->input('Contact.name', array(
  7472. 'div' => false, 'label' => array('class' => 'mandatory')
  7473. ));
  7474. $expected = array(
  7475. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7476. 'Name',
  7477. '/label',
  7478. 'input' => array(
  7479. 'type' => 'text', 'name' => 'data[Contact][name]',
  7480. 'id' => 'ContactName', 'maxlength' => '255'
  7481. )
  7482. );
  7483. $this->assertTags($result, $expected);
  7484. $result = $this->Form->input('Contact.name', array(
  7485. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  7486. ));
  7487. $expected = array(
  7488. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7489. 'My label',
  7490. '/label',
  7491. 'input' => array(
  7492. 'type' => 'text', 'name' => 'data[Contact][name]',
  7493. 'id' => 'ContactName', 'maxlength' => '255'
  7494. )
  7495. );
  7496. $this->assertTags($result, $expected);
  7497. $result = $this->Form->input('Contact.name', array(
  7498. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  7499. ));
  7500. $expected = array(
  7501. 'label' => array('for' => 'my_id'),
  7502. 'Name',
  7503. '/label',
  7504. 'input' => array(
  7505. 'type' => 'text', 'name' => 'data[Contact][name]',
  7506. 'id' => 'my_id', 'maxlength' => '255'
  7507. )
  7508. );
  7509. $this->assertTags($result, $expected);
  7510. $result = $this->Form->input('1.id');
  7511. $this->assertTags($result, array('input' => array(
  7512. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7513. 'id' => 'Contact1Id'
  7514. )));
  7515. $result = $this->Form->input("1.name");
  7516. $expected = array(
  7517. 'div' => array('class' => 'input text'),
  7518. 'label' => array('for' => 'Contact1Name'),
  7519. 'Name',
  7520. '/label',
  7521. 'input' => array(
  7522. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  7523. 'id' => 'Contact1Name', 'maxlength' => '255'
  7524. ),
  7525. '/div'
  7526. );
  7527. $this->assertTags($result, $expected);
  7528. $result = $this->Form->input('Contact.1.id');
  7529. $this->assertTags($result, array(
  7530. 'input' => array(
  7531. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7532. 'id' => 'Contact1Id'
  7533. )
  7534. ));
  7535. $result = $this->Form->input("Model.1.name");
  7536. $expected = array(
  7537. 'div' => array('class' => 'input text'),
  7538. 'label' => array('for' => 'Model1Name'),
  7539. 'Name',
  7540. '/label',
  7541. 'input' => array(
  7542. 'type' => 'text', 'name' => 'data[Model][1][name]',
  7543. 'id' => 'Model1Name'
  7544. ),
  7545. '/div'
  7546. );
  7547. $this->assertTags($result, $expected);
  7548. }
  7549. /**
  7550. * testFormEnd method
  7551. *
  7552. * @return void
  7553. */
  7554. public function testFormEnd() {
  7555. $this->assertEquals('</form>', $this->Form->end());
  7556. $result = $this->Form->end('');
  7557. $expected = array(
  7558. 'div' => array('class' => 'submit'),
  7559. 'input' => array('type' => 'submit', 'value' => ''),
  7560. '/div',
  7561. '/form'
  7562. );
  7563. $this->assertTags($result, $expected);
  7564. $result = $this->Form->end(array('label' => ''));
  7565. $expected = array(
  7566. 'div' => array('class' => 'submit'),
  7567. 'input' => array('type' => 'submit', 'value' => ''),
  7568. '/div',
  7569. '/form'
  7570. );
  7571. $this->assertTags($result, $expected);
  7572. $result = $this->Form->end('save');
  7573. $expected = array(
  7574. 'div' => array('class' => 'submit'),
  7575. 'input' => array('type' => 'submit', 'value' => 'save'),
  7576. '/div',
  7577. '/form'
  7578. );
  7579. $this->assertTags($result, $expected);
  7580. $result = $this->Form->end(array('label' => 'save'));
  7581. $expected = array(
  7582. 'div' => array('class' => 'submit'),
  7583. 'input' => array('type' => 'submit', 'value' => 'save'),
  7584. '/div',
  7585. '/form'
  7586. );
  7587. $this->assertTags($result, $expected);
  7588. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  7589. $expected = array(
  7590. 'div' => array('class' => 'submit'),
  7591. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7592. '/div',
  7593. '/form'
  7594. );
  7595. $this->assertTags($result, $expected);
  7596. $result = $this->Form->end(array('name' => 'Whatever'));
  7597. $expected = array(
  7598. 'div' => array('class' => 'submit'),
  7599. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  7600. '/div',
  7601. '/form'
  7602. );
  7603. $this->assertTags($result, $expected);
  7604. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  7605. $expected = array(
  7606. 'div' => array('class' => 'good'),
  7607. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7608. '/div',
  7609. '/form'
  7610. );
  7611. $this->assertTags($result, $expected);
  7612. $result = $this->Form->end(array(
  7613. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  7614. ));
  7615. $expected = array(
  7616. 'div' => array('class' => 'good'),
  7617. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7618. '/div',
  7619. '/form'
  7620. );
  7621. $this->assertTags($result, $expected);
  7622. }
  7623. /**
  7624. * testMultipleFormWithIdFields method
  7625. *
  7626. * @return void
  7627. */
  7628. public function testMultipleFormWithIdFields() {
  7629. $this->Form->create('UserForm');
  7630. $result = $this->Form->input('id');
  7631. $this->assertTags($result, array('input' => array(
  7632. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  7633. )));
  7634. $result = $this->Form->input('ValidateItem.id');
  7635. $this->assertTags($result, array('input' => array(
  7636. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  7637. 'id' => 'ValidateItemId'
  7638. )));
  7639. $result = $this->Form->input('ValidateUser.id');
  7640. $this->assertTags($result, array('input' => array(
  7641. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  7642. 'id' => 'ValidateUserId'
  7643. )));
  7644. }
  7645. /**
  7646. * testDbLessModel method
  7647. *
  7648. * @return void
  7649. */
  7650. public function testDbLessModel() {
  7651. $this->Form->create('TestMail');
  7652. $result = $this->Form->input('name');
  7653. $expected = array(
  7654. 'div' => array('class' => 'input text'),
  7655. 'label' => array('for' => 'TestMailName'),
  7656. 'Name',
  7657. '/label',
  7658. 'input' => array(
  7659. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7660. 'id' => 'TestMailName'
  7661. ),
  7662. '/div'
  7663. );
  7664. $this->assertTags($result, $expected);
  7665. ClassRegistry::init('TestMail');
  7666. $this->Form->create('TestMail');
  7667. $result = $this->Form->input('name');
  7668. $expected = array(
  7669. 'div' => array('class' => 'input text'),
  7670. 'label' => array('for' => 'TestMailName'),
  7671. 'Name',
  7672. '/label',
  7673. 'input' => array(
  7674. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7675. 'id' => 'TestMailName'
  7676. ),
  7677. '/div'
  7678. );
  7679. $this->assertTags($result, $expected);
  7680. }
  7681. /**
  7682. * testBrokenness method
  7683. *
  7684. * @return void
  7685. */
  7686. public function testBrokenness() {
  7687. /*
  7688. * #4 This test has two parents and four children. By default (as of r7117) both
  7689. * parents are show but the first parent is missing a child. This is the inconsistency
  7690. * in the default behaviour - one parent has all children, the other does not - dependent
  7691. * on the data values.
  7692. */
  7693. $result = $this->Form->select('Model.field', array(
  7694. 'Fred' => array(
  7695. 'freds_son_1' => 'Fred',
  7696. 'freds_son_2' => 'Freddie'
  7697. ),
  7698. 'Bert' => array(
  7699. 'berts_son_1' => 'Albert',
  7700. 'berts_son_2' => 'Bertie')
  7701. ),
  7702. array('showParents' => true, 'empty' => false)
  7703. );
  7704. $expected = array(
  7705. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7706. array('optgroup' => array('label' => 'Fred')),
  7707. array('option' => array('value' => 'freds_son_1')),
  7708. 'Fred',
  7709. '/option',
  7710. array('option' => array('value' => 'freds_son_2')),
  7711. 'Freddie',
  7712. '/option',
  7713. '/optgroup',
  7714. array('optgroup' => array('label' => 'Bert')),
  7715. array('option' => array('value' => 'berts_son_1')),
  7716. 'Albert',
  7717. '/option',
  7718. array('option' => array('value' => 'berts_son_2')),
  7719. 'Bertie',
  7720. '/option',
  7721. '/optgroup',
  7722. '/select'
  7723. );
  7724. $this->assertTags($result, $expected);
  7725. /*
  7726. * #2 This is structurally identical to the test above (#1) - only the parent name has
  7727. * changed, so we should expect the same select list data, just with a different name
  7728. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  7729. * This is where data corruption can occur, because when a select value is missing from
  7730. * a list a form will substitute the first value in the list - without the user knowing.
  7731. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  7732. * affect the availability of 3 => 'Three' as a valid option.
  7733. */
  7734. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  7735. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  7736. ));
  7737. $result = $this->Form->select(
  7738. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  7739. );
  7740. $expected = array(
  7741. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7742. array('option' => array('value' => 1)),
  7743. 'One',
  7744. '/option',
  7745. array('option' => array('value' => 2)),
  7746. 'Two',
  7747. '/option',
  7748. array('optgroup' => array('label' => 'Three')),
  7749. array('option' => array('value' => 3)),
  7750. 'Three',
  7751. '/option',
  7752. array('option' => array('value' => 4)),
  7753. 'Four',
  7754. '/option',
  7755. array('option' => array('value' => 5)),
  7756. 'Five',
  7757. '/option',
  7758. '/optgroup',
  7759. '/select'
  7760. );
  7761. $this->assertTags($result, $expected);
  7762. }
  7763. /**
  7764. * Test the generation of fields for a multi record form.
  7765. *
  7766. * @return void
  7767. */
  7768. public function testMultiRecordForm() {
  7769. $this->Form->create('ValidateProfile');
  7770. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  7771. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  7772. $expected = array(
  7773. 'div' => array('class' => 'input textarea'),
  7774. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  7775. 'Name',
  7776. '/label',
  7777. 'textarea' => array(
  7778. 'id' => 'ValidateProfile1ValidateItem2Name',
  7779. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  7780. 'cols' => 30,
  7781. 'rows' => 6
  7782. ),
  7783. 'Value',
  7784. '/textarea',
  7785. '/div'
  7786. );
  7787. $this->assertTags($result, $expected);
  7788. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
  7789. $expected = array(
  7790. 'div' => array('class' => 'input date'),
  7791. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7792. 'Created',
  7793. '/label',
  7794. array('select' => array(
  7795. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  7796. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7797. )
  7798. ),
  7799. array('option' => array('value' => '')), '/option',
  7800. $this->dateRegex['monthsRegex'],
  7801. '/select', '-',
  7802. array('select' => array(
  7803. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  7804. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7805. )
  7806. ),
  7807. array('option' => array('value' => '')), '/option',
  7808. $this->dateRegex['daysRegex'],
  7809. '/select', '-',
  7810. array('select' => array(
  7811. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  7812. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7813. )
  7814. ),
  7815. array('option' => array('value' => '')), '/option',
  7816. $this->dateRegex['yearsRegex'],
  7817. '/select',
  7818. '/div'
  7819. );
  7820. $this->assertTags($result, $expected);
  7821. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7822. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7823. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7824. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7825. $expected = array(
  7826. 'div' => array('class' => 'input select error'),
  7827. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7828. 'Profile',
  7829. '/label',
  7830. 'select' => array(
  7831. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7832. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7833. 'class' => 'form-error'
  7834. ),
  7835. '/select',
  7836. array('div' => array('class' => 'error-message')),
  7837. 'Error',
  7838. '/div',
  7839. '/div'
  7840. );
  7841. $this->assertTags($result, $expected);
  7842. }
  7843. /**
  7844. * test the correct display of multi-record form validation errors.
  7845. *
  7846. * @return void
  7847. */
  7848. public function testMultiRecordFormValidationErrors() {
  7849. $this->Form->create('ValidateProfile');
  7850. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7851. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7852. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7853. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7854. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7855. $result = $this->Form->error('ValidateProfile.2.city');
  7856. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7857. $result = $this->Form->error('2.city');
  7858. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7859. }
  7860. /**
  7861. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7862. *
  7863. * @return void
  7864. */
  7865. public function testInputTemplate() {
  7866. $result = $this->Form->input('Contact.email', array(
  7867. 'type' => 'text', 'format' => array('input')
  7868. ));
  7869. $expected = array(
  7870. 'div' => array('class' => 'input text'),
  7871. 'input' => array(
  7872. 'type' => 'text', 'name' => 'data[Contact][email]',
  7873. 'id' => 'ContactEmail'
  7874. ),
  7875. '/div'
  7876. );
  7877. $this->assertTags($result, $expected);
  7878. $result = $this->Form->input('Contact.email', array(
  7879. 'type' => 'text', 'format' => array('input', 'label'),
  7880. 'label' => '<em>Email (required)</em>'
  7881. ));
  7882. $expected = array(
  7883. 'div' => array('class' => 'input text'),
  7884. array('input' => array(
  7885. 'type' => 'text', 'name' => 'data[Contact][email]',
  7886. 'id' => 'ContactEmail'
  7887. )),
  7888. 'label' => array('for' => 'ContactEmail'),
  7889. 'em' => array(),
  7890. 'Email (required)',
  7891. '/em',
  7892. '/label',
  7893. '/div'
  7894. );
  7895. $this->assertTags($result, $expected);
  7896. $result = $this->Form->input('Contact.email', array(
  7897. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7898. 'between' => '<div>Something in the middle</div>',
  7899. 'after' => '<span>Some text at the end</span>'
  7900. ));
  7901. $expected = array(
  7902. 'div' => array('class' => 'input text'),
  7903. array('input' => array(
  7904. 'type' => 'text', 'name' => 'data[Contact][email]',
  7905. 'id' => 'ContactEmail'
  7906. )),
  7907. array('div' => array()),
  7908. 'Something in the middle',
  7909. '/div',
  7910. 'label' => array('for' => 'ContactEmail'),
  7911. 'Email',
  7912. '/label',
  7913. 'span' => array(),
  7914. 'Some text at the end',
  7915. '/span',
  7916. '/div'
  7917. );
  7918. $this->assertTags($result, $expected);
  7919. $result = $this->Form->input('Contact.method', array(
  7920. 'type' => 'radio',
  7921. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7922. 'between' => 'I am between',
  7923. ));
  7924. $expected = array(
  7925. 'div' => array('class' => 'input radio'),
  7926. 'fieldset' => array(),
  7927. 'legend' => array(),
  7928. 'Method',
  7929. '/legend',
  7930. 'I am between',
  7931. 'input' => array(
  7932. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  7933. 'value' => '', 'id' => 'ContactMethod_'
  7934. ),
  7935. array('input' => array(
  7936. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7937. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7938. )),
  7939. array('label' => array('for' => 'ContactMethodEmail')),
  7940. 'Email',
  7941. '/label',
  7942. array('input' => array(
  7943. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7944. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7945. )),
  7946. array('label' => array('for' => 'ContactMethodPigeon')),
  7947. 'Pigeon',
  7948. '/label',
  7949. '/fieldset',
  7950. '/div',
  7951. );
  7952. $this->assertTags($result, $expected);
  7953. }
  7954. /**
  7955. * test that some html5 inputs + FormHelper::__call() work
  7956. *
  7957. * @return void
  7958. */
  7959. public function testHtml5Inputs() {
  7960. $result = $this->Form->email('User.email');
  7961. $expected = array(
  7962. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  7963. );
  7964. $this->assertTags($result, $expected);
  7965. $result = $this->Form->search('User.query');
  7966. $expected = array(
  7967. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  7968. );
  7969. $this->assertTags($result, $expected);
  7970. $result = $this->Form->search('User.query', array('value' => 'test'));
  7971. $expected = array(
  7972. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7973. );
  7974. $this->assertTags($result, $expected);
  7975. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7976. $expected = array(
  7977. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7978. );
  7979. $this->assertTags($result, $expected);
  7980. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  7981. $expected = array(
  7982. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7983. );
  7984. $this->assertTags($result, $expected);
  7985. }
  7986. /**
  7987. *
  7988. * @expectedException CakeException
  7989. * @return void
  7990. */
  7991. public function testHtml5InputException() {
  7992. $this->Form->email();
  7993. }
  7994. /**
  7995. * Tests that a model can be loaded from the model names passed in the request object
  7996. *
  7997. * @return void
  7998. */
  7999. public function testIntrospectModelFromRequest() {
  8000. $this->loadFixtures('Post');
  8001. App::build(array(
  8002. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  8003. ));
  8004. CakePlugin::load('TestPlugin');
  8005. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  8006. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  8007. $this->Form->create('TestPluginPost');
  8008. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  8009. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  8010. CakePlugin::unload();
  8011. App::build();
  8012. }
  8013. /**
  8014. * Tests that it is possible to set the validation errors directly in the helper for a field
  8015. *
  8016. * @return void
  8017. */
  8018. public function testCustomValidationErrors() {
  8019. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  8020. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  8021. $this->assertEquals('Badness!', $result);
  8022. }
  8023. /**
  8024. * Tests that the 'on' key validates as expected on create
  8025. *
  8026. * @return void
  8027. */
  8028. public function testRequiredOnCreate() {
  8029. $this->Form->create('Contact');
  8030. $result = $this->Form->input('Contact.imrequiredonupdate');
  8031. $expected = array(
  8032. 'div' => array('class' => 'input text'),
  8033. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8034. 'Imrequiredonupdate',
  8035. '/label',
  8036. 'input' => array(
  8037. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8038. 'id' => 'ContactImrequiredonupdate'
  8039. ),
  8040. '/div'
  8041. );
  8042. $this->assertTags($result, $expected);
  8043. $result = $this->Form->input('Contact.imrequiredoncreate');
  8044. $expected = array(
  8045. 'div' => array('class' => 'input text required'),
  8046. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8047. 'Imrequiredoncreate',
  8048. '/label',
  8049. 'input' => array(
  8050. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8051. 'id' => 'ContactImrequiredoncreate',
  8052. 'required' => 'required'
  8053. ),
  8054. '/div'
  8055. );
  8056. $this->assertTags($result, $expected);
  8057. $result = $this->Form->input('Contact.imrequiredonboth');
  8058. $expected = array(
  8059. 'div' => array('class' => 'input text required'),
  8060. 'label' => array('for' => 'ContactImrequiredonboth'),
  8061. 'Imrequiredonboth',
  8062. '/label',
  8063. 'input' => array(
  8064. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8065. 'id' => 'ContactImrequiredonboth',
  8066. 'required' => 'required'
  8067. ),
  8068. '/div'
  8069. );
  8070. $this->assertTags($result, $expected);
  8071. $this->Form->inputDefaults(array('required' => false));
  8072. $result = $this->Form->input('Contact.imrequired');
  8073. $expected = array(
  8074. 'div' => array('class' => 'input text'),
  8075. 'label' => array('for' => 'ContactImrequired'),
  8076. 'Imrequired',
  8077. '/label',
  8078. 'input' => array(
  8079. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8080. 'id' => 'ContactImrequired'
  8081. ),
  8082. '/div'
  8083. );
  8084. $this->assertTags($result, $expected);
  8085. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  8086. $this->assertTags($result, $expected);
  8087. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  8088. $expected = array(
  8089. 'div' => array('class' => 'input text required'),
  8090. 'label' => array('for' => 'ContactImrequired'),
  8091. 'Imrequired',
  8092. '/label',
  8093. 'input' => array(
  8094. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8095. 'id' => 'ContactImrequired'
  8096. ),
  8097. '/div'
  8098. );
  8099. $this->assertTags($result, $expected);
  8100. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  8101. $this->assertTags($result, $expected);
  8102. }
  8103. /**
  8104. * Tests that the 'on' key validates as expected on update
  8105. *
  8106. * @return void
  8107. */
  8108. public function testRequiredOnUpdate() {
  8109. $this->Form->request->data['Contact']['id'] = 1;
  8110. $this->Form->create('Contact');
  8111. $result = $this->Form->input('Contact.imrequiredonupdate');
  8112. $expected = array(
  8113. 'div' => array('class' => 'input text required'),
  8114. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8115. 'Imrequiredonupdate',
  8116. '/label',
  8117. 'input' => array(
  8118. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8119. 'id' => 'ContactImrequiredonupdate',
  8120. 'required' => 'required'
  8121. ),
  8122. '/div'
  8123. );
  8124. $this->assertTags($result, $expected);
  8125. $result = $this->Form->input('Contact.imrequiredoncreate');
  8126. $expected = array(
  8127. 'div' => array('class' => 'input text'),
  8128. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8129. 'Imrequiredoncreate',
  8130. '/label',
  8131. 'input' => array(
  8132. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8133. 'id' => 'ContactImrequiredoncreate'
  8134. ),
  8135. '/div'
  8136. );
  8137. $this->assertTags($result, $expected);
  8138. $result = $this->Form->input('Contact.imrequiredonboth');
  8139. $expected = array(
  8140. 'div' => array('class' => 'input text required'),
  8141. 'label' => array('for' => 'ContactImrequiredonboth'),
  8142. 'Imrequiredonboth',
  8143. '/label',
  8144. 'input' => array(
  8145. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8146. 'id' => 'ContactImrequiredonboth',
  8147. 'required' => 'required'
  8148. ),
  8149. '/div'
  8150. );
  8151. $this->assertTags($result, $expected);
  8152. $result = $this->Form->input('Contact.imrequired');
  8153. $expected = array(
  8154. 'div' => array('class' => 'input text required'),
  8155. 'label' => array('for' => 'ContactImrequired'),
  8156. 'Imrequired',
  8157. '/label',
  8158. 'input' => array(
  8159. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8160. 'id' => 'ContactImrequired',
  8161. 'required' => 'required'
  8162. ),
  8163. '/div'
  8164. );
  8165. $this->assertTags($result, $expected);
  8166. }
  8167. /**
  8168. * Test inputDefaults setter and getter
  8169. *
  8170. * @return void
  8171. */
  8172. public function testInputDefaults() {
  8173. $this->Form->create('Contact');
  8174. $this->Form->inputDefaults(array(
  8175. 'label' => false,
  8176. 'div' => array(
  8177. 'style' => 'color: #000;'
  8178. )
  8179. ));
  8180. $result = $this->Form->input('Contact.field1');
  8181. $expected = array(
  8182. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  8183. 'input' => array(
  8184. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8185. 'id' => 'ContactField1'
  8186. ),
  8187. '/div'
  8188. );
  8189. $this->assertTags($result, $expected);
  8190. $this->Form->inputDefaults(array(
  8191. 'div' => false,
  8192. 'label' => 'Label',
  8193. ));
  8194. $result = $this->Form->input('Contact.field1');
  8195. $expected = array(
  8196. 'label' => array('for' => 'ContactField1'),
  8197. 'Label',
  8198. '/label',
  8199. 'input' => array(
  8200. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8201. 'id' => 'ContactField1'
  8202. ),
  8203. );
  8204. $this->assertTags($result, $expected);
  8205. $this->Form->inputDefaults(array(
  8206. 'label' => false,
  8207. ), true);
  8208. $result = $this->Form->input('Contact.field1');
  8209. $expected = array(
  8210. 'input' => array(
  8211. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8212. 'id' => 'ContactField1'
  8213. ),
  8214. );
  8215. $this->assertTags($result, $expected);
  8216. $result = $this->Form->inputDefaults();
  8217. $expected = array(
  8218. 'div' => false,
  8219. 'label' => false,
  8220. );
  8221. $this->assertEqual($result, $expected);
  8222. }
  8223. }